7d44dfacb3e08f7739b3a1785e2e2830f57dd308
[atutor.git] / docs / mods / _standard / tests / create_question_multianswer.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']) || isset($_POST['submit_no'])) {
23         $msg->addFeedback('CANCELLED');
24         header('Location: question_db.php');
25         exit;
26 } else if ($_POST['submit'] || $_POST['submit_yes']) {
27         $_POST['required'] = intval($_POST['required']);
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                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
34         }
35                 
36         if (!$msg->containsErrors()) {
37                 $choice_new = array(); // stores the non-blank choices
38                 $answer_new = array(); // stores the associated "answer" for the choices
39                 for ($i=0; $i<10; $i++) {
40                         /**
41                          * Db defined it to be 255 length, chop strings off it it's less than that
42                          * @harris
43                          */
44                         $_POST['choice'][$i] = validate_length($_POST['choice'][$i], 255);
45                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
46                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
47
48                         if ($_POST['choice'][$i] == '') {
49                                 /* an empty option can't be correct */
50                                 $_POST['answer'][$i] = 0;
51                         } else {
52                                 /* filter out empty choices/ remove gaps */
53                                 $choice_new[] = $_POST['choice'][$i];
54                                 $answer_new[] = $_POST['answer'][$i];
55
56                                 if ($_POST['answer'][$i] != 0)
57                                         $has_answer = TRUE;
58                         }
59                 }
60                         
61                 if ($has_answer != TRUE && !$_POST['submit_yes']) {
62         
63                         $hidden_vars['required']    = htmlspecialchars($_POST['required']);
64                         $hidden_vars['feedback']    = htmlspecialchars($_POST['feedback']);
65                         $hidden_vars['question']    = htmlspecialchars($_POST['question']);
66                         $hidden_vars['category_id'] = htmlspecialchars($_POST['category_id']);
67
68                         for ($i = 0; $i < count($choice_new); $i++) {
69                                 $hidden_vars['answer['.$i.']'] = htmlspecialchars($answer_new[$i]);
70                                 $hidden_vars['choice['.$i.']'] = htmlspecialchars($choice_new[$i]);
71                         }
72
73                         $msg->addConfirm('NO_ANSWER', $hidden_vars);
74                 } else {
75                 
76                         //add slahes throughout - does that fix it?
77                         $_POST['answer'] = $answer_new;
78                         $_POST['choice'] = $choice_new;
79                         $_POST['answer'] = array_pad($_POST['answer'], 10, 0);
80                         $_POST['choice'] = array_pad($_POST['choice'], 10, '');
81                 
82                         $_POST['feedback'] = $addslashes($_POST['feedback']);
83                         $_POST['question'] = $addslashes($_POST['question']);
84
85                         $sql_params = array(    $_POST['category_id'], 
86                                                                         $_SESSION['course_id'],
87                                                                         $_POST['feedback'], 
88                                                                         $_POST['question'], 
89                                                                         $_POST['choice'][0], 
90                                                                         $_POST['choice'][1], 
91                                                                         $_POST['choice'][2], 
92                                                                         $_POST['choice'][3], 
93                                                                         $_POST['choice'][4], 
94                                                                         $_POST['choice'][5], 
95                                                                         $_POST['choice'][6], 
96                                                                         $_POST['choice'][7], 
97                                                                         $_POST['choice'][8], 
98                                                                         $_POST['choice'][9], 
99                                                                         $_POST['answer'][0], 
100                                                                         $_POST['answer'][1], 
101                                                                         $_POST['answer'][2], 
102                                                                         $_POST['answer'][3], 
103                                                                         $_POST['answer'][4], 
104                                                                         $_POST['answer'][5], 
105                                                                         $_POST['answer'][6], 
106                                                                         $_POST['answer'][7], 
107                                                                         $_POST['answer'][8], 
108                                                                         $_POST['answer'][9]);
109                         $sql = vsprintf(AT_SQL_QUESTION_MULTIANSWER, $sql_params);
110
111                         $result = mysql_query($sql, $db);
112
113                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
114                         header('Location: question_db.php');
115                         exit;
116                 }
117         }
118 }
119
120 $onload = 'document.form.category_id.focus();';
121
122 require(AT_INCLUDE_PATH.'header.inc.php');
123
124 $msg->printConfirm();
125 ?>
126 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
127 <input type="hidden" name="required" value="1" />
128 <div class="input-form">
129         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_ma'); ?></legend>
130         <div class="row">
131                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="cats"><?php echo _AT('category'); ?></label><br />
132                 <select name="category_id" id="cats">
133                         <?php print_question_cats($_POST['category_id']); ?>
134                 </select>
135         </div>
136
137         <div class="row">
138                 <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label>
139                 <?php print_VE('optional_feedback'); ?> 
140                 <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
141         </div>
142
143         <div class="row">
144                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php echo _AT('question'); ?></label>
145                 <?php print_VE('question'); ?>
146                 <textarea id="question" cols="50" rows="4" name="question" style="width:90%;"><?php echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
147         </div>
148
149 <?php
150         for ($i=0; $i<10; $i++) {
151 ?>
152         <div class="row">
153                 <?php echo _AT('choice'); ?> <?php echo ($i+1); ?>
154                 
155                 <?php print_VE('choice_' . $i); ?>
156                 
157                 <br />
158
159                 <small><input type="checkbox" name="answer[<?php echo $i; ?>]" id="answer_<?php echo $i; ?>" value="1" <?php if($_POST['answer'][$i]) { echo 'checked="checked"';} ?>><label for="answer_<?php echo $i; ?>"><?php echo _AT('correct_answer'); ?></label></small>                        
160                 
161                 <textarea id="choice_<?php echo $i; ?>" cols="50" rows="2" name="choice[<?php echo $i; ?>]"><?php 
162                 echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?></textarea> 
163         </div>
164         <?php } ?>
165
166         <div class="row buttons">
167                 <input type="submit" value="<?php echo _AT('save'); ?>" name="submit" accesskey="s" />
168                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
169         </div>
170         </fieldset>
171 </div>
172 </form>
173
174 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>