ajax.php 4.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file processes AJAX enrolment actions and returns JSON
 *
20
21
22
 * The general idea behind this file is that any errors should throw exceptions
 * which will be returned and acted upon by the calling AJAX script.
 *
Petr Skoda's avatar
Petr Skoda committed
23
24
 * @package    core
 * @subpackage enrol
25
26
27
28
29
30
31
32
33
34
35
36
37
 * @copyright  2010 Sam Hemelryk
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

define('AJAX_SCRIPT', true);

require('../config.php');
require_once("$CFG->dirroot/enrol/locallib.php");
require_once("$CFG->dirroot/enrol/renderer.php");
require_once("$CFG->dirroot/group/lib.php");

// Must have the sesskey
$id      = required_param('id', PARAM_INT); // course id
38
$action  = required_param('action', PARAM_ALPHANUMEXT);
39
40
41
42

$PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));

$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
43
$context = context_course::instance($course->id, MUST_EXIST);
44
45

if ($course->id == SITEID) {
46
    throw new moodle_exception('invalidcourse');
47
48
49
50
}

require_login($course);
require_capability('moodle/course:enrolreview', $context);
51
require_sesskey();
52

53
54
echo $OUTPUT->header(); // send headers

55
$manager = new course_enrolment_manager($PAGE, $course);
56
57

$outcome = new stdClass;
58
$outcome->success = true;
59
60
61
62
63
64
$outcome->response = new stdClass;
$outcome->error = '';

switch ($action) {
    case 'unenrol':
        $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
65
        list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
66
        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)) {
67
            throw new enrol_ajax_exception('unenrolnotpermitted');
68
69
70
71
72
        }
        break;
    case 'unassign':
        $role = required_param('role', PARAM_INT);
        $user = required_param('user', PARAM_INT);
73
74
        if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
            throw new enrol_ajax_exception('unassignnotpermitted');
75
76
77
        }
        break;
    case 'assign':
78
        $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
79
        $roleid = required_param('roleid', PARAM_INT);
80
        if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
81
            throw new enrol_ajax_exception('invalidrole');
82
        }
83
84
        if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
            throw new enrol_ajax_exception('assignnotpermitted');
85
        }
86
        $outcome->response->roleid = $roleid;
87
88
        break;
    case 'getassignable':
89
        $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
90
91
        $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
        break;
92
    case 'searchotherusers':
93
        $search  = optional_param('search', '', PARAM_RAW);
94
95
96
97
98
99
100
101
102
103
        $page = optional_param('page', 0, PARAM_INT);
        $outcome->response = $manager->search_other_users($search, false, $page);
        foreach ($outcome->response['users'] as &$user) {
            $user->userId = $user->id;
            $user->picture = $OUTPUT->user_picture($user);
            $user->fullname = fullname($user);
            unset($user->id);
        }
        $outcome->success = true;
        break;
104
    default:
105
        throw new enrol_ajax_exception('unknowajaxaction');
106
107
108
}

echo json_encode($outcome);
109
die();