MDL-29938 Fix typo bypassing some code + remove misplaced code in deprecated function...
[moodle.git] / enrol / ajax.php
blob0b0ee3131e4900ed002b2bdf54ed97c2089757a7
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_ALPHANUMEXT);
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 = context_course::instance($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 $searchanywhere = get_user_preferences('userselector_searchanywhere', false);
64 switch ($action) {
65 case 'unenrol':
66 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
67 list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
68 if (!$instance || !$plugin || !enrol_is_enabled($instance->enrol) || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
69 throw new enrol_ajax_exception('unenrolnotpermitted');
71 break;
72 case 'unassign':
73 $role = required_param('role', PARAM_INT);
74 $user = required_param('user', PARAM_INT);
75 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
76 throw new enrol_ajax_exception('unassignnotpermitted');
78 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');
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');
88 $outcome->response->roleid = $roleid;
89 break;
90 case 'getassignable':
91 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
92 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
93 break;
94 case 'searchotherusers':
95 $search = optional_param('search', '', PARAM_RAW);
96 $page = optional_param('page', 0, PARAM_INT);
97 $outcome->response = $manager->search_other_users($search, $searchanywhere, $page);
98 $extrafields = get_extra_user_fields($context);
99 foreach ($outcome->response['users'] as &$user) {
100 $user->userId = $user->id;
101 $user->picture = $OUTPUT->user_picture($user);
102 $user->fullname = fullname($user);
103 $fieldvalues = array();
104 foreach ($extrafields as $field) {
105 $fieldvalues[] = s($user->{$field});
106 unset($user->{$field});
108 $user->extrafields = implode(', ', $fieldvalues);
109 unset($user->id);
111 // Chrome will display users in the order of the array keys, so we need
112 // to ensure that the results ordered array keys. Fortunately, the JavaScript
113 // does not care what the array keys are. It uses user.id where necessary.
114 $outcome->response['users'] = array_values($outcome->response['users']);
115 $outcome->success = true;
116 break;
117 default:
118 throw new enrol_ajax_exception('unknowajaxaction');
121 echo json_encode($outcome);
122 die();