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