usergroup_import.php 4.4 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. $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. // Language files that should be included.
  69. $language_file = array('admin', 'registration');
  70. // Resetting the course id.
  71. $cidReset = true;
  72. // Including some necessary dokeos files.
  73. include '../inc/global.inc.php';
  74. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  75. require_once api_get_path(LIBRARY_PATH) . 'import.lib.php';
  76. // Setting the section (for the tabs).
  77. $this_section = SECTION_PLATFORM_ADMIN;
  78. // Access restrictions.
  79. api_protect_admin_script();
  80. // setting breadcrumbs
  81. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  82. $interbreadcrumb[] = array('url' => 'usergroups.php', 'name' => get_lang('Classes'));
  83. // Database Table Definitions
  84. // Setting the name of the tool.
  85. $tool_name = get_lang('ImportClassListCSV');
  86. // Displaying the header.
  87. Display :: display_header($tool_name);
  88. set_time_limit(0);
  89. $form = new FormValidator('import_classes');
  90. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  91. $group = array();
  92. $group[] = $form->createElement('radio', 'file_type', '', 'CSV (<a href="example_class.csv" target="_blank">' . get_lang('ExampleCSVFile') . '</a>)', 'csv');
  93. $form->addGroup($group, '', get_lang('FileType'), '<br/>');
  94. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  95. if ($form->validate()) {
  96. $classes = Import::csv_to_array($_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::display_normal_message($number_of_added_classes . ' ' . get_lang('Added'));
  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 :: display_error_message($error_message, false);
  111. }
  112. }
  113. $form->display();
  114. ?>
  115. <p><?php echo get_lang('CSVMustLookLike') . ' (' . get_lang('MandatoryFields') . ')'; ?> :</p>
  116. <pre>
  117. <b>name;description;</b>users
  118. "User group 1";"Description";admin,username1,username2
  119. </pre>
  120. <?php
  121. // Displaying the footer.
  122. Display :: display_footer();