first translation
[moodle.git] / lib / adminlib.php
CommitLineData
88a7228a 1<?php //
2 //
3
4/**
5 * adminlib.php - Contains functions that only administrators will ever need to use
6 *
7 * @author Martin Dougiamas
8 * @version $Id$
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package moodlecore
11 */
12
13/**
14 * short description (optional)
15 *
16 * long description
17 *
18 * @uses $db
19 * @uses $CFG
20 * @param string $return The url to prompt the user to continue to
21 * @todo Finish documenting this function
22 */
173cc1c3 23function upgrade_enrol_plugins($return) {
e69ef14b 24 global $CFG, $db;
173cc1c3 25
88a7228a 26 if (!$mods = get_list_of_plugins('enrol') ) {
27 error('No modules installed!');
173cc1c3 28 }
29
30 foreach ($mods as $mod) {
31
88a7228a 32 $fullmod = $CFG->dirroot .'/enrol/'. $mod;
173cc1c3 33
34 unset($module);
35
88a7228a 36 if ( is_readable($fullmod .'/version.php')) {
37 include_once($fullmod .'/version.php'); // defines $module with version etc
173cc1c3 38 } else {
39 continue; // Nothing to do.
40 }
41
88a7228a 42 if ( is_readable($fullmod .'/db/'. $CFG->dbtype .'.php')) {
43 include_once($fullmod .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
173cc1c3 44 } else {
45 continue;
46 }
47
48 if (!isset($module)) {
49 continue;
50 }
51
52 if (!empty($module->requires)) {
53 if ($module->requires > $CFG->version) {
54 $info->modulename = $mod;
55 $info->moduleversion = $module->version;
56 $info->currentmoodle = $CFG->version;
57 $info->requiremoodle = $module->requires;
58 notify(get_string('modulerequirementsnotmet', 'error', $info));
59 unset($info);
60 continue;
61 }
62 }
63
64 $module->name = $mod; // The name MUST match the directory
65
66 $moduleversion = 'enrol_'.$mod.'_version';
67
68 if (!isset($CFG->$moduleversion)) {
69 set_config($moduleversion, 0);
70 }
71
72 if ($CFG->$moduleversion == $module->version) {
73 // do nothing
74 } else if ($CFG->$moduleversion < $module->version) {
75 if (empty($updated_modules)) {
88a7228a 76 $strmodulesetup = get_string('modulesetup');
77 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 78 }
88a7228a 79 print_heading($module->name .' module needs upgrading');
80 $upgrade_function = $module->name .'_upgrade';
173cc1c3 81 if (function_exists($upgrade_function)) {
82 $db->debug=true;
76201e66 83 if ($upgrade_function($CFG->$moduleversion)) {
173cc1c3 84 $db->debug=false;
85 // OK so far, now update the modules record
86 set_config($moduleversion, $module->version);
a8f68426 87 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 88 echo '<hr />';
173cc1c3 89 } else {
90 $db->debug=false;
88a7228a 91 notify('Upgrading '. $module->name .' from '. $CFG->$moduleversion .' to '. $module->version .' FAILED!');
173cc1c3 92 }
93 }
94 $updated_modules = true;
95 } else {
88a7228a 96 error('Version mismatch: '. $module->name .' can\'t downgrade '. $CFG->$moduleversion .' -> '. $module->version .' !');
173cc1c3 97 }
98 }
99
100 if (!empty($updated_modules)) {
101 print_continue($return);
102 die;
103 }
104}
105
88a7228a 106/**
107 * Find and check all modules and load them up or upgrade them if necessary
108 *
109 * @uses $db
110 * @uses $CFG
111 * @param string $return The url to prompt the user to continue to
112 * @todo Finish documenting this function
113 */
173cc1c3 114function upgrade_activity_modules($return) {
173cc1c3 115
e69ef14b 116 global $CFG, $db;
173cc1c3 117
88a7228a 118 if (!$mods = get_list_of_plugins('mod') ) {
119 error('No modules installed!');
173cc1c3 120 }
121
122 foreach ($mods as $mod) {
123
88a7228a 124 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
173cc1c3 125 continue;
126 }
127
88a7228a 128 $fullmod = $CFG->dirroot .'/mod/'. $mod;
173cc1c3 129
130 unset($module);
131
88a7228a 132 if ( is_readable($fullmod .'/version.php')) {
133 include_once($fullmod .'/version.php'); // defines $module with version etc
173cc1c3 134 } else {
88a7228a 135 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
173cc1c3 136 continue;
137 }
138
88a7228a 139 if ( is_readable($fullmod .'/db/'. $CFG->dbtype .'.php')) {
140 include_once($fullmod .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
173cc1c3 141 } else {
88a7228a 142 notify('Module '. $mod .': '. $fullmod .'/db/'. $CFG->dbtype .'.php was not readable');
173cc1c3 143 continue;
144 }
145
146 if (!isset($module)) {
147 continue;
148 }
149
150 if (!empty($module->requires)) {
151 if ($module->requires > $CFG->version) {
152 $info->modulename = $mod;
153 $info->moduleversion = $module->version;
154 $info->currentmoodle = $CFG->version;
155 $info->requiremoodle = $module->requires;
156 notify(get_string('modulerequirementsnotmet', 'error', $info));
157 unset($info);
158 continue;
159 }
160 }
161
162 $module->name = $mod; // The name MUST match the directory
163
88a7228a 164 if ($currmodule = get_record('modules', 'name', $module->name)) {
173cc1c3 165 if ($currmodule->version == $module->version) {
166 // do nothing
167 } else if ($currmodule->version < $module->version) {
168 if (empty($updated_modules)) {
88a7228a 169 $strmodulesetup = get_string('modulesetup');
170 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 171 }
88a7228a 172 print_heading($module->name .' module needs upgrading');
173 $upgrade_function = $module->name.'_upgrade';
173cc1c3 174 if (function_exists($upgrade_function)) {
175 $db->debug=true;
176 if ($upgrade_function($currmodule->version, $module)) {
177 $db->debug=false;
178 // OK so far, now update the modules record
179 $module->id = $currmodule->id;
88a7228a 180 if (! update_record('modules', $module)) {
181 error('Could not update '. $module->name .' record in modules table!');
173cc1c3 182 }
d6ead3a2 183 remove_dir($CFG->dataroot . '/cache', true); // flush cache
a8f68426 184 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 185 echo '<hr />';
173cc1c3 186 } else {
187 $db->debug=false;
88a7228a 188 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
173cc1c3 189 }
190 }
191 $updated_modules = true;
192 } else {
88a7228a 193 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
173cc1c3 194 }
195
196 } else { // module not installed yet, so install it
197 if (empty($updated_modules)) {
88a7228a 198 $strmodulesetup = get_string('modulesetup');
199 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 200 }
201 print_heading($module->name);
202 $updated_modules = true;
203 $db->debug = true;
204 @set_time_limit(0); // To allow slow databases to complete the long SQL
88a7228a 205 if (modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql')) {
173cc1c3 206 $db->debug = false;
88a7228a 207 if ($module->id = insert_record('modules', $module)) {
a8f68426 208 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 209 echo '<hr />';
173cc1c3 210 } else {
88a7228a 211 error($module->name .' module could not be added to the module list!');
173cc1c3 212 }
213 } else {
88a7228a 214 error($module->name .' tables could NOT be set up successfully!');
173cc1c3 215 }
216 }
a5c0990e 217
218 if ( is_readable($fullmod .'/defaults.php')) {
219 // Insert default values for any important configuration variables
9e6e7502 220 unset($defaults);
a5c0990e 221 include_once($fullmod .'/defaults.php');
222 foreach ($defaults as $name => $value) {
223 if (!isset($CFG->$name)) {
a5c0990e 224 set_config($name, $value);
225 }
226 }
227 }
173cc1c3 228 }
229
230 if (!empty($updated_modules)) {
231 print_continue($return);
232 die;
233 }
234}
235
9e6e7502 236?>