Skip to content
Snippets Groups Projects
Commit 64322927 authored by Erik Seifert's avatar Erik Seifert
Browse files

Fix: add webform module

parent c4c13856
No related branches found
No related tags found
No related merge requests found
......@@ -133,7 +133,7 @@ const cmap = require('colormap');
}
}
});
$(e, 'div').style.display = 'none';
// $(e, 'div').style.display = 'none';
}
}
})(Drupal, function(e,s) {
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -10,7 +10,7 @@ use Drupal\dashboards\Plugin\DashboardBase;
* @Dashboard(
* id = "matomo_countries",
* label = @Translation("Show per country."),
* category = @Translation("use Drupal\Core\Form\FormStateInterface;"),
* category = @Translation("Matomo"),
* )
*/
class Country extends MatomoBase {
......
name: Dashboards webform
description: 'Webform integration.'
package: Dashboards
core: 8.x
type: module
dependencies:
- dashboards:dashboards
- webform:webform
<?php
namespace Drupal\dashboards_webform\Plugin\Dashboard;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\dashboards\Plugin\DashboardBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\dashboards\Plugin\Dashboard\ChartTrait;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Show account info.
*
* @Dashboard(
* id = "webform_submissions",
* label = @Translation("Submission statistic."),
* category = @Translation("Webform"),
* )
*/
class Submissions extends DashboardBase {
use ChartTrait;
/**
* Database.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache_backend, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $cache_backend);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('dashboards.cache'),
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration): array {
$form['period'] = [
'#type' => 'select',
'#options' => [
'hour' => $this->t('Hour'),
'day' => $this->t('Day'),
'week' => $this->t('Week'),
'month' => $this->t('Month'),
],
'#default_value' => (isset($configuration['period'])) ? $configuration['period'] : 'day',
];
$form['date'] = [
'#type' => 'select',
'#options' => [
'today' => $this->t('Today'),
'yesterday' => $this->t('Yesterday'),
'this_week' => $this->t('This week'),
'this_month' => $this->t('This month'),
'last_three_months' => $this->t('Last 3 months'),
'last_six_months' => $this->t('Last 6 months'),
'year' => $this->t('This year'),
],
'#default_value' => (isset($configuration['date'])) ? $configuration['date'] : 'today',
];
$form['chart_type'] = [
'#type' => 'select',
'#options' => $this->getAllowedStyles(),
'#default_value' => (isset($configuration['chart_type'])) ? $configuration['chart_type'] : 'bar',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function buildRenderArray($configuration): array {
$field = $configuration['period'];
$cid = md5(serialize([
$configuration['period'],
$configuration['date'],
]));
$dateLabel = t('Date');
$cache = $this->getCache($cid);
$cache = FALSE;
if (!$cache) {
$query = $this->database->select('webform_submission', 'ws');
switch ($configuration['date']) {
case 'today':
$query->condition('ws.created', strtotime('yesterday'), '>=');
break;
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;
}
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();
$build['#cache'] = [
'tags' => ['node_list'],
];
return $build;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment