You are here

public function OpignoModuleController::userResults in Opigno module 3.x

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

Returns user results.

1 string reference to 'OpignoModuleController::userResults'
opigno_module.routing.yml in ./opigno_module.routing.yml
opigno_module.routing.yml

File

src/Controller/OpignoModuleController.php, line 711

Class

OpignoModuleController
Class OpignoModuleController.

Namespace

Drupal\opigno_module\Controller

Code

public function userResults(OpignoModuleInterface $opigno_module) {
  $content = [];
  $results_feedback = $opigno_module
    ->getResultsOptions();
  $user_attempts = $opigno_module
    ->getModuleAttempts($this
    ->currentUser());
  foreach ($user_attempts as $user_attempt) {

    /* @var $user_attempt UserModuleStatus */
    $score_percents = $user_attempt
      ->getScore();
    $max_score = $user_attempt
      ->getMaxScore();
    $score = round($max_score * $score_percents / 100);
    foreach ($results_feedback as $result_feedback) {

      // Check if result is between low and high percents.
      // Break on first meet.
      if ($score_percents <= $result_feedback->option_end && $score_percents >= $result_feedback->option_start) {
        $feedback = check_markup($result_feedback->option_summary, $result_feedback->option_summary_format);
        break;
      }
    }
    $content[] = [
      '#theme' => 'item_list',
      '#items' => [
        $this
          ->t('You got %score of %max_score possible points.', [
          '%max_score' => $max_score,
          '%score' => $score,
        ]),
        $this
          ->t('Score: %score%', [
          '%score' => $user_attempt
            ->getScore(),
        ]),
        isset($feedback) ? $feedback : '',
      ],
    ];
  }
  return $content;
}