diff --git a/README.md b/README.md index 77a3f1f..44ba43a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ The plugin aims to make users that are suspended **anonymous**. Therefore, the f (usernames must be unique therefore the id is appended). - The field `firstname` is set to *Anonym*. - Consequently, references in e.g. the forum activity merely refer to a user called `Anonym`. + - `username` and `firstname` of suspended users can be customized - Replaces all other data in the `user` table with the appropriate null value. - When viewing the page of the user he/she cannot be identified. diff --git a/classes/archiveduser.php b/classes/archiveduser.php index 485ee25..45462af 100644 --- a/classes/archiveduser.php +++ b/classes/archiveduser.php @@ -207,7 +207,7 @@ public function delete_me() { } /** - * Creates an empty user with 'anonym + id' as username and 'Anonym' as Firstname. + * Creates an empty user with ':suspendusername + id' as username and ':suspendfirstname' as Firstname. * * @param int $id * @param int $timestamp @@ -217,8 +217,8 @@ private function give_suspended_pseudo_user($id, $timestamp) { $cloneuser = new \stdClass(); $cloneuser->id = $id; // Usernames have to be unique therefore the id is used. - $cloneuser->username = 'anonym' . $id; - $cloneuser->firstname = 'Anonym'; + $cloneuser->username = get_config('tool_cleanupusers_settings', 'suspendusername') . $id; + $cloneuser->firstname = get_config('tool_cleanupusers_settings', 'suspendfirstname'); $cloneuser->lastname = ''; $cloneuser->suspended = 1; $cloneuser->email = ''; diff --git a/lang/en/tool_cleanupusers.php b/lang/en/tool_cleanupusers.php index 3691a1d..503504a 100644 --- a/lang/en/tool_cleanupusers.php +++ b/lang/en/tool_cleanupusers.php @@ -66,3 +66,10 @@ $string['cronjobcomplete'] = 'tool_cleanupusers cron job complete'; $string['cronjobwasrunning'] = 'The tool_cleanupusers cron job was running. No user was suspended or deleted.'; $string['using-plugin'] = 'You are currently using the {$a} Plugin'; +$string['suspendusername'] = 'anonym'; +$string['suspendfirstname'] = 'Anonym'; +$string['sett_title'] = 'Configure suspended users'; +$string['sett_suspendusername'] = 'Username for suspended'; +$string['sett_suspendfirstname'] = 'Firstname for suspended'; +$string['sett_suspendusername_description'] = 'Choose a username for suspended users (must be lowercase)'; +$string['sett_suspendfirstname_description'] = 'Choose a firstname for suspended users'; diff --git a/settings.php b/settings.php index 9a89e33..bb09d9a 100644 --- a/settings.php +++ b/settings.php @@ -33,6 +33,22 @@ get_string('pluginsettingstitle', 'tool_cleanupusers'), "$CFG->wwwroot/$CFG->admin/tool/cleanupusers/index.php" )); + $settings = new admin_settingpage('tool_cleanupusers_settings', get_string('sett_title', 'tool_cleanupusers')); + $settings->add(new admin_setting_configtext( + 'tool_cleanupusers_settings/suspendusername', + get_string('sett_suspendusername', 'tool_cleanupusers'), + get_string('sett_suspendusername_description', 'tool_cleanupusers'), + get_string('suspendusername', 'tool_cleanupusers'), + PARAM_TEXT + )); + $settings->add(new admin_setting_configtext( + 'tool_cleanupusers_settings/suspendfirstname', + get_string('sett_suspendfirstname', 'tool_cleanupusers'), + get_string('sett_suspendfirstname_description', 'tool_cleanupusers'), + get_string('suspendfirstname', 'tool_cleanupusers'), + PARAM_TEXT + )); + $ADMIN->add('tool_cleanupusers', $settings); // Add entry for own settings. $ADMIN->add('tool_cleanupusers', new admin_externalpage( 'Manage never logged in', diff --git a/tests/generator/lib.php b/tests/generator/lib.php index f8a8f51..18d9696 100644 --- a/tests/generator/lib.php +++ b/tests/generator/lib.php @@ -71,7 +71,10 @@ public function test_create_preparation() { 'lastaccess' => $timestamponeyearago, 'suspended' => '0']); $useroneyearnotloggedin->realusername = $userneverloggedin->username; - $usersuspendedbypluginandmanually = $generator->create_user(['username' => 'anonym-x', 'suspended' => '1']); + $usersuspendedbypluginandmanually = $generator->create_user(['username' => get_config( + 'tool_cleanupusers_settings', + 'suspendusername' + ) . '-x', 'suspended' => '1']); $usersuspendedbypluginandmanually->realusername = 'somerealusername'; $DB->insert_record_raw('tool_cleanupusers', ['id' => $usersuspendedbypluginandmanually->id, 'archived' => 1, 'timestamp' => $tendaysago], true, false, true); @@ -86,8 +89,11 @@ public function test_create_preparation() { 'lastaccess' => $timestamponeyearago]); $userdeleted->realusername = $userdeleted->username; - $usersuspendedbyplugin = $generator->create_user(['username' => 'anonym-y', 'suspended' => '1', - 'firstname' => 'Anonym']); + $usersuspendedbyplugin = $generator->create_user(['username' => get_config( + 'tool_cleanupusers_settings', + 'suspendusername' + ) . '-y', 'suspended' => '1', + 'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]); $usersuspendedbyplugin->realusername = 'usersuspendedbyplugin'; $DB->insert_record_raw('tool_cleanupusers', ['id' => $usersuspendedbyplugin->id, 'archived' => true, 'timestamp' => $timestamponeyearago], true, false, true); @@ -101,7 +107,10 @@ public function test_create_preparation() { ); $userinconsistentsuspended = $generator->create_user(['username' => 'userinconsistentarchivedbyplugin', - 'suspended' => '1', 'firstname' => 'Anonym', 'lastaccess' => $timestamponeyearago]); + 'suspended' => '1', 'firstname' => get_config( + 'tool_cleanupusers_settings', + 'suspendfirstname' + ), 'lastaccess' => $timestamponeyearago]); $userinconsistentsuspended->realusername = $userinconsistentsuspended->username; $DB->insert_record_raw( 'tool_cleanupusers_archive', @@ -113,11 +122,11 @@ public function test_create_preparation() { ); $userduplicatedname = $generator->create_user(['username' => 'duplicatedname', - 'suspended' => '0', 'firstname' => 'Anonym']); + 'suspended' => '0', 'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]); $userduplicatedname->realusername = $userduplicatedname->username; - $originaluser = $generator->create_user(['username' => 'anonym-z', - 'suspended' => '1', 'firstname' => 'Anonym']); + $originaluser = $generator->create_user(['username' => get_config('tool_cleanupusers_settings', 'suspendusername') . '-z', + 'suspended' => '1', 'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]); $originaluser->realusername = $userduplicatedname->username; $DB->insert_record_raw( 'tool_cleanupusers_archive', diff --git a/tests/tool_cleanupusers_test.php b/tests/tool_cleanupusers_test.php index a2b2af2..90cd126 100644 --- a/tests/tool_cleanupusers_test.php +++ b/tests/tool_cleanupusers_test.php @@ -68,7 +68,8 @@ public function test_archiveduser_archiveme() { // Users that are archived will be marked as suspended in the user table and transfer their previous suspended // status in the tool_cleanupusers table. - // Additionally, they will be anonymized in the user table. Firstname will be 'Anonym', Username will be 'anonym + id'. + // Additionally, they will be anonymized in the user table. Firstname will be :suspendfirstname, + // Username will be ':suspendusername + id'. $user = new archiveduser( $data['user']->id, @@ -84,8 +85,11 @@ public function test_archiveduser_archiveme() { $this->assertEquals(1, $recordusertable->suspended); $this->assertEquals(0, $recordshadowtable->suspended); $this->assertEquals(1, $recordtooltable->archived); - $this->assertEquals('Anonym', $recordusertable->firstname); - $this->assertEquals('anonym' . $data['user']->id, $recordusertable->username); + $this->assertEquals(get_config('tool_cleanupusers_settings', 'suspendfirstname'), $recordusertable->firstname); + $this->assertEquals(get_config( + 'tool_cleanupusers_settings', + 'suspendusername' + ) . $data['user']->id, $recordusertable->username); $this->resetAfterTest(true); } @@ -496,7 +500,10 @@ public function test_cronjob() { $this->assert_user_equals($data['useroneyearnotloggedin'], $recordtooltable2); $this->assertEquals(1, $recordusertable->suspended); $this->assertEquals(0, $recordtooltable2->suspended); - $this->assertEquals('anonym' . $data['useroneyearnotloggedin']->id, $recordusertable->username); + $this->assertEquals(get_config( + 'tool_cleanupusers_settings', + 'suspendusername' + ) . $data['useroneyearnotloggedin']->id, $recordusertable->username); $this->assertEquals(0, $recordusertable->deleted); // User is deleted. diff --git a/userstatus/timechecker/classes/timechecker.php b/userstatus/timechecker/classes/timechecker.php index 424bcac..b640bec 100644 --- a/userstatus/timechecker/classes/timechecker.php +++ b/userstatus/timechecker/classes/timechecker.php @@ -91,20 +91,20 @@ public function get_to_suspend() { */ public function get_never_logged_in() { global $DB; - $select = 'lastaccess=0 AND deleted=0 AND firstname!=\'Anonym\''; - $arrayofuser = $DB->get_records_select('user', $select); + $arrayofuser = $DB->get_records_sql( + "SELECT u.id, u.suspended, u.lastaccess, u.username, u.deleted + FROM {user} u + LEFT JOIN {tool_cleanupusers} tc ON u.id = tc.id + WHERE u.auth = 'shibboleth' + AND u.lastaccess = 0 + AND u.deleted = 0 + AND tc.id IS NULL" + ); $neverloggedin = []; foreach ($arrayofuser as $key => $user) { - if (empty($user->lastaccess) && $user->deleted == 0) { - $informationuser = new archiveduser( - $user->id, - $user->suspended, - $user->lastaccess, - $user->username, - $user->deleted - ); - $neverloggedin[$key] = $informationuser; - } + $informationuser = new archiveduser($user->id, $user->suspended, + $user->lastaccess, $user->username, $user->deleted); + $neverloggedin[$key] = $informationuser; } return $neverloggedin; } diff --git a/userstatus/timechecker/tests/generator/lib.php b/userstatus/timechecker/tests/generator/lib.php index c773acd..19f5b18 100644 --- a/userstatus/timechecker/tests/generator/lib.php +++ b/userstatus/timechecker/tests/generator/lib.php @@ -72,7 +72,10 @@ public function test_create_preparation() { // User suspended by the plugin. $tendaysago = $mytimestamp - 864000; - $reactivate = $generator->create_user(['username' => 'anonym', 'suspended' => 1]); + $reactivate = $generator->create_user(['username' => get_config( + 'tool_cleanupusers_settings', + 'suspendusername' + ), 'suspended' => 1]); $DB->insert_record_raw('tool_cleanupusers', ['id' => $reactivate->id, 'archived' => true, 'timestamp' => $tendaysago], true, false, true); $DB->insert_record_raw('tool_cleanupusers_archive', ['id' => $reactivate->id, 'username' => 'reactivate', diff --git a/userstatus/timechecker/version.php b/userstatus/timechecker/version.php index 1b5125b..6d1ef57 100644 --- a/userstatus/timechecker/version.php +++ b/userstatus/timechecker/version.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017010500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2024061800; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2016052300; // Requires 31 Moodle version. $plugin->component = 'userstatus_timechecker'; // Full name of the plugin (used for diagnostics). $plugin->release = 'v1.0-r0'; diff --git a/version.php b/version.php index 9d18615..2dad661 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023120800; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2024061800; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2022041900; // Requires Moodle version 4.0 or higher. $plugin->component = 'tool_cleanupusers'; // Full name of the plugin (used for diagnostics). $plugin->release = 'v1.0-r2';