weekly release 2.1.7+
[moodle.git] / enrol / ajax.php
blob0b344f2a0201949f1b2b9a78e00249a3417f31fe
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.
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
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.
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
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 $id = required_param('id', PARAM_INT); // course id
38 $action = required_param('action', PARAM_ACTION);
40 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
42 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
43 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
45 if ($course->id == SITEID) {
46 throw new moodle_exception('invalidcourse');
49 require_login($course);
50 require_capability('moodle/course:enrolreview', $context);
51 require_sesskey();
53 echo $OUTPUT->header(); // send headers
55 $manager = new course_enrolment_manager($PAGE, $course);
57 $outcome = new stdClass;
58 $outcome->success = true;
59 $outcome->response = new stdClass;
60 $outcome->error = '';
62 switch ($action) {
63 case 'unenrol':
64 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
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)) {
67 throw new enrol_ajax_exception('unenrolnotpermitted');
69 break;
70 case 'unassign':
71 $role = required_param('role', PARAM_INT);
72 $user = required_param('user', PARAM_INT);
73 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
74 throw new enrol_ajax_exception('unassignnotpermitted');
76 break;
77 case 'assign':
78 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
79 $roleid = required_param('roleid', PARAM_INT);
80 if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
81 throw new enrol_ajax_exception('invalidrole');
83 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
84 throw new enrol_ajax_exception('assignnotpermitted');
86 $outcome->response->roleid = $roleid;
87 break;
88 case 'getassignable':
89 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
90 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
91 break;
92 case 'searchotherusers':
93 $search = optional_param('search', '', PARAM_RAW);
94 $page = optional_param('page', 0, PARAM_INT);
95 $outcome->response = $manager->search_other_users($search, false, $page);
96 foreach ($outcome->response['users'] as &$user) {
97 $user->userId = $user->id;
98 $user->picture = $OUTPUT->user_picture($user);
99 $user->fullname = fullname($user);
100 unset($user->id);
102 $outcome->success = true;
103 break;
104 default:
105 throw new enrol_ajax_exception('unknowajaxaction');
108 echo json_encode($outcome);
109 die();