MDL-66335 behat: Avoid double processing the page type
[moodle.git] / user / action_redir.php
blob46babe7ccd6ae3e77be3af15afec956487d36f93
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 * Wrapper script redirecting user operations to correct destination.
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_user
25 require_once("../config.php");
27 $formaction = required_param('formaction', PARAM_LOCALURL);
28 $id = required_param('id', PARAM_INT);
30 $PAGE->set_url('/user/action_redir.php', array('formaction' => $formaction, 'id' => $id));
31 list($formaction) = explode('?', $formaction, 2);
33 // This page now only handles the bulk enrolment change actions, other actions are done with ajax.
34 $actions = array('bulkchange.php');
36 if (array_search($formaction, $actions) === false) {
37 print_error('unknownuseraction');
40 if (!confirm_sesskey()) {
41 print_error('confirmsesskeybad');
44 if ($formaction == 'bulkchange.php') {
45 // Backwards compatibility for enrolment plugins bulk change functionality.
46 // This awful code is adapting from the participant page with it's param names and values
47 // to the values expected by the bulk enrolment changes forms.
48 $formaction = required_param('formaction', PARAM_URL);
49 require_once($CFG->dirroot . '/enrol/locallib.php');
51 $url = new moodle_url($formaction);
52 // Get the enrolment plugin type and bulk action from the url.
53 $plugin = $url->param('plugin');
54 $operationname = $url->param('operation');
55 $dataformat = $url->param('dataformat');
57 $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
58 $context = context_course::instance($id);
59 $PAGE->set_context($context);
61 $userids = optional_param_array('userid', array(), PARAM_INT);
62 $default = new moodle_url('/user/index.php', ['id' => $course->id]);
63 $returnurl = new moodle_url(optional_param('returnto', $default, PARAM_URL));
65 if (empty($userids)) {
66 $userids = optional_param_array('bulkuser', array(), PARAM_INT);
68 if (empty($userids)) {
69 // The first time list hack.
70 if (empty($userids) and $post = data_submitted()) {
71 foreach ($post as $k => $v) {
72 if (preg_match('/^user(\d+)$/', $k, $m)) {
73 $userids[] = $m[1];
79 if (empty($plugin) AND $operationname == 'download_participants') {
80 // Check permissions.
81 if (has_capability('moodle/course:manageactivities', $context)) {
82 $plugins = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
83 if (isset($plugins[$dataformat])) {
84 if ($plugins[$dataformat]->is_enabled()) {
85 require_once($CFG->dirroot . '/lib/dataformatlib.php');
87 if (empty($userids)) {
88 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
91 $columnnames = array(
92 'firstname' => get_string('firstname'),
93 'lastname' => get_string('lastname'),
94 'email' => get_string('email'),
97 $identityfields = get_extra_user_fields($context);
98 $identityfieldsselect = '';
100 foreach ($identityfields as $field) {
101 $columnnames[$field] = get_string($field);
102 $identityfieldsselect .= ', u.' . $field . ' ';
105 if (!empty($userids)) {
106 list($insql, $inparams) = $DB->get_in_or_equal($userids);
109 $sql = "SELECT u.firstname, u.lastname, u.email" . $identityfieldsselect . "
110 FROM {user} u
111 WHERE u.id $insql";
113 $rs = $DB->get_recordset_sql($sql, $inparams);
114 download_as_dataformat('courseid_' . $course->id . '_participants', $dataformat, $columnnames, $rs);
115 $rs->close();
119 } else {
120 $instances = enrol_get_instances($course->id, false);
121 $instance = false;
122 foreach ($instances as $oneinstance) {
123 if ($oneinstance->enrol == $plugin) {
124 $instance = $oneinstance;
125 break;
128 if (!$instance) {
129 print_error('errorwithbulkoperation', 'enrol');
132 $manager = new course_enrolment_manager($PAGE, $course, $instance->id);
133 $plugins = $manager->get_enrolment_plugins();
135 if (!isset($plugins[$plugin])) {
136 print_error('errorwithbulkoperation', 'enrol');
139 $plugin = $plugins[$plugin];
141 $operations = $plugin->get_bulk_operations($manager);
143 if (!isset($operations[$operationname])) {
144 print_error('errorwithbulkoperation', 'enrol');
146 $operation = $operations[$operationname];
148 if (empty($userids)) {
149 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
152 $users = $manager->get_users_enrolments($userids);
154 $removed = array_diff($userids, array_keys($users));
155 if (!empty($removed)) {
156 // This manager does not filter by enrolment method - so we can get the removed users details.
157 $removedmanager = new course_enrolment_manager($PAGE, $course);
158 $removedusers = $removedmanager->get_users_enrolments($removed);
160 foreach ($removedusers as $removeduser) {
161 $msg = get_string('userremovedfromselectiona', 'enrol', fullname($removeduser));
162 \core\notification::warning($msg);
166 // We may have users from any kind of enrolment, we need to filter for the enrolment plugin matching the bulk action.
167 $matchesplugin = function($user) use ($plugin) {
168 foreach ($user->enrolments as $enrolment) {
169 if ($enrolment->enrolmentplugin->get_name() == $plugin->get_name()) {
170 return true;
173 return false;
175 $filteredusers = array_filter($users, $matchesplugin);
177 if (empty($filteredusers)) {
178 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
181 $users = $filteredusers;
183 // Get the form for the bulk operation.
184 $mform = $operation->get_form($PAGE->url, array('users' => $users));
185 // If the mform is false then attempt an immediate process. This may be an immediate action that
186 // doesn't require user input OR confirmation.... who know what but maybe one day.
187 if ($mform === false) {
188 if ($operation->process($manager, $users, new stdClass)) {
189 redirect($returnurl);
190 } else {
191 print_error('errorwithbulkoperation', 'enrol');
194 // Check if the bulk operation has been cancelled.
195 if ($mform->is_cancelled()) {
196 redirect($returnurl);
198 if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
199 if ($operation->process($manager, $users, $mform->get_data())) {
200 redirect($returnurl);
204 $pagetitle = get_string('bulkuseroperation', 'enrol');
206 $PAGE->set_title($pagetitle);
207 $PAGE->set_heading($pagetitle);
208 echo $OUTPUT->header();
209 echo $OUTPUT->heading($operation->get_title());
210 $mform->display();
211 echo $OUTPUT->footer();
212 exit();
214 } else {
215 throw new coding_exception('invalidaction');