Moodle 2.X bulk suspend users userbulksuspend

Issue:

Not the best practice so please use it as quick fix to build your own bulk suspend operation under “Bulk user actions”.

We need to modify few files

  • lang/en/moodle.php
  • admin/user/user_bulk.php
  • admin/user/user_bulk_forms.php

And create a new custom file called “user_bulk_suspend.php”

Cause/Problem:

N/A

Resolution:

  • add drop down item “Suspend Users”

So first thing first: admin/user/user_bulk.php holds “Bulk user actions” page where you can find drop down menu with functions like: confirm, send a message, delete…. to add a new drop down item we need to modify “user_bulk.php” file.

add new case under case 8 add this line => case 9:

redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_suspend.php');
  • rename ugly [[suspend]] item to friendly form such as “Suspend Users”

You should be able to see a new drop down item such as “[[suspend]]” to rewrite the name to let’s say “Suspend Users” we need to modify second file “lang/en/moodle.php” which holds all messages. Add thisline.

$string['suspend'] = 'Suspend users';
  • add a logic into drop down menu

Add a logic to the drop down menu – place this code:

if (has_capability('moodle/user:update', $syscontext)) {
            $actions[9] = get_string('suspend');
        }
under “class user_bulk_action_form” located in “user_bulk_forms.php”
  • and grand final lets add our custom bulk operation
    Create a new file called user_bulk_suspend.php and place this code into it
<?php
/**
* script for bulk user suspend operations
* Custom module created by Celox Group www.celoxgroup.com.au
*/
 
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
 
$confirm = optional_param('confirm', 0, PARAM_BOOL);
 
require_login();
admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM)); //user:delete
 
$return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
 
if (empty($SESSION->bulk_users)) {
    redirect($return);
}
 
echo $OUTPUT->header();
 
if ($confirm 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) {
        if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
            //unset($SESSION->bulk_users[$user->id]);
$user->suspended = 1;
            $user->timemodified = time();
$DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
            $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
session_kill_user($user->id);
            events_trigger('user_updated', $user);
        } else {
            echo $OUTPUT->notification(get_string('deletednot', '', fullname($user, true)));
        }
    }
    $rs->close();
    session_gc(); // remove stale sessions
    redirect($return, get_string('changessaved'));
 
} 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_suspend.php', array('confirm' => 1)), get_string('yes'));
    $formcancel = new single_button(new moodle_url('user_bulk.php'), get_string('no'), 'get');
    echo $OUTPUT->confirm(get_string('suspendcheckfull', '', $usernames), $formcontinue, $formcancel);
}
 
echo $OUTPUT->footer();

 

Now you should be able to perform bulk suspend action

Leave a Reply

avatar
  Subscribe  
Notify of