1
|
|
|
<?php |
2
|
|
|
/* For licensing terms, see /license.txt */ |
3
|
|
|
|
4
|
|
|
use Chamilo\PluginBundle\Entity\H5pImport\H5pImport; |
5
|
|
|
use Chamilo\PluginBundle\Entity\H5pImport\H5pImportLibrary; |
6
|
|
|
use Chamilo\PluginBundle\Entity\H5pImport\H5pImportResults; |
7
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
8
|
|
|
use Doctrine\ORM\Tools\ToolsException; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Define the H5pImportPlugin class as an extension of Plugin |
13
|
|
|
* install/uninstall the plugin. |
14
|
|
|
*/ |
15
|
|
|
class H5pImportPlugin extends Plugin |
16
|
|
|
{ |
17
|
|
|
public const TBL_H5P_IMPORT = 'plugin_h5p_import'; |
18
|
|
|
public const TBL_H5P_IMPORT_LIBRARY = 'plugin_h5p_import_library'; |
19
|
|
|
public const TBL_H5P_IMPORT_RESULTS = 'plugin_h5p_import_results'; |
20
|
|
|
|
21
|
|
|
protected function __construct() |
22
|
|
|
{ |
23
|
|
|
$settings = [ |
24
|
|
|
'tool_enable' => 'boolean', |
25
|
|
|
'frame' => 'boolean', |
26
|
|
|
'embed' => 'boolean', |
27
|
|
|
'copyright' => 'boolean', |
28
|
|
|
'icon' => 'boolean', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
parent::__construct( |
32
|
|
|
'0.1', |
33
|
|
|
'Borja Sanchez', |
34
|
|
|
$settings |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function create(): ?H5pImportPlugin |
39
|
|
|
{ |
40
|
|
|
static $result = null; |
41
|
|
|
|
42
|
|
|
return $result ? $result : $result = new self(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Updates and returns the total duration in the view of an H5P learning path item in a course. |
47
|
|
|
* |
48
|
|
|
* @param int $lpItemId The ID of the learning path item |
49
|
|
|
* @param int $userId The user ID |
50
|
|
|
* |
51
|
|
|
* @return int The updated total duration in the learning path item view |
52
|
|
|
*/ |
53
|
|
|
public static function fixTotalTimeInLpItemView( |
54
|
|
|
int $lpItemId, |
55
|
|
|
int $userId |
56
|
|
|
): int { |
57
|
|
|
$lpItemViewTable = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
58
|
|
|
|
59
|
|
|
$sql = "SELECT iid, score |
60
|
|
|
FROM $lpItemViewTable |
61
|
|
|
WHERE |
62
|
|
|
iid = $lpItemId |
63
|
|
|
ORDER BY view_count DESC |
64
|
|
|
LIMIT 1"; |
65
|
|
|
$responseItemView = Database::query($sql); |
66
|
|
|
$lpItemView = Database::fetch_array($responseItemView); |
67
|
|
|
|
68
|
|
|
// Get the total execution duration of the user in the learning path item view |
69
|
|
|
$sql = 'SELECT SUM(total_time) AS exe_duration |
70
|
|
|
FROM plugin_h5p_import_results |
71
|
|
|
WHERE |
72
|
|
|
user_id = '.$userId.' AND |
73
|
|
|
c_lp_item_view_id = '.$lpItemView['iid']. |
74
|
|
|
' ORDER BY total_time DESC'; |
75
|
|
|
$sumScoreResult = Database::query($sql); |
76
|
|
|
$durationRow = Database::fetch_assoc($sumScoreResult); |
77
|
|
|
|
78
|
|
|
if (!empty($durationRow['exe_duration'])) { |
79
|
|
|
// Update the total duration in the learning path item view |
80
|
|
|
$sqlUpdate = 'UPDATE '.$lpItemViewTable.' |
81
|
|
|
SET total_time = '.$durationRow['exe_duration'].' |
82
|
|
|
WHERE iid = '.$lpItemView['iid']; |
83
|
|
|
Database::query($sqlUpdate); |
84
|
|
|
|
85
|
|
|
return (int) $durationRow['exe_duration']; |
86
|
|
|
} else { |
87
|
|
|
// Update c_lp_item_view status |
88
|
|
|
$sqlUpdate = 'UPDATE '.$lpItemViewTable.' |
89
|
|
|
SET status = "not attempted", |
90
|
|
|
total_time = 0 |
91
|
|
|
WHERE iid = '.$lpItemView['iid']; |
92
|
|
|
Database::query($sqlUpdate); |
93
|
|
|
|
94
|
|
|
return 0; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getToolTitle(): string |
99
|
|
|
{ |
100
|
|
|
$title = $this->get_lang('plugin_title'); |
101
|
|
|
|
102
|
|
|
if (!empty($title)) { |
103
|
|
|
return $title; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->get_title(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @throws ToolsException |
111
|
|
|
* |
112
|
|
|
* @throws \Doctrine\DBAL\Exception |
113
|
|
|
*/ |
114
|
|
|
public function install() |
115
|
|
|
{ |
116
|
|
|
$em = Database::getManager(); |
117
|
|
|
if ($em->getConnection() |
118
|
|
|
->createSchemaManager() |
119
|
|
|
->tablesExist( |
120
|
|
|
[ |
121
|
|
|
self::TBL_H5P_IMPORT, |
122
|
|
|
self::TBL_H5P_IMPORT_LIBRARY, |
123
|
|
|
self::TBL_H5P_IMPORT_RESULTS, |
124
|
|
|
] |
125
|
|
|
) |
126
|
|
|
) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$schemaTool = new SchemaTool($em); |
131
|
|
|
$schemaTool->createSchema( |
132
|
|
|
[ |
133
|
|
|
$em->getClassMetadata(H5pImport::class), |
134
|
|
|
$em->getClassMetadata(H5pImportLibrary::class), |
135
|
|
|
$em->getClassMetadata(H5pImportResults::class), |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
$this->addCourseTools(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function addCourseTool(int $courseId) |
142
|
|
|
{ |
143
|
|
|
// The $link param is set to "../plugin" as a hack to link correctly to the plugin URL in course tool. |
144
|
|
|
// Otherwise, the link en the course tool will link to "/main/" URL. |
145
|
|
|
$this->createLinkToCourseTool( |
146
|
|
|
$this->get_lang('plugin_title'), |
147
|
|
|
$courseId, |
148
|
|
|
'plugin_h5p_import.png', |
149
|
|
|
'../plugin/H5pImport/start.php', |
150
|
|
|
0, |
151
|
|
|
'authoring' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @throws \Doctrine\DBAL\Exception |
157
|
|
|
*/ |
158
|
|
|
public function uninstall() |
159
|
|
|
{ |
160
|
|
|
$em = Database::getManager(); |
161
|
|
|
|
162
|
|
|
if (!$em->getConnection() |
163
|
|
|
->createSchemaManager() |
164
|
|
|
->tablesExist( |
165
|
|
|
[ |
166
|
|
|
self::TBL_H5P_IMPORT, |
167
|
|
|
self::TBL_H5P_IMPORT_LIBRARY, |
168
|
|
|
self::TBL_H5P_IMPORT_RESULTS, |
169
|
|
|
] |
170
|
|
|
) |
171
|
|
|
) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$schemaTool = new SchemaTool($em); |
176
|
|
|
$schemaTool->dropSchema( |
177
|
|
|
[ |
178
|
|
|
$em->getClassMetadata(H5pImport::class), |
179
|
|
|
$em->getClassMetadata(H5pImportLibrary::class), |
180
|
|
|
$em->getClassMetadata(H5pImportResults::class), |
181
|
|
|
] |
182
|
|
|
); |
183
|
|
|
$this->deleteCourseToolLinks(); |
184
|
|
|
$this->removeH5pDirectories(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Perform actions after configuring the H5P import plugin. |
189
|
|
|
* |
190
|
|
|
* @return H5pImportPlugin The H5P import plugin instance. |
191
|
|
|
*/ |
192
|
|
|
public function performActionsAfterConfigure(): H5pImportPlugin |
193
|
|
|
{ |
194
|
|
|
$this->deleteCourseToolLinks(); |
195
|
|
|
|
196
|
|
|
if ('true' === $this->get('tool_enable')) { |
197
|
|
|
$this->addCourseTools(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the view URL for an H5P import. |
205
|
|
|
* |
206
|
|
|
* @param H5pImport $h5pImport The H5P import object. |
207
|
|
|
* |
208
|
|
|
* @return string The view URL for the H5P import. |
209
|
|
|
*/ |
210
|
|
|
public function getViewUrl(H5pImport $h5pImport): string |
211
|
|
|
{ |
212
|
|
|
return api_get_path(WEB_PLUGIN_PATH).'H5pImport/view.php?id='.$h5pImport->getIid().'&'.api_get_cidreq(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Generates the LP resource block for H5P imports. |
217
|
|
|
* |
218
|
|
|
* @param int $lpId The LP ID. |
219
|
|
|
* |
220
|
|
|
* @return string The HTML for the LP resource block. |
221
|
|
|
*/ |
222
|
|
|
public function getLpResourceBlock(int $lpId): string |
223
|
|
|
{ |
224
|
|
|
$cidReq = api_get_cidreq(true, true, 'lp'); |
225
|
|
|
$webPath = api_get_path(WEB_PLUGIN_PATH).'H5pImport/'; |
226
|
|
|
$course = api_get_course_entity(); |
227
|
|
|
$session = api_get_session_entity(); |
228
|
|
|
|
229
|
|
|
$tools = Database::getManager() |
230
|
|
|
->getRepository(H5pImport::class) |
231
|
|
|
->findBy(['course' => $course, 'session' => $session]); |
232
|
|
|
|
233
|
|
|
$importIcon = Display::return_icon('plugin_h5p_import_upload.png'); |
234
|
|
|
$moveIcon = Display::url( |
235
|
|
|
Display::return_icon('move_everywhere.png', get_lang('Move'), [], ICON_SIZE_TINY), |
236
|
|
|
'#', |
237
|
|
|
['class' => 'moved'] |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$return = '<ul class="lp_resource">'; |
241
|
|
|
$return .= '<li class="lp_resource_element">'; |
242
|
|
|
$return .= $importIcon; |
243
|
|
|
$return .= Display::url( |
244
|
|
|
get_lang('Import'), |
245
|
|
|
$webPath."start.php?action=add&$cidReq&".http_build_query(['lp_id' => $lpId]) |
246
|
|
|
); |
247
|
|
|
$return .= '</li>'; |
248
|
|
|
|
249
|
|
|
/** @var H5pImport $tool */ |
250
|
|
|
foreach ($tools as $tool) { |
251
|
|
|
$toolAnchor = Display::url( |
252
|
|
|
Security::remove_XSS($tool->getName()), |
253
|
|
|
api_get_self()."?$cidReq&" |
254
|
|
|
.http_build_query( |
255
|
|
|
['action' => 'add_item', 'type' => TOOL_H5P, 'file' => $tool->getIid(), 'lp_id' => $lpId] |
|
|
|
|
256
|
|
|
), |
257
|
|
|
['class' => 'moved'] |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$return .= Display::tag( |
261
|
|
|
'li', |
262
|
|
|
$moveIcon.$importIcon.$toolAnchor, |
263
|
|
|
[ |
264
|
|
|
'class' => 'lp_resource_element', |
265
|
|
|
'data_id' => $tool->getIid(), |
266
|
|
|
'data_type' => TOOL_H5P, |
267
|
|
|
'title' => $tool->getName(), |
268
|
|
|
] |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$return .= '</ul>'; |
273
|
|
|
|
274
|
|
|
return $return; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Add course tools for all courses. |
279
|
|
|
*/ |
280
|
|
|
private function addCourseTools(): void |
281
|
|
|
{ |
282
|
|
|
$courses = Database::getManager() |
283
|
|
|
->createQuery('SELECT c.id FROM ChamiloCoreBundle:Course c') |
284
|
|
|
->getResult(); |
285
|
|
|
|
286
|
|
|
foreach ($courses as $course) { |
287
|
|
|
$this->addCourseTool($course['id']); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
private function deleteCourseToolLinks() |
292
|
|
|
{ |
293
|
|
|
Database::getManager() |
294
|
|
|
->createQuery('DELETE FROM ChamiloCourseBundle:CTool t WHERE t.category = :category AND t.link LIKE :link') |
295
|
|
|
->execute(['category' => 'authoring', 'link' => '../plugin/H5pImport/start.php%']); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Removes H5P directories for all courses. |
300
|
|
|
*/ |
301
|
|
|
private function removeH5pDirectories(): void |
302
|
|
|
{ |
303
|
|
|
$fs = new Filesystem(); |
304
|
|
|
$table = Database::get_main_table(TABLE_MAIN_COURSE); |
305
|
|
|
$sql = "SELECT id FROM $table ORDER BY id"; |
306
|
|
|
$res = Database::query($sql); |
307
|
|
|
while ($row = Database::fetch_assoc($res)) { |
308
|
|
|
$courseInfo = api_get_course_info_by_id($row['id']); |
309
|
|
|
$fs->remove($courseInfo['course_sys_path'].'/h5p'); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function get_name() |
314
|
|
|
{ |
315
|
|
|
return 'H5pImport'; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|