View source
<?php
namespace Drupal\captcha_questions_dblog\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Database\Connection;
class CaptchaQuestionsDblogController extends ControllerBase {
private $dateFormatter;
protected $database;
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('database'));
}
public function __construct(DateFormatter $dateFormatter, Connection $database) {
$this->dateFormatter = $dateFormatter;
$this->database = $database;
}
public function captchaQuestionsDblogView() {
$header = [
[
'data' => $this
->t('Submission'),
'field' => 'dblogid',
],
[
'data' => $this
->t('Timestamp'),
'field' => 'timestamp',
],
[
'data' => $this
->t('IP'),
'field' => 'IP',
],
[
'data' => $this
->t('Form ID'),
'field' => 'form_id',
],
[
'data' => $this
->t('Question asked'),
'field' => 'question_asked',
],
[
'data' => $this
->t('Answer given'),
'field' => 'answer_given',
],
[
'data' => $this
->t('Correct answer'),
'field' => 'answer_correct',
],
];
$rows = [];
$query = $this->database
->select('captcha_questions_dblog', 'log');
$query
->fields('log', [
'dblogid',
'timestamp',
'ip',
'form_id',
'question_asked',
'answer_given',
'answer_correct',
]);
$table_sort = $query
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($header);
$pager = $table_sort
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(5);
$result = $pager
->execute();
foreach ($result as $e) {
$rows[] = [
$e->dblogid,
$this->dateFormatter
->format($e->timestamp, 'custom', 'Y-m-d H:m:s'),
$e->ip,
$e->form_id,
Unicode::truncate($e->question_asked, '30', TRUE, 20),
$e->answer_given,
$e->answer_correct,
];
}
$count = count($rows);
$build = [
'#markup' => $this
->t('Found a total of @count failed submissions', [
'@count' => $count,
]),
];
$build['config_table'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
$build['pager'] = [
'#type' => 'pager',
];
return $build;
}
}