usergroup_import.php 4.2 KB

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