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