0) { $row = Database::fetch_assoc($resPromotion); $promotionId = $row['id']; } $users = Import :: csvToArray($file); $countUsers = 0; $notFoundCounter = 0; foreach ($users as $user) { $errorLogString = ''; $successLogString = ''; $countUsers ++; $customUsername = strtolower(Database::escape_string('efc'.$user['Numero Inscrit'])); $chamiloUsername = Database::escape_string($user['Login Chamilo']); $res = Database::query(sprintf($sqlFindUser, $customUsername)); if (Database::num_rows($res) < 1) { // This user was not found $errorLogString .= $customUsername.' not found in Chamilo'.PHP_EOL; $notFoundCounter++; } else { // The user exists. Get user info in $row. $row = Database::fetch_assoc($res); $successLogString .= $customUsername.' found in Chamilo with ID '.$row['id'].PHP_EOL; // Update extra field ScormStudentId $resField = Database::query(sprintf($sqlFindField, $row['id'])); if (Database::num_rows($resField) > 0) { $resUpdate = Database::query(sprintf($sqlUpdateField, $customUsername, $row['id'])); $successLogString .= "Updated $scormField to value '$customUsername' for user ".$row['id'].PHP_EOL; } else { $resInsert = Database::query(sprintf($sqlInsertField, $row['id'], $customUsername)); $successLogString .= "Inserted $scormField with value '$customUsername' for user ".$row['id'].PHP_EOL; } $sessions = []; $resFindSessions = Database::query(sprintf($sqlFindSessions, $row['id'])); while ($rowSessions = Database::fetch_assoc($resFindSessions)) { $sessions[] = $rowSessions['session_id']; } // Verify special condition where the user with the name as in // "Login Chamilo" already exists. $resFindChamiloUsername = Database::query(sprintf($sqlFindUser, $chamiloUsername)); if (Database::num_rows($resFindChamiloUsername) > 0) { // User with "Login Chamilo" exists. $rowUsername = Database::fetch_assoc($resFindChamiloUsername); // Get sessions from user efcUsername and assign them to user chamiloUsername foreach ($sessions as $session) { // subscribe the user with username = SessionManager::subscribeUsersToSession($session, [$rowUsername['id']], null, false, true); $errorLogString .= 'Special case: '.$chamiloUsername. ' already existed so it should be subscribed to the session of '.$customUsername.". Namely, session $session".PHP_EOL; $errorLogString .= " Example SQL: INSERT INTO session_rel_user (session_id, user_id, relation_type, registered_at) VALUES ($session, ".$row['id'].", 0, NOW())".PHP_EOL; } } else { // Update username $resUpdateUser = Database::query(sprintf($sqlUpdateUsername, $chamiloUsername, $row['id'])); $successLogString .= "Renamed $customUsername to $chamiloUsername".PHP_EOL; } if (count($sessions) < 1) { $errorLogString .= $customUsername.' has no associated session'.PHP_EOL; } else { // Rename the session $name = Database::escape_string($user['Login Chamilo'].' '.$user['Nom inscrit'].' '.$user['Prenom inscrit'].' : '.trim($user['Code formation']).' '.$user['Nom formation']); foreach ($sessions as $session) { // There should be only one session for this user // Rename the session and assign promotion ID if (empty($promotionId)) { $promotionId = 'NULL'; } $resUpdateSession = Database::query(sprintf($sqlUpdateSession, $name, $promotionId, $session)); $successLogString .= "Renamed session $session to $name".PHP_EOL; } } } // Write to the log files progressively file_put_contents($errorLogFile, $errorLogString, FILE_APPEND | LOCK_EX); file_put_contents($successLogFile, $successLogString, FILE_APPEND | LOCK_EX); } echo "Not found $notFoundCounter over a total of $countUsers users".PHP_EOL;