MDL-37566 AJAX Fix issues with minimum height and centring
[moodle.git] / enrol / cohort / lib.php
blob56dc5c9b0bf109c2e54bdae54afa77e6a3fd8488
1 <?php
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.
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/>.
17 /**
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();
27 /**
28 * Cohort enrolment plugin implementation.
29 * @author Petr Skoda
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class enrol_cohort_plugin extends enrol_plugin {
33 /**
34 * Returns localised name of enrol instance.
36 * @param stdClass $instance (null is accepted too)
37 * @return string
39 public function get_instance_name($instance) {
40 global $DB;
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 .')';
53 } else {
54 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ')';
57 } else {
58 return format_string($instance->name, true, array('context'=>context_course::instance($instance->courseid)));
62 /**
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)) {
69 return NULL;
71 // Multiple instances supported - multiple parent courses linked.
72 return new moodle_url('/enrol/cohort/edit.php', array('courseid'=>$courseid));
75 /**
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
80 * @return bool
82 protected function can_add_new_instances($courseid) {
83 global $DB;
85 $coursecontext = context_course::instance($courseid);
86 if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
87 return false;
89 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
90 $sql = "SELECT id, contextid
91 FROM {cohort}
92 WHERE contextid $sqlparents
93 ORDER BY name ASC";
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)) {
98 return true;
101 return false;
105 * Returns edit icons for the page with list of instances.
106 * @param stdClass $instance
107 * @return array
109 public function get_action_icons(stdClass $instance) {
110 global $OUTPUT;
112 if ($instance->enrol !== 'cohort') {
113 throw new coding_exception('invalid enrol instance!');
115 $context = context_course::instance($instance->courseid);
117 $icons = array();
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')));
124 return $icons;
128 * Called for all enabled enrol plugins that returned true from is_cron_required().
129 * @return void
131 public function cron() {
132 global $CFG;
134 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
135 enrol_cohort_sync();
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
144 * @return void
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
155 * @return void
157 public function update_status($instance, $newstatus) {
158 global $CFG;
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) {
177 return true;
180 return false;
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) {
191 $actions = array();
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));
200 return $actions;
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
207 * via AJAX.
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)) {
215 return false;
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(
223 'enrol',
224 'synced',
225 'enrolcohort',
226 'enrolcohortusers',
227 ), 'enrol');
228 $button->strings_for_js(array(
229 'ajaxmore',
230 'cohortsearch',
231 ), 'enrol_cohort');
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';
241 $arguments = array(
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));
248 return $button;
252 * Restore instance and map settings.
254 * @param restore_enrolments_structure_step $step
255 * @param stdClass $data
256 * @param stdClass $course
257 * @param int $oldid
259 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
260 global $DB, $CFG;
262 if (!$step->get_task()->is_samesite()) {
263 // No cohort restore from other sites.
264 $step->set_mapping('enrol', $oldid, 0);
265 return;
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()));
274 if ($instance) {
275 $instanceid = $instance->id;
276 } else {
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()));
288 if ($instance) {
289 $instanceid = $instance->id;
290 } else {
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);
299 } else {
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
311 * @param int $userid
313 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
314 global $DB;
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.
318 return;
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
333 * @param int $userid
335 public function restore_group_member($instance, $groupid, $userid) {
336 // Nothing to do here, the group members are added in $this->restore_group_restored()
337 return;
342 * Prevent removal of enrol roles.
343 * @param int $itemid
344 * @param int $groupid
345 * @param int $userid
346 * @return bool
348 function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
349 return false;