Skip to content

Commit

Permalink
MDL-62920 user: Uploading deleted user and adding to a cohort
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Oct 11, 2018
1 parent 0dfa4a3 commit acca768
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
32 changes: 22 additions & 10 deletions admin/user/user_bulk_cohortadd.php
Expand Up @@ -66,8 +66,8 @@
$countries = get_string_manager()->get_list_of_countries(true);
$namefields = get_all_user_name_fields(true);
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, ' . $namefields . ', username,
email, country, lastaccess, city');
$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);
Expand All @@ -84,7 +84,7 @@
} else if ($data = $mform->get_data()) {
// process request
foreach ($users as $user) {
if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) {
if (!$user->deleted && !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort, 'userid' => $user->id))) {
cohort_add_member($data->cohort, $user->id);
}
}
Expand Down Expand Up @@ -125,13 +125,25 @@ function sort_compare($a, $b) {
}

foreach ($users as $user) {
$table->data[] = array (
'<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
$user->email,
$user->city,
$user->country,
$user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
);
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>',
$user->email,
$user->city,
$user->country,
$user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
);
}
}

echo $OUTPUT->header();
Expand Down
2 changes: 1 addition & 1 deletion admin/user/user_bulk_delete.php
Expand Up @@ -25,7 +25,7 @@
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);
$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]);
Expand Down
1 change: 1 addition & 0 deletions lang/en/bulkusers.php
Expand Up @@ -30,6 +30,7 @@
$string['allusers'] = 'All users ({$a})';
$string['available'] = 'Available';
$string['confirmmessage'] = 'Do you really want to send the message above to all these users?<br />{$a}';
$string['deleteduser'] = 'Deleted user';
$string['nofilteredusers'] = 'No users found (0/{$a})';
$string['noselectedusers'] = 'No users selected';
$string['removeall'] = 'Remove all';
Expand Down
7 changes: 6 additions & 1 deletion lib/moodlelib.php
Expand Up @@ -4021,7 +4021,7 @@ function truncate_userinfo(array $info) {
* @throws coding_exception if invalid $user parameter detected
*/
function delete_user(stdClass $user) {
global $CFG, $DB;
global $CFG, $DB, $SESSION;
require_once($CFG->libdir.'/grouplib.php');
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/message/lib.php');
Expand Down Expand Up @@ -4119,6 +4119,11 @@ function delete_user(stdClass $user) {
// Remove users customised pages.
$DB->delete_records('my_pages', array('userid' => $user->id, 'private' => 1));

// Delete user from $SESSION->bulk_users.
if (isset($SESSION->bulk_users[$user->id])) {
unset($SESSION->bulk_users[$user->id]);
}

// Force logout - may fail if file based sessions used, sorry.
\core\session\manager::kill_user_sessions($user->id);

Expand Down
29 changes: 28 additions & 1 deletion user/tests/behat/bulk_editenrolment.feature
Expand Up @@ -18,6 +18,9 @@ Feature: Bulk enrolments
| student1 | C1 | student |
| student2 | C1 | student |
| teacher1 | C1 | editingteacher |
And the following "cohorts" exist:
| name | idnumber |
| Cohort | cohortid1 |

@javascript
Scenario: Bulk edit enrolments
Expand All @@ -29,7 +32,7 @@ Feature: Bulk enrolments
And I set the field "Alter status" to "Suspended"
And I press "Save changes"
Then I should see "Suspended" in the "Teacher 1" "table_row"
Then I should see "Suspended" in the "Student 1" "table_row"
And I should see "Suspended" in the "Student 1" "table_row"
And I should see "Suspended" in the "Student 2" "table_row"

@javascript
Expand All @@ -43,3 +46,27 @@ Feature: Bulk enrolments
Then I should not see "Student 1"
And I should not see "Student 2"
And I should not see "Teacher 1"

@javascript
Scenario: Bulk edit enrolment for deleted user
When I log in as "admin"
And I navigate to "Users > Accounts > Bulk user actions" in site administration
And I set the field "Available" to "Student 1"
And I press "Add to selection"
And I set the field "Available" to "Student 2"
And I press "Add to selection"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I set the following fields to these values:
| username | student1 |
And I press "Add filter"
And I click on "Delete" "link"
And I press "Delete"
And I navigate to "Users > Accounts > Bulk user actions" in site administration
And I set the field "id_action" to "Add to cohort"
And I press "Go"
And I set the field "id_cohort" to "Cohort [cohortid1]"
And I press "Add to cohort"
And I navigate to "Users > Accounts > Cohorts" in site administration
And I click on "Assign" "link"
Then the "removeselect" select box should contain "Student 2 (student2@example.com)"
And the "removeselect" select box should not contain "Student 1 (student1@example.com)"
18 changes: 18 additions & 0 deletions user/tests/behat/delete_users.feature
Expand Up @@ -78,3 +78,21 @@ Feature: Deleting users
And the "Available" select box should not contain "User Three"
And the "Available" select box should not contain "User One"
And the "Available" select box should not contain "User Two"

@javascript
Scenario: Deleting a bulked user
When I log in as "admin"
And I navigate to "Users > Accounts > Bulk user actions" in site administration
And I set the field "Available" to "User Two"
And I press "Add to selection"
And I set the field "Available" to "User One"
And I press "Add to selection"
Then I should see "User One"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I set the following fields to these values:
| username | user1 |
And I press "Add filter"
And I click on "Delete" "link"
And I press "Delete"
And I navigate to "Users > Accounts > Bulk user actions" in site administration
Then I should not see "User One"

0 comments on commit acca768

Please sign in to comment.