@@ -1788,10 +1788,18 @@ public static function getSessionsByDuration(string $dateFrom, string $dateUntil
1788
1788
1789
1789
/**
1790
1790
* Return duplicate users at a SortableTableFromArray object
1791
+ * @param string $type The type of duplication we are checking for ('name' or 'email')
1791
1792
*/
1792
- public static function returnDuplicatedUsersTable (array $ additionalExtraFieldsInfo ): SortableTableFromArray
1793
+ public static function returnDuplicatedUsersTable (
1794
+ string $ type = 'name ' ,
1795
+ array $ additionalExtraFieldsInfo
1796
+ ): SortableTableFromArray
1793
1797
{
1794
- $ usersInfo = Statistics::getDuplicatedUsers ($ additionalExtraFieldsInfo );
1798
+ if ($ type == 'email ' ) {
1799
+ $ usersInfo = Statistics::getDuplicatedUserMails ($ additionalExtraFieldsInfo );
1800
+ } else {
1801
+ $ usersInfo = Statistics::getDuplicatedUsers ($ additionalExtraFieldsInfo );
1802
+ }
1795
1803
1796
1804
$ column = 0 ;
1797
1805
@@ -1802,15 +1810,20 @@ public static function returnDuplicatedUsersTable(array $additionalExtraFieldsIn
1802
1810
]);
1803
1811
$ table ->set_header ($ column ++, get_lang ('Id ' ));
1804
1812
1813
+ if ($ type == 'email ' ) {
1814
+ $ table ->set_header ($ column ++, get_lang ('Email ' ));
1815
+ }
1805
1816
if (api_is_western_name_order ()) {
1806
1817
$ table ->set_header ($ column ++, get_lang ('FirstName ' ));
1807
1818
$ table ->set_header ($ column ++, get_lang ('LastName ' ));
1808
1819
} else {
1809
1820
$ table ->set_header ($ column ++, get_lang ('LastName ' ));
1810
1821
$ table ->set_header ($ column ++, get_lang ('FirstName ' ));
1811
1822
}
1823
+ if ($ type == 'name ' ) {
1824
+ $ table ->set_header ($ column ++, get_lang ('Email ' ));
1825
+ }
1812
1826
1813
- $ table ->set_header ($ column ++, get_lang ('Email ' ));
1814
1827
$ table ->set_header ($ column ++, get_lang ('RegistrationDate ' ));
1815
1828
$ table ->set_column_filter (
1816
1829
$ column - 1 ,
@@ -1984,4 +1997,85 @@ private static function getDuplicatedUsers(array $additionalExtraFieldsInfo): ar
1984
1997
1985
1998
return $ usersInfo ;
1986
1999
}
2000
+
2001
+ /**
2002
+ * Get a list of duplicated user emails
2003
+ * @param array $additionalExtraFieldsInfo A list of extra fields we want to get in return, additional to the user details
2004
+ * @return array
2005
+ */
2006
+ private static function getDuplicatedUserMails (array $ additionalExtraFieldsInfo ): array
2007
+ {
2008
+ $ sql = "SELECT email, COUNT(*) as count
2009
+ FROM user
2010
+ GROUP BY email
2011
+ HAVING count > 1
2012
+ ORDER BY email "
2013
+ ;
2014
+
2015
+ $ result = Database::query ($ sql );
2016
+
2017
+ if (1 > Database::num_rows ($ result )) {
2018
+ return [];
2019
+ }
2020
+
2021
+ $ usersInfo = [];
2022
+
2023
+ while ($ rowStat = Database::fetch_assoc ($ result )) {
2024
+ $ email = Database::escape_string ($ rowStat ['email ' ]);
2025
+ $ subsql = "SELECT id, firstname, lastname, registration_date, status, active
2026
+ FROM user WHERE email = ' $ email' "
2027
+ ;
2028
+
2029
+ $ subResult = Database::query ($ subsql );
2030
+
2031
+ if (1 > Database::num_rows ($ subResult )) {
2032
+ continue ;
2033
+ }
2034
+
2035
+ $ objExtraValue = new ExtraFieldValue ('user ' );
2036
+
2037
+ while ($ rowUser = Database::fetch_assoc ($ subResult )) {
2038
+ $ studentId = $ rowUser ['id ' ];
2039
+
2040
+ $ studentInfo = [];
2041
+ $ studentInfo [] = $ rowUser ['id ' ];
2042
+
2043
+ $ studentInfo [] = $ rowStat ['email ' ];
2044
+ if (api_is_western_name_order ()) {
2045
+ $ studentInfo [] = $ rowUser ['firstname ' ];
2046
+ $ studentInfo [] = $ rowUser ['lastname ' ];
2047
+ } else {
2048
+ $ studentInfo [] = $ rowUser ['lastname ' ];
2049
+ $ studentInfo [] = $ rowUser ['firstname ' ];
2050
+ }
2051
+
2052
+ $ studentInfo [] = $ rowUser ['registration_date ' ];
2053
+ $ studentInfo [] = Tracking::get_first_connection_date (
2054
+ $ studentId ,
2055
+ DATE_TIME_FORMAT_LONG
2056
+ );
2057
+ $ studentInfo [] = Tracking::get_last_connection_date (
2058
+ $ studentId ,
2059
+ true ,
2060
+ false ,
2061
+ DATE_TIME_FORMAT_LONG
2062
+ );
2063
+ $ studentInfo [] = $ rowUser ['status ' ];
2064
+ $ studentInfo [] = Tracking::count_course_per_student ($ studentId );
2065
+ $ studentInfo [] = Tracking::countSessionsPerStudent ($ studentId );
2066
+
2067
+ foreach ($ additionalExtraFieldsInfo as $ fieldInfo ) {
2068
+ $ extraValue = $ objExtraValue ->get_values_by_handler_and_field_id ($ studentId , $ fieldInfo ['id ' ], true );
2069
+ $ studentInfo [] = $ extraValue ['value ' ] ?? null ;
2070
+ }
2071
+
2072
+ $ studentInfo [] = $ rowUser ['active ' ]; // once to show status
2073
+ $ studentInfo [] = $ rowUser ['active ' ]; // twice to show actions
2074
+
2075
+ $ usersInfo [] = $ studentInfo ;
2076
+ }
2077
+ }
2078
+
2079
+ return $ usersInfo ;
2080
+ }
1987
2081
}
0 commit comments