You are here

protected function OptionsLimitWebformHandler::getTotalQuery in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php \Drupal\webform_options_limit\Plugin\WebformHandler\OptionsLimitWebformHandler::getTotalQuery()

Get base query for options and boolean limit totals.

Return value

bool|\Drupal\Core\Database\Query\SelectInterface The base query for options and boolean limit totals.

2 calls to OptionsLimitWebformHandler::getTotalQuery()
OptionsLimitWebformHandler::getBooleanTotal in modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php
Get boolean submission total for the current webform and source entity.
OptionsLimitWebformHandler::getOptionsTotals in modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php
Get options submission totals for the current webform and source entity.

File

modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php, line 1335

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

protected function getTotalQuery() {
  $webform = $this
    ->getWebform();

  /** @var \Drupal\Core\Database\StatementInterface $result */
  $query = $this->database
    ->select('webform_submission', 's');
  $query
    ->join('webform_submission_data', 'sd', 's.sid = sd.sid');
  $query
    ->fields('sd', [
    'value',
  ]);
  $query
    ->condition('sd.name', $this->configuration['element_key']);
  $query
    ->condition('sd.webform_id', $webform
    ->id());

  // Limit by source entity.
  if ($this->configuration['limit_source_entity']) {
    $source_entity = $this
      ->getSourceEntity();
    if ($source_entity) {
      $query
        ->condition('s.entity_type', $source_entity
        ->getEntityTypeId());
      $query
        ->condition('s.entity_id', $source_entity
        ->id());
    }
    else {
      $query
        ->isNull('s.entity_type');
      $query
        ->isNull('s.entity_id');
    }
  }

  // Limit by authenticated or anonymous user.
  if ($this->configuration['limit_user']) {
    $account = \Drupal::currentUser();
    if ($account
      ->isAuthenticated()) {
      $query
        ->condition('s.uid', $account
        ->id());
    }
    else {
      $sids = $this->submissionStorage
        ->getAnonymousSubmissionIds($account);
      if ($sids) {
        $query
          ->condition('s.sid', $sids, 'IN');
        $query
          ->condition('s.uid', 0);
      }
      else {
        return FALSE;
      }
    }
  }
  return $query;
}