-
Notifications
You must be signed in to change notification settings - Fork 18
/
definelib.php
401 lines (339 loc) · 13.8 KB
/
definelib.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
<?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/>.
/**
* @package local_metadata
* @author Mike Churchward <mike.churchward@poetopensource.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017, onwards Poet
*/
defined('MOODLE_INTERNAL') || die;
/**
* Base class for the customisable metadata fields.
*
* @package local_metadata
* @copyright 2017, onwards Poet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Reorder the profile fields within a given category starting at the field at the given startorder.
*/
function local_metadata_reorder_fields() {
global $DB;
if ($categories = $DB->get_records('local_metadata_category')) {
foreach ($categories as $category) {
$i = 1;
if ($fields = $DB->get_records('local_metadata_field', ['categoryid' => $category->id], 'sortorder ASC')) {
foreach ($fields as $field) {
$f = new stdClass();
$f->id = $field->id;
$f->sortorder = $i++;
$DB->update_record('local_metadata_field', $f);
}
}
}
}
}
/**
* Reorder the profile categoriess starting at the category at the given startorder.
* @param int $contextlevel The context of category to work on.
*/
function local_metadata_reorder_categories($contextlevel) {
global $DB;
$i = 1;
if ($categories = $DB->get_records('local_metadata_category', ['contextlevel' => $contextlevel], 'sortorder ASC')) {
foreach ($categories as $cat) {
$c = new stdClass();
$c->id = $cat->id;
$c->sortorder = $i++;
$DB->update_record('local_metadata_category', $c);
}
}
}
/**
* Delete a profile category
* @param int $id of the category to be deleted
* @return bool success of operation
*/
function local_metadata_delete_category($id) {
global $DB;
// Retrieve the category.
if (!$category = $DB->get_record('local_metadata_category', ['id' => $id])) {
print_error('invalidcategoryid');
}
if (!$categories = $DB->get_records('local_metadata_category', ['contextlevel' => $category->contextlevel], 'sortorder ASC')) {
print_error('nocate', 'debug');
}
unset($categories[$category->id]);
if (!count($categories)) {
return false; // We can not delete the last category.
}
// Does the category contain any fields.
if ($DB->count_records('local_metadata_field', ['categoryid' => $category->id])) {
if (array_key_exists($category->sortorder - 1, $categories)) {
$newcategory = $categories[$category->sortorder - 1];
} else if (array_key_exists($category->sortorder + 1, $categories)) {
$newcategory = $categories[$category->sortorder + 1];
} else {
$newcategory = reset($categories); // Get first category if sortorder broken.
}
$sortorder = $DB->count_records('local_metadata_field', ['categoryid' => $newcategory->id]) + 1;
if ($fields = $DB->get_records('local_metadata_field', ['categoryid' => $category->id], 'sortorder ASC')) {
foreach ($fields as $field) {
$f = new stdClass();
$f->id = $field->id;
$f->sortorder = $sortorder++;
$f->categoryid = $newcategory->id;
$DB->update_record('local_metadata_field', $f);
}
}
}
// Finally we get to delete the category.
$DB->delete_records('local_metadata_category', ['id' => $category->id]);
local_metadata_reorder_categories($category->contextlevel);
return true;
}
/**
* Deletes a profile field.
* @param int $id
*/
function local_metadata_delete_field($id) {
global $DB;
// Remove any user data associated with this field.
if (!$DB->delete_records('local_metadata', ['fieldid' => $id])) {
print_error('cannotdeletecustomfield');
}
// Note: Any availability conditions that depend on this field will remain,
// but show the field as missing until manually corrected to something else.
// Need to rebuild course cache to update the info.
rebuild_course_cache(0, true);
// Try to remove the record from the database.
$DB->delete_records('local_metadata_field', ['id' => $id]);
// Reorder the remaining fields in the same category.
local_metadata_reorder_fields();
}
/**
* Change the sort order of a field
*
* @param int $id of the field
* @param string $move direction of move
* @return bool success of operation
*/
function local_metadata_move_field($id, $move) {
global $DB;
// Get the field object.
if (!$field = $DB->get_record('local_metadata_field', ['id' => $id], 'id, sortorder, categoryid')) {
return false;
}
// Count the number of fields in this category.
$fieldcount = $DB->count_records('local_metadata_field', ['categoryid' => $field->categoryid]);
// Calculate the new sortorder.
if (($move == 'up') && ($field->sortorder > 1)) {
$neworder = $field->sortorder - 1;
} else if (($move == 'down') && ($field->sortorder < $fieldcount)) {
$neworder = $field->sortorder + 1;
} else {
return false;
}
// Retrieve the field object that is currently residing in the new position.
$params = ['categoryid' => $field->categoryid, 'sortorder' => $neworder];
if ($swapfield = $DB->get_record('local_metadata_field', $params, 'id, sortorder')) {
// Swap the sortorders.
$swapfield->sortorder = $field->sortorder;
$field->sortorder = $neworder;
// Update the field records.
$DB->update_record('local_metadata_field', $field);
$DB->update_record('local_metadata_field', $swapfield);
}
local_metadata_reorder_fields();
return true;
}
/**
* Change the sort order of a category.
*
* @param int $id of the category
* @param string $move direction of move
* @return bool success of operation
*/
function local_metadata_move_category($id, $move) {
global $DB;
// Get the category object.
if (!($category = $DB->get_record('local_metadata_category', ['id' => $id], 'id, contextlevel, sortorder'))) {
return false;
}
// Count the number of categories.
$categorycount = $DB->count_records('local_metadata_category', ['contextlevel' => $category->contextlevel]);
// Calculate the new sortorder.
if (($move == 'up') && ($category->sortorder > 1)) {
$neworder = $category->sortorder - 1;
} else if (($move == 'down') && ($category->sortorder < $categorycount)) {
$neworder = $category->sortorder + 1;
} else {
return false;
}
// Retrieve the category object that is currently residing in the new position.
if ($swapcategory = $DB->get_record('local_metadata_category',
['contextlevel' => $category->contextlevel, 'sortorder' => $neworder], 'id, contextlevel, sortorder')) {
// Swap the sortorders.
$swapcategory->sortorder = $category->sortorder;
$category->sortorder = $neworder;
// Update the category records.
$DB->update_record('local_metadata_category', $category) && $DB->update_record('local_metadata_category', $swapcategory);
return true;
}
return false;
}
/**
* Retrieve a list of all the available data types
* @return array a list of the datatypes suitable to use in a select statement
*/
function local_metadata_list_datatypes() {
$datatypes = [];
$fieldtypeplugins = core_component::get_plugin_list('metadatafieldtype');
foreach ($fieldtypeplugins as $fieldtypename => $fieldtypelocation) {
$classname = "\\metadatafieldtype_{$fieldtypename}\\metadata";
$newdatatype = new $classname();
$datatypes[$fieldtypename] = $newdatatype->name;
}
asort($datatypes);
return $datatypes;
}
/**
* Retrieve a list of categories and ids suitable for use in a form
* @param int $contextlevel The context level to retrieve categories for.
* @return array
*/
function local_metadata_list_categories($contextlevel) {
global $DB;
$categories = $DB->get_records_menu('local_metadata_category', ['contextlevel' => $contextlevel], 'sortorder ASC', 'id, name');
return array_map('format_string', $categories);
}
/**
* Edit a category
*
* @param int $id
* @param string $redirect
*/
function local_metadata_edit_category($id, $redirect, $contextlevel) {
global $DB, $OUTPUT;
$categoryform = new local_metadata\forms\category_form();
if (!($category = $DB->get_record('local_metadata_category', ['id' => $id]))) {
$category = new stdClass();
$category->contextlevel = $contextlevel;
}
$categoryform->set_data($category);
if ($categoryform->is_cancelled()) {
redirect($redirect);
} else {
if ($data = $categoryform->get_data()) {
if (empty($data->id)) {
unset($data->id);
$data->sortorder = $DB->count_records('local_metadata_category', ['contextlevel' => $contextlevel]) + 1;
$DB->insert_record('local_metadata_category', $data, false);
} else {
$DB->update_record('local_metadata_category', $data);
}
local_metadata_reorder_categories($category->contextlevel);
redirect($redirect);
}
if (empty($id)) {
$strheading = get_string('profilecreatenewcategory', 'admin');
} else {
$strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
}
// Print the page.
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
$categoryform->display();
echo $OUTPUT->footer();
}
}
/**
* Edit a profile field.
*
* @param int $id
* @param string $datatype
* @param string $redirect
* @param int $contextlevel
*/
function local_metadata_edit_field($id, $datatype, $redirect, $contextlevel) {
global $DB, $OUTPUT, $PAGE;
if (!$field = $DB->get_record('local_metadata_field', ['id' => $id])) {
$field = new stdClass();
$field->contextlevel = $contextlevel;
$field->datatype = $datatype;
$field->description = '';
$field->descriptionformat = FORMAT_HTML;
$field->defaultdata = '';
$field->defaultdataformat = FORMAT_HTML;
}
// Clean and prepare description for the editor.
$field->description = clean_text($field->description, $field->descriptionformat);
$field->description = ['text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0];
$fieldform = new local_metadata\forms\field_form(null, ['datatype' => $field->datatype, 'contextlevel' => $contextlevel]);
// Convert the data format for.
if (is_array($fieldform->editors())) {
foreach ($fieldform->editors() as $editor) {
if (isset($field->$editor)) {
$field->$editor = clean_text($field->$editor, $field->{$editor.'format'});
$field->$editor = ['text' => $field->$editor, 'format' => $field->{$editor.'format'}, 'itemid' => 0];
}
}
}
$fieldform->set_data($field);
if ($fieldform->is_cancelled()) {
redirect($redirect);
} else {
if ($data = $fieldform->get_data()) {
$newfield = "\\metadatafieldtype_{$datatype}\\define";
$formfield = new $newfield($contextlevel);
// Collect the description and format back into the proper data structure from the editor.
// Note: This field will ALWAYS be an editor.
$data->descriptionformat = $data->description['format'];
$data->description = $data->description['text'];
// Check whether the default data is an editor, this is (currently) only the textarea field type.
if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) {
// Collect the default data and format back into the proper data structure from the editor.
$data->defaultdataformat = $data->defaultdata['format'];
$data->defaultdata = $data->defaultdata['text'];
}
// Convert the data format for.
if (is_array($fieldform->editors())) {
foreach ($fieldform->editors() as $editor) {
if (isset($field->$editor)) {
$field->{$editor.'format'} = $field->{$editor}['format'];
$field->$editor = $field->{$editor}['text'];
}
}
}
$formfield->define_save($data);
local_metadata_reorder_fields();
local_metadata_reorder_categories($contextlevel);
redirect($redirect);
}
$datatypes = local_metadata_list_datatypes();
if (empty($id)) {
$strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
} else {
$strheading = get_string('profileeditfield', 'admin', format_string($field->name));
}
// Print the page.
$PAGE->navbar->add($strheading);
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
$fieldform->display();
echo $OUTPUT->footer();
}
}