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