a4e10845 |
1 | <?php |
88a7228a |
2 | |
3 | /** |
4 | * adminlib.php - Contains functions that only administrators will ever need to use |
5 | * |
a4e10845 |
6 | * @author Martin Dougiamas and many others |
88a7228a |
7 | * @version $Id$ |
8 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
9 | * @package moodlecore |
10 | */ |
11 | |
8ddcdd86 |
12 | function upgrade_main_savepoint($result, $version) { |
13 | global $CFG; |
14 | |
15 | if ($result) { |
16 | if ($CFG->version >= $version) { |
17 | // something really wrong is going on in main upgrade script |
220a90c5 |
18 | error("Upgrade savepoint: Can not upgrade main version from $CFG->version to $version."); |
8ddcdd86 |
19 | } |
20 | set_config('version', $version); |
21 | } else { |
22 | notify ("Upgrade savepoint: Error during main upgrade to version $version"); |
23 | } |
24 | } |
25 | |
26 | function upgrade_mod_savepoint($result, $version, $type) { |
27 | //TODO |
28 | } |
29 | |
30 | function upgrade_plugin_savepoint($result, $version, $type, $dir) { |
31 | //TODO |
32 | } |
33 | |
34 | function upgrade_backup_savepoint($result, $version) { |
35 | //TODO |
36 | } |
37 | |
38 | function upgrade_blocks_savepoint($result, $version, $type) { |
39 | //TODO |
40 | } |
41 | |
88a7228a |
42 | /** |
ead29342 |
43 | * Upgrade plugins |
88a7228a |
44 | * |
45 | * @uses $db |
46 | * @uses $CFG |
ead29342 |
47 | * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype') |
48 | * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes') |
49 | * @param string $return The url to prompt the user to continue to |
eef868d1 |
50 | */ |
ead29342 |
51 | function upgrade_plugins($type, $dir, $return) { |
e69ef14b |
52 | global $CFG, $db; |
173cc1c3 |
53 | |
15999a93 |
54 | /// Let's know if the header has been printed, so the funcion is being called embedded in an outer page |
55 | $embedded = defined('HEADER_PRINTED'); |
56 | |
bb7053f3 |
57 | $plugs = get_list_of_plugins($dir); |
583fad99 |
58 | $updated_plugins = false; |
59 | $strpluginsetup = get_string('pluginsetup'); |
60 | |
ead29342 |
61 | foreach ($plugs as $plug) { |
173cc1c3 |
62 | |
ead29342 |
63 | $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug; |
173cc1c3 |
64 | |
ead29342 |
65 | unset($plugin); |
173cc1c3 |
66 | |
bbbf2d40 |
67 | if (is_readable($fullplug .'/version.php')) { |
ead29342 |
68 | include_once($fullplug .'/version.php'); // defines $plugin with version etc |
173cc1c3 |
69 | } else { |
70 | continue; // Nothing to do. |
71 | } |
72 | |
e79a09a2 |
73 | $oldupgrade = false; |
74 | $newupgrade = false; |
7c006e34 |
75 | if (is_readable($fullplug . '/db/'. $CFG->dbtype . '.php')) { |
76 | include_once($fullplug . '/db/'. $CFG->dbtype . '.php'); // defines old upgrading function |
e79a09a2 |
77 | $oldupgrade = true; |
78 | } |
db8bd7a6 |
79 | if (is_readable($fullplug . '/db/upgrade.php')) { |
7c006e34 |
80 | include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function |
e79a09a2 |
81 | $newupgrade = true; |
82 | } |
83 | |
ead29342 |
84 | if (!isset($plugin)) { |
173cc1c3 |
85 | continue; |
86 | } |
87 | |
ead29342 |
88 | if (!empty($plugin->requires)) { |
89 | if ($plugin->requires > $CFG->version) { |
acdd790f |
90 | $info = new object(); |
ead29342 |
91 | $info->pluginname = $plug; |
92 | $info->pluginversion = $plugin->version; |
173cc1c3 |
93 | $info->currentmoodle = $CFG->version; |
ead29342 |
94 | $info->requiremoodle = $plugin->requires; |
15999a93 |
95 | if (!$updated_plugins && !$embedded) { |
0be6f678 |
96 | print_header($strpluginsetup, $strpluginsetup, |
97 | build_navigation(array(array('name' => $strpluginsetup, 'link' => null, 'type' => 'misc'))), '', |
371a32e3 |
98 | upgrade_get_javascript(), false, ' ', ' '); |
583fad99 |
99 | } |
100 | upgrade_log_start(); |
ead29342 |
101 | notify(get_string('pluginrequirementsnotmet', 'error', $info)); |
583fad99 |
102 | $updated_plugins = true; |
173cc1c3 |
103 | continue; |
104 | } |
105 | } |
106 | |
ead29342 |
107 | $plugin->name = $plug; // The name MUST match the directory |
173cc1c3 |
108 | |
ead29342 |
109 | $pluginversion = $type.'_'.$plug.'_version'; |
173cc1c3 |
110 | |
ead29342 |
111 | if (!isset($CFG->$pluginversion)) { |
112 | set_config($pluginversion, 0); |
173cc1c3 |
113 | } |
eef868d1 |
114 | |
ead29342 |
115 | if ($CFG->$pluginversion == $plugin->version) { |
173cc1c3 |
116 | // do nothing |
ead29342 |
117 | } else if ($CFG->$pluginversion < $plugin->version) { |
15999a93 |
118 | if (!$updated_plugins && !$embedded) { |
0be6f678 |
119 | print_header($strpluginsetup, $strpluginsetup, |
120 | build_navigation(array(array('name' => $strpluginsetup, 'link' => null, 'type' => 'misc'))), '', |
371a32e3 |
121 | upgrade_get_javascript(), false, ' ', ' '); |
173cc1c3 |
122 | } |
e79a09a2 |
123 | $updated_plugins = true; |
583fad99 |
124 | upgrade_log_start(); |
05b42119 |
125 | print_heading($dir.'/'. $plugin->name .' plugin needs upgrading'); |
e79a09a2 |
126 | $db->debug = true; |
127 | @set_time_limit(0); // To allow slow databases to complete the long SQL |
128 | |
d87a9d73 |
129 | if ($CFG->$pluginversion == 0) { // It's a new install of this plugin |
e79a09a2 |
130 | /// Both old .sql files and new install.xml are supported |
131 | /// but we priorize install.xml (XMLDB) if present |
132 | $status = false; |
db8bd7a6 |
133 | if (file_exists($fullplug . '/db/install.xml')) { |
450cf307 |
134 | $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method |
e79a09a2 |
135 | } else if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) { |
136 | $status = modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql'); //Old method |
eef868d1 |
137 | } else { |
7c006e34 |
138 | $status = true; |
d87a9d73 |
139 | } |
e79a09a2 |
140 | |
141 | $db->debug = false; |
eef868d1 |
142 | /// Continue with the instalation, roles and other stuff |
e79a09a2 |
143 | if ($status) { |
3f59a54e |
144 | /// OK so far, now update the plugins record |
e79a09a2 |
145 | set_config($pluginversion, $plugin->version); |
3f59a54e |
146 | |
147 | /// Install capabilities |
ae628043 |
148 | if (!update_capabilities($type.'/'.$plug)) { |
3f59a54e |
149 | error('Could not set up the capabilities for '.$plugin->name.'!'); |
e79a09a2 |
150 | } |
3f59a54e |
151 | /// Install events |
0856223c |
152 | events_update_definition($type.'/'.$plug); |
3f59a54e |
153 | |
154 | /// Run local install function if there is one |
155 | if (is_readable($fullplug .'/lib.php')) { |
9ba38673 |
156 | include_once($fullplug .'/lib.php'); |
3f59a54e |
157 | $installfunction = $plugin->name.'_install'; |
158 | if (function_exists($installfunction)) { |
159 | if (! $installfunction() ) { |
160 | notify('Encountered a problem running install function for '.$module->name.'!'); |
161 | } |
162 | } |
163 | } |
9ba38673 |
164 | |
e79a09a2 |
165 | notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess'); |
166 | } else { |
167 | notify('Installing '. $plugin->name .' FAILED!'); |
168 | } |
d87a9d73 |
169 | } else { // Upgrade existing install |
e79a09a2 |
170 | /// Run de old and new upgrade functions for the module |
171 | $oldupgrade_function = $type.'_'.$plugin->name .'_upgrade'; |
172 | $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name .'_upgrade'; |
173 | |
174 | /// First, the old function if exists |
175 | $oldupgrade_status = true; |
176 | if ($oldupgrade && function_exists($oldupgrade_function)) { |
177 | $db->debug = true; |
178 | $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion); |
179 | } else if ($oldupgrade) { |
180 | notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' . |
181 | $fullplug . '/db/' . $CFG->dbtype . '.php'); |
182 | } |
183 | |
184 | /// Then, the new function if exists and the old one was ok |
185 | $newupgrade_status = true; |
186 | if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) { |
187 | $db->debug = true; |
188 | $newupgrade_status = $newupgrade_function($CFG->$pluginversion); |
189 | } else if ($newupgrade) { |
190 | notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' . |
191 | $fullplug . '/db/upgrade.php'); |
192 | } |
193 | |
194 | $db->debug=false; |
195 | /// Now analyze upgrade results |
196 | if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed |
197 | // OK so far, now update the plugins record |
198 | set_config($pluginversion, $plugin->version); |
ae628043 |
199 | if (!update_capabilities($type.'/'.$plug)) { |
e79a09a2 |
200 | error('Could not update '.$plugin->name.' capabilities!'); |
d87a9d73 |
201 | } |
0856223c |
202 | events_update_definition($type.'/'.$plug); |
e79a09a2 |
203 | notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess'); |
204 | } else { |
205 | notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!'); |
173cc1c3 |
206 | } |
207 | } |
d87a9d73 |
208 | echo '<hr />'; |
173cc1c3 |
209 | } else { |
583fad99 |
210 | upgrade_log_start(); |
ead29342 |
211 | error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !'); |
173cc1c3 |
212 | } |
213 | } |
214 | |
583fad99 |
215 | upgrade_log_finish(); |
216 | |
15999a93 |
217 | if ($updated_plugins && !$embedded) { |
173cc1c3 |
218 | print_continue($return); |
acdd790f |
219 | print_footer('none'); |
173cc1c3 |
220 | die; |
221 | } |
222 | } |
223 | |
88a7228a |
224 | /** |
225 | * Find and check all modules and load them up or upgrade them if necessary |
226 | * |
227 | * @uses $db |
228 | * @uses $CFG |
229 | * @param string $return The url to prompt the user to continue to |
230 | * @todo Finish documenting this function |
eef868d1 |
231 | */ |
173cc1c3 |
232 | function upgrade_activity_modules($return) { |
173cc1c3 |
233 | |
e69ef14b |
234 | global $CFG, $db; |
173cc1c3 |
235 | |
88a7228a |
236 | if (!$mods = get_list_of_plugins('mod') ) { |
237 | error('No modules installed!'); |
173cc1c3 |
238 | } |
239 | |
583fad99 |
240 | $updated_modules = false; |
241 | $strmodulesetup = get_string('modulesetup'); |
242 | |
173cc1c3 |
243 | foreach ($mods as $mod) { |
244 | |
88a7228a |
245 | if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it |
173cc1c3 |
246 | continue; |
247 | } |
248 | |
88a7228a |
249 | $fullmod = $CFG->dirroot .'/mod/'. $mod; |
173cc1c3 |
250 | |
251 | unset($module); |
252 | |
88a7228a |
253 | if ( is_readable($fullmod .'/version.php')) { |
254 | include_once($fullmod .'/version.php'); // defines $module with version etc |
173cc1c3 |
255 | } else { |
88a7228a |
256 | notify('Module '. $mod .': '. $fullmod .'/version.php was not readable'); |
173cc1c3 |
257 | continue; |
258 | } |
259 | |
d6eb06b6 |
260 | $oldupgrade = false; |
261 | $newupgrade = false; |
7c006e34 |
262 | if ( is_readable($fullmod .'/db/' . $CFG->dbtype . '.php')) { |
263 | include_once($fullmod .'/db/' . $CFG->dbtype . '.php'); // defines old upgrading function |
d6eb06b6 |
264 | $oldupgrade = true; |
265 | } |
db8bd7a6 |
266 | if ( is_readable($fullmod . '/db/upgrade.php')) { |
7c006e34 |
267 | include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function |
d6eb06b6 |
268 | $newupgrade = true; |
173cc1c3 |
269 | } |
270 | |
271 | if (!isset($module)) { |
272 | continue; |
273 | } |
274 | |
275 | if (!empty($module->requires)) { |
276 | if ($module->requires > $CFG->version) { |
acdd790f |
277 | $info = new object(); |
173cc1c3 |
278 | $info->modulename = $mod; |
279 | $info->moduleversion = $module->version; |
280 | $info->currentmoodle = $CFG->version; |
281 | $info->requiremoodle = $module->requires; |
583fad99 |
282 | if (!$updated_modules) { |
0be6f678 |
283 | print_header($strmodulesetup, $strmodulesetup, |
284 | build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '', |
371a32e3 |
285 | upgrade_get_javascript(), false, ' ', ' '); |
583fad99 |
286 | } |
287 | upgrade_log_start(); |
173cc1c3 |
288 | notify(get_string('modulerequirementsnotmet', 'error', $info)); |
583fad99 |
289 | $updated_modules = true; |
173cc1c3 |
290 | continue; |
291 | } |
292 | } |
293 | |
294 | $module->name = $mod; // The name MUST match the directory |
eef868d1 |
295 | |
11e4a24b |
296 | include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions |
297 | |
88a7228a |
298 | if ($currmodule = get_record('modules', 'name', $module->name)) { |
173cc1c3 |
299 | if ($currmodule->version == $module->version) { |
300 | // do nothing |
301 | } else if ($currmodule->version < $module->version) { |
d6eb06b6 |
302 | /// If versions say that we need to upgrade but no upgrade files are available, notify and continue |
303 | if (!$oldupgrade && !$newupgrade) { |
304 | notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php or ' . |
305 | $fullmod . '/db/upgrade.php were not readable'); |
306 | continue; |
307 | } |
583fad99 |
308 | if (!$updated_modules) { |
0be6f678 |
309 | print_header($strmodulesetup, $strmodulesetup, |
310 | build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '', |
371a32e3 |
311 | upgrade_get_javascript(), false, ' ', ' '); |
173cc1c3 |
312 | } |
583fad99 |
313 | upgrade_log_start(); |
88a7228a |
314 | print_heading($module->name .' module needs upgrading'); |
d6eb06b6 |
315 | |
316 | /// Run de old and new upgrade functions for the module |
317 | $oldupgrade_function = $module->name . '_upgrade'; |
318 | $newupgrade_function = 'xmldb_' . $module->name . '_upgrade'; |
319 | |
320 | /// First, the old function if exists |
321 | $oldupgrade_status = true; |
322 | if ($oldupgrade && function_exists($oldupgrade_function)) { |
323 | $db->debug = true; |
324 | $oldupgrade_status = $oldupgrade_function($currmodule->version, $module); |
8bc9ebc4 |
325 | if (!$oldupgrade_status) { |
326 | notify ('Upgrade function ' . $oldupgrade_function . |
220a90c5 |
327 | ' did not complete successfully.'); |
8bc9ebc4 |
328 | } |
ba05965e |
329 | } else if ($oldupgrade) { |
d6eb06b6 |
330 | notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' . |
331 | $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php'); |
d6eb06b6 |
332 | } |
333 | |
334 | /// Then, the new function if exists and the old one was ok |
335 | $newupgrade_status = true; |
ba05965e |
336 | if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) { |
d6eb06b6 |
337 | $db->debug = true; |
338 | $newupgrade_status = $newupgrade_function($currmodule->version, $module); |
8bc9ebc4 |
339 | } else if ($newupgrade && $oldupgrade_status) { |
d6eb06b6 |
340 | notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' . |
341 | $mod . ': ' . $fullmod . '/db/upgrade.php'); |
d6eb06b6 |
342 | } |
343 | |
e79a09a2 |
344 | $db->debug=false; |
d6eb06b6 |
345 | /// Now analyze upgrade results |
668896e5 |
346 | if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed |
d6eb06b6 |
347 | // OK so far, now update the modules record |
348 | $module->id = $currmodule->id; |
349 | if (! update_record('modules', $module)) { |
350 | error('Could not update '. $module->name .' record in modules table!'); |
173cc1c3 |
351 | } |
d6eb06b6 |
352 | remove_dir($CFG->dataroot . '/cache', true); // flush cache |
353 | notify(get_string('modulesuccess', '', $module->name), 'notifysuccess'); |
354 | echo '<hr />'; |
355 | } else { |
d6eb06b6 |
356 | notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!'); |
173cc1c3 |
357 | } |
bbbf2d40 |
358 | |
d6eb06b6 |
359 | /// Update the capabilities table? |
bbbf2d40 |
360 | if (!update_capabilities('mod/'.$module->name)) { |
361 | error('Could not update '.$module->name.' capabilities!'); |
362 | } |
0856223c |
363 | events_update_definition('mod/'.$module->name); |
bbbf2d40 |
364 | |
173cc1c3 |
365 | $updated_modules = true; |
eef868d1 |
366 | |
173cc1c3 |
367 | } else { |
583fad99 |
368 | upgrade_log_start(); |
88a7228a |
369 | error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !'); |
173cc1c3 |
370 | } |
eef868d1 |
371 | |
173cc1c3 |
372 | } else { // module not installed yet, so install it |
583fad99 |
373 | if (!$updated_modules) { |
0be6f678 |
374 | print_header($strmodulesetup, $strmodulesetup, |
375 | build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '', |
371a32e3 |
376 | upgrade_get_javascript(), false, ' ', ' '); |
173cc1c3 |
377 | } |
583fad99 |
378 | upgrade_log_start(); |
173cc1c3 |
379 | print_heading($module->name); |
380 | $updated_modules = true; |
381 | $db->debug = true; |
382 | @set_time_limit(0); // To allow slow databases to complete the long SQL |
d6eb06b6 |
383 | |
384 | /// Both old .sql files and new install.xml are supported |
385 | /// but we priorize install.xml (XMLDB) if present |
db8bd7a6 |
386 | if (file_exists($fullmod . '/db/install.xml')) { |
d6eb06b6 |
387 | $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method |
388 | } else { |
389 | $status = modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql'); //Old method |
390 | } |
391 | |
e79a09a2 |
392 | $db->debug = false; |
11e4a24b |
393 | |
394 | /// Continue with the installation, roles and other stuff |
d6eb06b6 |
395 | if ($status) { |
88a7228a |
396 | if ($module->id = insert_record('modules', $module)) { |
11e4a24b |
397 | |
9ba38673 |
398 | /// Capabilities |
bbbf2d40 |
399 | if (!update_capabilities('mod/'.$module->name)) { |
400 | error('Could not set up the capabilities for '.$module->name.'!'); |
401 | } |
9ba38673 |
402 | |
11e4a24b |
403 | /// Events |
0856223c |
404 | events_update_definition('mod/'.$module->name); |
11e4a24b |
405 | |
406 | /// Run local install function if there is one |
407 | $installfunction = $module->name.'_install'; |
408 | if (function_exists($installfunction)) { |
409 | if (! $installfunction() ) { |
410 | notify('Encountered a problem running install function for '.$module->name.'!'); |
411 | } |
412 | } |
413 | |
a8f68426 |
414 | notify(get_string('modulesuccess', '', $module->name), 'notifysuccess'); |
88a7228a |
415 | echo '<hr />'; |
173cc1c3 |
416 | } else { |
88a7228a |
417 | error($module->name .' module could not be added to the module list!'); |
173cc1c3 |
418 | } |
eef868d1 |
419 | } else { |
88a7228a |
420 | error($module->name .' tables could NOT be set up successfully!'); |
173cc1c3 |
421 | } |
422 | } |
e5bd4e58 |
423 | |
424 | /// Check submodules of this module if necessary |
425 | |
e5bd4e58 |
426 | $submoduleupgrade = $module->name.'_upgrade_submodules'; |
427 | if (function_exists($submoduleupgrade)) { |
428 | $submoduleupgrade(); |
429 | } |
430 | |
431 | |
432 | /// Run any defaults or final code that is necessary for this module |
433 | |
a5c0990e |
434 | if ( is_readable($fullmod .'/defaults.php')) { |
435 | // Insert default values for any important configuration variables |
9e6e7502 |
436 | unset($defaults); |
220a90c5 |
437 | include($fullmod .'/defaults.php'); // include here means execute, not library include |
f9a2e515 |
438 | if (!empty($defaults)) { |
439 | foreach ($defaults as $name => $value) { |
440 | if (!isset($CFG->$name)) { |
441 | set_config($name, $value); |
442 | } |
a5c0990e |
443 | } |
444 | } |
445 | } |
173cc1c3 |
446 | } |
447 | |
583fad99 |
448 | upgrade_log_finish(); // finish logging if started |
449 | |
450 | if ($updated_modules) { |
173cc1c3 |
451 | print_continue($return); |
acdd790f |
452 | print_footer('none'); |
173cc1c3 |
453 | die; |
454 | } |
455 | } |
456 | |
eef868d1 |
457 | /** |
f3221af9 |
458 | * This function will return FALSE if the lock fails to be set (ie, if it's already locked) |
80be7ee3 |
459 | * |
460 | * @param string $name ? |
461 | * @param bool $value ? |
462 | * @param int $staleafter ? |
463 | * @param bool $clobberstale ? |
464 | * @todo Finish documenting this function |
f3221af9 |
465 | */ |
466 | function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) { |
467 | |
468 | if (empty($name)) { |
469 | mtrace("Tried to get a cron lock for a null fieldname"); |
470 | return false; |
471 | } |
472 | |
473 | if (empty($value)) { |
474 | set_config($name,0); |
475 | return true; |
476 | } |
477 | |
478 | if ($config = get_record('config','name',$name)) { |
479 | if (empty($config->value)) { |
480 | set_config($name,time()); |
481 | } else { |
482 | // check for stale. |
483 | if ((time() - $staleafter) > $config->value) { |
484 | mtrace("STALE LOCKFILE FOR $name - was $config->value"); |
485 | if (!empty($clobberstale)) { |
486 | set_config($name,time()); |
487 | return true; |
488 | } |
489 | } else { |
490 | return false; // was not stale - ie, we're ok to still be running. |
491 | } |
492 | } |
493 | } |
494 | else { |
495 | set_config($name,time()); |
496 | } |
497 | return true; |
498 | } |
a597f8a8 |
499 | |
fb06b255 |
500 | function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') { |
a597f8a8 |
501 | static $starttime; |
502 | static $lasttime; |
503 | |
26ea4888 |
504 | if ($total < 2) { // No need to show anything |
505 | return; |
506 | } |
507 | |
a597f8a8 |
508 | if (empty($starttime)) { |
509 | $starttime = $lasttime = time(); |
510 | $lasttime = $starttime - $updatetime; |
511 | echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">'; |
72da5046 |
512 | echo '<div id="bar'.$total.'" style="border-style:solid;border-width:1px;width:500px;height:50px;">'; |
513 | echo '<div id="slider'.$total.'" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>'; |
a597f8a8 |
514 | echo '</div>'; |
72da5046 |
515 | echo '<div id="text'.$total.'" align="center" style="width:500px;"></div>'; |
a597f8a8 |
516 | echo '</td></tr></table>'; |
517 | echo '</div>'; |
518 | } |
519 | |
a597f8a8 |
520 | $now = time(); |
521 | |
522 | if ($done && (($now - $lasttime) >= $updatetime)) { |
523 | $elapsedtime = $now - $starttime; |
524 | $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime; |
76317c73 |
525 | $percentage = round((float)$done / (float)$total, 2); |
a597f8a8 |
526 | $width = (int)(500 * $percentage); |
527 | |
fb06b255 |
528 | if ($projectedtime > 10) { |
529 | $projectedtext = ' Ending: '.format_time($projectedtime); |
530 | } else { |
531 | $projectedtext = ''; |
532 | } |
533 | |
a597f8a8 |
534 | echo '<script>'; |
aae37b63 |
535 | echo 'document.getElementById("text'.$total.'").innerHTML = "'.addslashes($donetext).' ('.$done.'/'.$total.') '.$projectedtext.'";'."\n"; |
536 | echo 'document.getElementById("slider'.$total.'").style.width = \''.$width.'px\';'."\n"; |
a597f8a8 |
537 | echo '</script>'; |
538 | |
539 | $lasttime = $now; |
540 | sleep($sleeptime); |
541 | } |
542 | } |
583fad99 |
543 | |
371a32e3 |
544 | function upgrade_get_javascript() { |
545 | global $CFG; |
546 | |
547 | if (!empty($_SESSION['installautopilot'])) { |
548 | $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = true;</script>'."\n"; |
549 | } else { |
550 | $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = false;</script>'."\n"; |
551 | } |
552 | $linktoscrolltoerrors .= '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>'; |
553 | |
554 | return $linktoscrolltoerrors; |
555 | } |
ad6226fb |
556 | |
557 | function create_admin_user() { |
558 | global $CFG, $USER; |
559 | |
560 | if (empty($CFG->rolesactive)) { // No admin user yet. |
561 | |
562 | $user = new object(); |
563 | $user->auth = 'manual'; |
564 | $user->firstname = get_string('admin'); |
565 | $user->lastname = get_string('user'); |
566 | $user->username = 'admin'; |
567 | $user->password = hash_internal_user_password('admin'); |
568 | $user->email = 'root@localhost'; |
569 | $user->confirmed = 1; |
570 | $user->mnethostid = $CFG->mnet_localhost_id; |
571 | $user->lang = $CFG->lang; |
572 | $user->maildisplay = 1; |
573 | $user->timemodified = time(); |
574 | |
575 | if (!$user->id = insert_record('user', $user)) { |
576 | error('SERIOUS ERROR: Could not create admin user record !!!'); |
577 | } |
578 | |
579 | if (!$user = get_record('user', 'id', $user->id)) { // Double check. |
580 | error('User ID was incorrect (can\'t find it)'); |
581 | } |
582 | |
583 | // Assign the default admin roles to the new user. |
584 | if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW)) { |
585 | error('No admin role could be found'); |
586 | } |
84c8ede0 |
587 | $sitecontext = get_context_instance(CONTEXT_SYSTEM); |
ad6226fb |
588 | foreach ($adminroles as $adminrole) { |
589 | role_assign($adminrole->id, $user->id, 0, $sitecontext->id); |
590 | } |
591 | |
592 | set_config('rolesactive', 1); |
593 | |
594 | // Log the user in. |
595 | $USER = get_complete_user_data('username', 'admin'); |
596 | $USER->newadminuser = 1; |
597 | load_all_capabilities(); |
598 | |
599 | redirect("$CFG->wwwroot/user/editadvanced.php?id=$user->id"); // Edit thyself |
600 | } else { |
601 | error('Can not create admin!'); |
602 | } |
603 | } |
604 | |
583fad99 |
605 | //////////////////////////////////////////////// |
606 | /// upgrade logging functions |
607 | //////////////////////////////////////////////// |
608 | |
609 | $upgradeloghandle = false; |
26c91c73 |
610 | $upgradelogbuffer = ''; |
611 | // I did not find out how to use static variable in callback function, |
612 | // the problem was that I could not flush the static buffer :-( |
613 | global $upgradeloghandle, $upgradelogbuffer; |
583fad99 |
614 | |
615 | /** |
616 | * Check if upgrade is already running. |
617 | * |
618 | * If anything goes wrong due to missing call to upgrade_log_finish() |
619 | * just restart the browser. |
620 | * |
621 | * @param string warning message indicating upgrade is already running |
622 | * @param int page reload timeout |
623 | */ |
624 | function upgrade_check_running($message, $timeout) { |
625 | if (!empty($_SESSION['upgraderunning'])) { |
626 | print_header(); |
627 | redirect(me(), $message, $timeout); |
628 | } |
629 | } |
630 | |
631 | /** |
632 | * Start logging of output into file (if not disabled) and |
633 | * prevent aborting and concurrent execution of upgrade script. |
634 | * |
635 | * Please note that you can not write into session variables after calling this function! |
636 | * |
637 | * This function may be called repeatedly. |
638 | */ |
639 | function upgrade_log_start() { |
426a369b |
640 | global $CFG, $upgradeloghandle; |
583fad99 |
641 | |
642 | if (!empty($_SESSION['upgraderunning'])) { |
643 | return; // logging already started |
644 | } |
645 | |
646 | @ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading |
647 | $_SESSION['upgraderunning'] = 1; // set upgrade indicator |
426a369b |
648 | if (empty($CFG->dbsessions)) { // workaround for bug in adodb, db session can not be restarted |
649 | session_write_close(); // from now on user can reload page - will be displayed warning |
650 | } |
583fad99 |
651 | make_upload_directory('upgradelogs'); |
652 | ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP |
dedb2304 |
653 | register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging |
583fad99 |
654 | } |
655 | |
656 | /** |
657 | * Terminate logging of output, flush all data, allow script aborting |
658 | * and reopen session for writing. Function error() does terminate the logging too. |
659 | * |
660 | * Please make sure that each upgrade_log_start() is properly terminated by |
661 | * this function or error(). |
662 | * |
663 | * This function may be called repeatedly. |
664 | */ |
665 | function upgrade_log_finish() { |
426a369b |
666 | global $CFG, $upgradeloghandle, $upgradelogbuffer; |
583fad99 |
667 | |
668 | if (empty($_SESSION['upgraderunning'])) { |
669 | return; // logging already terminated |
670 | } |
671 | |
672 | @ob_end_flush(); |
26c91c73 |
673 | if ($upgradelogbuffer !== '') { |
674 | @fwrite($upgradeloghandle, $upgradelogbuffer); |
40896537 |
675 | $upgradelogbuffer = ''; |
26c91c73 |
676 | } |
677 | if ($upgradeloghandle and ($upgradeloghandle !== 'error')) { |
678 | @fclose($upgradeloghandle); |
40896537 |
679 | $upgradeloghandle = false; |
26c91c73 |
680 | } |
426a369b |
681 | if (empty($CFG->dbsessions)) { |
682 | @session_start(); // ignore header errors, we only need to reopen session |
683 | } |
583fad99 |
684 | $_SESSION['upgraderunning'] = 0; // clear upgrade indicator |
685 | if (connection_aborted()) { |
686 | die; |
687 | } |
688 | @ignore_user_abort(false); |
689 | } |
690 | |
691 | /** |
692 | * Callback function for logging into files. Not more than one file is created per minute, |
693 | * upgrade session (terminated by upgrade_log_finish()) is always stored in one file. |
694 | * |
695 | * This function must not output any characters or throw warnigns and errors! |
696 | */ |
697 | function upgrade_log_callback($string) { |
26c91c73 |
698 | global $CFG, $upgradeloghandle, $upgradelogbuffer; |
583fad99 |
699 | |
700 | if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) { |
701 | if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) { |
26c91c73 |
702 | $upgradelogbuffer .= $string; |
703 | if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer |
704 | @fwrite($upgradeloghandle, $upgradelogbuffer); |
705 | $upgradelogbuffer = ''; |
706 | } |
583fad99 |
707 | } else { |
708 | $upgradeloghandle = 'error'; |
709 | } |
710 | } |
711 | return $string; |
712 | } |
713 | |
57e35f32 |
714 | /** |
715 | * Try to verify that dataroot is not accessible from web. |
716 | * It is not 100% correct but might help to reduce number of vulnerable sites. |
717 | * |
718 | * Protection from httpd.conf and .htaccess is not detected properly. |
719 | */ |
720 | function is_dataroot_insecure() { |
721 | global $CFG; |
722 | |
723 | $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround |
724 | |
725 | $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1); |
726 | $rp = strrev(trim($rp, '/')); |
727 | $rp = explode('/', $rp); |
728 | foreach($rp as $r) { |
729 | if (strpos($siteroot, '/'.$r.'/') === 0) { |
730 | $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory |
731 | } else { |
732 | break; // probably alias root |
733 | } |
734 | } |
735 | |
736 | $siteroot = strrev($siteroot); |
737 | $dataroot = str_replace('\\', '/', $CFG->dataroot.'/'); |
738 | |
739 | if (strpos($dataroot, $siteroot) === 0) { |
740 | return true; |
741 | } |
742 | return false; |
743 | } |
6e4dc10f |
744 | |
745 | /// ============================================================================================================= |
746 | /// administration tree classes and functions |
747 | |
748 | |
749 | // n.b. documentation is still in progress for this code |
750 | |
751 | /// INTRODUCTION |
752 | |
753 | /// This file performs the following tasks: |
754 | /// -it defines the necessary objects and interfaces to build the Moodle |
755 | /// admin hierarchy |
eef868d1 |
756 | /// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(), |
6e4dc10f |
757 | /// and admin_externalpage_print_footer() functions used on admin pages |
758 | |
759 | /// ADMIN_SETTING OBJECTS |
760 | |
eef868d1 |
761 | /// Moodle settings are represented by objects that inherit from the admin_setting |
6e4dc10f |
762 | /// class. These objects encapsulate how to read a setting, how to write a new value |
763 | /// to a setting, and how to appropriately display the HTML to modify the setting. |
764 | |
765 | /// ADMIN_SETTINGPAGE OBJECTS |
766 | |
767 | /// The admin_setting objects are then grouped into admin_settingpages. The latter |
768 | /// appear in the Moodle admin tree block. All interaction with admin_settingpage |
769 | /// objects is handled by the admin/settings.php file. |
770 | |
771 | /// ADMIN_EXTERNALPAGE OBJECTS |
772 | |
773 | /// There are some settings in Moodle that are too complex to (efficiently) handle |
774 | /// with admin_settingpages. (Consider, for example, user management and displaying |
775 | /// lists of users.) In this case, we use the admin_externalpage object. This object |
776 | /// places a link to an external PHP file in the admin tree block. |
777 | |
778 | /// If you're using an admin_externalpage object for some settings, you can take |
779 | /// advantage of the admin_externalpage_* functions. For example, suppose you wanted |
780 | /// to add a foo.php file into admin. First off, you add the following line to |
781 | /// admin/settings/first.php (at the end of the file) or to some other file in |
782 | /// admin/settings: |
783 | |
eef868d1 |
784 | /// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'), |
6e4dc10f |
785 | /// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission')); |
786 | |
787 | /// Next, in foo.php, your file structure would resemble the following: |
788 | |
789 | /// require_once('.../config.php'); |
790 | /// require_once($CFG->libdir.'/adminlib.php'); |
1ae083e4 |
791 | /// admin_externalpage_setup('foo'); |
6e4dc10f |
792 | /// // functionality like processing form submissions goes here |
1ae083e4 |
793 | /// admin_externalpage_print_header(); |
6e4dc10f |
794 | /// // your HTML goes here |
1ae083e4 |
795 | /// admin_externalpage_print_footer(); |
6e4dc10f |
796 | |
797 | /// The admin_externalpage_setup() function call ensures the user is logged in, |
798 | /// and makes sure that they have the proper role permission to access the page. |
799 | |
800 | /// The admin_externalpage_print_header() function prints the header (it figures |
801 | /// out what category and subcategories the page is classified under) and ensures |
802 | /// that you're using the admin pagelib (which provides the admin tree block and |
803 | /// the admin bookmarks block). |
804 | |
805 | /// The admin_externalpage_print_footer() function properly closes the tables |
806 | /// opened up by the admin_externalpage_print_header() function and prints the |
807 | /// standard Moodle footer. |
808 | |
809 | /// ADMIN_CATEGORY OBJECTS |
810 | |
811 | /// Above and beyond all this, we have admin_category objects. These objects |
812 | /// appear as folders in the admin tree block. They contain admin_settingpage's, |
813 | /// admin_externalpage's, and other admin_category's. |
814 | |
815 | /// OTHER NOTES |
816 | |
817 | /// admin_settingpage's, admin_externalpage's, and admin_category's all inherit |
818 | /// from part_of_admin_tree (a pseudointerface). This interface insists that |
819 | /// a class has a check_access method for access permissions, a locate method |
220a90c5 |
820 | /// used to find a specific node in the admin tree and find parent path. |
6e4dc10f |
821 | |
822 | /// admin_category's inherit from parentable_part_of_admin_tree. This pseudo- |
823 | /// interface ensures that the class implements a recursive add function which |
824 | /// accepts a part_of_admin_tree object and searches for the proper place to |
825 | /// put it. parentable_part_of_admin_tree implies part_of_admin_tree. |
826 | |
827 | /// Please note that the $this->name field of any part_of_admin_tree must be |
828 | /// UNIQUE throughout the ENTIRE admin tree. |
829 | |
830 | /// The $this->name field of an admin_setting object (which is *not* part_of_ |
831 | /// admin_tree) must be unique on the respective admin_settingpage where it is |
832 | /// used. |
833 | |
834 | |
6e4dc10f |
835 | /// CLASS DEFINITIONS ///////////////////////////////////////////////////////// |
836 | |
837 | /** |
838 | * Pseudointerface for anything appearing in the admin tree |
839 | * |
840 | * The pseudointerface that is implemented by anything that appears in the admin tree |
841 | * block. It forces inheriting classes to define a method for checking user permissions |
842 | * and methods for finding something in the admin tree. |
843 | * |
844 | * @author Vincenzo K. Marcovecchio |
845 | * @package admin |
846 | */ |
847 | class part_of_admin_tree { |
848 | |
849 | /** |
850 | * Finds a named part_of_admin_tree. |
851 | * |
852 | * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree |
853 | * and not parentable_part_of_admin_tree, then this function should only check if |
854 | * $this->name matches $name. If it does, it should return a reference to $this, |
855 | * otherwise, it should return a reference to NULL. |
856 | * |
857 | * If a class inherits parentable_part_of_admin_tree, this method should be called |
858 | * recursively on all child objects (assuming, of course, the parent object's name |
859 | * doesn't match the search criterion). |
860 | * |
861 | * @param string $name The internal name of the part_of_admin_tree we're searching for. |
862 | * @return mixed An object reference or a NULL reference. |
863 | */ |
eef868d1 |
864 | function &locate($name) { |
865 | trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING); |
866 | return; |
6e4dc10f |
867 | } |
4672d955 |
868 | |
869 | /** |
870 | * Removes named part_of_admin_tree. |
871 | * |
872 | * @param string $name The internal name of the part_of_admin_tree we want to remove. |
a8a66c96 |
873 | * @return bool success. |
4672d955 |
874 | */ |
875 | function prune($name) { |
eef868d1 |
876 | trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING); |
4672d955 |
877 | return; |
eef868d1 |
878 | } |
4672d955 |
879 | |
220a90c5 |
880 | /** |
881 | * Search using query |
882 | * @param strin query |
883 | * @return mixed array-object structure of found settings and pages |
884 | */ |
885 | function search($query) { |
886 | trigger_error('Admin class does not implement method <strong>search()</strong>', E_USER_WARNING); |
887 | return; |
888 | } |
889 | |
6e4dc10f |
890 | /** |
891 | * Verifies current user's access to this part_of_admin_tree. |
892 | * |
893 | * Used to check if the current user has access to this part of the admin tree or |
894 | * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree, |
895 | * then this method is usually just a call to has_capability() in the site context. |
896 | * |
897 | * If a class inherits parentable_part_of_admin_tree, this method should return the |
898 | * logical OR of the return of check_access() on all child objects. |
899 | * |
900 | * @return bool True if the user has access, false if she doesn't. |
901 | */ |
eef868d1 |
902 | function check_access() { |
903 | trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING); |
904 | return; |
6e4dc10f |
905 | } |
eef868d1 |
906 | |
a8a66c96 |
907 | /** |
908 | * Mostly usefull for removing of some parts of the tree in admin tree block. |
909 | * |
910 | * @return True is hidden from normal list view |
911 | */ |
912 | function is_hidden() { |
913 | trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING); |
914 | return; |
915 | } |
6e4dc10f |
916 | } |
917 | |
918 | /** |
919 | * Pseudointerface implemented by any part_of_admin_tree that has children. |
920 | * |
921 | * The pseudointerface implemented by any part_of_admin_tree that can be a parent |
922 | * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart |
eef868d1 |
923 | * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods |
6e4dc10f |
924 | * include an add method for adding other part_of_admin_tree objects as children. |
925 | * |
926 | * @author Vincenzo K. Marcovecchio |
927 | * @package admin |
928 | */ |
929 | class parentable_part_of_admin_tree extends part_of_admin_tree { |
eef868d1 |
930 | |
6e4dc10f |
931 | /** |
932 | * Adds a part_of_admin_tree object to the admin tree. |
933 | * |
934 | * Used to add a part_of_admin_tree object to this object or a child of this |
935 | * object. $something should only be added if $destinationname matches |
936 | * $this->name. If it doesn't, add should be called on child objects that are |
937 | * also parentable_part_of_admin_tree's. |
938 | * |
939 | * @param string $destinationname The internal name of the new parent for $something. |
940 | * @param part_of_admin_tree &$something The object to be added. |
941 | * @return bool True on success, false on failure. |
942 | */ |
220a90c5 |
943 | function add($destinationname, $something) { |
eef868d1 |
944 | trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING); |
945 | return; |
6e4dc10f |
946 | } |
eef868d1 |
947 | |
6e4dc10f |
948 | } |
949 | |
950 | /** |
951 | * The object used to represent folders (a.k.a. categories) in the admin tree block. |
eef868d1 |
952 | * |
6e4dc10f |
953 | * Each admin_category object contains a number of part_of_admin_tree objects. |
954 | * |
955 | * @author Vincenzo K. Marcovecchio |
956 | * @package admin |
957 | */ |
958 | class admin_category extends parentable_part_of_admin_tree { |
959 | |
960 | /** |
961 | * @var mixed An array of part_of_admin_tree objects that are this object's children |
962 | */ |
963 | var $children; |
eef868d1 |
964 | |
6e4dc10f |
965 | /** |
966 | * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects |
967 | */ |
968 | var $name; |
eef868d1 |
969 | |
6e4dc10f |
970 | /** |
971 | * @var string The displayed name for this category. Usually obtained through get_string() |
972 | */ |
973 | var $visiblename; |
eef868d1 |
974 | |
a8a66c96 |
975 | /** |
976 | * @var bool Should this category be hidden in admin tree block? |
977 | */ |
978 | var $hidden; |
979 | |
220a90c5 |
980 | /** |
981 | * paths |
982 | */ |
983 | var $path; |
984 | var $visiblepath; |
6e4dc10f |
985 | |
986 | /** |
987 | * Constructor for an empty admin category |
988 | * |
989 | * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects |
990 | * @param string $visiblename The displayed named for this category. Usually obtained through get_string() |
a8a66c96 |
991 | * @param bool $hidden hide category in admin tree block |
6e4dc10f |
992 | */ |
220a90c5 |
993 | function admin_category($name, $visiblename, $hidden=false) { |
994 | $this->children = array(); |
995 | $this->name = $name; |
6e4dc10f |
996 | $this->visiblename = $visiblename; |
220a90c5 |
997 | $this->hidden = $hidden; |
6e4dc10f |
998 | } |
eef868d1 |
999 | |
6e4dc10f |
1000 | /** |
220a90c5 |
1001 | * Returns a reference to the part_of_admin_tree object with internal name $name. |
6e4dc10f |
1002 | * |
220a90c5 |
1003 | * @param string $name The internal name of the object we want. |
1004 | * @param bool $findpath initialize path and visiblepath arrays |
1005 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
6e4dc10f |
1006 | */ |
220a90c5 |
1007 | function &locate($name, $findpath=false) { |
6e4dc10f |
1008 | if ($this->name == $name) { |
220a90c5 |
1009 | if ($findpath) { |
1010 | $this->visiblepath[] = $this->visiblename; |
1011 | $this->path[] = $this->name; |
1012 | } |
1013 | return $this; |
6e4dc10f |
1014 | } |
eef868d1 |
1015 | |
220a90c5 |
1016 | $return = NULL; |
1017 | foreach($this->children as $childid=>$unused) { |
1018 | if ($return =& $this->children[$childid]->locate($name, $findpath)) { |
1019 | break; |
6e4dc10f |
1020 | } |
1021 | } |
eef868d1 |
1022 | |
220a90c5 |
1023 | if (!is_null($return) and $findpath) { |
1024 | $return->visiblepath[] = $this->visiblename; |
1025 | $return->path[] = $this->name; |
1026 | } |
eef868d1 |
1027 | |
220a90c5 |
1028 | return $return; |
6e4dc10f |
1029 | } |
1030 | |
1031 | /** |
220a90c5 |
1032 | * Search using query |
1033 | * @param strin query |
1034 | * @return mixed array-object structure of found settings and pages |
6e4dc10f |
1035 | */ |
220a90c5 |
1036 | function search($query) { |
1037 | $result = array(); |
1038 | foreach ($this->children as $child) { |
3cea9c55 |
1039 | $subsearch = $child->search($query); |
1040 | if (!is_array($subsearch)) { |
1041 | debugging('Incorrect search result from '.$child->name); |
1042 | continue; |
1043 | } |
1044 | $result = array_merge($result, $subsearch); |
6e4dc10f |
1045 | } |
220a90c5 |
1046 | return $result; |
6e4dc10f |
1047 | } |
1048 | |
4672d955 |
1049 | /** |
1050 | * Removes part_of_admin_tree object with internal name $name. |
1051 | * |
1052 | * @param string $name The internal name of the object we want to remove. |
a8a66c96 |
1053 | * @return bool success |
4672d955 |
1054 | */ |
1055 | function prune($name) { |
1056 | |
1057 | if ($this->name == $name) { |
1058 | return false; //can not remove itself |
1059 | } |
1060 | |
1061 | foreach($this->children as $precedence => $child) { |
1062 | if ($child->name == $name) { |
1063 | // found it! |
eef868d1 |
1064 | unset($this->children[$precedence]); |
4672d955 |
1065 | return true; |
1066 | } |
1067 | if ($this->children[$precedence]->prune($name)) { |
1068 | return true; |
1069 | } |
1070 | } |
1071 | return false; |
1072 | } |
1073 | |
6e4dc10f |
1074 | /** |
1075 | * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object. |
1076 | * |
220a90c5 |
1077 | * @param string $destinationame The internal name of the immediate parent that we want for $something. |
1078 | * @param mixed $something A part_of_admin_tree or setting instanceto be added. |
1079 | * @return bool True if successfully added, false if $something can not be added. |
6e4dc10f |
1080 | */ |
220a90c5 |
1081 | function add($parentname, $something) { |
1082 | $parent =& $this->locate($parentname); |
1083 | if (is_null($parent)) { |
1084 | debugging('parent does not exist!'); |
6e4dc10f |
1085 | return false; |
1086 | } |
1087 | |
220a90c5 |
1088 | if (is_a($something, 'part_of_admin_tree')) { |
1089 | if (!is_a($parent, 'parentable_part_of_admin_tree')) { |
1090 | debugging('error - parts of tree can be inserted only into parentable parts'); |
1091 | return false; |
6e4dc10f |
1092 | } |
220a90c5 |
1093 | $parent->children[] = $something; |
6e4dc10f |
1094 | return true; |
eef868d1 |
1095 | |
220a90c5 |
1096 | } else { |
1097 | debugging('error - can not add this element'); |
1098 | return false; |
6e4dc10f |
1099 | } |
eef868d1 |
1100 | |
6e4dc10f |
1101 | } |
eef868d1 |
1102 | |
6e4dc10f |
1103 | /** |
1104 | * Checks if the user has access to anything in this category. |
1105 | * |
1106 | * @return bool True if the user has access to atleast one child in this category, false otherwise. |
1107 | */ |
1108 | function check_access() { |
6e4dc10f |
1109 | foreach ($this->children as $child) { |
220a90c5 |
1110 | if ($child->check_access()) { |
1111 | return true; |
1112 | } |
6e4dc10f |
1113 | } |
220a90c5 |
1114 | return false; |
6e4dc10f |
1115 | } |
eef868d1 |
1116 | |
a8a66c96 |
1117 | /** |
1118 | * Is this category hidden in admin tree block? |
1119 | * |
1120 | * @return bool True if hidden |
1121 | */ |
1122 | function is_hidden() { |
1123 | return $this->hidden; |
1124 | } |
6e4dc10f |
1125 | } |
1126 | |
220a90c5 |
1127 | class admin_root extends admin_category { |
1128 | /** |
1129 | * list of errors |
1130 | */ |
1131 | var $errors; |
1132 | |
1133 | /** |
1134 | * search query |
1135 | */ |
1136 | var $search; |
1137 | |
1138 | /** |
1139 | * full tree flag - true means all settings required, false onlypages required |
1140 | */ |
1141 | var $fulltree; |
1142 | |
1143 | |
1144 | function admin_root() { |
1145 | parent::admin_category('root', get_string('administration'), false); |
1146 | $this->errors = array(); |
1147 | $this->search = ''; |
1148 | $this->fulltree = true; |
1149 | } |
1150 | } |
1151 | |
6e4dc10f |
1152 | /** |
1153 | * Links external PHP pages into the admin tree. |
1154 | * |
1155 | * See detailed usage example at the top of this document (adminlib.php) |
1156 | * |
1157 | * @author Vincenzo K. Marcovecchio |
1158 | * @package admin |
1159 | */ |
1160 | class admin_externalpage extends part_of_admin_tree { |
1161 | |
eef868d1 |
1162 | /** |
6e4dc10f |
1163 | * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects |
1164 | */ |
1165 | var $name; |
eef868d1 |
1166 | |
6e4dc10f |
1167 | /** |
1168 | * @var string The displayed name for this external page. Usually obtained through get_string(). |
1169 | */ |
1170 | var $visiblename; |
eef868d1 |
1171 | |
6e4dc10f |
1172 | /** |
1173 | * @var string The external URL that we should link to when someone requests this external page. |
1174 | */ |
1175 | var $url; |
eef868d1 |
1176 | |
6e4dc10f |
1177 | /** |
1178 | * @var string The role capability/permission a user must have to access this external page. |
1179 | */ |
2ce38b70 |
1180 | var $req_capability; |
eef868d1 |
1181 | |
84c8ede0 |
1182 | /** |
1183 | * @var object The context in which capability/permission should be checked, default is site context. |
1184 | */ |
1185 | var $context; |
1186 | |
a8a66c96 |
1187 | /** |
1188 | * @var bool hidden in admin tree block. |
1189 | */ |
1190 | var $hidden; |
1191 | |
220a90c5 |
1192 | /** |
1193 | * visible path |
1194 | */ |
1195 | var $path; |
1196 | var $visiblepath; |
1197 | |
6e4dc10f |
1198 | /** |
1199 | * Constructor for adding an external page into the admin tree. |
1200 | * |
1201 | * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects. |
1202 | * @param string $visiblename The displayed name for this external page. Usually obtained through get_string(). |
1203 | * @param string $url The external URL that we should link to when someone requests this external page. |
38d2d43b |
1204 | * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'. |
6e4dc10f |
1205 | */ |
220a90c5 |
1206 | function admin_externalpage($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
1207 | $this->name = $name; |
6e4dc10f |
1208 | $this->visiblename = $visiblename; |
220a90c5 |
1209 | $this->url = $url; |
38d2d43b |
1210 | if (is_array($req_capability)) { |
1211 | $this->req_capability = $req_capability; |
1212 | } else { |
1213 | $this->req_capability = array($req_capability); |
1214 | } |
220a90c5 |
1215 | $this->hidden = $hidden; |
84c8ede0 |
1216 | $this->context = $context; |
6e4dc10f |
1217 | } |
eef868d1 |
1218 | |
6e4dc10f |
1219 | /** |
1220 | * Returns a reference to the part_of_admin_tree object with internal name $name. |
1221 | * |
1222 | * @param string $name The internal name of the object we want. |
1223 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
1224 | */ |
220a90c5 |
1225 | function &locate($name, $findpath=false) { |
1226 | if ($this->name == $name) { |
1227 | if ($findpath) { |
1228 | $this->visiblepath = array($this->visiblename); |
1229 | $this->path = array($this->name); |
1230 | } |
1231 | return $this; |
1232 | } else { |
1233 | $return = NULL; |
1234 | return $return; |
1235 | } |
6e4dc10f |
1236 | } |
4672d955 |
1237 | |
1238 | function prune($name) { |
1239 | return false; |
1240 | } |
1241 | |
220a90c5 |
1242 | /** |
1243 | * Search using query |
1244 | * @param strin query |
1245 | * @return mixed array-object structure of found settings and pages |
1246 | */ |
1247 | function search($query) { |
1248 | $textlib = textlib_get_instance(); |
1249 | |
1250 | $found = false; |
1251 | if (strpos(strtolower($this->name), $query) !== false) { |
1252 | $found = true; |
1253 | } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { |
1254 | $found = true; |
1255 | } |
1256 | if ($found) { |
1257 | $result = new object(); |
1258 | $result->page = $this; |
1259 | $result->settings = array(); |
1260 | return array($this->name => $result); |
1261 | } else { |
1262 | return array(); |
1263 | } |
1264 | } |
1265 | |
6e4dc10f |
1266 | /** |
2ce38b70 |
1267 | * Determines if the current user has access to this external page based on $this->req_capability. |
6e4dc10f |
1268 | * @return bool True if user has access, false otherwise. |
1269 | */ |
1270 | function check_access() { |
1271 | if (!get_site()) { |
1272 | return true; // no access check before site is fully set up |
1273 | } |
84c8ede0 |
1274 | $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context; |
38d2d43b |
1275 | foreach($this->req_capability as $cap) { |
1276 | if (has_capability($cap, $context)) { |
1277 | return true; |
1278 | } |
1279 | } |
1280 | return false; |
6e4dc10f |
1281 | } |
1282 | |
a8a66c96 |
1283 | /** |
1284 | * Is this external page hidden in admin tree block? |
1285 | * |
1286 | * @return bool True if hidden |
1287 | */ |
1288 | function is_hidden() { |
1289 | return $this->hidden; |
1290 | } |
1291 | |
6e4dc10f |
1292 | } |
1293 | |
1294 | /** |
1295 | * Used to group a number of admin_setting objects into a page and add them to the admin tree. |
1296 | * |
1297 | * @author Vincenzo K. Marcovecchio |
1298 | * @package admin |
1299 | */ |
1300 | class admin_settingpage extends part_of_admin_tree { |
1301 | |
eef868d1 |
1302 | /** |
6e4dc10f |
1303 | * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects |
1304 | */ |
1305 | var $name; |
eef868d1 |
1306 | |
6e4dc10f |
1307 | /** |
1308 | * @var string The displayed name for this external page. Usually obtained through get_string(). |
1309 | */ |
1310 | var $visiblename; |
1311 | /** |
1312 | * @var mixed An array of admin_setting objects that are part of this setting page. |
1313 | */ |
1314 | var $settings; |
eef868d1 |
1315 | |
6e4dc10f |
1316 | /** |
1317 | * @var string The role capability/permission a user must have to access this external page. |
1318 | */ |
2ce38b70 |
1319 | var $req_capability; |
eef868d1 |
1320 | |
84c8ede0 |
1321 | /** |
1322 | * @var object The context in which capability/permission should be checked, default is site context. |
1323 | */ |
1324 | var $context; |
1325 | |
a8a66c96 |
1326 | /** |
1327 | * @var bool hidden in admin tree block. |
1328 | */ |
1329 | var $hidden; |
1330 | |
220a90c5 |
1331 | /** |
1332 | * paths |
1333 | */ |
1334 | var $path; |
1335 | var $visiblepath; |
1336 | |
1337 | // see admin_externalpage |
1338 | function admin_settingpage($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
1339 | $this->settings = new object(); |
1340 | $this->name = $name; |
1341 | $this->visiblename = $visiblename; |
1342 | if (is_array($req_capability)) { |
1343 | $this->req_capability = $req_capability; |
6e4dc10f |
1344 | } else { |
220a90c5 |
1345 | $this->req_capability = array($req_capability); |
6e4dc10f |
1346 | } |
220a90c5 |
1347 | $this->hidden = $hidden; |
1348 | $this->context = $context; |
6e4dc10f |
1349 | } |
eef868d1 |
1350 | |
6e4dc10f |
1351 | // see admin_category |
220a90c5 |
1352 | function &locate($name, $findpath=false) { |
1353 | if ($this->name == $name) { |
1354 | if ($findpath) { |
1355 | $this->visiblepath = array($this->visiblename); |
1356 | $this->path = array($this->name); |
1357 | } |
1358 | return $this; |
1359 | } else { |
1360 | $return = NULL; |
1361 | return $return; |
1362 | } |
6e4dc10f |
1363 | } |
4672d955 |
1364 | |
220a90c5 |
1365 | function search($query) { |
1366 | $found = array(); |
4672d955 |
1367 | |
220a90c5 |
1368 | foreach ($this->settings as $setting) { |
1369 | if ($setting->is_related($query)) { |
1370 | $found[] = $setting; |
1371 | } |
1372 | } |
1373 | |
1374 | if ($found) { |
1375 | $result = new object(); |
1376 | $result->page = $this; |
1377 | $result->settings = $found; |
1378 | return array($this->name => $result); |
1379 | } |
1380 | |
1381 | $textlib = textlib_get_instance(); |
1382 | |
1383 | $found = false; |
1384 | if (strpos(strtolower($this->name), $query) !== false) { |
1385 | $found = true; |
1386 | } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { |
1387 | $found = true; |
1388 | } |
1389 | if ($found) { |
1390 | $result = new object(); |
1391 | $result->page = $this; |
1392 | $result->settings = array(); |
1393 | return array($this->name => $result); |
38d2d43b |
1394 | } else { |
220a90c5 |
1395 | return array(); |
38d2d43b |
1396 | } |
6e4dc10f |
1397 | } |
eef868d1 |
1398 | |
220a90c5 |
1399 | function prune($name) { |
6e4dc10f |
1400 | return false; |
1401 | } |
eef868d1 |
1402 | |
220a90c5 |
1403 | /** |
1404 | * not the same as add for admin_category. adds an admin_setting to this admin_settingpage. settings appear (on the settingpage) in the order in which they're added |
1405 | * n.b. each admin_setting in an admin_settingpage must have a unique internal name |
1406 | * @param object $setting is the admin_setting object you want to add |
1407 | * @return true if successful, false if not |
1408 | */ |
1409 | function add($setting) { |
1410 | if (!is_a($setting, 'admin_setting')) { |
1411 | debugging('error - not a setting instance'); |
1412 | return false; |
1413 | } |
1414 | |
1415 | $this->settings->{$setting->name} = $setting; |
1416 | return true; |
1417 | } |
1418 | |
6e4dc10f |
1419 | // see admin_externalpage |
1420 | function check_access() { |
1421 | if (!get_site()) { |
1422 | return true; // no access check before site is fully set up |
1423 | } |
84c8ede0 |
1424 | $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context; |
38d2d43b |
1425 | foreach($this->req_capability as $cap) { |
1426 | if (has_capability($cap, $context)) { |
1427 | return true; |
1428 | } |
1429 | } |
1430 | return false; |
6e4dc10f |
1431 | } |
eef868d1 |
1432 | |
220a90c5 |
1433 | /** |
1434 | * outputs this page as html in a table (suitable for inclusion in an admin pagetype) |
1435 | * returns a string of the html |
1436 | */ |
6e4dc10f |
1437 | function output_html() { |
220a90c5 |
1438 | $adminroot =& admin_get_root(); |
1439 | $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n"; |
6e4dc10f |
1440 | foreach($this->settings as $setting) { |
220a90c5 |
1441 | $fullname = $setting->get_full_name(); |
1442 | if (array_key_exists($fullname, $adminroot->errors)) { |
1443 | $data = $adminroot->errors[$fullname]->data; |
6e4dc10f |
1444 | } else { |
220a90c5 |
1445 | $data = $setting->get_setting(); |
1446 | if (is_null($data)) { |
1447 | $data = $setting->get_defaultsetting(); |
1448 | } |
6e4dc10f |
1449 | } |
220a90c5 |
1450 | $return .= $setting->output_html($data); |
6e4dc10f |
1451 | } |
220a90c5 |
1452 | $return .= '</fieldset>'; |
6e4dc10f |
1453 | return $return; |
1454 | } |
1455 | |
a8a66c96 |
1456 | /** |
1457 | * Is this settigns page hidden in admin tree block? |
1458 | * |
1459 | * @return bool True if hidden |
1460 | */ |
1461 | function is_hidden() { |
1462 | return $this->hidden; |
1463 | } |
1464 | |
6e4dc10f |
1465 | } |
1466 | |
1467 | |
220a90c5 |
1468 | /** |
1469 | * Admin settings class. Only exists on setting pages. |
1470 | * Read & write happens at this level; no authentication. |
1471 | */ |
6e4dc10f |
1472 | class admin_setting { |
1473 | |
1474 | var $name; |
1475 | var $visiblename; |
1476 | var $description; |
1477 | var $defaultsetting; |
220a90c5 |
1478 | var $updatedcallback; |
1479 | var $plugin; // null means main config table |
6e4dc10f |
1480 | |
220a90c5 |
1481 | /** |
1482 | * Constructor |
1483 | * @param $name string unique ascii name |
1484 | * @param $visiblename string localised name |
1485 | * @param strin $description localised long description |
1486 | * @param mixed $defaultsetting string or array depending on implementation |
1487 | */ |
6e4dc10f |
1488 | function admin_setting($name, $visiblename, $description, $defaultsetting) { |
220a90c5 |
1489 | $this->name = $name; |
1490 | $this->visiblename = $visiblename; |
1491 | $this->description = $description; |
6e4dc10f |
1492 | $this->defaultsetting = $defaultsetting; |
1493 | } |
eef868d1 |
1494 | |
220a90c5 |
1495 | function get_full_name() { |
1496 | return 's_'.$this->plugin.'_'.$this->name; |
1497 | } |
1498 | |
1499 | function get_id() { |
1500 | return 'id_s_'.$this->plugin.'_'.$this->name; |
1501 | } |
1502 | |
1503 | function config_read($name) { |
1504 | global $CFG; |
1505 | if ($this->plugin === 'backup') { |
1506 | require_once($CFG->dirroot.'/backup/lib.php'); |
1507 | $backupconfig = backup_get_config(); |
1508 | if (isset($backupconfig->$name)) { |
1509 | return $backupconfig->$name; |
1510 | } else { |
1511 | return NULL; |
1512 | } |
1513 | |
1514 | } else if (!empty($this->plugin)) { |
1515 | $value = get_config($this->plugin, $name); |
1516 | return $value === false ? NULL : $value; |
1517 | |
1518 | } else { |
1519 | if (isset($CFG->$name)) { |
1520 | return $CFG->$name; |
1521 | } else { |
1522 | return NULL; |
1523 | } |
1524 | } |
1525 | } |
1526 | |
1527 | function config_write($name, $value) { |
1528 | global $CFG; |
1529 | if ($this->plugin === 'backup') { |
1530 | require_once($CFG->dirroot.'/backup/lib.php'); |
1531 | return (boolean)backup_set_config($name, $value); |
1532 | } else { |
1533 | return (boolean)set_config($name, $value, $this->plugin); |
1534 | } |
1535 | } |
1536 | |
1537 | /** |
1538 | * Returns current value of this setting |
1539 | * @return mixed array or string depending on instance, NULL means not set yet |
1540 | */ |
6e4dc10f |
1541 | function get_setting() { |
220a90c5 |
1542 | // has to be overridden |
1543 | return NULL; |
6e4dc10f |
1544 | } |
eef868d1 |
1545 | |
220a90c5 |
1546 | /** |
1547 | * Returns default setting if exists |
1548 | * @return mixed array or string depending on instance; NULL means no default, user must supply |
1549 | */ |
8e5da17a |
1550 | function get_defaultsetting() { |
1551 | return $this->defaultsetting; |
1552 | } |
1553 | |
220a90c5 |
1554 | /** |
1555 | * Store new setting |
1556 | * @param mixed string or array, must not be NULL |
1557 | * @return '' if ok, string error message otherwise |
1558 | */ |
6e4dc10f |
1559 | function write_setting($data) { |
220a90c5 |
1560 | // should be overridden |
1561 | return ''; |
6e4dc10f |
1562 | } |
eef868d1 |
1563 | |
220a90c5 |
1564 | /** |
1565 | * Return part of form with setting |
1566 | * @param mixed data array or string depending on setting |
1567 | * @return string |
1568 | */ |
1569 | function output_html($data) { |
1570 | // should be overridden |
1571 | return; |
1572 | } |
1573 | |
1574 | /** |
1575 | * function called if setting updated - cleanup, cache reset, etc. |
1576 | */ |
1577 | function set_updatedcallback($functionname) { |
1578 | $this->updatedcallback = $functionname; |
1579 | } |
1580 | |
1581 | /** |
1582 | * Is setting related to query text - used when searching |
1583 | * @param string $query |
1584 | * @return bool |
1585 | */ |
1586 | function is_related($query) { |
1587 | if (strpos(strtolower($this->name), $query) !== false) { |
1588 | return true; |
1589 | } |
1590 | $textlib = textlib_get_instance(); |
1591 | if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { |
1592 | return true; |
1593 | } |
1594 | if (strpos($textlib->strtolower($this->description), $query) !== false) { |
1595 | return true; |
1596 | } |
1597 | return false; |
6e4dc10f |
1598 | } |
220a90c5 |
1599 | } |
eef868d1 |
1600 | |
220a90c5 |
1601 | /** |
1602 | * No setting - just heading and text. |
1603 | */ |
1604 | class admin_setting_heading extends admin_setting { |
1605 | /** |
1606 | * not a setting, just text |
1607 | * @param string $name of setting |
1608 | * @param string $heading heading |
1609 | * @param string $information text in box |
1610 | */ |
1611 | function admin_setting_heading($name, $heading, $information) { |
1612 | parent::admin_setting($name, $heading, $information, ''); |
1613 | } |
1614 | |
1615 | function get_setting() { |
1616 | return true; |
1617 | } |
1618 | |
1619 | function get_defaultsetting() { |
1620 | return true; |
1621 | } |
1622 | |
1623 | function write_setting($data) { |
1624 | // do not write any setting |
1625 | return ''; |
1626 | } |
1627 | |
1628 | function output_html($data) { |
1629 | $return = ''; |
1630 | if ($this->visiblename != '') { |
1631 | $return .= print_heading('<a name="'.$this->name.'">'.$this->visiblename.'</a>', '', 3, 'main', true); |
1632 | } |
1633 | if ($this->description != '') { |
1634 | $return .= print_box($this->description, 'generalbox formsettingheading', '', true); |
1635 | } |
1636 | return $return; |
1637 | } |
1638 | } |
6e4dc10f |
1639 | |
220a90c5 |
1640 | /** |
1641 | * The most flexibly setting, user is typing text |
1642 | */ |
6e4dc10f |
1643 | class admin_setting_configtext extends admin_setting { |
1644 | |
1645 | var $paramtype; |
1646 | |
220a90c5 |
1647 | /** |
1648 | * config text contructor |
1649 | * @param string $name of setting |
1650 | * @param string $visiblename localised |
1651 | * @param string $description long localised info |
1652 | * @param string $defaultsetting |
1653 | * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex |
1654 | */ |
50999a0b |
1655 | function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) { |
6e4dc10f |
1656 | $this->paramtype = $paramtype; |
1657 | parent::admin_setting($name, $visiblename, $description, $defaultsetting); |
1658 | } |
1659 | |
1660 | function get_setting() { |
220a90c5 |
1661 | return $this->config_read($this->name); |
6e4dc10f |
1662 | } |
eef868d1 |
1663 | |
6e4dc10f |
1664 | function write_setting($data) { |
8cad6cca |
1665 | if ($this->paramtype === PARAM_INT and $data === '') { |
1666 | // do not complain if '' used instead of 0 |
1667 | $data = 0; |
1668 | } |
220a90c5 |
1669 | // $data is a string |
e33fbf87 |
1670 | $validated = $this->validate($data); |
1671 | if ($validated !== true) { |
1672 | return $validated; |
c235598d |
1673 | } |
220a90c5 |
1674 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
1675 | } |
1676 | |
e33fbf87 |
1677 | /** |
1678 | * Validate data before storage |
1679 | * @param string data |
1680 | * @return mixed true if ok string if error found |
1681 | */ |
c235598d |
1682 | function validate($data) { |
9e24fbd1 |
1683 | if (is_string($this->paramtype)) { |
e33fbf87 |
1684 | if (preg_match($this->paramtype, $data)) { |
1685 | return true; |
1686 | } else { |
1687 | return get_string('validateerror', 'admin'); |
1688 | } |
1689 | |
9e24fbd1 |
1690 | } else if ($this->paramtype === PARAM_RAW) { |
1691 | return true; |
e33fbf87 |
1692 | |
9e24fbd1 |
1693 | } else { |
e33fbf87 |
1694 | $cleaned = stripslashes(clean_param(addslashes($data), $this->paramtype)); |
1695 | if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison |
1696 | return true; |
1697 | } else { |
1698 | return get_string('validateerror', 'admin'); |
1699 | } |
9e24fbd1 |
1700 | } |
c235598d |
1701 | } |
1702 | |
220a90c5 |
1703 | function output_html($data) { |
1704 | $default = $this->get_defaultsetting(); |
1705 | |
1706 | if (!is_null($default)) { |
1707 | if ($default === '') { |
1708 | $default = get_string('emptysettingvalue', 'admin'); |
1709 | } |
1710 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', s($default)).'</span>'; |
1711 | } else { |
1712 | $defaultinfo = ''; |
1713 | } |
1714 | |
1715 | if ($this->paramtype === PARAM_INT) { |
1716 | $paramclass = 'class="number"'; |
c8218a42 |
1717 | } else { |
220a90c5 |
1718 | $paramclass = ''; |
c8218a42 |
1719 | } |
220a90c5 |
1720 | |
1721 | return format_admin_setting($this, $this->visiblename, |
1722 | '<div class="form-text">' |
1723 | .'<input type="text" '.$paramclass.' id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />' |
1724 | .$defaultinfo.'</div>', |
6153cf58 |
1725 | $this->description); |
6e4dc10f |
1726 | } |
6e4dc10f |
1727 | } |
1728 | |
220a90c5 |
1729 | /** |
1730 | * General text area without html editor. |
1731 | */ |
1732 | class admin_setting_configtextarea extends admin_setting_configtext { |
1733 | var $rows; |
1734 | var $cols; |
eba8cd63 |
1735 | |
28764710 |
1736 | function admin_setting_configtextarea($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { |
220a90c5 |
1737 | $this->rows = $rows; |
1738 | $this->cols = $cols; |
eba8cd63 |
1739 | parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype); |
1740 | } |
1741 | |
220a90c5 |
1742 | function output_html($data) { |
1743 | $default = $this->get_defaultsetting(); |
1744 | |
1745 | if (!is_null($default)) { |
1746 | if ($default === '') { |
1747 | $default = get_string('emptysettingvalue', 'admin'); |
1748 | } |
1749 | $defaultinfo = '<div class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', '<br />'.format_text($default, FORMAT_PLAIN)).'</div>'; |
eba8cd63 |
1750 | } else { |
220a90c5 |
1751 | $defaultinfo = ''; |
eba8cd63 |
1752 | } |
220a90c5 |
1753 | |
1754 | return format_admin_setting($this, $this->visiblename, |
1755 | '<div class="form-textarea" ><textarea rows="'.$this->rows.'" cols="'.$this->cols.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'">'.s($data).'</textarea>'.$defaultinfo.'</div>', |
1756 | $this->description); |
1757 | } |
1758 | } |
1759 | |
1760 | /** |
1761 | * Password field, allows unmasking of password |
1762 | */ |
1763 | class admin_setting_configpasswordunmask extends admin_setting_configtext { |
1764 | /** |
1765 | * Constructor |
1766 | * @param string $name of setting |
1767 | * @param string $visiblename localised |
1768 | * @param string $description long localised info |
1769 | * @param string $defaultsetting default password |
1770 | */ |
1771 | function admin_setting_configpasswordunmask($name, $visiblename, $description, $defaultsetting) { |
1772 | parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, PARAM_RAW); |
1773 | } |
1774 | |
1775 | function output_html($data) { |
1776 | $id = $this->get_id(); |
1777 | $unmask = get_string('unmaskpassword', 'form'); |
1778 | $unmaskjs = '<script type="text/javascript"> |
eba8cd63 |
1779 | //<![CDATA[ |
220a90c5 |
1780 | document.write(\'<span class="unmask"><input id="'.$id.'unmask" value="1" type="checkbox" onclick="unmaskPassword(\\\''.$id.'\\\')"/><label for="'.$id.'unmask">'.addslashes_js($unmask).'<\/label><\/span>\'); |
eba8cd63 |
1781 | //]]> |
1782 | </script>'; |
220a90c5 |
1783 | return format_admin_setting($this, $this->visiblename, |
1784 | '<div class="form-password"><input type="password" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$unmaskjs.'</div>', |
1785 | $this->description); |
1786 | } |
1787 | } |
1788 | |
1789 | /** |
1790 | * Path to executable file |
1791 | */ |
1792 | class admin_setting_configexecutable extends admin_setting_configtext { |
1793 | /** |
1794 | * Constructor |
1795 | * @param string $name of setting |
1796 | * @param string $visiblename localised |
1797 | * @param string $description long localised info |
1798 | * @param string $defautpath default path |
1799 | */ |
1800 | function admin_setting_configexecutable($name, $visiblename, $description, $defaultpath) { |
1801 | parent::admin_setting_configtext($name, $visiblename, $description, $defaultpath, PARAM_RAW); |
1802 | } |
1803 | |
1804 | function output_html($data) { |
1805 | $default = $this->get_defaultsetting(); |
1806 | |
1807 | if (!is_null($default)) { |
1808 | if ($default === '') { |
1809 | $default = get_string('emptysettingvalue', 'admin'); |
1810 | } |
1811 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', s($default)).'</span>'; |
1812 | } else { |
1813 | $defaultinfo = ''; |
1814 | } |
1815 | |
1816 | if ($data) { |
1817 | if (file_exists($data) and is_executable($data)) { |
1818 | $executable = '<span class="pathok">✔</span>'; |
1819 | } else { |
1820 | $executable = '<span class="patherror">✘</span>'; |
1821 | } |
1822 | } else { |
1823 | $executable = ''; |
1824 | } |
1825 | |
1826 | return format_admin_setting($this, $this->visiblename, |
1827 | '<div class="form-executable">' |
1828 | .'<input type="text" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable |
1829 | .$defaultinfo.'</div>', |
eba8cd63 |
1830 | $this->description); |
1831 | } |
220a90c5 |
1832 | } |
1833 | |
1834 | /** |
1835 | * Path to directory |
1836 | */ |
1837 | class admin_setting_configdirectory extends admin_setting_configtext { |
1838 | /** |
1839 | * Constructor |
1840 | * @param string $name of setting |
1841 | * @param string $visiblename localised |
1842 | * @param string $description long localised info |
1843 | * @param string $defaultdirectory default directory location |
1844 | */ |
1845 | function admin_setting_configdirectory($name, $visiblename, $description, $defaultdirectory) { |
1846 | parent::admin_setting_configtext($name, $visiblename, $description, $defaultdirectory, PARAM_RAW); |
1847 | } |
1848 | |
1849 | function output_html($data) { |
1850 | $default = $this->get_defaultsetting(); |
1851 | |
1852 | if (!is_null($default)) { |
1853 | if ($default === '') { |
1854 | $default = get_string('emptysettingvalue', 'admin'); |
1855 | } |
1856 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', s($default)).'</span>'; |
1857 | } else { |
1858 | $defaultinfo = ''; |
1859 | } |
1860 | |
1861 | if ($data) { |
1862 | if (file_exists($data) and is_dir($data)) { |
1863 | $executable = '<span class="pathok">✔</span>'; |
1864 | } else { |
1865 | $executable = '<span class="patherror">✘</span>'; |
1866 | } |
1867 | } else { |
1868 | $executable = ''; |
1869 | } |
9ba38673 |
1870 | |
220a90c5 |
1871 | return format_admin_setting($this, $this->visiblename, |
1872 | '<div class="form-directory">' |
1873 | .'<input type="text" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable |
1874 | .$defaultinfo.'</div>', |
1875 | $this->description); |
1876 | } |
eba8cd63 |
1877 | } |
1878 | |
220a90c5 |
1879 | /** |
1880 | * Checkbox |
1881 | */ |
6e4dc10f |
1882 | class admin_setting_configcheckbox extends admin_setting { |
220a90c5 |
1883 | var $yes; |
1884 | var $no; |
6e4dc10f |
1885 | |
220a90c5 |
1886 | /** |
1887 | * Constructor |
1888 | * @param string $name of setting |
1889 | * @param string $visiblename localised |
1890 | * @param string $description long localised info |
1891 | * @param string $defaultsetting |
1892 | * @param string $yes value used when checked |
1893 | * @param string $no value used when not checked |
1894 | */ |
1895 | function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') { |
6e4dc10f |
1896 | parent::admin_setting($name, $visiblename, $description, $defaultsetting); |
220a90c5 |
1897 | $this->yes = (string)$yes; |
1898 | $this->no = (string)$no; |
6e4dc10f |
1899 | } |
1900 | |
1901 | function get_setting() { |
220a90c5 |
1902 | return $this->config_read($this->name); |
6e4dc10f |
1903 | } |
eef868d1 |
1904 | |
6e4dc10f |
1905 | function write_setting($data) { |
220a90c5 |
1906 | if ((string)$data === $this->yes) { // convert to strings before comparison |
1907 | $data = $this->yes; |
6e4dc10f |
1908 | } else { |
220a90c5 |
1909 | $data = $this->no; |
6e4dc10f |
1910 | } |
220a90c5 |
1911 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
1912 | } |
1913 | |
220a90c5 |
1914 | function output_html($data) { |
1915 | $default = $this->get_defaultsetting(); |
1916 | |
1917 | if (!is_null($default)) { |
1918 | if ((string)$default === $this->yes) { |
1919 | $str = get_string('checkboxyes', 'admin'); |
1920 | } else { |
1921 | $str = get_string('checkboxno', 'admin'); |
1922 | } |
1923 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', $str).'</span>'; |
c8218a42 |
1924 | } else { |
220a90c5 |
1925 | $defaultinfo = ''; |
c8218a42 |
1926 | } |
220a90c5 |
1927 | |
1928 | if ((string)$data === $this->yes) { // convert to strings before comparison |
1929 | $checked = 'checked="checked"'; |
1930 | } else { |
1931 | $checked = ''; |
1932 | } |
1933 | |
1934 | return format_admin_setting($this, $this->visiblename, |
1935 | '<div class="form-checkbox" ><input type="hidden" name="'.$this->get_full_name().'" value="'.s($this->no).'" /> ' |
1936 | .'<input type="checkbox" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($this->yes).'" '.$checked.' />' |
1937 | .$defaultinfo.'</div>', |
6153cf58 |
1938 | $this->description); |
6e4dc10f |
1939 | } |
6e4dc10f |
1940 | } |
1941 | |
220a90c5 |
1942 | /** |
1943 | * Multiple checkboxes, each represents different value, stored in csv format |
1944 | */ |
1945 | class admin_setting_configmulticheckbox extends admin_setting { |
6e4dc10f |
1946 | var $choices; |
eef868d1 |
1947 | |
220a90c5 |
1948 | /** |
1949 | * Constructor |
1950 | * @param string $name of setting |
1951 | * @param string $visiblename localised |
1952 | * @param string $description long localised info |
1953 | * @param array $defaultsetting array of selected |
1954 | * @param array $choices array of $value=>$label for each checkbox |
1955 | */ |
1956 | function admin_setting_configmulticheckbox($name, $visiblename, $description, $defaultsetting, $choices) { |
6e4dc10f |
1957 | $this->choices = $choices; |
1958 | parent::admin_setting($name, $visiblename, $description, $defaultsetting); |
1959 | } |
1960 | |
0a784551 |
1961 | /** |
1962 | * This function may be used in ancestors for lazy loading of choices |
220a90c5 |
1963 | * @return true if loaded, false if error |
0a784551 |
1964 | */ |
1965 | function load_choices() { |
1966 | /* |
220a90c5 |
1967 | if (is_array($this->choices)) { |
1968 | return true; |
0a784551 |
1969 | } |
1970 | .... load choices here |
1971 | */ |
220a90c5 |
1972 | return true; |
1973 | } |
1974 | |
1975 | /** |
1976 | * Is setting related to query text - used when searching |
1977 | * @param string $query |
1978 | * @return bool |
1979 | */ |
1980 | function is_related($query) { |
1981 | if (!$this->load_choices() or empty($this->choices)) { |
1982 | return false; |
1983 | } |
1984 | if (parent::is_related($query)) { |
1985 | return true; |
1986 | } |
1987 | |
1988 | $textlib = textlib_get_instance(); |
1989 | foreach ($this->choices as $desc) { |
1990 | if (strpos($textlib->strtolower($desc), $query) !== false) { |
1991 | return true; |
1992 | } |
1993 | } |
1994 | return false; |
0a784551 |
1995 | } |
1996 | |
6e4dc10f |
1997 | function get_setting() { |
220a90c5 |
1998 | $result = $this->config_read($this->name); |
1999 | if (is_null($result)) { |
2000 | return NULL; |
2001 | } |
2002 | if ($result === '') { |
2003 | return array(); |
2004 | } |
2005 | return explode(',', $result); |
6e4dc10f |
2006 | } |
eef868d1 |
2007 | |
6e4dc10f |
2008 | function write_setting($data) { |
220a90c5 |
2009 | if (!is_array($data)) { |
2010 | return ''; // ignore it |
2011 | } |
2012 | if (!$this->load_choices() or empty($this->choices)) { |
2013 | return ''; |
2014 | } |
2015 | unset($data['xxxxx']); |
2016 | $result = array(); |
2017 | foreach ($data as $key => $value) { |
2018 | if ($value and array_key_exists($key, $this->choices)) { |
2019 | $result[] = $key; |
2020 | } |
2021 | } |
2022 | return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin'); |
6e4dc10f |
2023 | } |
eef868d1 |
2024 | |
220a90c5 |
2025 | function output_html($data) { |
2026 | if (!$this->load_choices() or empty($this->choices)) { |
2027 | return ''; |
2028 | } |
2029 | $default = $this->get_defaultsetting(); |
2030 | if (is_null($default)) { |
2031 | $default = array(); |
2032 | } |
2033 | if (is_null($data)) { |
2034 | foreach ($default as $key=>$value) { |
2035 | if ($value) { |
2036 | $current[] = $value; |
2037 | } |
2038 | } |
2039 | } |
2040 | |
2041 | $options = array(); |
2042 | $defaults = array(); |
2043 | foreach($this->choices as $key=>$description) { |
2044 | if (in_array($key, $data)) { |
2045 | $checked = 'checked="checked"'; |
2046 | } else { |
2047 | $checked = ''; |
2048 | } |
2049 | if (!empty($default[$key])) { |
2050 | $defaults[] = $description; |
2051 | } |
2052 | |
2053 | $options[] = '<input type="checkbox" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'['.$key.']" value="1" '.$checked.' />' |
2054 | .'<label for="'.$this->get_id().'_'.$key.'">'.$description.'</label>'; |
2055 | } |
2056 | |
2057 | if (!empty($defaults)) { |
2058 | $defaultinfo = '<div class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', implode(', ', $defaults)).'</div>'; |
c8218a42 |
2059 | } else { |
220a90c5 |
2060 | $defaultinfo = ''; |
c8218a42 |
2061 | } |
220a90c5 |
2062 | |
2063 | $return = '<div class="form-multicheckbox">'; |
2064 | $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected |
2065 | if ($options) { |
2066 | $return .= '<ul>'; |
2067 | foreach ($options as $option) { |
2068 | $return .= '<li>'.$option.'</li>'; |
2069 | } |
2070 | $return .= '</ul>'; |
6e4dc10f |
2071 | } |
220a90c5 |
2072 | $return .= $defaultinfo.'</div>'; |
6153cf58 |
2073 | |
220a90c5 |
2074 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
6e4dc10f |
2075 | } |
6e4dc10f |
2076 | } |
2077 | |
220a90c5 |
2078 | /** |
2079 | * Multiple checkboxes 2, value stored as string 00101011 |
2080 | */ |
2081 | class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox { |
2082 | function get_setting() { |
2083 | $result = $this->config_read($this->name); |
2084 | if (is_null($result)) { |
2085 | return NULL; |
2086 | } |
2087 | if (!$this->load_choices()) { |
2088 | return NULL; |
2089 | } |
2090 | $result = str_pad($result, count($this->choices), '0'); |
2091 | $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY); |
2092 | $setting = array(); |
2093 | foreach ($this->choices as $key=>$unused) { |
2094 | $value = array_shift($result); |
2095 | if ($value) { |
2096 | $setting[] = $key; |
2097 | } |
2098 | } |
2099 | return $setting; |
2100 | } |
6e4dc10f |
2101 | |
220a90c5 |
2102 | function write_setting($data) { |
2103 | if (!is_array($data)) { |
2104 | return ''; // ignore it |
2105 | } |
2106 | if (!$this->load_choices() or empty($this->choices)) { |
2107 | return ''; |
2108 | } |
2109 | $result = ''; |
2110 | foreach ($this->choices as $key=>$unused) { |
2111 | if (!empty($data[$key])) { |
2112 | $result .= '1'; |
2113 | } else { |
2114 | $result .= '0'; |
2115 | } |
2116 | } |
2117 | return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin'); |
2118 | } |
2119 | } |
2120 | |
2121 | /** |
2122 | * Select one value from list |
2123 | */ |
2124 | class admin_setting_configselect extends admin_setting { |
6e4dc10f |
2125 | var $choices; |
6e4dc10f |
2126 | |
220a90c5 |
2127 | /** |
2128 | * Constructor |
2129 | * @param string $name of setting |
2130 | * @param string $visiblename localised |
2131 | * @param string $description long localised info |
2132 | * @param string $defaultsetting |
2133 | * @param array $choices array of $value=>$label for each selection |
2134 | */ |
2135 | function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) { |
2136 | $this->choices = $choices; |
2137 | parent::admin_setting($name, $visiblename, $description, $defaultsetting); |
2138 | } |
2139 | |
2140 | /** |
2141 | * This function may be used in ancestors for lazy loading of choices |
2142 | * @return true if loaded, false if error |
2143 | */ |
2144 | function load_choices() { |
2145 | /* |
2146 | if (is_array($this->choices)) { |
2147 | return true; |
6e4dc10f |
2148 | } |
220a90c5 |
2149 | .... load choices here |
2150 | */ |
2151 | return true; |
6e4dc10f |
2152 | } |
2153 | |
2154 | function get_setting() { |
220a90c5 |
2155 | return $this->config_read($this->name); |
6e4dc10f |
2156 | } |
eef868d1 |
2157 | |
6e4dc10f |
2158 | function write_setting($data) { |
220a90c5 |
2159 | if (!$this->load_choices() or empty($this->choices)) { |
2160 | return ''; |
2161 | } |
2162 | if (!array_key_exists($data, $this->choices)) { |
2163 | return ''; // ignore it |
2164 | } |
eef868d1 |
2165 | |
220a90c5 |
2166 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2167 | } |
eef868d1 |
2168 | |
220a90c5 |
2169 | function output_html($data) { |
2170 | if (!$this->load_choices() or empty($this->choices)) { |
2171 | return ''; |
2172 | } |
2173 | $default = $this->get_defaultsetting(); |
2174 | |
2175 | if (!is_null($default) and array_key_exists($default, $this->choices)) { |
2176 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', s($this->choices[$default])).'</span>'; |
cc73de71 |
2177 | } else { |
220a90c5 |
2178 | $defaultinfo = ''; |
6e4dc10f |
2179 | } |
220a90c5 |
2180 | |
9c305ba1 |
2181 | $current = $this->get_setting(); |
2182 | $warning = ''; |
2183 | if (is_null($current)) { |
2184 | //first run |
2185 | } else if (empty($current) and (array_key_exists('', $this->choices) or array_key_exists(0, $this->choices))) { |
2186 | // no warning |
2187 | } else if (!array_key_exists($current, $this->choices)) { |
2188 | $warning = get_string('warningcurrentsetting', 'admin', s($current)); |
2189 | if (!is_null($default) and $data==$current) { |
2190 | $data = $default; // use default instead of first value when showing the form |
2191 | } |
2192 | } |
2193 | |
220a90c5 |
2194 | $return = '<div class="form-select"><select id="'.$this->get_id().'" name="'.$this->get_full_name().'">'; |
6e4dc10f |
2195 | foreach ($this->choices as $key => $value) { |
220a90c5 |
2196 | // the string cast is needed because key may be integer - 0 is equal to most strings! |
2197 | $return .= '<option value="'.$key.'"'.((string)$key==$data ? ' selected="selected"' : '').'>'.$value.'</option>'; |
eef868d1 |
2198 | } |
220a90c5 |
2199 | $return .= '</select>'.$defaultinfo.'</div>'; |
2200 | |
9c305ba1 |
2201 | return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning); |
6e4dc10f |
2202 | } |
2203 | |
2204 | } |
2205 | |
220a90c5 |
2206 | /** |
2207 | * Select multiple items from list |
2208 | */ |
6e4dc10f |
2209 | class admin_setting_configmultiselect extends admin_setting_configselect { |
220a90c5 |
2210 | /** |
2211 | * Constructor |
2212 | * @param string $name of setting |
2213 | * @param string $visiblename localised |
2214 | * @param string $description long localised info |
2215 | * @param array $defaultsetting array of selected items |
2216 | * @param array $choices array of $value=>$label for each list item |
2217 | */ |
6e4dc10f |
2218 | function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) { |
2219 | parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices); |
2220 | } |
2221 | |
2222 | function get_setting() { |
220a90c5 |
2223 | $result = $this->config_read($this->name); |
2224 | if (is_null($result)) { |
d7933a55 |
2225 | return NULL; |
2226 | } |
220a90c5 |
2227 | if ($result === '') { |
2228 | return array(); |
2229 | } |
2230 | return explode(',', $result); |
6e4dc10f |
2231 | } |
eef868d1 |
2232 | |
6e4dc10f |
2233 | function write_setting($data) { |
220a90c5 |
2234 | if (!is_array($data)) { |
2235 | return ''; //ignore it |
2236 | } |
2237 | if (!$this->load_choices() or empty($this->choices)) { |
2238 | return ''; |
2239 | } |
2240 | |
a7ad48fd |
2241 | unset($data['xxxxx']); |
2242 | |
220a90c5 |
2243 | $save = array(); |
2244 | foreach ($data as $value) { |
2245 | if (!array_key_exists($value, $this->choices)) { |
2246 | continue; // ignore it |
2247 | } |
2248 | $save[] = $value; |
2249 | } |
2250 | |
2251 | return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin')); |
2252 | } |
2253 | |
2254 | /** |
2255 | * Is setting related to query text - used when searching |
2256 | * @param string $query |
2257 | * @return bool |
2258 | */ |
2259 | function is_related($query) { |
2260 | if (!$this->load_choices() or empty($this->choices)) { |
2261 | return false; |
2262 | } |
2263 | if (parent::is_related($query)) { |
2264 | return true; |
2265 | } |
2266 | |
2267 | $textlib = textlib_get_instance(); |
2268 | foreach ($this->choices as $desc) { |
2269 | if (strpos($textlib->strtolower($desc), $query) !== false) { |
2270 | return true; |
2271 | } |
2272 | } |
2273 | return false; |
2274 | } |
2275 | |
2276 | function output_html($data) { |
2277 | if (!$this->load_choices() or empty($this->choices)) { |
2278 | return ''; |
2279 | } |
2280 | $choices = $this->choices; |
2281 | $default = $this->get_defaultsetting(); |
2282 | if (is_null($default)) { |
2283 | $default = array(); |
2284 | } |
2285 | if (is_null($data)) { |
d7933a55 |
2286 | $data = array(); |
2287 | } |
220a90c5 |
2288 | |
2289 | $defaults = array(); |
a7ad48fd |
2290 | $return = '<div class="form-select"><input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected |
2291 | $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="10" multiple="multiple">'; |
220a90c5 |
2292 | foreach ($this->choices as $key => $description) { |
2293 | if (in_array($key, $data)) { |
2294 | $selected = 'selected="selected"'; |
2295 | } else { |
2296 | $selected = ''; |
2297 | } |
2298 | if (in_array($key, $default)) { |
2299 | $defaults[] = $description; |
6e4dc10f |
2300 | } |
220a90c5 |
2301 | |
2302 | $return .= '<option value="'.s($key).'" '.$selected.'>'.$description.'</option>'; |
2303 | } |
2304 | |
2305 | if (!empty($defaults)) { |
2306 | $defaultinfo = '<div class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', implode(', ', $defaults)).'</div>'; |
2307 | } else { |
2308 | $defaultinfo = ''; |
6e4dc10f |
2309 | } |
eef868d1 |
2310 | |
220a90c5 |
2311 | $return .= '</select>'.$defaultinfo.'</div>'; |
2312 | return format_admin_setting($this, $this->visiblename, $return, $this->description); |
6e4dc10f |
2313 | } |
220a90c5 |
2314 | } |
eef868d1 |
2315 | |
220a90c5 |
2316 | /** |
2317 | * Time selector |
2318 | * this is a liiitle bit messy. we're using two selects, but we're returning |
2319 | * them as an array named after $name (so we only use $name2 internally for the setting) |
2320 | */ |
2321 | class admin_setting_configtime extends admin_setting { |
2322 | var $name2; |
2323 | |
2324 | /** |
2325 | * Constructor |
2326 | * @param string $hoursname setting for hours |
2327 | * @param string $minutesname setting for hours |
2328 | * @param string $visiblename localised |
2329 | * @param string $description long localised info |
2330 | * @param array $defaultsetting array representing default time 'h'=>hours, 'm'=>minutes |
2331 | */ |
2332 | function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) { |
2333 | $this->name2 = $minutesname; |
2334 | parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting); |
2335 | } |
2336 | |
2337 | function get_setting() { |
2338 | $result1 = $this->config_read($this->name); |
2339 | $result2 = $this->config_read($this->name2); |
2340 | if (is_null($result1) or is_null($result2)) { |
2341 | return NULL; |
2342 | } |
2343 | |
2344 | return array('h' => $result1, 'm' => $result2); |
2345 | } |
2346 | |
2347 | function write_setting($data) { |
2348 | if (!is_array($data)) { |
2349 | return ''; |
2350 | } |
2351 | |
2352 | $result = $this->config_write($this->name, (int)$data['h']) && $this->config_write($this->name2, (int)$data['m']); |
2353 | return ($result ? '' : get_string('errorsetting', 'admin')); |
2354 | } |
2355 | |
2356 | function output_html($data) { |
2357 | $default = $this->get_defaultsetting(); |
2358 | |
2359 | if (is_array($default)) { |
2360 | $defaultinfo = '<span class="defaultinfo">'.get_string('defaultsettinginfo', 'admin', $default['h'].':'.$default['m']).'</span>'; |
cc73de71 |
2361 | } else { |
220a90c5 |
2362 | $defaultinfo = ''; |
6e4dc10f |
2363 | } |
220a90c5 |
2364 | |
2365 | $return = '<div class="form-time">'. |
2366 | '<select id="'.$this->get_id().'h" name="'.$this->get_full_name().'[h]">'; |
2367 | for ($i = 0; $i < 24; $i++) { |
2368 | $return .= '<option value="'.$i.'"'.($i == $data['h'] ? ' selected="selected"' : '').'>'.$i.'</option>'; |
6e4dc10f |
2369 | } |
220a90c5 |
2370 | $return .= '</select>:<select id="'.$this->get_id().'m" name="'.$this->get_full_name().'[m]">'; |
2371 | for ($i = 0; $i < 60; $i += 5) { |
2372 | $return .= '<option value="'.$i.'"'.($i == $data['m'] ? ' selected="selected"' : '').'>'.$i.'</option>'; |
2373 | } |
2374 | $return .= '</select>'.$defaultinfo.'</div>'; |
2375 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
6e4dc10f |
2376 | } |
2377 | |
2378 | } |
2379 | |
220a90c5 |
2380 | /** |
2381 | * Special checkbox for calendar - resets SESSION vars. |
2382 | */ |
6e4dc10f |
2383 | class admin_setting_special_adminseesall extends admin_setting_configcheckbox { |
6e4dc10f |
2384 | function admin_setting_special_adminseesall() { |
220a90c5 |
2385 | parent::admin_setting_configcheckbox('calendar_adminseesall', get_string('adminseesall', 'admin'), |
2386 | get_string('helpadminseesall', 'admin'), '0'); |
6e4dc10f |
2387 | } |
2388 | |
2389 | function write_setting($data) { |
2390 | global $SESSION; |
2391 | unset($SESSION->cal_courses_shown); |
220a90c5 |
2392 | return parent::write_setting($data); |
6e4dc10f |
2393 | } |
2394 | } |
2395 | |
392e7363 |
2396 | /** |
2397 | * Special select for settings that are altered in setup.php and can not be altered on the fly |
2398 | */ |
2399 | class admin_setting_special_selectsetup extends admin_setting_configselect { |
2400 | function get_setting() { |
2401 | // read directly from db! |
2402 | return get_config(NULL, $this->name); |
2403 | } |
2404 | |
2405 | function write_setting($data) { |
2406 | global $CFG; |
2407 | // do not change active CFG setting! |
2408 | $current = $CFG->{$this->name}; |
2409 | $result = parent::write_setting($data); |
2410 | $CFG->{$this->name} = $current; |
2411 | return $result; |
2412 | } |
2413 | } |
2414 | |
220a90c5 |
2415 | /** |
2416 | * Special select for frontpage - stores data in course table |
2417 | */ |
6e4dc10f |
2418 | class admin_setting_sitesetselect extends admin_setting_configselect { |
6e4dc10f |
2419 | function get_setting() { |
2420 | $site = get_site(); |
220a90c5 |
2421 | return $site->{$this->name}; |
6e4dc10f |
2422 | } |
eef868d1 |
2423 | |
6e4dc10f |
2424 | function write_setting($data) { |
2425 | if (!in_array($data, array_keys($this->choices))) { |
220a90c5 |
2426 | return get_string('errorsetting', 'admin'); |
6e4dc10f |
2427 | } |
2428 | $record = new stdClass(); |
220a90c5 |
2429 | $record->id = SITEID; |
2430 | $temp = $this->name; |
2431 | $record->$temp = $data; |
6e4dc10f |
2432 | $record->timemodified = time(); |
220a90c5 |
2433 | return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2434 | } |
6e4dc10f |
2435 | } |
2436 | |
220a90c5 |
2437 | /** |
2438 | * Special select - lists on the frontpage - hacky |
2439 | */ |
2440 | class admin_setting_courselist_frontpage extends admin_setting { |
2441 | var $choices; |
6e4dc10f |
2442 | |
e0f6e995 |
2443 | function admin_setting_courselist_frontpage($loggedin) { |
6e4dc10f |
2444 | global $CFG; |
220a90c5 |
2445 | require_once($CFG->dirroot.'/course/lib.php'); |
2446 | $name = 'frontpage'.($loggedin ? 'loggedin' : ''); |
2447 | $visiblename = get_string('frontpage'.($loggedin ? 'loggedin' : ''),'admin'); |
2448 | $description = get_string('configfrontpage'.($loggedin ? 'loggedin' : ''),'admin'); |
2449 | $defaults = array(FRONTPAGECOURSELIST); |
2450 | parent::admin_setting($name, $visiblename, $description, $defaults); |
6e4dc10f |
2451 | } |
eef868d1 |
2452 | |
220a90c5 |
2453 | function load_choices() { |
2454 | if (is_array($this->choices)) { |
2455 | return true; |
2456 | } |
2457 | $this->choices = array(FRONTPAGENEWS => get_string('frontpagenews'), |
2458 | FRONTPAGECOURSELIST => get_string('frontpagecourselist'), |
2459 | FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'), |
2460 | FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'), |
2461 | 'none' => get_string('none')); |
2462 | if ($this->name == 'frontpage' and count_records('course') > FRONTPAGECOURSELIMIT) { |
2463 | unset($this->choices[FRONTPAGECOURSELIST]); |
2464 | } |
2465 | return true; |
2466 | } |
6e4dc10f |
2467 | function get_setting() { |
220a90c5 |
2468 | $result = $this->config_read($this->name); |
2469 | if (is_null($result)) { |
2470 | return NULL; |
2471 | } |
2472 | if ($result === '') { |
2473 | return array(); |
2474 | } |
2475 | return explode(',', $result); |
6e4dc10f |
2476 | } |
eef868d1 |
2477 | |
6e4dc10f |
2478 | function write_setting($data) { |
220a90c5 |
2479 | if (!is_array($data)) { |
2480 | return ''; |
6e4dc10f |
2481 | } |
220a90c5 |
2482 | $this->load_choices(); |
2483 | $save = array(); |
6e4dc10f |
2484 | foreach($data as $datum) { |
220a90c5 |
2485 | if ($datum == 'none' or !array_key_exists($datum, $this->choices)) { |
2486 | continue; |
6e4dc10f |
2487 | } |
220a90c5 |
2488 | $save[$datum] = $datum; // no duplicates |
6e4dc10f |
2489 | } |
220a90c5 |
2490 | return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2491 | } |
eef868d1 |
2492 | |
220a90c5 |
2493 | function output_html($data) { |
2494 | $this->load_choices(); |
2495 | $currentsetting = array(); |
2496 | foreach ($data as $key) { |
2497 | if ($key != 'none' and array_key_exists($key, $this->choices)) { |
2498 | $currentsetting[] = $key; // already selected first |
6e4dc10f |
2499 | } |
2500 | } |
220a90c5 |
2501 | |
0a7e84c3 |
2502 | $return = '<div class="form-group">'; |
6e4dc10f |
2503 | for ($i = 0; $i < count($this->choices) - 1; $i++) { |
220a90c5 |
2504 | if (!array_key_exists($i, $currentsetting)) { |
2505 | $currentsetting[$i] = 'none'; //none |
2506 | } |
2507 | $return .='<select class="form-select" id="'.$this->get_id().$i.'" name="'.$this->get_full_name().'[]">'; |
6e4dc10f |
2508 | foreach ($this->choices as $key => $value) { |
220a90c5 |
2509 | $return .= '<option value="'.$key.'"'.("$key" == $currentsetting[$i] ? ' selected="selected"' : '').'>'.$value.'</option>'; |
6e4dc10f |
2510 | } |
2511 | $return .= '</select>'; |
2512 | if ($i !== count($this->choices) - 2) { |
975211bb |
2513 | $return .= '<br />'; |
6e4dc10f |
2514 | } |
2515 | } |
0a7e84c3 |
2516 | $return .= '</div>'; |
2517 | |
220a90c5 |
2518 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
6e4dc10f |
2519 | } |
2520 | } |
2521 | |
220a90c5 |
2522 | /** |
2523 | * Special checkbox for frontpage - stores data in course table |
2524 | */ |
6e4dc10f |
2525 | class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox { |
6e4dc10f |
2526 | function get_setting() { |
2527 | $site = get_site(); |
8e5da17a |
2528 | return $site->{$this->name}; |
6e4dc10f |
2529 | } |
eef868d1 |
2530 | |
6e4dc10f |
2531 | function write_setting($data) { |
220a90c5 |
2532 | $record = new object(); |
2533 | $record->id = SITEID; |
2534 | $record->{$this->name} = ($data == '1' ? 1 : 0); |
2535 | $record->timemodified = time(); |
2536 | return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2537 | } |
6e4dc10f |
2538 | } |
2539 | |
220a90c5 |
2540 | /** |
2541 | * Special text for frontpage - stores data in course table. |
2542 | * Empty string means not set here. Manual setting is required. |
2543 | */ |
6e4dc10f |
2544 | class admin_setting_sitesettext extends admin_setting_configtext { |
6e4dc10f |
2545 | function get_setting() { |
2546 | $site = get_site(); |
8e5da17a |
2547 | return $site->{$this->name} != '' ? $site->{$this->name} : NULL; |
6e4dc10f |
2548 | } |
90cfbd0a |
2549 | |
b89639f9 |
2550 | function validate($data) { |
e33fbf87 |
2551 | $cleaned = stripslashes(clean_param(addslashes($data), PARAM_MULTILANG)); |
2552 | if ($cleaned === '') { |
2553 | return get_string('required'); |
2554 | } |
2555 | if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison |
2556 | return true; |
2557 | } else { |
2558 | return get_string('validateerror', 'admin'); |
b89639f9 |
2559 | } |
b89639f9 |
2560 | } |
2561 | |
6e4dc10f |
2562 | function write_setting($data) { |
b89639f9 |
2563 | $data = trim($data); |
e33fbf87 |
2564 | $validated = $this->validate($data); |
2565 | if ($validated !== true) { |
2566 | return $validated; |
90cfbd0a |
2567 | } |
eef868d1 |
2568 | |
220a90c5 |
2569 | $record = new object(); |
2570 | $record->id = SITEID; |
87fa8a17 |
2571 | $record->{$this->name} = addslashes($data); |
220a90c5 |
2572 | $record->timemodified = time(); |
2573 | return (update_record('course', $record) ? '' : get_string('dbupdatefailed', 'error')); |
6e4dc10f |
2574 | } |
6e4dc10f |
2575 | } |
2576 | |
220a90c5 |
2577 | /** |
2578 | * Special text editor for site description. |
2579 | */ |
6e4dc10f |
2580 | class admin_setting_special_frontpagedesc extends admin_setting { |
6e4dc10f |
2581 | function admin_setting_special_frontpagedesc() { |
220a90c5 |
2582 | parent::admin_setting('summary', get_string('frontpagedescription'), get_string('frontpagedescriptionhelp'), NULL); |
6e4dc10f |
2583 | } |
eef868d1 |
2584 | |
6e4dc10f |
2585 | function get_setting() { |
6e4dc10f |
2586 | $site = get_site(); |
c626c2f4 |
2587 | return $site->{$this->name}; |
2588 | } |
eef868d1 |
2589 | |
6e4dc10f |
2590 | function write_setting($data) { |
c626c2f4 |
2591 | $record = new object(); |
220a90c5 |
2592 | $record->id = SITEID; |
c626c2f4 |
2593 | $record->{$this->name} = addslashes($data); |
2594 | $record->timemodified = time(); |
220a90c5 |
2595 | return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2596 | } |
2597 | |
220a90c5 |
2598 | function output_html($data) { |
2599 | global $CFG; |
6e4dc10f |
2600 | |
220a90c5 |
2601 | $CFG->adminusehtmleditor = can_use_html_editor(); |
2602 | $return = '<div class="form-htmlarea">'.print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, $this->get_full_name(), $data, 0, true).'</div>'; |
2603 | |
2604 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
2605 | } |
2606 | } |
6e4dc10f |
2607 | |
2608 | class admin_setting_special_editorfontlist extends admin_setting { |
2609 | |
2610 | var $items; |
2611 | |
2612 | function admin_setting_special_editorfontlist() { |
2613 | global $CFG; |
2614 | $name = 'editorfontlist'; |
2615 | $visiblename = get_string('editorfontlist', 'admin'); |
2616 | $description = get_string('configeditorfontlist', 'admin'); |
6e4dc10f |
2617 | $defaults = array('k0' => 'Trebuchet', |
2618 | 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif', |
2619 | 'k1' => 'Arial', |
2620 | 'v1' => 'arial,helvetica,sans-serif', |
2621 | 'k2' => 'Courier New', |
2622 | 'v2' => 'courier new,courier,monospace', |
2623 | 'k3' => 'Georgia', |
2624 | 'v3' => 'georgia,times new roman,times,serif', |
2625 | 'k4' => 'Tahoma', |
2626 | 'v4' => 'tahoma,arial,helvetica,sans-serif', |
2627 | 'k5' => 'Times New Roman', |
2628 | 'v5' => 'times new roman,times,serif', |
2629 | 'k6' => 'Verdana', |
2630 | 'v6' => 'verdana,arial,helvetica,sans-serif', |
2631 | 'k7' => 'Impact', |
2632 | 'v7' => 'impact', |
2633 | 'k8' => 'Wingdings', |
2634 | 'v8' => 'wingdings'); |
2635 | parent::admin_setting($name, $visiblename, $description, $defaults); |
2636 | } |
eef868d1 |
2637 | |
6e4dc10f |
2638 | function get_setting() { |
cc73de71 |
2639 | global $CFG; |
220a90c5 |
2640 | $result = $this->config_read($this->name); |
2641 | if (is_null($result)) { |
cc73de71 |
2642 | return NULL; |
2643 | } |
220a90c5 |
2644 | $i = 0; |
2645 | $currentsetting = array(); |
2646 | $items = explode(';', $result); |
2647 | foreach ($items as $item) { |
2648 | $item = explode(':', $item); |
2649 | $currentsetting['k'.$i] = $item[0]; |
2650 | $currentsetting['v'.$i] = $item[1]; |
2651 | $i++; |
2652 | } |
2653 | return $currentsetting; |
6e4dc10f |
2654 | } |
eef868d1 |
2655 | |
6e4dc10f |
2656 | function write_setting($data) { |
eef868d1 |
2657 | |
6e4dc10f |
2658 | // there miiight be an easier way to do this :) |
2659 | // if this is changed, make sure the $defaults array above is modified so that this |
2660 | // function processes it correctly |
eef868d1 |
2661 | |
6e4dc10f |
2662 | $keys = array(); |
2663 | $values = array(); |
eef868d1 |
2664 | |
6e4dc10f |
2665 | foreach ($data as $key => $value) { |
2666 | if (substr($key,0,1) == 'k') { |
2667 | $keys[substr($key,1)] = $value; |
2668 | } elseif (substr($key,0,1) == 'v') { |
2669 | $values[substr($key,1)] = $value; |
2670 | } |
2671 | } |
eef868d1 |
2672 | |
220a90c5 |
2673 | $result = array(); |
6e4dc10f |
2674 | for ($i = 0; $i < count($keys); $i++) { |
2675 | if (($keys[$i] !== '') && ($values[$i] !== '')) { |
220a90c5 |
2676 | $result[] = clean_param($keys[$i],PARAM_NOTAGS).':'.clean_param($values[$i], PARAM_NOTAGS); |
6e4dc10f |
2677 | } |
2678 | } |
eef868d1 |
2679 | |
220a90c5 |
2680 | return ($this->config_write($this->name, implode(';', $result)) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2681 | } |
eef868d1 |
2682 | |
220a90c5 |
2683 | function output_html($data) { |
2684 | $fullname = $this->get_full_name(); |
1beed35f |
2685 | $return = '<div class="form-group">'; |
220a90c5 |
2686 | for ($i = 0; $i < count($data) / 2; $i++) { |
2687 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="'.$data['k'.$i].'" />'; |
6e4dc10f |
2688 | $return .= ' '; |
220a90c5 |
2689 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="'.$data['v'.$i].'" /><br />'; |
6e4dc10f |
2690 | } |
220a90c5 |
2691 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="" />'; |
6e4dc10f |
2692 | $return .= ' '; |
220a90c5 |
2693 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="" /><br />'; |
2694 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.($i + 1).']" value="" />'; |
6e4dc10f |
2695 | $return .= ' '; |
220a90c5 |
2696 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.($i + 1).']" value="" />'; |
1beed35f |
2697 | $return .= '</div>'; |
6153cf58 |
2698 | |
220a90c5 |
2699 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
6e4dc10f |
2700 | } |
eef868d1 |
2701 | |
6e4dc10f |
2702 | } |
2703 | |
93c61c18 |
2704 | class admin_setting_emoticons extends admin_setting { |
2705 | |
2706 | var $items; |
2707 | |
2708 | function admin_setting_emoticons() { |
2709 | global $CFG; |
2710 | $name = 'emoticons'; |
2711 | $visiblename = get_string('emoticons', 'admin'); |
2712 | $description = get_string('configemoticons', 'admin'); |
2713 | $defaults = array('k0' => ':-)', |
2714 | 'v0' => 'smiley', |
2715 | 'k1' => ':)', |
2716 | 'v1' => 'smiley', |
2717 | 'k2' => ':-D', |
2718 | 'v2' => 'biggrin', |
2719 | 'k3' => ';-)', |
2720 | 'v3' => 'wink', |
2721 | 'k4' => ':-/', |
2722 | 'v4' => 'mixed', |
2723 | 'k5' => 'V-.', |
2724 | 'v5' => 'thoughtful', |
2725 | 'k6' => ':-P', |
2726 | 'v6' => 'tongueout', |
2727 | 'k7' => 'B-)', |
2728 | 'v7' => 'cool', |
2729 | 'k8' => '^-)', |
2730 | 'v8' => 'approve', |
2731 | 'k9' => '8-)', |
2732 | 'v9' => 'wideeyes', |
2733 | 'k10' => ':o)', |
2734 | 'v10' => 'clown', |
2735 | 'k11' => ':-(', |
2736 | 'v11' => 'sad', |
2737 | 'k12' => ':(', |
2738 | 'v12' => 'sad', |
2739 | 'k13' => '8-.', |
2740 | 'v13' => 'shy', |
2741 | 'k14' => ':-I', |
2742 | 'v14' => 'blush', |
2743 | 'k15' => ':-X', |
2744 | 'v15' => 'kiss', |
2745 | 'k16' => '8-o', |
2746 | 'v16' => 'surprise', |
2747 | 'k17' => 'P-|', |
2748 | 'v17' => 'blackeye', |
2749 | 'k18' => '8-[', |
2750 | 'v18' => 'angry', |
2751 | 'k19' => 'xx-P', |
2752 | 'v19' => 'dead', |
2753 | 'k20' => '|-.', |
2754 | 'v20' => 'sleepy', |
2755 | 'k21' => '}-]', |
2756 | 'v21' => 'evil', |
2757 | 'k22' => '(h)', |
2758 | 'v22' => 'heart', |
2759 | 'k23' => '(heart)', |
2760 | 'v23' => 'heart', |
2761 | 'k24' => '(y)', |
2762 | 'v24' => 'yes', |
2763 | 'k25' => '(n)', |
2764 | 'v25' => 'no', |
2765 | 'k26' => '(martin)', |
2766 | 'v26' => 'martin', |
2767 | 'k27' => '( )', |
2768 | 'v27' => 'egg'); |
2769 | parent::admin_setting($name, $visiblename, $description, $defaults); |
2770 | } |
2771 | |
2772 | function get_setting() { |
2773 | global $CFG; |
220a90c5 |
2774 | $result = $this->config_read($this->name); |
2775 | if (is_null($result)) { |
93c61c18 |
2776 | return NULL; |
2777 | } |
220a90c5 |
2778 | $i = 0; |
2779 | $currentsetting = array(); |
2780 | $items = explode('{;}', $result); |
2781 | foreach ($items as $item) { |
2782 | $item = explode('{:}', $item); |
2783 | $currentsetting['k'.$i] = $item[0]; |
2784 | $currentsetting['v'.$i] = $item[1]; |
2785 | $i++; |
2786 | } |
2787 | return $currentsetting; |
93c61c18 |
2788 | } |
2789 | |
2790 | function write_setting($data) { |
2791 | |
2792 | // there miiight be an easier way to do this :) |
2793 | // if this is changed, make sure the $defaults array above is modified so that this |
2794 | // function processes it correctly |
2795 | |
2796 | $keys = array(); |
2797 | $values = array(); |
2798 | |
2799 | foreach ($data as $key => $value) { |
2800 | if (substr($key,0,1) == 'k') { |
2801 | $keys[substr($key,1)] = $value; |
2802 | } elseif (substr($key,0,1) == 'v') { |
2803 | $values[substr($key,1)] = $value; |
2804 | } |
2805 | } |
2806 | |
220a90c5 |
2807 | $result = array(); |
93c61c18 |
2808 | for ($i = 0; $i < count($keys); $i++) { |
2809 | if (($keys[$i] !== '') && ($values[$i] !== '')) { |
220a90c5 |
2810 | $result[] = clean_param($keys[$i],PARAM_NOTAGS).'{:}'.clean_param($values[$i], PARAM_NOTAGS); |
93c61c18 |
2811 | } |
2812 | } |
2813 | |
220a90c5 |
2814 | return ($this->config_write($this->name, implode('{;}', $result)) ? '' : get_string('errorsetting', 'admin').$this->visiblename.'<br />'); |
93c61c18 |
2815 | } |
2816 | |
220a90c5 |
2817 | function output_html($data) { |
2818 | $fullname = $this->get_full_name(); |
93c61c18 |
2819 | $return = '<div class="form-group">'; |
220a90c5 |
2820 | for ($i = 0; $i < count($data) / 2; $i++) { |
2821 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="'.$data['k'.$i].'" />'; |
93c61c18 |
2822 | $return .= ' '; |
220a90c5 |
2823 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="'.$data['v'.$i].'" /><br />'; |
93c61c18 |
2824 | } |
220a90c5 |
2825 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="" />'; |
93c61c18 |
2826 | $return .= ' '; |
220a90c5 |
2827 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="" /><br />'; |
2828 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.($i + 1).']" value="" />'; |
93c61c18 |
2829 | $return .= ' '; |
220a90c5 |
2830 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.($i + 1).']" value="" />'; |
93c61c18 |
2831 | $return .= '</div>'; |
2832 | |
220a90c5 |
2833 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
93c61c18 |
2834 | } |
2835 | |
2836 | } |
2837 | |
220a90c5 |
2838 | /** |
2839 | * Setting for spellchecker language selection. |
2840 | */ |
6e4dc10f |
2841 | class admin_setting_special_editordictionary extends admin_setting_configselect { |
2842 | |
2843 | function admin_setting_special_editordictionary() { |
2844 | $name = 'editordictionary'; |
2845 | $visiblename = get_string('editordictionary','admin'); |
2846 | $description = get_string('configeditordictionary', 'admin'); |
220a90c5 |
2847 | parent::admin_setting_configselect($name, $visiblename, $description, '', NULL); |
6e4dc10f |
2848 | } |
2849 | |
220a90c5 |
2850 | function load_choices() { |
2851 | // function borrowed from the old moodle/admin/editor.php, slightly modified |
2852 | // Get all installed dictionaries in the system |
2853 | if (is_array($this->choices)) { |
2854 | return true; |
2855 | } |
2856 | |
2857 | $this->choices = array(); |
6e4dc10f |
2858 | |
2859 | global $CFG; |
eef868d1 |
2860 | |
6e4dc10f |
2861 | clearstatcache(); |
2862 | |
2863 | // If aspellpath isn't set don't even bother ;-) |
2864 | if (empty($CFG->aspellpath)) { |
220a90c5 |
2865 | $this->choices['error'] = 'Empty aspell path!'; |
2866 | return true; |
6e4dc10f |
2867 | } |
2868 | |
2869 | // Do we have access to popen function? |
2870 | if (!function_exists('popen')) { |
220a90c5 |
2871 | $this->choices['error'] = 'Popen function disabled!'; |
2872 | return true; |
6e4dc10f |
2873 | } |
eef868d1 |
2874 | |
6e4dc10f |
2875 | $cmd = $CFG->aspellpath; |
2876 | $output = ''; |
2877 | $dictionaries = array(); |
6e4dc10f |
2878 | |
220a90c5 |
2879 | if(!($handle = @popen(escapeshellarg($cmd).' dump dicts', 'r'))) { |
2880 | $this->choices['error'] = 'Couldn\'t create handle!'; |
6e4dc10f |
2881 | } |
2882 | |
2883 | while(!feof($handle)) { |
2884 | $output .= fread($handle, 1024); |
2885 | } |
2886 | @pclose($handle); |
2887 | |
2888 | $dictionaries = explode(chr(10), $output); |
220a90c5 |
2889 | foreach ($dictionaries as $dict) { |
2890 | if (empty($dict)) { |
2891 | continue; |
6e4dc10f |
2892 | } |
220a90c5 |
2893 | $this->choices[$dict] = $dict; |
6e4dc10f |
2894 | } |
2895 | |
220a90c5 |
2896 | if (empty($this->choices)) { |
2897 | $this->choices['error'] = 'Error! Check your aspell installation!'; |
6e4dc10f |
2898 | } |
220a90c5 |
2899 | return true; |
6e4dc10f |
2900 | } |
6e4dc10f |
2901 | } |
2902 | |
2903 | |
2904 | class admin_setting_special_editorhidebuttons extends admin_setting { |
6e4dc10f |
2905 | var $items; |
2906 | |
2907 | function admin_setting_special_editorhidebuttons() { |
220a90c5 |
2908 | parent::admin_setting('editorhidebuttons', get_string('editorhidebuttons', 'admin'), |
2909 | get_string('confeditorhidebuttons', 'admin'), array()); |
6e4dc10f |
2910 | // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead |
2911 | $this->items = array('fontname' => '', |
2912 | 'fontsize' => '', |
2913 | 'formatblock' => '', |
2914 | 'bold' => 'ed_format_bold.gif', |
2915 | 'italic' => 'ed_format_italic.gif', |
2916 | 'underline' => 'ed_format_underline.gif', |
2917 | 'strikethrough' => 'ed_format_strike.gif', |
2918 | 'subscript' => 'ed_format_sub.gif', |
2919 | 'superscript' => 'ed_format_sup.gif', |
2920 | 'copy' => 'ed_copy.gif', |
2921 | 'cut' => 'ed_cut.gif', |
2922 | 'paste' => 'ed_paste.gif', |
2923 | 'clean' => 'ed_wordclean.gif', |
2924 | 'undo' => 'ed_undo.gif', |
2925 | 'redo' => 'ed_redo.gif', |
2926 | 'justifyleft' => 'ed_align_left.gif', |
2927 | 'justifycenter' => 'ed_align_center.gif', |
2928 | 'justifyright' => 'ed_align_right.gif', |
2929 | 'justifyfull' => 'ed_align_justify.gif', |
2930 | 'lefttoright' => 'ed_left_to_right.gif', |
2931 | 'righttoleft' => 'ed_right_to_left.gif', |
2932 | 'insertorderedlist' => 'ed_list_num.gif', |
2933 | 'insertunorderedlist' => 'ed_list_bullet.gif', |
2934 | 'outdent' => 'ed_indent_less.gif', |
2935 | 'indent' => 'ed_indent_more.gif', |
2936 | 'forecolor' => 'ed_color_fg.gif', |
2937 | 'hilitecolor' => 'ed_color_bg.gif', |
2938 | 'inserthorizontalrule' => 'ed_hr.gif', |
2939 | 'createanchor' => 'ed_anchor.gif', |
2940 | 'createlink' => 'ed_link.gif', |
2941 | 'unlink' => 'ed_unlink.gif', |
2942 | 'insertimage' => 'ed_image.gif', |
2943 | 'inserttable' => 'insert_table.gif', |
2944 | 'insertsmile' => 'em.icon.smile.gif', |
2945 | 'insertchar' => 'icon_ins_char.gif', |
2946 | 'spellcheck' => 'spell-check.gif', |
2947 | 'htmlmode' => 'ed_html.gif', |
2948 | 'popupeditor' => 'fullscreen_maximize.gif', |
2949 | 'search_replace' => 'ed_replace.gif'); |
2950 | } |
2951 | |
2952 | function get_setting() { |
220a90c5 |
2953 | $result = $this->config_read($this->name); |
2954 | if (is_null($result)) { |
2955 | return NULL; |
2956 | } |
2957 | if ($result === '') { |
2958 | return array(); |
2959 | } |
2960 | return explode(',', $result); |
6e4dc10f |
2961 | } |
2962 | |
2963 | function write_setting($data) { |
220a90c5 |
2964 | if (!is_array($data)) { |
2965 | return ''; // ignore it |
2966 | } |
2967 | unset($data['xxxxx']); |
6e4dc10f |
2968 | $result = array(); |
220a90c5 |
2969 | |
6e4dc10f |
2970 | foreach ($data as $key => $value) { |
2971 | if (!in_array($key, array_keys($this->items))) { |
220a90c5 |
2972 | return get_string('errorsetting', 'admin'); |
6e4dc10f |
2973 | } |
2974 | if ($value == '1') { |
2975 | $result[] = $key; |
2976 | } |
2977 | } |
220a90c5 |
2978 | return ($this->config_write($this->name, implode(' ', $result)) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
2979 | } |
2980 | |
220a90c5 |
2981 | function output_html($data) { |
eef868d1 |
2982 | |
6e4dc10f |
2983 | global $CFG; |
eef868d1 |
2984 | |
6e4dc10f |
2985 | // checkboxes with input name="$this->name[$key]" value="1" |
2986 | // we do 15 fields per column |
eef868d1 |
2987 | |
1beed35f |
2988 | $return = '<div class="form-group">'; |
2989 | $return .= '<table><tr><td valign="top" align="right">'; |
220a90c5 |
2990 | $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected |
eef868d1 |
2991 | |
6e4dc10f |
2992 | $count = 0; |
eef868d1 |
2993 | |
6e4dc10f |
2994 | foreach($this->items as $key => $value) { |
8ddbd7a6 |
2995 | if ($count % 15 == 0 and $count != 0) { |
2996 | $return .= '</td><td valign="top" align="right">'; |
6e4dc10f |
2997 | } |
eef868d1 |
2998 | |
220a90c5 |
2999 | $return .= ($value == '' ? get_string($key,'editor') : '<img width="18" height="18" src="'.$CFG->wwwroot.'/lib/editor/htmlarea/images/'.$value.'" alt="'.get_string($key,'editor').'" title="'.get_string($key,'editor').'" />').' '; |
3000 | $return .= '<input type="checkbox" class="form-checkbox" value="1" id="'.$this->get_id().$key.'" name="'.$this->get_full_name().'['.$key.']"'.(in_array($key,$data) ? ' checked="checked"' : '').' /> '; |
6e4dc10f |
3001 | $count++; |
3002 | if ($count % 15 != 0) { |
3003 | $return .= '<br /><br />'; |
3004 | } |
3005 | } |
eef868d1 |
3006 | |
3007 | $return .= '</td></tr>'; |
6e4dc10f |
3008 | $return .= '</table>'; |
1beed35f |
3009 | $return .= '</div>'; |
6e4dc10f |
3010 | |
220a90c5 |
3011 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
6e4dc10f |
3012 | } |
6e4dc10f |
3013 | } |
3014 | |
220a90c5 |
3015 | /** |
3016 | * Special setting for limiting of the list of available languages. |
3017 | */ |
4642650f |
3018 | class admin_setting_langlist extends admin_setting_configtext { |
3019 | function admin_setting_langlist() { |
3020 | parent::admin_setting_configtext('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS); |
3021 | } |
3022 | |
3023 | function write_setting($data) { |
3024 | $return = parent::write_setting($data); |
3025 | get_list_of_languages(true);//refresh the list |
3026 | return $return; |
3027 | } |
3028 | } |
3029 | |
220a90c5 |
3030 | /** |
3031 | * Course category selection |
3032 | */ |
3033 | class admin_settings_coursecat_select extends admin_setting_configselect { |
3034 | function admin_settings_coursecat_select($name, $visiblename, $description, $defaultsetting) { |
3035 | parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, NULL); |
3036 | } |
6153cf58 |
3037 | |
220a90c5 |
3038 | function load_choices() { |
3039 | global $CFG; |
3040 | require_once($CFG->dirroot.'/course/lib.php'); |
3041 | if (is_array($this->choices)) { |
3042 | return true; |
3043 | } |
3044 | $this->choices = make_categories_options(); |
3045 | return true; |
3046 | } |
3047 | } |
eef868d1 |
3048 | |
220a90c5 |
3049 | class admin_setting_special_backupdays extends admin_setting_configmulticheckbox2 { |
3050 | function admin_setting_special_backupdays() { |
3051 | parent::admin_setting_configmulticheckbox2('backup_sche_weekdays', get_string('schedule'), get_string('backupschedulehelp'), array(), NULL); |
3052 | $this->plugin = 'backup'; |
6e4dc10f |
3053 | } |
eef868d1 |
3054 | |
220a90c5 |
3055 | function load_choices() { |
3056 | if (is_array($this->choices)) { |
3057 | return true; |
3058 | } |
3059 | $this->choices = array(); |
3060 | $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'); |
3061 | foreach ($days as $day) { |
3062 | $this->choices[$day] = get_string($day, 'calendar'); |
6e4dc10f |
3063 | } |
220a90c5 |
3064 | return true; |
6e4dc10f |
3065 | } |
3066 | } |
3067 | |
220a90c5 |
3068 | /** |
3069 | * Special debug setting |
3070 | */ |
ee437bbc |
3071 | class admin_setting_special_debug extends admin_setting_configselect { |
6e4dc10f |
3072 | function admin_setting_special_debug() { |
220a90c5 |
3073 | parent::admin_setting_configselect('debug', get_string('debug', 'admin'), get_string('configdebug', 'admin'), DEBUG_NONE, NULL); |
6e4dc10f |
3074 | } |
3075 | |
220a90c5 |
3076 | function load_choices() { |
3077 | if (is_array($this->choices)) { |
3078 | return true; |
ee437bbc |
3079 | } |
220a90c5 |
3080 | $this->choices = array(DEBUG_NONE => get_string('debugnone', 'admin'), |
3081 | DEBUG_MINIMAL => get_string('debugminimal', 'admin'), |
3082 | DEBUG_NORMAL => get_string('debugnormal', 'admin'), |
3083 | DEBUG_ALL => get_string('debugall', 'admin'), |
3084 | DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')); |
3085 | return true; |
6e4dc10f |
3086 | } |
6e4dc10f |
3087 | } |
3088 | |
3089 | |
3090 | class admin_setting_special_calendar_weekend extends admin_setting { |
6e4dc10f |
3091 | function admin_setting_special_calendar_weekend() { |
3092 | $name = 'calendar_weekend'; |
3093 | $visiblename = get_string('calendar_weekend', 'admin'); |
3094 | $description = get_string('helpweekenddays', 'admin'); |
5eaa6aa0 |
3095 | $default = array ('0', '6'); // Saturdays and Sundays |
b91b1f92 |
3096 | parent::admin_setting($name, $visiblename, $description, $default); |
6e4dc10f |
3097 | } |
3098 | |
3099 | function get_setting() { |
220a90c5 |
3100 | $result = $this->config_read($this->name); |
3101 | if (is_null($result)) { |
3102 | return NULL; |
3103 | } |
3104 | if ($result === '') { |
3105 | return array(); |
3106 | } |
3107 | $settings = array(); |
3108 | for ($i=0; $i<7; $i++) { |
3109 | if ($result & (1 << $i)) { |
3110 | $setting[] = $i; |
3111 | } |
3112 | } |
3113 | return $setting; |
6e4dc10f |
3114 | } |
eef868d1 |
3115 | |
6e4dc10f |
3116 | function write_setting($data) { |
220a90c5 |
3117 | if (!is_array($data)) { |
3118 | return ''; |
3119 | } |
3120 | unset($data['xxxxx']); |
4af8d5d3 |
3121 | $result = 0; |
220a90c5 |
3122 | foreach($data as $index) { |
3123 | $result |= 1 << $index; |
6e4dc10f |
3124 | } |
220a90c5 |
3125 | return ($this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f |
3126 | } |
eef868d1 |
3127 | |
220a90c5 |
3128 | function output_html($data) { |
4af8d5d3 |
3129 | // The order matters very much because of the implied numeric keys |
3130 | $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'); |
3131 | $return = '<table><thead><tr>'; |
220a90c5 |
3132 | $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected |
4af8d5d3 |
3133 | foreach($days as $index => $day) { |
220a90c5 |
3134 | $return .= '<td><label for="'.$this->get_id().$index.'">'.get_string($day, 'calendar').'</label></td>'; |
4af8d5d3 |
3135 | } |
3136 | $return .= '</tr></thead><tbody><tr>'; |
3137 | foreach($days as $index => $day) { |
220a90c5 |
3138 | $return .= '<td><input type="checkbox" class="form-checkbox" id="'.$this->get_id().$index.'" name="'.$this->get_full_name().'[]" value="'.$index.'" '.(in_array("$index", $data) ? 'checked="checked"' : '').' /></td>'; |
4af8d5d3 |
3139 | } |
3140 | $return .= '</tr></tbody></table>'; |
6153cf58 |
3141 | |
220a90c5 |
3142 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false); |
eef868d1 |
3143 | |
6e4dc10f |
3144 | } |
6e4dc10f |
3145 | } |
3146 | |
220a90c5 |
3147 | |
3d5c00b3 |
3148 | /** |
220a90c5 |
3149 | * Graded roles in gradebook |
5a412dbf |
3150 | */ |
220a90c5 |
3151 | class admin_setting_special_gradebookroles extends admin_setting_configmulticheckbox { |
0909d44e |
3152 | function admin_setting_special_gradebookroles() { |
220a90c5 |
3153 | parent::admin_setting_configmulticheckbox('gradebookroles', get_string('gradebookroles', 'admin'), |
3154 | get_string('configgradebookroles', 'admin'), NULL, NULL); |
5a412dbf |
3155 | } |
3156 | |
220a90c5 |
3157 | function load_choices() { |
5a412dbf |
3158 | global $CFG; |
220a90c5 |
3159 | if (empty($CFG->rolesactive)) { |
3160 | return false; |
3161 | } |
3162 | if (is_array($this->choices)) { |
3163 | return true; |
3164 | } |
3165 | if ($roles = get_records('role')) { |
3166 | $this->choices = array(); |
3167 | foreach($roles as $role) { |
3168 | $this->choices[$role->id] = format_string($role->name); |
60f7d402 |
3169 | } |
220a90c5 |
3170 | return true; |
5a412dbf |
3171 | } else { |
220a90c5 |
3172 | return false; |
5a412dbf |
3173 | } |
3174 | } |
3175 | |
220a90c5 |
3176 | function get_defaultsetting() { |
3177 | global $CFG; |
3178 | if (empty($CFG->rolesactive)) { |
3179 | return NULL; |
3180 | } |
3181 | $result = array(); |
3182 | if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { |
3183 | foreach ($studentroles as $studentrole) { |
3184 | $result[$studentrole->id] = '1'; |
5a412dbf |
3185 | } |
73afaf5b |
3186 | } |
220a90c5 |
3187 | return $result; |
5a412dbf |
3188 | } |
220a90c5 |
3189 | } |
5a412dbf |
3190 | |
220a90c5 |
3191 | /** |
3192 | * Which roles to show on course decription page |
3193 | */ |
3194 | class admin_setting_special_coursemanager extends admin_setting_configmulticheckbox { |
3195 | function admin_setting_special_coursemanager() { |
3196 | parent::admin_setting_configmulticheckbox('coursemanager', get_string('coursemanager', 'admin'), |
3197 | get_string('configcoursemanager', 'admin'), NULL, NULL); |
3198 | } |
5a412dbf |
3199 | |
220a90c5 |
3200 | function load_choices() { |
3201 | if (is_array($this->choices)) { |
3202 | return true; |
5a412dbf |
3203 | } |
5a412dbf |
3204 | if ($roles = get_records('role')) { |
220a90c5 |
3205 | $this->choices = array(); |
3206 | foreach($roles as $role) { |
3207 | $this->choices[$role->id] = format_string($role->name); |
5a412dbf |
3208 | } |
220a90c5 |
3209 | return true; |
5a412dbf |
3210 | } |
220a90c5 |
3211 | return false; |
3212 | } |
73afaf5b |
3213 | |
220a90c5 |
3214 | function get_defaultsetting() { |
3215 | global $CFG; |
3216 | if (empty($CFG->rolesactive)) { |
3217 | return NULL; |
3218 | } |
3219 | $result = array(); |
3220 | if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) { |
3221 | foreach ($teacherroles as $teacherrole) { |
3222 | $result[$teacherrole->id] = '1'; |
3223 | } |
3224 | } |
3225 | return $result; |
3226 | } |
3227 | } |
5a412dbf |
3228 | |
220a90c5 |
3229 | /** |
3230 | * Primary grade export plugin - has state tracking. |
3231 | */ |
3232 | class admin_setting_special_gradeexport extends admin_setting_configmulticheckbox { |
3233 | function admin_setting_special_gradeexport() { |
3234 | parent::admin_setting_configmulticheckbox('gradeexport', get_string('gradeexport', 'admin'), |
3235 | get_string('configgradeexport', 'admin'), array(), NULL); |
5a412dbf |
3236 | } |
3237 | |
220a90c5 |
3238 | function load_choices() { |
3239 | if (is_array($this->choices)) { |
3240 | return true; |
3241 | } |
3242 | $this->choices = array(); |
3243 | |
3244 | if ($plugins = get_list_of_plugins('grade/export')) { |
3245 | foreach($plugins as $plugin) { |
3246 | $this->choices[$plugin] = get_string('modulename', 'gradeexport_'.$plugin); |
3247 | } |
3248 | } |
3249 | return true; |
3250 | } |
5a412dbf |
3251 | } |
6e4dc10f |
3252 | |
220a90c5 |
3253 | /** |
3254 | * Grade category settings |
d42c64ba |
3255 | */ |
220a90c5 |
3256 | class admin_setting_gradecat_combo extends admin_setting { |
d42c64ba |
3257 | |
220a90c5 |
3258 | var $choices; |
3259 | |
3260 | function admin_setting_gradecat_combo($name, $visiblename, $description, $defaultsetting, $choices) { |
3261 | $this->choices = $choices; |
3262 | parent::admin_setting($name, $visiblename, $description, $defaultsetting); |
d42c64ba |
3263 | } |
3264 | |
3265 | function get_setting() { |
d42c64ba |
3266 | global $CFG; |
220a90c5 |
3267 | |
3268 | $value = $this->config_read($this->name); |
3269 | $flag = $this->config_read($this->name.'_flag'); |
3270 | |
3271 | if (is_null($value) or is_null($flag)) { |
3272 | return NULL; |
d42c64ba |
3273 | } |
220a90c5 |
3274 | |
3275 | $flag = (int)$flag; |
3276 | $forced = (boolean)(1 & $flag); // first bit |
3277 | $adv = (boolean)(2 & $flag); // second bit |
3278 | |
3279 | return array('value' => $value, 'forced' => $forced, 'adv' => $adv); |
d42c64ba |
3280 | } |
3281 | |
3282 | function write_setting($data) { |
220a90c5 |
3283 | global $CFG; |
d42c64ba |
3284 | |
220a90c5 |
3285 | $value = $data['value']; |
3286 | $forced = empty($data['forced']) ? 0 : 1; |
3287 | $adv = empty($data['adv']) ? 0 : 2; |
3288 | $flag = ($forced | $adv); //bitwise or |
3289 | |
3290 | if (!in_array($value, array_keys($this->choices))) { |
3291 | return 'Error setting '; |
3292 | } |
3293 | |
3294 | $oldvalue = $this->config_read($this->name); |
3295 | $oldflag = (int)$this->config_read($this->name.'_flag'); |
3296 | $oldforced = (1 & $oldflag); // first bit |
3297 | |
3298 | $result1 = $this->config_write($this->name, $value); |
3299 | $result2 = $this->config_write($this->name.'_flag', $flag); |
3300 | |
3301 | // force regrade if needed |
3302 | if ($oldforced != $forced or ($forced and $value != $oldvalue)) { |
3303 | require_once($CFG->libdir.'/gradelib.php'); |
3304 | grade_category::updated_forced_settings(); |
3305 | } |
3306 | |
3307 | if ($result1 and $result2) { |
3308 | return ''; |
d42c64ba |
3309 | } else { |
220a90c5 |
3310 | return get_string('errorsetting', 'admin'); |
d42c64ba |
3311 | } |
3312 | } |
3313 | |
220a90c5 |
3314 | function output_html($data) { |
3315 | $value = $data['value']; |
3316 | $forced = !empty($data['forced']); |
3317 | $adv = !empty($data['adv']); |
d42c64ba |
3318 | |
220a90c5 |
3319 | $return = '<select class="form-select" id="'.$this->get_id().'" name="'.$this->get_full_name().'[value]">'; |
3320 | foreach ($this->choices as $key => $val) { |
3321 | // the string cast is needed because key may be integer - 0 is equal to most strings! |
3322 | $return .= '<option value="'.$key.'"'.((string)$key==$value ? ' selected="selected"' : '').'>'.$val.'</option>'; |
3323 | } |
3324 | $return .= '</select>'; |
3325 | $return .= '</label><input type="checkbox" class="form-checkbox" id="'.$this->get_id().'force" name="'.$this->get_full_name().'[forced]" value="1" '.($forced ? 'checked="checked"' : '').' />' |
3326 | .'<label for="'.$this->get_id().'force">'.get_string('force'); |
3327 | $return .= '</label><input type="checkbox" class="form-checkbox" id="'.$this->get_id().'adv" name="'.$this->get_full_name().'[adv]" value="1" '.($adv ? 'checked="checked"' : '').' />' |
3328 | .'<label for="'.$this->get_id().'adv">'.get_string('advanced'); |
3329 | |
3330 | return format_admin_setting($this, $this->visiblename, $return, $this->description); |
3331 | } |
3332 | } |
3333 | |
3334 | |
3335 | /** |
3336 | * Selection of grade report in user profiles |
3337 | */ |
3338 | class admin_setting_grade_profilereport extends admin_setting_configselect { |
3339 | function admin_setting_grade_profilereport() { |
3340 | parent::admin_setting_configselect('grade_profilereport', get_string('profilereport', 'grades'), get_string('configprofilereport', 'grades'), 'user', null); |
3341 | } |
3342 | |
3343 | function load_choices() { |
3344 | if (is_array($this->choices)) { |
3345 | return true; |
3346 | } |
3347 | $this->choices = array(); |
3348 | |
3349 | global $CFG; |
3350 | require_once($CFG->libdir.'/gradelib.php'); |
3351 | |
3352 | foreach (get_list_of_plugins('grade/report') as $plugin) { |
3353 | if (file_exists($CFG->dirroot.'/grade/report/'.$plugin.'/lib.php')) { |
3354 | require_once($CFG->dirroot.'/grade/report/'.$plugin.'/lib.php'); |
3355 | $functionname = 'grade_report_'.$plugin.'_profilereport'; |
3356 | if (function_exists($functionname)) { |
3357 | $this->choices[$plugin] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'/lang/'); |
d42c64ba |
3358 | } |
d42c64ba |
3359 | } |
d42c64ba |
3360 | } |
220a90c5 |
3361 | return true; |
d42c64ba |
3362 | } |
3363 | } |
3364 | |
220a90c5 |
3365 | /** |
3366 | * Special class for register auth selection |
76127f37 |
3367 | */ |
220a90c5 |
3368 | class admin_setting_special_registerauth extends admin_setting_configselect { |
3369 | function admin_setting_special_registerauth() { |
3370 | parent::admin_setting_configselect('registerauth', get_string('selfregistration', 'auth'), get_string('selfregistration_help', 'auth'), 'email', null); |
76127f37 |
3371 | } |
3372 | |
220a90c5 |
3373 | function get_defaultsettings() { |
3374 | $this->load_choices(); |
3375 | if (array_key_exists($this->defaultsetting, $this->choices)) { |
3376 | return $this->defaultsetting; |
3377 | } else { |
3378 | return ''; |
3379 | } |
3380 | } |
76127f37 |
3381 | |
220a90c5 |
3382 | function load_choices() { |
76127f37 |
3383 | global $CFG; |
220a90c5 |
3384 | |
3385 | if (is_array($this->choices)) { |
3386 | return true; |
3387 | } |
3388 | $this->choices = array(); |
3389 | $this->choices[''] = get_string('disable'); |
3390 | |
3391 | $authsenabled = get_enabled_auth_plugins(true); |
3392 | |
3393 | foreach ($authsenabled as $auth) { |
3394 | $authplugin = get_auth_plugin($auth); |
3395 | if (!$authplugin->can_signup()) { |
3396 | continue; |
76127f37 |
3397 | } |
220a90c5 |
3398 | // Get the auth title (from core or own auth lang files) |
3399 | $authtitle = get_string("auth_{$auth}title", "auth"); |
3400 | if ($authtitle == "[[auth_{$auth}title]]") { |
3401 | $authtitle = get_string("auth_{$auth}title", "auth_{$auth}"); |
3402 | } |
3403 | $this->choices[$auth] = $authtitle; |
76127f37 |
3404 | } |
220a90c5 |
3405 | return true; |
76127f37 |
3406 | } |
220a90c5 |
3407 | } |
76127f37 |
3408 | |
220a90c5 |
3409 | /** |
3410 | * Module manage page |
3411 | */ |
3412 | class admin_page_managemods extends admin_externalpage { |
3413 | function admin_page_managemods() { |
3414 | global $CFG; |
3415 | parent::admin_externalpage('managemodules', get_string('modsettings', 'admin'), "$CFG->wwwroot/$CFG->admin/modules.php"); |
3416 | } |
76127f37 |
3417 | |
220a90c5 |
3418 | function search($query) { |
3cea9c55 |
3419 | if ($result = parent::search($query)) { |
3420 | return $result; |
220a90c5 |
3421 | } |
3422 | |
3423 | $found = false; |
3424 | if ($modules = get_records('modules')) { |
3425 | $textlib = textlib_get_instance(); |
3426 | foreach ($modules as $module) { |
3427 | if (strpos($module->name, $query) !== false) { |
3428 | $found = true; |
3429 | break; |
3430 | } |
3431 | $strmodulename = get_string('modulename', $module->name); |
3432 | if (strpos($textlib->strtolower($strmodulename), $query) !== false) { |
3433 | $found = true; |
3434 | break; |
76127f37 |
3435 | } |
3436 | } |
220a90c5 |
3437 | } |
3438 | if ($found) { |
3439 | $result = new object(); |
3440 | $result->page = $this; |
3441 | $result->settings = array(); |
3442 | return array($this->name => $result); |
76127f37 |
3443 | } else { |
220a90c5 |
3444 | return array(); |
76127f37 |
3445 | } |
3446 | } |
220a90c5 |
|