import.php 2.8 KB

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