3 * This file provides hooks from moodle core into custom code.
8 * If at all possible, the facilities provided here should not be used.
9 * Wherever possible, customisations should be written using one of the
10 * standard plug-in points like modules, blocks, auth plugins, themes, ...
12 * However, sometimes that is just not possible, because of the nature
13 * of the change you want to make. In which case the second best plan is
14 * to implement your feature in a generally useful way, which can
15 * be contributed back to the moodle project so that everyone benefits.
17 * But supposing you are forced to implement some nasty hack that only
18 * you will ever want, then the local folder is for you. The idea is that
19 * instead of scattering your changes throughout the code base, you
20 * put them all in a folder called 'local'. Then you won't have to
21 * deal with merging problems when you upgrade the rest of your moodle
28 * These are similar to the module interface, however, not all the the
29 * facilities that are available to modules are available to local code (yet).
32 * Local database customisations
33 * -----------------------------
35 * If your local customisations require changes to the database, use the files:
38 * local/db/{$CFG->dbtype}.php
40 * In the file version.php, set the variable $local_version to a versionstamp
41 * value like 2006030300.
43 * In the file {$CFG->dbtype}.php, implement the
44 * function local_upgrade($oldversion) to make the database changes.
46 * Note that you don't need to have the {$CFG->dbtype}.sql file. Instead,
47 * when your moodle instance is first installed, local_upgrade() will be called
48 * with $oldversion set to 0, so that all the updates run.
54 * To have your local customisations notified when a course is deleted,
59 * In there, implement the function local_delete_course($courseid). This
60 * function will then be called whenever the functions remove_course_contents()
61 * or delete_course() from moodlelib are called.
65 * This function checks to see whether local database customisations are up-to-date
66 * by comparing $CFG->local_version to the variable $local_version defined in
67 * local/version.php. If not, it looks for a function called 'local_upgrade'
68 * in a file called 'local/db/{$CFG->dbtype}.php', and if it's there calls it with the
69 * appropiate $oldversion parameter. Then it updates $CFG->local_version.
70 * On success it prints a continue link. On failure it prints an error.
73 * @uses $db to do something really evil with the debug setting that should probably be eliminated. TODO!
74 * @param string $continueto a URL passed to print_continue() if the local upgrades succeed.
76 function upgrade_local_db($continueto) {
80 // if we don't have code version or a db upgrade file, just return true, we're unneeded
81 if (!file_exists($CFG->dirroot
.'/local/version.php') ||
!file_exists($CFG->dirroot
.'/local/db/'.$CFG->dbtype
.'.php')) {
85 require_once ($CFG->dirroot
.'/local/version.php'); // Get code versions
87 if (empty($CFG->local_version
)) { // normally we'd install, but just replay all the upgrades.
88 $CFG->local_version
= 0;
91 if ($local_version > $CFG->local_version
) { // upgrade!
92 $strdatabaseupgrades = get_string('databaseupgrades');
93 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
95 require_once ($CFG->dirroot
.'/local/db/'. $CFG->dbtype
.'.php');
98 if (local_upgrade($CFG->local_version
)) {
100 if (set_config('local_version', $local_version)) {
101 notify(get_string('databasesuccess'), 'notifysuccess');
102 notify(get_string('databaseupgradelocal', '', $local_version));
103 print_continue($continueto);
106 error('Upgrade of local database customisations failed! (Could not update version in config table)');
110 error('Upgrade failed! See local/version.php');
113 } else if ($local_version < $CFG->local_version
) {
114 notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
119 * Notify local code that a course is being deleted.
120 * Look for a function local_delete_course() in a file called
121 * local/lib.php andn call it if it is there.
123 * @param int $courseid the course that is being deleted.
124 * @param bool $showfeedback Whether to display notifications on success.
125 * @return false if local_delete_course failed, or true if
126 * there was noting to do or local_delete_course succeeded.
128 function notify_local_delete_course($courseid, $showfeedback) {
130 $localfile = $CFG->dirroot
.'/local/lib.php';
131 if (file_exists($localfile)) {
132 require_once($localfile);
133 if (function_exists('local_delete_course')) {
134 if (local_delete_course($courseid)) {
136 notify(get_string('deleted') . ' local data');