Completed
Pull Request — master (#186)
by Vladimir
10:46
created

MatchActivityQueryBuilder::includeMatchActivity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
3
use Pecee\Pixie\QueryBuilder\JoinBuilder;
4
5
abstract class MatchActivityQueryBuilder extends QueryBuilderFlex
6
{
7
    /**
8
     * @throws \Pecee\Pixie\Exception
9
     * @throws Exception
10
     */
11
    protected function buildMatchActivity()
12
    {
13
        $qb = self::createBuilder();
14
        $type = $this->modelType;
15
16
        // The subquery to calculate each match's worth towards activity if it has occurred less than 45 days ago.
17
        //   - 86400 is in seconds; i.e. 24 hours
18
        //   - 0.0116687059537612 is a magic number
19
        $matchActivityWorthQuery = $qb->table('matches')->alias('m');
20
        $matchActivityWorthQuery
21
            ->select([
22
                'm.id',
23
                'm.team_a',
24
                'm.team_b',
25
                $qb->raw('TIMESTAMPDIFF(SECOND, `m`.`timestamp`, NOW()) / 86400 AS days_passed'),
26
                $qb->raw('(0.0116687059537612 * (POW((45 - LEAST((SELECT days_passed), 45)), (1/6)) + ATAN(31 - (SELECT days_passed)) / 2)) AS activity'),
27
            ])
28
            ->where($qb->raw('DATEDIFF(NOW(), `m`.`timestamp`) <= 45'))
29
            ->orderBy('m.timestamp', 'DESC')
30
        ;
31
32
        $this
33
            ->select(
34
                $qb->raw('SUM(m2.activity) AS activity')
35
            )
36
            ->leftJoin(
37
                $qb->subQuery($matchActivityWorthQuery, 'm2'),
38
                function ($table) {
39
                    /** @var JoinBuilder $table */
40
                    $table->on('teams.id', '=', 'm2.team_a');
41
                    $table->orOn('teams.id', '=', 'm2.team_b');
42
                }
43
            )
44
            ->whereNot('teams.is_deleted', '=', true)
45
            ->groupBy($type::getEagerColumnsList())
46
        ;
47
48
        return $this;
49
    }
50
51
//    protected function includeMatchActivity($selectColumns, $leftJoinOn, $useMatchParticipationTable = false)
52
//    {
53
//        $type = $this->type;
54
//        $columns = $type::getEagerColumns($this->getFromAlias());
55
//
56
//        $this->columns['activity'] = 'activity';
57
//        $this->extraColumns = 'SUM(m2.activity) AS activity';
58
//        $this->extras .= '
59
//          LEFT JOIN
60
//            (SELECT
61
//              m.id,'
62
//              . implode(',', $selectColumns) . ',
63
//              TIMESTAMPDIFF(SECOND, timestamp, NOW()) / 86400 AS days_passed,
64
//              (0.0116687059537612 * (POW((45 - LEAST((SELECT days_passed), 45)), (1/6)) + ATAN(31 - (SELECT days_passed)) / 2)) AS activity
65
//            FROM
66
//              matches m' .
67
//            ($useMatchParticipationTable ? ' INNER JOIN match_participation mp ON m.id = mp.match_id ' : '')
68
//            . '
69
//            WHERE
70
//              DATEDIFF(NOW(), timestamp) <= 45
71
//            ORDER BY
72
//              timestamp DESC) m2 ON ' . $leftJoinOn
73
//        ;
74
//
75
//        $this->groupQuery = 'GROUP BY ' . $columns;
76
//
77
//        return $this;
78
//    }
79
}
80