2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
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 /************************************************************************/
16 define('AT_INCLUDE_PATH', '../../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18 require(AT_INCLUDE_PATH.'classes/testQuestions.class.php');
20 authenticate(AT_PRIV_TESTS);
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']);
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'];
29 $_pages['tools/tests/questions.php']['guide'] = 'instructor/?p=add_questions.php';
32 $tid = intval($_REQUEST['tid']);
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; }
40 // for each question that isn't required
42 foreach ($_POST['weight'] as $qid => $weight) {
43 if ($_POST['required'][$qid]) { continue; }
44 if (!$current_weight) { $current_weight = $weight; }
46 if ($current_weight != $weight) {
47 // the weights aren't the same.
48 $msg->addError('RAND_TEST_Q_WEIGHT');
54 if (!$msg->containsErrors()) {
55 //update the weights & order
57 $total_required_weight = 0;
58 $total_required_num = 0;
61 foreach ($_POST['weight'] as $qid => $weight) {
63 $weight = intval($weight);
64 if ($_POST['required'][$qid]) {
72 $total_required_weight += $weight;
73 $total_required_num++;
75 $optional_weight = $weight; // what each optional question weights.
78 $total_weight += $weight; // not random, so just sum the weights
81 if (!$row['random']) {
82 $orders = $_POST['ordering'];
84 $orders = array_keys($orders);
86 foreach ($orders as $k => $id)
87 $orders[$k] = intval($id);
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;
92 $sql = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=$count WHERE question_id=$qid AND test_id=".$tid;
95 $result = mysql_query($sql, $db);
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'];
106 $total_weight = $total_required_weight; // there are no more optional questions
107 $num_questions_sql = ', num_questions='.$total_required_num;
112 $sql = "UPDATE ".TABLE_PREFIX."tests SET out_of='$total_weight' $num_questions_sql WHERE test_id=$tid";
113 $result = mysql_query($sql, $db);
116 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
117 header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
122 require(AT_INCLUDE_PATH.'header.inc.php');
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'];
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);
134 $msg->printWarnings('QUESTION_WEIGHT');
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);
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">
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>
157 <th scope="col"> </th>
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);
165 $cats[0] = _AT('cats_uncategorized');
166 while ($cat_row = mysql_fetch_assoc($cat_result)) {
167 $cats[$cat_row['category_id']] = $cat_row['title'];
173 echo '<td class="row1" align="center"><strong>'.$count.'</strong></td>';
174 echo '<td class="row1" align="center">';
176 if (isset($_POST['submit'])) {
177 $row['weight'] = $_POST['weight'][$row['question_id']];
178 $row['required'] = (isset($_POST['required'][$row['question_id']]) ? 1 : 0);
181 if ($row['type'] == 4) {
182 echo ''._AT('na').'';
183 echo '<input type="hidden" value="0" name="weight['.$row['question_id'].']" />';
185 echo '<input type="text" value="'.$row['weight'].'" name="weight['.$row['question_id'].']" size="2" />';
190 echo '<td class="row1" align="center">'._AT('na').'</td>';
192 echo '<td class="row1" align="center"><input type="text" name="ordering['.$row['question_id'].']" value="'.$row['ordering'].'" size="2" /></td>';
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") . '...';
199 echo AT_print(htmlspecialchars($row['question'], ENT_COMPAT, "UTF-8"), 'tests_questions.question');
203 echo '<td nowrap="nowrap">';
204 $o = TestQuestions::getQuestion($row['type']);
205 echo $o->printName();
208 $link = 'tools/tests/edit_question_'.$o->getPrefix().'.php?tid='.$tid.SEP.'qid='.$row['question_id'];
210 echo '<td align="center">'.$cats[$row['category_id']].'</td>';
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"';
217 echo ' id="q'.$row['question_id'].'" /><label for="q'.$row['question_id'].'">'._AT('required').'</label></td>';
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>';
226 } while ($row = mysql_fetch_assoc($result));
230 echo '<tr><td> </td>';
231 echo '<td colspan="';
238 echo '" align="left" nowrap="nowrap">';
239 echo '<input type="submit" value="'._AT('update').'" name="submit" /> </td>';
243 echo '<tr><td colspan="';
250 echo '" >'._AT('none_found').'</td></tr>';
253 echo '</table><br /></form>';
255 require(AT_INCLUDE_PATH.'footer.inc.php');