Merged XHTML fix from stable
[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/**
ead29342 14 * Upgrade plugins
88a7228a 15 *
16 * @uses $db
17 * @uses $CFG
ead29342 18 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
19 * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
20 * @param string $return The url to prompt the user to continue to
88a7228a 21 */
ead29342 22function upgrade_plugins($type, $dir, $return) {
e69ef14b 23 global $CFG, $db;
173cc1c3 24
ead29342 25 if (!$plugs = get_list_of_plugins($dir) ) {
26 error('No '.$type.' plugins installed!');
173cc1c3 27 }
28
ead29342 29 foreach ($plugs as $plug) {
173cc1c3 30
ead29342 31 $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
173cc1c3 32
ead29342 33 unset($plugin);
173cc1c3 34
ead29342 35 if ( is_readable($fullplug .'/version.php')) {
36 include_once($fullplug .'/version.php'); // defines $plugin with version etc
173cc1c3 37 } else {
38 continue; // Nothing to do.
39 }
40
ead29342 41 if ( is_readable($fullplug .'/db/'. $CFG->dbtype .'.php')) {
42 include_once($fullplug .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
173cc1c3 43 } else {
44 continue;
45 }
46
ead29342 47 if (!isset($plugin)) {
173cc1c3 48 continue;
49 }
50
ead29342 51 if (!empty($plugin->requires)) {
52 if ($plugin->requires > $CFG->version) {
53 $info->pluginname = $plug;
54 $info->pluginversion = $plugin->version;
173cc1c3 55 $info->currentmoodle = $CFG->version;
ead29342 56 $info->requiremoodle = $plugin->requires;
57 notify(get_string('pluginrequirementsnotmet', 'error', $info));
173cc1c3 58 unset($info);
59 continue;
60 }
61 }
62
ead29342 63 $plugin->name = $plug; // The name MUST match the directory
173cc1c3 64
ead29342 65 $pluginversion = $type.'_'.$plug.'_version';
173cc1c3 66
ead29342 67 if (!isset($CFG->$pluginversion)) {
68 set_config($pluginversion, 0);
173cc1c3 69 }
70
ead29342 71 if ($CFG->$pluginversion == $plugin->version) {
173cc1c3 72 // do nothing
ead29342 73 } else if ($CFG->$pluginversion < $plugin->version) {
74 if (empty($updated_plugins)) {
75 $strpluginsetup = get_string('pluginsetup');
76 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 77 }
ead29342 78 print_heading($plugin->name .' plugin needs upgrading');
d87a9d73 79
80 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
81 if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
82 $db->debug = true;
83 @set_time_limit(0); // To allow slow databases to complete the long SQL
84 if (modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
85 // OK so far, now update the plugins record
86 set_config($pluginversion, $plugin->version);
87 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
88 } else {
89 notify('Installing '. $plugin->name .' FAILED!');
90 }
91 $db->debug = false;
92 } else { // We'll assume no tables are necessary
ead29342 93 set_config($pluginversion, $plugin->version);
94 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
d87a9d73 95 }
96 } else { // Upgrade existing install
97 $upgrade_function = $type.'_'.$plugin->name .'_upgrade';
98 if (function_exists($upgrade_function)) {
99 $db->debug=true;
100 if ($upgrade_function($CFG->$pluginversion)) {
101 $db->debug=false;
102 // OK so far, now update the plugins record
103 set_config($pluginversion, $plugin->version);
104 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
105 } else {
106 $db->debug=false;
107 notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
108 }
173cc1c3 109 }
110 }
d87a9d73 111 echo '<hr />';
ead29342 112 $updated_plugins = true;
d87a9d73 113
173cc1c3 114 } else {
ead29342 115 error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
173cc1c3 116 }
117 }
118
ead29342 119 if (!empty($updated_plugins)) {
173cc1c3 120 print_continue($return);
121 die;
122 }
123}
124
88a7228a 125/**
126 * Find and check all modules and load them up or upgrade them if necessary
127 *
128 * @uses $db
129 * @uses $CFG
130 * @param string $return The url to prompt the user to continue to
131 * @todo Finish documenting this function
132 */
173cc1c3 133function upgrade_activity_modules($return) {
173cc1c3 134
e69ef14b 135 global $CFG, $db;
173cc1c3 136
88a7228a 137 if (!$mods = get_list_of_plugins('mod') ) {
138 error('No modules installed!');
173cc1c3 139 }
140
141 foreach ($mods as $mod) {
142
88a7228a 143 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
173cc1c3 144 continue;
145 }
146
88a7228a 147 $fullmod = $CFG->dirroot .'/mod/'. $mod;
173cc1c3 148
149 unset($module);
150
88a7228a 151 if ( is_readable($fullmod .'/version.php')) {
152 include_once($fullmod .'/version.php'); // defines $module with version etc
173cc1c3 153 } else {
88a7228a 154 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
173cc1c3 155 continue;
156 }
157
88a7228a 158 if ( is_readable($fullmod .'/db/'. $CFG->dbtype .'.php')) {
159 include_once($fullmod .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
173cc1c3 160 } else {
88a7228a 161 notify('Module '. $mod .': '. $fullmod .'/db/'. $CFG->dbtype .'.php was not readable');
173cc1c3 162 continue;
163 }
164
165 if (!isset($module)) {
166 continue;
167 }
168
169 if (!empty($module->requires)) {
170 if ($module->requires > $CFG->version) {
171 $info->modulename = $mod;
172 $info->moduleversion = $module->version;
173 $info->currentmoodle = $CFG->version;
174 $info->requiremoodle = $module->requires;
175 notify(get_string('modulerequirementsnotmet', 'error', $info));
176 unset($info);
177 continue;
178 }
179 }
180
181 $module->name = $mod; // The name MUST match the directory
182
88a7228a 183 if ($currmodule = get_record('modules', 'name', $module->name)) {
173cc1c3 184 if ($currmodule->version == $module->version) {
185 // do nothing
186 } else if ($currmodule->version < $module->version) {
187 if (empty($updated_modules)) {
88a7228a 188 $strmodulesetup = get_string('modulesetup');
189 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 190 }
88a7228a 191 print_heading($module->name .' module needs upgrading');
192 $upgrade_function = $module->name.'_upgrade';
173cc1c3 193 if (function_exists($upgrade_function)) {
194 $db->debug=true;
195 if ($upgrade_function($currmodule->version, $module)) {
196 $db->debug=false;
197 // OK so far, now update the modules record
198 $module->id = $currmodule->id;
88a7228a 199 if (! update_record('modules', $module)) {
200 error('Could not update '. $module->name .' record in modules table!');
173cc1c3 201 }
d6ead3a2 202 remove_dir($CFG->dataroot . '/cache', true); // flush cache
a8f68426 203 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 204 echo '<hr />';
173cc1c3 205 } else {
206 $db->debug=false;
88a7228a 207 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
173cc1c3 208 }
209 }
210 $updated_modules = true;
211 } else {
88a7228a 212 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
173cc1c3 213 }
214
215 } else { // module not installed yet, so install it
216 if (empty($updated_modules)) {
88a7228a 217 $strmodulesetup = get_string('modulesetup');
218 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '', '', false, '&nbsp;', '&nbsp;');
173cc1c3 219 }
220 print_heading($module->name);
221 $updated_modules = true;
222 $db->debug = true;
223 @set_time_limit(0); // To allow slow databases to complete the long SQL
88a7228a 224 if (modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql')) {
173cc1c3 225 $db->debug = false;
88a7228a 226 if ($module->id = insert_record('modules', $module)) {
a8f68426 227 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 228 echo '<hr />';
173cc1c3 229 } else {
88a7228a 230 error($module->name .' module could not be added to the module list!');
173cc1c3 231 }
232 } else {
88a7228a 233 error($module->name .' tables could NOT be set up successfully!');
173cc1c3 234 }
235 }
e5bd4e58 236
237 /// Check submodules of this module if necessary
238
239 include_once($fullmod.'/lib.php'); // defines upgrading function
240
241 $submoduleupgrade = $module->name.'_upgrade_submodules';
242 if (function_exists($submoduleupgrade)) {
243 $submoduleupgrade();
244 }
245
246
247 /// Run any defaults or final code that is necessary for this module
248
a5c0990e 249 if ( is_readable($fullmod .'/defaults.php')) {
250 // Insert default values for any important configuration variables
9e6e7502 251 unset($defaults);
a5c0990e 252 include_once($fullmod .'/defaults.php');
f9a2e515 253 if (!empty($defaults)) {
254 foreach ($defaults as $name => $value) {
255 if (!isset($CFG->$name)) {
256 set_config($name, $value);
257 }
a5c0990e 258 }
259 }
260 }
173cc1c3 261 }
262
263 if (!empty($updated_modules)) {
264 print_continue($return);
136f43dc 265 print_footer();
173cc1c3 266 die;
267 }
268}
269
f3221af9 270/**
271 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
80be7ee3 272 *
273 * @param string $name ?
274 * @param bool $value ?
275 * @param int $staleafter ?
276 * @param bool $clobberstale ?
277 * @todo Finish documenting this function
f3221af9 278 */
279function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
280
281 if (empty($name)) {
282 mtrace("Tried to get a cron lock for a null fieldname");
283 return false;
284 }
285
286 if (empty($value)) {
287 set_config($name,0);
288 return true;
289 }
290
291 if ($config = get_record('config','name',$name)) {
292 if (empty($config->value)) {
293 set_config($name,time());
294 } else {
295 // check for stale.
296 if ((time() - $staleafter) > $config->value) {
297 mtrace("STALE LOCKFILE FOR $name - was $config->value");
298 if (!empty($clobberstale)) {
299 set_config($name,time());
300 return true;
301 }
302 } else {
303 return false; // was not stale - ie, we're ok to still be running.
304 }
305 }
306 }
307 else {
308 set_config($name,time());
309 }
310 return true;
311}
a597f8a8 312
313function print_progress($done, $total, $updatetime=5, $sleeptime=1) {
314 static $count;
315 static $starttime;
316 static $lasttime;
317
318 if (empty($starttime)) {
319 $starttime = $lasttime = time();
320 $lasttime = $starttime - $updatetime;
321 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
322 echo '<div id="bar" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
323 echo '<div id="slider" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
324 echo '</div>';
325 echo '<div id="text" align="center" style="width:500px;"></div>';
326 echo '</td></tr></table>';
327 echo '</div>';
328 }
329
330 if (!isset($count)) {
331 $count = 0;
332 }
333
334 $count++;
335
336 $now = time();
337
338 if ($done && (($now - $lasttime) >= $updatetime)) {
339 $elapsedtime = $now - $starttime;
340 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
341 $percentage = format_float((float)$done / (float)$total, 2);
342 $width = (int)(500 * $percentage);
343
344 echo '<script>';
345 echo 'document.getElementById("text").innerHTML = "'.$count.' done. Ending: '.format_time($projectedtime).'";'."\n";
346 echo 'document.getElementById("slider").style.width = \''.$width.'px\';'."\n";
347 echo '</script>';
348
349 $lasttime = $now;
350 sleep($sleeptime);
351 }
352}
9e6e7502 353?>