Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / cohort / ajax.php
blobe987f87fb372b34c775458b78957f8c3e2497e5b
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 * 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.
23 * @package enrol_cohort
24 * @copyright 2011 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 define('AJAX_SCRIPT', true);
30 require('../../config.php');
31 require_once($CFG->dirroot.'/enrol/locallib.php');
32 require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
33 require_once($CFG->dirroot.'/group/lib.php');
35 // Must have the sesskey.
36 $id = required_param('id', PARAM_INT); // course id
37 $action = required_param('action', PARAM_ALPHANUMEXT);
39 $PAGE->set_url(new moodle_url('/enrol/cohort/ajax.php', array('id'=>$id, 'action'=>$action)));
41 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
42 $context = context_course::instance($course->id, MUST_EXIST);
44 if ($course->id == SITEID) {
45 throw new moodle_exception('invalidcourse');
48 require_login($course);
49 require_capability('moodle/course:enrolreview', $context);
50 require_sesskey();
52 if (!enrol_is_enabled('cohort')) {
53 // This should never happen, no need to invent new error strings.
54 throw new enrol_ajax_exception('errorenrolcohort');
57 echo $OUTPUT->header(); // Send headers.
59 $manager = new course_enrolment_manager($PAGE, $course);
61 $outcome = new stdClass();
62 $outcome->success = true;
63 $outcome->response = new stdClass();
64 $outcome->error = '';
66 switch ($action) {
67 case 'getassignable':
68 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
69 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
70 break;
71 case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
72 $cohortenrol = enrol_get_plugin('cohort');
73 $outcome->response = $cohortenrol->get_config('roleid');
74 break;
75 case 'getcohorts':
76 require_capability('moodle/course:enrolconfig', $context);
77 $offset = optional_param('offset', 0, PARAM_INT);
78 $search = optional_param('search', '', PARAM_RAW);
79 $outcome->response = enrol_cohort_search_cohorts($manager, $offset, 25, $search);
80 // Some browsers reorder collections by key.
81 $outcome->response['cohorts'] = array_values($outcome->response['cohorts']);
82 break;
83 case 'enrolcohort':
84 require_capability('moodle/course:enrolconfig', $context);
85 require_capability('enrol/cohort:config', $context);
86 $roleid = required_param('roleid', PARAM_INT);
87 $cohortid = required_param('cohortid', PARAM_INT);
89 $roles = $manager->get_assignable_roles();
90 if (!enrol_cohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) {
91 throw new enrol_ajax_exception('errorenrolcohort');
93 $enrol = enrol_get_plugin('cohort');
94 $enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid));
95 $trace = new null_progress_trace();
96 enrol_cohort_sync($trace, $manager->get_course()->id);
97 $trace->finished();
98 break;
99 case 'enrolcohortusers':
100 //TODO: this should be moved to enrol_manual, see MDL-35618.
101 require_capability('enrol/manual:enrol', $context);
102 $roleid = required_param('roleid', PARAM_INT);
103 $cohortid = required_param('cohortid', PARAM_INT);
105 $roles = $manager->get_assignable_roles();
106 if (!enrol_cohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) {
107 throw new enrol_ajax_exception('errorenrolcohort');
110 $result = enrol_cohort_enrol_all_users($manager, $cohortid, $roleid);
111 if ($result === false) {
112 throw new enrol_ajax_exception('errorenrolcohortusers');
115 $outcome->success = true;
116 $outcome->response->users = $result;
117 $outcome->response->title = get_string('success');
118 $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
119 $outcome->response->yesLabel = get_string('ok');
120 break;
121 default:
122 throw new enrol_ajax_exception('unknowajaxaction');
125 echo json_encode($outcome);
126 die();