made a copy
[atutor.git] / install / include / ustep3.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg, Heidi Hazelton */
6 /* http://atutor.ca                                                                                                             */
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 // $Id$
13
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 $_POST['db_login']    = urldecode($_POST['db_login']);
17 $_POST['db_password'] = urldecode($_POST['db_password']);
18 /* Destory session */
19 session_unset();
20 $_SESSION= array();
21 if(isset($_POST['submit']) && ($_POST['action'] == 'process')) {
22         unset($errors);
23         $db = @mysql_connect($_POST['step1']['db_host'] . ':' . $_POST['step1']['db_port'], $_POST['step1']['db_login'], urldecode($_POST['step1']['db_password']));
24         @mysql_select_db($_POST['step1']['db_name'], $db);
25
26         if (version_compare($_POST['step1']['old_version'], '1.5', '<')) {
27                 $_POST['admin_username'] = trim($_POST['admin_username']);
28                 $_POST['admin_password'] = trim($_POST['admin_password']);
29                 $_POST['admin_email']    = trim($_POST['admin_email']);
30                 $_POST['site_name']      = trim($_POST['site_name']);
31                 $_POST['home_url']           = trim($_POST['home_url']);
32
33                 /* Super Administrator Account checking: */
34                 if ($_POST['admin_username'] == ''){
35                         $errors[] = 'Administrator username cannot be empty.';
36                 } else {
37                         /* check for special characters */
38                         if (!(preg_match("/^[a-zA-Z0-9_]([a-zA-Z0-9_])*$/i", $_POST['admin_username']))){
39                                 $errors[] = 'Administrator username is not valid.';
40                         }
41                 }
42                 if ($_POST['admin_password'] == '') {
43                         $errors[] = 'Administrator password cannot be empty.';
44                 }
45                 if ($_POST['admin_email'] == '') {
46                         $errors[] = 'Administrator email cannot be empty.';
47                 } else if (!preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", $_POST['admin_email'])) {
48                         $errors[] = 'Administrator email is not valid.';
49                 }
50
51                 /* System Preferences checking: */
52                 if ($_POST['email'] == '') {
53                         $errors[] = 'Contact email cannot be empty.';
54                 } else if (!preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", $_POST['email'])) {
55                         $errors[] = 'Contact email is not valid.';
56                 }
57
58                 if (!isset($errors)) {
59                         $sql = "INSERT INTO ".$_POST['step1']['tb_prefix']."admins VALUES ('$_POST[admin_username]', '$_POST[admin_password]', '', '$_POST[admin_email]', 'en', 1, NOW())";
60                         $result= mysql_query($sql, $db);
61
62                         unset($_POST['admin_username']);
63                         unset($_POST['admin_password']);
64                         unset($_POST['admin_email']);
65                 }
66         }
67         if (version_compare($_POST['step1']['old_version'], '1.5.2', '<')) {
68                 // update config table
69                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('contact_email', '".urldecode($_POST['step1']['contact_email'])."')";
70                 mysql_query($sql, $db);
71
72                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('email_notification', '".($_POST['step1']['email_notification'] ? 1 : 0)."')";
73                 mysql_query($sql, $db);
74
75                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('allow_instructor_requests', '".($_POST['step1']['allow_instructor_requests'] ? 1 : 0)."')";
76                 mysql_query($sql, $db);
77
78                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('auto_approve_instructors', '".($_POST['step1']['auto_approve'] ? 1 : 0)."')";
79                 mysql_query($sql, $db);
80
81                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('max_file_size', '".(int) $_POST['step1']['max_file_size']."')";
82                 mysql_query($sql, $db);
83
84                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('max_course_size', '".(int) $_POST['step1']['max_course_size']."')";
85                 mysql_query($sql, $db);
86
87                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('max_course_float', '".(int) $_POST['step1']['max_course_float']."')";
88                 mysql_query($sql, $db);
89
90                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('illegal_extentions', '".str_replace(',','|',urldecode($_POST['step1']['ill_ext']))."')";
91                 mysql_query($sql, $db);
92
93                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('site_name', '".urldecode($_POST['step1']['site_name'])."')";
94                 mysql_query($sql, $db);
95
96                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('home_url', '".urldecode($_POST['step1']['home_url'])."')";
97                 mysql_query($sql, $db);
98
99                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('default_language', 'en')";
100                 mysql_query($sql, $db);
101
102                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('cache_dir', '".urldecode($_POST['step1']['cache_dir'])."')";
103                 mysql_query($sql, $db);
104
105                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('enable_category_themes', '".($_POST['step1']['theme_categories'] ? 1 : 0)."')";
106                 mysql_query($sql, $db);
107
108                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('course_backups', '". (int) $_POST['step1']['course_backups']."')";
109                 mysql_query($sql, $db);
110
111                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('email_confirmation', '".($_POST['step1']['email_confirmation'] ? 1 : 0)."')";
112                 mysql_query($sql, $db);
113
114                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('master_list', '".($_POST['step1']['master_list'] ? 1 : 0)."')";
115                 mysql_query($sql, $db);
116
117                 $sql = "REPLACE INTO ".$_POST['step1']['tb_prefix']."config VALUES ('enable_handbook_notes', '".($_POST['step1']['enable_handbook_notes'] ? 1 : 0)."')";
118                 mysql_query($sql, $db);
119
120                 // check for bits 8192 and 4096 and remove them if they're set.
121                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."course_enrollment SET `privileges` = `privileges` - 8192 WHERE `privileges` & 8192";
122                 mysql_query($sql, $db);
123
124                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."course_enrollment SET `privileges` = `privileges` - 4096 WHERE `privileges` & 4096";
125                 mysql_query($sql, $db);
126         }
127
128         if (version_compare($_POST['step1']['old_version'], '1.5.3', '<')) {
129                 $sql = "DELETE FROM ".$_POST['step1']['tb_prefix']."groups";
130                 mysql_query($sql, $db);
131
132                 $sql = "DELETE FROM ".$_POST['step1']['tb_prefix']."groups_members";
133                 mysql_query($sql, $db);
134
135                 $sql = "DELETE FROM ".$_POST['step1']['tb_prefix']."tests_groups";
136                 mysql_query($sql, $db);
137         }
138         if (version_compare($_POST['step1']['old_version'], '1.5.3.3', '<')) {
139                 // set display_name_format to "login"
140                 $sql = "INSERT INTO ".$_POST['step1']['tb_prefix']."config VALUES ('display_name_format', '0')";
141                 mysql_query($sql, $db);
142         }
143
144         if (version_compare($_POST['step1']['old_version'], '1.5.4', '<')) {
145                 /* find all the multiple choice multiple answer questions and convert them to 
146                  * Multiple Answer which is number 7.
147                  */
148                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."tests_questions SET type=7 WHERE type=1 AND answer_0 + answer_1 + answer_2 + answer_3 + answer_4 + answer_5 + answer_6 + answer_7 + answer_8 + answer_9 > 1";
149                 mysql_query($sql, $db);
150
151                 $sql = "SELECT MAX(admin_privilege) AS max FROM ".$_POST['step1']['tb_prefix']."modules";
152                 $result = mysql_query($sql, $db);
153                 $row = mysql_fetch_assoc($result);
154                 $priv = $row['max'] * 2;
155
156                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."modules SET `admin_privilege`=$priv WHERE `dir_name`='_core/enrolment'";
157                 mysql_query($sql, $db);
158         }
159         if (version_compare($_POST['step1']['old_version'], '1.5.5', '<')) {
160                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."tests_results SET status=1, date_taken=date_taken, end_time=date_taken";
161                 mysql_query($sql, $db);
162         }
163         if (version_compare($_POST['step1']['old_version'], '1.6.4', '<')) {
164                 /* convert all content nodes to the IMS standard. (adds null nodes for all top pages) */
165                 include('ustep_content_conversion.php');
166
167                 // fix all the wrong ordering
168                 $sql    = "SELECT content_id, content_parent_id, ordering, course_id FROM ".$_POST['step1']['tb_prefix']."content ORDER BY course_id, content_parent_id, ordering";
169                 $result = mysql_query($sql, $db);
170                 while ($row = mysql_fetch_assoc($result)) {
171                         if ($current_course_id != $row['course_id']) {
172                                 $current_course_id = $row['course_id'];
173                                 unset($current_parent_id);
174                                 unset($ordering);
175                         }
176                         if ($current_parent_id != $row['content_parent_id']) {
177                                 $current_parent_id = $row['content_parent_id'];
178                                 $ordering = 1;
179                         }
180                 
181                         if ($row['ordering'] != $ordering) {
182                                 $sql = "UPDATE ".$_POST['step1']['tb_prefix']."content SET ordering=$ordering WHERE content_id=$row[content_id]";
183                                 mysql_query($sql, $db);
184                         }
185                 
186                          echo "\n";
187                 
188                         $ordering++;
189                 }
190                 
191                 /* Convert db to a tree */
192                 $sql = 'SELECT distinct course_id FROM '.$_POST['step1']['tb_prefix'].'content';
193                 $result_course = mysql_query($sql, $db);
194                 while ($row_course = mysql_fetch_assoc($result_course)){
195                         
196                         $sql = 'SELECT * FROM '.$_POST['step1']['tb_prefix'].'content WHERE course_id='.$row_course['course_id'];
197                         $result = mysql_query($sql, $db);
198                         $content_array = array(); 
199         
200                         while ($row = mysql_fetch_assoc($result)){
201                                 $content_array[$row['content_parent_id']][$row['ordering']] = $row['content_id'];
202                         }
203
204                         $tree = buildTree($content_array[0], $content_array);
205
206                         /* Restructure the tree */
207                         $tree = rebuild($tree);
208
209                         
210                         /* Update the Db based on this new tree */
211                         reconstruct($tree, '', 0, $_POST['step1']['tb_prefix']);
212                 }
213         }
214
215         /* deal with the extra modules: */
216         /* for each module in the modules table check if that module still exists in the mod directory. */
217         /* if that module does not exist then check the old directory and prompt to have it copied */
218         /* or delete it from the modules table. or maybe disable it instead? */
219         if (version_compare($_POST['step1']['old_version'], '1.5.1', '>')) {
220                 define('TABLE_PREFIX', $_POST['step1']['tb_prefix']);
221                 require(AT_INCLUDE_PATH . 'classes/Module/Module.class.php');
222                 $moduleFactory = new ModuleFactory(FALSE);
223                 $module_list =& $moduleFactory->getModules(AT_MODULE_STATUS_DISABLED | AT_MODULE_STATUS_ENABLED);
224                 $keys = array_keys($module_list);
225                 foreach($keys as $dir_name) {
226                         $module =& $module_list[$dir_name];
227                         $module->setIsMissing($module->isExtra());
228                 }
229         }
230
231
232         if (!isset($errors)) {
233                 unset($errors);
234                 unset($_POST['submit']);
235                 unset($action);
236                 store_steps($step);
237                 $step++;
238                 return;
239         }
240 }
241
242 print_progress($step);
243
244 if (isset($errors)) {
245         print_errors($errors);
246 }
247
248
249 ?>
250
251 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
252         <input type="hidden" name="action" value="process" />
253         <input type="hidden" name="step" value="<?php echo $step; ?>" />
254         <?php print_hidden($step); ?>
255
256 <?php if (version_compare($_POST['step1']['old_version'], '1.5', '<')): ?>
257         <p>Below are new configuration options that are available for this version.</p>
258
259         <br />
260                 <table width="70%" class="tableborder" cellspacing="0" cellpadding="1" align="center">
261                 <tr>
262                         <th colspan="2">Super Administrator</th>
263                 </tr>
264                 <tr>
265                         <td class="row1" colspan="2">The Super Administrator account is used for managing ATutor. Since ATutor version 1.5 the Super Administrator can also create additional Administrators each with their own privileges and roles.</td>
266                 </tr>
267                 <tr>
268                         <td class="row1"><div class="required" title="Required Field">*</div><b><label for="username">Administrator Username:</label></b><br />
269                         May contain only letters, numbers, or underscores.</td>
270                         <td class="row1"><input type="text" name="admin_username" id="username" maxlength="20" size="20" value="<?php if (!empty($_POST['admin_username'])) { echo $stripslashes(htmlspecialchars($_POST['admin_username'])); } else { echo urldecode($_POST['step1']['admin_username']); } ?>" class="formfield" /></td>
271                 </tr>
272                 <tr>
273                         <td class="row1"><div class="required" title="Required Field">*</div><b><label for="password">Administrator Password:</label></b></td>
274                         <td class="row1"><input type="text" name="admin_password" id="password" maxlength="15" size="15" value="<?php if (!empty($_POST['admin_password'])) { echo $stripslashes(htmlspecialchars($_POST['admin_password'])); } else { echo urldecode($_POST['step1']['admin_password']); } ?>" class="formfield" /></td>
275                 </tr>
276                 <tr>
277                         <td class="row1"><div class="required" title="Required Field">*</div><b><label for="email">Administrator Email:</label></b></td>
278                         <td class="row1"><input type="text" name="admin_email" id="email" size="30" value="<?php if (!empty($_POST['admin_email'])) { echo $stripslashes(htmlspecialchars($_POST['admin_email'])); } else { echo urldecode($_POST['step1']['admin_email']); } ?>" class="formfield" /></td>
279                 </tr>
280                 </table>
281
282                 <br />
283
284                 <table width="70%" class="tableborder" cellspacing="0" cellpadding="1" align="center">
285                 <tr>
286                         <th colspan="2">System Preferences</th>
287                 </tr>
288                 <tr>
289                         <td class="row1"><div class="required" title="Required Field">*</div><b><label for="cemail">Contact Email:</label></b><br />
290                         The email that will be used as the return email when needed and when instructor account requests are made.</td>
291                         <td class="row1"><input type="text" name="email" id="cemail" size="30" value="<?php if (!empty($_POST['email'])) { echo $stripslashes(htmlspecialchars($_POST['admin_email'])); } else { echo urldecode($_POST['step1']['admin_email']); } ?>" class="formfield" /></td>
292                 </tr>
293                 </table>
294 <?php endif; ?>
295 <?php if (version_compare($_POST['step1']['old_version'], '1.5.3', '<')): ?>
296         <p>Groups made prior to 1.5.3 are not backwards compatible and will be removed.</p>
297 <?php else: ?>
298         <p>There are no new configuration options for this version.</p>
299 <?php endif; ?>
300
301         <br />
302         <br />
303         <div align="center"><input type="submit" class="button" value=" Next &raquo;" name="submit" /></div>
304 </form>