AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / tests / create_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.'lib/test_question_queries.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 if (isset($_POST['cancel'])) {
25         $msg->addFeedback('CANCELLED');
26         header('Location: question_db.php?_course_id='.$_course_id);
27         exit;
28 } else if ($_POST['submit']) {
29         $missing_fields = array();
30
31         $_POST['feedback']    = trim($_POST['feedback']);
32         $_POST['question']    = trim($_POST['question']);
33         $_POST['category_id'] = intval($_POST['category_id']);
34
35         if ($_POST['question'] == ''){
36                 $missing_fields[] = _AT('question');
37         }
38
39         if (trim($_POST['choice'][0]) == '') {
40                 $missing_fields[] = _AT('item').' 1';
41         }
42         if (trim($_POST['choice'][1]) == '') {
43                 $missing_fields[] = _AT('item').' 2';
44         }
45
46         if ($missing_fields) {
47                 $missing_fields = implode(', ', $missing_fields);
48                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
49         }
50
51         if (!$msg->containsErrors()) {
52                 $choice_new = array(); // stores the non-blank choices
53                 $answer_new = array(); // stores the non-blank answers
54                 $order = 0; // order count
55                 for ($i=0; $i<10; $i++) {
56                         /**
57                          * Db defined it to be 255 length, chop strings off it it's less than that
58                          * @harris
59                          */
60                         $_POST['choice'][$i] = Utility::validateLength($_POST['choice'][$i], 255);
61                         $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
62
63                         if ($_POST['choice'][$i] != '') {
64                                 /* filter out empty choices/ remove gaps */
65                                 $choice_new[] = $_POST['choice'][$i];
66                                 $answer_new[] = $order++;
67                         }
68                 }
69
70                 $_POST['choice']   = array_pad($choice_new, 10, '');
71                 $answer_new        = array_pad($answer_new, 10, 0);
72                 $_POST['feedback'] = $addslashes($_POST['feedback']);
73                 $_POST['question'] = $addslashes($_POST['question']);
74         
75                 $sql_params = array(    $_POST['category_id'], 
76                                                                 $_course_id,
77                                                                 $_POST['feedback'], 
78                                                                 $_POST['question'], 
79                                                                 $_POST['choice'][0], 
80                                                                 $_POST['choice'][1], 
81                                                                 $_POST['choice'][2], 
82                                                                 $_POST['choice'][3], 
83                                                                 $_POST['choice'][4], 
84                                                                 $_POST['choice'][5], 
85                                                                 $_POST['choice'][6], 
86                                                                 $_POST['choice'][7], 
87                                                                 $_POST['choice'][8], 
88                                                                 $_POST['choice'][9], 
89                                                                 $answer_new[0], 
90                                                                 $answer_new[1], 
91                                                                 $answer_new[2], 
92                                                                 $answer_new[3], 
93                                                                 $answer_new[4], 
94                                                                 $answer_new[5], 
95                                                                 $answer_new[6], 
96                                                                 $answer_new[7], 
97                                                                 $answer_new[8], 
98                                                                 $answer_new[9]);
99                 $sql = vsprintf(TR_SQL_QUESTION_ORDERING, $sql_params);
100
101                 if ($testsQuestionsDAO->execute($sql)) {
102                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
103                         header('Location: question_db.php?_course_id='.$_course_id);
104                         exit;
105                 }
106                 else
107                         $msg->addError('DB_NOT_UPDATED');
108         }
109 }
110
111 $onload = 'document.form.category_id.focus();';
112
113 require_once(TR_INCLUDE_PATH.'header.inc.php');
114
115 $savant->assign('course_id', $_course_id);
116 $savant->display('tests/create_edit_question_ordering.tmpl.php');
117
118 require (TR_INCLUDE_PATH.'footer.inc.php'); ?>