5e55eda4ff9f700b00a3bc191835a88b80368b3f
[atutor.git] / docs / mods / _standard / tests / delete_test.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
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                         //delete test content association as well
37                         $sql    = "DELETE FROM ".TABLE_PREFIX."content_tests_assoc WHERE test_id=$tid";
38                         $result = mysql_query($sql, $db);
39
40                         /* it has to delete the results as well... */
41                         $sql    = "SELECT result_id FROM ".TABLE_PREFIX."tests_results WHERE test_id=$tid";
42                         $result = mysql_query($sql, $db);
43                         if ($row = mysql_fetch_array($result)) {
44                                 $result_list = '('.$row['result_id'];
45
46                                 while ($row = mysql_fetch_array($result)) {
47                                         $result_list .= ','.$row['result_id'];
48                                 }
49                                 $result_list .= ')';
50                         }
51
52                         if ($result_list != '') {
53                                 $sql    = "DELETE FROM ".TABLE_PREFIX."tests_answers WHERE result_id IN $result_list";
54                                 $result = mysql_query($sql, $db);
55
56
57                                 $sql    = "DELETE FROM ".TABLE_PREFIX."tests_results WHERE test_id=$tid";
58                                 $result = mysql_query($sql, $db);
59                         }
60                 }
61
62                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
63                 header('Location: '.AT_BASE_HREF.'mods/_standard/tests/index.php');
64                 exit;
65
66         } /* else: */
67
68         require(AT_INCLUDE_PATH.'header.inc.php');
69
70         $_GET['tid'] = intval($_GET['tid']);
71
72         $sql    = "SELECT title FROM ".TABLE_PREFIX."tests WHERE test_id=$_GET[tid] AND course_id=$_SESSION[course_id]";
73         $result = mysql_query($sql, $db);
74         $row    = mysql_fetch_array($result);
75
76         unset($hidden_vars);
77         $hidden_vars['tid'] = $_GET['tid'];
78
79         $msg->addConfirm(array('DELETE_TEST', $row['title']), $hidden_vars);
80         $msg->printConfirm();
81
82         require(AT_INCLUDE_PATH.'footer.inc.php');
83 ?>