MDL-23187 Generic ratings and comments permissions now appear on course pages, AND...
[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 *
20 * @package moodlecore
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25define('AJAX_SCRIPT', true);
26
27require('../config.php');
28require_once("$CFG->dirroot/enrol/locallib.php");
29require_once("$CFG->dirroot/enrol/renderer.php");
30require_once("$CFG->dirroot/group/lib.php");
31
32// Must have the sesskey
33require_sesskey();
34$id = required_param('id', PARAM_INT); // course id
35$action = required_param('action', PARAM_ACTION);
36
37$PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
38
39$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
40$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
41
42if ($course->id == SITEID) {
43 redirect(new moodle_url('/'));
44}
45
46require_login($course);
47require_capability('moodle/course:enrolreview', $context);
48
49$manager = new course_enrolment_manager($course);
50
51$outcome = new stdClass;
52$outcome->success = false;
53$outcome->response = new stdClass;
54$outcome->error = '';
55
56if (!confirm_sesskey()) {
57 $outcome->error = 'invalidsesskey';
58 echo json_encode($outcome);
59 die();
60}
61
62switch ($action) {
63 case 'unenrol':
a70eb30f 64 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
a3c71984
SH
65 list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
66 if ($instance && $plugin && $plugin->allow_unenrol($instance) && has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) && $manager->unenrol_user($ue)) {
a70eb30f
SH
67 $outcome->success = true;
68 } else {
69 $outcome->error = 'unabletounenrol';
70 }
71 break;
72 case 'unassign':
73 $role = required_param('role', PARAM_INT);
74 $user = required_param('user', PARAM_INT);
a3c71984 75 if (has_capability('moodle/role:assign', $manager->get_context()) && $manager->unassign_role_from_user($user, $role)) {
a70eb30f
SH
76 $outcome->success = true;
77 } else {
78 $outcome->error = 'unabletounassign';
79 }
80 break;
81
82 case 'assign':
a3c71984 83 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
a70eb30f
SH
84 $roleid = required_param('roleid', PARAM_INT);
85
86 if (!is_enrolled($context, $user)) {
87 $outcome->error = 'mustbeenrolled';
88 break; // no roles without enrolments here in this script
89 }
823a807e
SH
90 if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
91 $outcome->error = 'invalidrole';
92 break;
93 }
a70eb30f 94
a3c71984 95 if (has_capability('moodle/role:assign', $manager->get_context()) && $manager->assign_role_to_user($roleid, $user->id)) {
a70eb30f
SH
96 $outcome->success = true;
97 $outcome->response->roleid = $roleid;
98 } else {
99 $outcome->error = 'unabletoassign';
100 }
101 break;
102
103 case 'getassignable':
104 $outcome->success = true;
105 $outcome->response = $manager->get_assignable_roles();
106 break;
2ec702c9
SH
107 case 'getcohorts':
108 require_capability('moodle/course:enrolconfig', $context);
109 $outcome->success = true;
110 $outcome->response = $manager->get_cohorts();
111 break;
112 case 'enrolcohort':
113 require_capability('moodle/course:enrolconfig', $context);
114 $roleid = required_param('roleid', PARAM_INT);
115 $cohortid = required_param('cohortid', PARAM_INT);
116 $outcome->success = $manager->enrol_cohort($cohortid, $roleid);
117 break;
118 case 'enrolcohortusers':
119 require_capability('moodle/course:enrolconfig', $context);
120 $roleid = required_param('roleid', PARAM_INT);
121 $cohortid = required_param('cohortid', PARAM_INT);
122 $result = $manager->enrol_cohort_users($cohortid, $roleid);
123 if ($result !== false) {
124 $outcome->success = true;
125 $outcome->response->users = $result;
126 $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
127 }
128 break;
a70eb30f
SH
129 case 'searchusers':
130 $enrolid = required_param('enrolid', PARAM_INT);
131 $search = optional_param('search', '', PARAM_CLEAN);
132 $page = optional_param('page', 0, PARAM_INT);
133 $outcome->response = $manager->get_potential_users($enrolid, $search, false, $page);
134 foreach ($outcome->response['users'] as &$user) {
135 $user->picture = $OUTPUT->user_picture($user);
136 $user->fullname = fullname($user);
137 }
138 $outcome->success = true;
a70eb30f
SH
139 break;
140
141 case 'enrol':
142 $enrolid = required_param('enrolid', PARAM_INT);
143 $userid = required_param('userid', PARAM_INT);
144
145 $roleid = optional_param('role', null, PARAM_INT);
146 $duration = optional_param('duration', 0, PARAM_INT);
147 $startdate = optional_param('startdate', 0, PARAM_INT);
148 if (empty($roleid)) {
149 $roleid = null;
150 }
151
152 switch($startdate) {
153 case 2:
154 $timestart = $course->startdate;
155 break;
156 case 3:
157 default:
158 $today = time();
159 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
160 $timestart = $today;
161 break;
162 }
163 if ($duration <= 0) {
a70eb30f
SH
164 $timeend = 0;
165 } else {
166 $timeend = $timestart + ($duration*24*60*60);
167 }
168
169 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
170 $instances = $manager->get_enrolment_instances();
171 $plugins = $manager->get_enrolment_plugins();
172 if (!array_key_exists($enrolid, $instances)) {
173 $outcome->error = 'invalidinstance';
174 break;
175 }
176 $instance = $instances[$enrolid];
177 $plugin = $plugins[$instance->enrol];
c41f9200 178 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
a3c71984
SH
179 try {
180 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
181 } catch (Exception $e) {
182 $outcome->error = 'unabletoenrol';
183 break;
184 }
185 } else {
186 $outcome->error = 'unablenotallowed';
a70eb30f
SH
187 break;
188 }
189 $outcome->success = true;
190 break;
191
192 default:
193 $outcome->error = 'unknownaction';
194 break;
195}
196
197echo json_encode($outcome);
198die();