Bump to today
[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':
64
65 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
66 if ($manager->unenrol_user($ue)) {
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);
75 if ($manager->unassign_role_from_user($user, $role)) {
76 $outcome->success = true;
77 } else {
78 $outcome->error = 'unabletounassign';
79 }
80 break;
81
82 case 'assign':
83
84 $user = required_param('user', PARAM_INT);
85 $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST);
86 $roleid = required_param('roleid', PARAM_INT);
87
88 if (!is_enrolled($context, $user)) {
89 $outcome->error = 'mustbeenrolled';
90 break; // no roles without enrolments here in this script
91 }
92
93 if ($manager->assign_role_to_user($roleid, $user->id)) {
94 $outcome->success = true;
95 $outcome->response->roleid = $roleid;
96 } else {
97 $outcome->error = 'unabletoassign';
98 }
99 break;
100
101 case 'getassignable':
102 $outcome->success = true;
103 $outcome->response = $manager->get_assignable_roles();
104 break;
105
106 case 'searchusers':
107 $enrolid = required_param('enrolid', PARAM_INT);
108 $search = optional_param('search', '', PARAM_CLEAN);
109 $page = optional_param('page', 0, PARAM_INT);
110 $outcome->response = $manager->get_potential_users($enrolid, $search, false, $page);
111 foreach ($outcome->response['users'] as &$user) {
112 $user->picture = $OUTPUT->user_picture($user);
113 $user->fullname = fullname($user);
114 }
115 $outcome->success = true;
116
117 break;
118
119 case 'enrol':
120 $enrolid = required_param('enrolid', PARAM_INT);
121 $userid = required_param('userid', PARAM_INT);
122
123 $roleid = optional_param('role', null, PARAM_INT);
124 $duration = optional_param('duration', 0, PARAM_INT);
125 $startdate = optional_param('startdate', 0, PARAM_INT);
126 if (empty($roleid)) {
127 $roleid = null;
128 }
129
130 switch($startdate) {
131 case 2:
132 $timestart = $course->startdate;
133 break;
134 case 3:
135 default:
136 $today = time();
137 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
138 $timestart = $today;
139 break;
140 }
141 if ($duration <= 0) {
142 $timestart = 0;
143 $timeend = 0;
144 } else {
145 $timeend = $timestart + ($duration*24*60*60);
146 }
147
148 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
149 $instances = $manager->get_enrolment_instances();
150 $plugins = $manager->get_enrolment_plugins();
151 if (!array_key_exists($enrolid, $instances)) {
152 $outcome->error = 'invalidinstance';
153 break;
154 }
155 $instance = $instances[$enrolid];
156 $plugin = $plugins[$instance->enrol];
157 try {
158 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
159 } catch (Exception $e) {
160 $outcome->error = 'unabletoenrol';
161 break;
162 }
163 $outcome->success = true;
164 break;
165
166 default:
167 $outcome->error = 'unknownaction';
168 break;
169}
170
171echo json_encode($outcome);
172die();