forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
214 lines (202 loc) · 7.77 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* This file provides hooks from moodle core into custom code.
*
* Important note
* --------------
*
* If at all possible, the facilities provided here should not be used.
* Wherever possible, customisations should be written using one of the
* standard plug-in points like modules, blocks, auth plugins, themes, ...
*
* However, sometimes that is just not possible, because of the nature
* of the change you want to make. In which case the second best plan is
* to implement your feature in a generally useful way, which can
* be contributed back to the moodle project so that everyone benefits.
*
* But supposing you are forced to implement some nasty hack that only
* you will ever want, then the local folder is for you. The idea is that
* instead of scattering your changes throughout the code base, you
* put them all in a folder called 'local'. Then you won't have to
* deal with merging problems when you upgrade the rest of your moodle
* installation.
*
*
* Available hooks
* ===============
*
* These are similar to the module interface, however, not all the the
* facilities that are available to modules are available to local code (yet).
*
*
* Local database customisations
* -----------------------------
*
* If your local customisations require changes to the database, use the files:
*
* local/version.php
* local/db/upgrade.php
*
* In the file version.php, set the variable $local_version to a versionstamp
* value like 2006030300 (a concatenation of year, month, day, serial).
*
* In the file upgrade.php, implement the
* function xmldb_local_upgrade($oldversion) to make the database changes.
*
* Note that you don't need to have an install.xml file. Instead,
* when your moodle instance is first installed, xmldb_local_upgrade() will be called
* with $oldversion set to 0, so that all the updates run.
*
* Local capabilities
* ------------------
*
* If your local customisations require their own capabilities, use
*
* local/db/access.php
*
* You should create an array called $local_capabilities, which looks like:
*
* $local_capabilities = array(
* 'moodle/local:capability' => array(
* 'captype' => 'read',
* 'contextlevel' => CONTEXT_SYSTEM,
* ),
* );
*
* Note that for all local capabilities you add, you'll need to add language strings.
* Moodle will expect to find them in local/lang/en_utf8/local.php (eg for English)
* with a key (following the above example) of local:capability
* See the next section for local language support.
*
*
* Local language support
* ----------------------
*
* Moodle supports looking in the local/ directory for language files.
* You would need to create local/lang/en_utf8/local.php
* and then could call strings like get_string('key', 'local');
* Make sure you don't call the language file something that moodle already has one of,
* stick to local or $clientname)
*
*
* Local admin menu items
* ----------------------
*
* It is possible to add new items to the admin_tree block.
* To do this, create a file, local/settings.php
* which can access the $ADMIN variable directly and add things to it.
* You might do something like:
* $ADMIN->add('root', new admin_category($name, $title);
* $ADMIN->add('foo', new admin_externalpage($name, $title, $url, $cap);
*
*
* Course deletion
* ---------------
*
* To have your local customisations notified when a course is deleted,
* make a file called
*
* local/lib.php
*
* In there, implement the function local_delete_course($courseid). This
* function will then be called whenever the functions remove_course_contents()
* or delete_course() from moodlelib are called.
*/
/**
* This function checks to see whether local database customisations are up-to-date
* by comparing $CFG->local_version to the variable $local_version defined in
* local/version.php. If not, it looks for a function called 'xmldb_local_upgrade'
* in a file called 'local/db/upgrade.php', and if it's there calls it with the
* appropiate $oldversion parameter. Then it updates $CFG->local_version.
* On success it prints a continue link. On failure it prints an error.
*
* @uses $CFG
* @param string $continueto a URL passed to print_continue() if the local upgrades succeed.
*/
function upgrade_local_db($continueto) {
global $CFG, $DB;
// if we don't have code version or a db upgrade file, just return true, we're unneeded
if (!file_exists($CFG->dirroot.'/local/version.php') || !file_exists($CFG->dirroot.'/local/db/upgrade.php')) {
return true;
}
require_once ($CFG->dirroot .'/local/version.php'); // Get code versions
if (empty($CFG->local_version)) { // normally we'd install, but just replay all the upgrades.
$CFG->local_version = 0;
}
if ($local_version > $CFG->local_version) { // upgrade!
$strdatabaseupgrades = get_string('databaseupgrades');
if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) {
print_header($strdatabaseupgrades, $strdatabaseupgrades,
build_navigation(array(array('name' => $strdatabaseupgrades, 'link' => null, 'type' => 'misc'))), '', upgrade_get_javascript());
}
upgrade_log_start();
require_once ($CFG->dirroot .'/local/db/upgrade.php');
if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) {
$DB->set_debug(true);
}
if (xmldb_local_upgrade($CFG->local_version)) {
if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) {
$DB->set_debug(false);
}
if (set_config('local_version', $local_version)) {
notify(get_string('databasesuccess'), 'notifysuccess');
notify(get_string('databaseupgradelocal', '', $local_version), 'notifysuccess');
if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) {
print_continue($continueto);
print_footer('none');
exit;
} else if (CLI_UPGRADE && ($interactive > CLI_SEMI) ) {
console_write(STDOUT,'askcontinue');
if (read_boolean()){
return ;
}else {
console_write(STDERR,'','',false);
}
}
} else {
print_error('cannotupgradedbcustom', 'debug');
}
} else {
if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) {
$DB->set_debug(false);
}
print_error('upgradefail', 'debug', '', 'local/version.php');
}
} else if ($local_version < $CFG->local_version) {
upgrade_log_start();
notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
}
/// Capabilities
if (!update_capabilities('local')) {
error('Could not set up the capabilities for local!');
}
upgrade_log_finish();
}
/**
* Notify local code that a course is being deleted.
* Look for a function local_delete_course() in a file called
* local/lib.php andn call it if it is there.
*
* @param int $courseid the course that is being deleted.
* @param bool $showfeedback Whether to display notifications on success.
* @return false if local_delete_course failed, or true if
* there was noting to do or local_delete_course succeeded.
*/
function notify_local_delete_course($courseid, $showfeedback) {
global $CFG;
$localfile = $CFG->dirroot .'/local/lib.php';
if (file_exists($localfile)) {
require_once($localfile);
if (function_exists('local_delete_course')) {
if (local_delete_course($courseid)) {
if ($showfeedback) {
notify(get_string('deleted') . ' local data');
}
} else {
return false;
}
}
}
return true;
}
?>