usergroup_import.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to add classes by uploading a CSV file.
  5. *
  6. * @todo Add some langvars to DLTT
  7. *
  8. * @package chamilo.admin
  9. */
  10. /**
  11. * Validates imported data.
  12. */
  13. function validate_data($classes)
  14. {
  15. $errors = [];
  16. $usergroup = new UserGroup();
  17. foreach ($classes as $index => $class) {
  18. // 1. Check of class name is available.
  19. if (!isset($class['name']) || strlen(trim($class['name'])) == 0) {
  20. $class['line'] = $index + 2;
  21. $class['error'] = get_lang('MissingClassName');
  22. $errors[] = $class;
  23. } else {
  24. // 2. Check whether class doesn't exist yet.
  25. if ($usergroup->usergroup_exists($class['name'])) {
  26. $class['line'] = $index + 2;
  27. $class['error'] = get_lang('ClassNameExists').
  28. ': <strong>'.$class['name'].'</strong>';
  29. $errors[] = $class;
  30. }
  31. }
  32. }
  33. return $errors;
  34. }
  35. /**
  36. * Save imported class data to database.
  37. *
  38. * @param $classes
  39. *
  40. * @return int
  41. */
  42. function save_data($classes)
  43. {
  44. $count = 0;
  45. $usergroup = new UserGroup();
  46. foreach ($classes as $index => $class) {
  47. $usersToAdd = isset($class['users']) ? $class['users'] : null;
  48. unset($class['users']);
  49. $id = $usergroup->save($class);
  50. if ($id) {
  51. if (!empty($usersToAdd)) {
  52. $usersToAddList = explode(',', $usersToAdd);
  53. $userIdList = [];
  54. foreach ($usersToAddList as $username) {
  55. $userInfo = api_get_user_info_from_username($username);
  56. $userIdList[] = $userInfo['user_id'];
  57. }
  58. if (!empty($userIdList)) {
  59. $usergroup->subscribe_users_to_usergroup(
  60. $id,
  61. $userIdList,
  62. false
  63. );
  64. }
  65. }
  66. $count++;
  67. }
  68. }
  69. return $count;
  70. }
  71. // Resetting the course id.
  72. $cidReset = true;
  73. require_once __DIR__.'/../inc/global.inc.php';
  74. // Setting the section (for the tabs).
  75. $this_section = SECTION_PLATFORM_ADMIN;
  76. $usergroup = new UserGroup();
  77. $usergroup->protectScript();
  78. // setting breadcrumbs
  79. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  80. // Setting the name of the tool.
  81. $tool_name = get_lang('ImportClassListCSV');
  82. set_time_limit(0);
  83. $form = new FormValidator('import_classes');
  84. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  85. $group = [];
  86. $group[] = $form->createElement(
  87. 'radio',
  88. 'file_type',
  89. '',
  90. 'CSV (<a href="example_class.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
  91. 'csv'
  92. );
  93. $form->addGroup($group, '', get_lang('FileType'), null);
  94. $form->addButtonImport(get_lang('Import'));
  95. if ($form->validate()) {
  96. $classes = Import::csvToArray($_FILES['import_file']['tmp_name']);
  97. $errors = validate_data($classes);
  98. if (count($errors) == 0) {
  99. $number_of_added_classes = save_data($classes);
  100. Display::addFlash(Display::return_message($number_of_added_classes.' '.get_lang('Added'), 'normal'));
  101. } else {
  102. $error_message = get_lang('ErrorsWhenImportingFile');
  103. $error_message .= '<ul>';
  104. foreach ($errors as $index => $error_class) {
  105. $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
  106. $error_message .= '</li>';
  107. }
  108. $error_message .= '</ul>';
  109. $error_message .= get_lang('Error');
  110. Display::addFlash(Display::return_message($error_message, 'error', false));
  111. }
  112. }
  113. // Displaying the header.
  114. Display::display_header($tool_name);
  115. $form->display();
  116. ?>
  117. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  118. <pre>
  119. <b>name;description;</b>users
  120. "User group 1";"Description";admin,username1,username2
  121. </pre>
  122. <?php
  123. // Displaying the footer.
  124. Display::display_footer();