remove old readme
[atutor.git] / mods / _standard / tests / create_question_likert.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
14 define('AT_INCLUDE_PATH', '../../../include/');
15 require(AT_INCLUDE_PATH.'vitals.inc.php');
16 require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/likert_presets.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 (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_params = array(    $_POST['category_id'], 
62                                                                 $_SESSION['course_id'],
63                                                                 $_POST['feedback'], 
64                                                                 $_POST['question'], 
65                                                                 $_POST['choice'][0], 
66                                                                 $_POST['choice'][1], 
67                                                                 $_POST['choice'][2], 
68                                                                 $_POST['choice'][3], 
69                                                                 $_POST['choice'][4], 
70                                                                 $_POST['choice'][5], 
71                                                                 $_POST['choice'][6], 
72                                                                 $_POST['choice'][7], 
73                                                                 $_POST['choice'][8], 
74                                                                 $_POST['choice'][9], 
75                                                                 $_POST['answer'][0], 
76                                                                 $_POST['answer'][1], 
77                                                                 $_POST['answer'][2], 
78                                                                 $_POST['answer'][3], 
79                                                                 $_POST['answer'][4], 
80                                                                 $_POST['answer'][5], 
81                                                                 $_POST['answer'][6], 
82                                                                 $_POST['answer'][7], 
83                                                                 $_POST['answer'][8], 
84                                                                 $_POST['answer'][9]);
85
86                 $sql = vsprintf(AT_SQL_QUESTION_LIKERT, $sql_params);
87                                 $result = mysql_query($sql, $db);
88                 
89                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
90                 header('Location: question_db.php');
91                 exit;
92         }
93 } else if (isset($_POST['preset'])) {
94         // load preset
95         $_POST['preset_num'] = intval($_POST['preset_num']);
96
97         if (isset($_likert_preset[$_POST['preset_num']])) {
98                 $_POST['choice'] = $_likert_preset[$_POST['preset_num']];
99         } else if ($_POST['preset_num']) {
100                 $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$_POST[preset_num] AND course_id=$_SESSION[course_id]";
101                 $result = mysql_query($sql, $db);
102                 if ($row = mysql_fetch_assoc($result)){
103                         for ($i=0; $i<10; $i++) {
104                                 $_POST['choice'][$i] = $row['choice_' . $i];
105                         }
106                 }
107         }
108
109 }
110
111 $onload = 'document.form.category_id.focus();';
112
113 require(AT_INCLUDE_PATH.'header.inc.php');
114
115 ?>
116 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
117 <input type="hidden" name="required" value="1" />
118
119 <div class="input-form">
120         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('preset_scales'); ?></legend>
121
122         <div class="row">
123                 <select name="preset_num">
124                         <optgroup label="<?php echo _AT('presets'); ?>">
125                 <?php
126                         //presets
127                         foreach ($_likert_preset as $val=>$preset) {
128                                 echo '<option value="'.$val.'">'.$preset[0].' - '.$preset[count($preset)-1].'</option>';
129                         }
130                         echo '</optgroup>';
131                         //previously used
132
133                         $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE course_id=$_SESSION[course_id] AND type=4";
134                         $result = mysql_query($sql, $db);
135                         if ($row = mysql_fetch_assoc($result)) {
136                                 echo '<optgroup label="'. _AT('prev_used').'">';
137                                 $used_choices = array();
138                                 do {
139                                         $choices = array_slice($row, 9, 10);
140                                         if (in_array($choices, $used_choices)) {
141                                                 continue;
142                                         }
143
144                                         $used_choices[] = $choices;
145
146                                         for ($i=0; $i<=10; $i++) {
147                                                 if ($row['choice_'.$i] == '') {
148                                                         $i--;
149                                                         break;
150                                                 }
151                                         }
152                                         echo '<option value="'.$row['question_id'].'">'.$row['choice_0'].' - '.$row['choice_'.$i].'</option>';
153                                 } while ($row = mysql_fetch_assoc($result));
154                                 echo '</optgroup>';
155                         }
156                 ?>
157                 </select>
158         </div>
159
160         <div class="row buttons">
161                 <input type="submit" name="preset" value="<?php echo _AT('set_preset'); ?>" class="button" />
162         </div>
163         </fieldset>
164 </div>
165
166 <br />
167 <div class="input-form">
168         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_lk'); ?></legend>
169         <div class="row">
170                 <label for="cats"><?php echo _AT('category'); ?></label><br />
171                 <select name="category_id" id="cats">
172                         <?php print_question_cats($_POST['category_id']); ?>
173                 </select>
174         </div>
175
176         <div class="row">
177                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php echo _AT('question'); ?></label> 
178                 <?php print_VE('question'); ?>
179                 <textarea id="question" cols="50" rows="6" name="question" style="width:90%;"><?php 
180                 echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
181         </div>
182
183
184 <?php for ($i=0; $i<10; $i++) { ?>
185                 <div class="row">
186                         <?php if ($i==0 || $i==1) { ?>
187                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span>
188                         <?php } ?>
189                         <label for="choice_<?php echo $i; ?>">
190                         <?php echo _AT('choice'); ?> <?php echo ($i+1); ?></label><br />
191                         <input type="text" id="choice_<?php echo $i; ?>" size="40" name="choice[<?php echo $i; ?>]" value="<?php echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?>" />
192                 </div>
193 <?php } ?>
194
195         <div class="row buttons">
196                 <input type="submit" value="<?php echo _AT('save'); ?>"   name="submit" accesskey="s" />
197                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
198         </div>
199         </fieldset>
200 </div>
201 </form>
202
203 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>