Merge pull request #8 from radiocontrolled/0004872
[atutor.git] / move_module.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002 - 2009                                            */
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('AT_INCLUDE_PATH', 'include/');
14 require(AT_INCLUDE_PATH.'vitals.inc.php');
15
16 global $addslashes;
17
18 // handle ajax post request from course index page and student tools index page
19 if (isset($_POST['from']))
20 {
21         $from = $_POST['from'];
22         if ($_POST['moved_modules'] <> '') $final_home_links = $addslashes(str_replace('-', '/', $_POST['moved_modules']));
23         
24         // when pretty url is turned on, revert the pretty urls back to regular urls
25         if ($_config['pretty_url'] > 0)
26         {
27                 $home_links = explode('|', $final_home_links);
28                 $final_home_links = '';
29                 if (is_array($home_links))
30                 {
31                         foreach ($home_links as $link)
32                         {
33                                 $url_parser = new UrlParser($link);
34                                 $pathinfo = $url_parser->getPathArray();
35                                 $final_home_links .= $pathinfo[1]->getPath(). $pathinfo[1]->getFileName(). '|';
36                         }
37                         $final_home_links = substr($final_home_links, 0, -1);
38                 }
39         }
40 }
41
42 // handle ajax post request to remove module from course index page and student tools index page
43 if ($_POST['remove'] <> '')
44 {
45         $remove_module = $_POST['remove'];
46         
47         // when pretty url is turned on, revert the pretty url back to regular url
48         if ($_config['pretty_url'] > 0)
49         {
50                 if (substr($remove_module, 0, 6) == 'go.php') $remove_module = substr($remove_module, 6);
51                 
52                 $url_parser = new UrlParser($remove_module);
53                 $pathinfo = $url_parser->getPathArray();
54                 $remove_module = $pathinfo[1]->getPath(). $pathinfo[1]->getFileName();
55         }
56
57         if ($from == 'course_index')
58                 $sql = "SELECT home_links links FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
59         else if ($from == 'student_tools')
60                 $sql = "SELECT links FROM ".TABLE_PREFIX."fha_student_tools WHERE course_id=$_SESSION[course_id]";
61
62         $result = mysql_query($sql, $db);
63         $row= mysql_fetch_assoc($result);
64
65         if (substr($row['links'], 0, strlen($remove_module)) == $remove_module)
66                 $final_home_links = substr($row['links'], strlen($remove_module)+1);
67         else
68                 $final_home_links = preg_replace('/\|'.preg_quote($remove_module, '/').'/', '', $row['links']);
69 }
70
71 // save the module display order into db
72 if ($from == 'course_index')
73         $sql = "UPDATE ".TABLE_PREFIX."courses SET home_links='$final_home_links' WHERE course_id=$_SESSION[course_id]";
74 else if ($from == 'student_tools')
75         $sql    = "UPDATE ".TABLE_PREFIX."fha_student_tools SET links='$final_home_links' WHERE course_id=$_SESSION[course_id]";
76
77 $result = mysql_query($sql, $db);
78 ?>