-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
296 lines (264 loc) · 10.2 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
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
<?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/>.
/**
* relationship enrolment plugin.
*
* @package enrol_relationship
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* relationship enrolment plugin implementation.
* @author Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_relationship_plugin extends enrol_plugin {
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/relationship:config', $context);
}
/**
* Returns localised name of enrol instance.
*
* @param stdClass $instance (null is accepted too)
* @return string
*/
public function get_instance_name($instance) {
global $DB;
if (empty($instance)) {
$enrol = $this->get_name();
return get_string('pluginname', 'enrol_'.$enrol);
} else if (empty($instance->name)) {
$enrol = $this->get_name();
if($relationship = $DB->get_record('relationship', array('id'=>$instance->customint1))) {
$relationshipname = format_string($relationship->name, true, array('context'=>context::instance_by_id($relationship->contextid)));
return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $relationshipname . ')';
} else {
return get_string('pluginname', 'enrol_'.$enrol);
}
} else {
return format_string($instance->name, true, array('context'=>context_course::instance($instance->courseid)));
}
}
/**
* Returns link to page which may be used to add new instance of enrolment plugin in course.
* @param int $courseid
* @return moodle_url page url
*/
public function get_newinstance_link($courseid) {
if (!$this->can_add_new_instances($courseid)) {
return NULL;
}
// Multiple instances supported - multiple parent courses linked.
return new moodle_url('/enrol/relationship/edit.php', array('courseid'=>$courseid));
}
/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/relationship:config', $context);
}
/**
* Given a courseid this function returns true if the user is able to enrol or configure relationships.
* AND there are relationships that the user can view.
*
* @param int $courseid
* @return bool
*/
protected function can_add_new_instances($courseid) {
global $DB;
$coursecontext = context_course::instance($courseid);
if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/relationship:config', $coursecontext)) {
return false;
}
list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
$sql = "SELECT id, contextid
FROM {relationship}
WHERE contextid $sqlparents
ORDER BY name ASC";
$relationships = $DB->get_records_sql($sql, $params);
foreach ($relationships as $c) {
$context = context::instance_by_id($c->contextid);
if (has_capability('local/relationship:view', $context)) {
return true;
}
}
return false;
}
/**
* 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 !== 'relationship') {
throw new coding_exception('invalid enrol instance!');
}
$context = context_course::instance($instance->courseid);
$icons = array();
if (has_capability('enrol/relationship:config', $context)) {
$editlink = new moodle_url("/enrol/relationship/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;
}
/**
* Called for all enabled enrol plugins that returned true from is_cron_required().
* @return void
*/
public function cron() {
global $CFG;
require_once($CFG->dirroot . '/enrol/relationship/locallib.php');
$trace = new null_progress_trace();
enrol_relationship_sync($trace);
$trace->finished();
}
/**
* Called after updating/inserting course.
*
* @param bool $inserted true if course just inserted
* @param stdClass $course
* @param stdClass $data form data
* @return void
*/
public function course_updated($inserted, $course, $data) {
// It turns out there is no need for relationships to deal with this hook, see MDL-34870.
}
/**
* Update instance status
*
* @param stdClass $instance
* @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
* @return void
*/
public function update_status($instance, $newstatus) {
global $CFG;
parent::update_status($instance, $newstatus);
require_once($CFG->dirroot . '/enrol/relationship/locallib.php');
$trace = new null_progress_trace();
enrol_relationship_sync($trace, $instance->courseid);
$trace->finished();
}
/**
* 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) {
$actions = array();
$context = $manager->get_context();
$instance = $ue->enrolmentinstance;
$params = $manager->get_moodlepage()->url->params();
$params['ue'] = $ue->id;
if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/relationship:unenrol', $context)) {
$url = new moodle_url('/enrol/unenroluser.php', $params);
$actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
}
return $actions;
}
/**
* 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, $CFG;
if (!$step->get_task()->is_samesite()) {
// No relationship restore from other sites.
$step->set_mapping('enrol', $oldid, 0);
return;
}
if ($DB->record_exists('relationship', array('id'=>$data->customint1))) {
$instance = $DB->get_record('enrol', array('customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
if ($instance) {
$instanceid = $instance->id;
} else {
$instanceid = $this->add_instance($course, (array)$data);
}
$step->set_mapping('enrol', $oldid, $instanceid);
require_once($CFG->dirroot . '/enrol/relationship/locallib.php');
$trace = new null_progress_trace();
enrol_relationship_sync($trace, $course->id);
$trace->finished();
} else {
$step->set_mapping('enrol', $oldid, 0);
}
}
public function delete_instance($instance) {
global $DB;
parent::delete_instance($instance);
$sql = "SELECT id
FROM {groups}
WHERE courseid = {$instance->courseid} AND idnumber LIKE 'relationship_{$instance->customint1}_%'";
$groups = $DB->get_records_sql($sql);
foreach($groups AS $g) {
groups_delete_group($g->id);
}
}
/**
* Restore user enrolment.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $instance
* @param int $oldinstancestatus
* @param int $userid
*/
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
global $DB;
if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $oldinstancestatus);
}
}
/**
* Restore user group membership.
* @param stdClass $instance
* @param int $groupid
* @param int $userid
*/
public function restore_group_member($instance, $groupid, $userid) {
// Nothing to do here, the group members are added in $this->restore_group_restored()
return;
}
}
/**
* Prevent removal of enrol roles.
* @param int $itemid
* @param int $groupid
* @param int $userid
* @return bool
*/
function enrol_relationship_allow_group_member_remove($itemid, $groupid, $userid) {
return false;
}