AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / tests / edit_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.'classes/DAO/TestsQuestionsDAO.class.php');
16 require_once(TR_INCLUDE_PATH.'classes/Utility.class.php');
17
18 global $_course_id;
19
20 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
21 $testsQuestionsDAO = new TestsQuestionsDAO();
22
23 $qid = intval($_GET['qid']);
24 if ($qid == 0){
25         $qid = intval($_POST['qid']);
26 }
27
28 if (isset($_POST['cancel'])) {
29         $msg->addFeedback('CANCELLED');
30         if ($_POST['tid']) {
31                 header('Location: questions.php?tid='.$_POST['tid'].'&_course_id='.$_course_id);                        
32         } else {
33                 header('Location: question_db.php?_course_id='.$_course_id);
34         }
35         exit;
36 } else if (isset($_POST['submit'])) {
37         $_POST['feedback'] = trim($_POST['feedback']);
38         $_POST['question'] = trim($_POST['question']);
39         $_POST['tid']      = intval($_POST['tid']);
40         $_POST['qid']      = intval($_POST['qid']);
41         $_POST['weight']   = intval($_POST['weight']);
42
43         if ($_POST['question'] == ''){
44                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
45         }
46
47         if (!$msg->containsErrors()) {
48                 $choice_new = array(); // stores the non-blank choices
49                 $answer_new = array(); // stores the associated "answer" for the choices
50
51                 for ($i=0; $i<10; $i++) {
52                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
53                         /**
54                          * Db defined it to be 255 length, chop strings off it it's less than that
55                          * @harris
56                          */
57                         $_POST['choice'][$i] = Utility::validateLength($_POST['choice'][$i], 255);
58                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
59
60                         if ($_POST['choice'][$i] == '') {
61                                 /* an empty option can't be correct */
62                                 $_POST['answer'][$i] = 0;
63                         } else {
64                                 /* filter out empty choices/ remove gaps */
65                                 $choice_new[] = $_POST['choice'][$i];
66                                 $answer_new[] = $_POST['answer'][$i];
67                         }
68                 }
69
70                 $_POST['answer'] = $answer_new;
71                 $_POST['choice'] = $choice_new;
72                 $_POST['answer'] = array_pad($_POST['answer'], 10, 0);
73                 $_POST['choice'] = array_pad($_POST['choice'], 10, '');
74
75                 $_POST['feedback']   = $addslashes($_POST['feedback']);
76                 $_POST['question']   = $addslashes($_POST['question']);
77
78                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
79             category_id=$_POST[category_id],
80                     feedback='$_POST[feedback]',
81                         question='$_POST[question]',
82                         choice_0='{$_POST[choice][0]}',
83                         choice_1='{$_POST[choice][1]}',
84                         choice_2='{$_POST[choice][2]}',
85                         choice_3='{$_POST[choice][3]}',
86                         choice_4='{$_POST[choice][4]}',
87                         choice_5='{$_POST[choice][5]}',
88                         choice_6='{$_POST[choice][6]}',
89                         choice_7='{$_POST[choice][7]}',
90                         choice_8='{$_POST[choice][8]}',
91                         choice_9='{$_POST[choice][9]}',
92                         answer_0={$_POST[answer][0]},
93                         answer_1={$_POST[answer][1]},
94                         answer_2={$_POST[answer][2]},
95                         answer_3={$_POST[answer][3]},
96                         answer_4={$_POST[answer][4]},
97                         answer_5={$_POST[answer][5]},
98                         answer_6={$_POST[answer][6]},
99                         answer_7={$_POST[answer][7]},
100                         answer_8={$_POST[answer][8]},
101                         answer_9={$_POST[answer][9]}
102                         WHERE question_id=$_POST[qid]";
103
104                 if ($testsQuestionsDAO->execute($sql)) {
105                         $msg->addFeedback('QUESTION_UPDATED');
106                         if ($_POST['tid']) {
107                                 header('Location: questions.php?tid='.$_POST['tid'].'&_course_id='.$_course_id);                        
108                         } else {
109                                 header('Location: question_db.php?_course_id='.$_course_id);
110                         }
111                         exit;
112                 }
113                 else
114                 {
115                         $msg->addError('DB_NOT_UPDATED');
116                 }
117         }
118 }
119
120 if (!isset($_POST['submit'])) {
121         if (!($row = $testsQuestionsDAO->get($qid))){
122                 require_once(TR_INCLUDE_PATH.'header.inc.php');
123                 $msg->printErrors('ITEM_NOT_FOUND');
124                 require (TR_INCLUDE_PATH.'footer.inc.php');
125                 exit;
126         }
127         $_POST['category_id'] = $row['category_id'];
128         $_POST['feedback']        = $row['feedback'];
129         $_POST['weight']          = $row['weight'];
130         $_POST['question']        = $row['question'];
131
132         for ($i=0; $i<10; $i++) {
133                 $_POST['choice'][$i] = $row['choice_'.$i];
134                 $_POST['answer'][$i] = $row['answer_'.$i];
135         }
136 }
137
138 $onload = 'document.form.category_id.focus();';
139 require_once(TR_INCLUDE_PATH.'header.inc.php');
140
141 $savant->assign('qid', $qid);
142 $savant->assign('tid', $_REQUEST['tid']);
143 $savant->assign('course_id', $_course_id);
144 $savant->display('tests/create_edit_question_multianswer.tmpl.php');
145
146 require (TR_INCLUDE_PATH.'footer.inc.php'); ?>