import.lib.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Validates imported data.
  4. */
  5. function validate_data($users)
  6. {
  7. global $defined_auth_sources;
  8. $errors = [];
  9. $usernames = [];
  10. if (is_array($users)) {
  11. foreach ($users as $index => $user) {
  12. // 1. Check whether mandatory fields have been set.
  13. $mandatory_fields = ['LastName', 'FirstName'];
  14. if (api_get_setting('registration', 'email') == 'true') {
  15. $mandatory_fields[] = 'Email';
  16. }
  17. foreach ($mandatory_fields as $key => $field) {
  18. if (!isset($user[$field]) || strlen($user[$field]) == 0) {
  19. $user['error'] = get_lang($field.'Mandatory');
  20. $errors[] = $user;
  21. }
  22. }
  23. // 2. Check username.
  24. if (!UserManager::is_username_empty($user['UserName'])) {
  25. // 2.1. Check whether username was used twice in the import file.
  26. if (isset($usernames[$user['UserName']])) {
  27. $user['error'] = get_lang('UserNameUsedTwice');
  28. $errors[] = $user;
  29. }
  30. $usernames[$user['UserName']] = 1;
  31. // 2.2. Check whether username is allready in use in database.
  32. if (!UserManager::is_username_available($user['UserName'])) {
  33. $user['error'] = get_lang('UserNameNotAvailable');
  34. $errors[] = $user;
  35. }
  36. // 2.3. Check whether username is too long.
  37. if (UserManager::is_username_too_long($user['UserName'])) {
  38. $user['error'] = get_lang('UserNameTooLong');
  39. $errors[] = $user;
  40. }
  41. }
  42. // 3. Check status.
  43. if (isset($user['Status']) && !api_status_exists($user['Status'])) {
  44. $user['error'] = get_lang('WrongStatus');
  45. $errors[] = $user;
  46. }
  47. // 5. Check authentication source.
  48. if (isset($user['AuthSource']) && strlen($user['AuthSource']) != 0) {
  49. if (!in_array($user['AuthSource'], $defined_auth_sources)) {
  50. $user['error'] = get_lang('AuthSourceNotAvailable');
  51. $errors[] = $user;
  52. }
  53. }
  54. }
  55. }
  56. return $errors;
  57. }
  58. /**
  59. * Adds missing user-information (which isn't required, like password, username, etc).
  60. */
  61. function complete_missing_data($user)
  62. {
  63. // 1. Create a username if necessary.
  64. if (UserManager::is_username_empty($user['UserName'])) {
  65. $user['UserName'] = UserManager::create_unique_username(
  66. $user['FirstName'],
  67. $user['LastName']
  68. );
  69. }
  70. // 2. Generate a password if necessary.
  71. if (!isset($user['Password']) || strlen($user['Password']) == 0) {
  72. $user['Password'] = api_generate_password();
  73. }
  74. // 3. set status if not allready set.
  75. if (!isset($user['Status']) || strlen($user['Status']) == 0) {
  76. $user['Status'] = 'user';
  77. }
  78. // 4. Set authsource if not allready set.
  79. if (!isset($user['AuthSource']) || strlen($user['AuthSource']) == 0) {
  80. $user['AuthSource'] = PLATFORM_AUTH_SOURCE;
  81. }
  82. return $user;
  83. }
  84. /**
  85. * Save the imported data.
  86. */
  87. function save_data($users)
  88. {
  89. if (is_array($users)) {
  90. foreach ($users as $index => $user) {
  91. $user = complete_missing_data($user);
  92. $user['Status'] = api_status_key($user['Status']);
  93. $user_id = UserManager:: create_user(
  94. $user['FirstName'],
  95. $user['LastName'],
  96. $user['Status'],
  97. $user['Email'],
  98. $user['UserName'],
  99. $user['Password'],
  100. $user['OfficialCode'],
  101. api_get_setting('PlatformLanguage'),
  102. $user['PhoneNumber'],
  103. '',
  104. $user['AuthSource']
  105. );
  106. foreach ($user['Courses'] as $index => $course) {
  107. if (CourseManager:: course_exists($course)) {
  108. CourseManager:: subscribe_user(
  109. $user_id,
  110. $course,
  111. $user['Status']
  112. );
  113. }
  114. }
  115. // TODO: Hard-coded French texts.
  116. // Qualite
  117. if (!empty($user['Qualite'])) {
  118. UserManager::update_extra_field_value($user_id, 'qualite', $user['Qualite']);
  119. }
  120. // Categorie
  121. if (!empty($user['Categorie'])) {
  122. UserManager::update_extra_field_value($user_id, 'categorie', $user['Categorie']);
  123. }
  124. // Etat
  125. if (!empty($user['Etat'])) {
  126. UserManager::update_extra_field_value($user_id, 'etat', $user['Etat']);
  127. }
  128. // Niveau
  129. if (!empty($user['Niveau'])) {
  130. UserManager::update_extra_field_value($user_id, 'niveau', $user['Niveau']);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Reads the CSV-file.
  137. *
  138. * @param string $file Path to the CSV-file
  139. *
  140. * @return array All user information read from the file
  141. */
  142. function parse_csv_data($file)
  143. {
  144. $users = Import :: csvToArray($file);
  145. foreach ($users as $index => $user) {
  146. if (isset($user['Courses'])) {
  147. $user['Courses'] = explode('|', trim($user['Courses']));
  148. }
  149. $users[$index] = $user;
  150. }
  151. return $users;
  152. }