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