weekly release 2.4dev
[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 *
23 * @package enrol
24 * @subpackage manual
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29define('AJAX_SCRIPT', true);
30
31require('../../config.php');
32require_once($CFG->dirroot.'/enrol/locallib.php');
33require_once($CFG->dirroot.'/group/lib.php');
34
35// Must have the sesskey
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
41$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
55bcef29 42$context = context_course::instance($course->id, MUST_EXIST);
cf855c0b
SH
43
44if ($course->id == SITEID) {
45 throw new moodle_exception('invalidcourse');
46}
47
48require_login($course);
49require_capability('moodle/course:enrolreview', $context);
50require_sesskey();
51
52echo $OUTPUT->header(); // send headers
53
076995bf 54$manager = new course_enrolment_manager($PAGE, $course);
cf855c0b
SH
55
56$outcome = new stdClass;
57$outcome->success = true;
58$outcome->response = new stdClass;
59$outcome->error = '';
60
61switch ($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 $outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
b849c212 71 $extrafields = get_extra_user_fields($context);
cf855c0b
SH
72 foreach ($outcome->response['users'] as &$user) {
73 $user->picture = $OUTPUT->user_picture($user);
74 $user->fullname = fullname($user);
b849c212 75 $fieldvalues = array();
76 foreach ($extrafields as $field) {
77 $fieldvalues[] = s($user->{$field});
78 unset($user->{$field});
79 }
80 $user->extrafields = implode(', ', $fieldvalues);
cf855c0b
SH
81 }
82 $outcome->success = true;
83 break;
84 case 'enrol':
85 $enrolid = required_param('enrolid', PARAM_INT);
86 $userid = required_param('userid', PARAM_INT);
87
88 $roleid = optional_param('role', null, PARAM_INT);
89 $duration = optional_param('duration', 0, PARAM_INT);
90 $startdate = optional_param('startdate', 0, PARAM_INT);
d4c98cff
SH
91 $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
92
cf855c0b
SH
93 if (empty($roleid)) {
94 $roleid = null;
95 }
96
97 switch($startdate) {
98 case 2:
99 $timestart = $course->startdate;
100 break;
101 case 3:
102 default:
103 $today = time();
104 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
105 $timestart = $today;
106 break;
107 }
108 if ($duration <= 0) {
109 $timeend = 0;
110 } else {
111 $timeend = $timestart + ($duration*24*60*60);
112 }
113
114 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
115 $instances = $manager->get_enrolment_instances();
116 $plugins = $manager->get_enrolment_plugins();
117 if (!array_key_exists($enrolid, $instances)) {
118 throw new enrol_ajax_exception('invalidenrolinstance');
119 }
120 $instance = $instances[$enrolid];
121 $plugin = $plugins[$instance->enrol];
122 if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
123 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
d4c98cff
SH
124 if ($recovergrades) {
125 require_once($CFG->libdir.'/gradelib.php');
126 grade_recover_history_grades($user->id, $instance->courseid);
127 }
cf855c0b
SH
128 } else {
129 throw new enrol_ajax_exception('enrolnotpermitted');
130 }
131 $outcome->success = true;
132 break;
133
134 default:
135 throw new enrol_ajax_exception('unknowajaxaction');
136}
137
75ee207b 138echo json_encode($outcome);