import.lib.php 4.7 KB

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