Completed
Push — 1.11.x ( 1039fe...2dc406 )
by José
159:48 queued 115:41
created

UserRepository::getSessionAdmins()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 26
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 1
dl 26
loc 26
rs 8.8571
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\UserBundle\Entity\Repository;
5
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\Common\Collections\Criteria;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Chamilo\CoreBundle\Entity\Course;
10
use Doctrine\ORM\Query\Expr\Join;
11
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
12
13
//use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
14
//use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
15
16
/**
17
 * Class UserRepository
18
 *
19
 * All functions that query the database (selects)
20
 * Functions should return query builders.
21
 *
22
 * @package Chamilo\UserBundle\Repository
23
 */
24
class UserRepository extends EntityRepository
25
{
26
    /**
27
    * @param string $keyword
28
     *
29
    * @return mixed
30
    */
31 View Code Duplication
    public function searchUserByKeyword($keyword)
32
    {
33
        $qb = $this->createQueryBuilder('a');
34
35
        // Selecting user info
36
        $qb->select('DISTINCT b');
37
38
        $qb->from('Chamilo\UserBundle\Entity\User', 'b');
39
40
        // Selecting courses for users
41
        //$qb->innerJoin('u.courses', 'c');
42
43
        //@todo check app settings
44
        $qb->add('orderBy', 'b.firstname ASC');
45
        $qb->where('b.firstname LIKE :keyword OR b.lastname LIKE :keyword ');
46
        $qb->setParameter('keyword', "%$keyword%");
47
        $query = $qb->getQuery();
48
49
        return $query->execute();
50
    }
51
52
    /**
53
     * Get course user relationship based in the course_rel_user table.
54
     * @return array
55
     */
56
    /*public function getCourses(User $user)
57
    {
58
        $queryBuilder = $this->createQueryBuilder('user');
59
60
        // Selecting course info.
61
        $queryBuilder->select('c');
62
63
        // Loading User.
64
        //$qb->from('Chamilo\UserBundle\Entity\User', 'u');
65
66
        // Selecting course
67
        $queryBuilder->innerJoin('Chamilo\CoreBundle\Entity\Course', 'c');
68
69
        //@todo check app settings
70
        //$qb->add('orderBy', 'u.lastname ASC');
71
72
        $wherePart = $queryBuilder->expr()->andx();
73
74
        // Get only users subscribed to this course
75
        $wherePart->add($queryBuilder->expr()->eq('user.userId', $user->getUserId()));
76
77
        $queryBuilder->where($wherePart);
78
        $query = $queryBuilder->getQuery();
79
80
        return $query->execute();
81
    }
82
83
    public function getTeachers()
84
    {
85
        $queryBuilder = $this->createQueryBuilder('u');
86
87
        // Selecting course info.
88
        $queryBuilder
89
            ->select('u')
90
            ->where('u.groups.id = :groupId')
91
            ->setParameter('groupId', 1);
92
93
        $query = $queryBuilder->getQuery();
94
95
        return $query->execute();
96
    }*/
97
98
    /*public function getUsers($group)
99
    {
100
        $queryBuilder = $this->createQueryBuilder('u');
101
102
        // Selecting course info.
103
        $queryBuilder
104
            ->select('u')
105
            ->where('u.groups = :groupId')
106
            ->setParameter('groupId', $group);
107
108
        $query = $queryBuilder->getQuery();
109
110
        return $query->execute();
111
    }*/
112
113
    /**
114
     * Get a filtered list of user by status and (optionally) access url
115
     * @param string $query The query to filter
116
     * @param int $status The status
117
     * @param int $accessUrlId The access URL ID
118
     * @return array
119
     */
120 View Code Duplication
    public function searchUsersByStatus($query, $status, $accessUrlId = null)
121
    {
122
        $accessUrlId = intval($accessUrlId);
123
124
        $queryBuilder = $this->createQueryBuilder('u');
125
126
        if ($accessUrlId > 0) {
127
            $queryBuilder->innerJoin(
128
                'ChamiloCoreBundle:AccessUrlRelUser',
129
                'auru',
130
                \Doctrine\ORM\Query\Expr\Join::WITH,
131
                'u.id = auru.userId'
132
            );
133
        }
134
135
        $queryBuilder->where('u.status = :status')
136
            ->andWhere('u.username LIKE :query OR u.firstname LIKE :query OR u.lastname LIKE :query')
137
            ->setParameter('status', $status)
138
            ->setParameter('query', "$query%");
139
140
        if ($accessUrlId > 0) {
141
            $queryBuilder->andWhere('auru.accessUrlId = :url')
142
                ->setParameter(':url', $accessUrlId);
143
        }
144
145
        return $queryBuilder->getQuery()->getResult();
146
    }
147
148
    /**
149
     * Get the coaches for a course within a session
150
     * @param Session $session The session
151
     * @param Course $course The course
152
     * @return \Doctrine\ORM\QueryBuilder
153
     */
154 View Code Duplication
    public function getCoachesForSessionCourse(Session $session, Course $course)
155
    {
156
        $queryBuilder = $this->createQueryBuilder('u');
157
158
        $queryBuilder->select('u')
159
            ->innerJoin(
160
                'ChamiloCoreBundle:SessionRelCourseRelUser',
161
                'scu',
162
                Join::WITH,
163
                'scu.user = u'
164
            )
165
            ->where(
166
                $queryBuilder->expr()->andX(
167
                    $queryBuilder->expr()->eq('scu.session', $session->getId()),
168
                    $queryBuilder->expr()->eq('scu.course', $course->getId()),
169
                    $queryBuilder->expr()->eq('scu.status', SessionRelCourseRelUser::STATUS_COURSE_COACH)
170
                )
171
            );
172
173
        return $queryBuilder->getQuery()->getResult();
174
    }
175
176
    /**
177
     * Get the sessions admins for a user
178
     * @param \Chamilo\UserBundle\Entity\User $user The user
179
     * @return array
180
     */
181 View Code Duplication
    public function getSessionAdmins($user)
182
    {
183
        $queryBuilder = $this->createQueryBuilder('u');
184
        $queryBuilder
185
            ->distinct()
186
            ->innerJoin(
187
                'ChamiloCoreBundle:SessionRelUser',
188
                'su',
189
                Join::WITH,
190
                $queryBuilder->expr()->eq('u', 'su.user')
191
            )
192
            ->innerJoin(
193
                'ChamiloCoreBundle:SessionRelCourseRelUser',
194
                'scu',
195
                Join::WITH,
196
                $queryBuilder->expr()->eq('su.session', 'scu.session')
197
            )
198
            ->where(
199
                $queryBuilder->expr()->eq('scu.user', $user->getId())
200
            )
201
            ->andWhere(
202
                $queryBuilder->expr()->eq('su.relationType', SESSION_RELATION_TYPE_RRHH)
203
            );
204
205
        return $queryBuilder->getQuery()->getResult();
206
    }
207
208
    /**
209
     * Get the student bosses for a user
210
     * @param User $user The user
211
     * @return array
212
     */
213 View Code Duplication
    public function getStudentBosses($user)
214
    {
215
        $queryBuilder = $this->createQueryBuilder('u');
216
        $queryBuilder
217
            ->distinct()
218
            ->innerJoin(
219
                'ChamiloCoreBundle:UserRelUser',
220
                'uu',
221
                Join::WITH,
222
                $queryBuilder->expr()->eq('u.id', 'uu.friendUserId')
223
            )
224
            ->where(
225
                $queryBuilder->expr()->eq('uu.relationType', USER_RELATION_TYPE_BOSS)
226
            )
227
            ->andWhere(
228
                $queryBuilder->expr()->eq('uu.userId', $user->getId())
229
            );
230
231
        return $queryBuilder->getQuery()->getResult();
232
    }
233
}
234