first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
require_once($CFG->dirroot.'/user/filters/lib.php');
if (!defined('MAX_BULK_USERS')) {
define('MAX_BULK_USERS', 2000);
}
function add_selection_all($ufiltering) {
global $SESSION, $DB, $CFG;
list($sqlwhere, $params) = $ufiltering->get_sql_filter("id<>:exguest AND deleted <> 1", array('exguest'=>$CFG->siteguest));
$rs = $DB->get_recordset_select('user', $sqlwhere, $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
foreach ($rs as $user) {
if (!isset($SESSION->bulk_users[$user->id])) {
$SESSION->bulk_users[$user->id] = $user->id;
}
}
$rs->close();
}
function get_selection_data($ufiltering) {
global $SESSION, $DB, $CFG;
// get the SQL filter
list($sqlwhere, $params) = $ufiltering->get_sql_filter("id<>:exguest AND deleted <> 1", array('exguest'=>$CFG->siteguest));
$total = $DB->count_records_select('user', "id<>:exguest AND deleted <> 1", array('exguest'=>$CFG->siteguest));
$acount = $DB->count_records_select('user', $sqlwhere, $params);
$scount = count($SESSION->bulk_users);
$userlist = array('acount'=>$acount, 'scount'=>$scount, 'ausers'=>false, 'susers'=>false, 'total'=>$total);
$userlist['ausers'] = $DB->get_records_select_menu('user', $sqlwhere, $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname', 0, MAX_BULK_USERS);
if ($scount) {
if ($scount < MAX_BULK_USERS) {
$bulkusers = $SESSION->bulk_users;
} else {
$bulkusers = array_slice($SESSION->bulk_users, 0, MAX_BULK_USERS, true);
}
list($in, $inparams) = $DB->get_in_or_equal($bulkusers);
$userlist['susers'] = $DB->get_records_select_menu('user', "id $in", $inparams, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
}
return $userlist;
}
+109
View File
@@ -0,0 +1,109 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Bulk user actions
*
* @package core
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/lib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/user_bulk_forms.php');
admin_externalpage_setup('userbulk');
if (!isset($SESSION->bulk_users)) {
$SESSION->bulk_users = array();
}
// Create the user filter form.
$ufiltering = new user_filtering();
// Create the bulk operations form.
$actionform = new user_bulk_action_form();
$actionform->set_data(['returnurl' => $PAGE->url->out_as_local_url(false)]);
if ($data = $actionform->get_data()) {
if ($data->passuserids) {
// This means we called the form from /admin/user.php or similar and the userids should be taken from the form
// data and not from $SESSION->bulk_users. For backwards compatibility we still set $SESSION->bulk_users.
$users = preg_split('/,/', $data->userids, -1, PREG_SPLIT_NO_EMPTY);
$SESSION->bulk_users = array_combine($users, $users);
}
// Check if an action should be performed and do so.
$bulkactions = $actionform->get_actions();
if (array_key_exists($data->action, $bulkactions)) {
redirect(new moodle_url($bulkactions[$data->action]->url, ['returnurl' => $data->returnurl ?: null]));
}
}
$userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
if ($data = $userbulkform->get_data()) {
if (!empty($data->addall)) {
add_selection_all($ufiltering);
} else if (!empty($data->addsel)) {
if (!empty($data->ausers)) {
if (in_array(0, $data->ausers)) {
add_selection_all($ufiltering);
} else {
foreach ($data->ausers as $userid) {
if ($userid == -1) {
continue;
}
if (!isset($SESSION->bulk_users[$userid])) {
$SESSION->bulk_users[$userid] = $userid;
}
}
}
}
} else if (!empty($data->removeall)) {
$SESSION->bulk_users = array();
} else if (!empty($data->removesel)) {
if (!empty($data->susers)) {
if (in_array(0, $data->susers)) {
$SESSION->bulk_users = array();
} else {
foreach ($data->susers as $userid) {
if ($userid == -1) {
continue;
}
unset($SESSION->bulk_users[$userid]);
}
}
}
}
// Reset the form selections.
unset($_POST);
$userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
}
echo $OUTPUT->header();
$ufiltering->display_add();
$ufiltering->display_active();
$userbulkform->display();
$actionform->display();
echo $OUTPUT->footer();
+163
View File
@@ -0,0 +1,163 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* script for bulk user multi cohort add
*
* @package core
* @subpackage user
* @copyright 2011 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once('user_bulk_cohortadd_form.php');
require_once("$CFG->dirroot/cohort/lib.php");
$sort = optional_param('sort', 'fullname', PARAM_ALPHA);
$dir = optional_param('dir', 'asc', PARAM_ALPHA);
admin_externalpage_setup('userbulk');
require_capability('moodle/cohort:assign', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
$users = $SESSION->bulk_users;
$strnever = get_string('never');
$cohorts = array(''=>get_string('choosedots'));
$allcohorts = $DB->get_records('cohort', null, 'name');
foreach ($allcohorts as $c) {
if (!empty($c->component)) {
// external cohorts can not be modified
continue;
}
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:assign', $context)) {
continue;
}
if (empty($c->idnumber)) {
$cohorts[$c->id] = format_string($c->name);
} else {
$cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']';
}
}
unset($allcohorts);
if (count($cohorts) < 2) {
redirect($return, get_string('bulknocohort', 'core_cohort'));
}
$countries = get_string_manager()->get_list_of_countries(true);
$userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
email, country, lastaccess, city, deleted');
$user->fullname = fullname($user, true);
$user->country = @$countries[$user->country];
unset($user->firstname);
unset($user->lastname);
$users[$key] = $user;
}
unset($countries);
$mform = new user_bulk_cohortadd_form(null, $cohorts);
$mform->set_data(['returnurl' => $returnurl]);
if (empty($users) or $mform->is_cancelled()) {
redirect($return);
} else if ($data = $mform->get_data()) {
// process request
foreach ($users as $user) {
if (!$user->deleted && !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort, 'userid' => $user->id))) {
cohort_add_member($data->cohort, $user->id);
}
}
redirect($return);
}
// Need to sort by date
function sort_compare($a, $b) {
global $sort, $dir;
if ($sort == 'lastaccess') {
$rez = $b->lastaccess - $a->lastaccess;
} else {
$rez = strcasecmp(@$a->$sort, @$b->$sort);
}
return $dir == 'desc' ? -$rez : $rez;
}
usort($users, 'sort_compare');
$table = new html_table();
$table->width = "95%";
$columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
foreach ($columns as $column) {
$strtitle = get_string($column);
if ($sort != $column) {
$columnicon = '';
$columndir = 'asc';
} else {
$columndir = ($dir == 'asc') ? 'desc' : 'asc';
$icon = 't/down';
$iconstr = $columndir;
if ($dir != 'asc') {
$icon = 't/up';
}
$columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
}
$table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
$table->align[] = 'left';
}
foreach ($users as $user) {
if ($user->deleted) {
$table->data[] = array (
$user->fullname,
'',
'',
'',
get_string('deleteduser', 'bulkusers')
);
} else {
$table->data[] = array(
'<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . SITEID . '">' .
$user->fullname .
'</a>',
s($user->email),
$user->city,
$user->country,
$user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
);
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
echo html_writer::table($table);
echo $OUTPUT->box_start();
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
+42
View File
@@ -0,0 +1,42 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* form for bulk user multi cohort add
*
* @package core
* @subpackage user
* @copyright 2011 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
class user_bulk_cohortadd_form extends moodleform {
function definition() {
$mform = $this->_form;
$cohorts = $this->_customdata;
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->addElement('select', 'cohort', get_string('cohort', 'core_cohort'), $cohorts);
$mform->addRule('cohort', get_string('required'), 'required', null, 'client');
$this->add_action_buttons(true, get_string('bulkadd', 'core_cohort'));
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
/**
* script for bulk user delete operations
*/
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
echo $OUTPUT->header();
//TODO: add support for large number of users
if ($confirm and confirm_sesskey()) {
$notifications = '';
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, username, secret, confirmed, auth, firstname, lastname');
foreach ($rs as $user) {
if ($user->confirmed) {
continue;
}
$auth = get_auth_plugin($user->auth);
$result = $auth->user_confirm($user->username, $user->secret);
if ($result != AUTH_CONFIRM_OK && $result != AUTH_CONFIRM_ALREADY) {
$notifications .= $OUTPUT->notification(get_string('usernotconfirmed', '', fullname($user, true)));
}
}
$rs->close();
echo $OUTPUT->box_start('generalbox', 'notice');
if (!empty($notifications)) {
echo $notifications;
} else {
echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
}
$continue = new single_button($return, get_string('continue'), 'post');
echo $OUTPUT->render($continue);
echo $OUTPUT->box_end();
} else {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
$usernames = implode(', ', $userlist);
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
$formcontinue = new single_button(new moodle_url('user_bulk_confirm.php',
['confirm' => 1, 'returnurl' => $returnurl]), get_string('yes'));
$formcancel = new single_button($return, get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('confirmcheckfull', '', $usernames), $formcontinue, $formcancel);
}
echo $OUTPUT->footer();
+61
View File
@@ -0,0 +1,61 @@
<?php
/**
* script for bulk user delete operations
*/
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
admin_externalpage_setup('userbulk');
require_capability('moodle/user:delete', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
echo $OUTPUT->header();
//TODO: add support for large number of users
if ($confirm and confirm_sesskey()) {
$notifications = '';
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "deleted = 0 and id $in", $params);
foreach ($rs as $user) {
if (!is_siteadmin($user) and $USER->id != $user->id and delete_user($user)) {
unset($SESSION->bulk_users[$user->id]);
} else {
$notifications .= $OUTPUT->notification(get_string('deletednot', '', fullname($user, true)));
}
}
$rs->close();
\core\session\manager::gc(); // Remove stale sessions.
echo $OUTPUT->box_start('generalbox', 'notice');
if (!empty($notifications)) {
echo $notifications;
} else {
echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
}
$continue = new single_button($return, get_string('continue'), 'post');
echo $OUTPUT->render($continue);
echo $OUTPUT->box_end();
} else {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
$usernames = implode(', ', $userlist);
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
$formcontinue = new single_button(new moodle_url('user_bulk_delete.php',
['confirm' => 1, 'returnurl' => $returnurl]), get_string('yes'));
$formcancel = new single_button($return, get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('deletecheckfull', '', $usernames), $formcontinue, $formcancel);
}
echo $OUTPUT->footer();
+92
View File
@@ -0,0 +1,92 @@
<?php
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$sort = optional_param('sort', 'fullname', PARAM_ALPHA);
$dir = optional_param('dir', 'asc', PARAM_ALPHA);
admin_externalpage_setup('userbulk');
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
$users = $SESSION->bulk_users;
$usertotal = get_users(false);
$usercount = count($users);
$strnever = get_string('never');
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
echo $OUTPUT->header();
$countries = get_string_manager()->get_list_of_countries(true);
$userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city');
$user->fullname = fullname($user, true);
$user->country = @$countries[$user->country];
unset($user->firstname);
unset($user->lastname);
$users[$key] = $user;
}
unset($countries);
// Need to sort by date
function sort_compare($a, $b) {
global $sort, $dir;
if($sort == 'lastaccess') {
$rez = $b->lastaccess - $a->lastaccess;
} else {
$rez = strcasecmp(@$a->$sort, @$b->$sort);
}
return $dir == 'desc' ? -$rez : $rez;
}
usort($users, 'sort_compare');
$table = new html_table();
$table->width = "95%";
$columns = array('fullname', /*'username', */'email', 'city', 'country', 'lastaccess');
foreach ($columns as $column) {
$strtitle = get_string($column);
if ($sort != $column) {
$columnicon = '';
$columndir = 'asc';
} else {
$columndir = $dir == 'asc' ? 'desc' : 'asc';
$icon = 't/down';
$iconstr = $columndir;
if ($dir != 'asc') {
$icon = 't/up';
}
$columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
}
$table->head[] = '<a href="user_bulk_display.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
$table->align[] = 'left';
}
foreach($users as $user) {
$table->data[] = array (
'<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
// $user->username,
s($user->email),
$user->city,
$user->country,
$user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
);
}
echo $OUTPUT->heading("$usercount / $usertotal ".get_string('users'));
echo html_writer::table($table);
echo $OUTPUT->continue_button($return);
echo $OUTPUT->footer();
+118
View File
@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Bulk export user into any dataformat
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @copyright 2007 Petr Skoda
* @package core
*/
use core_user\fields;
define('NO_OUTPUT_BUFFERING', true);
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
$dataformat = optional_param('dataformat', '', PARAM_ALPHA);
admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
if ($dataformat) {
$originfields = array('id' => 'id',
'username' => 'username',
'email' => 'email',
'firstname' => 'firstname',
'lastname' => 'lastname',
'idnumber' => 'idnumber',
'institution' => 'institution',
'department' => 'department',
'phone1' => 'phone1',
'phone2' => 'phone2',
'city' => 'city',
'country' => 'country');
$extrafields = profile_get_user_fields_with_data(0);
$profilefields = [];
foreach ($extrafields as $formfield) {
$profilefields[fields::PROFILE_FIELD_PREFIX . $formfield->get_shortname()] = fields::PROFILE_FIELD_PREFIX .
$formfield->get_shortname();
}
$filename = clean_filename(get_string('users'));
$downloadusers = new ArrayObject($SESSION->bulk_users);
$iterator = $downloadusers->getIterator();
\core\dataformat::download_data($filename, $dataformat, array_merge($originfields, $profilefields), $iterator,
function($userid, $supportshtml) use ($originfields) {
global $DB;
if (!$user = $DB->get_record('user', array('id' => $userid))) {
return null;
}
$userprofiledata = array();
foreach ($originfields as $field) {
// Custom user profile textarea fields come in an array
// The first element is the text and the second is the format.
// We only take the text.
if (is_array($user->$field)) {
$userprofiledata[$field] = reset($user->$field);
} else if ($supportshtml) {
$userprofiledata[$field] = s($user->$field);
} else {
$userprofiledata[$field] = $user->$field;
}
}
// Formatting extra field if transform is true.
$extrafields = profile_get_user_fields_with_data($userid);
foreach ($extrafields as $field) {
$fieldkey = fields::PROFILE_FIELD_PREFIX . $field->get_shortname();
if ($field->is_transform_supported()) {
$userprofiledata[$fieldkey] = $field->display_data();
} else {
$userprofiledata[$fieldkey] = $field->data;
}
}
return $userprofiledata;
});
exit;
}
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('download', 'admin'));
echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
echo $OUTPUT->footer();
@@ -0,0 +1,72 @@
<?php
/**
* script for bulk user force password change
*/
require_once('../../config.php');
require_once('lib.php');
require_once($CFG->libdir.'/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
echo $OUTPUT->header();
if ($confirm and confirm_sesskey()) {
// only force password change if user may actually change the password
$authsavailable = get_enabled_auth_plugins();
$changeable = array();
foreach($authsavailable as $authplugin) {
if (!$auth = get_auth_plugin($authplugin)) {
continue;
}
if ($auth->is_internal() and $auth->can_change_password()) {
$changeable[$authplugin] = true;
}
}
$parts = array_chunk($SESSION->bulk_users, 300);
foreach ($parts as $users) {
list($in, $params) = $DB->get_in_or_equal($users);
$rs = $DB->get_recordset_select('user', "id $in", $params);
foreach ($rs as $user) {
if (!empty($changeable[$user->auth])) {
set_user_preference('auth_forcepasswordchange', 1, $user->id);
unset($SESSION->bulk_users[$user->id]);
} else {
echo $OUTPUT->notification(get_string('forcepasswordchangenot', '', fullname($user, true)));
}
}
$rs->close();
}
echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
echo $OUTPUT->continue_button($return);
} else {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname', 0, MAX_BULK_USERS);
$usernames = implode(', ', $userlist);
if (count($SESSION->bulk_users) > MAX_BULK_USERS) {
$usernames .= ', ...';
}
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
$formcontinue = new single_button(new moodle_url('/admin/user/user_bulk_forcepasswordchange.php',
['confirm' => 1, 'returnurl' => $returnurl]), get_string('yes'));
$formcancel = new single_button($return, get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('forcepasswordchangecheckfull', '', $usernames), $formcontinue, $formcancel);
}
echo $OUTPUT->footer();
+275
View File
@@ -0,0 +1,275 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Bulk user action forms
*
* @package core
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/datalib.php');
/**
* Bulk user action form
*
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_bulk_action_form extends moodleform {
/** @var bool */
protected $hasbulkactions = false;
/** @var array|null */
protected $actions = null;
/**
* Returns an array of action_link's of all bulk actions available for this user.
*
* @param bool $flatlist whether to return a flat list (for easier searching) or a list with
* option groups that can be used to build a select element
* @return array of action_link objects
*/
public function get_actions(bool $flatlist = true): array {
if ($this->actions === null) {
$this->actions = $this->build_actions();
$this->hasbulkactions = !empty($this->actions);
}
if ($flatlist) {
return array_reduce($this->actions, fn($carry, $item) => $carry + $item, []);
}
return $this->actions;
}
/**
* Builds the list of bulk user actions available for this user.
*
* @return array
*/
protected function build_actions(): array {
global $CFG;
$canaccessbulkactions = has_any_capability(['moodle/user:update', 'moodle/user:delete'], context_system::instance());
$syscontext = context_system::instance();
$actions = [];
if (has_capability('moodle/user:update', $syscontext)) {
$actions['confirm'] = new action_link(
new moodle_url('/admin/user/user_bulk_confirm.php'),
get_string('confirm'));
}
if ($canaccessbulkactions && has_capability('moodle/site:readallmessages', $syscontext) && !empty($CFG->messaging)) {
$actions['message'] = new action_link(
new moodle_url('/admin/user/user_bulk_message.php'),
get_string('messageselectadd'));
}
if (has_capability('moodle/user:delete', $syscontext)) {
$actions['delete'] = new action_link(
new moodle_url('/admin/user/user_bulk_delete.php'),
get_string('delete'));
}
if ($canaccessbulkactions) {
$actions['displayonpage'] = new action_link(
new moodle_url('/admin/user/user_bulk_display.php'),
get_string('displayonpage'));
}
if (has_capability('moodle/user:update', $syscontext)) {
$actions['download'] = new action_link(
new moodle_url('/admin/user/user_bulk_download.php'),
get_string('download', 'admin'));
}
if (has_capability('moodle/user:update', $syscontext)) {
$actions['forcepasswordchange'] = new action_link(
new moodle_url('/admin/user/user_bulk_forcepasswordchange.php'),
get_string('forcepasswordchange'));
}
if ($canaccessbulkactions && has_capability('moodle/cohort:assign', $syscontext)) {
$actions['addtocohort'] = new action_link(
new moodle_url('/admin/user/user_bulk_cohortadd.php'),
get_string('bulkadd', 'core_cohort'));
}
// Collect all bulk user actions.
$hook = new \core_user\hook\extend_bulk_user_actions();
// Add actions from core.
foreach ($actions as $identifier => $action) {
$hook->add_action($identifier, $action);
}
// Add actions from the legacy callback 'bulk_user_actions'.
$moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php', true, true);
foreach ($moreactions as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginactions = $pluginfunction();
foreach ($pluginactions as $identifier => $action) {
$hook->add_action($identifier, $action);
}
}
}
// Any plugin can append user bulk actions to this list by implementing a hook callback.
\core\di::get(\core\hook\manager::class)->dispatch($hook);
// This method may be called from 'Bulk actions' and 'Browse user list' pages. Some actions
// may be irrelevant in one of the contexts and they can be excluded by specifying the
// 'excludeactions' customdata.
$excludeactions = $this->_customdata['excludeactions'] ?? [];
foreach ($excludeactions as $excludeaction) {
$hook->add_action($excludeaction, null);
}
return $hook->get_actions();
}
/**
* Form definition
*/
public function definition() {
$mform =& $this->_form;
// Most bulk actions perform a redirect on selection, so we shouldn't trigger formchange warnings (specifically because
// the user must have _already_ changed the current form by selecting users to perform the action on).
$mform->disable_form_change_checker();
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
// When 'passuserids' is specified in the customdata, the user ids are expected in the form
// data rather than in the $SESSION->bulk_users .
$passuserids = !empty($this->_customdata['passuserids']);
$mform->addElement('hidden', 'passuserids', $passuserids);
$mform->setType('passuserids', PARAM_BOOL);
$mform->addElement('hidden', 'userids');
$mform->setType('userids', PARAM_SEQUENCE);
$actions = ['' => [0 => get_string('choose') . '...']];
$bulkactions = $this->get_actions(false);
foreach ($bulkactions as $category => $categoryactions) {
$actions[$category] = array_map(fn($action) => $action->text, $categoryactions);
}
$objs = array();
$objs[] = $selectel = $mform->createElement('selectgroups', 'action', get_string('userbulk', 'admin'), $actions);
$selectel->setHiddenLabel(true);
if (empty($this->_customdata['hidesubmit'])) {
$objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));
}
$mform->addElement('group', 'actionsgrp', get_string('withselectedusers'), $objs, ' ', false);
}
/**
* Is there at least one available bulk action in this form
*
* @return bool
*/
public function has_bulk_actions(): bool {
return $this->hasbulkactions;
}
}
/**
* Bulk user form
*
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_bulk_form extends moodleform {
/**
* Form definition
*/
public function definition() {
$mform =& $this->_form;
$acount =& $this->_customdata['acount'];
$scount =& $this->_customdata['scount'];
$ausers =& $this->_customdata['ausers'];
$susers =& $this->_customdata['susers'];
$total =& $this->_customdata['total'];
$achoices = array();
$schoices = array();
if (is_array($ausers)) {
if ($total == $acount) {
$achoices[0] = get_string('allusers', 'bulkusers', $total);
} else {
$a = new stdClass();
$a->total = $total;
$a->count = $acount;
$achoices[0] = get_string('allfilteredusers', 'bulkusers', $a);
}
$achoices = $achoices + $ausers;
if ($acount > MAX_BULK_USERS) {
$achoices[-1] = '...';
}
} else {
$achoices[-1] = get_string('nofilteredusers', 'bulkusers', $total);
}
if (is_array($susers)) {
$a = new stdClass();
$a->total = $total;
$a->count = $scount;
$schoices[0] = get_string('allselectedusers', 'bulkusers', $a);
$schoices = $schoices + $susers;
if ($scount > MAX_BULK_USERS) {
$schoices[-1] = '...';
}
} else {
$schoices[-1] = get_string('noselectedusers', 'bulkusers');
}
$mform->addElement('header', 'users', get_string('usersinlist', 'bulkusers'));
$objs = array();
$objs[0] =& $mform->createElement('select', 'ausers', get_string('available', 'bulkusers'), $achoices, 'size="15"');
$objs[0]->setMultiple(true);
$objs[1] =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), $schoices, 'size="15"');
$objs[1]->setMultiple(true);
$grp =& $mform->addElement('group', 'usersgrp', get_string('users', 'bulkusers'), $objs, ' ', false);
$mform->addHelpButton('usersgrp', 'users', 'bulkusers');
$mform->addElement('static', 'comment');
$objs = array();
$objs[] =& $mform->createElement('submit', 'addsel', get_string('addsel', 'bulkusers'));
$objs[] =& $mform->createElement('submit', 'removesel', get_string('removesel', 'bulkusers'));
$grp =& $mform->addElement('group', 'buttonsgrp', get_string('selectedlist', 'bulkusers'), $objs, null, false);
$mform->addHelpButton('buttonsgrp', 'selectedlist', 'bulkusers');
$objs = array();
$objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'bulkusers'));
$objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'bulkusers'));
$grp =& $mform->addElement('group', 'buttonsgrp2', '', $objs, null, false);
$renderer =& $mform->defaultRenderer();
$template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
$renderer->setGroupElementTemplate($template, 'usersgrp');
}
}
+74
View File
@@ -0,0 +1,74 @@
<?php
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/message/lib.php');
require_once('user_message_form.php');
$msg = optional_param('msg', '', PARAM_RAW);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
admin_externalpage_setup('userbulk');
require_capability('moodle/site:manageallmessaging', context_system::instance());
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
if (empty($SESSION->bulk_users)) {
redirect($return);
}
if (empty($CFG->messaging)) {
throw new \moodle_exception('messagingdisable', 'error');
}
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->set_secondary_active_tab('users');
//TODO: add support for large number of users
if ($confirm and !empty($msg) and confirm_sesskey()) {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "id $in", $params);
foreach ($rs as $user) {
//TODO we should probably support all text formats here or only FORMAT_MOODLE
//For now bulk messaging is still using the html editor and its supplying html
//so we have to use html format for it to be displayed correctly
message_post_message($USER, $user, $msg, FORMAT_HTML);
}
$rs->close();
redirect($return);
}
$msgform = new user_message_form('user_bulk_message.php');
$msgform->set_data(['returnurl' => $returnurl]);
if ($msgform->is_cancelled()) {
redirect($return);
} else if ($formdata = $msgform->get_data()) {
$options = new stdClass();
$options->para = false;
$options->newlines = true;
$options->trusted = trusttext_trusted(\context_system::instance());
$msg = format_text($formdata->messagebody['text'], $formdata->messagebody['format'], $options);
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
$usernames = implode(', ', $userlist);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('confirmation', 'admin'));
echo $OUTPUT->box($msg, 'boxwidthnarrow boxaligncenter generalbox', 'preview'); //TODO: clean once we start using proper text formats here
$formcontinue = new single_button(new moodle_url('user_bulk_message.php',
['confirm' => 1, 'msg' => $msg, 'returnurl' => $returnurl]),
get_string('yes')); // TODO: clean once we start using proper text formats here.
$formcancel = new single_button($return, get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('confirmmessage', 'bulkusers', $usernames), $formcontinue, $formcancel);
echo $OUTPUT->footer();
die;
}
echo $OUTPUT->header();
$msgform->display();
echo $OUTPUT->footer();
+23
View File
@@ -0,0 +1,23 @@
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');
class user_message_form extends moodleform {
function definition() {
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string('message', 'message'));
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->addElement('editor', 'messagebody', get_string('messagebody'), null, null);
$mform->addRule('messagebody', '', 'required', null, 'server');
$this->add_action_buttons();
}
}