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