You are here

public function Submissions::buildRenderArray in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x modules/dashboards_webform/src/Plugin/Dashboard/Submissions.php \Drupal\dashboards_webform\Plugin\Dashboard\Submissions::buildRenderArray()

Build render array.

Parameters

array $configuration: Plugin configuration.

Return value

array Return render array.

Overrides DashboardBase::buildRenderArray

File

modules/dashboards_webform/src/Plugin/Dashboard/Submissions.php, line 110

Class

Submissions
Show account info.

Namespace

Drupal\dashboards_webform\Plugin\Dashboard

Code

public function buildRenderArray($configuration) : array {
  $field = $configuration['period'];
  $cid = md5(serialize([
    $configuration['period'],
    $configuration['date'],
  ]));
  $dateLabel = $this
    ->t('Date');
  $cache = $this
    ->getCache($cid);
  $cache = FALSE;
  if (!$cache) {
    $query = $this->database
      ->select('webform_submission', 'ws');
    if (isset($configuration['webform'])) {
      $query
        ->condition('webform_id', $configuration['webform']);
    }
    switch ($configuration['date']) {
      case 'yesterday':
        $query
          ->condition('ws.created', [
          strtotime('yesterday'),
          strtotime('today'),
        ], 'BETWEEN');
        break;
      case 'this_week':
        $query
          ->condition('ws.created', strtotime('this week'), '>=');
        break;
      case 'this_month':
        $query
          ->condition('ws.created', strtotime('first day of this month'), '>=');
        break;
      case 'last_three_months':
        $query
          ->condition('ws.created', strtotime('first day of this month -3 months'), '>=');
        break;
      case 'last_six_months':
        $query
          ->condition('ws.created', strtotime('first day of this month -6 months'), '>=');
        break;
      case 'year':
        $query
          ->condition('ws.created', strtotime('first day of january this year'), '>=');
        break;
      default:
        $query
          ->condition('ws.created', strtotime('yesterday'), '>=');
        break;
    }
    switch ($field) {
      case 'week':
        $query
          ->addExpression('CONCAT(YEAR(FROM_UNIXTIME(ws.created)), \'-\', WEEK(FROM_UNIXTIME(ws.created)))', 'date');
        break;
      case 'month':
        $query
          ->addExpression('CONCAT(YEAR(FROM_UNIXTIME(ws.created)), \'-\', MONTH(FROM_UNIXTIME(ws.created)))', 'date');
        break;
      case 'hour':
        $query
          ->addExpression('CONCAT(YEAR(FROM_UNIXTIME(ws.created)), \'-\', MONTH(FROM_UNIXTIME(ws.created)),\'-\', DAY(FROM_UNIXTIME(ws.created)), \' \', HOUR(FROM_UNIXTIME(ws.created)),\':00\')', 'date');
        break;
      default:
        $query
          ->addExpression('CONCAT(YEAR(FROM_UNIXTIME(ws.created)), \'-\', MONTH(FROM_UNIXTIME(ws.created)), \'-\', DAY(FROM_UNIXTIME(ws.created)))', 'date');
        break;
    }
    $query
      ->addExpression('COUNT(*)', 'count');
    $query
      ->groupBy('date');
    $query
      ->groupBy('webform_id');
    $query
      ->orderBy('webform_id');
    $query
      ->fields('ws', [
      'webform_id',
    ]);
    $result = $query
      ->execute()
      ->fetchAll();
    $rows = [];
    $labels = [];
    foreach ($result as $r) {
      $labels[$r->webform_id] = $r->webform_id;
    }
    foreach ($labels as $key => $label) {
      foreach ($result as $r) {
        if ($r->webform_id != $label) {
          continue;
        }
        $rows[$r->date][$key] = $r->count;
      }
    }
    foreach ($rows as $key => $row) {
      foreach ($labels as $label) {
        if (!isset($row[$label])) {
          $rows[$key][$label] = 0;
        }
      }
    }
    foreach ($rows as $key => $row) {
      array_unshift($rows[$key], $key);
    }
    usort($rows, function ($a, $b) {
      return strcmp($a[0], $b[0]);
    });
    $labels = array_merge([
      $dateLabel,
    ], $labels);
    $this
      ->setCache($cid, [
      'labels' => $labels,
      'rows' => $rows,
    ], time() + 1800, [
      'node_list',
    ]);
  }
  else {
    $rows = $cache->data['rows'];
    $labels = $cache->data['labels'];
  }
  $this
    ->setLabels($labels);
  $this
    ->setRows($rows);
  $this
    ->setChartType($configuration['chart_type']);
  $build = $this
    ->renderChart($configuration);
  $build['#cache'] = [
    'tags' => [
      'node_list',
    ],
  ];
  return $build;
}