tagging as ATutor 1.5.4-release
[atutor.git] / tools / tests / questions.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 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=15.6.add_questions.php';
30
31
32 $tid = intval($_REQUEST['tid']);
33
34 if (isset($_POST['submit'])) {
35         // check if we own this tid:
36         $sql    = "SELECT test_id, random FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
37         $result = mysql_query($sql, $db);
38         if ($row = mysql_fetch_assoc($result)) {
39                 /*
40                 // For #1760
41                 //check that randomized questions are all same weight.
42                 $randomized_question_weight = -1;
43                 foreach ($_POST['weight'] as $qid => $weight) {
44                         $weight = $addslashes($weight);
45                         if ($_POST['required'][$qid]) {
46                                 // do nothing
47                         } else {
48                                 if ($randomized_question_weight == -1) {
49                                         // if first time through this loop.
50                                         $randomized_question_weight = $weight;
51                                 } else if ($randomized_question_weight != $weight) {
52                                         // The values of two non-required questions are not equal.
53                                         $msg->addError ("NON_REQUIRED_QUESTION_WEIGHT");
54                                         header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
55                                         exit;
56                                 } else {
57                                         // The values of non-required questions are equal so far.
58                                 }
59                         }
60                 }
61                 */
62
63                 //update the weights & order
64                 $total_weight = 0;
65                 $count = 1;
66                 foreach ($_POST['weight'] as $qid => $weight) {
67                         $weight = $addslashes($weight);
68                         if ($_POST['required'][$qid]) {
69                                 $required = 1;
70                         } else {
71                                 $required = 0;
72                         }
73                         
74                         if (!$row['random']) {
75                                 $orders = $_POST['ordering'];
76                                 asort($orders);
77                                 $orders = array_keys($orders);
78                                 $orders = array_flip($orders);
79                                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=".($orders[$qid]+1)." WHERE question_id=$qid AND test_id=".$tid;
80                         } else {
81                                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=$count WHERE question_id=$qid AND test_id=".$tid;
82                         }
83
84                         $result = mysql_query($sql, $db);
85                         $total_weight += $weight;
86                         $count++;
87                 }
88
89                 $sql    = "UPDATE ".TABLE_PREFIX."tests SET out_of='$total_weight' WHERE test_id=$tid";
90                 $result = mysql_query($sql, $db);
91         }
92         $total_weight = 0;
93         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
94         header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
95         exit;
96 }
97
98 require(AT_INCLUDE_PATH.'header.inc.php');
99
100 $sql    = "SELECT title, random FROM ".TABLE_PREFIX."tests WHERE test_id=$tid";
101 $result = mysql_query($sql, $db);
102 $row    = mysql_fetch_assoc($result);
103 echo '<h3>'._AT('questions_for').' '.AT_print($row['title'], 'tests.title').'</h3>';
104 $random = $row['random'];
105
106 $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";
107 $result = mysql_query($sql, $db);
108 $row = mysql_fetch_array($result);
109 if ($row['cnt']) {
110         $msg->printWarnings('QUESTION_WEIGHT');
111 }
112
113 $msg->printAll();
114
115 $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";
116 $result = mysql_query($sql, $db);
117
118 ?>
119 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
120 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
121 <table class="data static" summary="" rules="cols">
122 <thead>
123 <tr>
124         <th scope="col"><?php echo _AT('num');      ?></th>
125         <th scope="col"><?php echo _AT('points');   ?></th>
126         <th scope="col"><?php echo _AT('order'); ?></th>
127         <th scope="col"><?php echo _AT('question'); ?></th>
128         <th scope="col"><?php echo _AT('type');     ?></th>
129         <th scope="col"><?php echo _AT('category'); ?></th>
130         <?php if ($random): ?>
131                 <th scope="col"><?php echo _AT('required'); ?></th>
132         <?php endif; ?>
133         <th scope="col">&nbsp;</th>
134 </tr>
135 </thead>
136 <?php
137 if ($row = mysql_fetch_assoc($result)) {
138         $sql    = "SELECT title, category_id FROM ".TABLE_PREFIX."tests_questions_categories WHERE course_id=".$_SESSION['course_id'];
139         $cat_result     = mysql_query($sql, $db);
140         $cats    = array();
141         $cats[0] = _AT('cats_uncategorized');
142         while ($cat_row = mysql_fetch_assoc($cat_result)) {
143                 $cats[$cat_row['category_id']] = $cat_row['title'];
144         }
145
146         do {
147                 $total_weight += $row['weight'];
148                 $count++;
149                 echo '<tr>';
150                 echo '<td class="row1" align="center"><strong>'.$count.'</strong></td>';
151                 echo '<td class="row1" align="center">';
152                 
153                 if ($row['type'] == 4) {
154                         echo ''._AT('na').'';
155                         echo '<input type="hidden" value="0" name="weight['.$row['question_id'].']" />';
156                 } else {
157                         echo '<input type="text" value="'.$row['weight'].'" name="weight['.$row['question_id'].']" size="2" />';
158                 }
159                 echo '</td>';
160
161                 if ($random) {
162                         echo '<td class="row1" align="center">'._AT('na').'</td>';
163                 } else {
164                         echo '<td class="row1" align="center"><input type="text" name="ordering['.$row['question_id'].']" value="'.$row['ordering'].'" size="2" /></td>';
165                 }
166
167                 echo '<td class="row1">';
168                 if (strlen($row['question']) > 45) {
169                         echo htmlspecialchars(AT_print(substr($row['question'], 0, 43), 'tests_questions.question')) . '...';
170                 } else {
171                         echo AT_print(htmlspecialchars($row['question']), 'tests_questions.question');
172                 }
173
174                 echo '</td>';
175                 echo '<td nowrap="nowrap">';
176                 $o = TestQuestions::getQuestion($row['type']);
177                 echo $o->printName();
178                 echo '</td>';
179
180                 $link = 'tools/tests/edit_question_'.$o->getPrefix().'.php?tid='.$tid.SEP.'qid='.$row['question_id'];
181
182                 echo '<td align="center">'.$cats[$row['category_id']].'</td>';
183
184                 if ($random) {
185                         echo '<td align="center" nowrap="nowrap"><input type="checkbox" name="required['.$row['question_id'].']" value="1"';
186                         if ($row['required']) {
187                                 echo ' checked="checked"';
188                         }
189                         echo ' id="q'.$row['question_id'].'" /><label for="q'.$row['question_id'].'">'._AT('required').'</label></td>';
190                 }
191
192                 echo '<td nowrap="nowrap">';
193                 echo '<a href="' . $link . '">' . _AT('edit').'</a> | ';
194                 echo '<a href="tools/tests/question_remove.php?tid=' . $tid . SEP . 'qid=' . $row['question_id'] . '">' . _AT('remove') . '</a>';
195                 echo '</td>';
196
197                 echo '</tr>';
198         } while ($row = mysql_fetch_assoc($result));
199
200         //total weight
201         echo '<tfoot>';
202         echo '<tr><td>&nbsp;</td>';
203         echo '<td align="center" nowrap="nowrap"><strong>'._AT('total').':</strong> '.$total_weight.'</td>';
204         echo '<td colspan="';
205         if ($random) {
206                 echo 5;
207         } else {
208                 echo 4;
209         }
210
211         echo '" align="left" nowrap="nowrap">';
212         echo '<input type="submit" value="'._AT('update').'" name="submit" /> </td>';
213         echo '</tr>';
214         echo '</tfoot>';
215 } else {
216         echo '<tr><td colspan="';
217         if ($random) {
218                 echo 7;
219         } else {
220                 echo 6;
221         }
222
223         echo '" >'._AT('none_found').'</td></tr>';
224 }
225
226 echo '</table><br /></form>';
227
228 require(AT_INCLUDE_PATH.'footer.inc.php');
229 ?>