-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
92 lines (76 loc) · 3.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
<?php
/**
* MDL-47323 zbackuptest enrol plugin to demonstrate backup and restore.
*
* This plugin does nothing by itself. It demonstrates enrol backup/restore.
*
* Settings:
*
* Role id - This is stored in customint1, and annotated on backup.
* Term - This string is stored in the enrol_zbackuptest_termmap table should backup/restore with the enrol instance.
*
* @author Matt Petro
*/
class enrol_zbackuptest_plugin extends enrol_plugin {
public function get_newinstance_link($courseid) {
return new moodle_url('/enrol/zbackuptest/edit.php', array('courseid'=>$courseid));
}
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
$context = context_course::instance($instance->courseid);
$icons = array();
if (has_capability('enrol/manual:config', $context)) {
$editlink = new moodle_url("/enrol/zbackuptest/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
array('class' => 'iconsmall')));
}
return $icons;
}
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/manual:config', $context);
}
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/manual:config', $context);
}
public function allow_manage(stdClass $instance) {
// Users with manage cap may tweak period and status.
return true;
}
public function sync_enrols($instance) {
global $CFG;
// Ensure admins are added, just to have something to test with.
$admins = get_admins();
foreach ($admins as $admin) {
$this->enrol_user($instance, $admin->id, null, 0, 0, null, false);
}
}
public function add_instance($course, array $fields = NULL) {
global $DB;
// For simplicity, just make the test enrollments on creation.
$instanceid = parent::add_instance($course, $fields);
$this->sync_enrols($DB->get_record('enrol', array('id'=>$instanceid)));
return $instanceid;
}
/**
* Backup execution step hook.
*
* @param backup_enrolments_execution_step $step
* @param stdClass $enrol
*/
public function backup_annotate_custom_fields(backup_enrolments_execution_step $step, stdClass $enrol) {
// annotate customint1 as a role
$step->annotate_id('role', $enrol->customint1);
}
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB, $CFG;
$data->customint1 = $step->get_mappingid('role', $data->customint1, null);
$instanceid = $this->add_instance($course, (array)$data);
$step->set_mapping('enrol', $oldid, $instanceid);
//$this->sync_enrols($DB->get_record('enrol', array('id'=>$instanceid)));
}
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $oldinstancestatus);
}
}