MDL-36754 output: Support token pluginfiles in userpic
[moodle.git] / user / action_redir.php
blob53f6618fe282167fbb5d677bb49d633473a6802e
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');
56 $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
57 $context = context_course::instance($id);
58 $PAGE->set_context($context);
60 $instances = enrol_get_instances($course->id, false);
61 $instance = false;
62 foreach ($instances as $oneinstance) {
63 if ($oneinstance->enrol == $plugin) {
64 $instance = $oneinstance;
65 break;
68 if (!$instance) {
69 print_error('errorwithbulkoperation', 'enrol');
72 $manager = new course_enrolment_manager($PAGE, $course, $instance->id);
73 $plugins = $manager->get_enrolment_plugins();
75 if (!isset($plugins[$plugin])) {
76 print_error('errorwithbulkoperation', 'enrol');
79 $plugin = $plugins[$plugin];
81 $operations = $plugin->get_bulk_operations($manager);
83 if (!isset($operations[$operationname])) {
84 print_error('errorwithbulkoperation', 'enrol');
86 $operation = $operations[$operationname];
88 $userids = optional_param_array('userid', array(), PARAM_INT);
89 $default = new moodle_url('/user/index.php', ['id' => $course->id]);
90 $returnurl = new moodle_url(optional_param('returnto', $default, PARAM_URL));
92 if (empty($userids)) {
93 $userids = optional_param_array('bulkuser', array(), PARAM_INT);
95 if (empty($userids)) {
96 // The first time list hack.
97 if (empty($userids) and $post = data_submitted()) {
98 foreach ($post as $k => $v) {
99 if (preg_match('/^user(\d+)$/', $k, $m)) {
100 $userids[] = $m[1];
106 if (empty($userids)) {
107 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
110 $users = $manager->get_users_enrolments($userids);
112 $removed = array_diff($userids, array_keys($users));
113 if (!empty($removed)) {
114 // This manager does not filter by enrolment method - so we can get the removed users details.
115 $removedmanager = new course_enrolment_manager($PAGE, $course);
116 $removedusers = $removedmanager->get_users_enrolments($removed);
118 foreach ($removedusers as $removeduser) {
119 $msg = get_string('userremovedfromselectiona', 'enrol', fullname($removeduser));
120 \core\notification::warning($msg);
124 // We may have users from any kind of enrolment, we need to filter for the enrolment plugin matching the bulk action.
125 $matchesplugin = function($user) use ($plugin) {
126 foreach ($user->enrolments as $enrolment) {
127 if ($enrolment->enrolmentplugin->get_name() == $plugin->get_name()) {
128 return true;
131 return false;
133 $filteredusers = array_filter($users, $matchesplugin);
135 if (empty($filteredusers)) {
136 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
139 $users = $filteredusers;
141 // Get the form for the bulk operation.
142 $mform = $operation->get_form($PAGE->url, array('users' => $users));
143 // If the mform is false then attempt an immediate process. This may be an immediate action that
144 // doesn't require user input OR confirmation.... who know what but maybe one day.
145 if ($mform === false) {
146 if ($operation->process($manager, $users, new stdClass)) {
147 redirect($returnurl);
148 } else {
149 print_error('errorwithbulkoperation', 'enrol');
152 // Check if the bulk operation has been cancelled.
153 if ($mform->is_cancelled()) {
154 redirect($returnurl);
156 if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
157 if ($operation->process($manager, $users, $mform->get_data())) {
158 redirect($returnurl);
162 $pagetitle = get_string('bulkuseroperation', 'enrol');
164 $PAGE->set_title($pagetitle);
165 $PAGE->set_heading($pagetitle);
166 echo $OUTPUT->header();
167 echo $OUTPUT->heading($operation->get_title());
168 $mform->display();
169 echo $OUTPUT->footer();
170 exit();
172 } else {
173 throw new coding_exception('invalidaction');