tagging as ATutor 1.5.4-release
[atutor.git] / tools / tests / create_question_ordering.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay & Joel Kronenberg        */
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
15 define('AT_INCLUDE_PATH', '../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17
18 authenticate(AT_PRIV_TESTS);
19 require(AT_INCLUDE_PATH.'lib/test_result_functions.inc.php');
20
21 if (isset($_POST['cancel'])) {
22         $msg->addFeedback('CANCELLED');
23         header('Location: question_db.php');
24         exit;
25 } else if ($_POST['submit']) {
26         $missing_fields = array();
27
28         $_POST['feedback']    = trim($_POST['feedback']);
29         $_POST['question']    = trim($_POST['question']);
30         $_POST['category_id'] = intval($_POST['category_id']);
31
32         if ($_POST['question'] == ''){
33                 $missing_fields[] = _AT('question');
34         }
35
36         if (trim($_POST['choice'][0]) == '') {
37                 $missing_fields[] = _AT('item').' 1';
38         }
39         if (trim($_POST['choice'][1]) == '') {
40                 $missing_fields[] = _AT('item').' 2';
41         }
42
43         if ($missing_fields) {
44                 $missing_fields = implode(', ', $missing_fields);
45                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
46         }
47
48         if (!$msg->containsErrors()) {
49                 $choice_new = array(); // stores the non-blank choices
50                 $answer_new = array(); // stores the non-blank answers
51                 $order = 0; // order count
52                 for ($i=0; $i<10; $i++) {
53                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
54
55                         if ($_POST['choice'][$i] != '') {
56                                 /* filter out empty choices/ remove gaps */
57                                 $choice_new[] = $_POST['choice'][$i];
58                                 $answer_new[] = $order++;
59                         }
60                 }
61
62                 $_POST['choice']   = array_pad($choice_new, 10, '');
63                 $answer_new        = array_pad($answer_new, 10, 0);
64                 $_POST['feedback'] = $addslashes($_POST['feedback']);
65                 $_POST['question'] = $addslashes($_POST['question']);
66         
67                 $sql    = "INSERT INTO ".TABLE_PREFIX."tests_questions VALUES ( NULL,
68                         $_POST[category_id],
69                         $_SESSION[course_id],
70                         6,
71                         '$_POST[feedback]',
72                         '$_POST[question]',
73                         '{$_POST[choice][0]}',
74                         '{$_POST[choice][1]}',
75                         '{$_POST[choice][2]}',
76                         '{$_POST[choice][3]}',
77                         '{$_POST[choice][4]}',
78                         '{$_POST[choice][5]}',
79                         '{$_POST[choice][6]}',
80                         '{$_POST[choice][7]}',
81                         '{$_POST[choice][8]}',
82                         '{$_POST[choice][9]}',
83                         $answer_new[0],
84                         $answer_new[1],
85                         $answer_new[2],
86                         $answer_new[3],
87                         $answer_new[4],
88                         $answer_new[5],
89                         $answer_new[6],
90                         $answer_new[7],
91                         $answer_new[8],
92                         $answer_new[9],
93                         '',
94                         '',
95                         '',
96                         '',
97                         '',
98                         '',
99                         '',
100                         '',
101                         '',
102                         '',
103                         0,
104                         0)";
105
106                 $result = mysql_query($sql, $db);
107
108                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
109                 header('Location: question_db.php');
110                 exit;
111         }
112 }
113
114 $onload = 'document.form.category_id.focus();';
115
116 require(AT_INCLUDE_PATH.'header.inc.php');
117
118 ?>
119 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
120 <input type="hidden" name="required" value="1" />
121 <div class="input-form">
122         <div class="row">
123                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="cats"><?php echo _AT('category'); ?></label><br />
124                 <select name="category_id" id="cats">
125                         <?php print_question_cats($_POST['category_id']); ?>
126                 </select>
127         </div>
128
129         <div class="row">
130                 <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label> 
131                 <?php print_VE('optional_feedback'); ?>
132
133                 <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
134         </div>
135
136         <div class="row">
137                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?></label> 
138                 <?php print_VE('question'); ?>
139                 <textarea id="question" cols="50" rows="6" name="question" style="width:90%;"><?php 
140                 echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
141         </div>
142         
143         <?php for ($i=0; $i<10; $i++): ?>
144                 <div class="row">
145                         <?php if ($i < 2): ?>
146                                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
147                         <?php endif; ?> <?php echo _AT('item'); ?> <?php echo ($i+1); ?>
148                         
149                         <?php print_VE('choice_' . $i); ?>
150                         
151                         <br />
152         
153                         <textarea id="choice_<?php echo $i; ?>" cols="50" rows="2" name="choice[<?php echo $i; ?>]"><?php echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?></textarea> 
154                 </div>
155         <?php endfor; ?>
156         
157         <div class="row buttons">
158                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
159                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
160         </div>
161 </div>
162 </form>
163
164 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>