import.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // Name of the language file that needs to be included
  4. $language_file = array('group', 'admin', 'registration');
  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('ImportCSVFileLocation'));
  22. $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  23. $form->addElement('checkbox', 'delete_not_in_file', null, get_lang('DeleteItemsNotInFile'));
  24. $form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv'));
  25. $form->addElement('button', 'submit', get_lang('Import'));
  26. if ($form->validate()) {
  27. if (isset($_FILES['file']['tmp_name']) &&
  28. !empty($_FILES['file']['tmp_name'])
  29. ) {
  30. $groupData = Import::csv_reader($_FILES['file']['tmp_name']);
  31. $deleteNotInArray = $form->getSubmitValue('delete_not_in_file') == 1 ? true : false;
  32. $result = GroupManager::importCategoriesAndGroupsFromArray(
  33. $groupData,
  34. $deleteNotInArray
  35. );
  36. if (!empty($result)) {
  37. $html = null;
  38. foreach ($result as $status => $data) {
  39. if ($status != 'error') {
  40. if (empty($data['category']) && empty($data['group'])) {
  41. continue;
  42. }
  43. }
  44. $html .= " <h3>".get_lang(ucfirst($status)).' </h3>';
  45. if (!empty($data['category'])) {
  46. $html .= "<h4> ".get_lang('Categories').':</h4>';
  47. foreach ($data['category'] as $category) {
  48. $html .= "<div>".$category['category']."</div>";
  49. }
  50. }
  51. if (!empty($data['group'])) {
  52. $html .= "<h4> ".get_lang('Groups').':</h4>';
  53. foreach ($data['group'] as $group) {
  54. $html .= "<div>".$group['group']."</div>";
  55. }
  56. }
  57. if ($status == 'error') {
  58. if (!empty($data)) {
  59. foreach ($data as $message) {
  60. if (!empty($message)) {
  61. $html .= "<div>".$message."</div>";
  62. }
  63. }
  64. }
  65. }
  66. }
  67. echo $html;
  68. }
  69. }
  70. }
  71. $form->display();
  72. Display::display_footer();