81c0ede4719ef2a4737e4d649f0644239e50a725
[moodle.git] / enrol / ajax.php
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/>.
17 /**
18  * This file processes AJAX enrolment actions and returns JSON
19  *
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  *
23  * @package    core
24  * @subpackage enrol
25  * @copyright  2010 Sam Hemelryk
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 define('AJAX_SCRIPT', true);
31 require('../config.php');
32 require_once("$CFG->dirroot/enrol/locallib.php");
33 require_once("$CFG->dirroot/enrol/renderer.php");
34 require_once("$CFG->dirroot/group/lib.php");
36 // Must have the sesskey
37 require_sesskey();
38 $id      = required_param('id', PARAM_INT); // course id
39 $action  = required_param('action', PARAM_ACTION);
41 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
43 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
44 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
46 if ($course->id == SITEID) {
47     throw new moodle_exception('invalidcourse');
48 }
50 require_login($course);
51 require_capability('moodle/course:enrolreview', $context);
52 require_sesskey();
54 echo $OUTPUT->header(); // send headers
56 $manager = new course_enrolment_manager($course);
58 $outcome = new stdClass;
59 $outcome->success = true;
60 $outcome->response = new stdClass;
61 $outcome->error = '';
63 switch ($action) {
64     case 'unenrol':
65         $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
66         list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
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');
69         }
70         break;
71     case 'unassign':
72         $role = required_param('role', PARAM_INT);
73         $user = required_param('user', PARAM_INT);
74         if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
75             throw new enrol_ajax_exception('unassignnotpermitted');
76         }
77         break;
79     case 'assign':
80         $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
81         $roleid = required_param('roleid', PARAM_INT);
82         if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
83             throw new enrol_ajax_exception('invalidrole');
84         }
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');
87         }
88         $outcome->response->roleid = $roleid;
89         break;
91     case 'getassignable':
92         $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
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');
98         break;
99     case 'getcohorts':
100         require_capability('moodle/course:enrolconfig', $context);
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);
107         if (!$manager->enrol_cohort($cohortid, $roleid)) {
108             throw new enrol_ajax_exception('errorenrolcohort');
109         }
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);
116         if ($result === false) {
117             throw new enrol_ajax_exception('errorenrolcohortusers');
118         }
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');
124         break;
125     case 'searchusers':
126         $enrolid = required_param('enrolid', PARAM_INT);
127         $search  = optional_param('search', '', PARAM_RAW);
128         $page = optional_param('page', 0, PARAM_INT);
129         $outcome->response = $manager->get_potential_users($enrolid, $search, false, $page);
130         foreach ($outcome->response['users'] as &$user) {
131             $user->picture = $OUTPUT->user_picture($user);
132             $user->fullname = fullname($user);
133         }
134         $outcome->success = true;
135         break;
137     case 'searchotherusers':
138         $search  = optional_param('search', '', PARAM_RAW);
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;
150     case 'enrol':
151         $enrolid = required_param('enrolid', PARAM_INT);
152         $userid = required_param('userid', PARAM_INT);
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         }
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) {
173             $timeend = 0;
174         } else {
175             $timeend = $timestart + ($duration*24*60*60);
176         }
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)) {
182             throw new enrol_ajax_exception('invalidenrolinstance');
183         }
184         $instance = $instances[$enrolid];
185         $plugin = $plugins[$instance->enrol];
186         if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
187             $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
188         } else {
189             throw new enrol_ajax_exception('enrolnotpermitted');
190         }
191         $outcome->success = true;
192         break;
194     default:
195         throw new enrol_ajax_exception('unknowajaxaction');
198 echo json_encode($outcome);
199 die();