import.lib.php 5.5 KB

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