usergroup_import.php 4.1 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. * @todo Add some langvars to DLTT
  6. * @package chamilo.admin
  7. */
  8. /**
  9. * Validates imported data.
  10. */
  11. function validate_data($classes)
  12. {
  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. require_once __DIR__.'/../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' => 'index.php', '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. set_time_limit(0);
  83. $form = new FormValidator('import_classes');
  84. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  85. $group = array();
  86. $group[] = $form->createElement(
  87. 'radio',
  88. 'file_type',
  89. '',
  90. 'CSV (<a href="example_class.csv" target="_blank">'.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();