You are here

public function WebformAnalysis::getComponentValuesCount in Webform Analysis 8

Get Component Values Count.

Parameters

string $component: The component name.

Return value

array Values.

Overrides WebformAnalysisInterface::getComponentValuesCount

1 call to WebformAnalysis::getComponentValuesCount()
WebformAnalysis::getComponentRows in src/WebformAnalysis.php
Get Component Rows.

File

src/WebformAnalysis.php, line 85

Class

WebformAnalysis
WebformAnalysis.

Namespace

Drupal\webform_analysis

Code

public function getComponentValuesCount($component) {
  $db = \Drupal::database();
  $query = $db
    ->select('webform_submission_data', 'wsd');
  $query
    ->fields('wsd', [
    'value',
  ]);
  $query
    ->addExpression('COUNT(value)', 'quantity');
  if ($this->entity) {
    $query
      ->leftJoin('webform_submission', 'ws', 'wsd.sid = ws.sid');
  }
  $query
    ->condition('wsd.webform_id', $this->webform
    ->id());
  $query
    ->condition('name', $component);
  if ($this->entity) {
    $query
      ->condition('entity_type', $this->entity
      ->getEntityTypeId());
    $query
      ->condition('entity_id', $this->entity
      ->id());
  }
  $query
    ->groupBy('wsd.value');
  $records = $query
    ->execute()
    ->fetchAll();
  $values = [];
  $allNumeric = TRUE;
  foreach ($records as $record) {
    if (is_numeric($record->value)) {
      $value = $this
        ->castNumeric($record->value);
    }
    else {
      $value = $record->value;
      $allNumeric = FALSE;
    }
    $values[$value] = (int) $record->quantity;
  }
  if ($allNumeric) {
    ksort($values);
  }
  return $values;
}