usergroup_user_import.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. * This tool allows platform admins to update class-user relations by uploading
  9. * a CSV file.
  10. */
  11. /**
  12. * Validates imported data.
  13. */
  14. function validate_data($user_classes)
  15. {
  16. global $purification_option_for_usernames;
  17. $errors = [];
  18. $classcodes = [];
  19. $usergroup = new UserGroup();
  20. foreach ($user_classes as $index => $user_class) {
  21. $user_class['line'] = $index + 1;
  22. // 1. Check whether mandatory fields are set.
  23. $mandatory_fields = ['UserName', 'ClassName'];
  24. foreach ($mandatory_fields as $field) {
  25. if (!isset($user_class[$field]) || strlen($user_class[$field]) == 0) {
  26. $user_class['error'] = get_lang($field.'Mandatory');
  27. $errors[] = $user_class;
  28. }
  29. }
  30. // 2. Check whether class code exists.
  31. if (isset($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) {
  32. // 2.1 Check whether code has been already used in this CVS-file.
  33. if (!isset($classcodes[$user_class['ClassName']])) {
  34. // 2.1.1 Check whether code exists in DB
  35. $exists = $usergroup->usergroup_exists($user_class['ClassName']);
  36. if (!$exists) {
  37. $user_class['error'] = get_lang('This code does not exist').': '.$user_class['ClassName'];
  38. $errors[] = $user_class;
  39. } else {
  40. $classcodes[$user_class['CourseCode']] = 1;
  41. }
  42. }
  43. }
  44. // 3. Check username, first, check whether it is empty.
  45. if (!UserManager::is_username_empty($user_class['UserName'])) {
  46. // 3.1. Check whether username is too long.
  47. if (UserManager::is_username_too_long($user_class['UserName'])) {
  48. $user_class['error'] = get_lang('This login is too long').': '.$user_class['UserName'];
  49. $errors[] = $user_class;
  50. }
  51. $username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames);
  52. // 3.2. Check whether username exists.
  53. if (UserManager::is_username_available($username)) {
  54. $user_class['error'] = get_lang('Unknown user').': '.$username;
  55. $errors[] = $user_class;
  56. }
  57. }
  58. }
  59. return $errors;
  60. }
  61. /**
  62. * Saves imported data.
  63. */
  64. function save_data($users_classes, $deleteUsersNotInList = false)
  65. {
  66. global $purification_option_for_usernames;
  67. // Table definitions.
  68. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  69. $usergroup = new UserGroup();
  70. // Data parsing: purification + conversion (UserName, ClassName) --> (user_is, class_id)
  71. $csv_data = [];
  72. if (!empty($users_classes)) {
  73. foreach ($users_classes as $user_class) {
  74. $sql1 = "SELECT user_id FROM $user_table
  75. WHERE username = '".Database::escape_string(UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames))."'";
  76. $res1 = Database::query($sql1);
  77. $obj1 = Database::fetch_object($res1);
  78. $id = $usergroup->getIdByName($user_class['ClassName']);
  79. if ($obj1 && $id) {
  80. $csv_data[$id]['user_list'][] = $obj1->user_id;
  81. $csv_data[$id]['user_list_name'][] = $user_class['UserName'];
  82. $csv_data[$id]['class_name'] = $user_class['ClassName'];
  83. }
  84. }
  85. }
  86. // Logic for processing the request (data + UI options).
  87. $message = null;
  88. if (!empty($csv_data)) {
  89. foreach ($csv_data as $class_id => $user_data) {
  90. $user_list = $user_data['user_list'];
  91. $class_name = $user_data['class_name'];
  92. $user_list_name = $user_data['user_list_name'];
  93. $usergroup->subscribe_users_to_usergroup($class_id, $user_list, $deleteUsersNotInList);
  94. $message .= Display::return_message(
  95. get_lang('Class').': '.$class_name.'<br />',
  96. 'normal',
  97. false
  98. );
  99. $message .= Display::return_message(
  100. get_lang('Users').': '.implode(', ', $user_list_name)
  101. );
  102. }
  103. }
  104. return $message;
  105. }
  106. /**
  107. * Reads a CSV-file.
  108. *
  109. * @param string $file Path to the CSV-file
  110. *
  111. * @return array All course-information read from the file
  112. */
  113. function parse_csv_data($file)
  114. {
  115. $courses = Import::csvToArray($file);
  116. return $courses;
  117. }
  118. $cidReset = true;
  119. require_once __DIR__.'/../inc/global.inc.php';
  120. $this_section = SECTION_PLATFORM_ADMIN;
  121. $usergroup = new UserGroup();
  122. $usergroup->protectScript();
  123. $tool_name = get_lang('Add users to a class').' CSV';
  124. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  125. // Set this option to true to enforce strict purification for usenames.
  126. $purification_option_for_usernames = false;
  127. set_time_limit(0);
  128. $form = new FormValidator('class_user_import');
  129. $form->addElement('header', $tool_name);
  130. $form->addElement('file', 'import_file', get_lang('CSV file import location'));
  131. //$form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('Add user in the course only if not yet in'));
  132. $form->addElement('checkbox', 'unsubscribe', '', get_lang('Remove user from course if his name is not in the list'));
  133. $form->addButtonImport(get_lang('Import'));
  134. $errors = [];
  135. if ($form->validate()) {
  136. $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']);
  137. $errors = validate_data($users_classes);
  138. if (count($errors) == 0) {
  139. $deleteUsersNotInList = isset($_REQUEST['unsubscribe']) && !empty($_REQUEST['unsubscribe']) ? true : false;
  140. $return = save_data($users_classes, $deleteUsersNotInList);
  141. }
  142. }
  143. Display::display_header($tool_name);
  144. if (isset($return) && $return) {
  145. echo $return;
  146. }
  147. if (count($errors) != 0) {
  148. $error_message = "\n";
  149. foreach ($errors as $index => $error_class_user) {
  150. $error_message .= get_lang('Line').' '.$error_class_user['line'].': '.$error_class_user['error'].'</b>';
  151. $error_message .= "<br />";
  152. }
  153. $error_message .= "\n";
  154. echo Display::return_message($error_message, 'error', false);
  155. }
  156. $form->display();
  157. ?>
  158. <p><?php echo get_lang('The CSV file must look like this').' ('.get_lang('Fields in <strong>bold</strong> are mandatory.').')'; ?> :</p>
  159. <pre>
  160. <b>UserName</b>;<b>ClassName</b>
  161. jdoe;class01
  162. adam;class01
  163. </pre>
  164. <?php
  165. Display::display_footer();