Merge branch 'wip-mdl-57245' of https://github.com/rajeshtaneja/moodle
[moodle.git] / enrol / bulkchange.php
blob1d451ae209806e952828841fb65686450cd463a3
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 * Bulk user enrolment processing.
20 * @package core_enrol
21 * @copyright 2011 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
26 require_once("$CFG->dirroot/enrol/locallib.php");
27 require_once("$CFG->dirroot/enrol/users_forms.php");
28 require_once("$CFG->dirroot/enrol/renderer.php");
29 require_once("$CFG->dirroot/group/lib.php");
31 $id = required_param('id', PARAM_INT); // course id
32 $bulkuserop = required_param('bulkuserop', PARAM_ALPHANUMEXT);
33 $userids = required_param_array('bulkuser', PARAM_INT);
34 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
35 $filter = optional_param('ifilter', 0, PARAM_INT);
37 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
38 $context = context_course::instance($course->id, MUST_EXIST);
40 if ($course->id == SITEID) {
41 redirect(new moodle_url('/'));
44 require_login($course);
45 require_capability('moodle/course:enrolreview', $context);
46 $PAGE->set_pagelayout('admin');
48 $manager = new course_enrolment_manager($PAGE, $course, $filter);
49 $table = new course_enrolment_users_table($manager, $PAGE);
50 $returnurl = new moodle_url('/enrol/users.php', $table->get_combined_url_params());
51 $actionurl = new moodle_url('/enrol/bulkchange.php', $table->get_combined_url_params()+array('bulkuserop' => $bulkuserop));
53 $PAGE->set_url($actionurl);
54 navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
56 $ops = $table->get_bulk_user_enrolment_operations();
57 if (!array_key_exists($bulkuserop, $ops)) {
58 throw new moodle_exception('invalidbulkenrolop');
60 $operation = $ops[$bulkuserop];
62 // Prepare the properties of the form
63 $users = $manager->get_users_enrolments($userids);
65 // Get the form for the bulk operation
66 $mform = $operation->get_form($actionurl, array('users' => $users));
67 // If the mform is false then attempt an immediate process. This may be an immediate action that
68 // doesn't require user input OR confirmation.... who know what but maybe one day
69 if ($mform === false) {
70 if ($operation->process($manager, $users, new stdClass)) {
71 redirect($returnurl);
72 } else {
73 print_error('errorwithbulkoperation', 'enrol');
76 // Check if the bulk operation has been cancelled
77 if ($mform->is_cancelled()) {
78 redirect($returnurl);
80 if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
81 if ($operation->process($manager, $users, $mform->get_data())) {
82 redirect($returnurl);
86 $pagetitle = get_string('bulkuseroperation', 'enrol');
88 $PAGE->set_title($pagetitle);
89 $PAGE->set_heading($pagetitle);
90 echo $OUTPUT->header();
91 echo $OUTPUT->heading($operation->get_title());
92 $mform->display();
93 echo $OUTPUT->footer();