-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib.php
238 lines (205 loc) · 7.46 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
<?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/>.
/**
* Group-cohort sync plugin.
*
* @package enrol_groupsync
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Group-cohort sync plugin.
*
* @copyright Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_groupsync_plugin extends enrol_plugin {
/**
* 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();
$cohort = $DB->get_record('cohort', array('id' => $instance->customint1));
$group = $DB->get_record('groups', array('id' => $instance->customint2));
if ($cohort and $group) {
$groupname = format_string($group->name, true, array('context' => context_course::instance($instance->courseid)));
$cohortname = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ' -> ' . $groupname . ')';
} else {
return get_string('pluginname', 'enrol_'.$enrol) . ' - ' . get_string('error');
}
} 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/groupsync/edit.php', array('courseid' => $courseid));
}
/**
* Given a courseid this function returns true if the user is able to enrol or configure cohorts.
* AND there are cohorts 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/groupsync:config', $coursecontext)) {
return false;
}
list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
$sql = "SELECT id, contextid
FROM {cohort}
WHERE contextid $sqlparents
ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
foreach ($cohorts as $c) {
$context = context::instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
return true;
}
}
return false;
}
/**
* 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/groupsync:config', $context);
}
/**
* Returns edit icons for the page with list of instances.
* @param stdClass $instance
* @return array
*/
public function get_action_icons(stdClass $instance) {
if ($instance->enrol !== 'groupsync') {
throw new coding_exception('invalid enrol instance!');
}
return array();
}
/**
* 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/groupsync/locallib.php");
enrol_groupsync_sync();
}
/**
* 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/groupsync:config', $context);
}
/**
* 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/groupsync/locallib.php");
enrol_groupsync_sync($instance->courseid);
}
/**
* Delete course enrol plugin instance, unenrol all users.
* @param stdClass $instance
* @return void
*/
public function delete_instance($instance) {
global $CFG, $DB;
$name = $this->get_name();
if ($instance->enrol !== $name) {
throw new coding_exception('invalid enrol instance!');
}
// NOTE: We must delete groups manually, there are no user enrolments that would clean them up.
if ($gms = $DB->get_records('groups_members', array('component' => 'enrol_'.$name, 'itemid' => $instance->id))) {
foreach ($gms as $gm) {
groups_remove_member($gm->groupid, $gm->userid);
}
}
parent::delete_instance($instance);
require_once("$CFG->dirroot/enrol/groupsync/locallib.php");
enrol_groupsync_sync($instance->courseid);
}
/**
* 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;
// No restore support, sorry.
$step->set_mapping('enrol', $oldid, 0);
return;
}
/**
* Restore user group membership.
* @param stdClass $instance
* @param int $groupid
* @param int $userid
*/
public function restore_group_member($instance, $groupid, $userid) {
// No restore support, sorry.
return;
}
}
/**
* Prevent removal of enrol roles.
* @param int $itemid
* @param int $groupid
* @param int $userid
* @return bool
*/
function enrol_groupsync_allow_group_member_remove($itemid, $groupid, $userid) {
return false;
}