2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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)) {
79 if (empty($plugin) AND $operationname == 'download_participants') {
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 if (empty($userids)) {
86 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
90 'firstname' => get_string('firstname'),
91 'lastname' => get_string('lastname'),
92 'email' => get_string('email'),
95 $identityfields = get_extra_user_fields($context);
96 $identityfieldsselect = '';
98 foreach ($identityfields as $field) {
99 $columnnames[$field] = get_string($field);
100 $identityfieldsselect .= ', u.' . $field . ' ';
103 if (!empty($userids)) {
104 list($insql, $inparams) = $DB->get_in_or_equal($userids);
107 $sql = "SELECT u.firstname, u.lastname, u.email" . $identityfieldsselect . "
111 $rs = $DB->get_recordset_sql($sql, $inparams);
112 \core\dataformat
::download_data('courseid_' . $course->id
. '_participants', $dataformat, $columnnames, $rs);
118 $instances = enrol_get_instances($course->id
, false);
120 foreach ($instances as $oneinstance) {
121 if ($oneinstance->enrol
== $plugin) {
122 $instance = $oneinstance;
127 print_error('errorwithbulkoperation', 'enrol');
130 $manager = new course_enrolment_manager($PAGE, $course, $instance->id
);
131 $plugins = $manager->get_enrolment_plugins();
133 if (!isset($plugins[$plugin])) {
134 print_error('errorwithbulkoperation', 'enrol');
137 $plugin = $plugins[$plugin];
139 $operations = $plugin->get_bulk_operations($manager);
141 if (!isset($operations[$operationname])) {
142 print_error('errorwithbulkoperation', 'enrol');
144 $operation = $operations[$operationname];
146 if (empty($userids)) {
147 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
150 $users = $manager->get_users_enrolments($userids);
152 $removed = array_diff($userids, array_keys($users));
153 if (!empty($removed)) {
154 // This manager does not filter by enrolment method - so we can get the removed users details.
155 $removedmanager = new course_enrolment_manager($PAGE, $course);
156 $removedusers = $removedmanager->get_users_enrolments($removed);
158 foreach ($removedusers as $removeduser) {
159 $msg = get_string('userremovedfromselectiona', 'enrol', fullname($removeduser));
160 \core\notification
::warning($msg);
164 // We may have users from any kind of enrolment, we need to filter for the enrolment plugin matching the bulk action.
165 $matchesplugin = function($user) use ($plugin) {
166 foreach ($user->enrolments
as $enrolment) {
167 if ($enrolment->enrolmentplugin
->get_name() == $plugin->get_name()) {
173 $filteredusers = array_filter($users, $matchesplugin);
175 if (empty($filteredusers)) {
176 redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
179 $users = $filteredusers;
181 // Get the form for the bulk operation.
182 $mform = $operation->get_form($PAGE->url
, array('users' => $users));
183 // If the mform is false then attempt an immediate process. This may be an immediate action that
184 // doesn't require user input OR confirmation.... who know what but maybe one day.
185 if ($mform === false) {
186 if ($operation->process($manager, $users, new stdClass
)) {
187 redirect($returnurl);
189 print_error('errorwithbulkoperation', 'enrol');
192 // Check if the bulk operation has been cancelled.
193 if ($mform->is_cancelled()) {
194 redirect($returnurl);
196 if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
197 if ($operation->process($manager, $users, $mform->get_data())) {
198 redirect($returnurl);
202 $pagetitle = get_string('bulkuseroperation', 'enrol');
204 $PAGE->set_title($pagetitle);
205 $PAGE->set_heading($pagetitle);
206 echo $OUTPUT->header();
207 echo $OUTPUT->heading($operation->get_title());
209 echo $OUTPUT->footer();
213 throw new coding_exception('invalidaction');