Commit | Line | Data |
---|---|---|
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 | ||
28 | define('AJAX_SCRIPT', true); | |
29 | ||
30 | require('../../config.php'); | |
31 | require_once($CFG->dirroot.'/enrol/locallib.php'); | |
32 | require_once($CFG->dirroot.'/group/lib.php'); | |
b6548192 MG |
33 | require_once($CFG->dirroot.'/enrol/manual/locallib.php'); |
34 | require_once($CFG->dirroot.'/cohort/lib.php'); | |
cf855c0b | 35 | |
6f6c9e5c | 36 | $id = required_param('id', PARAM_INT); // Course id. |
405aca35 | 37 | $action = required_param('action', PARAM_ALPHANUMEXT); |
cf855c0b SH |
38 | |
39 | $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action))); | |
40 | ||
74df2951 | 41 | $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); |
55bcef29 | 42 | $context = context_course::instance($course->id, MUST_EXIST); |
cf855c0b SH |
43 | |
44 | if ($course->id == SITEID) { | |
45 | throw new moodle_exception('invalidcourse'); | |
46 | } | |
47 | ||
48 | require_login($course); | |
49 | require_capability('moodle/course:enrolreview', $context); | |
50 | require_sesskey(); | |
51 | ||
6f6c9e5c | 52 | echo $OUTPUT->header(); // Send headers. |
cf855c0b | 53 | |
076995bf | 54 | $manager = new course_enrolment_manager($PAGE, $course); |
cf855c0b | 55 | |
13bd955e | 56 | $outcome = new stdClass(); |
cf855c0b | 57 | $outcome->success = true; |
13bd955e | 58 | $outcome->response = new stdClass(); |
cf855c0b SH |
59 | $outcome->error = ''; |
60 | ||
e059c033 TH |
61 | $searchanywhere = get_user_preferences('userselector_searchanywhere', false); |
62 | ||
cf855c0b SH |
63 | switch ($action) { |
64 | case 'getassignable': | |
65 | $otheruserroles = optional_param('otherusers', false, PARAM_BOOL); | |
66 | $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true); | |
67 | break; | |
68 | case 'searchusers': | |
69 | $enrolid = required_param('enrolid', PARAM_INT); | |
e059c033 | 70 | $search = optional_param('search', '', PARAM_RAW); |
cf855c0b | 71 | $page = optional_param('page', 0, PARAM_INT); |
4be3ed4b | 72 | $addedenrollment = optional_param('enrolcount', 0, PARAM_INT); |
62a973ce | 73 | $perpage = optional_param('perpage', 25, PARAM_INT); // This value is hard-coded to 25 in quickenrolment.js |
4be3ed4b | 74 | $outcome->response = $manager->get_potential_users($enrolid, $search, $searchanywhere, $page, $perpage, $addedenrollment); |
b849c212 | 75 | $extrafields = get_extra_user_fields($context); |
d2f3c5c5 PS |
76 | $useroptions = array(); |
77 | // User is not enrolled yet, either link to site profile or do not link at all. | |
78 | if (has_capability('moodle/user:viewdetails', context_system::instance())) { | |
79 | $useroptions['courseid'] = SITEID; | |
80 | } else { | |
81 | $useroptions['link'] = false; | |
82 | } | |
cf855c0b | 83 | foreach ($outcome->response['users'] as &$user) { |
d2f3c5c5 | 84 | $user->picture = $OUTPUT->user_picture($user, $useroptions); |
cf855c0b | 85 | $user->fullname = fullname($user); |
b849c212 | 86 | $fieldvalues = array(); |
87 | foreach ($extrafields as $field) { | |
88 | $fieldvalues[] = s($user->{$field}); | |
89 | unset($user->{$field}); | |
90 | } | |
91 | $user->extrafields = implode(', ', $fieldvalues); | |
cf855c0b | 92 | } |
13bd955e TH |
93 | // Chrome will display users in the order of the array keys, so we need |
94 | // to ensure that the results ordered array keys. Fortunately, the JavaScript | |
95 | // does not care what the array keys are. It uses user.id where necessary. | |
96 | $outcome->response['users'] = array_values($outcome->response['users']); | |
cf855c0b SH |
97 | $outcome->success = true; |
98 | break; | |
b6548192 MG |
99 | case 'searchcohorts': |
100 | $enrolid = required_param('enrolid', PARAM_INT); | |
101 | $search = optional_param('search', '', PARAM_RAW); | |
102 | $page = optional_param('page', 0, PARAM_INT); | |
103 | $addedenrollment = optional_param('enrolcount', 0, PARAM_INT); | |
104 | $perpage = optional_param('perpage', 25, PARAM_INT); // This value is hard-coded to 25 in quickenrolment.js | |
105 | $outcome->response = enrol_manual_get_potential_cohorts($context, $enrolid, $search, $page, $perpage, $addedenrollment); | |
106 | $outcome->success = true; | |
107 | break; | |
cf855c0b SH |
108 | case 'enrol': |
109 | $enrolid = required_param('enrolid', PARAM_INT); | |
b6548192 MG |
110 | $cohort = $user = null; |
111 | $cohortid = optional_param('cohortid', 0, PARAM_INT); | |
112 | if (!$cohortid) { | |
113 | $userid = required_param('userid', PARAM_INT); | |
114 | $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); | |
115 | } else { | |
116 | $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); | |
117 | if (!cohort_can_view_cohort($cohort, $context)) { | |
118 | throw new enrol_ajax_exception('invalidenrolinstance'); // TODO error text! | |
119 | } | |
120 | } | |
cf855c0b SH |
121 | |
122 | $roleid = optional_param('role', null, PARAM_INT); | |
123 | $duration = optional_param('duration', 0, PARAM_INT); | |
124 | $startdate = optional_param('startdate', 0, PARAM_INT); | |
d4c98cff SH |
125 | $recovergrades = optional_param('recovergrades', 0, PARAM_INT); |
126 | ||
cf855c0b SH |
127 | if (empty($roleid)) { |
128 | $roleid = null; | |
129 | } | |
130 | ||
131 | switch($startdate) { | |
132 | case 2: | |
133 | $timestart = $course->startdate; | |
134 | break; | |
135 | case 3: | |
136 | default: | |
137 | $today = time(); | |
138 | $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); | |
139 | $timestart = $today; | |
140 | break; | |
141 | } | |
142 | if ($duration <= 0) { | |
143 | $timeend = 0; | |
144 | } else { | |
145 | $timeend = $timestart + ($duration*24*60*60); | |
146 | } | |
147 | ||
cf855c0b | 148 | $instances = $manager->get_enrolment_instances(); |
005e57a2 | 149 | $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins. |
cf855c0b SH |
150 | if (!array_key_exists($enrolid, $instances)) { |
151 | throw new enrol_ajax_exception('invalidenrolinstance'); | |
152 | } | |
153 | $instance = $instances[$enrolid]; | |
005e57a2 PS |
154 | if (!isset($plugins[$instance->enrol])) { |
155 | throw new enrol_ajax_exception('enrolnotpermitted'); | |
156 | } | |
cf855c0b SH |
157 | $plugin = $plugins[$instance->enrol]; |
158 | if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) { | |
b6548192 MG |
159 | if ($user) { |
160 | $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend, null, $recovergrades); | |
161 | } else { | |
162 | $plugin->enrol_cohort($instance, $cohort->id, $roleid, $timestart, $timeend, null, $recovergrades); | |
163 | } | |
cf855c0b SH |
164 | } else { |
165 | throw new enrol_ajax_exception('enrolnotpermitted'); | |
166 | } | |
167 | $outcome->success = true; | |
168 | break; | |
169 | ||
170 | default: | |
171 | throw new enrol_ajax_exception('unknowajaxaction'); | |
172 | } | |
173 | ||
6f6c9e5c | 174 | echo json_encode($outcome); |