-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.php
214 lines (194 loc) · 7.49 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
<?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/>.
/**
* The enrol plugin selma is defined here.
*
* @package enrol_selma
* @copyright 2020 LearningWorks <selma@learningworks.co.nz>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// The base class 'enrol_plugin' can be found at lib/enrollib.php. Override
// methods as necessary.
require_once(__DIR__ . '/vendor/autoload.php');
/**
* Class enrol_selma_plugin.
*
* @copyright 2020 LearningWorks <selma@learningworks.co.nz>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_selma_plugin extends enrol_plugin {
/**
* Enrol the user. This helps identify whether users were installed with the selma plugin (in the GUI, reports, etc).
*
* @param stdClass $instance
* @param int $userid
* @param null $roleid
* @param int $timestart
* @param int $timeend
* @param null $status
* @param null $recovergrades
* @throws coding_exception
*/
public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null,
$recovergrades = null) {
// Just call the parent directly.
parent::enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status,
$recovergrades);
}
/**
* Does this plugin allow manual enrolments?
*
* All plugins allowing this must implement 'enrol/selma:enrol' capability.
*
* @param stdClass $instance Course enrol instance.
* @return bool False means nobody may add more enrolments manually.
*/
public function allow_enrol($instance) {
return false;
}
/**
* Does this plugin allow manual unenrolment of all users?
*
* All plugins allowing this must implement 'enrol/selma:unenrol' capability.
*
* @param stdClass $instance Course enrol instance.
* @return bool False means nobody may touch user_enrolments.
*/
public function allow_unenrol($instance) {
return false;
}
/**
* Does this plugin allow manual changes in user_enrolments table?
*
* All plugins allowing this must implement 'enrol/selma:manage' capability.
*
* @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($instance) {
return true;
}
/**
* Does this plugin allow manual unenrolment of a specific user?
*
* All plugins allowing this must implement 'enrol/selma:unenrol' capability.
*
* This is useful especially for synchronisation plugins that
* do suspend instead of full unenrolment.
*
* @param stdClass $instance Course enrol instance.
* @param stdClass $ue Record from user_enrolments table, specifies user.
* @return bool False means nobody may touch this user enrolment.
*/
public function allow_unenrol_user($instance, $ue) {
return false;
}
/**
* Use the standard interface for adding/editing the form.
*
* @since Moodle 3.1.
* @return bool.
*/
public function use_standard_editing_ui() {
return true;
}
/**
* Single instance added programmatically. One instance per course.
*
* @param int $courseid Course ID attempting to add instance to.
* @return bool Bool whether instance can be added.
*/
public function can_add_instance($courseid) {
return true;
}
/**
* Add new instance of SELMA enrol plugin.
* @param object $course
* @param array $fields instance fields
* @return int id of new instance, null if can not be created
*/
public function add_instance($course, array $fields = null) {
global $DB;
// We can add to $fields here later, if needed.
// Check if we can really add an instance to the course.
$enrolinstance = $DB->record_exists('enrol', array('courseid' => $course->id, 'enrol' => $this->get_name()));
// Return null if an instance already exists for the course.
if ($enrolinstance) {
return null;
}
// Otherwise, continue adding the instance to the course.
return parent::add_instance($course, $fields);
}
/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
return false;
}
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
return false;
}
/**
* Manages the unenrolment process for the plugin - keep, strip roles, suspend or unenrol.
*
* @param int $ueid User-enrolment ID.
* @return bool|Exception Whether withdrawal was successful or not.
*/
public function unenrol_user_enrolment(int $ueid) {
global $DB;
// Check if $ueid set/valid.
if ($DB->get_record('user_enrolments', array('id' => $ueid)) === false || !isset($ueid)) {
return false;
}
// TODO - how to make sure user isn't re-enrolled into course? They are still in the intake. A 'withdrawn' table?
// Deal with unenrolments.
switch ($this->get_config('unenrolaction')) {
case ENROL_EXT_REMOVED_SUSPEND:
$DB->set_field('user_enrolments', 'status', ENROL_USER_SUSPENDED, array('id' => $ueid));
return true;
case ENROL_EXT_REMOVED_SUSPENDNOROLES:
$DB->set_field('user_enrolments', 'status', ENROL_USER_SUSPENDED, array('id' => $ueid));
$ue = $DB->get_record('user_enrolments', array('id' => $ueid));
$enrol = $DB->get_record('enrol', array('id' => $ue->enrolid));
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid' => $context->id, 'userid' => $ue->userid, 'component' => 'enrol_selma',
'itemid' => $ue->enrolid));
return true;
case ENROL_EXT_REMOVED_UNENROL:
$ue = $DB->get_record('user_enrolments', array('id' => $ueid));
if ($ue === false) {
throw new moodle_exception('warning_message_notfound', 'enrol_selma', null, $ueid);
}
$instance = $DB->get_record('enrol', array('id' => $ue->enrolid));
parent::unenrol_user($instance, $ue->userid);
return true;
case ENROL_EXT_REMOVED_KEEP:
// Keep - only adding enrolments allowed.
return true;
}
return false;
}
}