MDL-35776 user ajax: fix sort order in chrome.
[moodle.git] / enrol / manual / ajax.php
CommitLineData
cf855c0b
SH
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/>.
16
17/**
18 * This file processes AJAX enrolment actions and returns JSON for the manual enrolments plugin
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 *
6f6c9e5c 23 * @package enrol_manual
cf855c0b
SH
24 * @copyright 2010 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28define('AJAX_SCRIPT', true);
29
30require('../../config.php');
31require_once($CFG->dirroot.'/enrol/locallib.php');
32require_once($CFG->dirroot.'/group/lib.php');
33
6f6c9e5c 34$id = required_param('id', PARAM_INT); // Course id.
405aca35 35$action = required_param('action', PARAM_ALPHANUMEXT);
cf855c0b
SH
36
37$PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
38
39$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
55bcef29 40$context = context_course::instance($course->id, MUST_EXIST);
cf855c0b
SH
41
42if ($course->id == SITEID) {
43 throw new moodle_exception('invalidcourse');
44}
45
46require_login($course);
47require_capability('moodle/course:enrolreview', $context);
48require_sesskey();
49
6f6c9e5c 50echo $OUTPUT->header(); // Send headers.
cf855c0b 51
076995bf 52$manager = new course_enrolment_manager($PAGE, $course);
cf855c0b
SH
53
54$outcome = new stdClass;
55$outcome->success = true;
56$outcome->response = new stdClass;
57$outcome->error = '';
58
59switch ($action) {
60 case 'getassignable':
61 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
62 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
63 break;
64 case 'searchusers':
65 $enrolid = required_param('enrolid', PARAM_INT);
66 $search = optional_param('search', '', PARAM_RAW);
67 $page = optional_param('page', 0, PARAM_INT);
68 $outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
b849c212 69 $extrafields = get_extra_user_fields($context);
cf855c0b
SH
70 foreach ($outcome->response['users'] as &$user) {
71 $user->picture = $OUTPUT->user_picture($user);
72 $user->fullname = fullname($user);
b849c212 73 $fieldvalues = array();
74 foreach ($extrafields as $field) {
75 $fieldvalues[] = s($user->{$field});
76 unset($user->{$field});
77 }
78 $user->extrafields = implode(', ', $fieldvalues);
cf855c0b
SH
79 }
80 $outcome->success = true;
81 break;
82 case 'enrol':
83 $enrolid = required_param('enrolid', PARAM_INT);
84 $userid = required_param('userid', PARAM_INT);
85
86 $roleid = optional_param('role', null, PARAM_INT);
87 $duration = optional_param('duration', 0, PARAM_INT);
88 $startdate = optional_param('startdate', 0, PARAM_INT);
d4c98cff
SH
89 $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
90
cf855c0b
SH
91 if (empty($roleid)) {
92 $roleid = null;
93 }
94
95 switch($startdate) {
96 case 2:
97 $timestart = $course->startdate;
98 break;
99 case 3:
100 default:
101 $today = time();
102 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
103 $timestart = $today;
104 break;
105 }
106 if ($duration <= 0) {
107 $timeend = 0;
108 } else {
109 $timeend = $timestart + ($duration*24*60*60);
110 }
111
112 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
113 $instances = $manager->get_enrolment_instances();
005e57a2 114 $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins.
cf855c0b
SH
115 if (!array_key_exists($enrolid, $instances)) {
116 throw new enrol_ajax_exception('invalidenrolinstance');
117 }
118 $instance = $instances[$enrolid];
005e57a2
PS
119 if (!isset($plugins[$instance->enrol])) {
120 throw new enrol_ajax_exception('enrolnotpermitted');
121 }
cf855c0b
SH
122 $plugin = $plugins[$instance->enrol];
123 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
124 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
d4c98cff
SH
125 if ($recovergrades) {
126 require_once($CFG->libdir.'/gradelib.php');
127 grade_recover_history_grades($user->id, $instance->courseid);
128 }
cf855c0b
SH
129 } else {
130 throw new enrol_ajax_exception('enrolnotpermitted');
131 }
132 $outcome->success = true;
133 break;
134
135 default:
136 throw new enrol_ajax_exception('unknowajaxaction');
137}
138
6f6c9e5c 139echo json_encode($outcome);