Skip to content

Commit f8f3638

Browse files
committed
User: Account unification: add the possibility to filter on user status and the unification on a teacher account if one exist instead of the most recent account - refs BT#22702
1 parent 170796a commit f8f3638

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

main/inc/lib/myspace.lib.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4583,18 +4583,22 @@ public static function duGetDuplicateValues(int $fieldId, int $urlId): array
45834583
}
45844584

45854585
/** Users having exactly a value for the given extra-field (filtered by portal). */
4586-
public static function duGetUsersByFieldValue(int $fieldId, int $urlId, string $value): array
4586+
public static function duGetUsersByFieldValue(int $fieldId, int $urlId, string $value, string $userStatusList = ''): array
45874587
{
45884588
$tblVals = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
45894589
$tblUser = Database::get_main_table(TABLE_MAIN_USER);
45904590
$tblRel = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
45914591
$valueSql = "'".Database::escape_string($value)."'";
4592-
4592+
$filterUserStatus = "";
4593+
if ($userStatusList != '') {
4594+
$filterUserStatus = "AND u.status in (".$userStatusList.")";
4595+
}
45934596
$sql = "SELECT DISTINCT u.id AS user_id, u.username, u.firstname, u.lastname, u.email, u.registration_date
45944597
FROM $tblUser u
45954598
JOIN $tblRel r ON r.user_id = u.id AND r.access_url_id = $urlId
45964599
JOIN $tblVals v ON v.item_id = u.id AND v.field_id = $fieldId
45974600
WHERE v.value = $valueSql
4601+
$filterUserStatus
45984602
ORDER BY u.id ASC";
45994603
$res = Database::query($sql);
46004604

tests/scripts/unify_duplicated_user_based_on_extra_field_value.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
* This script takes a user extra field variabler and looks for users duplicated
77
* based on this extra field values and unify them on the most recent account
8+
* or the first teacher account if one has a teacher status/role
89
*/
910
exit;
1011
$now = date('Y-m-d H:i:s');
@@ -30,6 +31,12 @@
3031
echo PHP_EOL;
3132
}
3233

34+
// Filter user status to only search for user with those status
35+
// $filteredUserStatusList = "1,5";
36+
if (!empty($filteredUserStatusList)) {
37+
echo "Filtered user status list is defined: " . $filteredUserStatusList . PHP_EOL;
38+
}
39+
3340
// set the extra field variable to use
3441
$extraFieldVariable = 'dni';
3542

@@ -63,12 +70,18 @@
6370
echo "Value $value is in the filtered list. Proceeding..." . PHP_EOL;
6471
}
6572
echo 'Analysing duplicates of ' . $value . ' ...' . PHP_EOL;
66-
$users = MySpace::duGetUsersByFieldValue($fieldId, $urlId, $value);
73+
$users = MySpace::duGetUsersByFieldValue($fieldId, $urlId, $value, $filteredUserStatusList);
6774
$userIdToUnifyOn = 0;
6875
$mostRecentRegistrationDate = 0;
6976
foreach ($users as $u) {
7077
$uid = (int)$u['user_id'];
7178
$userInfo = api_get_user_info($uid);
79+
// unify on the teacher account if exist
80+
if ($userInfo['status'] == 1) {
81+
$userIdToUnifyOn = $uid;
82+
break;
83+
}
84+
// unify on the most recent account if none is teacher
7285
if ($userInfo['registration_date'] > $mostRecentRegistrationDate) {
7386
$mostRecentRegistrationDate = $userInfo['registration_date'];
7487
$userIdToUnifyOn = $uid;

0 commit comments

Comments
 (0)