import.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // Name of the language file that needs to be included
  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']) && !empty($_FILES['file']['tmp_name'])) {
  26. $groupData = Import::csv_reader($_FILES['file']['tmp_name']);
  27. $deleteNotInArray = $form->getSubmitValue('delete_not_in_file') == 1 ? true : false;
  28. $result = GroupManager::importCategoriesAndGroupsFromArray(
  29. $groupData,
  30. $deleteNotInArray
  31. );
  32. if (!empty($result)) {
  33. $html = null;
  34. foreach ($result as $status => $data) {
  35. if ($status != 'error') {
  36. if (empty($data['category']) && empty($data['group'])) {
  37. continue;
  38. }
  39. }
  40. $html .= " <h3>".get_lang(ucfirst($status)).' </h3>';
  41. if (!empty($data['category'])) {
  42. $html .= "<h4> ".get_lang('Categories').':</h4>';
  43. foreach ($data['category'] as $category) {
  44. $html .= "<div>".$category['category']."</div>";
  45. }
  46. }
  47. if (!empty($data['group'])) {
  48. $html .= "<h4> ".get_lang('Groups').':</h4>';
  49. foreach ($data['group'] as $group) {
  50. $html .= "<div>".$group['group']."</div>";
  51. }
  52. }
  53. if ($status == 'error') {
  54. if (!empty($data)) {
  55. foreach ($data as $message) {
  56. if (!empty($message)) {
  57. $html .= "<div>".$message."</div>";
  58. }
  59. }
  60. }
  61. }
  62. }
  63. echo $html;
  64. }
  65. }
  66. }
  67. $form->display();
  68. Display::display_footer();