forked from ewallah/moodle-enrol_coursecompleted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
471 lines (435 loc) · 18 KB
/
lib.php
File metadata and controls
471 lines (435 loc) · 18 KB
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
<?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/>.
/**
* Coursecompleted enrolment plugin.
*
* @package enrol_coursecompleted
* @copyright 2017 eWallah (www.eWallah.net)
* @author Renaat Debleu <info@eWallah.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Coursecompleted enrolment plugin.
*
* @package enrol_coursecompleted
* @copyright 2017 eWallah (www.eWallah.net)
* @author Renaat Debleu <info@eWallah.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_coursecompleted_plugin extends enrol_plugin {
/**
* Returns localised name of enrol instance
*
* @param object $instance (null is accepted too)
* @return string
*/
public function get_instance_name($instance) {
global $DB;
if ($short = $DB->get_field('course', 'shortname', ['id' => $instance->customint1])) {
$coursename = format_string($short, true, ['context' => context_course::instance($instance->customint1)]);
return get_string('aftercourse', 'enrol_coursecompleted', $coursename);
}
return get_string('coursedeleted', '', $instance->customint1);
}
/**
* Returns optional enrolment information icons.
*
* @param array $instances all enrol instances of this type in one course
* @return array of pix_icon
*/
public function get_info_icons(array $instances) {
global $DB;
$arr = [];
foreach ($instances as $instance) {
if ($fullname = $DB->get_field('course', 'fullname', ['id' => $instance->customint1])) {
$context = context_course::instance($instance->customint1);
$name = format_string($fullname, true, ['context' => $context]);
$arr[] = new pix_icon('icon', get_string('aftercourse', 'enrol_coursecompleted', $name), 'enrol_coursecompleted');
}
}
return $arr;
}
/**
* Returns optional enrolment instance description text.
*
* @param object $instance
* @return string short html text
*/
public function get_description_text($instance) {
return 'Enrolment by completion of course with id ' . $instance->customint1;
}
/**
* Add information for people who want to enrol.
*
* @param stdClass $instance
* @return string html text, usually a form in a text box
*/
public function enrol_page_hook(stdClass $instance) {
global $DB, $OUTPUT;
$str = '';
if ($this->get_config('svglearnpath')) {
$items = $this->build_course_path($instance);
$arr = [];
$i = 1;
foreach ($items as $item) {
if ($item == $instance->courseid) {
$str = html_writer::span('', 'fa fa-circle fa-stack-2x text-dark');
$str .= html_writer::tag('strong', $i, ['class' => 'fa-stack-1x text-light']);
} else {
$str = html_writer::span('', 'fa fa-circle-o fa-stack-2x');
$str .= html_writer::tag('strong', $i, ['class' => 'fa-stack-1x']);
$str = $this->build_courselink($item, $str);
}
$arr[] = html_writer::span($str, 'fa-stack fa-2x');
$i++;
}
$arrow = html_writer::span(html_writer::span('', 'fa fa-arrow-right fa-stack-1x text-dark'), 'fa-stack fa-2x');
$str = implode($arrow, $arr);
}
if ($fullname = $DB->get_field('course', 'fullname', ['id' => $instance->customint1])) {
$context = context_course::instance($instance->customint1);
$name = format_string($fullname, true, ['context' => $context]);
$link = html_writer::link(new moodle_url('/course/view.php', ['id' => $instance->customint1]), $name);
$str = $OUTPUT->box(get_string('willbeenrolled', 'enrol_coursecompleted', $link . '<br/>' . $str));
}
return $str;
}
/**
* Gets an array of the user enrolment actions
*
* @param course_enrolment_manager $manager
* @param stdClass $ue A user enrolment object
* @return array An array of user_enrolment_actions
*/
public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
global $DB;
$actions = parent::get_user_enrolment_actions($manager, $ue);
$id = $ue->enrolmentinstance->customint1;
if ($DB->record_exists('course', ['id' => $id])) {
$context = context_course::instance($id);
if (has_capability('report/completion:view', $context)) {
$actions[] = new user_enrolment_action(
new pix_icon('a/search', ''),
get_string('pluginname', 'report_completion'),
new moodle_url('/report/completion/index.php', ['course' => $id]),
['class' => 'originlink', 'rel' => $ue->id]);
}
}
return $actions;
}
/**
* Returns edit icons for the page with list of instances
* @param stdClass $instance
* @return array
*/
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
if ($instance->enrol !== 'coursecompleted') {
throw new coding_exception('invalid enrol instance!');
}
$icons = parent::get_action_icons($instance);
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/coursecompleted:enrolpast', $context)) {
$managelink = new moodle_url("/enrol/coursecompleted/manage.php", ['enrolid' => $instance->id]);
$icon = new pix_icon('t/enrolusers', get_string('enrolusers', 'enrol_manual'), 'core', ['class' => 'iconsmall']);
$icons[] = $OUTPUT->action_icon($managelink, $icon);
}
return $icons;
}
/**
* Restore instance and map settings.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $course
* @param int $oldid
*/
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB;
if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) {
$merge = false;
} else {
$merge = ['courseid' => $course->id, 'enrol' => 'coursecompleted', 'roleid' => $data->roleid,
'customint1' => $data->customint1, 'customint2' => $data->customint2, 'customint3' => $data->customint3];
}
if ($merge && $instances = $DB->get_records('enrol', $merge, 'id')) {
$instance = reset($instances);
$instanceid = $instance->id;
} else {
$instanceid = $this->add_instance($course, (array)$data);
}
$step->set_mapping('enrol', $oldid, $instanceid);
}
/**
* Restore user enrolment.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $instance
* @param int $userid
* @param int $oldinstancestatus
*/
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status);
}
mark_user_dirty($userid);
}
/**
* Is it possible to add enrol instance via standard UI?
*
* @param int $courseid id of the course to add the instance to
* @return boolean
*/
public function can_add_instance($courseid) {
return has_capability('enrol/coursecompleted:manage', context_course::instance($courseid));
}
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
return has_capability('enrol/coursecompleted:manage', context_course::instance($instance->courseid));
}
/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
return has_capability('enrol/coursecompleted:manage', context_course::instance($instance->courseid));
}
/**
* Does this plugin allow manual unenrolment of all users?
*
* @param stdClass $instance course enrol instance
* @return bool - true means user with 'enrol/xxx:unenrol' may unenrol others freely
*/
public function allow_unenrol(stdClass $instance) {
return has_capability('enrol/coursecompleted:manage', context_course::instance($instance->courseid));
}
/**
* Does this plugin allow manual changes in user_enrolments table?
*
* @param stdClass $instance course enrol instance
* @return bool - true means it is possible to change enrol period and status in user_enrolments table
*/
public function allow_manage(stdClass $instance) {
return has_capability('enrol/coursecompleted:manage', context_course::instance($instance->courseid));
}
/**
* Does this plugin shows enrol me?
*
* @param stdClass $instance course enrol instance
* @return bool - true means it is possible to enrol.
*/
public function show_enrolme_link(stdClass $instance) {
return ($instance->status == ENROL_INSTANCE_ENABLED);
}
/**
* Execute synchronisation.
* @param progress_trace $trace
* @return int exit code, 0 means ok
*/
public function sync(progress_trace $trace) {
$this->process_expirations($trace);
return 0;
}
/**
* We are a good plugin and don't invent our own UI/validation code path.
*
* @return bool
*/
public function use_standard_editing_ui() {
return true;
}
/**
* Add elements to the edit instance form.
*
* @param stdClass $instance
* @param MoodleQuickForm $mform
* @param context $context
* @return bool
*/
public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
$options = [ENROL_INSTANCE_ENABLED => get_string('yes'), ENROL_INSTANCE_DISABLED => get_string('no')];
$mform->addElement('select', 'status', get_string('enabled', 'admin'), $options);
$mform->setDefault('status', $this->get_config('status'));
$role = ($instance->id) ? $instance->roleid : $this->get_config('roleid');
$roles = get_default_enrol_roles($context, $role);
$mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_paypal'), $roles);
$mform->setDefault('roleid', $this->get_config('roleid'));
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_paypal'),
['optional' => true, 'defaultunit' => 86400]);
$mform->setDefault('enrolperiod', $this->get_config('enrolperiod'));
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_paypal');
$mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_paypal'),
['optional' => true]);
$mform->setDefault('enrolstartdate', 0);
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_paypal');
$mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_paypal'), ['optional' => true]);
$mform->setDefault('enrolenddate', 0);
$mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_paypal');
$mform->addElement('course', 'customint1', get_string('course'), ['multiple' => false, 'includefrontpage' => false]);
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('customint1', 'compcourse', 'enrol_coursecompleted');
$mform->addElement('advcheckbox', 'customint3', get_string('groups'), get_string('group', 'enrol_coursecompleted'));
$mform->addHelpButton('customint3', 'group', 'enrol_coursecompleted');
$mform->setDefault('customint3', $this->get_config('keepgroup'));
$mform->addElement('advcheckbox', 'customint2', get_string('categoryemail', 'admin'),
get_string('welcome', 'enrol_coursecompleted'));
$mform->addHelpButton('customint2', 'welcome', 'enrol_coursecompleted');
$mform->setDefault('customint2', $this->get_config('welcome'));
$mform->addElement('textarea', 'customtext1',
get_string('customwelcome', 'enrol_coursecompleted'), ['cols' => '60', 'rows' => '8']);
$mform->addHelpButton('customtext1', 'customwelcome', 'enrol_coursecompleted');
$mform->disabledIf('customtext1', 'customint2', 'notchecked');
}
/**
* Add new instance of enrol plugin.
* @param object $course
* @param array $fields
* @return int id of new instance, null if can not be created
*/
public function add_instance($course, array $fields = null) {
if ($fields) {
if (!isset($fields['customint2'])) {
$fields['customint2'] = $this->get_config('welcome', 1);
}
if (!isset($fields['customint3'])) {
$fields['customint3'] = $this->get_config('keepgroup', 1);
}
}
return parent::add_instance($course, $fields);
}
/**
* Perform custom validation of the data used to edit the instance.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @param object $instance The instance loaded from the DB
* @param context $context The context of the instance we are editing
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK.
*/
public function edit_instance_validation($data, $files, $instance, $context) {
global $DB;
$errors = [];
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
if (!empty($data['enrolenddate']) && $data['enrolenddate'] < $data['enrolstartdate']) {
$errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_paypal');
}
if (empty($data['customint1']) or
$data['customint1'] == 1 or
!$DB->record_exists('course', ['id' => $data['customint1']])) {
$errors['customint'] = get_string('error_nonexistingcourse', 'tool_generator');
}
}
return $errors;
}
/**
* Build (possible) coursepath
*
* @param stdClass $instance
* @return array $items
*/
public function build_course_path(stdClass $instance) {
$parents = $this->search_parents($instance->customint1);
$children = $this->search_children($instance->courseid);
return array_unique(array_merge($parents, $children));
}
/**
* Search parents
*
* @param int $id
* @param int $level
* @return array items
*/
private function search_parents($id, $level = 1) {
global $DB;
$arr = [$id];
if ($level < 5) {
$level++;
$params = ['enrol' => 'coursecompleted', 'courseid' => $id];
if ($parent = $DB->get_field('enrol', 'customint1', $params, IGNORE_MULTIPLE)) {
$arr = array_merge($arr, $this->search_parents($parent, $level));
}
}
return $arr;
}
/**
* Search children
*
* @param int $id
* @param int $level
* @return array items
*/
private function search_children($id, $level = 1) {
global $DB;
$arr = [$id];
if ($level < 5) {
$level++;
$params = ['enrol' => 'coursecompleted', 'customint1' => $id];
if ($child = $DB->get_field('enrol', 'courseid', $params, IGNORE_MULTIPLE)) {
$arr = array_merge($arr, $this->search_children($child, $level));
}
}
return $arr;
}
/**
* Build course link
*
* @param int $id Course id
* @param string $svg svg image
* @return string course name
*/
private function build_courselink($id, $svg) {
global $DB;
$str = '';
if ($fullname = $DB->get_field('course', 'fullname', ['id' => $id])) {
$name = format_string($fullname, true, ['context' => context_course::instance($id)]);
$str = html_writer::link(new moodle_url('/course/view.php', ['id' => $id]), $svg, ['title' => $name]);
}
return $str;
}
/**
* Keep user in group
*
* @param stdClass $enrol Enrolment record
* @param int $userid User id
*/
public static function keepingroup($enrol, $userid) {
global $CFG;
if ($enrol->customint3 > 0) {
require_once($CFG->dirroot . '/group/lib.php');
$groups = array_values(groups_get_user_groups($enrol->customint1, $userid));
foreach ($groups as $group) {
$subs = array_values($group);
foreach ($subs as $sub) {
$groupnamea = groups_get_group_name($sub);
$groupnameb = groups_get_group_by_name($enrol->courseid, $groupnamea);
if ($groupnameb) {
groups_add_member($groupnameb, $userid);
}
}
}
}
}
}