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.exerciseCategory = :category_id') |
99
|
|
|
->setParameter('category_id', $categoryId) |
100
|
|
|
; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $qb; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int|null $active |
108
|
|
|
* null = no filter |
109
|
|
|
* -1 = deleted exercises |
110
|
|
|
* 0 = inactive exercises |
111
|
|
|
* 1 = active exercises |
112
|
|
|
* 2 = all exercises (active and inactive) |
113
|
|
|
*/ |
114
|
|
|
private function addActiveQueryBuilder(?int $active = null, QueryBuilder $qb = null): QueryBuilder |
115
|
|
|
{ |
116
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
117
|
|
|
|
118
|
|
|
if (null !== $active) { |
119
|
|
|
if (2 === $active) { |
120
|
|
|
$qb |
121
|
|
|
->andWhere('resource.active = 1 OR resource.active = 0') |
122
|
|
|
; |
123
|
|
|
} else { |
124
|
|
|
$qb |
125
|
|
|
->andWhere('resource.active = :active') |
126
|
|
|
->setParameter('active', $active) |
127
|
|
|
; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $qb; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|