AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / tests / create_question_multianswer.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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
13 define('TR_INCLUDE_PATH', '../include/');
14 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
15 require_once(TR_INCLUDE_PATH.'lib/test_question_queries.inc.php');
16 require_once(TR_INCLUDE_PATH.'classes/DAO/TestsQuestionsDAO.class.php');
17 require_once(TR_INCLUDE_PATH.'classes/Utility.class.php');
18
19 global $_course_id;
20
21 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
22 $testsQuestionsDAO = new TestsQuestionsDAO();
23
24 if (isset($_POST['cancel']) || isset($_POST['submit_no'])) {
25         $msg->addFeedback('CANCELLED');
26         header('Location: question_db.php?_course_id='.$_course_id);
27         exit;
28 } else if ($_POST['submit'] || $_POST['submit_yes']) {
29         $_POST['required'] = intval($_POST['required']);
30         $_POST['feedback'] = trim($_POST['feedback']);
31         $_POST['question'] = trim($_POST['question']);
32         $_POST['category_id'] = intval($_POST['category_id']);
33
34         if ($_POST['question'] == ''){
35                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
36         }
37                 
38         if (!$msg->containsErrors()) {
39                 $choice_new = array(); // stores the non-blank choices
40                 $answer_new = array(); // stores the associated "answer" for the choices
41                 for ($i=0; $i<10; $i++) {
42                         /**
43                          * Db defined it to be 255 length, chop strings off it it's less than that
44                          * @harris
45                          */
46                         $_POST['choice'][$i] = Utility::validateLength($_POST['choice'][$i], 255);
47                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
48                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
49
50                         if ($_POST['choice'][$i] == '') {
51                                 /* an empty option can't be correct */
52                                 $_POST['answer'][$i] = 0;
53                         } else {
54                                 /* filter out empty choices/ remove gaps */
55                                 $choice_new[] = $_POST['choice'][$i];
56                                 $answer_new[] = $_POST['answer'][$i];
57
58                                 if ($_POST['answer'][$i] != 0)
59                                         $has_answer = TRUE;
60                         }
61                 }
62                         
63                 if ($has_answer != TRUE && !$_POST['submit_yes']) {
64         
65                         $hidden_vars['required']    = htmlspecialchars($_POST['required']);
66                         $hidden_vars['feedback']    = htmlspecialchars($_POST['feedback']);
67                         $hidden_vars['question']    = htmlspecialchars($_POST['question']);
68                         $hidden_vars['category_id'] = htmlspecialchars($_POST['category_id']);
69
70                         for ($i = 0; $i < count($choice_new); $i++) {
71                                 $hidden_vars['answer['.$i.']'] = htmlspecialchars($answer_new[$i]);
72                                 $hidden_vars['choice['.$i.']'] = htmlspecialchars($choice_new[$i]);
73                         }
74
75                         $msg->addConfirm('NO_ANSWER', $hidden_vars);
76                 } else {
77                 
78                         //add slahes throughout - does that fix it?
79                         $_POST['answer'] = $answer_new;
80                         $_POST['choice'] = $choice_new;
81                         $_POST['answer'] = array_pad($_POST['answer'], 10, 0);
82                         $_POST['choice'] = array_pad($_POST['choice'], 10, '');
83                 
84                         $_POST['feedback'] = $addslashes($_POST['feedback']);
85                         $_POST['question'] = $addslashes($_POST['question']);
86
87                         $sql_params = array(    $_POST['category_id'], 
88                                                                         $_course_id,
89                                                                         $_POST['feedback'], 
90                                                                         $_POST['question'], 
91                                                                         $_POST['choice'][0], 
92                                                                         $_POST['choice'][1], 
93                                                                         $_POST['choice'][2], 
94                                                                         $_POST['choice'][3], 
95                                                                         $_POST['choice'][4], 
96                                                                         $_POST['choice'][5], 
97                                                                         $_POST['choice'][6], 
98                                                                         $_POST['choice'][7], 
99                                                                         $_POST['choice'][8], 
100                                                                         $_POST['choice'][9], 
101                                                                         $_POST['answer'][0], 
102                                                                         $_POST['answer'][1], 
103                                                                         $_POST['answer'][2], 
104                                                                         $_POST['answer'][3], 
105                                                                         $_POST['answer'][4], 
106                                                                         $_POST['answer'][5], 
107                                                                         $_POST['answer'][6], 
108                                                                         $_POST['answer'][7], 
109                                                                         $_POST['answer'][8], 
110                                                                         $_POST['answer'][9]);
111                         $sql = vsprintf(TR_SQL_QUESTION_MULTIANSWER, $sql_params);
112
113                         if ($testsQuestionsDAO->execute($sql)) {
114                                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
115                                 header('Location: question_db.php?_course_id='.$_course_id);
116                                 exit;
117                         }
118                 }
119         }
120 }
121
122 $onload = 'document.form.category_id.focus();';
123
124 require_once(TR_INCLUDE_PATH.'header.inc.php');
125
126 $msg->printConfirm();
127
128 $savant->assign('qid', $qid);
129 $savant->assign('tid', $_REQUEST['tid']);
130 $savant->assign('course_id', $_course_id);
131 $savant->display('tests/create_edit_question_multianswer.tmpl.php');
132
133 require (TR_INCLUDE_PATH.'footer.inc.php'); ?>