Coverage index

src/Plugin/views/field/AvgTimeOnPage.php

Lines: 63.89%

LineHitsSource
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() {
203
    $this->ensureMyTable();
21
223
    $alias = $this->tableAlias . '__avg_time_on_page';
233
    foreach ($this->query->fields as $field_info) {
243
      if (!empty($field_info['alias']) && $field_info['alias'] === $alias) {
250
        $this->field_alias = $alias;
260
        return;
27
      }
28
    }
29
30
    // Views wraps this expression in SUM(), which closes on the CASE.
313
    $count = 'SUM(' . $this->pageCase('1') . ')';
323
    $expression = $this->pageCase('time_spent') . ') / NULLIF(' . $count . ', 0';
333
    $this->field_alias = $this->query->addField(NULL, $expression, $alias, [
343
      'alias' => $alias,
353
      'aggregate' => TRUE,
363
      'function' => 'sum',
373
    ]);
38
  }
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function render($values) {
441
    $value = $this->getValue($values);
451
    if ($value === NULL || $value === '') {
460
      return '';
47
    }
481
    return $this->formatSeconds((int) round((float) $value));
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function usesGroupBy() {
550
    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 {
683
    $event = $this->tableAlias;
693
    $measured = $value === 'time_spent' ? "{$event}.time_spent" : '1';
703
    return "CASE WHEN {$event}.event_category IS NULL"
713
      . " AND ({$event}.event IS NULL"
723
      . " OR {$event}.event NOT IN ('download', 'outlink'))"
733
      . " 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 {
861
    $seconds = max(0, $seconds);
871
    if ($seconds < 60) {
881
      return $seconds . 's';
89
    }
900
    $minutes = intdiv($seconds, 60);
910
    $remain = $seconds % 60;
920
    if ($minutes < 60) {
930
      return $remain === 0 ? $minutes . 'm' : $minutes . 'm ' . $remain . 's';
94
    }
950
    $hours = intdiv($minutes, 60);
960
    $minutes %= 60;
970
    if ($minutes === 0) {
980
      return $hours . 'h';
99
    }
1000
    return $hours . 'h ' . $minutes . 'm';
101
  }
102
103
}