1717 * - Username format: lastname + first letter of each firstname word; for active duplicates, append next letter from last firstname part
1818 * - For 3+ occurrences of lastname + firstname, append increasing letters from last firstname part (e.g., jpii, jpiii)
1919 * - Generates unmatched_db_users.xlsx listing database users not found in the input XLSX based on username
20- * - Exports terminal output to import-yyyymmddhhiiss.log in the output directory
20+ * - Exports terminal output to import-yyyymmddhhiiss.log in the output directory.
2121 */
2222
2323// Ensure the script is run from the command line
2424if (php_sapi_name () !== 'cli ' ) {
25- die ('This script must be run from the command line. ' );
25+ exit ('This script must be run from the command line. ' );
2626}
2727
2828// Configuration
5757// Validate and prepare output directory
5858if (!is_dir ($ outputDir )) {
5959 if (!mkdir ($ outputDir , 0755 , true )) {
60- die ("Error: Could not create output directory ' $ outputDir' \n" );
60+ exit ("Error: Could not create output directory ' $ outputDir' \n" );
6161 }
6262}
6363if (!is_writable ($ outputDir )) {
64- die ("Error: Output directory ' $ outputDir' is not writable \n" );
64+ exit ("Error: Output directory ' $ outputDir' is not writable \n" );
6565}
6666// Ensure trailing slash for consistency
6767$ outputDir = rtrim ($ outputDir , '/ ' ).'/ ' ;
8080echo " Output directory: $ outputDir \n" ;
8181
8282if (empty ($ xlsxFile ) || !file_exists ($ xlsxFile )) {
83- die ("Usage: php import_users_from_xlsx.php <path_to_xlsx_file> [-p|--proceed] [-o <directory>|--output-dir=<directory>] \n" );
83+ exit ("Usage: php import_users_from_xlsx.php <path_to_xlsx_file> [-p|--proceed] [-o <directory>|--output-dir=<directory>] \n" );
8484}
8585
8686// Initialize database connection
9494 $ worksheet = $ phpExcel ->getActiveSheet ();
9595 $ xlsxRows = $ worksheet ->toArray ();
9696} catch (Exception $ e ) {
97- die ("Error loading XLSX file: {$ e ->getMessage ()}\n" );
97+ exit ("Error loading XLSX file: {$ e ->getMessage ()}\n" );
9898}
9999
100100// Map XLSX columns to Chamilo database user table fields
115115foreach ($ xlsxColumnMap as $ xlsxHeader => $ dbField ) {
116116 $ index = array_search ($ xlsxHeader , $ xlsxHeaders );
117117 if ($ index === false ) {
118- die ("Missing required column: {$ xlsxHeader }\n" );
118+ exit ("Missing required column: {$ xlsxHeader }\n" );
119119 }
120120 $ xlsxColumnIndices [$ dbField ] = $ index ;
121121}
@@ -138,21 +138,25 @@ function normalizeName($name)
138138{
139139 $ name = strtolower (trim ($ name ));
140140 $ name = preg_replace ('/[\s-]+/ ' , ' ' , $ name );
141+
141142 return $ name ;
142143}
143144
144145// Remove accents from strings
145- function removeAccents ($ str ) {
146+ function removeAccents ($ str )
147+ {
146148 $ str = str_replace (
147- ['à ' ,'á ' ,'â ' ,'ã ' ,'ä ' ,'ç ' ,'è ' ,'é ' ,'ê ' ,'ë ' ,'ì ' ,'í ' ,'î ' ,'ï ' ,'ñ ' ,'ò ' ,'ó ' ,'ô ' ,'õ ' ,'ö ' ,'ù ' ,'ú ' ,'û ' ,'ü ' ,'ý ' ,'ÿ ' ,'À ' ,'Á ' ,'Â ' ,'Ã ' ,'Ä ' ,'Å ' ,'Ç ' ,'È ' ,'É ' ,'Ê ' ,'Ë ' ,'Ì ' ,'Í ' ,'Î ' ,'Ï ' ,'Ñ ' ,'Ò ' ,'Ó ' ,'Ô ' ,'Õ ' ,'Ö ' ,'Ù ' ,'Ú ' ,'Û ' ,'Ü ' ,'Ý ' ],
148- ['a ' ,'a ' ,'a ' ,'a ' ,'a ' ,'c ' ,'e ' ,'e ' ,'e ' ,'e ' ,'i ' ,'i ' ,'i ' ,'i ' ,'n ' ,'o ' ,'o ' ,'o ' ,'o ' ,'o ' ,'u ' ,'u ' ,'u ' ,'u ' ,'y ' ,'y ' ,'A ' ,'A ' ,'A ' ,'A ' ,'A ' ,'A ' ,'C ' ,'E ' ,'E ' ,'E ' ,'E ' ,'I ' ,'I ' ,'I ' ,'I ' ,'N ' ,'O ' ,'O ' ,'O ' ,'O ' ,'O ' ,'U ' ,'U ' ,'U ' ,'U ' ,'Y ' ],
149+ ['à ' , 'á ' , 'â ' , 'ã ' , 'ä ' , 'ç ' , 'è ' , 'é ' , 'ê ' , 'ë ' , 'ì ' , 'í ' , 'î ' , 'ï ' , 'ñ ' , 'ò ' , 'ó ' , 'ô ' , 'õ ' , 'ö ' , 'ù ' , 'ú ' , 'û ' , 'ü ' , 'ý ' , 'ÿ ' , 'À ' , 'Á ' , 'Â ' , 'Ã ' , 'Ä ' , 'Å ' , 'Ç ' , 'È ' , 'É ' , 'Ê ' , 'Ë ' , 'Ì ' , 'Í ' , 'Î ' , 'Ï ' , 'Ñ ' , 'Ò ' , 'Ó ' , 'Ô ' , 'Õ ' , 'Ö ' , 'Ù ' , 'Ú ' , 'Û ' , 'Ü ' , 'Ý ' ],
150+ ['a ' , 'a ' , 'a ' , 'a ' , 'a ' , 'c ' , 'e ' , 'e ' , 'e ' , 'e ' , 'i ' , 'i ' , 'i ' , 'i ' , 'n ' , 'o ' , 'o ' , 'o ' , 'o ' , 'o ' , 'u ' , 'u ' , 'u ' , 'u ' , 'y ' , 'y ' , 'A ' , 'A ' , 'A ' , 'A ' , 'A ' , 'A ' , 'C ' , 'E ' , 'E ' , 'E ' , 'E ' , 'I ' , 'I ' , 'I ' , 'I ' , 'N ' , 'O ' , 'O ' , 'O ' , 'O ' , 'O ' , 'U ' , 'U ' , 'U ' , 'U ' , 'Y ' ],
149151 $ str
150152 );
153+
151154 return $ str ;
152155}
153156
154157// Generate login based on lastname and firstname
155- function generateProposedLogin ($ xlsxLastname , $ xlsxFirstname , $ isActive , &$ usedLogins ) {
158+ function generateProposedLogin ($ xlsxLastname , $ xlsxFirstname , $ isActive , &$ usedLogins )
159+ {
156160 $ lastname = strtolower (trim (removeAccents ($ xlsxLastname )));
157161 $ lastname = preg_replace ('/[\s-]+/ ' , '' , $ lastname );
158162
@@ -210,13 +214,16 @@ function generateProposedLogin($xlsxLastname, $xlsxFirstname, $isActive, &$usedL
210214
211215 // Store login with active status
212216 $ usedLogins ['logins ' ][$ login ] = ['active ' => $ isActive ];
217+
213218 return $ login ;
214219}
215220
216221// Generate XLSX files for missing fields and duplicates
217- function createMissingFieldFile ($ filename , $ rows , $ columns ) {
222+ function createMissingFieldFile ($ filename , $ rows , $ columns )
223+ {
218224 if (empty ($ rows )) {
219225 echo "No rows to write for $ filename \n" ;
226+
220227 return ;
221228 }
222229
@@ -526,7 +533,7 @@ function createMissingFieldFile($filename, $rows, $columns) {
526533 'Official Code ' => $ xlsxUserData ['official_code ' ],
527534 'E-mail ' => $ xlsxUserData ['email ' ],
528535 'External User ID ' => $ xlsxMatricule ,
529- 'Updated Fields ' => implode (', ' , array_map (function ($ update ) { return trim (explode (': ' , $ update )[0 ]); }, $ updates )),
536+ 'Updated Fields ' => implode (', ' , array_map (function ($ update ) { return trim (explode (': ' , $ update )[0 ]); }, $ updates )),
530537 ];
531538 } else {
532539 echo " Error: Could not update user (username: $ dbUsername) \n" ;
@@ -561,7 +568,7 @@ function createMissingFieldFile($filename, $rows, $columns) {
561568 'Official Code ' => $ xlsxUserData ['official_code ' ],
562569 'E-mail ' => $ xlsxUserData ['email ' ],
563570 'External User ID ' => $ xlsxMatricule ,
564- 'Updated Fields ' => implode (', ' , array_map (function ($ update ) { return trim (explode (': ' , $ update )[0 ]); }, $ updates )),
571+ 'Updated Fields ' => implode (', ' , array_map (function ($ update ) { return trim (explode (': ' , $ update )[0 ]); }, $ updates )),
565572 ];
566573 }
567574 } else {
0 commit comments