usergroup_user_import.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 CSVfile
  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 $key => $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 classcode exists.
  30. if (isset($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) {
  31. // 2.1 Check whether code has been allready 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) {
  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 WHERE username = '" . Database::escape_string(UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames)) . "'";
  73. $res1 = Database::query($sql1);
  74. $obj1 = Database::fetch_object($res1);
  75. $usergroup = new UserGroup();
  76. $id = $usergroup->get_id_by_name($user_class['ClassName']);
  77. if ($obj1 && $id) {
  78. $csv_data[$id]['user_list'][] = $obj1->user_id;
  79. $csv_data[$id]['user_list_name'][] = $user_class['UserName'];
  80. $csv_data[$id]['class_name'] = $user_class['ClassName'];
  81. }
  82. }
  83. }
  84. // Logic for processing the request (data + UI options).
  85. $message = null;
  86. if (!empty($csv_data)) {
  87. foreach ($csv_data as $class_id => $user_data) {
  88. $user_list = $user_data['user_list'];
  89. $class_name = $user_data['class_name'];
  90. $user_list_name = $user_data['user_list_name'];
  91. $usergroup->subscribe_users_to_usergroup($class_id, $user_list, false);
  92. $message .= Display::return_message(get_lang('Class') . ': ' . $class_name . '<br />', 'normal', false);
  93. $message .= Display::return_message(get_lang('Users') . ': ' . implode(', ', $user_list_name));
  94. }
  95. }
  96. return $message;
  97. }
  98. /**
  99. * Reads a CSV-file.
  100. * @param string $file Path to the CSV-file
  101. * @return array All course-information read from the file
  102. */
  103. function parse_csv_data($file) {
  104. $courses = Import::csv_to_array($file);
  105. return $courses;
  106. }
  107. $language_file = array('admin', 'registration');
  108. $cidReset = true;
  109. require_once '../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->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  124. if ($form->validate()) {
  125. $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']);
  126. $errors = validate_data($users_classes);
  127. if (count($errors) == 0) {
  128. $return = save_data($users_classes);
  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();