made a copy
[atutor.git] / tools / tests / edit_question_likert.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 define('AT_INCLUDE_PATH', '../../include/');
15 require(AT_INCLUDE_PATH.'vitals.inc.php');
16 require(AT_INCLUDE_PATH.'lib/likert_presets.inc.php');
17
18 authenticate(AT_PRIV_TESTS);
19 require(AT_INCLUDE_PATH.'lib/test_result_functions.inc.php');
20
21 $qid = intval($_GET['qid']);
22 if ($qid == 0){
23         $qid = intval($_POST['qid']);
24 }
25
26 if (isset($_POST['cancel'])) {
27         $msg->addFeedback('CANCELLED');
28         if ($_POST['tid']) {
29                 header('Location: questions.php?tid='.$_POST['tid']);                   
30         } else {
31                 header('Location: question_db.php');
32         }
33         exit;
34 } else if (isset($_POST['submit'])) {
35         $_POST['required']    = intval($_POST['required']);
36         $_POST['question']    = trim($_POST['question']);
37         $_POST['category_id'] = intval($_POST['category_id']);
38         $_POST['alignment']   = intval($_POST['alignment']);
39
40         $empty_fields = array();
41         if ($_POST['question'] == ''){
42                 $empty_fields[] = _AT('question');
43         }
44         if ($_POST['choice'][0] == '') {
45                 $empty_fields[] = _AT('choice').' 1';
46         }
47
48         if ($_POST['choice'][1] == '') {
49                 $empty_fields[] = _AT('choice').' 2';
50         }
51
52         if (!empty($empty_fields)) {
53                 $msg->addError(array('EMPTY_FIELDS', implode(', ', $empty_fields)));
54         }
55
56         if (!$msg->containsErrors()) {
57                 $_POST['question'] = $addslashes($_POST['question']);
58
59                 for ($i=0; $i<10; $i++) {
60                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
61                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
62
63                         if ($_POST['choice'][$i] == '') {
64                                 /* an empty option can't be correct */
65                                 $_POST['answer'][$i] = 0;
66                         }
67                 }               
68                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
69                         category_id=$_POST[category_id],
70                         feedback='',
71                         question='$_POST[question]',
72                         choice_0='{$_POST[choice][0]}',
73                         choice_1='{$_POST[choice][1]}',
74                         choice_2='{$_POST[choice][2]}',
75                         choice_3='{$_POST[choice][3]}',
76                         choice_4='{$_POST[choice][4]}',
77                         choice_5='{$_POST[choice][5]}',
78                         choice_6='{$_POST[choice][6]}',
79                         choice_7='{$_POST[choice][7]}',
80                         choice_8='{$_POST[choice][8]}',
81                         choice_9='{$_POST[choice][9]}',
82                         answer_0={$_POST[answer][0]},
83                         answer_1={$_POST[answer][1]},
84                         answer_2={$_POST[answer][2]},
85                         answer_3={$_POST[answer][3]},
86                         answer_4={$_POST[answer][4]},
87                         answer_5={$_POST[answer][5]},
88                         answer_6={$_POST[answer][6]},
89                         answer_7={$_POST[answer][7]},
90                         answer_8={$_POST[answer][8]},
91                         answer_9={$_POST[answer][9]}
92
93                         WHERE question_id=$_POST[qid] AND course_id=$_SESSION[course_id]";
94                 $result = mysql_query($sql, $db);
95
96                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
97                 if ($_POST['tid']) {
98                         header('Location: questions.php?tid='.$_POST['tid']);                   
99                 } else {
100                         header('Location: question_db.php');
101                 }
102                 exit;
103         }
104 } else if (isset($_POST['preset'])) {
105         // load preset
106         $_POST['preset_num'] = intval($_POST['preset_num']);
107
108         if (isset($_likert_preset[$_POST['preset_num']])) {
109                 $_POST['choice'] = $_likert_preset[$_POST['preset_num']];
110         } else if ($_POST['preset_num']) {
111                 $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$_POST[preset_num] AND course_id=$_SESSION[course_id]";
112                 $result = mysql_query($sql, $db);
113                 if ($row = mysql_fetch_assoc($result)){
114                         for ($i=0; $i<10; $i++) {
115                                 $_POST['choice'][$i] = $row['choice_' . $i];
116                         }
117                 }
118         }
119
120 } else {
121         $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$qid AND course_id=$_SESSION[course_id] AND type=4";
122         $result = mysql_query($sql, $db);
123
124         if (!($row = mysql_fetch_array($result))){
125                 require(AT_INCLUDE_PATH.'header.inc.php');
126                 $msg->printErrors('ITEM_NOT_FOUND');
127                 require (AT_INCLUDE_PATH.'footer.inc.php');
128                 exit;
129         }
130         $_POST['required']              = $row['required'];
131         $_POST['question']              = $row['question'];
132         $_POST['category_id']   = $row['category_id'];
133
134         for ($i=0; $i<10; $i++) {
135                 $_POST['choice'][$i] = $row['choice_'.$i];
136         }
137 }
138
139 require(AT_INCLUDE_PATH.'header.inc.php');
140 ?>
141
142 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
143 <input type="hidden" name="qid" value="<?php echo $qid; ?>" />
144 <input type="hidden" name="tid" value="<?php echo $_REQUEST['tid']; ?>" />
145 <input type="hidden" name="required" value="1" />
146         <fieldset class="group_form" style="width:88%;margin-left:auto;margin-right:auto;"><legend class="group_form"><?php echo _AT('preset_scales'); ?></legend>
147 <table cellspacing="1" cellpadding="0" border="0" class="bodyline" summary="" align="center">
148 <tr>
149         <th align="left"><?php echo _AT('preset_scales'); ?> </th>
150 </tr>
151 <tr>
152         <td class="row1" nowrap="nowrap">
153                 <select name="preset_num">
154                                 <option value="0"></option>
155                         <optgroup label="<?php echo _AT('presets'); ?>"><?php
156                                 // presets
157                                 foreach ($_likert_preset as $val => $preset) {
158                                         echo '<option value="'.$val.'">'.$preset[0].' - '.$preset[count($preset)-1].'</option>';
159                                 }
160                         //previously used
161                         echo '</optgroup>';
162
163                         $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE course_id=$_SESSION[course_id] AND type=4";
164                         $result = mysql_query($sql, $db);
165                         if ($row = mysql_fetch_assoc($result)) {
166                                 echo '<optgroup label="'. _AT('prev_used').'">';
167                                 $used_choices = array();
168                                 do {
169                                         $choices = array_slice($row, 9, 10);
170                                         if (in_array($choices, $used_choices)) {
171                                                 continue;
172                                         }
173
174                                         $used_choices[] = $choices;
175
176                                         for ($i=0; $i<=10; $i++) {
177                                                 if ($row['choice_'.$i] == '') {
178                                                         $i--;
179                                                         break;
180                                                 }
181                                         }
182                                         echo '<option value="'.$row['question_id'].'">'.$row['choice_0'].' - '.$row['choice_'.$i].'</option>';
183                                 } while ($row = mysql_fetch_assoc($result));
184                                 echo '</optgroup>';
185                         }
186                 ?>
187                 </select> 
188                 <input type="submit" name="preset" value="<?php echo _AT('set_preset'); ?>" class="button" />
189         </td>
190 </tr>
191 </table>
192 </fieldset>
193 <br />
194
195 <div class="input-form">
196         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('question'); ?></legend>
197         <div class="row">
198                 <label for="cats"><?php echo _AT('category'); ?></label><br />
199                 <select name="category_id" id="cats">
200                         <?php print_question_cats($_POST['category_id']); ?>
201                 </select>
202         </div>
203
204         <div class="row">
205                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?></label> 
206                 
207                 <?php print_VE('question'); ?>
208                 
209                 <textarea id="question" cols="50" rows="6" name="question"><?php echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
210         </div>
211
212 <?php
213         for ($i=0; $i<10; $i++) { ?>
214                 <div class="row">
215                         <?php if ($i==0 || $i==1) { ?>
216                                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
217                         <?php } ?>
218
219                         <label for="choice_<?php echo $i; ?>">
220                         <?php echo _AT('choice'); ?> <?php echo ($i+1); ?></label><br />
221                         <input type="text" id="choice_<?php echo $i; ?>" size="40" name="choice[<?php echo $i; ?>]" value="<?php echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?>" />
222                 </div>
223 <?php } ?>
224
225         <div class="row buttons">
226                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
227                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
228         </div>
229         </fieldset>
230 </div>
231 </form>
232
233 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>