forked from PoetOS/moodle-local_metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
436 lines (385 loc) · 15.4 KB
/
lib.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die;
/**
* @package local_metadata
* @author Mike Churchward <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/
function local_metadata_supports($feature) {
switch($feature) {
case FEATURE_BACKUP_MOODLE2:
return true;
default:
return null;
}
}
/**
* Loads user profile field data into the context object.
* @param stdClass $user
*/
function local_metadata_load_data($instance, $contextlevel) {
global $DB;
if (is_object($instance) && isset($instance->id)) {
$instanceid = $instance->id;
} else {
$instanceid = 0;
}
$sql = 'SELECT lmf.*, lm.instanceid, lm.fieldid, lm.data, lm.dataformat ';
$sql .= 'FROM {local_metadata_field} lmf ';
$sql .= 'LEFT JOIN {local_metadata} lm ON lmf.id = lm.fieldid AND lm.instanceid = :instanceid ';
$sql .= 'WHERE lmf.contextlevel = :contextlevel ';
$fields = $DB->get_records_sql($sql, ['instanceid' => $instanceid, 'contextlevel' => $contextlevel]);
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $instanceid, $field);
$formfield->edit_load_instance_data($instance);
}
}
/**
* Print out the customisable categories and fields for a users profile
*
* @param moodleform $mform instance of the moodleform class
* @param int $instanceid id of user whose profile is being edited.
*/
function local_metadata_definition($mform, $instanceid = 0, $contextlevel) {
global $DB;
// If user is "admin" fields are displayed regardless.
$update = has_capability('moodle/user:update', context_system::instance());
if ($categories = $DB->get_records('local_metadata_category', ['contextlevel' => $contextlevel], 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('local_metadata_field', ['categoryid' => $category->id], 'sortorder ASC')) {
// Check first if *any* fields will be displayed.
$display = false;
foreach ($fields as $field) {
if ($field->visible != PROFILE_VISIBLE_NONE) {
$display = true;
}
}
// Display the header and the fields.
if ($display || $update) {
$mform->addElement('header', 'category_'.$category->id, format_string($category->name));
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $instanceid);
$formfield->edit_field($mform);
}
}
}
}
}
}
/**
* Adds profile fields to instance edit forms.
* @param moodleform $mform
* @param int $instanceid
*/
function local_metadata_definition_after_data($mform, $instanceid, $contextlevel) {
global $DB;
$instanceid = ($instanceid < 0) ? 0 : (int)$instanceid;
if ($fields = $DB->get_records('local_metadata_field', ['contextlevel' => $contextlevel])) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $instanceid);
$formfield->edit_after_data($mform);
}
}
}
/**
* Validates profile data.
* @param stdClass $new
* @param array $files
* @return array
*/
function local_metadata_validation($new, $files, $contextlevel) {
global $DB;
if (is_array($new)) {
$new = (object)$new;
}
$err = [];
if ($fields = $DB->get_records('local_metadata_field', ['contextlevel' => $contextlevel])) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $new->id);
$err += $formfield->edit_validate_field($new, $files);
}
}
return $err;
}
/**
* Saves profile data for a instance.
* @param stdClass $new
*/
function local_metadata_save_data($new, $contextlevel) {
global $DB;
if ($fields = $DB->get_records('local_metadata_field', ['contextlevel' => $contextlevel])) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $new->id);
$formfield->edit_save_data($new);
}
}
}
/**
* Display profile fields.
* @param int $instanceid
*/
function local_metadata_display_fields($instanceid, $contextlevel, $returnonly=false) {
global $DB;
$output = '';
if ($categories = $DB->get_records('local_metadata_category', ['contextlevel' => $contextlevel], 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('local_metadata_field', ['categoryid' => $category->id], 'sortorder ASC')) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $instanceid);
if ($formfield->is_visible() && !$formfield->is_empty()) {
$output .= html_writer::tag('dt', format_string($formfield->field->name));
$output .= html_writer::tag('dd', $formfield->display_data());
}
}
}
}
}
if (!$returnonly) {
echo $output;
} else {
return $output;
}
}
/**
* Retrieves a list of profile fields that must be displayed in the sign-up form.
* Specific to user profiles.
*
* @return array list of profile fields info
* @since Moodle 3.2
*/
function local_metadata_get_signup_fields() {
global $DB;
$profilefields = [];
// Only retrieve required custom fields (with category information)
// results are sort by categories, then by fields.
$sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
FROM {local_metadata_field} uf
JOIN {local_metadata_category} ic
ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
WHERE uf.contextlevel = ?
ORDER BY ic.sortorder ASC, uf.sortorder ASC";
if ($fields = $DB->get_records_sql($sql, [CONTEXT_USER])) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$fieldobject = new $newfield($field->fieldid);
$profilefields[] = (object)[
'categoryid' => $field->categoryid,
'categoryname' => $field->categoryname,
'fieldid' => $field->fieldid,
'datatype' => $field->datatype,
'object' => $fieldobject
];
}
}
return $profilefields;
}
/**
* Adds code snippet to a moodle form object for custom profile fields that
* should appear on the signup page
* Specific to user profiles.
* @param moodleform $mform moodle form object
*/
function local_metadata_signup_fields($mform) {
if ($fields = local_metadata_get_signup_fields()) {
foreach ($fields as $field) {
// Check if we change the categories.
if (!isset($currentcat) || $currentcat != $field->categoryid) {
$currentcat = $field->categoryid;
$mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
};
$field->object->edit_field($mform);
}
}
}
/**
* Returns an object with the custom profile fields set for the given user
* Specific to user profiles.
* @param integer $instanceid
* @param bool $onlyinuserobject True if you only want the ones in $USER.
* @return stdClass
*/
function local_metadata_user_record($instanceid, $onlyinuserobject = true) {
global $DB;
$usercustomfields = new \stdClass();
if ($fields = $DB->get_records('local_metadata_field', ['contextlevel' => CONTEXT_USER])) {
foreach ($fields as $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield($field->id, $instanceid);
if (!$onlyinuserobject || $formfield->is_instance_object_data()) {
$usercustomfields->{$field->shortname} = $formfield->data;
}
}
}
return $usercustomfields;
}
/**
* Obtains a list of all available custom profile fields, indexed by id.
*
* Some profile fields are not included in the user object data (see
* local_metadata_user_record function above). Optionally, you can obtain only those
* fields that are included in the user object.
*
* To be clear, this function returns the available fields, and does not
* return the field values for a particular user.
*
* @param bool $onlyinuserobject True if you only want the ones in $USER
* @return array Array of field objects from database (indexed by id)
* @since Moodle 2.7.1
*/
function local_metadata_get_custom_fields($contextlevel, $onlyinuserobject = false) {
global $DB;
// Get all the fields.
$fields = $DB->get_records('local_metadata_field', ['contextlevel' => $contextlevel], 'id ASC');
// If only doing the user object ones, unset the rest.
if ($onlyinuserobject) {
foreach ($fields as $id => $field) {
$newfield = "\\metadatafieldtype_{$field->datatype}\\metadata";
$formfield = new $newfield();
if (!$formfield->is_instance_object_data()) {
unset($fields[$id]);
}
}
}
return $fields;
}
/**
* Does the user have all required custom fields set?
*
* Internal, to be exclusively used by {@link user_not_fully_set_up()} only.
*
* Note that if users have no way to fill a required field via editing their
* profiles (e.g. the field is not visible or it is locked), we still return true.
* So this is actually checking if we should redirect the user to edit their
* profile, rather than whether there is a value in the database.
*
* @param int $instanceid
* @return bool
*/
function local_metadata_has_required_custom_fields_set($instanceid, $contextlevel) {
global $DB;
$sql = "SELECT f.id
FROM {local_metadata_field} f
LEFT JOIN {local_metadata} d ON (d.fieldid = f.id AND d.instanceid = ?)
WHERE f.contextlevel = ? AND f.required = 1 AND f.visible > 0 AND f.locked = 0 AND d.id IS NULL";
if ($DB->record_exists_sql($sql, [$instanceid, $contextlevel])) {
return false;
}
return true;
}
/**
* Returns the contextlevel defined for the specified context plugin name.
*
* @param int $contextlevel The context level to look for.
* @return string The name of the located context.
*/
function local_metadata_get_contextname($contextlevel) {
static $contextnames = []; // Cache for located contexts.
$returnname = '';
if (isset($contextnames[$contextlevel])) {
$returnname = $contextnames[$contextlevel];
} else {
foreach (\local_metadata\context\context_handler::all_subplugins() as $contexthandler) {
if ($contexthandler->contextlevel == $contextlevel) {
$contextnames[$contextlevel] = $contexthandler->contextname;
$returnname = $contexthandler->contextname;
break;
}
}
}
return $returnname;
}
/**
* Implements callback inplace_editable() allowing to edit values in-place
*
* @param string $itemtype
* @param int $itemid
* @param mixed $newvalue
* @return local_metadata\output\inplace_editable
*/
function local_metadata_inplace_editable($itemtype, $itemid, $newvalue) {
\external_api::validate_context(\context_system::instance());
if ($itemtype === 'categoryname') {
return local_metadata\output\categoryname::update($itemid, $newvalue);
}
}
/**
* Hook function that is called when settings blocks are being built. Call all context functions
*/
function local_metadata_extend_settings_navigation($settingsnav, $context) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$contexthandler->extend_settings_navigation($settingsnav, $context);
}
}
/**
* Hook function that is called when user profile page is being built. Call all context functions
*/
function local_metadata_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$contexthandler->myprofile_navigation($tree, $user, $iscurrentuser, $course);
}
}
/**
* Hook function to extend the course settings navigation. Call all context functions
*/
function local_metadata_extend_navigation_course($parentnode, $course, $context) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$contexthandler->extend_navigation_course($parentnode, $course, $context);
}
}
/**
* This function extends the navigation with the metadata for user settings node.
*
* @param navigation_node $navigation The navigation node to extend
* @param stdClass $user The user object
* @param context $usercontext The context of the user
* @param stdClass $course The course to object for the tool
* @param context $coursecontext The context of the course
*/
function local_metadata_extend_navigation_user_settings($navigation, $user, $usercontext, $course, $coursecontext) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$contexthandler->extend_navigation_user_settings($navigation, $user, $usercontext, $course, $coursecontext);
}
}
/**
* Hook function to insert metadata form elements in the native module form
* @param moodleform $formwrapper The moodle quickforms wrapper object.
* @param MoodleQuickForm $mform The actual form object (required to modify the form).
*/
function local_metadata_coursemodule_standard_elements($formwrapper, $mform) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$contexthandler->coursemodule_standard_elements($formwrapper, $mform);
}
}
/**
* Hook the add/edit of the course module.
*
* @param stdClass $data Data from the form submission.
* @param stdClass $course The course.
*/
function local_metadata_coursemodule_edit_post_actions($data, $course) {
foreach (\local_metadata\context\context_handler::all_enabled_subplugins() as $contexthandler) {
$data = $contexthandler->coursemodule_edit_post_actions($data, $course);
}
return $data;
}