Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new setting to allow replacing name and surname instead of scrambling #180

Open
wants to merge 1 commit into
base: MOODLE_311_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions cleaner/users/classes/clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
class clean extends \local_datacleaner\clean {
const TASK = 'Scrambling user data';
const USERNAME_PREFIX = 'user_';
const FIRST_NAME_PREFIX = 'anonfirstname';
const LAST_NAME_PREFIX = 'anonlastname';

/**
* A SQL string with the comma-separated IDs of the users to update.
Expand Down Expand Up @@ -96,13 +98,39 @@ private static function replace_usernames() {
$DB->execute($sql);
}

private static function replace_first_and_last_names() {
global $DB;

echo "Updating all first and last names...\n";

$where = 'id IN ('.self::$idstoupdate.')';
$firstprefix = self::FIRST_NAME_PREFIX;
$lastprefix = self::LAST_NAME_PREFIX;
$sql = <<<SQL
UPDATE {user}
SET firstname = CONCAT('{$firstprefix}', id), lastname = CONCAT('{$lastprefix}', id)
WHERE $where
SQL;
$DB->execute($sql);
}

private static function scramble_fields() {
$fieldset = [
'main names' => ['firstname', 'lastname'],
'other names' => ['firstnamephonetic', 'alternatename', 'middlename', 'lastnamephonetic'],
'department' => ['institution', 'department'],
'address' => ['address', 'city', 'country', 'lang', 'calendartype', 'timezone'],
];
$config = get_config('cleaner_users');
if (isset($config->renameusers) && $config->renameusers == 1) {
$fieldset = [
'other names' => ['firstnamephonetic', 'alternatename', 'middlename', 'lastnamephonetic'],
'department' => ['institution', 'department'],
'address' => ['address', 'city', 'country', 'lang', 'calendartype', 'timezone'],
];
self::replace_first_and_last_names();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be all or nothing? ie we either scramble all the names, OR we replace all the names. We don't want to do both? The scrambling is mildly expensive so we don't want to replace them and then needlessly scramble them again

Some unit tests would be good too

}else{
$fieldset = [
'main names' => ['firstname', 'lastname'],
'other names' => ['firstnamephonetic', 'alternatename', 'middlename', 'lastnamephonetic'],
'department' => ['institution', 'department'],
'address' => ['address', 'city', 'country', 'lang', 'calendartype', 'timezone'],
];
}

foreach ($fieldset as $title => $fields) {
echo "Scrambling: {$title} ...\n";
Expand Down
2 changes: 2 additions & 0 deletions cleaner/users/lang/en/cleaner_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
$string['keepsiteadminsdesc'] = 'Tick to avoid modifying site administrator accounts and data.';
$string['keepusernames'] = 'Non site administrator usernames';
$string['keepusernamesdesc'] = 'A comma separated list of non site administrator usernames that should be untouched.';
$string['renameusers'] = 'Anonymise users names and surnames';
$string['renameusersdesc'] = 'Change users names and surnames, ex. "anonfirstname1 anonlastname1", instead of scrambling';
3 changes: 3 additions & 0 deletions cleaner/users/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@
$settings->add(new admin_setting_configtextarea('cleaner_users/keepusernames',
new lang_string('keepusernames', 'cleaner_users'),
new lang_string('keepusernamesdesc', 'cleaner_users'), '', PARAM_RAW));

$settings->add(new admin_setting_configcheckbox('cleaner_users/renameusers', new lang_string('renameusers', 'cleaner_users'),
new lang_string('renameusersdesc', 'cleaner_users'), 0));
Loading