moved code up one level to eliminate the docs subdirectory
[acontent.git] / tests / edit_question_matching.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 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
21 $testsQuestionsDAO = new TestsQuestionsDAO();
22
23 // for matching test questions
24 $_letters = array(_AT('a'), _AT('b'), _AT('c'), _AT('d'), _AT('e'), _AT('f'), _AT('g'), _AT('h'), _AT('i'), _AT('j'));
25
26 $qid = intval($_GET['qid']);
27 if ($qid == 0){
28         $qid = intval($_POST['qid']);
29 }
30
31 if (isset($_POST['cancel'])) {
32         $msg->addFeedback('CANCELLED');
33         if ($_POST['tid']) {
34                 header('Location: questions.php?tid='.$_POST['tid'].'&_course_id='.$_course_id);                        
35         } else {
36                 header('Location: question_db.php?_course_id='.$_course_id);
37         }
38         exit;
39 } else if (isset($_POST['submit'])) {
40         $_POST['tid']          = intval($_POST['tid']);
41         $_POST['qid']          = intval($_POST['qid']);
42         $_POST['feedback']     = trim($_POST['feedback']);
43         $_POST['instructions'] = trim($_POST['instructions']);
44         $_POST['category_id']  = intval($_POST['category_id']);
45
46         for ($i = 0 ; $i < 10; $i++) {
47                 $_POST['question'][$i]        = $addslashes(trim($_POST['question'][$i]));
48                 $_POST['question_answer'][$i] = (int) $_POST['question_answer'][$i];
49                 $_POST['answer'][$i]          = $addslashes(trim($_POST['answer'][$i]));
50         }
51
52         if (!$_POST['question'][0] 
53                 || !$_POST['question'][1] 
54                 || !$_POST['answer'][0] 
55                 || !$_POST['answer'][1]) {
56
57                 $msg->addError('QUESTION_EMPTY');
58         }
59
60         if (!$msg->containsErrors()) {
61                 $_POST['feedback']     = $addslashes($_POST['feedback']);
62                 $_POST['instructions'] = $addslashes($_POST['instructions']);
63                 
64                 $sql    = "UPDATE ".TABLE_PREFIX."tests_questions SET
65                         category_id=$_POST[category_id],
66                         feedback='$_POST[feedback]',
67                         question='$_POST[instructions]',
68                         choice_0='{$_POST[question][0]}',
69                         choice_1='{$_POST[question][1]}',
70                         choice_2='{$_POST[question][2]}',
71                         choice_3='{$_POST[question][3]}',
72                         choice_4='{$_POST[question][4]}',
73                         choice_5='{$_POST[question][5]}',
74                         choice_6='{$_POST[question][6]}',
75                         choice_7='{$_POST[question][7]}',
76                         choice_8='{$_POST[question][8]}',
77                         choice_9='{$_POST[question][9]}',
78                         answer_0={$_POST[question_answer][0]},
79                         answer_1={$_POST[question_answer][1]},
80                         answer_2={$_POST[question_answer][2]},
81                         answer_3={$_POST[question_answer][3]},
82                         answer_4={$_POST[question_answer][4]},
83                         answer_5={$_POST[question_answer][5]},
84                         answer_6={$_POST[question_answer][6]},
85                         answer_7={$_POST[question_answer][7]},
86                         answer_8={$_POST[question_answer][8]},
87                         answer_9={$_POST[question_answer][9]},
88                         option_0='{$_POST[answer][0]}',
89                         option_1='{$_POST[answer][1]}',
90                         option_2='{$_POST[answer][2]}',
91                         option_3='{$_POST[answer][3]}',
92                         option_4='{$_POST[answer][4]}',
93                         option_5='{$_POST[answer][5]}',
94                         option_6='{$_POST[answer][6]}',
95                         option_7='{$_POST[answer][7]}',
96                         option_8='{$_POST[answer][8]}',
97                         option_9='{$_POST[answer][9]}'
98
99                         WHERE question_id=$_POST[qid]";
100                 
101                 if ($testsQuestionsDAO->execute($sql)) {
102                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
103                         if ($_POST['tid']) {
104                                 header('Location: questions.php?tid='.$_POST['tid'].'&_course_id='.$_course_id);                        
105                         } else {
106                                 header('Location: question_db.php?_course_id='.$_course_id);
107                         }
108                         exit;
109                 }
110         }
111 } else {
112         if (!($row = $testsQuestionsDAO->get($qid))){
113                 require_once(TR_INCLUDE_PATH.'header.inc.php');
114                 $msg->printErrors('ITEM_NOT_FOUND');
115                 require (TR_INCLUDE_PATH.'footer.inc.php');
116                 exit;
117         }
118         $_POST['feedback']              = $row['feedback'];
119         $_POST['instructions']  = $row['question'];
120         $_POST['category_id']   = $row['category_id'];
121
122         for ($i=0; $i<10; $i++) {
123                 $_POST['question'][$i]        = $row['choice_'.$i];
124                 $_POST['question_answer'][$i] = $row['answer_'.$i];
125                 $_POST['answer'][$i]          = $row['option_'.$i];
126         }
127 }
128
129 $onload = 'document.form.category_id.focus();';
130
131 require_once(TR_INCLUDE_PATH.'header.inc.php');
132
133 $savant->assign('qid', $qid);
134 $savant->assign('tid', $_REQUEST['tid']);
135 $savant->assign('letters', $_letters);
136 $savant->assign('course_id', $_course_id);
137 $savant->display('tests/create_edit_question_matching.tmpl.php');
138
139 require (TR_INCLUDE_PATH.'footer.inc.php');  ?>