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 * Cohort enrolment plugin.
20 * @package enrol_cohort
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 * Cohort enrolment plugin implementation.
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class enrol_cohort_plugin
extends enrol_plugin
{
34 * Returns localised name of enrol instance.
36 * @param stdClass $instance (null is accepted too)
39 public function get_instance_name($instance) {
42 if (empty($instance)) {
43 $enrol = $this->get_name();
44 return get_string('pluginname', 'enrol_'.$enrol);
46 } else if (empty($instance->name
)) {
47 $enrol = $this->get_name();
48 $cohort = $DB->get_record('cohort', array('id'=>$instance->customint1
));
49 $cohortname = format_string($cohort->name
, true, array('context'=>context
::instance_by_id($cohort->contextid
)));
50 if ($role = $DB->get_record('role', array('id'=>$instance->roleid
))) {
51 $role = role_get_name($role, context_course
::instance($instance->courseid
, IGNORE_MISSING
));
52 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ' - ' . $role .')';
54 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ')';
58 return format_string($instance->name
, true, array('context'=>context_course
::instance($instance->courseid
)));
63 * Returns link to page which may be used to add new instance of enrolment plugin in course.
64 * @param int $courseid
65 * @return moodle_url page url
67 public function get_newinstance_link($courseid) {
68 if (!$this->can_add_new_instances($courseid)) {
71 // Multiple instances supported - multiple parent courses linked.
72 return new moodle_url('/enrol/cohort/edit.php', array('courseid'=>$courseid));
76 * Given a courseid this function returns true if the user is able to enrol or configure cohorts.
77 * AND there are cohorts that the user can view.
79 * @param int $courseid
82 protected function can_add_new_instances($courseid) {
85 $coursecontext = context_course
::instance($courseid);
86 if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
89 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
90 $sql = "SELECT id, contextid
92 WHERE contextid $sqlparents
94 $cohorts = $DB->get_records_sql($sql, $params);
95 foreach ($cohorts as $c) {
96 $context = context
::instance_by_id($c->contextid
);
97 if (has_capability('moodle/cohort:view', $context)) {
105 * Returns edit icons for the page with list of instances.
106 * @param stdClass $instance
109 public function get_action_icons(stdClass
$instance) {
112 if ($instance->enrol
!== 'cohort') {
113 throw new coding_exception('invalid enrol instance!');
115 $context = context_course
::instance($instance->courseid
);
119 if (has_capability('enrol/cohort:config', $context)) {
120 $editlink = new moodle_url("/enrol/cohort/edit.php", array('courseid'=>$instance->courseid
, 'id'=>$instance->id
));
121 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
128 * Called for all enabled enrol plugins that returned true from is_cron_required().
131 public function cron() {
134 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
139 * Called after updating/inserting course.
141 * @param bool $inserted true if course just inserted
142 * @param stdClass $course
143 * @param stdClass $data form data
146 public function course_updated($inserted, $course, $data) {
147 // It turns out there is no need for cohorts to deal with this hook, see MDL-34870.
151 * Update instance status
153 * @param stdClass $instance
154 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
157 public function update_status($instance, $newstatus) {
160 parent
::update_status($instance, $newstatus);
162 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
163 enrol_cohort_sync($instance->courseid
);
167 * Does this plugin allow manual unenrolment of a specific user?
168 * Yes, but only if user suspended...
170 * @param stdClass $instance course enrol instance
171 * @param stdClass $ue record from user_enrolments table
173 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
175 public function allow_unenrol_user(stdClass
$instance, stdClass
$ue) {
176 if ($ue->status
== ENROL_USER_SUSPENDED
) {
184 * Gets an array of the user enrolment actions.
186 * @param course_enrolment_manager $manager
187 * @param stdClass $ue A user enrolment object
188 * @return array An array of user_enrolment_actions
190 public function get_user_enrolment_actions(course_enrolment_manager
$manager, $ue) {
192 $context = $manager->get_context();
193 $instance = $ue->enrolmentinstance
;
194 $params = $manager->get_moodlepage()->url
->params();
195 $params['ue'] = $ue->id
;
196 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/cohort:unenrol', $context)) {
197 $url = new moodle_url('/enrol/unenroluser.php', $params);
198 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id
));
204 * Returns a button to enrol a cohort or its users through the manual enrolment plugin.
206 * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
209 * @param course_enrolment_manager $manager
210 * @return enrol_user_button
212 public function get_manual_enrol_button(course_enrolment_manager
$manager) {
213 $course = $manager->get_course();
214 if (!$this->can_add_new_instances($course->id
)) {
218 $cohorturl = new moodle_url('/enrol/cohort/edit.php', array('courseid' => $course->id
));
219 $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
220 $button->class .= ' enrol_cohort_plugin';
222 $button->strings_for_js(array(
228 $button->strings_for_js(array(
232 $button->strings_for_js('assignroles', 'role');
233 $button->strings_for_js('cohort', 'cohort');
234 $button->strings_for_js('users', 'moodle');
236 // No point showing this at all if the user cant manually enrol users.
237 $hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual');
239 $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
240 $function = 'M.enrol_cohort.quickenrolment.init';
242 'courseid' => $course->id
,
243 'ajaxurl' => '/enrol/cohort/ajax.php',
244 'url' => $manager->get_moodlepage()->url
->out(false),
245 'manualEnrolment' => $hasmanualinstance);
246 $button->require_yui_module($modules, $function, array($arguments));
252 * Restore instance and map settings.
254 * @param restore_enrolments_structure_step $step
255 * @param stdClass $data
256 * @param stdClass $course
259 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
262 if (!$step->get_task()->is_samesite()) {
263 // No cohort restore from other sites.
264 $step->set_mapping('enrol', $oldid, 0);
268 if (!empty($data->customint2
)) {
269 $data->customint2
= $step->get_mappingid('group', $data->customint2
);
272 if ($data->roleid
and $DB->record_exists('cohort', array('id'=>$data->customint1
))) {
273 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid
, 'customint1'=>$data->customint1
, 'courseid'=>$course->id
, 'enrol'=>$this->get_name()));
275 $instanceid = $instance->id
;
277 $instanceid = $this->add_instance($course, (array)$data);
279 $step->set_mapping('enrol', $oldid, $instanceid);
281 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
282 enrol_cohort_sync($course->id
, false);
284 } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
285 $data->customint1
= 0;
286 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid
, 'customint1'=>$data->customint1
, 'courseid'=>$course->id
, 'enrol'=>$this->get_name()));
289 $instanceid = $instance->id
;
291 $data->status
= ENROL_INSTANCE_DISABLED
;
292 $instanceid = $this->add_instance($course, (array)$data);
294 $step->set_mapping('enrol', $oldid, $instanceid);
296 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
297 enrol_cohort_sync($course->id
, false);
300 $step->set_mapping('enrol', $oldid, 0);
305 * Restore user enrolment.
307 * @param restore_enrolments_structure_step $step
308 * @param stdClass $data
309 * @param stdClass $instance
310 * @param int $oldinstancestatus
313 public function restore_user_enrolment(restore_enrolments_structure_step
$step, $data, $instance, $userid, $oldinstancestatus) {
316 if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
317 // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
321 // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
322 // but without roles and suspended.
324 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id
, 'userid'=>$userid))) {
325 $this->enrol_user($instance, $userid, null, $data->timestart
, $data->timeend
, ENROL_USER_SUSPENDED
);
330 * Restore user group membership.
331 * @param stdClass $instance
332 * @param int $groupid
335 public function restore_group_member($instance, $groupid, $userid) {
336 // Nothing to do here, the group members are added in $this->restore_group_restored()
342 * Prevent removal of enrol roles.
344 * @param int $groupid
348 function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {