|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CourseBundle\Repository; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\ResourceInterface; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\ResourceRepository; |
|
13
|
|
|
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface; |
|
14
|
|
|
use Chamilo\CourseBundle\Entity\CQuiz; |
|
15
|
|
|
use DateTime; |
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class CQuizRepository extends ResourceRepository implements ResourceWithLinkInterface |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct(ManagerRegistry $registry) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($registry, CQuiz::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function findAllByCourse( |
|
28
|
|
|
Course $course, |
|
29
|
|
|
?Session $session = null, |
|
30
|
|
|
?string $title = null, |
|
31
|
|
|
?int $active = null, |
|
32
|
|
|
bool $onlyPublished = true, |
|
33
|
|
|
?int $categoryId = null |
|
34
|
|
|
): QueryBuilder { |
|
35
|
|
|
$qb = $this->getResourcesByCourse($course, $session); |
|
36
|
|
|
|
|
37
|
|
|
if ($onlyPublished) { |
|
38
|
|
|
$this->addDateFilterQueryBuilder(new DateTime(), $qb); |
|
39
|
|
|
} |
|
40
|
|
|
$this->addCategoryQueryBuilder($categoryId, $qb); |
|
41
|
|
|
$this->addActiveQueryBuilder($active, $qb); |
|
42
|
|
|
$this->addNotDeletedQueryBuilder($qb); |
|
43
|
|
|
$this->addTitleQueryBuilder($title, $qb); |
|
44
|
|
|
|
|
45
|
|
|
return $qb; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getLink(ResourceInterface $resource, RouterInterface $router, array $extraParams = []): string |
|
49
|
|
|
{ |
|
50
|
|
|
$params = [ |
|
51
|
|
|
'name' => 'exercise/overview.php', |
|
52
|
|
|
'exerciseId' => $resource->getResourceIdentifier(), |
|
53
|
|
|
]; |
|
54
|
|
|
if (!empty($extraParams)) { |
|
55
|
|
|
$params = array_merge($params, $extraParams); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $router->generate('legacy_main', $params); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function addDateFilterQueryBuilder(DateTime $dateTime, ?QueryBuilder $qb = null): QueryBuilder |
|
62
|
|
|
{ |
|
63
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
|
64
|
|
|
$qb |
|
65
|
|
|
->andWhere('( |
|
66
|
|
|
( |
|
67
|
|
|
resource.startTime IS NOT NULL AND |
|
68
|
|
|
resource.startTime < :date AND |
|
69
|
|
|
resource.endTime IS NOT NULL AND |
|
70
|
|
|
resource.endTime > :date |
|
71
|
|
|
) OR |
|
72
|
|
|
(resource.startTime IS NOT NULL AND resource.startTime < :date AND resource.endTime IS NULL) OR |
|
73
|
|
|
(resource.startTime IS NULL AND resource.endTime IS NOT NULL AND resource.endTime > :date) OR |
|
74
|
|
|
(resource.startTime IS NULL AND resource.endTime IS NULL) |
|
75
|
|
|
) |
|
76
|
|
|
') |
|
77
|
|
|
->setParameter('date', $dateTime) |
|
78
|
|
|
; |
|
79
|
|
|
|
|
80
|
|
|
return $qb; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function addNotDeletedQueryBuilder(?QueryBuilder $qb = null): QueryBuilder |
|
84
|
|
|
{ |
|
85
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
|
86
|
|
|
|
|
87
|
|
|
$qb->andWhere('resource.active <> -1'); |
|
88
|
|
|
|
|
89
|
|
|
return $qb; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function addCategoryQueryBuilder(?int $categoryId = null, ?QueryBuilder $qb = null): QueryBuilder |
|
93
|
|
|
{ |
|
94
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
|
95
|
|
|
|
|
96
|
|
|
if (null !== $categoryId) { |
|
97
|
|
|
$qb |
|
98
|
|
|
->andWhere('resource.quizCategory = :category_id') |
|
99
|
|
|
->setParameter('category_id', $categoryId) |
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $qb; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Adds resource.active filter. |
|
108
|
|
|
* |
|
109
|
|
|
* The active parameter can be one of the following values. |
|
110
|
|
|
* |
|
111
|
|
|
* - null = no filter |
|
112
|
|
|
* - -1 = deleted exercises |
|
113
|
|
|
* - 0 = inactive exercises |
|
114
|
|
|
* - 1 = active exercises |
|
115
|
|
|
* - 2 = all exercises (active and inactive) |
|
116
|
|
|
*/ |
|
117
|
|
|
private function addActiveQueryBuilder(?int $active = null, ?QueryBuilder $qb = null): QueryBuilder |
|
118
|
|
|
{ |
|
119
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
|
120
|
|
|
|
|
121
|
|
|
if (null !== $active) { |
|
122
|
|
|
if (2 === $active) { |
|
123
|
|
|
$qb |
|
124
|
|
|
->andWhere('resource.active = 1 OR resource.active = 0') |
|
125
|
|
|
; |
|
126
|
|
|
} else { |
|
127
|
|
|
$qb |
|
128
|
|
|
->andWhere('resource.active = :active') |
|
129
|
|
|
->setParameter('active', $active) |
|
130
|
|
|
; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $qb; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Finds the auto-launchable quiz for the given course and session. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function findAutoLaunchableQuizByCourseAndSession(Course $course, ?Session $session = null): ?int |
|
141
|
|
|
{ |
|
142
|
|
|
$qb = $this->getResourcesByCourse($course, $session) |
|
143
|
|
|
->select('resource.iid') |
|
144
|
|
|
->andWhere('resource.autoLaunch = 1'); |
|
145
|
|
|
|
|
146
|
|
|
$qb->setMaxResults(1); |
|
147
|
|
|
|
|
148
|
|
|
$result = $qb->getQuery()->getOneOrNullResult(); |
|
149
|
|
|
|
|
150
|
|
|
return $result ? $result['iid'] : null; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|