usergroup_user_import.php 6.4 KB

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