MDL-43175 events: Base event unit tests for JSON encoding / decoding comparison.
[moodle.git] / enrol / manual / ajax.php
blobc1a6f80c9b374008508201afba6cc200d4f6795c
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 for the manual enrolments plugin
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 enrol_manual
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.'/group/lib.php');
34 $id = required_param('id', PARAM_INT); // Course id.
35 $action = required_param('action', PARAM_ALPHANUMEXT);
37 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
39 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
40 $context = context_course::instance($course->id, MUST_EXIST);
42 if ($course->id == SITEID) {
43 throw new moodle_exception('invalidcourse');
46 require_login($course);
47 require_capability('moodle/course:enrolreview', $context);
48 require_sesskey();
50 echo $OUTPUT->header(); // Send headers.
52 $manager = new course_enrolment_manager($PAGE, $course);
54 $outcome = new stdClass();
55 $outcome->success = true;
56 $outcome->response = new stdClass();
57 $outcome->error = '';
59 $searchanywhere = get_user_preferences('userselector_searchanywhere', false);
61 switch ($action) {
62 case 'getassignable':
63 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
64 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
65 break;
66 case 'searchusers':
67 $enrolid = required_param('enrolid', PARAM_INT);
68 $search = optional_param('search', '', PARAM_RAW);
69 $page = optional_param('page', 0, PARAM_INT);
70 $addedenrollment = optional_param('enrolcount', 0, PARAM_INT);
71 $perpage = optional_param('perpage', 25, PARAM_INT); // This value is hard-coded to 25 in quickenrolment.js
72 $outcome->response = $manager->get_potential_users($enrolid, $search, $searchanywhere, $page, $perpage, $addedenrollment);
73 $extrafields = get_extra_user_fields($context);
74 $useroptions = array();
75 // User is not enrolled yet, either link to site profile or do not link at all.
76 if (has_capability('moodle/user:viewdetails', context_system::instance())) {
77 $useroptions['courseid'] = SITEID;
78 } else {
79 $useroptions['link'] = false;
81 foreach ($outcome->response['users'] as &$user) {
82 $user->picture = $OUTPUT->user_picture($user, $useroptions);
83 $user->fullname = fullname($user);
84 $fieldvalues = array();
85 foreach ($extrafields as $field) {
86 $fieldvalues[] = s($user->{$field});
87 unset($user->{$field});
89 $user->extrafields = implode(', ', $fieldvalues);
91 // Chrome will display users in the order of the array keys, so we need
92 // to ensure that the results ordered array keys. Fortunately, the JavaScript
93 // does not care what the array keys are. It uses user.id where necessary.
94 $outcome->response['users'] = array_values($outcome->response['users']);
95 $outcome->success = true;
96 break;
97 case 'enrol':
98 $enrolid = required_param('enrolid', PARAM_INT);
99 $userid = required_param('userid', PARAM_INT);
101 $roleid = optional_param('role', null, PARAM_INT);
102 $duration = optional_param('duration', 0, PARAM_INT);
103 $startdate = optional_param('startdate', 0, PARAM_INT);
104 $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
106 if (empty($roleid)) {
107 $roleid = null;
110 switch($startdate) {
111 case 2:
112 $timestart = $course->startdate;
113 break;
114 case 3:
115 default:
116 $today = time();
117 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
118 $timestart = $today;
119 break;
121 if ($duration <= 0) {
122 $timeend = 0;
123 } else {
124 $timeend = $timestart + ($duration*24*60*60);
127 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
128 $instances = $manager->get_enrolment_instances();
129 $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins.
130 if (!array_key_exists($enrolid, $instances)) {
131 throw new enrol_ajax_exception('invalidenrolinstance');
133 $instance = $instances[$enrolid];
134 if (!isset($plugins[$instance->enrol])) {
135 throw new enrol_ajax_exception('enrolnotpermitted');
137 $plugin = $plugins[$instance->enrol];
138 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
139 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend, null, $recovergrades);
140 } else {
141 throw new enrol_ajax_exception('enrolnotpermitted');
143 $outcome->success = true;
144 break;
146 default:
147 throw new enrol_ajax_exception('unknowajaxaction');
150 echo json_encode($outcome);