ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.VideoViewGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
25 
30 {
31  protected \ilCtrlInterface $ctrl;
32  protected \ILIAS\MediaCast\MediaCastManager $mc_manager;
33  protected string $rss_link;
34  protected \ilToolbarGUI $toolbar;
35  protected \ilGlobalTemplateInterface $main_tpl;
36  protected \ilObjMediaCast $media_cast;
37  protected \ilGlobalTemplateInterface $tpl;
38  protected \ILIAS\DI\UIServices $ui;
39  protected \ilLanguage $lng;
40  protected \ilObjUser $user;
41  protected string $completed_callback = "";
42  protected string $autoplay_callback = "";
44  protected string $video_wrapper_id = "mcst_video";
45 
46  public function __construct(
47  \ilObjMediaCast $obj,
48  \ilGlobalTemplateInterface $tpl = null,
49  string $rss_link = ""
50  ) {
51  global $DIC;
52 
53  $this->ui = $DIC->ui();
54  $this->rss_link = $rss_link;
55  $this->lng = $DIC->language();
56  $this->media_cast = $obj;
57  $this->tpl = $tpl;
58  $this->video_sequence = new VideoSequence($this->media_cast);
59  $this->user = $DIC->user();
60  $this->main_tpl = $DIC->ui()->mainTemplate();
61  $this->toolbar = $DIC->toolbar();
62  $this->ctrl = $DIC->ctrl();
63  $this->mc_manager = $DIC->mediaCast()->internal()->domain()->mediaCast(
64  $this->media_cast
65  );
66  }
67 
68  public function setCompletedCallback(string $completed_callback): void
69  {
70  $this->completed_callback = $completed_callback;
71  }
72 
73  public function setAutoplayCallback(string $autoplay_callback): void
74  {
75  $this->autoplay_callback = $autoplay_callback;
76  }
77 
81  public function renderMainColumn(): string
82  {
83  if (count($this->video_sequence->getVideos()) == 0) {
84  return "";
85  }
86 
87  $widget = new VideoWidgetGUI($this->tpl, "mcst_video");
88  $widget->setVideo($this->video_sequence->getFirst());
89  return $widget->render();
90  }
91 
92  public function renderToolbar(): void
93  {
94  $toolbar = $this->toolbar;
95  $lng = $this->lng;
96 
97  $video_cnt = count($this->video_sequence->getVideos());
98 
99  $mcst_settings = \ilMediaCastSettings::_getInstance();
100 
101  $autoplay = $this->getAutoplay();
102 
103  $factory = $this->ui->factory();
104  $renderer = $this->ui->renderer();
105 
106  if ($video_cnt > 1) {
107  $back = $factory->button()->standard(
108  "<span class=\"glyphicon glyphicon-chevron-left \" aria-hidden=\"true\"></span>",
109  ""
110  )
111  ->withOnLoadCode(function ($id) {
112  return
113  "$(\"#$id\").click(function() { il.VideoWidget.previous(\"" . $this->video_wrapper_id . "\"); return false;});";
114  });
115  $next = $factory->button()->standard(
116  "<span class=\"glyphicon glyphicon-chevron-right \" aria-hidden=\"true\"></span>",
117  ""
118  )
119  ->withOnLoadCode(function ($id) {
120  return
121  "$(\"#$id\").click(function() { il.VideoWidget.next(\"" . $this->video_wrapper_id . "\"); return false;});";
122  });
123 
124  $toolbar->addStickyItem($back);
125 
126  $dd = $this->getDropdown();
127  if (!is_null($dd)) {
128  $toolbar->addStickyItem($dd);
129  }
130 
131  $toolbar->addStickyItem($next);
132  }
133 
134  // autoplay
135  if ($this->media_cast->getAutoplayMode() !== \ilObjMediaCast::AUTOPLAY_NO && $video_cnt > 1) {
136  $toolbar->addSeparator();
137  $s = new SignalGenerator();
138  $autoplay_on = $s->create();
139  $autoplay_off = $s->create();
140  $button = $factory->button()->toggle($lng->txt("mcst_autoplay"), $autoplay_on, $autoplay_off, $autoplay);
141  $toolbar->addStickyItem($button);
142  $this->main_tpl->addOnLoadCode("
143  $(document).on('" . $autoplay_on . "', function (event, signalData) {
144  il.VideoPlaylist.autoplay('mcst_playlist', true);
145  });
146  $(document).on('" . $autoplay_off . "', function (event, signalData) {
147  il.VideoPlaylist.autoplay('mcst_playlist', false);
148  });");
149  }
150 
151  if ($video_cnt > 0 && $this->rss_link !== "") {
152  $f = $this->ui->factory();
153  $actions = [
154  $f->link()->standard(
155  $lng->txt("mcst_webfeed"),
157  )->withOpenInNewViewport(true)
158  ];
159  $toolbar->addComponent($f->dropdown()->standard($actions));
160  }
161  }
162 
163  protected function getAutoplay(): bool
164  {
165  $video_cnt = count($this->video_sequence->getVideos());
166  if ($video_cnt <= 1) {
167  return false;
168  }
169  $autoplay = ($this->user->existsPref("mcst_autoplay"))
170  ? (bool) $this->user->getPref("mcst_autoplay")
171  : ($this->media_cast->getAutoplayMode() == \ilObjMediaCast::AUTOPLAY_ACT);
172  if ($this->media_cast->getAutoplayMode() == \ilObjMediaCast::AUTOPLAY_NO) {
173  $autoplay = false;
174  }
175  return $autoplay;
176  }
177 
178  protected function getDropdown(): ?\ILIAS\UI\Component\Dropdown\Standard
179  {
180 
181  $actions = [];
182  foreach ($this->video_sequence->getVideos() as $video) {
183  $actions[] = $this->ui->factory()->button()->shy(
184  $video->getTitle(),
185  "#"
186  )->withOnLoadCode(function (string $id) use ($video) {
187  return "document.getElementById('$id').addEventListener('click', () => {il.VideoPlaylist.toggleItem('mcst_playlist', '" . $video->getId() . "'); $('.ilToolbarStickyItem .dropdown.open').removeClass('open');});";
188  });
189  }
190  if (count($actions) > 0) {
191  return $this->ui->factory()->dropdown()->standard($actions);
192  }
193  return null;
194  }
195 
196  public function renderSideColumn(): string
197  {
198  $mcst_settings = \ilMediaCastSettings::_getInstance();
199 
200  $autoplay = $this->getAutoplay();
201 
202  $lng = $this->lng;
203  $tpl = new \ilTemplate("tpl.video_cast_side.html", true, true, "Modules/MediaCast/Presentation");
204 
205  $factory = $this->ui->factory();
206  $renderer = $this->ui->renderer();
207 
208  // items
209  $items = [];
210  $has_items = false;
211 
212  $panel_items = [];
213 
214  foreach ($this->video_sequence->getVideos() as $video) {
215  $has_items = true;
216  $preview = new VideoPreviewGUI(
217  $video->getPreviewPic(),
218  "il.VideoPlaylist.toggleItem('mcst_playlist', '" . $video->getId() . "');",
219  $video->getPlayingTime()
220  );
221  $completed = false;
222 
223  $re = \ilChangeEvent::_lookupReadEvents($video->getId(), $this->user->getId());
224  if (count($re) > 0) {
225  if ($re[0]["read_count"] > 0) {
226  $completed = true;
227  }
228  }
229 
230  $b = $factory->button()->shy($video->getTitle(), "")->withOnLoadCode(function ($id) use ($video) {
231  return
232  "$(\"#$id\").click(function() { il.VideoPlaylist.toggleItem('mcst_playlist', '" . $video->getId() . "'); return false;});";
233  });
234 
235  $items[] = [
236  "id" => $video->getId(),
237  "resource" => $video->getResource(),
238  "preview" => $preview->render(),
239  "preview_pic" => $video->getPreviewPic(),
240  "title" => $video->getTitle(),
241  "linked_title" => $renderer->renderAsync($b),
242  "mime" => $video->getMime(),
243  "poster" => $video->getPreviewPic(),
244  "description" => nl2br($video->getDescription()),
245  "completed" => $completed,
246  "duration" => $video->getDuration()
247  ];
248  }
249 
250  $panel = $factory->panel()->secondary()->listing(
251  "Videos",
252  []
253  );
254  $panel_html = $renderer->render($panel);
255  $panel_html = str_replace(
256  '<div class="panel-body">',
257  '<div class="panel-body"><div id="mcst_playlist"></div>',
258  $panel_html,
259  );
260 
261  $tpl->setVariable("PANEL", $panel_html);
262  // previous items / next items links
263  if ($has_items) {
264  $tpl->setVariable(
265  "PREV",
266  $renderer->render(
267  $factory->button()->standard($lng->txt("mcst_prev_items"), "")->withOnLoadCode(
268  function ($id) {
269  return
270  "$(\"#$id\").click(function() { il.VideoPlaylist.previousItems('mcst_playlist'); return false;});";
271  }
272  )
273  )
274  );
275  $tpl->setVariable(
276  "NEXT",
277  $renderer->render(
278  $factory->button()->standard($lng->txt("mcst_next_items"), "")->withOnLoadCode(
279  function ($id) {
280  return
281  "$(\"#$id\").click(function() { il.VideoPlaylist.nextItems('mcst_playlist'); return false;});";
282  }
283  )
284  )
285  );
286 
287  $item_tpl = new \ilTemplate("tpl.playlist_item.html", true, true, "Modules/MediaCast/Video");
288  $item_tpl->setVariable("TITLE", " ");
289  $item_content = str_replace(["\n", "\r"], "", $item_tpl->get());
290 
291  $item = $factory->item()->standard("#video-title#")
292  ->withLeadImage(
293  $factory->image()->responsive("#img-src#", "#img-alt#")
294  );
295 
296  $item_content = $renderer->render($item);
297  $item_content = str_replace(["\n", "\r"], "", $item_content);
298 
299  $init_videos = $this->media_cast->getNumberInitialVideos() > 0
300  ? $this->media_cast->getNumberInitialVideos()
301  : 1;
302 
303  $this->tpl->addOnLoadCode(
304  "il.VideoPlaylist.init('mcst_playlist', 'mcst_video', " . json_encode(
305  $items
306  ) . ", '$item_content', " . ($autoplay ? "true" : "false") . ", " .
307  (int) $init_videos . ", '" . $this->completed_callback . "', '" . $this->autoplay_callback . "', " . ((int) $mcst_settings->getVideoCompletionThreshold()) . ");"
308  );
309 
310  if (count($items) === 1) {
311  return " ";
312  }
313 
314  return $tpl->get();
315  }
316 
317  return "";
318  }
319 
323  public function render(): string
324  {
325  // this is current only to include the resize mechanism when
326  // the main menu is changed, so that the player is resized, too
327  //\ilMediaPlayerGUI::initJavascript();
328  $tpl = new \ilTemplate("tpl.video_cast_layout.html", true, true, "Modules/MediaCast/Presentation");
329  $side_column = $this->renderSideColumn();
330 
331 
332  if ($side_column != "") {
333  $tpl->setCurrentBlock("with_side_column");
334  $tpl->setVariable("SIDE", $side_column);
335  } else {
336  $tpl->setCurrentBlock("video_only");
337  }
338  $tpl->setVariable("MAIN", $this->renderMainColumn());
339  $tpl->parseCurrentBlock();
340  return $tpl->get();
341  }
342 
343  public function show(): void
344  {
345  if (is_object($this->tpl)) {
346  $this->renderToolbar();
347  $this->tpl->setContent(
348  $this->renderMainColumn() .
349  $this->renderCommentsContainer()
350  );
351  $this->tpl->setRightContent($this->renderSideColumn());
352  }
353  }
354 
355  public function renderCommentsContainer()
356  {
357  if ($this->mc_manager->commentsActive()) {
358  $target = $this->ctrl->getLinkTargetByClass(
359  \ilObjMediaCastGUI::class,
360  "showComments"
361  );
362  return "<div data-mcst-comments='$target'></div>";
363  }
364  }
365 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS MediaCast MediaCastManager $mc_manager
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupReadEvents($obj_id, $usr_id=null)
Reads all read events which occured on the object.
setCompletedCallback(string $completed_callback)
setAutoplayCallback(string $autoplay_callback)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(\ilObjMediaCast $obj, \ilGlobalTemplateInterface $tpl=null, string $rss_link="")