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.
22 * @copyright 2010 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Cohort enrolment plugin implementation.
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class enrol_cohort_plugin
extends enrol_plugin
{
35 * Returns localised name of enrol instance
37 * @param object $instance (null is accepted too)
40 public function get_instance_name($instance) {
43 if (empty($instance)) {
44 $enrol = $this->get_name();
45 return get_string('pluginname', 'enrol_'.$enrol);
47 } else if (empty($instance->name
)) {
48 $enrol = $this->get_name();
49 if ($role = $DB->get_record('role', array('id'=>$instance->roleid
))) {
50 $role = role_get_name($role, get_context_instance(CONTEXT_COURSE
, $instance->courseid
));
51 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($DB->get_field('cohort', 'name', array('id'=>$instance->customint1
))) . ' - ' . $role .')';
53 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($DB->get_field('cohort', 'name', array('id'=>$instance->customint1
))) . ')';
57 return format_string($instance->name
);
62 * Returns link to page which may be used to add new instance of enrolment plugin in course.
63 * @param int $courseid
64 * @return moodle_url page url
66 public function get_newinstance_link($courseid) {
67 if (!$this->can_add_new_instances($courseid)) {
70 // multiple instances supported - multiple parent courses linked
71 return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
75 * Given a courseid this function returns true if the user is able to enrol or configure cohorts
76 * AND there are cohorts that the user can view.
78 * @param int $courseid
81 protected function can_add_new_instances($courseid) {
84 $coursecontext = get_context_instance(CONTEXT_COURSE
, $courseid);
85 if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
88 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
89 $sql = "SELECT id, contextid
91 WHERE contextid $sqlparents
93 $cohorts = $DB->get_records_sql($sql, $params);
94 foreach ($cohorts as $c) {
95 $context = get_context_instance_by_id($c->contextid
);
96 if (has_capability('moodle/cohort:view', $context)) {
105 * Called for all enabled enrol plugins that returned true from is_cron_required().
108 public function cron() {
111 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
116 * Called after updating/inserting course.
118 * @param bool $inserted true if course just inserted
119 * @param object $course
120 * @param object $data form data
123 public function course_updated($inserted, $course, $data) {
124 // It turns out there is no need for cohorts to deal with this hook, see MDL-34870.
128 * Update instance status
130 * @param stdClass $instance
131 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
134 public function update_status($instance, $newstatus) {
137 parent
::update_status($instance, $newstatus);
139 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
140 enrol_cohort_sync($instance->courseid
);
144 * Does this plugin allow manual unenrolment of a specific user?
145 * Yes, but only if user suspended...
147 * @param stdClass $instance course enrol instance
148 * @param stdClass $ue record from user_enrolments table
150 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
152 public function allow_unenrol_user(stdClass
$instance, stdClass
$ue) {
153 if ($ue->status
== ENROL_USER_SUSPENDED
) {
161 * Gets an array of the user enrolment actions
163 * @param course_enrolment_manager $manager
164 * @param stdClass $ue A user enrolment object
165 * @return array An array of user_enrolment_actions
167 public function get_user_enrolment_actions(course_enrolment_manager
$manager, $ue) {
169 $context = $manager->get_context();
170 $instance = $ue->enrolmentinstance
;
171 $params = $manager->get_moodlepage()->url
->params();
172 $params['ue'] = $ue->id
;
173 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/cohort:unenrol', $context)) {
174 $url = new moodle_url('/enrol/unenroluser.php', $params);
175 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id
));
181 * Returns a button to enrol a cohort or its users through the manual enrolment plugin.
183 * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
186 * @param course_enrolment_manager $manager
187 * @return enrol_user_button
189 public function get_manual_enrol_button(course_enrolment_manager
$manager) {
190 $course = $manager->get_course();
191 if (!$this->can_add_new_instances($course->id
)) {
195 $cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id' => $course->id
));
196 $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
197 $button->class .= ' enrol_cohort_plugin';
199 $button->strings_for_js(array(
205 $button->strings_for_js(array(
209 $button->strings_for_js('assignroles', 'role');
210 $button->strings_for_js('cohort', 'cohort');
211 $button->strings_for_js('users', 'moodle');
213 // No point showing this at all if the user cant manually enrol users
214 $hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual');
216 $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
217 $function = 'M.enrol_cohort.quickenrolment.init';
219 'courseid' => $course->id
,
220 'ajaxurl' => '/enrol/cohort/ajax.php',
221 'url' => $manager->get_moodlepage()->url
->out(false),
222 'manualEnrolment' => $hasmanualinstance);
223 $button->require_yui_module($modules, $function, array($arguments));