usergroup_user_import.php 6.5 KB

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