You are here

public static function ExternalPackageController::ajaxFormExternalPackageFormSubmit in Opigno module 3.x

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

Custom submit handler added via the function opigno_module_form_alter().

See also

opigno_module_form_alter()

File

src/Controller/ExternalPackageController.php, line 90

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

public static function ajaxFormExternalPackageFormSubmit($form, FormState &$form_state) {
  $fid = $form_state
    ->get('package');
  $file = File::load($fid);
  $params = \Drupal::routeMatch()
    ->getParameters();
  $module = $params
    ->get('opigno_module');

  // If one information missing, return an error.
  if (!isset($module)) {

    // TODO: Add an error message here.
    return;
  }
  if (!empty($file)) {

    // Get file extension.
    $extension = static::getFileExtension($file
      ->getFilename());
    switch ($extension) {
      case 'zip':

        // Check file type. Can be "scorm" or "tincan".
        $type = static::checkPackageType($file);
        if (!$type) {
          \Drupal::messenger()
            ->addError(t("Package does not contain required files."));
          return $form_state
            ->setRebuild();
        }

        // Create activity.
        $entity = static::createActivityByPackageType($file, $type, $form, $form_state);
        break;
      case 'h5p':
        $entity = static::createActivityByPackageType($file, 'h5p', $form, $form_state);
        $storage = $form_state
          ->getStorage();
        if (!empty($storage['mode']) && $storage['mode'] == 'ppt') {
          $ppt_dir = static::getPptConversionDir();
          $file_default_scheme = \Drupal::config('system.file')
            ->get('default_scheme');
          $public_files_real_path = \Drupal::service('file_system')
            ->realpath($file_default_scheme . "://");
          $ppt_dir_real_path = $public_files_real_path . '/' . $ppt_dir;

          // Clean up extra files.
          self::cleanDirectoryFiles($ppt_dir_real_path, [
            $file,
          ]);
        }
        break;
    }
    if (!$entity) {
      \Drupal::messenger()
        ->addWarning(t("Can't create activity."));
    }
    else {

      // Clear user input.
      $input = $form_state
        ->getUserInput();

      // We should not clear the system items from the user input.
      $clean_keys = $form_state
        ->getCleanValueKeys();
      $clean_keys[] = 'ajax_page_state';
      foreach ($input as $key => $item) {
        if (!in_array($key, $clean_keys) && substr($key, 0, 1) !== '_') {
          unset($input[$key]);
        }
      }

      // Store new entity for display in the AJAX callback.
      $input['activity'] = $entity;
      $form_state
        ->setUserInput($input);

      // Rebuild the form state values.
      $form_state
        ->setRebuild();
      $form_state
        ->setStorage([]);

      // Assign activity to module if entity is new.
      if (!isset($item_id)) {

        /** @var \Drupal\opigno_module\Controller\OpignoModuleController $opigno_module_controller */
        $opigno_module_controller = \Drupal::service('opigno_module.opigno_module');
        $opigno_module_controller
          ->activitiesToModule([
          $entity,
        ], $module);
      }
    }
  }
  return $form_state
    ->setRebuildInfo([
    t("Can't create an activity. File can't be uploaded."),
  ]);
}