You are here

protected static function ExternalPackageController::createH5pContent in Opigno module 8

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

Function for creating h5p content for Interactive content activity.

Parameters

\Drupal\file\Entity\File $file: File object.

string $mode: Kind of creating functionality.

Return value

int|bool Return h5p content id or FALSE.

1 call to ExternalPackageController::createH5pContent()
ExternalPackageController::createActivityByPackageType in src/Controller/ExternalPackageController.php
Function for creating activity depending file type.

File

src/Controller/ExternalPackageController.php, line 316

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

protected static function createH5pContent(File $file, $mode = NULL) {
  $file_field = 'package';

  // Prepare temp folder.
  $interface = H5PDrupal::getInstance('interface', $file_field);
  $temporary_file_path = $mode && $mode == 'ppt' ? 'public://' . static::getPptConversionDir() : 'public://external_packages';

  // Tell H5P Core where to look for the files.
  $interface
    ->getUploadedH5pPath(\Drupal::service('file_system')
    ->realpath($file
    ->getFileUri()));
  $interface
    ->getUploadedH5pFolderPath(\Drupal::service('file_system')
    ->realpath($temporary_file_path));

  // Call upon H5P Core to validate the contents of the package.
  $validator = H5PDrupal::getInstance('validator', $file_field);
  $validator
    ->isValidPackage();

  // Store the uploaded file.
  $storage = H5PDrupal::getInstance('storage', $file_field);
  $content = [
    'id' => NULL,
    'uploaded' => TRUE,
    'disable' => 0,
  ];

  // Save and update content id.
  $storage
    ->savePackage($content);
  $content_id = $storage->contentId;
  if (!$content_id) {
    return FALSE;
  }
  return $content_id;
}