Merge branch 'MOODLE_21_STABLE' into install_21_STABLE
[moodle.git] / enrol / bulkchange.php
blobd9d44261bd6cc46116e3391c2a8f7395d1a3c690
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
21 * @subpackage enrol
22 * @copyright 2011 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../config.php');
27 require_once("$CFG->dirroot/enrol/locallib.php");
28 require_once("$CFG->dirroot/enrol/users_forms.php");
29 require_once("$CFG->dirroot/enrol/renderer.php");
30 require_once("$CFG->dirroot/group/lib.php");
32 $id = required_param('id', PARAM_INT); // course id
33 $bulkuserop = required_param('bulkuserop', PARAM_ALPHANUMEXT);
34 $userids = required_param('bulkuser', PARAM_INT);
35 $action = optional_param('action', '', PARAM_ACTION);
36 $filter = optional_param('ifilter', 0, PARAM_INT);
38 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
39 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
41 if ($course->id == SITEID) {
42 redirect(new moodle_url('/'));
45 require_login($course);
46 require_capability('moodle/course:enrolreview', $context);
47 $PAGE->set_pagelayout('admin');
49 $manager = new course_enrolment_manager($PAGE, $course, $filter);
50 $table = new course_enrolment_users_table($manager, $PAGE);
51 $returnurl = new moodle_url('/enrol/users.php', $table->get_combined_url_params());
52 $actionurl = new moodle_url('/enrol/bulkchange.php', $table->get_combined_url_params()+array('bulkuserop' => $bulkuserop));
54 $PAGE->set_url($actionurl);
55 navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
57 $ops = $table->get_bulk_user_enrolment_operations();
58 if (!array_key_exists($bulkuserop, $ops)) {
59 throw new moodle_exception('invalidbulkenrolop');
61 $operation = $ops[$bulkuserop];
63 // Prepare the properties of the form
64 $users = $manager->get_users_enrolments($userids);
66 // Get the form for the bulk operation
67 $mform = $operation->get_form($actionurl, array('users' => $users));
68 // If the mform is false then attempt an immediate process. This may be an immediate action that
69 // doesn't require user input OR confirmation.... who know what but maybe one day
70 if ($mform === false) {
71 if ($operation->process($manager, $users, new stdClass)) {
72 redirect($returnurl);
73 } else {
74 print_error('errorwithbulkoperation', 'enrol');
77 // Check if the bulk operation has been cancelled
78 if ($mform->is_cancelled()) {
79 redirect($returnurl);
81 if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
82 if ($operation->process($manager, $users, $mform->get_data())) {
83 redirect($returnurl);
87 $pagetitle = get_string('bulkuseroperation', 'enrol');
89 $PAGE->set_title($pagetitle);
90 $PAGE->set_heading($pagetitle);
91 echo $OUTPUT->header();
92 echo $OUTPUT->heading($operation->get_title());
93 $mform->display();
94 echo $OUTPUT->footer();