migrate.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Functions used by migrate.php to migrate from Claroline to Chamilo
  4. */
  5. class Migrate {
  6. public $db;
  7. public $userIdChanges = array(); // Previous ID => New ID array
  8. public function __construct() {
  9. require __DIR__.'/config.php';
  10. $this->db = new PDO(
  11. "mysql:host=$sourceHost;dbname=$sourceDB",
  12. $sourceUser,
  13. $sourcePass
  14. );
  15. $this->dbName = $sourceDB;
  16. $this->dbSingle = $sourceSingle;
  17. $this->dbTablePrefix = $sourcePref;
  18. }
  19. /**
  20. * Migrate users
  21. * @return int Number of users migrated
  22. */
  23. public function migrateUsers() {
  24. $count = 0;
  25. // Claroline => Chamilo
  26. $match = array(
  27. 'user_id' => 'id',
  28. 'nom' => 'lastname',
  29. 'prenom' => 'firstname',
  30. 'username' => 'username',
  31. 'password' => 'password',
  32. 'language' => 'language',
  33. 'authSource' => 'auth_source', //in Claroline: claroline. In Chamilo: platform
  34. 'email' => 'email',
  35. 'officialCode' => 'official_code',
  36. 'phoneNumber' => 'phone',
  37. 'pictureUri' => 'picture_uri',
  38. 'creatorId' => 'creator_id',
  39. 'isPlatformAdmin' => 'is_admin', //existence in admin table (see below)
  40. 'isCourseCreator' => 'status', //Claro: 1=teacher, 0=student. Chamilo: 1=teacher, 5=student
  41. 'lastLogin' => 'last_login',
  42. );
  43. $sql = "SELECT * FROM ".$this->dbName.".".$this->dbTablePrefix."user ORDER BY user_id";
  44. $stmt = $this->db->prepare($sql);
  45. $stmt->execute();
  46. $rows = $stmt->fetchAll();
  47. foreach ($rows as $row) {
  48. //print_r($row);
  49. $user = array();
  50. foreach ($match as $source => $dest) {
  51. $user[$dest] = $row[$source];
  52. }
  53. if ($row['user_id'] == 1) {
  54. // skip first user to try and keep the same user IDs for all users
  55. continue;
  56. }
  57. if ($row['isCourseCreator'] == 0) {
  58. $user['status'] = 5;
  59. }
  60. if ($row['authSource'] == 'claroline') {
  61. $user['auth_source'] = 'platform';
  62. }
  63. $newUserId = UserManager::create_user(
  64. $user['firstname'],
  65. $user['lastname'],
  66. $user['status'],
  67. $user['email'],
  68. $user['username'],
  69. $user['password'],
  70. $user['official_code'],
  71. $user['language'],
  72. $user['phone'],
  73. $user['picture_uri'],
  74. $user['auth_source'],
  75. null,
  76. null,
  77. null,
  78. null,
  79. null,
  80. null,
  81. $user['is_admin']
  82. );
  83. // Now we have created the user, but we'll try to give it the
  84. // same ID as in the original database, or otherwise store the
  85. // new ID in an array for later re-use
  86. $sql = "SELECT username FROM user WHERE id = " . $user['id'] . " AND username != '".$user['username']."'";
  87. $res = Database::query($sql);
  88. $num = Database::num_rows($res);
  89. if ($num > 0) {
  90. //The ID is already used by someone else
  91. $this->userIdChanges[$user['id']] = $newUserId;
  92. }
  93. $count++;
  94. }
  95. return $count;
  96. }
  97. }