From c1ffcd30c829b5a535cc67f81ea9edb4e4c8471e Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Tue, 17 Mar 2020 04:18:38 +0100 Subject: [PATCH] Scripts: Add example script to fix issues in the database based on a CSV file - refs BT#16838 --- tests/scripts/fix_moodle_migration.php | 133 +++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 tests/scripts/fix_moodle_migration.php diff --git a/tests/scripts/fix_moodle_migration.php b/tests/scripts/fix_moodle_migration.php new file mode 100644 index 0000000000..93ecb5cea7 --- /dev/null +++ b/tests/scripts/fix_moodle_migration.php @@ -0,0 +1,133 @@ + 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 = 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 (missing "code formation" in CSV for full-name) + $name = Database::escape_string($user['Login Chamilo'].' '.$user['Nom inscrit'].' '.$user['Prenom inscrit'].' : '.$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;