|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Badger\Bundle\UserBundle\Doctrine\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Badger\Component\Game\Model\TagInterface; |
|
6
|
|
|
use Badger\Component\User\Repository\UserRepositoryInterface; |
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\ORM\EntityRepository; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Adrien Pétremann <[email protected]> |
|
12
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
13
|
|
|
*/ |
|
14
|
|
|
class UserRepository extends EntityRepository implements UserRepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public function countAll() |
|
20
|
|
|
{ |
|
21
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
22
|
|
|
$qb->select($qb->expr()->count('u')) |
|
23
|
|
|
->from('UserBundle:User', 'u'); |
|
24
|
|
|
|
|
25
|
|
|
$query = $qb->getQuery(); |
|
26
|
|
|
|
|
27
|
|
|
return $query->getSingleScalarResult(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getSortedUserByUnlockedBadges($order = 'DESC', $limit = 10) |
|
34
|
|
|
{ |
|
35
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
36
|
|
|
$qb->select('u AS user, COUNT(bc.id) AS nbUnlockedBadges') |
|
37
|
|
|
->from('UserBundle:User', 'u') |
|
38
|
|
|
->leftJoin('GameBundle:BadgeCompletion', 'bc') |
|
39
|
|
|
->where('bc.user = u') |
|
40
|
|
|
->setMaxResults($limit) |
|
41
|
|
|
->orderBy('nbUnlockedBadges', $order) |
|
42
|
|
|
->groupBy('u') |
|
43
|
|
|
; |
|
44
|
|
|
|
|
45
|
|
|
$query = $qb->getQuery(); |
|
46
|
|
|
|
|
47
|
|
|
return $query->getResult(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getAllUsernames() |
|
54
|
|
|
{ |
|
55
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
56
|
|
|
$qb->select('u.username') |
|
57
|
|
|
->from('UserBundle:User', 'u') |
|
58
|
|
|
; |
|
59
|
|
|
|
|
60
|
|
|
$result = $qb->getQuery()->getResult(); |
|
61
|
|
|
|
|
62
|
|
|
return array_column($result, 'username'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getNewUsersForMonth($month, $year) |
|
69
|
|
|
{ |
|
70
|
|
|
$lastDay = date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
71
|
|
|
|
|
72
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
73
|
|
|
$qb->select('u') |
|
74
|
|
|
->from('UserBundle:User', 'u') |
|
75
|
|
|
->where('u.date_registered >= :firstDayOfMonth') |
|
76
|
|
|
->andWhere('u.date_registered <= :lastDayOfMonth') |
|
77
|
|
|
->orderBy('u.date_registered', 'DESC') |
|
78
|
|
|
->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month))) |
|
79
|
|
|
->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay))) |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
$query = $qb->getQuery(); |
|
83
|
|
|
|
|
84
|
|
|
return $query->getResult(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getMonthlyBadgeChampions($month, $year, TagInterface $tag, array $nbOfBadges) |
|
91
|
|
|
{ |
|
92
|
|
|
$lastDay = date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
93
|
|
|
|
|
94
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
95
|
|
|
$qb->select('u as user, COUNT(bc.id) as badgeCompletions') |
|
96
|
|
|
->from('UserBundle:User', 'u') |
|
97
|
|
|
->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'u.id = bc.user') |
|
98
|
|
|
->leftJoin('bc.badge', 'b') |
|
99
|
|
|
->leftJoin('b.tags', 't') |
|
100
|
|
|
->where('t.id = :tagId') |
|
101
|
|
|
->setParameter('tagId', $tag->getId()) |
|
102
|
|
|
->andWhere('bc.pending = 0') |
|
103
|
|
|
->andWhere('bc.completionDate >= :firstDayOfMonth') |
|
104
|
|
|
->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month))) |
|
105
|
|
|
->andWhere('bc.completionDate <= :lastDayOfMonth') |
|
106
|
|
|
->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay))) |
|
107
|
|
|
->groupBy('u') |
|
108
|
|
|
->having('badgeCompletions IN (:maxValues)') |
|
109
|
|
|
->setParameter('maxValues', $nbOfBadges, Connection::PARAM_STR_ARRAY) |
|
110
|
|
|
->orderBy('badgeCompletions', 'DESC') |
|
111
|
|
|
; |
|
112
|
|
|
|
|
113
|
|
|
$query = $qb->getQuery(); |
|
114
|
|
|
|
|
115
|
|
|
return $query->getResult(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|