enrol MDL-22867 Added functionality to add cohort sycn or preform a one off member...
[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;
2ec702c9
SH
105 case 'getcohorts':
106 require_capability('moodle/course:enrolconfig', $context);
107 $outcome->success = true;
108 $outcome->response = $manager->get_cohorts();
109 break;
110 case 'enrolcohort':
111 require_capability('moodle/course:enrolconfig', $context);
112 $roleid = required_param('roleid', PARAM_INT);
113 $cohortid = required_param('cohortid', PARAM_INT);
114 $outcome->success = $manager->enrol_cohort($cohortid, $roleid);
115 break;
116 case 'enrolcohortusers':
117 require_capability('moodle/course:enrolconfig', $context);
118 $roleid = required_param('roleid', PARAM_INT);
119 $cohortid = required_param('cohortid', PARAM_INT);
120 $result = $manager->enrol_cohort_users($cohortid, $roleid);
121 if ($result !== false) {
122 $outcome->success = true;
123 $outcome->response->users = $result;
124 $outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
125 }
126 break;
a70eb30f
SH
127 case 'searchusers':
128 $enrolid = required_param('enrolid', PARAM_INT);
129 $search = optional_param('search', '', PARAM_CLEAN);
130 $page = optional_param('page', 0, PARAM_INT);
131 $outcome->response = $manager->get_potential_users($enrolid, $search, false, $page);
132 foreach ($outcome->response['users'] as &$user) {
133 $user->picture = $OUTPUT->user_picture($user);
134 $user->fullname = fullname($user);
135 }
136 $outcome->success = true;
137
138 break;
139
140 case 'enrol':
141 $enrolid = required_param('enrolid', PARAM_INT);
142 $userid = required_param('userid', PARAM_INT);
143
144 $roleid = optional_param('role', null, PARAM_INT);
145 $duration = optional_param('duration', 0, PARAM_INT);
146 $startdate = optional_param('startdate', 0, PARAM_INT);
147 if (empty($roleid)) {
148 $roleid = null;
149 }
150
151 switch($startdate) {
152 case 2:
153 $timestart = $course->startdate;
154 break;
155 case 3:
156 default:
157 $today = time();
158 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
159 $timestart = $today;
160 break;
161 }
162 if ($duration <= 0) {
163 $timestart = 0;
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];
178 try {
179 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
180 } catch (Exception $e) {
181 $outcome->error = 'unabletoenrol';
182 break;
183 }
184 $outcome->success = true;
185 break;
186
187 default:
188 $outcome->error = 'unknownaction';
189 break;
190}
191
192echo json_encode($outcome);
193die();