Merge branch 'w41_MDL-42047_m26_cohortdel' of https://github.com/skodak/moodle
[moodle.git] / enrol / cohort / lib.php
CommitLineData
df997f84 1<?php
df997f84
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Cohort enrolment plugin.
19 *
e7193380 20 * @package enrol_cohort
465f5088
PS
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
23 */
24
97795859 25defined('MOODLE_INTERNAL') || die();
df997f84
PS
26
27/**
28 * Cohort enrolment plugin implementation.
29 * @author Petr Skoda
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class enrol_cohort_plugin extends enrol_plugin {
33 /**
e7193380 34 * Returns localised name of enrol instance.
df997f84 35 *
e7193380 36 * @param stdClass $instance (null is accepted too)
df997f84
PS
37 * @return string
38 */
39 public function get_instance_name($instance) {
40 global $DB;
41
42 if (empty($instance)) {
43 $enrol = $this->get_name();
44 return get_string('pluginname', 'enrol_'.$enrol);
7cfe6404 45
df997f84
PS
46 } else if (empty($instance->name)) {
47 $enrol = $this->get_name();
7881024e 48 $cohort = $DB->get_record('cohort', array('id'=>$instance->customint1));
48391606
PS
49 if (!$cohort) {
50 return get_string('pluginname', 'enrol_'.$enrol);
51 }
7881024e 52 $cohortname = format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid)));
df997f84 53 if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
55bcef29 54 $role = role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING));
7881024e 55 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ' - ' . $role .')';
df997f84 56 } else {
7881024e 57 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ')';
df997f84
PS
58 }
59
df997f84 60 } else {
7881024e 61 return format_string($instance->name, true, array('context'=>context_course::instance($instance->courseid)));
df997f84
PS
62 }
63 }
64
65 /**
66 * Returns link to page which may be used to add new instance of enrolment plugin in course.
67 * @param int $courseid
68 * @return moodle_url page url
69 */
e25f2466 70 public function get_newinstance_link($courseid) {
b69ca6be
SH
71 if (!$this->can_add_new_instances($courseid)) {
72 return NULL;
73 }
e7193380 74 // Multiple instances supported - multiple parent courses linked.
7881024e 75 return new moodle_url('/enrol/cohort/edit.php', array('courseid'=>$courseid));
b69ca6be
SH
76 }
77
78 /**
e7193380 79 * Given a courseid this function returns true if the user is able to enrol or configure cohorts.
b69ca6be
SH
80 * AND there are cohorts that the user can view.
81 *
82 * @param int $courseid
83 * @return bool
84 */
85 protected function can_add_new_instances($courseid) {
df997f84
PS
86 global $DB;
87
55bcef29 88 $coursecontext = context_course::instance($courseid);
465f5088 89 if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
b69ca6be 90 return false;
df997f84 91 }
8e8891b7 92 list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
f6fdf620
PS
93 $sql = "SELECT id, contextid
94 FROM {cohort}
95 WHERE contextid $sqlparents
96 ORDER BY name ASC";
97 $cohorts = $DB->get_records_sql($sql, $params);
f6fdf620 98 foreach ($cohorts as $c) {
d197ea43 99 $context = context::instance_by_id($c->contextid);
f6fdf620 100 if (has_capability('moodle/cohort:view', $context)) {
b69ca6be 101 return true;
f6fdf620
PS
102 }
103 }
b69ca6be 104 return false;
df997f84
PS
105 }
106
7881024e
PS
107 /**
108 * Returns edit icons for the page with list of instances.
109 * @param stdClass $instance
110 * @return array
111 */
112 public function get_action_icons(stdClass $instance) {
113 global $OUTPUT;
114
115 if ($instance->enrol !== 'cohort') {
116 throw new coding_exception('invalid enrol instance!');
117 }
118 $context = context_course::instance($instance->courseid);
119
120 $icons = array();
121
122 if (has_capability('enrol/cohort:config', $context)) {
123 $editlink = new moodle_url("/enrol/cohort/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
982f4bc4 124 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
8ef3d514 125 array('class' => 'iconsmall')));
7881024e
PS
126 }
127
128 return $icons;
129 }
130
df997f84
PS
131 /**
132 * Called for all enabled enrol plugins that returned true from is_cron_required().
133 * @return void
134 */
135 public function cron() {
136 global $CFG;
137
df997f84 138 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
c6116231
PS
139 $trace = new null_progress_trace();
140 enrol_cohort_sync($trace);
141 $trace->finished();
df997f84
PS
142 }
143
144 /**
145 * Called after updating/inserting course.
146 *
147 * @param bool $inserted true if course just inserted
e7193380
PS
148 * @param stdClass $course
149 * @param stdClass $data form data
df997f84
PS
150 * @return void
151 */
152 public function course_updated($inserted, $course, $data) {
d94ef563 153 // It turns out there is no need for cohorts to deal with this hook, see MDL-34870.
df997f84 154 }
b69ca6be 155
7cfe6404
PS
156 /**
157 * Update instance status
158 *
159 * @param stdClass $instance
160 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
161 * @return void
162 */
163 public function update_status($instance, $newstatus) {
164 global $CFG;
165
166 parent::update_status($instance, $newstatus);
167
168 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
c6116231
PS
169 $trace = new null_progress_trace();
170 enrol_cohort_sync($trace, $instance->courseid);
171 $trace->finished();
7cfe6404
PS
172 }
173
174 /**
175 * Does this plugin allow manual unenrolment of a specific user?
176 * Yes, but only if user suspended...
177 *
178 * @param stdClass $instance course enrol instance
179 * @param stdClass $ue record from user_enrolments table
180 *
181 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
182 */
183 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
184 if ($ue->status == ENROL_USER_SUSPENDED) {
185 return true;
186 }
187
188 return false;
189 }
190
191 /**
e7193380 192 * Gets an array of the user enrolment actions.
7cfe6404
PS
193 *
194 * @param course_enrolment_manager $manager
195 * @param stdClass $ue A user enrolment object
196 * @return array An array of user_enrolment_actions
197 */
198 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
199 $actions = array();
200 $context = $manager->get_context();
201 $instance = $ue->enrolmentinstance;
202 $params = $manager->get_moodlepage()->url->params();
203 $params['ue'] = $ue->id;
204 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/cohort:unenrol', $context)) {
205 $url = new moodle_url('/enrol/unenroluser.php', $params);
206 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
207 }
208 return $actions;
209 }
210
b69ca6be
SH
211 /**
212 * Returns a button to enrol a cohort or its users through the manual enrolment plugin.
213 *
214 * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
215 * via AJAX.
216 *
b69ca6be
SH
217 * @param course_enrolment_manager $manager
218 * @return enrol_user_button
219 */
220 public function get_manual_enrol_button(course_enrolment_manager $manager) {
b69ca6be
SH
221 $course = $manager->get_course();
222 if (!$this->can_add_new_instances($course->id)) {
223 return false;
224 }
225
7881024e 226 $cohorturl = new moodle_url('/enrol/cohort/edit.php', array('courseid' => $course->id));
b69ca6be
SH
227 $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
228 $button->class .= ' enrol_cohort_plugin';
229
22a811be
RK
230 $button->strings_for_js(array(
231 'enrol',
232 'synced',
233 'enrolcohort',
234 'enrolcohortusers',
235 ), 'enrol');
236 $button->strings_for_js(array(
237 'ajaxmore',
238 'cohortsearch',
239 ), 'enrol_cohort');
b69ca6be
SH
240 $button->strings_for_js('assignroles', 'role');
241 $button->strings_for_js('cohort', 'cohort');
242 $button->strings_for_js('users', 'moodle');
243
e7193380 244 // No point showing this at all if the user cant manually enrol users.
6df97617 245 $hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual');
b69ca6be
SH
246
247 $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
248 $function = 'M.enrol_cohort.quickenrolment.init';
249 $arguments = array(
250 'courseid' => $course->id,
cf855c0b 251 'ajaxurl' => '/enrol/cohort/ajax.php',
076995bf 252 'url' => $manager->get_moodlepage()->url->out(false),
b69ca6be
SH
253 'manualEnrolment' => $hasmanualinstance);
254 $button->require_yui_module($modules, $function, array($arguments));
255
256 return $button;
257 }
df997f84 258
94335e5a
PS
259 /**
260 * Restore instance and map settings.
261 *
262 * @param restore_enrolments_structure_step $step
263 * @param stdClass $data
264 * @param stdClass $course
265 * @param int $oldid
266 */
267 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
268 global $DB, $CFG;
269
270 if (!$step->get_task()->is_samesite()) {
271 // No cohort restore from other sites.
272 $step->set_mapping('enrol', $oldid, 0);
273 return;
274 }
275
7881024e
PS
276 if (!empty($data->customint2)) {
277 $data->customint2 = $step->get_mappingid('group', $data->customint2);
278 }
279
94335e5a
PS
280 if ($data->roleid and $DB->record_exists('cohort', array('id'=>$data->customint1))) {
281 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
282 if ($instance) {
9549090d 283 $instanceid = $instance->id;
94335e5a
PS
284 } else {
285 $instanceid = $this->add_instance($course, (array)$data);
286 }
287 $step->set_mapping('enrol', $oldid, $instanceid);
288
289 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
c6116231
PS
290 $trace = new null_progress_trace();
291 enrol_cohort_sync($trace, $course->id);
292 $trace->finished();
94335e5a
PS
293
294 } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
295 $data->customint1 = 0;
296 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
297
298 if ($instance) {
9549090d 299 $instanceid = $instance->id;
94335e5a
PS
300 } else {
301 $data->status = ENROL_INSTANCE_DISABLED;
302 $instanceid = $this->add_instance($course, (array)$data);
303 }
304 $step->set_mapping('enrol', $oldid, $instanceid);
df997f84 305
94335e5a 306 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
c6116231
PS
307 $trace = new null_progress_trace();
308 enrol_cohort_sync($trace, $course->id);
309 $trace->finished();
94335e5a
PS
310
311 } else {
312 $step->set_mapping('enrol', $oldid, 0);
313 }
314 }
315
316 /**
317 * Restore user enrolment.
318 *
319 * @param restore_enrolments_structure_step $step
320 * @param stdClass $data
321 * @param stdClass $instance
322 * @param int $oldinstancestatus
323 * @param int $userid
324 */
325 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
326 global $DB;
327
328 if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) {
329 // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
330 return;
331 }
332
333 // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
334 // but without roles and suspended.
335
336 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
337 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED);
338 }
339 }
7881024e
PS
340
341 /**
342 * Restore user group membership.
343 * @param stdClass $instance
344 * @param int $groupid
345 * @param int $userid
346 */
347 public function restore_group_member($instance, $groupid, $userid) {
348 // Nothing to do here, the group members are added in $this->restore_group_restored()
349 return;
350 }
351}
352
353/**
354 * Prevent removal of enrol roles.
355 * @param int $itemid
356 * @param int $groupid
357 * @param int $userid
358 * @return bool
359 */
360function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
361 return false;
94335e5a 362}