You are here

function template_preprocess_opigno_user_result_item__opigno_h5p in Opigno module 3.x

Same name and namespace in other branches
  1. 8 ActivityTypes/opigno_h5p/opigno_h5p.module \template_preprocess_opigno_user_result_item__opigno_h5p()

Implements hook_preprocess_HOOK().

File

ActivityTypes/opigno_h5p/opigno_h5p.module, line 79
Module main functionality.

Code

function template_preprocess_opigno_user_result_item__opigno_h5p(&$variables) {
  $route = \Drupal::routeMatch();

  /** @var \Drupal\opigno_module\Entity\OpignoModule $opigno_module */
  $opigno_module = $route
    ->getParameter('opigno_module');

  /** @var \Drupal\opigno_module\Entity\OpignoAnswer $answer */
  $answer = $variables['opigno_answer'];

  // Check if module allow display answer content for users.
  $hide_results = $opigno_module
    ->getHideResults($answer);

  /** @var Drupal\opigno_module\Entity\OpignoActivity $answer_activity */
  $answer_activity = $answer
    ->getActivity();
  if (!$hide_results) {

    // Get xApiData.

    /* @var $db_connection \Drupal\Core\Database\Connection */
    $db_connection = \Drupal::service('database');
    $query = $db_connection
      ->select('opigno_h5p_user_answer_results', 'ohr')
      ->fields('ohr')
      ->condition('ohr.answer_id', $answer
      ->id());
    $result = $query
      ->execute()
      ->fetchAll();
    $content = [];
    if ($result) {
      foreach ($result as $xapi_data) {
        $H5PReport = H5PReport::getInstance();
        $reportHtml = $H5PReport
          ->generateReport($xapi_data);
        $variables['h5p_library'] = $H5PReport
          ->getStylesUsed();

        // TODO: find a way for attaching libraries
        // for each template separately.
        $content[] = [
          '#markup' => $reportHtml,
        ];
      }
    }
    $variables['content'] = $content;
    $variables['#attached']['library'][] = 'opigno_module/module_results_page';
  }

  // Output question number info.
  if ($variables['question_number']) {
    $activity_title = $answer_activity
      ->getName();
    $variables['label'] = $variables['question_number'] . '. ' . ucfirst($activity_title);
  }
  if (isset($variables['answer_max_score']) && $variables['answer_max_score'] > 0) {

    // Get the user score.
    $variables['score'] = t('Score: %score of %max_score', [
      '%score' => $answer
        ->isEvaluated() ? $answer
        ->getScore() : '?',
      '%max_score' => $variables['answer_max_score'],
    ]);
  }
  else {
    $variables['score'] = t('No score retrieved');
  }
  return $variables;
}