You are here

public function AbjsDefaultController::abjsTestAdmin in A/B Test JS 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/AbjsDefaultController.php \Drupal\abjs\Controller\AbjsDefaultController::abjsTestAdmin()

Lists all tests in a default table theme.

Sorted by active tests first, then modified most recently, then created most recently. For each test, link to test, and list active status, conditions applied (with links), experiences with fractions assigned (with links), and created and edited info.

1 string reference to 'AbjsDefaultController::abjsTestAdmin'
abjs.routing.yml in ./abjs.routing.yml
abjs.routing.yml

File

src/Controller/AbjsDefaultController.php, line 82

Class

AbjsDefaultController
Default controller for the abjs module.

Namespace

Drupal\abjs\Controller

Code

public function abjsTestAdmin() {

  //$db = Database::getConnection();
  $date_Formatter = $this->dateFormatter;
  $output = [];
  $header = [
    $this
      ->t('ID'),
    $this
      ->t('Name'),
    $this
      ->t('Status'),
    $this
      ->t('Conditions'),
    $this
      ->t('Experiences'),
    $this
      ->t('Created'),
    $this
      ->t('Created By'),
    $this
      ->t('Changed'),
    $this
      ->t('Changed By'),
  ];
  $rows = [];
  $active_array = [
    $this
      ->t('Inactive'),
    $this
      ->t('Active'),
  ];
  $tests = $this->database
    ->query("SELECT * FROM {abjs_test} ORDER BY active DESC, changed DESC, created DESC")
    ->fetchAll();
  foreach ($tests as $test) {
    $test_link = [
      '#title' => $test->name,
      '#type' => 'link',
      '#url' => Url::fromRoute('abjs.test_edit_form', [
        'tid' => $test->tid,
      ]),
    ];
    $condition_list = [];
    $condition_output = '';
    $conditions = $this->database
      ->query("SELECT tc.cid, c.name FROM {abjs_test_condition} AS tc INNER JOIN {abjs_condition} AS c ON tc.cid = c.cid WHERE tc.tid = :tid", [
      ':tid' => $test->tid,
    ])
      ->fetchAll();
    if (!empty($conditions)) {
      foreach ($conditions as $condition) {
        $condition_link = [
          '#title' => $condition->name . ' (c_' . $condition->cid . ')',
          '#type' => 'link',
          '#url' => Url::fromRoute('abjs.condition_edit_form', [
            'cid' => $condition->cid,
          ]),
        ];
        $condition_list[] = $this->renderer
          ->render($condition_link);
      }
      $condition_output = [
        '#theme' => 'item_list',
        '#items' => $condition_list,
      ];
    }
    $experience_list = [];
    $experience_output = '';
    $experiences = $this->database
      ->query("SELECT te.eid, te.fraction, e.name FROM {abjs_test_experience} AS te INNER JOIN {abjs_experience} AS e ON te.eid = e.eid WHERE te.tid = :tid", [
      ':tid' => $test->tid,
    ])
      ->fetchAll();
    if (!empty($experiences)) {
      foreach ($experiences as $experience) {
        $experience_link = [
          '#title' => '[' . $experience->fraction . '] ' . $experience->name . ' (e_' . $experience->eid . ')',
          '#type' => 'link',
          '#url' => Url::fromRoute('abjs.experience_edit_form', [
            'eid' => $experience->eid,
          ]),
        ];
        $experience_list[] = $this->renderer
          ->render($experience_link);
      }
      $experience_output = [
        '#theme' => 'item_list',
        '#items' => $experience_list,
      ];
    }
    $user_created = $this->userStorage
      ->load($test->created_by);
    $user_changed = $this->userStorage
      ->load($test->changed_by);
    $rows[] = [
      't_' . $test->tid,
      $this->renderer
        ->render($test_link),
      $active_array[$test->active],
      $this->renderer
        ->render($condition_output),
      $this->renderer
        ->render($experience_output),
      $date_Formatter
        ->format($test->created),
      $user_created
        ->toLink(),
      $date_Formatter
        ->format($test->changed),
      $user_changed
        ->toLink(),
    ];
  }
  $output['add'] = [
    '#title' => $this
      ->t('Add new test'),
    '#type' => 'link',
    '#url' => Url::fromRoute('abjs.test_add_form'),
    '#attributes' => [
      'class' => 'button button-action button--primary button--small',
    ],
    '#suffix' => '<br><br>',
  ];
  $output['table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];
  return $output;
}