enrol-users MDL-24618 Enrol users search now concats firstname and lastname as reques...
[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 92 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
88cf740d
PS
93 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
94 break;
95 case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
96 $cohortenrol = enrol_get_plugin('cohort');
97 $outcome->response = $cohortenrol->get_config('roleid');
a70eb30f 98 break;
2ec702c9
SH
99 case 'getcohorts':
100 require_capability('moodle/course:enrolconfig', $context);
2ec702c9
SH
101 $outcome->response = $manager->get_cohorts();
102 break;
103 case 'enrolcohort':
104 require_capability('moodle/course:enrolconfig', $context);
105 $roleid = required_param('roleid', PARAM_INT);
106 $cohortid = required_param('cohortid', PARAM_INT);
6db3eee0
SH
107 if (!$manager->enrol_cohort($cohortid, $roleid)) {
108 throw new enrol_ajax_exception('errorenrolcohort');
109 }
2ec702c9
SH
110 break;
111 case 'enrolcohortusers':
112 require_capability('moodle/course:enrolconfig', $context);
113 $roleid = required_param('roleid', PARAM_INT);
114 $cohortid = required_param('cohortid', PARAM_INT);
115 $result = $manager->enrol_cohort_users($cohortid, $roleid);
6db3eee0
SH
116 if ($result === false) {
117 throw new enrol_ajax_exception('errorenrolcohortusers');
2ec702c9 118 }
6db3eee0
SH
119 $outcome->success = true;
120 $outcome->response->users = $result;
121 $outcome->response->title = get_string('success');
122 $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
123 $outcome->response->yesLabel = get_string('ok');
2ec702c9 124 break;
a70eb30f
SH
125 case 'searchusers':
126 $enrolid = required_param('enrolid', PARAM_INT);
965ccba9 127 $search = optional_param('search', '', PARAM_RAW);
a70eb30f 128 $page = optional_param('page', 0, PARAM_INT);
9fc9de91 129 $outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
a70eb30f
SH
130 foreach ($outcome->response['users'] as &$user) {
131 $user->picture = $OUTPUT->user_picture($user);
132 $user->fullname = fullname($user);
133 }
134 $outcome->success = true;
a70eb30f
SH
135 break;
136
6db3eee0 137 case 'searchotherusers':
965ccba9 138 $search = optional_param('search', '', PARAM_RAW);
6db3eee0
SH
139 $page = optional_param('page', 0, PARAM_INT);
140 $outcome->response = $manager->search_other_users($search, false, $page);
141 foreach ($outcome->response['users'] as &$user) {
142 $user->userId = $user->id;
143 $user->picture = $OUTPUT->user_picture($user);
144 $user->fullname = fullname($user);
145 unset($user->id);
146 }
147 $outcome->success = true;
148 break;
149
a70eb30f
SH
150 case 'enrol':
151 $enrolid = required_param('enrolid', PARAM_INT);
152 $userid = required_param('userid', PARAM_INT);
153
154 $roleid = optional_param('role', null, PARAM_INT);
155 $duration = optional_param('duration', 0, PARAM_INT);
156 $startdate = optional_param('startdate', 0, PARAM_INT);
157 if (empty($roleid)) {
158 $roleid = null;
159 }
160
161 switch($startdate) {
162 case 2:
163 $timestart = $course->startdate;
164 break;
165 case 3:
166 default:
167 $today = time();
168 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
169 $timestart = $today;
170 break;
171 }
172 if ($duration <= 0) {
a70eb30f
SH
173 $timeend = 0;
174 } else {
175 $timeend = $timestart + ($duration*24*60*60);
176 }
177
178 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
179 $instances = $manager->get_enrolment_instances();
180 $plugins = $manager->get_enrolment_plugins();
181 if (!array_key_exists($enrolid, $instances)) {
6db3eee0 182 throw new enrol_ajax_exception('invalidenrolinstance');
a70eb30f
SH
183 }
184 $instance = $instances[$enrolid];
185 $plugin = $plugins[$instance->enrol];
c41f9200 186 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
6db3eee0 187 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
a3c71984 188 } else {
6db3eee0 189 throw new enrol_ajax_exception('enrolnotpermitted');
a70eb30f
SH
190 }
191 $outcome->success = true;
192 break;
193
194 default:
6db3eee0 195 throw new enrol_ajax_exception('unknowajaxaction');
a70eb30f
SH
196}
197
198echo json_encode($outcome);
6db3eee0 199die();