Merge branch 'MDL-72173-39' of git://github.com/andrewnicols/moodle into MOODLE_39_STABLE
[moodle.git] / enrol / ajax.php
blob4134c397280b43d23e414b8489bccd3d1a6336f3
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_enrol
24 * @copyright 2010 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 define('AJAX_SCRIPT', true);
30 require('../config.php');
31 require_once("$CFG->dirroot/enrol/locallib.php");
32 require_once("$CFG->dirroot/enrol/renderer.php");
33 require_once("$CFG->dirroot/group/lib.php");
35 // Must have the sesskey
36 $id = required_param('id', PARAM_INT); // course id
37 $action = required_param('action', PARAM_ALPHANUMEXT);
39 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
41 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
42 $context = context_course::instance($course->id, MUST_EXIST);
44 if ($course->id == SITEID) {
45 throw new moodle_exception('invalidcourse');
48 require_login($course);
49 require_capability('moodle/course:enrolreview', $context);
50 require_sesskey();
52 echo $OUTPUT->header(); // send headers
54 $manager = new course_enrolment_manager($PAGE, $course);
56 $outcome = new stdClass();
57 $outcome->success = true;
58 $outcome->response = new stdClass();
59 $outcome->error = '';
61 $searchanywhere = get_user_preferences('userselector_searchanywhere', false);
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 || !enrol_is_enabled($instance->enrol) || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
68 throw new enrol_ajax_exception('unenrolnotpermitted');
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');
77 break;
78 case 'assign':
79 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
80 $roleid = required_param('roleid', PARAM_INT);
81 if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
82 throw new enrol_ajax_exception('invalidrole');
84 if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
85 throw new enrol_ajax_exception('assignnotpermitted');
87 $outcome->response->roleid = $roleid;
88 break;
89 case 'getassignable':
90 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
91 $outcome->response = $manager->get_assignable_roles_for_json($otheruserroles);
92 break;
93 case 'searchotherusers':
94 $search = optional_param('search', '', PARAM_RAW);
95 $page = optional_param('page', 0, PARAM_INT);
96 $outcome->response = $manager->search_other_users($search, $searchanywhere, $page);
97 $extrafields = get_extra_user_fields($context);
98 $useroptions = array();
99 // User is not enrolled, either link to site profile or do not link at all.
100 if (has_capability('moodle/user:viewdetails', context_system::instance())) {
101 $useroptions['courseid'] = SITEID;
102 } else {
103 $useroptions['link'] = false;
105 foreach ($outcome->response['users'] as &$user) {
106 $user->userId = $user->id;
107 $user->picture = $OUTPUT->user_picture($user, $useroptions);
108 $user->fullname = fullname($user);
109 $fieldvalues = array();
110 foreach ($extrafields as $field) {
111 $fieldvalues[] = s($user->{$field});
112 unset($user->{$field});
114 $user->extrafields = implode(', ', $fieldvalues);
115 unset($user->id);
117 // Chrome will display users in the order of the array keys, so we need
118 // to ensure that the results ordered array keys. Fortunately, the JavaScript
119 // does not care what the array keys are. It uses user.id where necessary.
120 $outcome->response['users'] = array_values($outcome->response['users']);
121 $outcome->success = true;
122 break;
123 default:
124 throw new enrol_ajax_exception('unknowajaxaction');
127 echo json_encode($outcome);
128 die();