move code up one directory
[atutor.git] / mods / _standard / tests / add_test_questions_confirm.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
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 // $Id$
13
14 $page = 'tests';
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17
18 authenticate(AT_PRIV_TESTS);
19
20 $tid = intval($_POST['tid']);
21
22 $_pages['mods/_standard/tests/questions.php?tid='.$tid]['title_var']    = 'questions';
23 $_pages['mods/_standard/tests/questions.php?tid='.$tid]['parent']   = 'mods/_standard/tests/index.php';
24 $_pages['mods/_standard/tests/questions.php?tid='.$tid]['children'] = array('mods/_standard/tests/add_test_questions.php?tid='.$tid);
25
26 $_pages['mods/_standard/tests/add_test_questions.php?tid='.$tid]['title_var']  = 'add_questions';
27 $_pages['mods/_standard/tests/add_test_questions.php?tid='.$tid]['parent'] = 'mods/_standard/tests/questions.php?tid='.$tid;
28
29 $_pages['mods/_standard/tests/add_test_questions_confirm.php']['title_var'] = 'add_questions';
30 $_pages['mods/_standard/tests/add_test_questions_confirm.php']['parent']    = 'mods/_standard/tests/questions.php?tid='.$tid;
31
32 if (isset($_POST['cancel'])) {
33         $msg->addFeedback('CANCELLED');
34         header('Location: questions.php?tid='.$tid);
35         exit;
36 } else if (isset($_POST['submit_yes'])) {
37         //get order
38         $sql = "SELECT MAX(ordering) AS max_ordering FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=".$tid;
39         $result = mysql_query($sql, $db);
40         $order  = mysql_fetch_assoc($result);
41         $order = $order['max_ordering'];
42
43         $sql = "REPLACE INTO ".TABLE_PREFIX."tests_questions_assoc VALUES ";
44         foreach ($_POST['questions'] as $question) {
45                 $order++;
46                 $question = intval($question);
47                 $sql .= '('.$tid.', '.$question.', 0, '.$order.', 0),';
48         }
49         $sql = substr($sql, 0, -1);
50         $result = mysql_query($sql, $db);
51
52         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
53         header('Location: questions.php?tid='.$tid);
54         exit;
55 } else if (isset($_POST['submit_no'])) {
56         $msg->addFeedback('CANCELLED');
57         header('Location: add_test_questions.php?tid='.$tid);
58         exit;
59 }
60
61 if (!is_array($_POST['questions']) || !count($_POST['questions'])) {
62         $msg->addError('NO_QUESTIONS_SELECTED');
63         header('Location: add_test_questions.php?tid='.$tid);
64         require(AT_INCLUDE_PATH.'footer.inc.php');
65         exit;
66 }
67
68 require(AT_INCLUDE_PATH.'header.inc.php');
69
70 foreach ($_POST['questions'] as $id => $cat_array) {
71         foreach ($cat_array as $idx => $q) {
72                 $_POST['questions'][$id][$idx] = intval($q);
73         }
74 }
75 foreach ($_POST['questions'] as $cat_array) {
76         $questions .= addslashes(implode(',',$cat_array)).',';
77 }
78
79 $questions = substr($questions, 0, -1);
80
81 $sql = "SELECT question, question_id FROM ".TABLE_PREFIX."tests_questions WHERE question_id IN ($questions) AND course_id=$_SESSION[course_id] ORDER BY question";
82 $result = mysql_query($sql, $db);
83
84 $questions = '';
85 while ($row = mysql_fetch_assoc($result)) {
86         $questions .= '<li>'.htmlspecialchars($row['question']).'</li>';
87         $questions_array['questions['.$row['question_id'].']'] = $row['question_id'];
88 }
89 $questions_array['tid'] = $_POST['tid'];
90 $msg->addConfirm(array('ADD_TEST_QUESTIONS', $questions), $questions_array);
91
92 $msg->printConfirm();
93 ?>
94
95 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>