You are here

protected function LearningPathContentController::getModuleConditionals in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathContentController.php \Drupal\opigno_learning_path\Controller\LearningPathContentController::getModuleConditionals()

Returns conditional activities with the module.

Parameters

\Drupal\opigno_module\Entity\OpignoModule $opigno_module: Entity OpignoModule".

array $results: Results.

1 call to LearningPathContentController::getModuleConditionals()
LearningPathContentController::getModuleRequiredActivities in src/Controller/LearningPathContentController.php
Returns conditional activities with the module.

File

src/Controller/LearningPathContentController.php, line 392

Class

LearningPathContentController
Controller for all the actions of the Learning Path content.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function getModuleConditionals(OpignoModule $opigno_module, &$results = []) {
  if ($opigno_module) {
    $activities = $this
      ->getModuleActivitiesEntities($opigno_module);
    $conditional_h5p_types = [
      'H5P.TrueFalse',
      'H5P.MultiChoice',
    ];
    if ($activities) {

      // Get only H5P.TrueFalse/H5P.MultiChoice activities.
      foreach ($activities as $key => $activity) {
        $exclude = FALSE;
        $activity = OpignoActivity::load($activity->id);
        if ($activity
          ->hasField('opigno_h5p')) {
          $opigno_h5p = $activity
            ->get('opigno_h5p')
            ->getValue();
          if (!empty($opigno_h5p[0]['h5p_content_id']) && ($h5p_content_id = $opigno_h5p[0]['h5p_content_id'])) {
            $h5p_content = H5PContent::load($h5p_content_id);
            $library = $h5p_content
              ->getLibrary();
            if (!in_array($library->name, $conditional_h5p_types)) {
              $exclude = TRUE;
            }
            if ($library->name == 'H5P.TrueFalse') {
              $params = $h5p_content
                ->getParameters();
              $activities[$key]->answers[0] = [
                'id' => $activity
                  ->id() . '-0',
                'correct' => $params->correct == 'true' ? TRUE : FALSE,
                'text' => trim(strip_tags(nl2br(str_replace([
                  '\\n',
                  '\\r',
                ], '', $params->l10n->trueText)))),
              ];
              $activities[$key]->answers[1] = [
                'id' => $activity
                  ->id() . '-1',
                'correct' => $params->correct == 'false' ? TRUE : FALSE,
                'text' => trim(strip_tags(nl2br(str_replace([
                  '\\n',
                  '\\r',
                ], '', $params->l10n->falseText)))),
              ];
            }
            if ($library->name == 'H5P.MultiChoice') {
              $answers = $h5p_content
                ->getParameters()->answers;
              if ($answers) {
                foreach ($answers as $k => $answer) {
                  $activities[$key]->answers[$k] = [
                    'id' => $activity
                      ->id() . '-' . $k,
                    'correct' => $answer->correct,
                    'text' => trim(strip_tags(nl2br(str_replace([
                      '\\n',
                      '\\r',
                    ], '', $answer->text)))),
                  ];
                }
              }
            }
          }
          else {
            $exclude = TRUE;
          }
        }
        else {
          $exclude = TRUE;
        }
        if ($exclude) {
          unset($activities[$key]);
          $results['simple'] = TRUE;
        }
        else {
          $results['conditional'][] = $activities[$key];
        }
      }
    }
  }
}