tagging as ATutor 1.5.4-release
[atutor.git] / tools / tests / add_test_questions_confirm.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 $page = 'tests';
16 define('AT_INCLUDE_PATH', '../../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18
19 authenticate(AT_PRIV_TESTS);
20
21 $tid = intval($_POST['tid']);
22
23 $_pages['tools/tests/questions.php?tid='.$tid]['title_var']    = 'questions';
24 $_pages['tools/tests/questions.php?tid='.$tid]['parent']   = 'tools/tests/index.php';
25 $_pages['tools/tests/questions.php?tid='.$tid]['children'] = array('tools/tests/add_test_questions.php?tid='.$tid);
26
27 $_pages['tools/tests/add_test_questions.php?tid='.$tid]['title_var']  = 'add_questions';
28 $_pages['tools/tests/add_test_questions.php?tid='.$tid]['parent'] = 'tools/tests/questions.php?tid='.$tid;
29
30 $_pages['tools/tests/add_test_questions_confirm.php']['title_var'] = 'add_questions';
31 $_pages['tools/tests/add_test_questions_confirm.php']['parent']    = 'tools/tests/questions.php?tid='.$tid;
32
33 if (isset($_POST['cancel'])) {
34         $msg->addFeedback('CANCELLED');
35         header('Location: questions.php?tid='.$tid);
36         exit;
37 } else if (isset($_POST['submit_yes'])) {
38         //get order
39         $sql = "SELECT MAX(ordering) AS max_ordering FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=".$tid;
40         $result = mysql_query($sql, $db);
41         $order  = mysql_fetch_assoc($result);
42         $order = $order['max_ordering'];
43
44         $sql = "REPLACE INTO ".TABLE_PREFIX."tests_questions_assoc VALUES ";
45         foreach ($_POST['questions'] as $question) {
46                 $order++;
47                 $question = intval($question);
48                 $sql .= '('.$tid.', '.$question.', 0, '.$order.', 0),';
49         }
50         $sql = substr($sql, 0, -1);
51         $result = mysql_query($sql, $db);
52
53         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
54         header('Location: questions.php?tid='.$tid);
55         exit;
56 } else if (isset($_POST['submit_no'])) {
57         $msg->addFeedback('CANCELLED');
58         header('Location: add_test_questions.php?tid='.$tid);
59         exit;
60 }
61
62 if (!is_array($_POST['questions']) || !count($_POST['questions'])) {
63         $msg->addError('NO_QUESTIONS_SELECTED');
64         header('Location: add_test_questions.php?tid='.$tid);
65         require(AT_INCLUDE_PATH.'footer.inc.php');
66         exit;
67 }
68
69 require(AT_INCLUDE_PATH.'header.inc.php');
70
71 foreach ($_POST['questions'] as $id => $cat_array) {
72         foreach ($cat_array as $idx => $q) {
73                 $_POST['questions'][$id][$idx] = intval($q);
74         }
75 }
76 foreach ($_POST['questions'] as $cat_array) {
77         $questions .= addslashes(implode(',',$cat_array)).',';
78 }
79
80 $questions = substr($questions, 0, -1);
81
82 $sql = "SELECT question, question_id FROM ".TABLE_PREFIX."tests_questions WHERE question_id IN ($questions) AND course_id=$_SESSION[course_id] ORDER BY question";
83 $result = mysql_query($sql, $db);
84
85 $questions = '';
86 while ($row = mysql_fetch_assoc($result)) {
87         $questions .= '<li>'.htmlspecialchars($row['question']).'</li>';
88         $questions_array['questions['.$row['question_id'].']'] = $row['question_id'];
89 }
90 $questions_array['tid'] = $_POST['tid'];
91 $msg->addConfirm(array('ADD_TEST_QUESTIONS', $questions), $questions_array);
92
93 $msg->printConfirm();
94 ?>
95
96 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>