remove old readme
[atutor.git] / mods / _standard / tests / questions.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 $page = 'tests';
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 require(AT_INCLUDE_PATH.'../mods/_standard/tests/classes/testQuestions.class.php');
18
19 authenticate(AT_PRIV_TESTS);
20
21 $_pages['mods/_standard/tests/questions.php']['title_var']    = 'questions';
22 $_pages['mods/_standard/tests/questions.php']['parent']   = 'mods/_standard/tests/index.php';
23 $_pages['mods/_standard/tests/questions.php']['children'] = array('mods/_standard/tests/add_test_questions.php?tid='.$_GET['tid']);
24
25 $_pages['mods/_standard/tests/add_test_questions.php?tid='.$_GET['tid']]['title_var']    = 'add_questions';
26 $_pages['mods/_standard/tests/add_test_questions.php?tid='.$_GET['tid']]['parent']   = 'mods/_standard/tests/questions.php?tid='.$_GET['tid'];
27
28 $_pages['mods/_standard/tests/questions.php']['guide']    = 'instructor/?p=add_questions.php';
29
30
31 $tid = intval($_REQUEST['tid']);
32
33 if (isset($_POST['submit'])) {
34         $sql    = "SELECT test_id, random, num_questions FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
35         $result = mysql_query($sql, $db);
36         if (!($row = mysql_fetch_assoc($result))) { exit; }
37
38         // #1760
39         // for each question that isn't required
40         if ($row['random']) {
41                 foreach ($_POST['weight'] as $qid => $weight) {
42                         if ($_POST['required'][$qid]) { continue; }
43                         if (!$current_weight) { $current_weight = $weight; }
44
45                         if ($current_weight != $weight) {
46                                 // the weights aren't the same.
47                                 $msg->addError('RAND_TEST_Q_WEIGHT');
48                                 break;
49                         }
50                 }
51         }
52
53         if (!$msg->containsErrors()) {
54                 //update the weights & order
55                 $total_weight = 0;
56                 $total_required_weight = 0;
57                 $total_required_num = 0;
58                 $optional_weight = 0;
59                 $count = 1;
60                 foreach ($_POST['weight'] as $qid => $weight) {
61                         $qid    = intval($qid);
62                         $weight = intval($weight);
63                         if ($_POST['required'][$qid]) {
64                                 $required = 1;
65                         } else {
66                                 $required = 0;
67                         }
68
69                         if ($row['random']) {
70                                 if ($required) {
71                                         $total_required_weight += $weight;
72                                         $total_required_num++;
73                                 } else {
74                                         $optional_weight = $weight; // what each optional question weights.
75                                 }
76                         } else {
77                                 $total_weight += $weight; // not random, so just sum the weights
78                         }
79                                 
80                         if (!$row['random']) {
81                                 $orders = $_POST['ordering'];
82                                 asort($orders);
83                                 $orders = array_keys($orders);
84
85                                 foreach ($orders as $k => $id)
86                                         $orders[$k] = intval($id);
87                                         
88                                 $orders = array_flip($orders);
89                                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=".($orders[$qid]+1)." WHERE question_id=$qid AND test_id=".$tid;
90                         } else {
91                                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=$count WHERE question_id=$qid AND test_id=".$tid;
92                         }
93
94                         $result = mysql_query($sql, $db);
95                         $count++;
96                 }
97
98                 $num_questions_sql = '';
99                 if ($row['random']) {
100                         $row['num_questions'] -= $total_required_num;
101                         if ($row['num_questions'] > 0) {
102                                 // how much do the optional questions add up to: (assume they all weight the same)
103                                 $total_weight = $total_required_weight + $optional_weight * $row['num_questions'];
104                         } else {
105                                 $total_weight = $total_required_weight; // there are no more optional questions
106                                 $num_questions_sql = ', num_questions='.$total_required_num;
107                         }
108                 }
109
110
111                 $sql    = "UPDATE ".TABLE_PREFIX."tests SET out_of='$total_weight' $num_questions_sql WHERE test_id=$tid";
112                 $result = mysql_query($sql, $db);
113
114                 $total_weight = 0;
115                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
116                 header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
117                 exit;
118         }
119 }
120
121 require(AT_INCLUDE_PATH.'header.inc.php');
122
123 $sql    = "SELECT title, random FROM ".TABLE_PREFIX."tests WHERE test_id=$tid";
124 $result = mysql_query($sql, $db);
125 $row    = mysql_fetch_assoc($result);
126 echo '<h3>'._AT('questions_for').' '.AT_print($row['title'], 'tests.title').'</h3>';
127 $random = $row['random'];
128
129 $sql    = "SELECT count(*) as cnt FROM ".TABLE_PREFIX."tests_questions_assoc QA, ".TABLE_PREFIX."tests_questions Q WHERE QA.test_id=$tid AND QA.weight=0 AND QA.question_id=Q.question_id AND Q.type<>4";
130 $result = mysql_query($sql, $db);
131 $row = mysql_fetch_array($result);
132 if ($row['cnt']) {
133         $msg->printWarnings('QUESTION_WEIGHT');
134 }
135
136 $msg->printAll();
137
138 $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions Q, ".TABLE_PREFIX."tests_questions_assoc TQ WHERE Q.course_id=$_SESSION[course_id] AND Q.question_id=TQ.question_id AND TQ.test_id=$tid ORDER BY TQ.ordering";
139 $result = mysql_query($sql, $db);
140
141 ?>
142 <script type="text/javascript">
143 //<!--
144 function setAllWeights() {
145     for (var i=0; i<document.form.elements.length;i++) {
146         var e = document.form.elements[i];
147         if ((e.type == 'text') && (e.name.substring(0, 7) == 'weight[')) {
148             e.value = document.form.all_weights.value;
149         }
150     }
151 }
152 //-->
153 </script>
154
155 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?tid=<?php echo $tid; ?>" method="post" name="form">
156 <div class="input-form">
157         <div class="row">
158                 <?php echo _AT('set_all_weights'); ?>
159         </div>
160         <div class="row">
161                 <label for="all_weights"><?php echo _AT('points') . ':'; ?></label>
162                 <input type="text" id="all_weights" name="all_weights" size="2">
163         </div>
164         <div class="row">
165                 <input type="button" class="button" name="set_all_weights" value="<?php echo _AT('set'); ?>" onclick="setAllWeights()">
166         </div>
167 </div>
168
169 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
170 <table class="data static" summary="" rules="rows">
171 <thead>
172 <tr>
173         <th scope="col"><?php echo _AT('num');      ?></th>
174         <th scope="col"><?php echo _AT('points');   ?></th>
175         <th scope="col"><?php echo _AT('order'); ?></th>
176         <th scope="col"><?php echo _AT('question'); ?></th>
177         <th scope="col"><?php echo _AT('type');     ?></th>
178         <th scope="col"><?php echo _AT('category'); ?></th>
179         <?php if ($random): ?>
180                 <th scope="col"><?php echo _AT('required'); ?></th>
181         <?php endif; ?>
182         <th scope="col">&nbsp;</th>
183 </tr>
184 </thead>
185 <?php
186 if ($row = mysql_fetch_assoc($result)) {
187         $sql    = "SELECT title, category_id FROM ".TABLE_PREFIX."tests_questions_categories WHERE course_id=".$_SESSION['course_id'];
188         $cat_result     = mysql_query($sql, $db);
189         $cats    = array();
190         $cats[0] = _AT('cats_uncategorized');
191         while ($cat_row = mysql_fetch_assoc($cat_result)) {
192                 $cats[$cat_row['category_id']] = $cat_row['title'];
193         }
194
195         do {
196                 $count++;
197                 echo '<tr>';
198                 echo '<td class="row1" align="center"><strong>'.$count.'</strong></td>';
199                 echo '<td class="row1" align="center">';
200                 
201                 if (isset($_POST['submit'])) {
202                         $row['weight'] = $_POST['weight'][$row['question_id']];
203                         $row['required'] = (isset($_POST['required'][$row['question_id']]) ? 1 : 0);
204                 }
205
206                 if ($row['type'] == 4) {
207                         echo ''._AT('na').'';
208                         echo '<input type="hidden" value="0" name="weight['.$row['question_id'].']" />';
209                 } else {
210                         echo '<input type="text" value="'.$row['weight'].'" name="weight['.$row['question_id'].']" size="2" />';
211                 }
212                 echo '</td>';
213
214                 if ($random) {
215                         echo '<td class="row1" align="center">'._AT('na').'</td>';
216                 } else {
217                         echo '<td class="row1" align="center"><input type="text" name="ordering['.$row['question_id'].']" value="'.$row['ordering'].'" size="2" /></td>';
218                 }
219
220                 echo '<td class="row1">';
221         echo AT_print(validate_length($row['question'], 45, VALIDATE_LENGTH_FOR_DISPLAY), 'tests_questions.question');
222
223                 echo '</td>';
224                 echo '<td nowrap="nowrap">';
225                 $o = TestQuestions::getQuestion($row['type']);
226                 echo $o->printName();
227                 echo '</td>';
228
229                 $link = 'mods/_standard/tests/edit_question_'.$o->getPrefix().'.php?tid='.$tid.SEP.'qid='.$row['question_id'];
230
231                 echo '<td align="center">'.$cats[$row['category_id']].'</td>';
232
233                 if ($random) {
234                         echo '<td align="center" nowrap="nowrap"><input type="checkbox" name="required['.$row['question_id'].']" value="1"';
235                         if ($row['required']) {
236                                 echo ' checked="checked"';
237                         }
238                         echo ' id="q'.$row['question_id'].'" /><label for="q'.$row['question_id'].'">'._AT('required').'</label></td>';
239                 }
240
241                 echo '<td nowrap="nowrap">';
242                 echo '<a href="' . $link . '">' . _AT('edit').'</a> | ';
243                 echo '<a href="mods/_standard/tests/question_remove.php?tid=' . $tid . SEP . 'qid=' . $row['question_id'] . '">' . _AT('remove') . '</a>';
244                 echo '</td>';
245
246                 echo '</tr>';
247         } while ($row = mysql_fetch_assoc($result));
248
249         //total weight
250         echo '<tfoot>';
251         echo '<tr><td>&nbsp;</td>';
252         echo '<td colspan="';
253         if ($random) {
254                 echo 7;
255         } else {
256                 echo 6;
257         }
258
259         echo '" align="left" nowrap="nowrap">';
260         echo '<input type="submit" value="'._AT('save').'" name="submit" /> </td>';
261         echo '</tr>';
262         echo '</tfoot>';
263 } else {
264         echo '<tr><td colspan="';
265         if ($random) {
266                 echo 7;
267         } else {
268                 echo 6;
269         }
270
271         echo '" >'._AT('none_found').'</td></tr>';
272 }
273
274 echo '</table><br /></form>';
275
276 require(AT_INCLUDE_PATH.'footer.inc.php');
277 ?>