You are here

public function LearningPathResults::results in Opigno Learning path 8

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

Results page for a learning path.

It shows all the results of the users of this learning path.

File

src/Controller/LearningPathResults.php, line 21

Class

LearningPathResults
Class LearningPathResults.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function results(Group $group) {

  // Get the results from the database.
  try {
    $results = LPResult::loadByLearningPath($group);
  } catch (InvalidPluginDefinitionException $e) {
    return [
      '#markup' => '<p>A problem occured while gathering the results</p>',
    ];
  }
  if (!$results) {
    return [
      '#markup' => '<p>No results for this learning path</p>',
    ];
  }

  // Format the results to be able to show them in a table.
  $rows = [];
  foreach ($results as $result) {
    if (!$result
      ->access('view') || !$result
      ->getUser()) {
      continue;
    }
    $rows[] = [
      $result
        ->getUser()
        ->getUsername(),
      $result
        ->hasPassed() ? 'Passed' : 'Not passed',
      \Drupal::service('date.formatter')
        ->format($result
        ->getCreatedTime(), 'short'),
      Link::createFromRoute('Delete', 'opigno_learning_path.results.delete', [
        'group' => $group
          ->id(),
        'result' => $result
          ->id(),
      ]),
    ];
  }

  // Now create the table.
  $form = [];
  $form['results_table'] = [
    '#type' => 'table',
    '#header' => [
      'Username',
      'Result',
      'Date',
      'Actions',
    ],
    '#rows' => $rows,
  ];
  return $form;
}