-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclass.xvmpGUI.php
More file actions
354 lines (317 loc) · 11.7 KB
/
class.xvmpGUI.php
File metadata and controls
354 lines (317 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
declare(strict_types=1);
/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
use ILIAS\DI\Container;
use srag\Plugins\ViMP\UIComponents\PlayerModal\PlayerContainerDTO;
use srag\Plugins\ViMP\Content\MediumMetadataDTOBuilder;
use srag\Plugins\ViMP\UIComponents\Renderer\Factory;
use srag\Plugins\ViMP\UIComponents\Player\VideoPlayer;
use ILIAS\StaticURL\Services;
use ILIAS\Data\ReferenceId;
/**
* Class xvmpGUI
* @author Theodor Truffer <tt@studer-raimann.ch>
*/
abstract class xvmpGUI
{
public const CMD_STANDARD = 'index';
public const CMD_CANCEL = 'cancel';
public const CMD_FLUSH_CACHE = 'flushCache';
public const CMD_FILL_MODAL = 'fillModalPlayer';
public const TAB_ACTIVE = ''; // overwrite in subclass
public const CMD_DOWNLOAD_MEDIUM = 'downloadMedium';
protected ilObjViMPGUI $parent_gui;
protected ilViMPPlugin $pl;
protected Container $dic;
protected Factory $renderer_factory;
protected MediumMetadataDTOBuilder $metadata_builder;
/**
* xvmpGUI constructor.
* @param ilObjViMPGUI $parent_gui
*/
public function __construct(ilObjViMPGUI $parent_gui)
{
global $DIC;
$this->dic = $DIC;
$this->pl = ilViMPPlugin::getInstance();
$this->parent_gui = $parent_gui;
$this->metadata_builder = new MediumMetadataDTOBuilder($DIC, $this->pl);
$this->renderer_factory = new Factory($DIC, $this->pl);
$this->addJavaScript();
}
protected function addJavaScript() : void
{
$this->dic->ui()->mainTemplate()->addJavaScript('./node_modules/webui-popover/dist/jquery.webui-popover.js');
$this->dic->ui()->mainTemplate()->addJavaScript('./src/UI/templates/js/Popover/popover.js');
}
/**
* @return ilModalGUI
*/
public static function getModalPlayer() : ilModalGUI
{
global $tpl;
$tpl->addCss(ilViMPPlugin::getInstance()->getAssetURL('default/modal.css'));
$modal = ilModalGUI::getInstance();
$modal->setId('xvmp_modal_player');
$modal->setType(ilModalGUI::TYPE_LARGE);
$modal->setBody('<section><div id="xvmp_video_container"></div></section>');
return $modal;
}
/**
* @return mixed
*/
abstract protected function index();
/**
* @throws ilCtrlException
*/
protected function cancel() : void
{
$this->dic->ctrl()->redirect($this, self::CMD_STANDARD);
}
/**
* @throws xvmpException
*/
protected function downloadMedium() : void
{
$mid = filter_input(INPUT_GET, 'mid', FILTER_VALIDATE_INT);
$video = xvmpMedium::find($mid);
ilObjViMPAccess::checkAction(ilObjViMPAccess::ACTION_DOWNLOAD_VIDEO, $this, $video);
xvmp::deliverMedium($video);
}
/**
* @throws ilCtrlException
*/
public function executeCommand() : void
{
if (!$this->dic->ctrl()->isAsynch()) {
$this->dic->tabs()->activateTab(static::TAB_ACTIVE);
}
$nextClass = $this->dic->ctrl()->getNextClass();
switch ($nextClass) {
default:
$cmd = $this->dic->ctrl()->getCmd(self::CMD_STANDARD);
$this->performCommand($cmd);
break;
}
}
/**
* @param $cmd
*/
protected function performCommand($cmd) : void
{
switch ($cmd) {
case self::CMD_FILL_MODAL:
if(isset($_GET['mid'])) {
$mid = $_GET['mid'];
$medium = xvmpMedium::find($mid);
ilObjViMPAccess::checkAction(ilObjViMPAccess::ACTION_PLAY_VIDEO, $this, $medium);
}
break;
}
$this->{$cmd}();
}
/**
*
*/
public function addFlushCacheButton() : void
{
$button = ilLinkButton::getInstance();
$button->setUrl($this->dic->ctrl()->getLinkTarget($this, self::CMD_FLUSH_CACHE));
$button->setCaption($this->pl->txt('flush_video_cache'), false);
$button->setId('xvmp_flush_video_cache');
$this->dic->toolbar()->addButtonInstance($button);
ilTooltipGUI::addTooltip('xvmp_flush_video_cache', $this->pl->txt('flush_video_cache_tooltip'));
}
/**
* @throws ilCtrlException
*/
public function flushCache() : void
{
// xvmpCacheFactory::getInstance()->flush();
foreach (xvmpSelectedMedia::getSelected($this->getObjId()) as $selected) {
xvmpCacheFactory::getInstance()->delete(xvmpMedium::class . '-' . $selected->getMid());
}
$this->dic->ctrl()->redirect($this, self::CMD_STANDARD);
}
/**
* called by ilObjViMPAccess
* @throws ilCtrlException
*/
public function accessDenied() : void
{
$this->dic->ui()->mainTemplate()->setOnScreenMessage('failure', $this->pl->txt('access_denied'), true);
$this->dic->ctrl()->redirect($this->parent_gui, ilObjViMPGUI::CMD_SHOW_CONTENT);
}
/**
* @param $video_mid
* @return ilModalGUI
* @throws ilTemplateException
* @throws xvmpException
*/
public function getFilledModalPlayer($video_mid) : ilModalGUI
{
$selected_medium = xvmpSelectedMedia::where(array('obj_id' => $this->getObjId(), 'mid' => $video_mid));
if (!ilObjViMPAccess::hasWriteAccess()) {
$selected_medium = $selected_medium->where(['visible' => 1]);
}
/** @var xvmpSelectedMedia $selected_medium */
$selected_medium = $selected_medium->first();
if (!$selected_medium) {
return $this->getAccessDeniedModal();
}
$this->dic->ui()->mainTemplate()->addCss($this->pl->getAssetURL('default/modal.css'));
$modal_content = $this->fillModalPlayer($video_mid, false);
/** @var xvmpSettings $settings */
$settings = xvmpSettings::find($this->getObjId());
if ($settings->getLpActive()) {
$this->dic->ui()->mainTemplate()->addOnLoadCode('VimpObserver.init(' . $video_mid . ', ' . json_encode($modal_content->time_ranges) . ');');
}
$modal = ilModalGUI::getInstance();
$modal->setId('xvmp_modal_player');
$modal->setHeading($modal_content->video_title);
$modal->setType(ilModalGUI::TYPE_LARGE);
$modal->setBody('<section><div id="xvmp_video_container">' .
$modal_content->html .
'</div></section>');
return $modal;
}
/**
* @return ilModalGUI
*/
protected function getAccessDeniedModal() : ilModalGUI
{
$modal = ilModalGUI::getInstance();
$modal->setId('xvmp_modal_player');
$modal->setType(ilModalGUI::TYPE_LARGE);
$modal->setBody($this->dic->ui()->renderer()->render($this->dic->ui()->factory()->messageBox()->failure($this->pl->txt('access_denied'))));
return $modal;
}
/**
* @param null $play_video_id
* @param bool $async
* @return stdClass
* @throws ilTemplateException
* @throws xvmpException
*/
public function fillModalPlayer($play_video_id = null, bool $async = true) : stdClass
{
$mid = $play_video_id ?? $_GET['mid'];
$video = xvmpMedium::find($mid);
$playModalDto = $this->buildPlayerContainerDTO($video);
$response = new stdClass();
// TODO: Abstract classes MUST NOT know their children. this is a cognitive overload
// Refactoring Issue: https://git.fluxlabs.ch/fluxlabs/ilias/plugins/RepositoryObjects/ViMP/-/issues/3
$show_unavailable = ($this instanceof xvmpVideosGUI) || ($this instanceof xvmpContentGUI);
$response->html = $this->renderer_factory->playerModal()->render($playModalDto, $async, $show_unavailable);
$response->video_title = $video->getTitle();
/** @var xvmpUserProgress $progress */
$progress = xvmpUserProgress::where(array(xvmpUserProgress::F_USR_ID => $this->dic->user()->getId(),
xvmpMedium::F_MID => $mid
))->first();
if ($progress) {
$response->time_ranges = json_decode($progress->getRanges());
} else {
$response->time_ranges = array();
}
if ($async) {
echo json_encode($response);
exit;
} else {
return $response;
}
}
/**
* @param xvmpMedium $medium
* @return PlayerContainerDTO
* @throws xvmpException|ilCtrlException
*/
public function buildPlayerContainerDTO(xvmpMedium $medium) : PlayerContainerDTO
{
$playerContainerDTO = new PlayerContainerDTO(
$this->getVideoPlayer($medium, $this->getObjId()),
$this->metadata_builder->buildFromVimpMedium($medium, false, false)
);
$buttons = [];
if (!is_null($this->getObject())) {
$buttons[] = $this->buildPermLinkUI($medium);
}
if ($medium->isDownloadAllowed()) {
$this->dic->ctrl()->setParameter($this, 'mid', $medium->getMid());
$buttons[] = $this->dic->ui()->factory()->button()->standard(
$this->pl->txt('btn_download'),
$this->dic->ctrl()->getLinkTarget($this, self::CMD_DOWNLOAD_MEDIUM)
);
}
if (!empty($buttons)) {
$playerContainerDTO = $playerContainerDTO->withButtons($buttons);
}
return $playerContainerDTO;
}
/**
* @throws xvmpException
*/
protected function getVideoPlayer($video, int $obj_id) : VideoPlayer
{
return (new VideoPlayer($video, xvmp::isUseEmbeddedPlayer($obj_id, $video), false));
}
/**
* @return int
*/
public function getObjId() : int
{
$obj = $this->parent_gui->getObject();
return $obj ? $obj->getId() : 0;
}
/**
* @return ?ilObject
*/
public function getObject() : ?ilObject
{
return $this->parent_gui->getObject();
}
/**
* @param xvmpMedium $video
* @return ILIAS\UI\Component\Component[]
*/
public function buildPermLinkUI(xvmpMedium $video) : array
{
$items = [];
$link_tpl = $this->dic['static_url']->builder()->build(
$this->parent_gui->getType(),
new ReferenceId($this->parent_gui->getRefId()),
[$video->getMid() , '0']
);
$popover = $this->dic->ui()->factory()->popover()->standard(
$this->dic->ui()->factory()->legacy($this->pl->txt('popover_link_copied'))
);
if (!xvmpConf::getConfig(xvmpConf::F_EMBED_PLAYER)) {
$items[] = $this->dic->ui()->factory()->button()->shy(
$this->pl->txt('btn_copy_link_w_time'),
''
)->withOnClick($popover->getShowSignal())->withOnLoadCode(function ($id) use ($link_tpl) {
return "document.getElementById('$id').addEventListener('click', () => VimpContent.copyDirectLinkWithTime('$link_tpl'));";
});
}
return [
$popover,
$this->dic->ui()->factory()->legacy('
<div class="ilPermalinkContainer input-group">
<input class="form-control" id="current_perma_link" type="text" value="' . $link_tpl . '" readonly="readonly" onclick="this.focus();this.select();return false;" />
<span class="input-group-btn">'),
$this->dic->ui()->factory()->dropdown()->standard($items)->withLabel(''),
$this->dic->ui()->factory()->legacy('</span></div>'),
];
}
/**
* ajax
* @throws xvmpException
*/
public function updateProgress()
{
$mid = (int) $_POST[xvmpMedium::F_MID];
$ranges = $_POST[xvmpUserProgress::F_RANGES];
xvmpUserProgress::storeProgress($this->dic->user()->getid(), $mid, $ranges);
echo "ok";
exit;
}
}