import.php 2.4 KB

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