Skip to content

Commit

Permalink
Admin: Statistics: Add list of duplicated users by e-mail - refs BT#2…
Browse files Browse the repository at this point in the history
…1146
  • Loading branch information
ywarnier committed Oct 24, 2023
1 parent 44b313e commit a12dae7
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
34 changes: 33 additions & 1 deletion main/admin/statistics/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@
'report=users_online' => get_lang('UsersOnline'),
'report=invoicing' => get_lang('InvoicingByAccessUrl'),
'report=duplicated_users' => get_lang('DuplicatedUsers'),
'report=duplicated_users_by_mail' => get_lang('DuplicatedUsersByMail'),
],
get_lang('System') => [
'report=activities' => get_lang('ImportantActivities'),
Expand Down Expand Up @@ -1859,7 +1860,7 @@ function loadReportQuarterlyTotalDiskUsage () {
$additionalExtraFieldsInfo = TrackingCourseLog::getAdditionalProfileExtraFields();

$frmFields = TrackingCourseLog::displayAdditionalProfileFields([], api_get_self());
$table = Statistics::returnDuplicatedUsersTable($additionalExtraFieldsInfo);
$table = Statistics::returnDuplicatedUsersTable('name', $additionalExtraFieldsInfo);

if (isset($_GET['action_table'])) {
$data = $table->toArray(true, true);
Expand All @@ -1878,6 +1879,37 @@ function loadReportQuarterlyTotalDiskUsage () {
$content .= Display::page_subheader2(get_lang('DuplicatedUsers'));
$content .= Display::return_message(get_lang('ThisReportOnlyListsUsersThatHaveTheSameFirstnameAndLastname'));

$content .= $frmFields;
$content .= $table->return_table();
break;
case 'duplicated_users_by_mail':
$interbreadcrumb[] = [
'name' => $tool_name,
'url' => 'index.php',
];

$additionalExtraFieldsInfo = TrackingCourseLog::getAdditionalProfileExtraFields();

$frmFields = TrackingCourseLog::displayAdditionalProfileFields([], api_get_self());
$table = Statistics::returnDuplicatedUsersTable('email', $additionalExtraFieldsInfo);

if (isset($_GET['action_table'])) {
$data = $table->toArray(true, true);

if ('export_excel' === $_GET['action_table']) {
Export::arrayToXls($data);
} elseif ('export_csv' === $_GET['action_table']) {
Export::arrayToCsv($data);
}

exit;
}

$htmlHeadXtra[] = '<script>'.UserManager::getScriptFunctionForActiveFilter().'</script>';

$content .= Display::page_subheader2(get_lang('DuplicatedUsersByMail'));
$content .= Display::return_message(get_lang('ThisReportOnlyListsUsersThatHaveTheSameEmail'));

$content .= $frmFields;
$content .= $table->return_table();
break;
Expand Down
100 changes: 97 additions & 3 deletions main/inc/lib/statistics.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1788,10 +1788,18 @@ public static function getSessionsByDuration(string $dateFrom, string $dateUntil

/**
* Return duplicate users at a SortableTableFromArray object
* @param string $type The type of duplication we are checking for ('name' or 'email')
*/
public static function returnDuplicatedUsersTable(array $additionalExtraFieldsInfo): SortableTableFromArray
public static function returnDuplicatedUsersTable(
string $type = 'name',
array $additionalExtraFieldsInfo
): SortableTableFromArray
{
$usersInfo = Statistics::getDuplicatedUsers($additionalExtraFieldsInfo);
if ($type == 'email') {
$usersInfo = Statistics::getDuplicatedUserMails($additionalExtraFieldsInfo);
} else {
$usersInfo = Statistics::getDuplicatedUsers($additionalExtraFieldsInfo);
}

$column = 0;

Expand All @@ -1802,15 +1810,20 @@ public static function returnDuplicatedUsersTable(array $additionalExtraFieldsIn
]);
$table->set_header($column++, get_lang('Id'));

if ($type == 'email') {
$table->set_header($column++, get_lang('Email'));
}
if (api_is_western_name_order()) {
$table->set_header($column++, get_lang('FirstName'));
$table->set_header($column++, get_lang('LastName'));
} else {
$table->set_header($column++, get_lang('LastName'));
$table->set_header($column++, get_lang('FirstName'));
}
if ($type == 'name') {
$table->set_header($column++, get_lang('Email'));
}

$table->set_header($column++, get_lang('Email'));
$table->set_header($column++, get_lang('RegistrationDate'));
$table->set_column_filter(
$column - 1,
Expand Down Expand Up @@ -1984,4 +1997,85 @@ private static function getDuplicatedUsers(array $additionalExtraFieldsInfo): ar

return $usersInfo;
}

/**
* Get a list of duplicated user emails
* @param array $additionalExtraFieldsInfo A list of extra fields we want to get in return, additional to the user details
* @return array
*/
private static function getDuplicatedUserMails(array $additionalExtraFieldsInfo): array
{
$sql = "SELECT email, COUNT(*) as count
FROM user
GROUP BY email
HAVING count > 1
ORDER BY email"
;

$result = Database::query($sql);

if (1 > Database::num_rows($result)) {
return [];
}

$usersInfo = [];

while ($rowStat = Database::fetch_assoc($result)) {
$email = Database::escape_string($rowStat['email']);
$subsql = "SELECT id, firstname, lastname, registration_date, status, active
FROM user WHERE email = '$email'"
;

$subResult = Database::query($subsql);

if (1 > Database::num_rows($subResult)) {
continue;
}

$objExtraValue = new ExtraFieldValue('user');

while ($rowUser = Database::fetch_assoc($subResult)) {
$studentId = $rowUser['id'];

$studentInfo = [];
$studentInfo[] = $rowUser['id'];

$studentInfo[] = $rowStat['email'];
if (api_is_western_name_order()) {
$studentInfo[] = $rowUser['firstname'];
$studentInfo[] = $rowUser['lastname'];
} else {
$studentInfo[] = $rowUser['lastname'];
$studentInfo[] = $rowUser['firstname'];
}

$studentInfo[] = $rowUser['registration_date'];
$studentInfo[] = Tracking::get_first_connection_date(
$studentId,
DATE_TIME_FORMAT_LONG
);
$studentInfo[] = Tracking::get_last_connection_date(
$studentId,
true,
false,
DATE_TIME_FORMAT_LONG
);
$studentInfo[] = $rowUser['status'];
$studentInfo[] = Tracking::count_course_per_student($studentId);
$studentInfo[] = Tracking::countSessionsPerStudent($studentId);

foreach ($additionalExtraFieldsInfo as $fieldInfo) {
$extraValue = $objExtraValue->get_values_by_handler_and_field_id($studentId, $fieldInfo['id'], true);
$studentInfo[] = $extraValue['value'] ?? null;
}

$studentInfo[] = $rowUser['active']; // once to show status
$studentInfo[] = $rowUser['active']; // twice to show actions

$usersInfo[] = $studentInfo;
}
}

return $usersInfo;
}
}

0 comments on commit a12dae7

Please sign in to comment.