import.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../inc/global.inc.php';
  4. $this_section = SECTION_COURSES;
  5. $current_course_tool = TOOL_GROUP;
  6. // Notice for unauthorized people.
  7. api_protect_course_script(true);
  8. if (!api_is_allowed_to_edit(false, true)) {
  9. api_not_allowed(true);
  10. }
  11. $nameTools = get_lang('Import');
  12. /* Libraries */
  13. include_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  14. include_once api_get_path(LIBRARY_PATH).'export.lib.inc.php';
  15. $interbreadcrumb[] = array('url' => 'group.php', 'name' => get_lang('Groups'));
  16. Display::display_header($nameTools, 'Group');
  17. $form = new FormValidator('import', 'post', api_get_self().'?'.api_get_cidreq());
  18. $form->addElement('header', get_lang('ImportGroups'));
  19. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  20. $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  21. $form->addElement('checkbox', 'delete_not_in_file', null, get_lang('DeleteItemsNotInFile'));
  22. $form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv'));
  23. $form->addElement('button', 'submit', get_lang('Import'));
  24. if ($form->validate()) {
  25. if (isset($_FILES['file']['tmp_name']) &&
  26. !empty($_FILES['file']['tmp_name'])
  27. ) {
  28. $groupData = Import::csv_reader($_FILES['file']['tmp_name']);
  29. $deleteNotInArray = $form->getSubmitValue('delete_not_in_file') == 1 ? true : false;
  30. $result = GroupManager::importCategoriesAndGroupsFromArray(
  31. $groupData,
  32. $deleteNotInArray
  33. );
  34. if (!empty($result)) {
  35. $html = null;
  36. foreach ($result as $status => $data) {
  37. if ($status != 'error') {
  38. if (empty($data['category']) && empty($data['group'])) {
  39. continue;
  40. }
  41. }
  42. $html .= " <h3>".get_lang(ucfirst($status)).' </h3>';
  43. if (!empty($data['category'])) {
  44. $html .= "<h4> ".get_lang('Categories').':</h4>';
  45. foreach ($data['category'] as $category) {
  46. $html .= "<div>".$category['category']."</div>";
  47. }
  48. }
  49. if (!empty($data['group'])) {
  50. $html .= "<h4> ".get_lang('Groups').':</h4>';
  51. foreach ($data['group'] as $group) {
  52. $html .= "<div>".$group['group']."</div>";
  53. }
  54. }
  55. if ($status == 'error') {
  56. if (!empty($data)) {
  57. foreach ($data as $message) {
  58. if (!empty($message)) {
  59. $html .= "<div>".$message."</div>";
  60. }
  61. }
  62. }
  63. }
  64. }
  65. echo $html;
  66. }
  67. }
  68. }
  69. $form->display();
  70. Display::display_footer();