2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 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=15.6.add_questions.php';
32 $tid = intval($_REQUEST['tid']);
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)) {
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]) {
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);
57 // The values of non-required questions are equal so far.
63 //update the weights & order
66 foreach ($_POST['weight'] as $qid => $weight) {
67 $weight = $addslashes($weight);
68 if ($_POST['required'][$qid]) {
74 if (!$row['random']) {
75 $orders = $_POST['ordering'];
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;
81 $sql = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=$count WHERE question_id=$qid AND test_id=".$tid;
84 $result = mysql_query($sql, $db);
85 $total_weight += $weight;
89 $sql = "UPDATE ".TABLE_PREFIX."tests SET out_of='$total_weight' WHERE test_id=$tid";
90 $result = mysql_query($sql, $db);
93 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
94 header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
98 require(AT_INCLUDE_PATH.'header.inc.php');
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'];
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);
110 $msg->printWarnings('QUESTION_WEIGHT');
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);
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">
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>
133 <th scope="col"> </th>
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);
141 $cats[0] = _AT('cats_uncategorized');
142 while ($cat_row = mysql_fetch_assoc($cat_result)) {
143 $cats[$cat_row['category_id']] = $cat_row['title'];
147 $total_weight += $row['weight'];
150 echo '<td class="row1" align="center"><strong>'.$count.'</strong></td>';
151 echo '<td class="row1" align="center">';
153 if ($row['type'] == 4) {
154 echo ''._AT('na').'';
155 echo '<input type="hidden" value="0" name="weight['.$row['question_id'].']" />';
157 echo '<input type="text" value="'.$row['weight'].'" name="weight['.$row['question_id'].']" size="2" />';
162 echo '<td class="row1" align="center">'._AT('na').'</td>';
164 echo '<td class="row1" align="center"><input type="text" name="ordering['.$row['question_id'].']" value="'.$row['ordering'].'" size="2" /></td>';
167 echo '<td class="row1">';
168 if (strlen($row['question']) > 45) {
169 echo htmlspecialchars(AT_print(substr($row['question'], 0, 43), 'tests_questions.question')) . '...';
171 echo AT_print(htmlspecialchars($row['question']), 'tests_questions.question');
175 echo '<td nowrap="nowrap">';
176 $o = TestQuestions::getQuestion($row['type']);
177 echo $o->printName();
180 $link = 'tools/tests/edit_question_'.$o->getPrefix().'.php?tid='.$tid.SEP.'qid='.$row['question_id'];
182 echo '<td align="center">'.$cats[$row['category_id']].'</td>';
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"';
189 echo ' id="q'.$row['question_id'].'" /><label for="q'.$row['question_id'].'">'._AT('required').'</label></td>';
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>';
198 } while ($row = mysql_fetch_assoc($result));
202 echo '<tr><td> </td>';
203 echo '<td align="center" nowrap="nowrap"><strong>'._AT('total').':</strong> '.$total_weight.'</td>';
204 echo '<td colspan="';
211 echo '" align="left" nowrap="nowrap">';
212 echo '<input type="submit" value="'._AT('update').'" name="submit" /> </td>';
216 echo '<tr><td colspan="';
223 echo '" >'._AT('none_found').'</td></tr>';
226 echo '</table><br /></form>';
228 require(AT_INCLUDE_PATH.'footer.inc.php');