MDL-15476
[moodle-linuxchix.git] / lib / locallib.php
blob0c38b615d52707d70c08f191c528ab5580f9be7e
1 <?php
2 /**
3 * This file provides hooks from moodle core into custom code.
5 * Important note
6 * --------------
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
22 * installation.
25 * Available hooks
26 * ===============
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:
37 * local/version.php
38 * local/db/upgrade.php
40 * In the file version.php, set the variable $local_version to a versionstamp
41 * value like 2006030300 (a concatenation of year, month, day, serial).
43 * In the file upgrade.php, implement the
44 * function xmldb_local_upgrade($oldversion) to make the database changes.
46 * Note that you don't need to have an install.xml file. Instead,
47 * when your moodle instance is first installed, xmldb_local_upgrade() will be called
48 * with $oldversion set to 0, so that all the updates run.
50 * Local capabilities
51 * ------------------
53 * If your local customisations require their own capabilities, use
55 * local/db/access.php
57 * You should create an array called $local_capabilities, which looks like:
59 * $local_capabilities = array(
60 * 'moodle/local:capability' => array(
61 * 'captype' => 'read',
62 * 'contextlevel' => CONTEXT_SYSTEM,
63 * ),
64 * );
66 * Note that for all local capabilities you add, you'll need to add language strings.
67 * Moodle will expect to find them in local/lang/en_utf8/local.php (eg for English)
68 * with a key (following the above example) of local:capability
69 * See the next section for local language support.
72 * Local language support
73 * ----------------------
75 * Moodle supports looking in the local/ directory for language files.
76 * You would need to create local/lang/en_utf8/local.php
77 * and then could call strings like get_string('key', 'local');
78 * Make sure you don't call the language file something that moodle already has one of,
79 * stick to local or $clientname)
82 * Local admin menu items
83 * ----------------------
85 * It is possible to add new items to the admin_tree block.
86 * To do this, create a file, local/settings.php
87 * which can access the $ADMIN variable directly and add things to it.
88 * You might do something like:
89 * $ADMIN->add('root', new admin_category($name, $title);
90 * $ADMIN->add('foo', new admin_externalpage($name, $title, $url, $cap);
93 * Course deletion
94 * ---------------
96 * To have your local customisations notified when a course is deleted,
97 * make a file called
99 * local/lib.php
101 * In there, implement the function local_delete_course($courseid). This
102 * function will then be called whenever the functions remove_course_contents()
103 * or delete_course() from moodlelib are called.
107 * This function checks to see whether local database customisations are up-to-date
108 * by comparing $CFG->local_version to the variable $local_version defined in
109 * local/version.php. If not, it looks for a function called 'xmldb_local_upgrade'
110 * in a file called 'local/db/upgrade.php', and if it's there calls it with the
111 * appropiate $oldversion parameter. Then it updates $CFG->local_version.
112 * On success it prints a continue link. On failure it prints an error.
114 * @uses $CFG
115 * @uses $db to do something really evil with the debug setting that should probably be eliminated. TODO!
116 * @param string $continueto a URL passed to print_continue() if the local upgrades succeed.
118 function upgrade_local_db($continueto) {
120 global $CFG, $db;
122 // if we don't have code version or a db upgrade file, just return true, we're unneeded
123 if (!file_exists($CFG->dirroot.'/local/version.php') || !file_exists($CFG->dirroot.'/local/db/upgrade.php')) {
124 return true;
127 require_once ($CFG->dirroot .'/local/version.php'); // Get code versions
129 if (empty($CFG->local_version)) { // normally we'd install, but just replay all the upgrades.
130 $CFG->local_version = 0;
133 if ($local_version > $CFG->local_version) { // upgrade!
134 $strdatabaseupgrades = get_string('databaseupgrades');
135 print_header($strdatabaseupgrades, $strdatabaseupgrades,
136 build_navigation(array(array('name' => $strdatabaseupgrades, 'link' => null, 'type' => 'misc'))), '', upgrade_get_javascript());
138 upgrade_log_start();
139 require_once ($CFG->dirroot .'/local/db/upgrade.php');
141 $db->debug=true;
142 if (xmldb_local_upgrade($CFG->local_version)) {
143 $db->debug=false;
144 if (set_config('local_version', $local_version)) {
145 notify(get_string('databasesuccess'), 'notifysuccess');
146 notify(get_string('databaseupgradelocal', '', $local_version), 'notifysuccess');
147 print_continue($continueto);
148 print_footer('none');
149 exit;
150 } else {
151 error('Upgrade of local database customisations failed! (Could not update version in config table)');
153 } else {
154 $db->debug=false;
155 error('Upgrade failed! See local/version.php');
158 } else if ($local_version < $CFG->local_version) {
159 upgrade_log_start();
160 notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
163 /// Capabilities
164 if (!update_capabilities('local')) {
165 error('Could not set up the capabilities for local!');
168 upgrade_log_finish();
172 * Notify local code that a course is being deleted.
173 * Look for a function local_delete_course() in a file called
174 * local/lib.php andn call it if it is there.
176 * @param int $courseid the course that is being deleted.
177 * @param bool $showfeedback Whether to display notifications on success.
178 * @return false if local_delete_course failed, or true if
179 * there was noting to do or local_delete_course succeeded.
181 function notify_local_delete_course($courseid, $showfeedback) {
182 global $CFG;
183 $localfile = $CFG->dirroot .'/local/lib.php';
184 if (file_exists($localfile)) {
185 require_once($localfile);
186 if (function_exists('local_delete_course')) {
187 if (local_delete_course($courseid)) {
188 if ($showfeedback) {
189 notify(get_string('deleted') . ' local data');
191 } else {
192 return false;
196 return true;