made a copy
[atutor.git] / tools / tests / edit_question_matching.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 require(AT_INCLUDE_PATH.'lib/likert_presets.inc.php');
17
18 authenticate(AT_PRIV_TESTS);
19 require(AT_INCLUDE_PATH.'lib/test_result_functions.inc.php');
20
21 // for matching test questions
22 $_letters = array(_AT('A'), _AT('B'), _AT('C'), _AT('D'), _AT('E'), _AT('F'), _AT('G'), _AT('H'), _AT('I'), _AT('J'));
23
24
25 $qid = intval($_GET['qid']);
26 if ($qid == 0){
27         $qid = intval($_POST['qid']);
28 }
29
30 if (isset($_POST['cancel'])) {
31         $msg->addFeedback('CANCELLED');
32         if ($_POST['tid']) {
33                 header('Location: questions.php?tid='.$_POST['tid']);                   
34         } else {
35                 header('Location: question_db.php');
36         }
37         exit;
38 } else if (isset($_POST['submit'])) {
39         $_POST['tid']          = intval($_POST['tid']);
40         $_POST['qid']          = intval($_POST['qid']);
41         $_POST['feedback']     = trim($_POST['feedback']);
42         $_POST['instructions'] = trim($_POST['instructions']);
43         $_POST['category_id']  = intval($_POST['category_id']);
44
45         for ($i = 0 ; $i < 10; $i++) {
46                 $_POST['question'][$i]        = $addslashes(trim($_POST['question'][$i]));
47                 $_POST['question_answer'][$i] = (int) $_POST['question_answer'][$i];
48                 $_POST['answer'][$i]          = $addslashes(trim($_POST['answer'][$i]));
49         }
50
51         if (!$_POST['question'][0] 
52                 || !$_POST['question'][1] 
53                 || !$_POST['answer'][0] 
54                 || !$_POST['answer'][1]) {
55
56                 $msg->addError('QUESTION_EMPTY');
57         }
58
59         if (!$msg->containsErrors()) {
60                 $_POST['feedback']     = $addslashes($_POST['feedback']);
61                 $_POST['instructions'] = $addslashes($_POST['instructions']);
62                 
63                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
64                         category_id=$_POST[category_id],
65                         feedback='$_POST[feedback]',
66                         question='$_POST[instructions]',
67                         choice_0='{$_POST[question][0]}',
68                         choice_1='{$_POST[question][1]}',
69                         choice_2='{$_POST[question][2]}',
70                         choice_3='{$_POST[question][3]}',
71                         choice_4='{$_POST[question][4]}',
72                         choice_5='{$_POST[question][5]}',
73                         choice_6='{$_POST[question][6]}',
74                         choice_7='{$_POST[question][7]}',
75                         choice_8='{$_POST[question][8]}',
76                         choice_9='{$_POST[question][9]}',
77                         answer_0={$_POST[question_answer][0]},
78                         answer_1={$_POST[question_answer][1]},
79                         answer_2={$_POST[question_answer][2]},
80                         answer_3={$_POST[question_answer][3]},
81                         answer_4={$_POST[question_answer][4]},
82                         answer_5={$_POST[question_answer][5]},
83                         answer_6={$_POST[question_answer][6]},
84                         answer_7={$_POST[question_answer][7]},
85                         answer_8={$_POST[question_answer][8]},
86                         answer_9={$_POST[question_answer][9]},
87                         option_0='{$_POST[answer][0]}',
88                         option_1='{$_POST[answer][1]}',
89                         option_2='{$_POST[answer][2]}',
90                         option_3='{$_POST[answer][3]}',
91                         option_4='{$_POST[answer][4]}',
92                         option_5='{$_POST[answer][5]}',
93                         option_6='{$_POST[answer][6]}',
94                         option_7='{$_POST[answer][7]}',
95                         option_8='{$_POST[answer][8]}',
96                         option_9='{$_POST[answer][9]}'
97
98                         WHERE question_id=$_POST[qid] AND course_id=$_SESSION[course_id]";
99                 $result = mysql_query($sql, $db);
100
101                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
102                 if ($_POST['tid']) {
103                         header('Location: questions.php?tid='.$_POST['tid']);                   
104                 } else {
105                         header('Location: question_db.php');
106                 }
107                 exit;
108         }
109 } else {
110         $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$qid AND course_id=$_SESSION[course_id] AND type=5";
111         $result = mysql_query($sql, $db);
112
113         if (!($row = mysql_fetch_assoc($result))){
114                 require(AT_INCLUDE_PATH.'header.inc.php');
115                 $msg->printErrors('ITEM_NOT_FOUND');
116                 require (AT_INCLUDE_PATH.'footer.inc.php');
117                 exit;
118         }
119         $_POST['feedback']              = $row['feedback'];
120         $_POST['instructions']  = $row['question'];
121         $_POST['category_id']   = $row['category_id'];
122
123         for ($i=0; $i<10; $i++) {
124                 $_POST['question'][$i]        = $row['choice_'.$i];
125                 $_POST['question_answer'][$i] = $row['answer_'.$i];
126                 $_POST['answer'][$i]          = $row['option_'.$i];
127         }
128         
129 }
130
131 require(AT_INCLUDE_PATH.'header.inc.php');
132 ?>
133
134 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
135 <input type="hidden" name="qid" value="<?php echo $qid; ?>" />
136 <input type="hidden" name="tid" value="<?php echo $_REQUEST['tid']; ?>" />
137
138
139 <div class="input-form">
140         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_matching'); ?></legend>
141         <div class="row">
142                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="cats"><?php echo _AT('category'); ?></label><br />
143                 <select name="category_id" id="cats">
144                         <?php print_question_cats($_POST['category_id']); ?>
145                 </select>
146         </div>
147
148         <div class="row">
149                 <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label> 
150                 <?php print_VE('optional_feedback'); ?>
151
152                 <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php 
153                 echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
154         </div>
155
156         <div class="row">
157                 <label for="instructions"><?php echo _AT('instructions'); ?></label> 
158                 <?php print_VE('instructions'); ?>
159                 <textarea id="instructions" cols="50" rows="3" name="instructions"><?php 
160                 echo htmlspecialchars(stripslashes($_POST['instructions'])); ?></textarea>
161         </div>
162
163         <div class="row">
164                 <h2><?php echo _AT('questions');?></h2>
165         </div>
166 <?php for ($i=0; $i<10; $i++): ?>
167         <div class="row">
168                 <?php if ($i < 2) :?>
169                         <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
170                 <?php endif; ?>
171                 <?php echo _AT('question'); ?> <?php echo ($i+1); ?>
172                 
173                 <?php print_VE('question_' . $i); ?>
174                 
175                 <br />
176
177                 <select name="question_answer[<?php echo $i; ?>]">
178                         <option value="-1">-</option>
179                         <?php foreach ($_letters as $key => $value): ?>
180                                 <option value="<?php echo $key; ?>" <?php if ($key == $_POST['question_answer'][$i]) { echo 'selected="selected"'; }?>><?php echo $value; ?></option>
181                         <?php endforeach; ?>
182                 </select>
183                 
184                 <textarea id="question_<?php echo $i; ?>" cols="50" rows="2" name="question[<?php echo $i; ?>]"><?php 
185                 echo htmlspecialchars(stripslashes($_POST['question'][$i])); ?></textarea> 
186         </div>
187 <?php endfor; ?>
188         
189         <div class="row">
190                 <h2><?php echo _AT('answers');?></h2>
191         </div>
192         <?php for ($i=0; $i<10; $i++): ?>
193                 <div class="row">
194                         <?php if ($i < 2) :?>
195                                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
196                         <?php endif; ?>
197                         <?php echo _AT('answer'); ?> <?php echo $_letters[$i]; ?>
198                         <?php print_VE('answer' . $i); ?>
199                         <br />
200                         <textarea id="answer_<?php echo $i; ?>" cols="50" rows="2" name="answer[<?php echo $i; ?>]"><?php 
201                         echo htmlspecialchars(stripslashes($_POST['answer'][$i])); ?></textarea>
202                 </div>
203         <?php endfor; ?>
204
205         <div class="row buttons">
206                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
207                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
208         </div>
209         </fieldset>
210 </div>
211 </form>
212
213 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>