LearningPathResults.php in Opigno Learning path 3.x
File
src/Controller/LearningPathResults.php
View source
<?php
namespace Drupal\opigno_learning_path\Controller;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\group\Entity\Group;
use Drupal\opigno_learning_path\Entity\LPResult;
class LearningPathResults extends ControllerBase {
public function results(Group $group) {
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>',
];
}
$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(),
]),
];
}
$form = [];
$form['results_table'] = [
'#type' => 'table',
'#header' => [
'Username',
'Result',
'Date',
'Actions',
],
'#rows' => $rows,
];
return $form;
}
public function delete(Group $group, LPResult $result) {
$result
->delete();
$roles = \Drupal::currentUser()
->getRoles();
if (array_search('administrator', $roles) === FALSE) {
drupal_flush_all_caches();
}
return $this
->redirect('opigno_learning_path.results', [
'group' => $group
->id(),
]);
}
}