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