move code up one directory
[atutor.git] / mods / _core / courses / admin / default_mods.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
15 define('AT_INCLUDE_PATH', '../../../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 admin_authenticate(AT_ADMIN_PRIV_ADMIN);
18
19 if (isset($_POST['cancel'])) {
20         $msg->addFeedback('CANCELLED');
21         header('Location: courses.php');
22         exit;
23 }
24
25 if (isset($_POST['up'])) {
26         $up = key($_POST['up']);
27         $_new_modules  = array();
28         if (isset($_POST['main'])) {
29                 foreach ($_POST['main'] as $m) {
30                         if ($m == $up) {
31                                 $last_m = array_pop($_new_modules);
32                                 $_new_modules[] = $m;
33                                 $_new_modules[] = $last_m;
34                         } else {
35                                 $_new_modules[] = $m;
36                         }
37                 }
38
39                 $_POST['main'] = $_new_modules;
40         }
41         if (isset($_POST['home'])) {
42                 $_new_modules  = array();
43                 foreach ($_POST['home'] as $m) {
44                         if ($m == $up) {
45                                 $last_m = array_pop($_new_modules);
46                                 $_new_modules[] = $m;
47                                 $_new_modules[] = $last_m;
48                         } else {
49                                 $_new_modules[] = $m;
50                         }
51                 }
52                 $_POST['home'] = $_new_modules;
53         }
54
55         $_POST['submit'] = TRUE;
56 } else if (isset($_POST['down'])) {
57         $_new_modules  = array();
58
59         $down = key($_POST['down']);
60
61         if (isset($_POST['main'])) {
62                 foreach ($_POST['main'] as $m) {
63                         if ($m == $down) {
64                                 $found = TRUE;
65                                 continue;
66                         }
67                         $_new_modules[] = $m;
68                         if ($found) {
69                                 $_new_modules[] = $down;
70                                 $found = FALSE;
71                         }
72                 }
73
74                 $_POST['main'] = $_new_modules;
75         }
76
77         if (isset($_POST['home'])) {
78                 $_new_modules  = array();
79                 foreach ($_POST['home'] as $m) {
80                         if ($m == $down) {
81                                 $found = TRUE;
82                                 continue;
83                         }
84                         $_new_modules[] = $m;
85                         if ($found) {
86                                 $_new_modules[] = $down;
87                                 $found = FALSE;
88                         }
89                 }
90
91                 $_POST['home'] = $_new_modules;
92         }
93
94         $_POST['submit'] = TRUE;
95 }
96
97 if (isset($_POST['submit'])) {
98         if (isset($_POST['main'])) {
99                 $_POST['main'] = array_unique($_POST['main']);
100                 $_POST['main'] = array_filter($_POST['main']); // remove empties
101                 $main_defaults = implode('|', $_POST['main']);
102
103         } else {
104                 $main_defaults = '';
105         }
106
107         if (isset($_POST['home'])) {
108                 $_POST['home'] = array_unique($_POST['home']);
109                 $_POST['home'] = array_filter($_POST['home']); // remove empties
110                 $home_defaults = implode('|', $_POST['home']);
111         } else {
112                 $home_defaults = '';
113         }
114
115         if (!($_config_defaults['main_defaults'] == $main_defaults) && (strlen($main_defaults) < 256)) {
116                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('main_defaults', '$main_defaults')";
117                 $result = mysql_query($sql, $db);
118
119                 $sql    = "DELETE FROM ".TABLE_PREFIX."config WHERE name='main_defaults_2'";
120         } else if (!($_config_defaults['main_defaults'] == $main_defaults) && (strlen($main_defaults) > 255)) {
121                 // we don't have to worry about chopping in the middle since they'll be combined anyway
122                 $main_defaults_1 = substr($main_defaults, 0, 255);
123                 $main_defaults_2 = substr($main_defaults, 255);
124                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('main_defaults', '$main_defaults_1')";
125                 $result = mysql_query($sql, $db);
126
127                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('main_defaults_2', '$main_defaults_2')";
128         } else if ($_config_defaults['main_defaults'] == $main_defaults) {
129                 $sql    = "DELETE FROM ".TABLE_PREFIX."config WHERE name='main_defaults' OR name='name_defaults_2'";
130         }
131         $result = mysql_query($sql, $db);
132
133
134         if (!($_config_defaults['home_defaults'] == $home_defaults) && (strlen($home_defaults) < 256)) {
135                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('home_defaults', '$home_defaults')";
136                 $result = mysql_query($sql, $db);
137
138                 $sql    = "DELETE FROM ".TABLE_PREFIX."config WHERE name='home_defaults_2'";
139
140         } else  if (!($_config_defaults['home_defaults'] == $home_defaults) && (strlen($home_defaults) > 255)) {
141                 // we don't have to worry about chopping in the middle since they'll be combined anyway
142                 $home_defaults_1 = substr($home_defaults, 0, 255);
143                 $home_defaults_2 = substr($home_defaults, 255);
144                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('home_defaults', '$home_defaults_1')";
145                 $result = mysql_query($sql, $db);
146
147                 $sql    = "REPLACE INTO ".TABLE_PREFIX."config VALUES('home_defaults_2', '$home_defaults_2')";
148
149         } else if ($_config_defaults['home_defaults'] == $home_defaults) {
150                 $sql    = "DELETE FROM ".TABLE_PREFIX."config WHERE name='home_defaults' OR name='home_defaults_2'";
151         }
152         $result = mysql_query($sql, $db);
153
154         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
155         header('Location: '.$_SERVER['PHP_SELF']);
156         exit;
157 }
158
159
160 require(AT_INCLUDE_PATH.'header.inc.php');
161
162 $main_defaults = explode('|', $_config['main_defaults']);
163 $home_defaults = explode('|', $_config['home_defaults']);
164
165 $main_defaults = array_filter($main_defaults); // remove empties
166 $home_defaults = array_filter($home_defaults); // remove empties
167 ?>
168 <?php 
169 $module_list = $moduleFactory->getModules(AT_MODULE_STATUS_ENABLED);
170 $keys = array_keys($module_list);
171
172 foreach ($keys as $dir_name) {
173         $module =& $module_list[$dir_name]; 
174
175         if ($module->getStudentTools()) {
176                 $student_tools[] = $module->getStudentTools();
177         }
178 }
179
180 $count = 0;
181
182 //main mods
183 $_current_modules = $main_defaults;
184 $num_main    = count($_current_modules);
185 //main and home merged
186 $_current_modules = array_merge($_current_modules, array_diff($home_defaults, $main_defaults));
187 $num_modules = count($_current_modules);
188 //all other mods
189 $_current_modules = array_merge($_current_modules, array_diff($student_tools, $_current_modules));
190
191
192 ?>
193
194 <?php 
195 $savant->assign('current_modules', $_current_modules);
196 $savant->assign('home_defaults', $home_defaults);
197 $savant->assign('main_defaults', $main_defaults);
198 $savant->assign('num_modules', $num_modules);
199 $savant->assign('num_main', $num_main);
200 $savant->assign('pages', $_pages);
201 $savant->display('admin/courses/default_mods.tmpl.php');
202 require(AT_INCLUDE_PATH.'footer.inc.php'); ?>