import.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../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. $interbreadcrumb[] = [
  13. 'url' => 'group.php?'.api_get_cidreq(),
  14. 'name' => get_lang('Groups'),
  15. ];
  16. $form = new FormValidator(
  17. 'import',
  18. 'post',
  19. api_get_self().'?'.api_get_cidreq()
  20. );
  21. $form->addElement('header', get_lang('ImportGroups'));
  22. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  23. $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  24. $form->addElement(
  25. 'checkbox',
  26. 'delete_not_in_file',
  27. null,
  28. get_lang('DeleteItemsNotInFile')
  29. );
  30. $form->addElement(
  31. 'label',
  32. null,
  33. Display::url(
  34. get_lang('ExampleCSVFile'),
  35. api_get_path(WEB_CODE_PATH).'group/example.csv',
  36. ['download' => true]
  37. )
  38. );
  39. $form->addButtonImport(get_lang('Import'));
  40. if ($form->validate()) {
  41. if (isset($_FILES['file']['tmp_name']) &&
  42. !empty($_FILES['file']['tmp_name'])
  43. ) {
  44. $groupData = Import::csv_reader($_FILES['file']['tmp_name']);
  45. $deleteNotInArray = $form->getSubmitValue('delete_not_in_file') == 1 ? true : false;
  46. $result = GroupManager::importCategoriesAndGroupsFromArray(
  47. $groupData,
  48. $deleteNotInArray
  49. );
  50. if (!empty($result)) {
  51. $html = null;
  52. foreach ($result as $status => $data) {
  53. if ($status != 'error') {
  54. if (empty($data['category']) && empty($data['group'])) {
  55. continue;
  56. }
  57. }
  58. $html .= " <h3>".get_lang(ucfirst($status)).' </h3>';
  59. if (!empty($data['category'])) {
  60. $html .= "<h4> ".get_lang('Categories').':</h4>';
  61. foreach ($data['category'] as $category) {
  62. $html .= "<div>".$category['category']."</div>";
  63. }
  64. }
  65. if (!empty($data['group'])) {
  66. $html .= "<h4> ".get_lang('Groups').':</h4>';
  67. foreach ($data['group'] as $group) {
  68. $html .= "<div>".$group['group']."</div>";
  69. }
  70. }
  71. if ($status == 'error') {
  72. if (!empty($data)) {
  73. foreach ($data as $message) {
  74. if (!empty($message)) {
  75. $html .= "<div>".$message."</div>";
  76. }
  77. }
  78. }
  79. }
  80. }
  81. Display::addFlash(
  82. Display::return_message($html, 'information', false)
  83. );
  84. header('Location: '.api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq());
  85. exit;
  86. }
  87. }
  88. }
  89. Display::display_header($nameTools, 'Group');
  90. $form->display();
  91. Display::display_footer();