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('t/edit', get_string('edit'), 'core',
122 array('class' => 'smallicon')));
129 * Called for all enabled enrol plugins that returned true from is_cron_required().
132 public function cron() {
135 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
136 $trace = new null_progress_trace();
137 enrol_cohort_sync($trace);
142 * Called after updating/inserting course.
144 * @param bool $inserted true if course just inserted
145 * @param stdClass $course
146 * @param stdClass $data form data
149 public function course_updated($inserted, $course, $data) {
150 // It turns out there is no need for cohorts to deal with this hook, see MDL-34870.
154 * Update instance status
156 * @param stdClass $instance
157 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
160 public function update_status($instance, $newstatus) {
163 parent
::update_status($instance, $newstatus);
165 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
166 $trace = new null_progress_trace();
167 enrol_cohort_sync($trace, $instance->courseid
);
172 * Does this plugin allow manual unenrolment of a specific user?
173 * Yes, but only if user suspended...
175 * @param stdClass $instance course enrol instance
176 * @param stdClass $ue record from user_enrolments table
178 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
180 public function allow_unenrol_user(stdClass
$instance, stdClass
$ue) {
181 if ($ue->status
== ENROL_USER_SUSPENDED
) {
189 * Gets an array of the user enrolment actions.
191 * @param course_enrolment_manager $manager
192 * @param stdClass $ue A user enrolment object
193 * @return array An array of user_enrolment_actions
195 public function get_user_enrolment_actions(course_enrolment_manager
$manager, $ue) {
197 $context = $manager->get_context();
198 $instance = $ue->enrolmentinstance
;
199 $params = $manager->get_moodlepage()->url
->params();
200 $params['ue'] = $ue->id
;
201 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/cohort:unenrol', $context)) {
202 $url = new moodle_url('/enrol/unenroluser.php', $params);
203 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id
));
209 * Returns a button to enrol a cohort or its users through the manual enrolment plugin.
211 * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
214 * @param course_enrolment_manager $manager
215 * @return enrol_user_button
217 public function get_manual_enrol_button(course_enrolment_manager
$manager) {
218 $course = $manager->get_course();
219 if (!$this->can_add_new_instances($course->id
)) {
223 $cohorturl = new moodle_url('/enrol/cohort/edit.php', array('courseid' => $course->id
));
224 $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
225 $button->class .= ' enrol_cohort_plugin';
227 $button->strings_for_js(array(
233 $button->strings_for_js(array(
237 $button->strings_for_js('assignroles', 'role');
238 $button->strings_for_js('cohort', 'cohort');
239 $button->strings_for_js('users', 'moodle');
241 // No point showing this at all if the user cant manually enrol users.
242 $hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual');
244 $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
245 $function = 'M.enrol_cohort.quickenrolment.init';
247 'courseid' => $course->id
,
248 'ajaxurl' => '/enrol/cohort/ajax.php',
249 'url' => $manager->get_moodlepage()->url
->out(false),
250 'manualEnrolment' => $hasmanualinstance);
251 $button->require_yui_module($modules, $function, array($arguments));
257 * Restore instance and map settings.
259 * @param restore_enrolments_structure_step $step
260 * @param stdClass $data
261 * @param stdClass $course
264 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
267 if (!$step->get_task()->is_samesite()) {
268 // No cohort restore from other sites.
269 $step->set_mapping('enrol', $oldid, 0);
273 if (!empty($data->customint2
)) {
274 $data->customint2
= $step->get_mappingid('group', $data->customint2
);
277 if ($data->roleid
and $DB->record_exists('cohort', array('id'=>$data->customint1
))) {
278 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid
, 'customint1'=>$data->customint1
, 'courseid'=>$course->id
, 'enrol'=>$this->get_name()));
280 $instanceid = $instance->id
;
282 $instanceid = $this->add_instance($course, (array)$data);
284 $step->set_mapping('enrol', $oldid, $instanceid);
286 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
287 $trace = new null_progress_trace();
288 enrol_cohort_sync($trace, $course->id
);
291 } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
292 $data->customint1
= 0;
293 $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid
, 'customint1'=>$data->customint1
, 'courseid'=>$course->id
, 'enrol'=>$this->get_name()));
296 $instanceid = $instance->id
;
298 $data->status
= ENROL_INSTANCE_DISABLED
;
299 $instanceid = $this->add_instance($course, (array)$data);
301 $step->set_mapping('enrol', $oldid, $instanceid);
303 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
304 $trace = new null_progress_trace();
305 enrol_cohort_sync($trace, $course->id
);
309 $step->set_mapping('enrol', $oldid, 0);
314 * Restore user enrolment.
316 * @param restore_enrolments_structure_step $step
317 * @param stdClass $data
318 * @param stdClass $instance
319 * @param int $oldinstancestatus
322 public function restore_user_enrolment(restore_enrolments_structure_step
$step, $data, $instance, $userid, $oldinstancestatus) {
325 if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
326 // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
330 // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
331 // but without roles and suspended.
333 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id
, 'userid'=>$userid))) {
334 $this->enrol_user($instance, $userid, null, $data->timestart
, $data->timeend
, ENROL_USER_SUSPENDED
);
339 * Restore user group membership.
340 * @param stdClass $instance
341 * @param int $groupid
344 public function restore_group_member($instance, $groupid, $userid) {
345 // Nothing to do here, the group members are added in $this->restore_group_restored()
351 * Prevent removal of enrol roles.
353 * @param int $groupid
357 function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {