made a copy
[atutor.git] / tools / tests / edit_question_multi.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         $_POST['answer']   = intval($_POST['answer']);
42
43         if ($_POST['question'] == ''){
44                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
45         }
46
47         if (!$msg->containsErrors()) {
48                 $answers    = array_fill(0, 10, 0);
49                 $answers[$_POST['answer']] = 1;
50
51                 for ($i=0; $i<10; $i++) {
52                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
53                 }
54
55                 $_POST['feedback']   = $addslashes($_POST['feedback']);
56                 $_POST['question']   = $addslashes($_POST['question']);
57
58                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
59             category_id=$_POST[category_id],
60                     feedback='$_POST[feedback]',
61                         question='$_POST[question]',
62                         choice_0='{$_POST[choice][0]}',
63                         choice_1='{$_POST[choice][1]}',
64                         choice_2='{$_POST[choice][2]}',
65                         choice_3='{$_POST[choice][3]}',
66                         choice_4='{$_POST[choice][4]}',
67                         choice_5='{$_POST[choice][5]}',
68                         choice_6='{$_POST[choice][6]}',
69                         choice_7='{$_POST[choice][7]}',
70                         choice_8='{$_POST[choice][8]}',
71                         choice_9='{$_POST[choice][9]}',
72                         answer_0={$answers[0]},
73                         answer_1={$answers[1]},
74                         answer_2={$answers[2]},
75                         answer_3={$answers[3]},
76                         answer_4={$answers[4]},
77                         answer_5={$answers[5]},
78                         answer_6={$answers[6]},
79                         answer_7={$answers[7]},
80                         answer_8={$answers[8]},
81                         answer_9={$answers[9]}
82
83                         WHERE question_id=$_POST[qid] AND course_id=$_SESSION[course_id]";
84
85                 $result = mysql_query($sql, $db);
86
87                 $msg->addFeedback('QUESTION_UPDATED');
88                 if ($_POST['tid']) {
89                         header('Location: questions.php?tid='.$_POST['tid']);                   
90                 } else {
91                         header('Location: question_db.php');
92                 }
93                 exit;
94         }
95 }
96
97 if (!isset($_POST['submit'])) {
98         $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$qid AND course_id=$_SESSION[course_id] AND type=1";
99         $result = mysql_query($sql, $db);
100
101         if (!($row = mysql_fetch_array($result))){
102                 require(AT_INCLUDE_PATH.'header.inc.php');
103                 $msg->printErrors('ITEM_NOT_FOUND');
104                 require (AT_INCLUDE_PATH.'footer.inc.php');
105                 exit;
106         }
107         $_POST['category_id'] = $row['category_id'];
108         $_POST['feedback']        = $row['feedback'];
109         $_POST['required']        = $row['required'];
110         $_POST['weight']          = $row['weight'];
111         $_POST['question']        = $row['question'];
112
113         for ($i=0; $i<10; $i++) {
114                 $_POST['choice'][$i] = $row['choice_'.$i];
115                 $_POST['answer'][$i] = $row['answer_'.$i];
116         }
117 }
118
119 require(AT_INCLUDE_PATH.'header.inc.php');
120
121 ?>
122 <form action="tools/tests/edit_question_multi.php" method="post" name="form">
123 <input type="hidden" name="tid" value="<?php echo $_REQUEST['tid']; ?>" />
124 <input type="hidden" name="qid" value="<?php echo $qid; ?>" />
125 <input type="hidden" name="required" value="1" />
126
127 <div class="input-form">
128         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_mc'); ?></legend>
129         <div class="row">
130                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="cats"><?php echo _AT('category'); ?></label>
131                 <select name="category_id" id="cats">
132                         <?php print_question_cats($_POST['category_id']); ?>
133                 </select>
134         </div>
135         
136         <div class="row">
137                 <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label> 
138                 <?php print_VE('optional_feedback'); ?> 
139
140                 <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
141         </div>
142
143         <div class="row">
144                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?></label> 
145                 <?php print_VE('question'); ?>          
146                 <textarea id="question" cols="50" rows="4" name="question"><?php 
147                         echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
148         </div>
149
150         <?php 
151         for ($i=0; $i<10; $i++) { ?>
152                 <div class="row">
153                         <label for="choice_<?php echo $i; ?>"><?php echo _AT('choice'); ?> <?php echo ($i+1); ?></label> 
154                         <?php print_VE('choice_' . $i); ?>
155                         <br />
156                         <small><input type="radio" name="answer" id="answer_<?php echo $i; ?>" value="<?php echo $i; ?>" <?php if($_POST['answer'][$i]) { echo 'checked="checked"';} ?>><label for="answer_<?php echo $i; ?>"><?php echo _AT('correct_answer'); ?></label></small>
157                         
158
159                         <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>
160                 </div>
161         <?php } ?>
162
163         <div class="row buttons">
164                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
165                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
166         </div>
167         </fieldset>
168 </div>
169 </form>
170
171 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>