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 * This file processes AJAX enrolment actions and returns JSON for the cohort plugin
20 * The general idea behind this file is that any errors should throw exceptions
21 * which will be returned and acted upon by the calling AJAX script.
25 * @copyright 2011 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 define('AJAX_SCRIPT', true);
31 require('../../config.php');
32 require_once($CFG->dirroot
.'/enrol/locallib.php');
33 require_once($CFG->dirroot
.'/enrol/cohort/locallib.php');
34 require_once($CFG->dirroot
.'/group/lib.php');
36 // Must have the sesskey
37 $id = required_param('id', PARAM_INT
); // course id
38 $action = required_param('action', PARAM_ACTION
);
40 $PAGE->set_url(new moodle_url('/enrol/cohort/ajax.php', array('id'=>$id, 'action'=>$action)));
42 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST
);
43 $context = get_context_instance(CONTEXT_COURSE
, $course->id
, MUST_EXIST
);
45 if ($course->id
== SITEID
) {
46 throw new moodle_exception('invalidcourse');
49 require_login($course);
50 require_capability('moodle/course:enrolreview', $context);
53 echo $OUTPUT->header(); // send headers
55 $manager = new course_enrolment_manager($PAGE, $course);
57 $outcome = new stdClass
;
58 $outcome->success
= true;
59 $outcome->response
= new stdClass
;
64 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL
);
65 $outcome->response
= array_reverse($manager->get_assignable_roles($otheruserroles), true);
67 case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
68 $cohortenrol = enrol_get_plugin('cohort');
69 $outcome->response
= $cohortenrol->get_config('roleid');
72 require_capability('moodle/course:enrolconfig', $context);
73 $offset = optional_param('offset', 0, PARAM_INT
);
74 $search = optional_param('search', '', PARAM_RAW
);
75 $outcome->response
= enrol_cohort_search_cohorts($manager, $offset, 25, $search);
78 require_capability('moodle/course:enrolconfig', $context);
79 require_capability('enrol/cohort:config', $context);
80 $roleid = required_param('roleid', PARAM_INT
);
81 $cohortid = required_param('cohortid', PARAM_INT
);
83 $roles = $manager->get_assignable_roles();
84 if (!enrol_cohort_can_view_cohort($cohortid) ||
!array_key_exists($roleid, $roles)) {
85 throw new enrol_ajax_exception('errorenrolcohort');
87 $enrol = enrol_get_plugin('cohort');
88 $enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid));
89 enrol_cohort_sync($manager->get_course()->id
);
91 case 'enrolcohortusers':
92 require_capability('enrol/manual:enrol', $context);
93 $roleid = required_param('roleid', PARAM_INT
);
94 $cohortid = required_param('cohortid', PARAM_INT
);
95 $result = enrol_cohort_enrol_all_users($manager, $cohortid, $roleid);
97 $roles = $manager->get_assignable_roles();
98 if (!enrol_cohort_can_view_cohort($cohortid) ||
!array_key_exists($roleid, $roles)) {
99 throw new enrol_ajax_exception('errorenrolcohort');
101 if ($result === false) {
102 throw new enrol_ajax_exception('errorenrolcohortusers');
104 $outcome->success
= true;
105 $outcome->response
->users
= $result;
106 $outcome->response
->title
= get_string('success');
107 $outcome->response
->message
= get_string('enrollednewusers', 'enrol', $result);
108 $outcome->response
->yesLabel
= get_string('ok');
111 throw new enrol_ajax_exception('unknowajaxaction');
114 echo json_encode($outcome);