2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
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');
19 authenticate(AT_PRIV_TESTS);
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']);
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'];
28 $_pages['mods/_standard/tests/questions.php']['guide'] = 'instructor/?p=add_questions.php';
31 $tid = intval($_REQUEST['tid']);
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; }
39 // for each question that isn't required
41 foreach ($_POST['weight'] as $qid => $weight) {
42 if ($_POST['required'][$qid]) { continue; }
43 if (!$current_weight) { $current_weight = $weight; }
45 if ($current_weight != $weight) {
46 // the weights aren't the same.
47 $msg->addError('RAND_TEST_Q_WEIGHT');
53 if (!$msg->containsErrors()) {
54 //update the weights & order
56 $total_required_weight = 0;
57 $total_required_num = 0;
60 foreach ($_POST['weight'] as $qid => $weight) {
62 $weight = intval($weight);
63 if ($_POST['required'][$qid]) {
71 $total_required_weight += $weight;
72 $total_required_num++;
74 $optional_weight = $weight; // what each optional question weights.
77 $total_weight += $weight; // not random, so just sum the weights
80 if (!$row['random']) {
81 $orders = $_POST['ordering'];
83 $orders = array_keys($orders);
85 foreach ($orders as $k => $id)
86 $orders[$k] = intval($id);
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;
91 $sql = "UPDATE ".TABLE_PREFIX."tests_questions_assoc SET weight=$weight, required=$required, ordering=$count WHERE question_id=$qid AND test_id=".$tid;
94 $result = mysql_query($sql, $db);
98 $num_questions_sql = '';
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'];
105 $total_weight = $total_required_weight; // there are no more optional questions
106 $num_questions_sql = ', num_questions='.$total_required_num;
111 $sql = "UPDATE ".TABLE_PREFIX."tests SET out_of='$total_weight' $num_questions_sql WHERE test_id=$tid";
112 $result = mysql_query($sql, $db);
115 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
116 header('Location: '.$_SERVER['PHP_SELF'] .'?tid='.$tid);
121 require(AT_INCLUDE_PATH.'header.inc.php');
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'];
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);
133 $msg->printWarnings('QUESTION_WEIGHT');
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);
142 <script type="text/javascript">
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;
155 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?tid=<?php echo $tid; ?>" method="post" name="form">
156 <div class="input-form">
158 <?php echo _AT('set_all_weights'); ?>
161 <label for="all_weights"><?php echo _AT('points') . ':'; ?></label>
162 <input type="text" id="all_weights" name="all_weights" size="2">
165 <input type="button" class="button" name="set_all_weights" value="<?php echo _AT('set'); ?>" onclick="setAllWeights()">
169 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
170 <table class="data static" summary="" rules="rows">
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>
182 <th scope="col"> </th>
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);
190 $cats[0] = _AT('cats_uncategorized');
191 while ($cat_row = mysql_fetch_assoc($cat_result)) {
192 $cats[$cat_row['category_id']] = $cat_row['title'];
198 echo '<td class="row1" align="center"><strong>'.$count.'</strong></td>';
199 echo '<td class="row1" align="center">';
201 if (isset($_POST['submit'])) {
202 $row['weight'] = $_POST['weight'][$row['question_id']];
203 $row['required'] = (isset($_POST['required'][$row['question_id']]) ? 1 : 0);
206 if ($row['type'] == 4) {
207 echo ''._AT('na').'';
208 echo '<input type="hidden" value="0" name="weight['.$row['question_id'].']" />';
210 echo '<input type="text" value="'.$row['weight'].'" name="weight['.$row['question_id'].']" size="2" />';
215 echo '<td class="row1" align="center">'._AT('na').'</td>';
217 echo '<td class="row1" align="center"><input type="text" name="ordering['.$row['question_id'].']" value="'.$row['ordering'].'" size="2" /></td>';
220 echo '<td class="row1">';
221 echo AT_print(validate_length($row['question'], 45, VALIDATE_LENGTH_FOR_DISPLAY), 'tests_questions.question');
224 echo '<td nowrap="nowrap">';
225 $o = TestQuestions::getQuestion($row['type']);
226 echo $o->printName();
229 $link = 'mods/_standard/tests/edit_question_'.$o->getPrefix().'.php?tid='.$tid.SEP.'qid='.$row['question_id'];
231 echo '<td align="center">'.$cats[$row['category_id']].'</td>';
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"';
238 echo ' id="q'.$row['question_id'].'" /><label for="q'.$row['question_id'].'">'._AT('required').'</label></td>';
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>';
247 } while ($row = mysql_fetch_assoc($result));
251 echo '<tr><td> </td>';
252 echo '<td colspan="';
259 echo '" align="left" nowrap="nowrap">';
260 echo '<input type="submit" value="'._AT('save').'" name="submit" /> </td>';
264 echo '<tr><td colspan="';
271 echo '" >'._AT('none_found').'</td></tr>';
274 echo '</table><br /></form>';
276 require(AT_INCLUDE_PATH.'footer.inc.php');