|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Repository\Node; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
|
8
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
|
9
|
|
|
use Chamilo\CoreBundle\Entity\ResourceNode; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\ResourceRepository; |
|
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
14
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
15
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class CourseRepository. |
|
21
|
|
|
* |
|
22
|
|
|
* The functions inside this class must return an instance of QueryBuilder. |
|
23
|
|
|
*/ |
|
24
|
|
|
class CourseRepository extends ResourceRepository |
|
25
|
|
|
{ |
|
26
|
|
|
public function __construct(ManagerRegistry $registry) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($registry, Course::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Session $session |
|
33
|
|
|
* |
|
34
|
|
|
* @return ArrayCollection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getTools(Course $course, Session $session = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$orWhere = Criteria::expr()->eq('sessionId', 0); |
|
39
|
|
|
|
|
40
|
|
|
if ($session) { |
|
41
|
|
|
$orWhere = Criteria::expr()->in('sessionId', [0, $session->getId()]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$criteria = Criteria::create() |
|
45
|
|
|
->where(Criteria::expr()->isNull('sessionId')) |
|
46
|
|
|
->orWhere($orWhere); |
|
47
|
|
|
|
|
48
|
|
|
return $course->getTools()->matching($criteria); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function deleteCourse(Course $course): void |
|
52
|
|
|
{ |
|
53
|
|
|
$em = $this->getEntityManager(); |
|
54
|
|
|
|
|
55
|
|
|
// Deleting all nodes connected to the course: |
|
56
|
|
|
$node = $course->getResourceNode(); |
|
57
|
|
|
$children = $node->getChildren(); |
|
58
|
|
|
/** @var ResourceNode $child */ |
|
59
|
|
|
foreach ($children as $child) { |
|
60
|
|
|
$em->remove($child); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$em->remove($course); |
|
64
|
|
|
$em->flush(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function findOneByCode(string $code): ?Course |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->findOneBy(['code' => $code]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get all users that are registered in the course. No matter the status. |
|
74
|
|
|
* |
|
75
|
|
|
* @return QueryBuilder |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getSubscribedUsers(Course $course) |
|
78
|
|
|
{ |
|
79
|
|
|
// Course builder |
|
80
|
|
|
$queryBuilder = $this->createQueryBuilder('c'); |
|
81
|
|
|
|
|
82
|
|
|
// Selecting user info. |
|
83
|
|
|
$queryBuilder->select('DISTINCT user'); |
|
84
|
|
|
|
|
85
|
|
|
// Selecting courses for users. |
|
86
|
|
|
$queryBuilder->innerJoin('c.users', 'subscriptions'); |
|
87
|
|
|
$queryBuilder->innerJoin( |
|
88
|
|
|
User::class, |
|
89
|
|
|
'user', |
|
90
|
|
|
Join::WITH, |
|
91
|
|
|
'subscriptions.user = user.id' |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
if (api_is_western_name_order()) { |
|
95
|
|
|
$queryBuilder->add('orderBy', 'user.firstname ASC'); |
|
96
|
|
|
} else { |
|
97
|
|
|
$queryBuilder->add('orderBy', 'user.lastname ASC'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$wherePart = $queryBuilder->expr()->andx(); |
|
101
|
|
|
|
|
102
|
|
|
// Get only users subscribed to this course |
|
103
|
|
|
$wherePart->add($queryBuilder->expr()->eq('c.id', $course->getId())); |
|
104
|
|
|
|
|
105
|
|
|
// $wherePart->add($queryBuilder->expr()->eq('c.status', $status)); |
|
106
|
|
|
|
|
107
|
|
|
$queryBuilder->where($wherePart); |
|
108
|
|
|
|
|
109
|
|
|
return $queryBuilder; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Gets students subscribed in the course. |
|
114
|
|
|
* |
|
115
|
|
|
* @return QueryBuilder |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getSubscribedStudents(Course $course) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getSubscribedUsersByStatus($course, STUDENT); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gets the students subscribed in the course. |
|
124
|
|
|
* |
|
125
|
|
|
* @return QueryBuilder |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getSubscribedCoaches(Course $course) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->getSubscribedUsers($course); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets the teachers subscribed in the course. |
|
134
|
|
|
* |
|
135
|
|
|
* @return QueryBuilder |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getSubscribedTeachers(Course $course) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getSubscribedUsersByStatus($course, COURSEMANAGER); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $status use legacy chamilo constants COURSEMANAGER|STUDENT |
|
144
|
|
|
* |
|
145
|
|
|
* @return QueryBuilder |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getSubscribedUsersByStatus(Course $course, $status) |
|
148
|
|
|
{ |
|
149
|
|
|
$queryBuilder = $this->getSubscribedUsers($course); |
|
150
|
|
|
$queryBuilder |
|
151
|
|
|
->andWhere( |
|
152
|
|
|
$queryBuilder->expr()->eq('subscriptions.status', $status) |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
return $queryBuilder; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function getCoursesWithNoSession($urlId) |
|
159
|
|
|
{ |
|
160
|
|
|
$queryBuilder = $this->getRepository()->createQueryBuilder('c'); |
|
161
|
|
|
$queryBuilder = $queryBuilder |
|
162
|
|
|
->select('c') |
|
163
|
|
|
->leftJoin('c.urls', 'u') |
|
164
|
|
|
->leftJoin('c.sessions', 's') |
|
165
|
|
|
/*->leftJoin( |
|
166
|
|
|
'ChamiloCoreBundle:SessionRelCourse', |
|
167
|
|
|
'sc', |
|
168
|
|
|
Join::WITH, |
|
169
|
|
|
'c != sc.course' |
|
170
|
|
|
)->leftJoin( |
|
171
|
|
|
'ChamiloCoreBundle:AccessUrlRelCourse', |
|
172
|
|
|
'ac', |
|
173
|
|
|
Join::WITH, |
|
174
|
|
|
'c = ac.course' |
|
175
|
|
|
)*/ |
|
176
|
|
|
->where($queryBuilder->expr()->isNull('s')) |
|
177
|
|
|
//->where($queryBuilder->expr()->eq('s', 0)) |
|
178
|
|
|
->where($queryBuilder->expr()->eq('u.url', $urlId)) |
|
179
|
|
|
->getQuery(); |
|
180
|
|
|
|
|
181
|
|
|
$courses = $queryBuilder->getResult(); |
|
182
|
|
|
$courseList = []; |
|
183
|
|
|
/** @var Course $course */ |
|
184
|
|
|
foreach ($courses as $course) { |
|
185
|
|
|
if (empty(0 == $course->getSessions()->count())) { |
|
186
|
|
|
$courseList[] = $course; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $courseList; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get course user relationship based in the course_rel_user table. |
|
195
|
|
|
* |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getCoursesByUser(User $user, AccessUrl $url) |
|
199
|
|
|
{ |
|
200
|
|
|
$qb = $this->repository->createQueryBuilder('course'); |
|
201
|
|
|
|
|
202
|
|
|
$qb |
|
203
|
|
|
->innerJoin('user.courses', 'courseRelUser') |
|
204
|
|
|
->innerJoin('courseRelUser.course', 'course') |
|
205
|
|
|
->innerJoin('course.urls', 'accessUrlRelCourse') |
|
206
|
|
|
->innerJoin('accessUrlRelCourse.url', 'url') |
|
207
|
|
|
->where('user = :user') |
|
208
|
|
|
->andWhere('url = :url') |
|
209
|
|
|
->setParameters(['user' => $user, 'url' => $url]) |
|
210
|
|
|
->addSelect('courseRelUser') |
|
211
|
|
|
; |
|
212
|
|
|
|
|
213
|
|
|
$query = $qb->getQuery(); |
|
214
|
|
|
|
|
215
|
|
|
return $query->getResult(); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|