You are here

protected static function ExternalPackageController::createActivityByPackageType in Opigno module 8

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

Function for creating activity depending file type.

1 call to ExternalPackageController::createActivityByPackageType()
ExternalPackageController::ajaxFormExternalPackageFormSubmit in src/Controller/ExternalPackageController.php
Custom submit handler added via the function opigno_module_form_alter().

File

src/Controller/ExternalPackageController.php, line 232

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

protected static function createActivityByPackageType(File $file, $package_type, array &$form, FormStateInterface $form_state, $ppt_dir_real_path = NULL) {
  switch ($package_type) {

    // Create Scorm_package activity.
    case 'scorm':
      $activity = OpignoActivity::create([
        'type' => 'opigno_scorm',
        'name' => $form_state
          ->getValue('name'),
        'opigno_scorm_package' => [
          'target_id' => $file
            ->id(),
        ],
      ]);
      $activity
        ->save();
      break;

    // Create Tincan_package activity.
    case 'tincan':

      // Check if Tincan PHP library is installed.
      $has_library = opigno_tincan_api_tincanphp_is_installed();
      if (!$has_library) {
        \Drupal::logger('opigno_module')
          ->error(t('Impossible to create a new TinCan Package activity. Tincan PHP Library is not installed.'));
        return FALSE;
      }

      // Check if the LRS settings are set.
      $config = \Drupal::config('opigno_tincan_api.settings');
      $endpoint = $config
        ->get('opigno_tincan_api_endpoint');
      $username = $config
        ->get('opigno_tincan_api_username');
      $password = $config
        ->get('opigno_tincan_api_password');
      if (empty($endpoint) || empty($username) || empty($password)) {
        \Drupal::logger('opigno_module')
          ->error(t('Impossible to create a new TinCan Package activity. LRS is not configured.'));
        return FALSE;
      }
      $activity = OpignoActivity::create([
        'type' => 'opigno_tincan',
        'name' => $form_state
          ->getValue('name'),
        'opigno_tincan_package' => [
          'target_id' => $file
            ->id(),
        ],
      ]);
      $activity
        ->save();
      break;

    // Create Interactive content activity.
    case 'h5p':
      $storage = $form_state
        ->getStorage();
      $mode = NULL;
      if (!empty($storage['mode']) && $storage['mode'] == 'ppt') {
        $mode = 'ppt';
      }
      $h5p_content_id = static::createH5pContent($file, $mode);
      if (!$h5p_content_id) {
        \Drupal::messenger()
          ->addError(t("Can't create h5p content. Wrong h5p package."));
        return FALSE;
      }
      $activity = OpignoActivity::create([
        'type' => 'opigno_h5p',
        'name' => $form_state
          ->getValue('name'),
        'opigno_h5p' => [
          'h5p_content_id' => $h5p_content_id,
        ],
      ]);
      $activity
        ->save();
      break;
  }
  return $activity;
}