improved hiding support in grade/
[moodle-pu.git] / lib / locallib.php
blob85aa28f91437487ef2aaee98f37f3b39a3fec817
1 <?php
2 /**
3 * This file provides hooks from moodle core into custom code.
4 *
5 * Important note
6 * --------------
7 *
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.
51 * Course deletion
52 * ---------------
54 * To have your local customisations notified when a course is deleted,
55 * make a file called
57 * local/lib.php
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.
64 /**
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 'xmldb_local_upgrade'
68 * in a file called 'local/db/upgrade.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.
72 * @uses $CFG
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) {
78 global $CFG, $db;
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/upgrade.php')) {
82 return true;
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, '', upgrade_get_javascript());
95 upgrade_log_start();
96 require_once ($CFG->dirroot .'/local/db/upgrade.php');
98 $db->debug=true;
99 if (xmldb_local_upgrade($CFG->local_version)) {
100 $db->debug=false;
101 if (set_config('local_version', $local_version)) {
102 notify(get_string('databasesuccess'), 'notifysuccess');
103 notify(get_string('databaseupgradelocal', '', $local_version), 'notifysuccess');
104 print_continue($continueto);
105 print_footer('none');
106 exit;
107 } else {
108 error('Upgrade of local database customisations failed! (Could not update version in config table)');
110 } else {
111 $db->debug=false;
112 error('Upgrade failed! See local/version.php');
115 } else if ($local_version < $CFG->local_version) {
116 upgrade_log_start();
117 notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
119 upgrade_log_finish();
123 * Notify local code that a course is being deleted.
124 * Look for a function local_delete_course() in a file called
125 * local/lib.php andn call it if it is there.
127 * @param int $courseid the course that is being deleted.
128 * @param bool $showfeedback Whether to display notifications on success.
129 * @return false if local_delete_course failed, or true if
130 * there was noting to do or local_delete_course succeeded.
132 function notify_local_delete_course($courseid, $showfeedback) {
133 global $CFG;
134 $localfile = $CFG->dirroot .'/local/lib.php';
135 if (file_exists($localfile)) {
136 require_once($localfile);
137 if (function_exists('local_delete_course')) {
138 if (local_delete_course($courseid)) {
139 if ($showfeedback) {
140 notify(get_string('deleted') . ' local data');
142 } else {
143 return false;
147 return true;