mirrored from git://git.moodle.org/moodle.git
/
helper.php
479 lines (432 loc) · 16.4 KB
/
helper.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?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/>.
/**
* File containing the helper class.
*
* @package tool_uploadcourse
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/coursecatlib.php');
require_once($CFG->dirroot . '/cache/lib.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Class containing a set of helpers.
*
* @package tool_uploadcourse
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_uploadcourse_helper {
/**
* Generate a shortname based on a template.
*
* @param array|object $data course data.
* @param string $templateshortname template of shortname.
* @return null|string shortname based on the template, or null when an error occured.
*/
public static function generate_shortname($data, $templateshortname) {
if (empty($templateshortname) && !is_numeric($templateshortname)) {
return null;
}
if (strpos($templateshortname, '%') === false) {
return $templateshortname;
}
$course = (object) $data;
$fullname = isset($course->fullname) ? $course->fullname : '';
$idnumber = isset($course->idnumber) ? $course->idnumber : '';
$callback = partial(array('tool_uploadcourse_helper', 'generate_shortname_callback'), $fullname, $idnumber);
$result = preg_replace_callback('/(?<!%)%([+~-])?(\d)*([fi])/', $callback, $templateshortname);
if (!is_null($result)) {
$result = clean_param($result, PARAM_TEXT);
}
if (empty($result) && !is_numeric($result)) {
$result = null;
}
return $result;
}
/**
* Callback used when generating a shortname based on a template.
*
* @param string $fullname full name.
* @param string $idnumber ID number.
* @param array $block result from preg_replace_callback.
* @return string
*/
public static function generate_shortname_callback($fullname, $idnumber, $block) {
switch ($block[3]) {
case 'f':
$repl = $fullname;
break;
case 'i':
$repl = $idnumber;
break;
default:
return $block[0];
}
switch ($block[1]) {
case '+':
$repl = core_text::strtoupper($repl);
break;
case '-':
$repl = core_text::strtolower($repl);
break;
case '~':
$repl = core_text::strtotitle($repl);
break;
}
if (!empty($block[2])) {
$repl = core_text::substr($repl, 0, $block[2]);
}
return $repl;
}
/**
* Return the available course formats.
*
* @return array
*/
public static function get_course_formats() {
return array_keys(core_component::get_plugin_list('format'));
}
/**
* Extract enrolment data from passed data.
*
* Constructs an array of methods, and their options:
* array(
* 'method1' => array(
* 'option1' => value,
* 'option2' => value
* ),
* 'method2' => array(
* 'option1' => value,
* 'option2' => value
* )
* )
*
* @param array $data data to extract the enrolment data from.
* @return array
*/
public static function get_enrolment_data($data) {
$enrolmethods = array();
$enroloptions = array();
foreach ($data as $field => $value) {
// Enrolmnent data.
$matches = array();
if (preg_match('/^enrolment_(\d+)(_(.+))?$/', $field, $matches)) {
$key = $matches[1];
if (!isset($enroloptions[$key])) {
$enroloptions[$key] = array();
}
if (empty($matches[3])) {
$enrolmethods[$key] = $value;
} else {
$enroloptions[$key][$matches[3]] = $value;
}
}
}
// Combining enrolment methods and their options in a single array.
$enrolmentdata = array();
if (!empty($enrolmethods)) {
$enrolmentplugins = self::get_enrolment_plugins();
foreach ($enrolmethods as $key => $method) {
if (!array_key_exists($method, $enrolmentplugins)) {
// Error!
continue;
}
$enrolmentdata[$enrolmethods[$key]] = $enroloptions[$key];
}
}
return $enrolmentdata;
}
/**
* Return the enrolment plugins.
*
* The result is cached for faster execution.
*
* @return array
*/
public static function get_enrolment_plugins() {
$cache = cache::make('tool_uploadcourse', 'helper');
if (($enrol = $cache->get('enrol')) === false) {
$enrol = enrol_get_plugins(false);
$cache->set('enrol', $enrol);
}
return $enrol;
}
/**
* Get the restore content tempdir.
*
* The tempdir is the sub directory in which the backup has been extracted.
*
* This caches the result for better performance, but $CFG->keeptempdirectoriesonbackup
* needs to be enabled, otherwise the cache is ignored.
*
* @param string $backupfile path to a backup file.
* @param string $shortname shortname of a course.
* @param array $errors will be populated with errors found.
* @return string|false false when the backup couldn't retrieved.
*/
public static function get_restore_content_dir($backupfile = null, $shortname = null, &$errors = array()) {
global $CFG, $DB, $USER;
$cachekey = null;
if (!empty($backupfile)) {
$backupfile = realpath($backupfile);
if (empty($backupfile) || !is_readable($backupfile)) {
$errors['cannotreadbackupfile'] = new lang_string('cannotreadbackupfile', 'tool_uploadcourse');
return false;
}
$cachekey = 'backup_path:' . $backupfile;
} else if (!empty($shortname) || is_numeric($shortname)) {
$cachekey = 'backup_sn:' . $shortname;
}
if (empty($cachekey)) {
return false;
}
// If $CFG->keeptempdirectoriesonbackup is not set to true, any restore happening would
// automatically delete the backup directory... causing the cache to return an unexisting directory.
$usecache = !empty($CFG->keeptempdirectoriesonbackup);
if ($usecache) {
$cache = cache::make('tool_uploadcourse', 'helper');
}
// If we don't use the cache, or if we do and not set, or the directory doesn't exist any more.
if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir("$CFG->tempdir/backup/$backupid"))) {
// Use null instead of false because it would consider that the cache key has not been set.
$backupid = null;
if (!empty($backupfile)) {
// Extracting the backup file.
$packer = get_file_packer('application/vnd.moodle.backup');
$backupid = restore_controller::get_tempdir_name(SITEID, $USER->id);
$path = "$CFG->tempdir/backup/$backupid/";
$result = $packer->extract_to_pathname($backupfile, $path);
if (!$result) {
$errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');
}
} else if (!empty($shortname) || is_numeric($shortname)) {
// Creating restore from an existing course.
$courseid = $DB->get_field('course', 'id', array('shortname' => $shortname), IGNORE_MISSING);
if (!empty($courseid)) {
$bc = new backup_controller(backup::TYPE_1COURSE, $courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
$bc->execute_plan();
$backupid = $bc->get_backupid();
$bc->destroy();
} else {
$errors['coursetorestorefromdoesnotexist'] =
new lang_string('coursetorestorefromdoesnotexist', 'tool_uploadcourse');
}
}
if ($usecache) {
$cache->set($cachekey, $backupid);
}
}
if ($backupid === null) {
$backupid = false;
}
return $backupid;
}
/**
* Return the role IDs.
*
* The result is cached for faster execution.
*
* @return array
*/
public static function get_role_ids() {
$cache = cache::make('tool_uploadcourse', 'helper');
if (($roles = $cache->get('roles')) === false) {
$roles = array();
$rolesraw = get_all_roles();
foreach ($rolesraw as $role) {
$roles[$role->shortname] = $role->id;
}
$cache->set('roles', $roles);
}
return $roles;
}
/**
* Get the role renaming data from the passed data.
*
* @param array $data data to extract the names from.
* @param array $errors will be populated with errors found.
* @return array where the key is the role_<id>, the value is the new name.
*/
public static function get_role_names($data, &$errors = array()) {
$rolenames = array();
$rolesids = self::get_role_ids();
$invalidroles = array();
foreach ($data as $field => $value) {
$matches = array();
if (preg_match('/^role_(.+)?$/', $field, $matches)) {
if (!isset($rolesids[$matches[1]])) {
$invalidroles[] = $matches[1];
continue;
}
$rolenames['role_' . $rolesids[$matches[1]]] = $value;
}
}
if (!empty($invalidroles)) {
$errors['invalidroles'] = new lang_string('invalidroles', 'tool_uploadcourse', implode(', ', $invalidroles));
}
// Roles names.
return $rolenames;
}
/**
* Helper to increment an ID number.
*
* This first checks if the ID number is in use.
*
* @param string $idnumber ID number to increment.
* @return string new ID number.
*/
public static function increment_idnumber($idnumber) {
global $DB;
while ($DB->record_exists('course', array('idnumber' => $idnumber))) {
$matches = array();
if (!preg_match('/(.*?)([0-9]+)$/', $idnumber, $matches)) {
$newidnumber = $idnumber . '_2';
} else {
$newidnumber = $matches[1] . ((int) $matches[2] + 1);
}
$idnumber = $newidnumber;
}
return $idnumber;
}
/**
* Helper to increment a shortname.
*
* This considers that the shortname passed has to be incremented.
*
* @param string $shortname shortname to increment.
* @return string new shortname.
*/
public static function increment_shortname($shortname) {
global $DB;
do {
$matches = array();
if (!preg_match('/(.*?)([0-9]+)$/', $shortname, $matches)) {
$newshortname = $shortname . '_2';
} else {
$newshortname = $matches[1] . ($matches[2]+1);
}
$shortname = $newshortname;
} while ($DB->record_exists('course', array('shortname' => $shortname)));
return $shortname;
}
/**
* Resolve a category based on the data passed.
*
* Key accepted are:
* - category, which is supposed to be a category ID.
* - category_idnumber
* - category_path, array of categories from parent to child.
*
* @param array $data to resolve the category from.
* @param array $errors will be populated with errors found.
* @return int category ID.
*/
public static function resolve_category($data, &$errors = array()) {
$catid = null;
if (!empty($data['category'])) {
$category = coursecat::get((int) $data['category'], IGNORE_MISSING);
if (!empty($category) && !empty($category->id)) {
$catid = $category->id;
} else {
$errors['couldnotresolvecatgorybyid'] =
new lang_string('couldnotresolvecatgorybyid', 'tool_uploadcourse');
}
}
if (empty($catid) && !empty($data['category_idnumber'])) {
$catid = self::resolve_category_by_idnumber($data['category_idnumber']);
if (empty($catid)) {
$errors['couldnotresolvecatgorybyidnumber'] =
new lang_string('couldnotresolvecatgorybyidnumber', 'tool_uploadcourse');
}
}
if (empty($catid) && !empty($data['category_path'])) {
$catid = self::resolve_category_by_path(explode(' / ', $data['category_path']));
if (empty($catid)) {
$errors['couldnotresolvecatgorybypath'] =
new lang_string('couldnotresolvecatgorybypath', 'tool_uploadcourse');
}
}
return $catid;
}
/**
* Resolve a category by ID number.
*
* @param string $idnumber category ID number.
* @return int category ID.
*/
public static function resolve_category_by_idnumber($idnumber) {
global $DB;
$cache = cache::make('tool_uploadcourse', 'helper');
$cachekey = 'cat_idn_' . $idnumber;
if (($id = $cache->get($cachekey)) === false) {
$params = array('idnumber' => $idnumber);
$id = $DB->get_field_select('course_categories', 'id', 'idnumber = :idnumber', $params, IGNORE_MISSING);
// Little hack to be able to differenciate between the cache not set and a category not found.
if ($id === false) {
$id = -1;
}
$cache->set($cachekey, $id);
}
// Little hack to be able to differenciate between the cache not set and a category not found.
if ($id == -1) {
$id = false;
}
return $id;
}
/**
* Resolve a category by path.
*
* @param array $path category names indexed from parent to children.
* @return int category ID.
*/
public static function resolve_category_by_path(array $path) {
global $DB;
$cache = cache::make('tool_uploadcourse', 'helper');
$cachekey = 'cat_path_' . serialize($path);
if (($id = $cache->get($cachekey)) === false) {
$parent = 0;
$sql = 'name = :name AND parent = :parent';
while ($name = array_shift($path)) {
$params = array('name' => $name, 'parent' => $parent);
if ($records = $DB->get_records_select('course_categories', $sql, $params, null, 'id, parent')) {
if (count($records) > 1) {
// Too many records with the same name!
$id = -1;
break;
}
$record = reset($records);
$id = $record->id;
$parent = $record->id;
} else {
// Not found.
$id = -1;
break;
}
}
$cache->set($cachekey, $id);
}
// We save -1 when the category has not been found to be able to know if the cache was set.
if ($id == -1) {
$id = false;
}
return $id;
}
}