1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Repository\Node; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Component\Utils\ChamiloApi; |
10
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
11
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
12
|
|
|
use Chamilo\CoreBundle\Entity\CourseRelUser; |
13
|
|
|
use Chamilo\CoreBundle\Entity\ResourceNode; |
14
|
|
|
use Chamilo\CoreBundle\Entity\User; |
15
|
|
|
use Chamilo\CoreBundle\Repository\ResourceRepository; |
16
|
|
|
use Doctrine\Common\Collections\Criteria; |
17
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
19
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class CourseRepository. |
23
|
|
|
* |
24
|
|
|
* The functions inside this class should return an instance of QueryBuilder. |
25
|
|
|
*/ |
26
|
|
|
class CourseRepository extends ResourceRepository |
27
|
|
|
{ |
28
|
|
|
public function __construct(ManagerRegistry $registry) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($registry, Course::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function deleteCourse(Course $course): void |
34
|
|
|
{ |
35
|
|
|
$em = $this->getEntityManager(); |
36
|
|
|
|
37
|
|
|
// Deleting all nodes connected to the course: |
38
|
|
|
//$node = $course->getResourceNode(); |
39
|
|
|
//$children = $node->getChildren(); |
40
|
|
|
///* var ResourceNode $child |
41
|
|
|
/*foreach ($children as $child) { |
42
|
|
|
var_dump($child->getId().'-'.$child->getTitle().'<br />'); |
43
|
|
|
var_dump(get_class($child)); |
44
|
|
|
$em->remove($child); |
45
|
|
|
}*/ |
46
|
|
|
|
47
|
|
|
$em->remove($course); |
48
|
|
|
$em->flush(); |
49
|
|
|
$em->clear(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function findOneByCode(string $code): ?Course |
53
|
|
|
{ |
54
|
|
|
return $this->findOneBy([ |
55
|
|
|
'code' => $code, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get course user relationship based in the course_rel_user table. |
61
|
|
|
* |
62
|
|
|
* @return CourseRelUser[] |
63
|
|
|
*/ |
64
|
|
|
public function getCoursesByUser(User $user, AccessUrl $url) |
65
|
|
|
{ |
66
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
67
|
|
|
|
68
|
|
|
$qb |
69
|
|
|
->select('courseRelUser') |
70
|
|
|
->from(Course::class, 'c') |
71
|
|
|
->innerJoin(CourseRelUser::class, 'courseRelUser') |
72
|
|
|
//->innerJoin('c.users', 'courseRelUser') |
73
|
|
|
->innerJoin('c.urls', 'accessUrlRelCourse') |
74
|
|
|
->where('courseRelUser.user = :user') |
75
|
|
|
->andWhere('accessUrlRelCourse.url = :url') |
76
|
|
|
->setParameters([ |
77
|
|
|
'user' => $user, |
78
|
|
|
'url' => $url, |
79
|
|
|
]) |
80
|
|
|
; |
81
|
|
|
|
82
|
|
|
$query = $qb->getQuery(); |
83
|
|
|
|
84
|
|
|
return $query->getResult(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all users that are registered in the course. No matter the status. |
89
|
|
|
* |
90
|
|
|
* @return QueryBuilder |
91
|
|
|
*/ |
92
|
|
|
public function getSubscribedUsers(Course $course) |
93
|
|
|
{ |
94
|
|
|
// Course builder |
95
|
|
|
$queryBuilder = $this->createQueryBuilder('c'); |
96
|
|
|
|
97
|
|
|
// Selecting user info. |
98
|
|
|
$queryBuilder->select('DISTINCT user'); |
99
|
|
|
|
100
|
|
|
// Selecting courses for users. |
101
|
|
|
$queryBuilder->innerJoin('c.users', 'subscriptions'); |
102
|
|
|
$queryBuilder->innerJoin( |
103
|
|
|
User::class, |
104
|
|
|
'user', |
105
|
|
|
Join::WITH, |
106
|
|
|
'subscriptions.user = user.id' |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
if (api_is_western_name_order()) { |
110
|
|
|
$queryBuilder->orderBy('user.firstname', Criteria::ASC); |
111
|
|
|
} else { |
112
|
|
|
$queryBuilder->orderBy('user.lastname', Criteria::ASC); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$wherePart = $queryBuilder->expr()->andx(); |
116
|
|
|
|
117
|
|
|
// Get only users subscribed to this course |
118
|
|
|
$wherePart->add($queryBuilder->expr()->eq('c.id', $course->getId())); |
119
|
|
|
|
120
|
|
|
// $wherePart->add($queryBuilder->expr()->eq('c.status', $status)); |
121
|
|
|
|
122
|
|
|
$queryBuilder->where($wherePart); |
123
|
|
|
|
124
|
|
|
return $queryBuilder; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets students subscribed in the course. |
129
|
|
|
* |
130
|
|
|
* @return QueryBuilder |
131
|
|
|
*/ |
132
|
|
|
public function getSubscribedStudents(Course $course) |
133
|
|
|
{ |
134
|
|
|
return $this->getSubscribedUsersByStatus($course, STUDENT); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the students subscribed in the course. |
139
|
|
|
* |
140
|
|
|
* @return QueryBuilder |
141
|
|
|
*/ |
142
|
|
|
public function getSubscribedCoaches(Course $course) |
143
|
|
|
{ |
144
|
|
|
return $this->getSubscribedUsers($course); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets the teachers subscribed in the course. |
149
|
|
|
* |
150
|
|
|
* @return QueryBuilder |
151
|
|
|
*/ |
152
|
|
|
public function getSubscribedTeachers(Course $course) |
153
|
|
|
{ |
154
|
|
|
return $this->getSubscribedUsersByStatus($course, ChamiloApi::COURSE_MANAGER); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param int $status use legacy chamilo constants COURSEMANAGER|STUDENT |
159
|
|
|
* |
160
|
|
|
* @return QueryBuilder |
161
|
|
|
*/ |
162
|
|
|
public function getSubscribedUsersByStatus(Course $course, int $status) |
163
|
|
|
{ |
164
|
|
|
$queryBuilder = $this->getSubscribedUsers($course); |
165
|
|
|
$queryBuilder |
166
|
|
|
->andWhere( |
167
|
|
|
$queryBuilder->expr()->eq('subscriptions.status', $status) |
168
|
|
|
) |
169
|
|
|
; |
170
|
|
|
|
171
|
|
|
return $queryBuilder; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|