2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Meta course enrolment plugin.
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * ENROL_META_CREATE_GROUP constant for automatically creating a group for a meta course.
30 define('ENROL_META_CREATE_GROUP', -1);
33 * Meta course enrolment plugin.
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class enrol_meta_plugin extends enrol_plugin {
40 * Returns localised name of enrol instance
42 * @param stdClass $instance (null is accepted too)
45 public function get_instance_name($instance) {
48 if (empty($instance)) {
49 $enrol = $this->get_name();
50 return get_string('pluginname', 'enrol_'.$enrol);
51 } else if (empty($instance->name)) {
52 $enrol = $this->get_name();
53 $course = $DB->get_record('course', array('id'=>$instance->customint1));
55 $coursename = format_string(get_course_display_name_for_list($course));
57 // Use course id, if course is deleted.
58 $coursename = $instance->customint1;
60 return get_string('pluginname', 'enrol_' . $enrol) . ' (' . $coursename . ')';
62 return format_string($instance->name);
67 * Returns true if we can add a new instance to this course.
69 * @param int $courseid
72 public function can_add_instance($courseid) {
73 $context = context_course::instance($courseid, MUST_EXIST);
74 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/meta:config', $context)) {
77 // Multiple instances supported - multiple parent courses linked.
82 * Does this plugin allow manual unenrolment of a specific user?
83 * Yes, but only if user suspended...
85 * @param stdClass $instance course enrol instance
86 * @param stdClass $ue record from user_enrolments table
88 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
90 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
91 if ($ue->status == ENROL_USER_SUSPENDED) {
99 * Gets an array of the user enrolment actions
101 * @param course_enrolment_manager $manager
102 * @param stdClass $ue A user enrolment object
103 * @return array An array of user_enrolment_actions
105 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
107 $context = $manager->get_context();
108 $instance = $ue->enrolmentinstance;
109 $params = $manager->get_moodlepage()->url->params();
110 $params['ue'] = $ue->id;
111 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/meta:unenrol', $context)) {
112 $url = new moodle_url('/enrol/unenroluser.php', $params);
113 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
119 * Called after updating/inserting course.
121 * @param bool $inserted true if course just inserted
122 * @param stdClass $course
123 * @param stdClass $data form data
126 public function course_updated($inserted, $course, $data) {
127 // Meta sync updates are slow, if enrolments get out of sync teacher will have to wait till next cron.
128 // We should probably add some sync button to the course enrol methods overview page.
132 * Add new instance of enrol plugin.
133 * @param object $course
134 * @param array $fields instance fields
135 * @return int id of last instance, null if can not be created
137 public function add_instance($course, array $fields = null) {
140 require_once("$CFG->dirroot/enrol/meta/locallib.php");
142 // Support creating multiple at once.
143 if (is_array($fields['customint1'])) {
144 $courses = array_unique($fields['customint1']);
146 $courses = array($fields['customint1']);
148 foreach ($courses as $courseid) {
149 if (!empty($fields['customint2']) && $fields['customint2'] == ENROL_META_CREATE_GROUP) {
150 $context = context_course::instance($course->id);
151 require_capability('moodle/course:managegroups', $context);
152 $groupid = enrol_meta_create_new_group($course->id, $courseid);
153 $fields['customint2'] = $groupid;
156 $fields['customint1'] = $courseid;
157 $result = parent::add_instance($course, $fields);
160 enrol_meta_sync($course->id);
166 * Update instance of enrol plugin.
167 * @param stdClass $instance
168 * @param stdClass $data modified instance fields
171 public function update_instance($instance, $data) {
174 require_once("$CFG->dirroot/enrol/meta/locallib.php");
176 if (!empty($data->customint2) && $data->customint2 == ENROL_META_CREATE_GROUP) {
177 $context = context_course::instance($instance->courseid);
178 require_capability('moodle/course:managegroups', $context);
179 $groupid = enrol_meta_create_new_group($instance->courseid, $data->customint1);
180 $data->customint2 = $groupid;
183 $result = parent::update_instance($instance, $data);
185 enrol_meta_sync($instance->courseid);
191 * Update instance status
193 * @param stdClass $instance
194 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
197 public function update_status($instance, $newstatus) {
200 parent::update_status($instance, $newstatus);
202 require_once("$CFG->dirroot/enrol/meta/locallib.php");
203 enrol_meta_sync($instance->courseid);
207 * Called for all enabled enrol plugins that returned true from is_cron_required().
210 public function cron() {
213 require_once("$CFG->dirroot/enrol/meta/locallib.php");
218 * Is it possible to delete enrol instance via standard UI?
220 * @param stdClass $instance
223 public function can_delete_instance($instance) {
224 $context = context_course::instance($instance->courseid);
225 return has_capability('enrol/meta:config', $context);
229 * Is it possible to hide/show enrol instance via standard UI?
231 * @param stdClass $instance
234 public function can_hide_show_instance($instance) {
235 $context = context_course::instance($instance->courseid);
236 return has_capability('enrol/meta:config', $context);
240 * We are a good plugin and don't invent our own UI/validation code path.
244 public function use_standard_editing_ui() {
249 * Return an array of valid options for the courses.
251 * @param stdClass $instance
252 * @param context $coursecontext
255 protected function get_course_options($instance, $coursecontext) {
259 $where = 'WHERE c.id = :courseid';
260 $params = array('courseid' => $instance->customint1);
265 $instanceparams = array('enrol' => 'meta', 'courseid' => $instance->courseid);
266 $existing = $DB->get_records('enrol', $instanceparams, '', 'customint1, id');
269 // TODO: this has to be done via ajax or else it will fail very badly on large sites!
271 $select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
272 $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
274 $sortorder = 'c.' . $this->get_config('coursesort', 'sortorder') . ' ASC';
276 $sql = "SELECT c.id, c.fullname, c.shortname, c.visible $select FROM {course} c $join $where ORDER BY $sortorder";
277 $rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE) + $params);
278 foreach ($rs as $c) {
279 if ($c->id == SITEID or $c->id == $instance->courseid or isset($existing[$c->id])) {
282 context_helper::preload_from_record($c);
283 $coursecontext = context_course::instance($c->id);
284 if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
287 if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
290 $courses[$c->id] = $coursecontext->get_context_name(false);
297 * Return an array of valid options for the groups.
299 * @param context $coursecontext
302 protected function get_group_options($coursecontext) {
303 $groups = array(0 => get_string('none'));
304 $courseid = $coursecontext->instanceid;
305 if (has_capability('moodle/course:managegroups', $coursecontext)) {
306 $groups[ENROL_META_CREATE_GROUP] = get_string('creategroup', 'enrol_meta');
308 foreach (groups_get_all_groups($courseid) as $group) {
309 $groups[$group->id] = format_string($group->name, true, array('context' => $coursecontext));
315 * Add elements to the edit instance form.
317 * @param stdClass $instance
318 * @param MoodleQuickForm $mform
319 * @param context $coursecontext
322 public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecontext) {
325 $groups = $this->get_group_options($coursecontext);
326 $existing = $DB->get_records('enrol', array('enrol' => 'meta', 'courseid' => $coursecontext->instanceid), '', 'customint1, id');
328 $excludelist = array($coursecontext->instanceid);
329 foreach ($existing as $existinginstance) {
330 $excludelist[] = $existinginstance->customint1;
334 'requiredcapabilities' => array('enrol/meta:selectaslinked'),
336 'exclude' => $excludelist
338 $mform->addElement('course', 'customint1', get_string('linkedcourse', 'enrol_meta'), $options);
339 $mform->addRule('customint1', get_string('required'), 'required', null, 'client');
340 if (!empty($instance->id)) {
341 $mform->freeze('customint1');
344 $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_meta'), $groups);
348 * Perform custom validation of the data used to edit the instance.
350 * @param array $data array of ("fieldname"=>value) of submitted data
351 * @param array $files array of uploaded files "element_name"=>tmp_file_path
352 * @param object $instance The instance loaded from the DB
353 * @param context $context The context of the instance we are editing
354 * @return array of "element_name"=>"error_description" if there are errors,
355 * or an empty array if everything is OK.
358 public function edit_instance_validation($data, $files, $instance, $context) {
361 $thiscourseid = $context->instanceid;
364 if (!empty($data['customint1'])) {
365 foreach ($data['customint1'] as $courseid) {
366 $c = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
367 $coursecontext = context_course::instance($c->id);
368 $existing = $DB->get_records('enrol', array('enrol' => 'meta', 'courseid' => $thiscourseid), '', 'customint1, id');
369 if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
370 $errors['customint1'] = get_string('error');
371 } else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
372 $errors['customint1'] = get_string('error');
373 } else if ($c->id == SITEID or $c->id == $thiscourseid or isset($existing[$c->id])) {
374 $errors['customint1'] = get_string('error');
378 $errors['customint1'] = get_string('required');
381 $validgroups = array_keys($this->get_group_options($context));
384 'customint2' => $validgroups
386 $typeerrors = $this->validate_param_types($data, $tovalidate);
387 $errors = array_merge($errors, $typeerrors);
394 * Restore instance and map settings.
396 * @param restore_enrolments_structure_step $step
397 * @param stdClass $data
398 * @param stdClass $course
401 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
404 if (!$step->get_task()->is_samesite()) {
405 // No meta restore from other sites.
406 $step->set_mapping('enrol', $oldid, 0);
410 if (!empty($data->customint2)) {
411 $data->customint2 = $step->get_mappingid('group', $data->customint2);
414 if ($DB->record_exists('course', array('id' => $data->customint1))) {
415 $instance = $DB->get_record('enrol', array('roleid' => $data->roleid, 'customint1' => $data->customint1,
416 'courseid' => $course->id, 'enrol' => $this->get_name()));
418 $instanceid = $instance->id;
420 $instanceid = $this->add_instance($course, (array)$data);
422 $step->set_mapping('enrol', $oldid, $instanceid);
424 require_once("$CFG->dirroot/enrol/meta/locallib.php");
425 enrol_meta_sync($data->customint1);
428 $step->set_mapping('enrol', $oldid, 0);
433 * Restore user enrolment.
435 * @param restore_enrolments_structure_step $step
436 * @param stdClass $data
437 * @param stdClass $instance
439 * @param int $oldinstancestatus
441 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
444 if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) {
445 // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
449 // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
450 // but without roles and suspended.
452 if (!$DB->record_exists('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid))) {
453 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED);
454 if ($instance->customint2) {
455 groups_add_member($instance->customint2, $userid, 'enrol_meta', $instance->id);
461 * Restore user group membership.
462 * @param stdClass $instance
463 * @param int $groupid
466 public function restore_group_member($instance, $groupid, $userid) {
467 // Nothing to do here, the group members are added in $this->restore_group_restored().