You are here

public static function ExternalPackageController::createH5pCoursePresentationPackage in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Controller/ExternalPackageController.php \Drupal\opigno_module\Controller\ExternalPackageController::createH5pCoursePresentationPackage()

Creates H5P content package file.

Parameters

array|mixed $images: Array of images with properties.

string $ppt_dir_real_path: Real path to ppt directory.

string $title: Presentation activity title.

1 call to ExternalPackageController::createH5pCoursePresentationPackage()
AddExternalPackageForm::validateForm in src/Form/AddExternalPackageForm.php
Form validation handler.

File

src/Controller/ExternalPackageController.php, line 450

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

public static function createH5pCoursePresentationPackage(array $images, $ppt_dir_real_path, $title) {
  $libraries = [
    'H5P.CoursePresentation',
    'FontAwesome',
    'H5P.FontIcons',
    'H5P.JoubelUI',
    'Drop',
    'Tether',
    'H5P.Transition',
  ];
  $libraries_data = [];
  foreach ($libraries as $library) {
    $libraries_data[$library] = self::getH5PLibraryData($library);
  }
  $h5p_json_string = '{"title":"Interactive Content","language":"und","mainLibrary":"H5P.CoursePresentation","embedTypes":["div"],"preloadedDependencies":[{"machineName":"H5P.CoursePresentation","majorVersion":"1","minorVersion":"17"},{"machineName":"FontAwesome","majorVersion":"4","minorVersion":"5"},{"machineName":"H5P.FontIcons","majorVersion":"1","minorVersion":"0"},{"machineName":"H5P.JoubelUI","majorVersion":"1","minorVersion":"3"},{"machineName":"Drop","majorVersion":"1","minorVersion":"0"},{"machineName":"Tether","majorVersion":"1","minorVersion":"0"},{"machineName":"H5P.Transition","majorVersion":"1","minorVersion":"0"}]}';
  $h5p_array = json_decode($h5p_json_string);

  // Update libraries numbers to last versions.
  foreach ($h5p_array->preloadedDependencies as $key => &$dependency) {
    $dependency->majorVersion = $libraries_data[$dependency->machineName]->major_version;
    $dependency->minorVersion = $libraries_data[$dependency->machineName]->minor_version;
  }
  $h5p_json_string = json_encode($h5p_array);
  $content_json_string = '{"presentation":{"slides":[],"keywordListEnabled":true,"globalBackgroundSelector":{},"keywordListAlwaysShow":false,"keywordListAutoHide":false,"keywordListOpacity":90},"l10n":{"slide":"Slide","score":"Score","yourScore":"Your Score","maxScore":"Max Score","goodScore":"Congratulations! You got @percent correct!","okScore":"Nice effort! You got @percent correct!","badScore":"You got @percent correct.","total":"Total","totalScore":"Total Score","showSolutions":"Show solutions","retry":"Retry","title":"Title","author":"Author","lisence":"License","license":"License","exportAnswers":"Export text","copyright":"Rights of use","hideKeywords":"Hide keywords list","showKeywords":"Show keywords list","fullscreen":"Fullscreen","exitFullscreen":"Exit fullscreen","prevSlide":"Previous slide","nextSlide":"Next slide","currentSlide":"Current slide","lastSlide":"Last slide","solutionModeTitle":"Exit solution mode","solutionModeText":"Solution Mode","summaryMultipleTaskText":"Multiple tasks","scoreMessage":"You achieved:","shareFacebook":"Share on Facebook","shareTwitter":"Share on Twitter","shareGoogle":"Share on Google+","summary":"Summary","solutionsButtonTitle":"Show comments","printTitle":"Print","printIngress":"How would you like to print this presentation?","printAllSlides":"Print all slides","printCurrentSlide":"Print current slide","noTitle":"No title","accessibilitySlideNavigationExplanation":"Use left and right arrow to change slide in that direction whenever canvas is selected.","accessibilityCanvasLabel":"Presentation canvas. Use left and right arrow to move between slides.","containsNotCompleted":"@slideName contains not completed interaction","containsCompleted":"@slideName contains completed interaction","slideCount":"Slide @index of @total","containsOnlyCorrect":"@slideName only has correct answers","containsIncorrectAnswers":"@slideName has incorrect answers","shareResult":"Share Result"},"override":{"activeSurface":false,"hideSummarySlide":false,"enablePrintButton":false,"social":{"showFacebookShare":true,"facebookShare":{"url":"@currentpageurl","quote":"I scored @score out of @maxScore on a task at @currentpageurl."},"showTwitterShare":false,"twitterShare":{"statement":"I scored @score out of @maxScore on a task at @currentpageurl.","url":"@currentpageurl","hashtags":"h5p, course"},"showGoogleShare":false,"googleShareUrl":"@currentpageurl"}}}';
  $content_array = json_decode($content_json_string);
  foreach ($images as $key => $image) {
    $dimensions = getimagesize($image->uri);
    $content_array->presentation->slides[] = [
      'slideBackgroundSelector' => [
        'imageSlideBackground' => [
          'path' => 'images/' . $image->filename,
          'mime' => 'image/jpeg',
          'copyright' => [
            'license' => 'MIT',
          ],
          'width' => $dimensions[0],
          'height' => $dimensions[1],
        ],
      ],
      'keywords' => [
        [
          'main' => $title . ' - slide ' . $key,
        ],
      ],
    ];
  }
  $content_json_string = json_encode($content_array);
  $zip = new \ZipArchive();
  $zip
    ->open($ppt_dir_real_path . '/content.zip', constant('ZipArchive::CREATE'));
  foreach ($images as $image) {
    $zip
      ->addFile($image->uri, 'content/images/' . $image->filename);
  }
  file_put_contents($ppt_dir_real_path . '/content.json', $content_json_string);
  $zip
    ->addFile($ppt_dir_real_path . '/content.json', 'content/content.json');
  file_put_contents($ppt_dir_real_path . '/h5p.json', $h5p_json_string);
  $zip
    ->addFile($ppt_dir_real_path . '/h5p.json', 'h5p.json');
  $zip
    ->close();
  rename($ppt_dir_real_path . '/content.zip', $ppt_dir_real_path . '/ppt-content-import.h5p');
}