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