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