usergroup_import.php 4.1 KB

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