You are here

class WebformAnalysis in Webform Analysis 8

WebformAnalysis.

Hierarchy

Expanded class hierarchy of WebformAnalysis

3 files declare their use of WebformAnalysis
WebformAnalysisBlock.php in src/Plugin/Block/WebformAnalysisBlock.php
WebformAnalysisForm.php in src/Form/WebformAnalysisForm.php
webform_analysis.install in ./webform_analysis.install
Webform Analysis - Install.

File

src/WebformAnalysis.php, line 12

Namespace

Drupal\webform_analysis
View source
class WebformAnalysis implements WebformAnalysisInterface {
  use StringTranslationTrait;
  protected $webform;
  protected $entity;
  protected $elements;

  /**
   * Construct.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity of form.
   */
  public function __construct(EntityInterface $entity) {
    if ($entity instanceof WebformInterface) {
      $this->webform = $entity;
      $this->entity = NULL;
    }
    else {
      $this->entity = $entity;
      $this->webform = $entity->webform->entity;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getWebform() {
    return $this->webform;
  }

  /**
   * {@inheritdoc}
   */
  public function setComponents(array $components = []) {
    $this->webform
      ->setThirdPartySetting('webform_analysis', 'components', $components);
  }

  /**
   * {@inheritdoc}
   */
  public function getComponents() {
    return (array) $this->webform
      ->getThirdPartySetting('webform_analysis', 'components');
  }

  /**
   * {@inheritdoc}
   */
  public function setChartType($chart_type = '') {
    $this->webform
      ->setThirdPartySetting('webform_analysis', 'chart_type', $chart_type);
  }

  /**
   * {@inheritdoc}
   */
  public function getChartType() {
    return (string) $this->webform
      ->getThirdPartySetting('webform_analysis', 'chart_type');
  }

  /**
   * {@inheritdoc}
   */
  public function getElements() {
    if (!$this->elements) {
      $this->elements = $this->webform
        ->getElementsInitializedFlattenedAndHasValue();
    }
    return $this->elements;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponentRows($component, array $header = [], $value_label_with_count = FALSE) {
    $rows = [];
    foreach ($this
      ->getComponentValuesCount($component) as $value => $count) {
      switch ($this
        ->getElements()[$component]['#type']) {
        case 'checkbox':
          $value_label = $value ? $this
            ->t('Yes') : $this
            ->t('No');
          break;
        default:
          $value_label = isset($this
            ->getElements()[$component]['#options'][$value]) ? $this
            ->getElements()[$component]['#options'][$value] : $value;
          break;
      }
      if ($value_label_with_count) {
        $value_label .= ' : ' . $count;
      }
      $rows[] = [
        (string) $value_label,
        $count,
      ];
    }
    if ($header && $rows) {
      array_unshift($rows, $header);
    }
    return $rows;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponentTitle($component) {
    if (!isset($this
      ->getElements()[$component]['#title'])) {
      return $component;
    }
    return $this
      ->getElements()[$component]['#title'];
  }

  /**
   * {@inheritdoc}
   */
  public static function getChartTypeOptions() {
    return [
      '' => t('Table'),
      'PieChart' => t('Pie Chart'),
      'ColumnChart' => t('Column Chart'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function isInt($i = '') {
    return $i === (string) (int) $i;
  }

  /**
   * {@inheritdoc}
   */
  public function castNumeric($i = '') {
    return $this
      ->isInt($i) ? (int) $i : (double) $i;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformAnalysis::$elements protected property
WebformAnalysis::$entity protected property
WebformAnalysis::$webform protected property
WebformAnalysis::castNumeric public function Cast Numeric. Overrides WebformAnalysisInterface::castNumeric
WebformAnalysis::getChartType public function Get Chart Type. Overrides WebformAnalysisInterface::getChartType
WebformAnalysis::getChartTypeOptions public static function Get Chart Type Options. Overrides WebformAnalysisInterface::getChartTypeOptions
WebformAnalysis::getComponentRows public function Get Component Rows. Overrides WebformAnalysisInterface::getComponentRows
WebformAnalysis::getComponents public function Get Components. Overrides WebformAnalysisInterface::getComponents
WebformAnalysis::getComponentTitle public function Get Component title. Overrides WebformAnalysisInterface::getComponentTitle
WebformAnalysis::getComponentValuesCount public function Get Component Values Count. Overrides WebformAnalysisInterface::getComponentValuesCount
WebformAnalysis::getElements public function Get Elements. Overrides WebformAnalysisInterface::getElements
WebformAnalysis::getWebform public function Get Webform. Overrides WebformAnalysisInterface::getWebform
WebformAnalysis::isInt public function Is Int. Overrides WebformAnalysisInterface::isInt
WebformAnalysis::setChartType public function Set Chart Type. Overrides WebformAnalysisInterface::setChartType
WebformAnalysis::setComponents public function Set components and save webform. Overrides WebformAnalysisInterface::setComponents
WebformAnalysis::__construct public function Construct.