MDL-24276 reversed order of roles
[moodle.git] / enrol / ajax.php
CommitLineData
a70eb30f
SH
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.
13//
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/>.
16
17/**
18 * This file processes AJAX enrolment actions and returns JSON
19 *
6db3eee0
SH
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.
22 *
5379726a
PS
23 * @package core
24 * @subpackage enrol
a70eb30f
SH
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29define('AJAX_SCRIPT', true);
30
31require('../config.php');
32require_once("$CFG->dirroot/enrol/locallib.php");
33require_once("$CFG->dirroot/enrol/renderer.php");
34require_once("$CFG->dirroot/group/lib.php");
35
36// Must have the sesskey
37require_sesskey();
38$id = required_param('id', PARAM_INT); // course id
39$action = required_param('action', PARAM_ACTION);
40
41$PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
42
43$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
44$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
45
46if ($course->id == SITEID) {
6db3eee0 47 throw new moodle_exception('invalidcourse');
a70eb30f
SH
48}
49
50require_login($course);
51require_capability('moodle/course:enrolreview', $context);
6db3eee0 52require_sesskey();
a70eb30f 53
bce08d9a
PS
54echo $OUTPUT->header(); // send headers
55
a70eb30f
SH
56$manager = new course_enrolment_manager($course);
57
58$outcome = new stdClass;
6db3eee0 59$outcome->success = true;
a70eb30f
SH
60$outcome->response = new stdClass;
61$outcome->error = '';
62
a70eb30f
SH
63switch ($action) {
64 case 'unenrol':
a70eb30f 65 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
a3c71984 66 list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
6db3eee0
SH
67 if (!$instance || !$plugin || !$plugin->allow_unenrol($instance) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
68 throw new enrol_ajax_exception('unenrolnotpermitted');
a70eb30f
SH
69 }
70 break;
71 case 'unassign':
72 $role = required_param('role', PARAM_INT);
73 $user = required_param('user', PARAM_INT);
6db3eee0
SH
74 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
75 throw new enrol_ajax_exception('unassignnotpermitted');
a70eb30f
SH
76 }
77 break;
78
79 case 'assign':
a3c71984 80 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
a70eb30f 81 $roleid = required_param('roleid', PARAM_INT);
823a807e 82 if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
6db3eee0 83 throw new enrol_ajax_exception('invalidrole');
823a807e 84 }
6db3eee0
SH
85 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
86 throw new enrol_ajax_exception('assignnotpermitted');
a70eb30f 87 }
6db3eee0 88 $outcome->response->roleid = $roleid;
a70eb30f
SH
89 break;
90
91 case 'getassignable':
9f15347e
SH
92 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
93 $outcome->response = $manager->get_assignable_roles($otheruserroles);
a70eb30f 94 break;
2ec702c9
SH
95 case 'getcohorts':
96 require_capability('moodle/course:enrolconfig', $context);
2ec702c9
SH
97 $outcome->response = $manager->get_cohorts();
98 break;
99 case 'enrolcohort':
100 require_capability('moodle/course:enrolconfig', $context);
101 $roleid = required_param('roleid', PARAM_INT);
102 $cohortid = required_param('cohortid', PARAM_INT);
6db3eee0
SH
103 if (!$manager->enrol_cohort($cohortid, $roleid)) {
104 throw new enrol_ajax_exception('errorenrolcohort');
105 }
2ec702c9
SH
106 break;
107 case 'enrolcohortusers':
108 require_capability('moodle/course:enrolconfig', $context);
109 $roleid = required_param('roleid', PARAM_INT);
110 $cohortid = required_param('cohortid', PARAM_INT);
111 $result = $manager->enrol_cohort_users($cohortid, $roleid);
6db3eee0
SH
112 if ($result === false) {
113 throw new enrol_ajax_exception('errorenrolcohortusers');
2ec702c9 114 }
6db3eee0
SH
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');
2ec702c9 120 break;
a70eb30f
SH
121 case 'searchusers':
122 $enrolid = required_param('enrolid', PARAM_INT);
965ccba9 123 $search = optional_param('search', '', PARAM_RAW);
a70eb30f
SH
124 $page = optional_param('page', 0, PARAM_INT);
125 $outcome->response = $manager->get_potential_users($enrolid, $search, false, $page);
126 foreach ($outcome->response['users'] as &$user) {
127 $user->picture = $OUTPUT->user_picture($user);
128 $user->fullname = fullname($user);
129 }
130 $outcome->success = true;
a70eb30f
SH
131 break;
132
6db3eee0 133 case 'searchotherusers':
965ccba9 134 $search = optional_param('search', '', PARAM_RAW);
6db3eee0
SH
135 $page = optional_param('page', 0, PARAM_INT);
136 $outcome->response = $manager->search_other_users($search, false, $page);
137 foreach ($outcome->response['users'] as &$user) {
138 $user->userId = $user->id;
139 $user->picture = $OUTPUT->user_picture($user);
140 $user->fullname = fullname($user);
141 unset($user->id);
142 }
143 $outcome->success = true;
144 break;
145
a70eb30f
SH
146 case 'enrol':
147 $enrolid = required_param('enrolid', PARAM_INT);
148 $userid = required_param('userid', PARAM_INT);
149
150 $roleid = optional_param('role', null, PARAM_INT);
151 $duration = optional_param('duration', 0, PARAM_INT);
152 $startdate = optional_param('startdate', 0, PARAM_INT);
153 if (empty($roleid)) {
154 $roleid = null;
155 }
156
157 switch($startdate) {
158 case 2:
159 $timestart = $course->startdate;
160 break;
161 case 3:
162 default:
163 $today = time();
164 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
165 $timestart = $today;
166 break;
167 }
168 if ($duration <= 0) {
a70eb30f
SH
169 $timeend = 0;
170 } else {
171 $timeend = $timestart + ($duration*24*60*60);
172 }
173
174 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
175 $instances = $manager->get_enrolment_instances();
176 $plugins = $manager->get_enrolment_plugins();
177 if (!array_key_exists($enrolid, $instances)) {
6db3eee0 178 throw new enrol_ajax_exception('invalidenrolinstance');
a70eb30f
SH
179 }
180 $instance = $instances[$enrolid];
181 $plugin = $plugins[$instance->enrol];
c41f9200 182 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
6db3eee0 183 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
a3c71984 184 } else {
6db3eee0 185 throw new enrol_ajax_exception('enrolnotpermitted');
a70eb30f
SH
186 }
187 $outcome->success = true;
188 break;
189
190 default:
6db3eee0 191 throw new enrol_ajax_exception('unknowajaxaction');
a70eb30f
SH
192}
193
6db3eee0 194header('Content-type: application/json');
a70eb30f 195echo json_encode($outcome);
6db3eee0 196die();