import.php 3.0 KB

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