Lines: 63.89%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\views\field; | |
| 6 | ||
| 7 | use Drupal\views\Attribute\ViewsField; | |
| 8 | use Drupal\views\Plugin\views\field\FieldPluginBase; | |
| 9 | ||
| 10 | /** | |
| 11 | * Average time on page from stored page-view durations. | |
| 12 | */ | |
| 13 | #[ViewsField("visitors_avg_time_on_page")] | |
| 14 | class AvgTimeOnPage extends FieldPluginBase { | |
| 15 | ||
| 16 | /** | |
| 17 | * {@inheritdoc} | |
| 18 | */ | |
| 19 | public function query() { | |
| 20 | 3 | $this->ensureMyTable(); |
| 21 | ||
| 22 | 3 | $alias = $this->tableAlias . '__avg_time_on_page'; |
| 23 | 3 | foreach ($this->query->fields as $field_info) { |
| 24 | 3 | if (!empty($field_info['alias']) && $field_info['alias'] === $alias) { |
| 25 | 0 | $this->field_alias = $alias; |
| 26 | 0 | return; |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | // Views wraps this expression in SUM(), which closes on the CASE. | |
| 31 | 3 | $count = 'SUM(' . $this->pageCase('1') . ')'; |
| 32 | 3 | $expression = $this->pageCase('time_spent') . ') / NULLIF(' . $count . ', 0'; |
| 33 | 3 | $this->field_alias = $this->query->addField(NULL, $expression, $alias, [ |
| 34 | 3 | 'alias' => $alias, |
| 35 | 3 | 'aggregate' => TRUE, |
| 36 | 3 | 'function' => 'sum', |
| 37 | 3 | ]); |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * {@inheritdoc} | |
| 42 | */ | |
| 43 | public function render($values) { | |
| 44 | 1 | $value = $this->getValue($values); |
| 45 | 1 | if ($value === NULL || $value === '') { |
| 46 | 0 | return ''; |
| 47 | } | |
| 48 | 1 | return $this->formatSeconds((int) round((float) $value)); |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * {@inheritdoc} | |
| 53 | */ | |
| 54 | public function usesGroupBy() { | |
| 55 | 0 | return FALSE; |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * SQL that is the page-view value, or zero for other events. | |
| 60 | * | |
| 61 | * @param string $value | |
| 62 | * Column time_spent, or 1 when counting the page view. | |
| 63 | * | |
| 64 | * @return string | |
| 65 | * A CASE expression. | |
| 66 | */ | |
| 67 | protected function pageCase(string $value): string { | |
| 68 | 3 | $event = $this->tableAlias; |
| 69 | 3 | $measured = $value === 'time_spent' ? "{$event}.time_spent" : '1'; |
| 70 | 3 | return "CASE WHEN {$event}.event_category IS NULL" |
| 71 | 3 | . " AND ({$event}.event IS NULL" |
| 72 | 3 | . " OR {$event}.event NOT IN ('download', 'outlink'))" |
| 73 | 3 | . " THEN {$measured} ELSE 0 END"; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Formats a duration. | |
| 78 | * | |
| 79 | * @param int $seconds | |
| 80 | * The duration in seconds. | |
| 81 | * | |
| 82 | * @return string | |
| 83 | * A short duration, such as 45s or 1m 3s. | |
| 84 | */ | |
| 85 | protected function formatSeconds(int $seconds): string { | |
| 86 | 1 | $seconds = max(0, $seconds); |
| 87 | 1 | if ($seconds < 60) { |
| 88 | 1 | return $seconds . 's'; |
| 89 | } | |
| 90 | 0 | $minutes = intdiv($seconds, 60); |
| 91 | 0 | $remain = $seconds % 60; |
| 92 | 0 | if ($minutes < 60) { |
| 93 | 0 | return $remain === 0 ? $minutes . 'm' : $minutes . 'm ' . $remain . 's'; |
| 94 | } | |
| 95 | 0 | $hours = intdiv($minutes, 60); |
| 96 | 0 | $minutes %= 60; |
| 97 | 0 | if ($minutes === 0) { |
| 98 | 0 | return $hours . 'h'; |
| 99 | } | |
| 100 | 0 | return $hours . 'h ' . $minutes . 'm'; |
| 101 | } | |
| 102 | ||
| 103 | } |