import.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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();