qti2.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code for Qti2 import integration.
  5. *
  6. * @package chamilo.exercise
  7. *
  8. * @author Ronny Velasquez
  9. *
  10. * @version $Id: qti2.php 2010-03-12 12:14:25Z $
  11. */
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_protect_course_script(true);
  14. // section (for the tabs)
  15. $this_section = SECTION_COURSES;
  16. // access restriction: only teachers are allowed here
  17. if (!api_is_allowed_to_edit(null, true)) {
  18. api_not_allowed(true);
  19. }
  20. // the breadcrumbs
  21. $interbreadcrumb[] = [
  22. 'url' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(),
  23. 'name' => get_lang('Exercises'),
  24. ];
  25. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  26. /**
  27. * This function displays the form to import the zip file with qti2.
  28. */
  29. function displayForm()
  30. {
  31. $form = '<div class="actions">';
  32. $form .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?show=test&'.api_get_cidreq().'">'.
  33. Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
  34. $form .= '</div>';
  35. $formValidator = new FormValidator(
  36. 'qti_upload',
  37. 'post',
  38. api_get_self().'?'.api_get_cidreq(),
  39. null,
  40. ['enctype' => 'multipart/form-data']
  41. );
  42. $formValidator->addHeader(get_lang('ImportQtiQuiz'));
  43. $formValidator->addElement('file', 'userFile', get_lang('DownloadFile'));
  44. $formValidator->addButtonImport(get_lang('Upload'));
  45. $form .= $formValidator->returnForm();
  46. echo $form;
  47. }
  48. /**
  49. * This function will import the zip file with the respective qti2.
  50. *
  51. * @param array $array_file ($_FILES)
  52. *
  53. * @return string|array
  54. */
  55. function importFile($array_file)
  56. {
  57. $unzip = 0;
  58. $process = process_uploaded_file($array_file, false);
  59. if (preg_match('/\.zip$/i', $array_file['name'])) {
  60. // if it's a zip, allow zip upload
  61. $unzip = 1;
  62. }
  63. if ($process && $unzip == 1) {
  64. $main_path = api_get_path(SYS_CODE_PATH);
  65. require_once $main_path.'exercise/export/exercise_import.inc.php';
  66. require_once $main_path.'exercise/export/qti2/qti2_classes.php';
  67. return import_exercise($array_file['name']);
  68. }
  69. return 'FileError';
  70. }
  71. $message = null;
  72. // import file
  73. if (api_is_allowed_to_edit(null, true)) {
  74. if (isset($_POST['submit'])) {
  75. $imported = importFile($_FILES['userFile']);
  76. if (is_numeric($imported) && !empty($imported)) {
  77. header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&exerciseId='.$imported);
  78. exit;
  79. } else {
  80. $message = Display::return_message(get_lang($imported));
  81. }
  82. }
  83. }
  84. Display::display_header(get_lang('ImportQtiQuiz'), 'Exercises');
  85. echo $message;
  86. // display qti form
  87. displayForm();
  88. Display::display_footer();