tagging as ATutor 1.5.4-release
[atutor.git] / tools / tests / create_question_likert.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 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
15 define('AT_INCLUDE_PATH', '../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 require(AT_INCLUDE_PATH.'lib/likert_presets.inc.php');
18
19 authenticate(AT_PRIV_TESTS);
20 require(AT_INCLUDE_PATH.'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 (isset($_POST['submit'])) {
27         $_POST['required']    = intval($_POST['required']);
28         $_POST['question']    = trim($_POST['question']);
29         $_POST['category_id'] = intval($_POST['category_id']);
30
31         $empty_fields = array();
32         if ($_POST['question'] == ''){
33                 $empty_fields[] = _AT('question');
34         }
35         if ($_POST['choice'][0] == '') {
36                 $empty_fields[] = _AT('choice').' 1';
37         }
38
39         if ($_POST['choice'][1] == '') {
40                 $empty_fields[] = _AT('choice').' 2';
41         }
42
43         if (!empty($empty_fields)) {
44                 $msg->addError(array('EMPTY_FIELDS', implode(', ', $empty_fields)));
45         }
46
47         if (!$msg->containsErrors()) {
48                 $_POST['feedback']   = '';
49                 $_POST['question']   = $addslashes($_POST['question']);
50
51                 for ($i=0; $i<10; $i++) {
52                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
53                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
54
55                         if ($_POST['choice'][$i] == '') {
56                                 /* an empty option can't be correct */
57                                 $_POST['answer'][$i] = 0;
58                         }
59                 }
60
61                 $sql    = "INSERT INTO ".TABLE_PREFIX."tests_questions VALUES ( 0, 
62                         $_POST[category_id],
63                         $_SESSION[course_id],
64                         4,
65                         '$_POST[feedback]',
66                         '$_POST[question]',
67                         '{$_POST[choice][0]}',
68                         '{$_POST[choice][1]}',
69                         '{$_POST[choice][2]}',
70                         '{$_POST[choice][3]}',
71                         '{$_POST[choice][4]}',
72                         '{$_POST[choice][5]}',
73                         '{$_POST[choice][6]}',
74                         '{$_POST[choice][7]}',
75                         '{$_POST[choice][8]}',
76                         '{$_POST[choice][9]}',
77                         {$_POST[answer][0]},
78                         {$_POST[answer][1]},
79                         {$_POST[answer][2]},
80                         {$_POST[answer][3]},
81                         {$_POST[answer][4]},
82                         {$_POST[answer][5]},
83                         {$_POST[answer][6]},
84                         {$_POST[answer][7]},
85                         {$_POST[answer][8]},
86                         {$_POST[answer][9]},
87                         '',
88                         '',
89                         '',
90                         '',
91                         '',
92                         '',
93                         '',
94                         '',
95                         '',
96                         '',
97                         0,
98                         0)";
99                 $result = mysql_query($sql, $db);
100                 
101                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
102                 header('Location: question_db.php');
103                 exit;
104         }
105 } else if (isset($_POST['preset'])) {
106         // load preset
107         $_POST['preset_num'] = intval($_POST['preset_num']);
108
109         if (isset($_likert_preset[$_POST['preset_num']])) {
110                 $_POST['choice'] = $_likert_preset[$_POST['preset_num']];
111         } else if ($_POST['preset_num']) {
112                 $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$_POST[preset_num] AND course_id=$_SESSION[course_id]";
113                 $result = mysql_query($sql, $db);
114                 if ($row = mysql_fetch_assoc($result)){
115                         for ($i=0; $i<10; $i++) {
116                                 $_POST['choice'][$i] = $row['choice_' . $i];
117                         }
118                 }
119         }
120
121 }
122
123 $onload = 'document.form.category_id.focus();';
124
125 require(AT_INCLUDE_PATH.'header.inc.php');
126
127 ?>
128 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
129 <input type="hidden" name="required" value="1" />
130
131 <div class="input-form">
132         <div class="row">
133                 <h3><?php echo _AT('preset_scales'); ?></h3>
134         </div>
135         
136         <div class="row">
137                 <select name="preset_num">
138                         <optgroup label="<?php echo _AT('presets'); ?>">
139                 <?php
140                         //presets
141                         foreach ($_likert_preset as $val=>$preset) {
142                                 echo '<option value="'.$val.'">'.$preset[0].' - '.$preset[count($preset)-1].'</option>';
143                         }
144                         echo '</optgroup>';
145                         //previously used
146
147                         $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE course_id=$_SESSION[course_id] AND type=4";
148                         $result = mysql_query($sql, $db);
149                         if ($row = mysql_fetch_assoc($result)) {
150                                 echo '<optgroup label="'. _AT('prev_used').'">';
151                                 $used_choices = array();
152                                 do {
153                                         $choices = array_slice($row, 9, 10);
154                                         if (in_array($choices, $used_choices)) {
155                                                 continue;
156                                         }
157
158                                         $used_choices[] = $choices;
159
160                                         for ($i=0; $i<=10; $i++) {
161                                                 if ($row['choice_'.$i] == '') {
162                                                         $i--;
163                                                         break;
164                                                 }
165                                         }
166                                         echo '<option value="'.$row['question_id'].'">'.$row['choice_0'].' - '.$row['choice_'.$i].'</option>';
167                                 } while ($row = mysql_fetch_assoc($result));
168                                 echo '</optgroup>';
169                         }
170                 ?>
171                 </select>
172         </div>
173
174         <div class="row buttons">
175                 <input type="submit" name="preset" value="<?php echo _AT('set_preset'); ?>" class="button" />
176         </div>
177 </div>
178
179 <br />
180 <div class="input-form">
181         <div class="row">
182                 <label for="cats"><?php echo _AT('category'); ?></label><br />
183                 <select name="category_id" id="cats">
184                         <?php print_question_cats($_POST['category_id']); ?>
185                 </select>
186         </div>
187
188         <div class="row">
189                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?></label> 
190                 <?php print_VE('question'); ?>
191                 <textarea id="question" cols="50" rows="6" name="question" style="width:90%;"><?php 
192                 echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
193         </div>
194
195
196 <?php for ($i=0; $i<10; $i++) { ?>
197                 <div class="row">
198                         <?php if ($i==0 || $i==1) { ?>
199                                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
200                         <?php } ?>
201                         <label for="choice_<?php echo $i; ?>">
202                         <?php echo _AT('choice'); ?> <?php echo ($i+1); ?></label><br />
203                         <input type="text" id="choice_<?php echo $i; ?>" size="40" name="choice[<?php echo $i; ?>]" value="<?php echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?>" />
204                 </div>
205 <?php } ?>
206
207         <div class="row buttons">
208                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
209                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
210         </div>
211 </div>
212 </form>
213
214 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>