remove old readme
[atutor.git] / mods / _standard / tests / edit_question_ordering.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         $missing_fields = array();
35
36         $_POST['feedback']    = trim($_POST['feedback']);
37         $_POST['question']    = trim($_POST['question']);
38         $_POST['category_id'] = intval($_POST['category_id']);
39
40         if ($_POST['question'] == ''){
41                 $missing_fields[] = _AT('question');
42         }
43
44         if (trim($_POST['choice'][0]) == '') {
45                 $missing_fields[] = _AT('item').' 1';
46         }
47         if (trim($_POST['choice'][1]) == '') {
48                 $missing_fields[] = _AT('item').' 2';
49         }
50
51         if ($missing_fields) {
52                 $missing_fields = implode(', ', $missing_fields);
53                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
54         }
55         if (!$msg->containsErrors()) {
56                 $_POST['question'] = $addslashes($_POST['question']);
57                 $_POST['feedback'] = $addslashes($_POST['feedback']);
58
59                 $choice_new = array(); // stores the non-blank choices
60                 $answer_new = array(); // stores the non-blank answers
61                 $order = 0; // order count
62                 for ($i=0; $i<10; $i++) {
63                         /**
64                          * Db defined it to be 255 length, chop strings off it it's less than that
65                          * @harris
66                          */
67                         $_POST['choice'][$i] = validate_length($_POST['choice'][$i], 255);
68                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
69
70                         if ($_POST['choice'][$i] != '') {
71                                 /* filter out empty choices/ remove gaps */
72                                 $choice_new[] = $_POST['choice'][$i];
73                                 $answer_new[] = $order++;
74                         }
75                 }
76                 
77                 $_POST['choice']   = array_pad($choice_new, 10, '');
78                 $answer_new        = array_pad($answer_new, 10, 0);
79
80                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
81                         category_id=$_POST[category_id],
82                         feedback='$_POST[feedback]',
83                         question='$_POST[question]',
84                         choice_0='{$_POST[choice][0]}',
85                         choice_1='{$_POST[choice][1]}',
86                         choice_2='{$_POST[choice][2]}',
87                         choice_3='{$_POST[choice][3]}',
88                         choice_4='{$_POST[choice][4]}',
89                         choice_5='{$_POST[choice][5]}',
90                         choice_6='{$_POST[choice][6]}',
91                         choice_7='{$_POST[choice][7]}',
92                         choice_8='{$_POST[choice][8]}',
93                         choice_9='{$_POST[choice][9]}',
94                         answer_0=$answer_new[0],
95                         answer_1=$answer_new[1],
96                         answer_2=$answer_new[2],
97                         answer_3=$answer_new[3],
98                         answer_4=$answer_new[4],
99                         answer_5=$answer_new[5],
100                         answer_6=$answer_new[6],
101                         answer_7=$answer_new[7],
102                         answer_8=$answer_new[8],
103                         answer_9=$answer_new[9]
104
105                         WHERE question_id=$_POST[qid] AND course_id=$_SESSION[course_id]";
106                 $result = mysql_query($sql, $db);
107
108                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
109                 if ($_POST['tid']) {
110                         header('Location: questions.php?tid='.$_POST['tid']);                   
111                 } else {
112                         header('Location: question_db.php');
113                 }
114                 exit;
115         }
116 } else {
117         $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$qid AND course_id=$_SESSION[course_id] AND type=6";
118         $result = mysql_query($sql, $db);
119
120         if (!($row = mysql_fetch_assoc($result))){
121                 require(AT_INCLUDE_PATH.'header.inc.php');
122                 $msg->printErrors('ITEM_NOT_FOUND');
123                 require (AT_INCLUDE_PATH.'footer.inc.php');
124                 exit;
125         }
126
127         $_POST['required']              = $row['required'];
128         $_POST['question']              = $row['question'];
129         $_POST['category_id']   = $row['category_id'];
130         $_POST['feedback']              = $row['feedback'];
131
132         for ($i=0; $i<10; $i++) {
133                 $_POST['choice'][$i] = $row['choice_'.$i];
134         }
135 }
136 require(AT_INCLUDE_PATH.'header.inc.php');
137 ?>
138
139 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
140 <input type="hidden" name="qid" value="<?php echo $qid; ?>" />
141 <input type="hidden" name="tid" value="<?php echo $_REQUEST['tid']; ?>" />
142
143 <div class="input-form">
144         <div class="row">
145                 <label for="cats"><?php echo _AT('category'); ?></label><br />
146                 <select name="category_id" id="cats">
147                         <?php print_question_cats($_POST['category_id']); ?>
148                 </select>
149         </div>
150
151         <div class="row">
152                 <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label> 
153                 <?php print_VE('optional_feedback'); ?>
154
155                 <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
156         </div>
157
158         <div class="row">
159                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php echo _AT('question'); ?></label> 
160                 
161                 <?php print_VE('question'); ?>
162                 
163                 <textarea id="question" cols="50" rows="6" name="question"><?php echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
164         </div>
165
166         <?php for ($i=0; $i<10; $i++): ?>
167                 <div class="row">
168                         <?php if ($i < 2): ?>
169                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span>
170                         <?php endif; ?> <?php echo _AT('item'); ?> <?php echo ($i+1); ?>
171                         
172                         <?php print_VE('choice_' . $i); ?>
173                         
174                         <br />
175         
176                         <textarea id="choice_<?php echo $i; ?>" cols="50" rows="2" name="choice[<?php echo $i; ?>]"><?php 
177                         echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?></textarea> 
178                 </div>
179         <?php endfor; ?>
180
181         <div class="row buttons">
182                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
183                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
184         </div>
185 </div>
186 </form>
187
188 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>