tagging as ATutor 1.5.4-release
[atutor.git] / tools / tests / delete_test.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2007 by Greg Gay & Joel Kronenberg        */
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 $page = 'tests';
15 define('AT_INCLUDE_PATH', '../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_TESTS);
18
19
20         
21         if (isset($_POST['submit_no'])) {
22                 $msg->addFeedback('CANCELLED');
23                 header('Location: index.php');
24                 exit;
25         } else if (isset($_POST['submit_yes'])) {
26                 
27                 $tid = intval($_POST['tid']);
28
29                 $sql    = "DELETE FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
30                 $result = mysql_query($sql, $db);
31
32                 if (mysql_affected_rows($db) == 1) {
33                         $sql    = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid";
34                         $result = mysql_query($sql, $db);
35
36                         /* it has to delete the results as well... */
37                         $sql    = "SELECT result_id FROM ".TABLE_PREFIX."tests_results WHERE test_id=$tid";
38                         $result = mysql_query($sql, $db);
39                         if ($row = mysql_fetch_array($result)) {
40                                 $result_list = '('.$row['result_id'];
41
42                                 while ($row = mysql_fetch_array($result)) {
43                                         $result_list .= ','.$row['result_id'];
44                                 }
45                                 $result_list .= ')';
46                         }
47
48                         if ($result_list != '') {
49                                 $sql    = "DELETE FROM ".TABLE_PREFIX."tests_answers WHERE result_id IN $result_list";
50                                 $result = mysql_query($sql, $db);
51
52
53                                 $sql    = "DELETE FROM ".TABLE_PREFIX."tests_results WHERE test_id=$tid";
54                                 $result = mysql_query($sql, $db);
55                         }
56                 }
57
58                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
59                 header('Location: '.AT_BASE_HREF.'tools/tests/index.php');
60                 exit;
61
62         } /* else: */
63
64         require(AT_INCLUDE_PATH.'header.inc.php');
65
66         $_GET['tid'] = intval($_GET['tid']);
67
68         $sql    = "SELECT title FROM ".TABLE_PREFIX."tests WHERE test_id=$_GET[tid] AND course_id=$_SESSION[course_id]";
69         $result = mysql_query($sql, $db);
70         $row    = mysql_fetch_array($result);
71
72         unset($hidden_vars);
73         $hidden_vars['tid'] = $_GET['tid'];
74
75         $msg->addConfirm(array('DELETE_TEST', $row['title']), $hidden_vars);
76         $msg->printConfirm();
77
78         require(AT_INCLUDE_PATH.'footer.inc.php');
79 ?>