moved code up one level to eliminate the docs subdirectory
[acontent.git] / tests / edit_question_ordering.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         $missing_fields = array();
38
39         $_POST['feedback']    = trim($_POST['feedback']);
40         $_POST['question']    = trim($_POST['question']);
41         $_POST['category_id'] = intval($_POST['category_id']);
42
43         if ($_POST['question'] == ''){
44                 $missing_fields[] = _AT('question');
45         }
46
47         if (trim($_POST['choice'][0]) == '') {
48                 $missing_fields[] = _AT('item').' 1';
49         }
50         if (trim($_POST['choice'][1]) == '') {
51                 $missing_fields[] = _AT('item').' 2';
52         }
53
54         if ($missing_fields) {
55                 $missing_fields = implode(', ', $missing_fields);
56                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
57         }
58         if (!$msg->containsErrors()) {
59                 $_POST['question'] = $addslashes($_POST['question']);
60                 $_POST['feedback'] = $addslashes($_POST['feedback']);
61
62                 $choice_new = array(); // stores the non-blank choices
63                 $answer_new = array(); // stores the non-blank answers
64                 $order = 0; // order count
65                 for ($i=0; $i<10; $i++) {
66                         /**
67                          * Db defined it to be 255 length, chop strings off it it's less than that
68                          * @harris
69                          */
70                         $_POST['choice'][$i] = Utility::validateLength($_POST['choice'][$i], 255);
71                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
72
73                         if ($_POST['choice'][$i] != '') {
74                                 /* filter out empty choices/ remove gaps */
75                                 $choice_new[] = $_POST['choice'][$i];
76                                 $answer_new[] = $order++;
77                         }
78                 }
79                 
80                 $_POST['choice']   = array_pad($choice_new, 10, '');
81                 $answer_new        = array_pad($answer_new, 10, 0);
82
83                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
84                         category_id=$_POST[category_id],
85                         feedback='$_POST[feedback]',
86                         question='$_POST[question]',
87                         choice_0='{$_POST[choice][0]}',
88                         choice_1='{$_POST[choice][1]}',
89                         choice_2='{$_POST[choice][2]}',
90                         choice_3='{$_POST[choice][3]}',
91                         choice_4='{$_POST[choice][4]}',
92                         choice_5='{$_POST[choice][5]}',
93                         choice_6='{$_POST[choice][6]}',
94                         choice_7='{$_POST[choice][7]}',
95                         choice_8='{$_POST[choice][8]}',
96                         choice_9='{$_POST[choice][9]}',
97                         answer_0=$answer_new[0],
98                         answer_0=$answer_new[1],
99                         answer_0=$answer_new[2],
100                         answer_0=$answer_new[3],
101                         answer_0=$answer_new[4],
102                         answer_0=$answer_new[5],
103                         answer_0=$answer_new[6],
104                         answer_0=$answer_new[7],
105                         answer_0=$answer_new[8],
106                         answer_0=$answer_new[9]
107
108                         WHERE question_id=$_POST[qid]";
109                 
110                 if ($testsQuestionsDAO->execute($sql)) {
111                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
112                         if ($_POST['tid']) {
113                                 header('Location: questions.php?tid='.$_POST['tid'].'&_course_id='.$_course_id);                        
114                         } else {
115                                 header('Location: question_db.php?_course_id='.$_course_id);
116                         }
117                         exit;
118                 }
119                 else
120                         $msg->addError('DB_NOT_UPDATED');
121         }
122 } else {
123         if (!($row = $testsQuestionsDAO->get($qid))){
124                 require_once(TR_INCLUDE_PATH.'header.inc.php');
125                 $msg->printErrors('ITEM_NOT_FOUND');
126                 require (TR_INCLUDE_PATH.'footer.inc.php');
127                 exit;
128         }
129
130         $_POST['required']              = $row['required'];
131         $_POST['question']              = $row['question'];
132         $_POST['category_id']   = $row['category_id'];
133         $_POST['feedback']              = $row['feedback'];
134
135         for ($i=0; $i<10; $i++) {
136                 $_POST['choice'][$i] = $row['choice_'.$i];
137         }
138 }
139
140 $onload = 'document.form.category_id.focus();';
141 require_once(TR_INCLUDE_PATH.'header.inc.php');
142
143 $savant->assign('qid', $qid);
144 $savant->assign('tid', $_REQUEST['tid']);
145 $savant->assign('course_id', $_course_id);
146 $savant->display('tests/create_edit_question_ordering.tmpl.php');
147
148 require (TR_INCLUDE_PATH.'footer.inc.php');  ?>