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