1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Tool; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
10
|
|
|
use Chamilo\CoreBundle\Entity\ResourceType; |
11
|
|
|
use Chamilo\CoreBundle\Entity\Tool; |
12
|
|
|
use Chamilo\CoreBundle\Entity\ToolResourceRight; |
13
|
|
|
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; |
14
|
|
|
use Chamilo\CoreBundle\Settings\SettingsManager; |
15
|
|
|
use Chamilo\CourseBundle\Entity\CTool; |
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Symfony\Component\Security\Core\Security; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ToolChain. |
21
|
|
|
* |
22
|
|
|
* The course tools classes (agenda, blog, etc) are located in: |
23
|
|
|
* |
24
|
|
|
* src/Chamilo/CourseBundle/Tool |
25
|
|
|
* |
26
|
|
|
* All this classes are registered as a service with the tag "chamilo_core.tool" here: |
27
|
|
|
* |
28
|
|
|
* src/Chamilo/CoreBundle/Resources/config/tools.yml |
29
|
|
|
* |
30
|
|
|
* The register process is made using the class ToolCompilerClass: |
31
|
|
|
* |
32
|
|
|
* src/Chamilo/CoreBundle/DependencyInjection/Compiler/ToolCompilerClass.php |
33
|
|
|
* |
34
|
|
|
* The tool chain is just an array that includes all the tools registered in services.yml |
35
|
|
|
* |
36
|
|
|
* The tool chain is hook when a new course is created via a listener here: |
37
|
|
|
* |
38
|
|
|
* src/Chamilo/CoreBundle/Entity/Listener/CourseListener.php |
39
|
|
|
* |
40
|
|
|
* After a course is created this function is called: CourseListener::prePersist() |
41
|
|
|
* This function includes the called to the function "addToolsInCourse" inside the tool chain. |
42
|
|
|
* |
43
|
|
|
* This allows to tools more easily. Steps: |
44
|
|
|
* |
45
|
|
|
* 1. Create a new tool class here: src/Chamilo/CoreBundle/Tool |
46
|
|
|
* 2. Add the class as a service here: src/Chamilo/CoreBundle/Resources/config/tools.yml (see examples there) |
47
|
|
|
* 3. Create a new course. When you create a new course the new tool will be created. |
48
|
|
|
*/ |
49
|
|
|
class ToolChain |
50
|
|
|
{ |
51
|
|
|
protected EntityManagerInterface $entityManager; |
52
|
|
|
|
53
|
|
|
protected SettingsManager $settingsManager; |
54
|
|
|
|
55
|
|
|
protected Security $security; |
56
|
|
|
|
57
|
|
|
protected HandlerCollection $handlerCollection; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string[] |
61
|
|
|
*/ |
62
|
|
|
private array $resourceTypeList = []; |
63
|
|
|
|
64
|
|
|
public function __construct(EntityManagerInterface $entityManager, SettingsManager $settingsManager, Security $security, HandlerCollection $handlerCollection) |
65
|
|
|
{ |
66
|
|
|
$this->entityManager = $entityManager; |
67
|
|
|
$this->settingsManager = $settingsManager; |
68
|
|
|
$this->security = $security; |
69
|
|
|
$this->handlerCollection = $handlerCollection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function createTools(): void |
73
|
|
|
{ |
74
|
|
|
$manager = $this->entityManager; |
75
|
|
|
$tools = $this->handlerCollection->getCollection(); |
76
|
|
|
$toolRepo = $manager->getRepository(Tool::class); |
77
|
|
|
|
78
|
|
|
foreach ($tools as $tool) { |
79
|
|
|
$name = $tool->getName(); |
80
|
|
|
$toolFromDatabase = $toolRepo->findOneBy([ |
81
|
|
|
'name' => $name, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
if (null !== $toolFromDatabase) { |
85
|
|
|
$toolEntity = $toolFromDatabase; |
86
|
|
|
} else { |
87
|
|
|
$toolEntity = (new Tool()) |
88
|
|
|
->setName($name) |
89
|
|
|
; |
90
|
|
|
if ($tool->isCourseTool()) { |
91
|
|
|
$this->setToolPermissions($toolEntity); |
92
|
|
|
} |
93
|
|
|
$manager->persist($toolEntity); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$types = $tool->getResourceTypes(); |
97
|
|
|
|
98
|
|
|
if (!empty($types)) { |
99
|
|
|
foreach ($types as $key => $typeName) { |
100
|
|
|
$resourceType = (new ResourceType()) |
101
|
|
|
->setName($key) |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
if ($toolEntity->hasResourceType($resourceType)) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
$resourceType->setTool($toolEntity); |
108
|
|
|
$manager->persist($resourceType); |
109
|
|
|
} |
110
|
|
|
$manager->flush(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function setToolPermissions(Tool $tool): void |
116
|
|
|
{ |
117
|
|
|
$toolResourceRight = new ToolResourceRight(); |
118
|
|
|
$toolResourceRight |
119
|
|
|
->setRole('ROLE_TEACHER') |
120
|
|
|
->setMask(ResourceNodeVoter::getEditorMask()) |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
$toolResourceRightReader = new ToolResourceRight(); |
124
|
|
|
$toolResourceRightReader |
125
|
|
|
->setRole('ROLE_STUDENT') |
126
|
|
|
->setMask(ResourceNodeVoter::getReaderMask()) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
//$tool->addToolResourceRight($toolResourceRight); |
130
|
|
|
//$tool->addToolResourceRight($toolResourceRightReader); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function addToolsInCourse(Course $course): Course |
134
|
|
|
{ |
135
|
|
|
$manager = $this->entityManager; |
136
|
|
|
$toolVisibility = $this->settingsManager->getSetting('course.active_tools_on_create'); |
137
|
|
|
|
138
|
|
|
// Hardcoded tool list order |
139
|
|
|
$toolList = [ |
140
|
|
|
'course_description', |
141
|
|
|
'document', |
142
|
|
|
'learnpath', |
143
|
|
|
'link', |
144
|
|
|
'quiz', |
145
|
|
|
'announcement', |
146
|
|
|
'gradebook', |
147
|
|
|
'glossary', |
148
|
|
|
'attendance', |
149
|
|
|
'course_progress', |
150
|
|
|
'agenda', |
151
|
|
|
'forum', |
152
|
|
|
'dropbox', |
153
|
|
|
'member', |
154
|
|
|
'group', |
155
|
|
|
'chat', |
156
|
|
|
'student_publication', |
157
|
|
|
'survey', |
158
|
|
|
//'wiki', |
159
|
|
|
'notebook', |
160
|
|
|
//'blog', |
161
|
|
|
'course_tool', |
162
|
|
|
'tracking', |
163
|
|
|
'course_setting', |
164
|
|
|
'course_maintenance', |
165
|
|
|
]; |
166
|
|
|
$toolList = array_flip($toolList); |
167
|
|
|
|
168
|
|
|
$toolRepo = $manager->getRepository(Tool::class); |
169
|
|
|
|
170
|
|
|
$tools = $this->handlerCollection->getCollection(); |
171
|
|
|
|
172
|
|
|
foreach ($tools as $tool) { |
173
|
|
|
$visibility = \in_array($tool->getName(), $toolVisibility, true); |
174
|
|
|
$criteria = [ |
175
|
|
|
'name' => $tool->getName(), |
176
|
|
|
]; |
177
|
|
|
if (!isset($toolList[$tool->getName()])) { |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** @var Tool $toolEntity */ |
182
|
|
|
$toolEntity = $toolRepo->findOneBy($criteria); |
183
|
|
|
$position = $toolList[$tool->getName()] + 1; |
184
|
|
|
|
185
|
|
|
$courseTool = (new CTool()) |
186
|
|
|
->setTool($toolEntity) |
187
|
|
|
->setName($tool->getName()) |
188
|
|
|
->setPosition($position) |
189
|
|
|
->setVisibility($visibility) |
190
|
|
|
->setParent($course) |
191
|
|
|
->setCreator($course->getCreator()) |
192
|
|
|
->addCourseLink($course) |
193
|
|
|
; |
194
|
|
|
$course->addTool($courseTool); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $course; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getTools() |
201
|
|
|
{ |
202
|
|
|
return $this->handlerCollection->getCollection(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getToolFromName(string $name): AbstractTool |
206
|
|
|
{ |
207
|
|
|
return $this->handlerCollection->getHandler($name); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/*public function getToolFromEntity(string $entityClass): AbstractTool |
211
|
|
|
{ |
212
|
|
|
return $this->handlerCollection->getHandler($entityClass); |
213
|
|
|
}*/ |
214
|
|
|
|
215
|
|
|
public function getResourceTypeNameByEntity(string $entityClass): ?string |
216
|
|
|
{ |
217
|
|
|
$name = $this->getResourceTypeList()[$entityClass] ?? null; |
218
|
|
|
|
219
|
|
|
if (null === $name) { |
220
|
|
|
return null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$name = explode('::', $name); |
224
|
|
|
|
225
|
|
|
return $name[1]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getResourceTypeList(): array |
229
|
|
|
{ |
230
|
|
|
$tools = $this->handlerCollection->getCollection(); |
231
|
|
|
|
232
|
|
|
foreach ($tools as $tool) { |
233
|
|
|
$toolName = $tool->getName(); |
234
|
|
|
$typeList = $tool->getResourceTypes(); |
235
|
|
|
if (!empty($typeList)) { |
236
|
|
|
foreach ($typeList as $name => $entityClass) { |
237
|
|
|
$this->resourceTypeList[$entityClass] = $toolName.'::'.$name; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this->resourceTypeList; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|