You are here

public function CaptchaQuestionsDblogController::captchaQuestionsDblogView in Captcha Questions 8

Fetch and display failed form submissions.

Return value

array Returns themed table with pager

1 string reference to 'CaptchaQuestionsDblogController::captchaQuestionsDblogView'
captcha_questions_dblog.routing.yml in captcha_questions_dblog/captcha_questions_dblog.routing.yml
captcha_questions_dblog/captcha_questions_dblog.routing.yml

File

captcha_questions_dblog/src/Controller/CaptchaQuestionsDblogController.php, line 58

Class

CaptchaQuestionsDblogController
Provides route responses for the Example module.

Namespace

Drupal\captcha_questions_dblog\Controller

Code

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',
  ]);

  // The actual action of sorting the rows is here.
  $table_sort = $query
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
    ->orderByHeader($header);

  // Limit the rows to 5 for each page.
  $pager = $table_sort
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->limit(5);
  $result = $pager
    ->execute();

  // Constructing rows from $entries matching $header.
  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);

  // The table description.
  $build = [
    '#markup' => $this
      ->t('Found a total of @count failed submissions', [
      '@count' => $count,
    ]),
  ];

  // Generate the table.
  $build['config_table'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];

  // Finally add the pager.
  $build['pager'] = [
    '#type' => 'pager',
  ];
  return $build;
}