6734cadd9bddc0e41ce4bae2b4a4a52246d8a51a
[acontent.git] / docs / tests / create_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.'lib/test_question_queries.inc.php');
17 require_once(TR_INCLUDE_PATH.'classes/DAO/TestsQuestionsDAO.class.php');
18 require_once(TR_INCLUDE_PATH.'classes/Utility.class.php');
19
20 global $_course_id;
21 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
22
23 $testsQuestionsDAO = new TestsQuestionsDAO();
24
25 if (isset($_POST['cancel'])) {
26         $msg->addFeedback('CANCELLED');
27         header('Location: question_db.php?_course_id='.$_course_id);
28         exit;
29 } else if (isset($_POST['submit'])) {
30         $_POST['question']    = trim($_POST['question']);
31         $_POST['category_id'] = intval($_POST['category_id']);
32
33         $empty_fields = array();
34         if ($_POST['question'] == ''){
35                 $empty_fields[] = _AT('question');
36         }
37         if ($_POST['choice'][0] == '') {
38                 $empty_fields[] = _AT('choice').' 1';
39         }
40
41         if ($_POST['choice'][1] == '') {
42                 $empty_fields[] = _AT('choice').' 2';
43         }
44
45         if (!empty($empty_fields)) {
46                 $msg->addError(array('EMPTY_FIELDS', implode(', ', $empty_fields)));
47         }
48
49         if (!$msg->containsErrors()) {
50                 $_POST['feedback']   = '';
51                 $_POST['question']   = $addslashes($_POST['question']);
52
53                 for ($i=0; $i<10; $i++) {
54                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
55                         $_POST['answer'][$i] = intval($_POST['answer'][$i]);
56
57                         if ($_POST['choice'][$i] == '') {
58                                 /* an empty option can't be correct */
59                                 $_POST['answer'][$i] = 0;
60                         }
61                 }
62                 
63                 $sql_params = array(    $_POST['category_id'], 
64                                                                 $_course_id,
65                                                                 $_POST['feedback'], 
66                                                                 $_POST['question'], 
67                                                                 $_POST['choice'][0], 
68                                                                 $_POST['choice'][1], 
69                                                                 $_POST['choice'][2], 
70                                                                 $_POST['choice'][3], 
71                                                                 $_POST['choice'][4], 
72                                                                 $_POST['choice'][5], 
73                                                                 $_POST['choice'][6], 
74                                                                 $_POST['choice'][7], 
75                                                                 $_POST['choice'][8], 
76                                                                 $_POST['choice'][9], 
77                                                                 $_POST['answer'][0], 
78                                                                 $_POST['answer'][1], 
79                                                                 $_POST['answer'][2], 
80                                                                 $_POST['answer'][3], 
81                                                                 $_POST['answer'][4], 
82                                                                 $_POST['answer'][5], 
83                                                                 $_POST['answer'][6], 
84                                                                 $_POST['answer'][7], 
85                                                                 $_POST['answer'][8], 
86                                                                 $_POST['answer'][9]);
87
88                 $sql = vsprintf(TR_SQL_QUESTION_LIKERT, $sql_params);
89                 if ($testsQuestionsDAO->execute($sql)) {
90                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
91                         header('Location: question_db.php?_course_id='.$_course_id);
92                         exit;
93                 }
94                 else {
95                         $msg->addError('DB_NOT_UPDATED');
96                 }
97         }
98 } else if (isset($_POST['preset'])) {
99         // load preset
100         $_POST['preset_num'] = intval($_POST['preset_num']);
101
102         if (isset($_likert_preset[$_POST['preset_num']])) {
103                 $_POST['choice'] = $_likert_preset[$_POST['preset_num']];
104         } else if ($_POST['preset_num']) {
105                 $row = $testsQuestionsDAO->get($_POST[preset_num]);
106                 if (isset($row)) {
107                         for ($i=0; $i<10; $i++) {
108                                 $_POST['choice'][$i] = $row['choice_' . $i];
109                         }
110                 }
111         }
112 }
113
114 global $onload;
115 $onload = 'document.form.category_id.focus();';
116
117 require_once(TR_INCLUDE_PATH.'header.inc.php');
118
119 $savant->assign('likert_preset', $_likert_preset);
120 $savant->assign('testsQuestionsDAO', $testsQuestionsDAO);
121 $savant->assign('course_id', $_course_id);
122 $savant->display('tests/create_edit_question_likert.tmpl.php');
123
124 require (TR_INCLUDE_PATH.'footer.inc.php');  ?>