Completed
Push — master ( 9804ea...45188d )
by Sam
02:27
created

UserQuery::getMostActiveWatchersQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Jalle19\StatusManager\Database;
4
5
use Jalle19\StatusManager\Database\Base\UserQuery as BaseUserQuery;
6
use Propel\Runtime\ActiveQuery\Criteria;
7
8
/**
9
 * @package   Jalle19\StatusManager\Database
10
 * @copyright Copyright &copy; Sam Stenvall 2015-
11
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
12
 */
13
class UserQuery extends BaseUserQuery
14
{
15
16
	/**
17
	 * @param string $instanceName
18
	 * @param string $userName
19
	 *
20
	 * @return bool
21
	 */
22
	public function hasUser($instanceName, $userName)
23
	{
24
		return $this->filterByInstanceName($instanceName)->filterByName($userName)->findOne() !== null;
25
	}
26
27
28
	/**
29
	 * @param Instance $instance
30
	 *
31
	 * @return UserQuery
32
	 * @throws \Propel\Runtime\Exception\PropelException
33
	 */
34
	public function getMostActiveWatchersQuery($instance)
35
	{
36
		$this->withColumn('user.name', 'userName');
37
		$this->withColumn('SUM((julianday(subscription.stopped) - julianday(subscription.started)) * 86400)',
38
			'totalTimeSeconds');
39
40
		$this->select(['userName', 'totalTimeSeconds']);
41
42
		$this->useSubscriptionQuery()->filterByStopped(null, Criteria::NOT_EQUAL)->endUse();
43
		$this->filterByInstance($instance);
44
45
		$this->groupBy('userName');
46
		$this->orderBy('totalTimeSeconds', Criteria::DESC);
47
48
		return $this;
49
	}
50
51
}
52