ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilPCMediaObject.php
Go to the documentation of this file.
1<?php
2
20
27{
31 protected ilObjUser $user;
33 protected \ILIAS\DI\UIServices $ui;
34 protected ?ilObjMediaObject $mediaobject = null;
35 protected ilLanguage $lng;
37 protected static string $modal_show_signal = "";
38 protected static string $modal_suffix = "";
39
40 public function init(): void
41 {
42 global $DIC;
43
44 $this->user = $DIC->user();
45 $this->setType("media");
46 $this->ui = $DIC->ui();
47 $this->lng = $DIC->language();
48 $this->global_tpl = $DIC['tpl'];
49 $this->mob_usage_repo = $DIC->mediaObjects()
50 ->internal()
51 ->repo()
52 ->usage();
53 $this->ctrl = $DIC->ctrl();
54 }
55
56 public function readMediaObject(int $a_mob_id = 0): void
57 {
58 if ($a_mob_id > 0) {
59 $mob = new ilObjMediaObject($a_mob_id);
60 $this->setMediaObject($mob);
61 }
62 }
63
64 public function setNode(php4DOMElement $a_node): void
65 {
66 parent::setNode($a_node); // this is the PageContent node
67 $this->mob_node = $a_node->first_child();
68 }
69
70 public function setDom(php4DOMDocument $a_dom): void
71 {
72 $this->dom = $a_dom;
73 }
74
75 public function setHierId(string $a_hier_id): void
76 {
77 $this->hier_id = $a_hier_id;
78 }
79
80 public function setMediaObject(ilObjMediaObject $a_mediaobject): void
81 {
82 $this->mediaobject = $a_mediaobject;
83 }
84
85 public function getMediaObject(): ?ilObjMediaObject
86 {
87 return $this->mediaobject;
88 }
89
90 public function createMediaObject(): void
91 {
92 $this->setMediaObject(new ilObjMediaObject());
93 }
94
95 public function create(): void
96 {
97 $this->node = $this->createPageContentNode();
98 }
99
100 public function createAlias(
101 ilPageObject $a_pg_obj,
102 string $a_hier_id,
103 string $a_pc_id = ""
104 ): void {
105 $this->node = $this->dom->create_element("PageContent");
106 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
107 $this->mob_node = $this->dom->create_element("MediaObject");
108 $this->mob_node = $this->node->append_child($this->mob_node);
109 $this->mal_node = $this->dom->create_element("MediaAlias");
110 $this->mal_node = $this->mob_node->append_child($this->mal_node);
111 $this->mal_node->set_attribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
112
113 // standard view
114 $item_node = $this->dom->create_element("MediaAliasItem");
115 $item_node = $this->mob_node->append_child($item_node);
116 $item_node->set_attribute("Purpose", "Standard");
117 $media_item = $this->getMediaObject()->getMediaItem("Standard");
118 if (is_null($media_item)) {
119 return;
120 }
121
122 $layout_node = $this->dom->create_element("Layout");
123 $layout_node = $item_node->append_child($layout_node);
124 if ($media_item->getWidth() > 0) {
125 //$layout_node->set_attribute("Width", $media_item->getWidth());
126 }
127 if ($media_item->getHeight() > 0) {
128 //$layout_node->set_attribute("Height", $media_item->getHeight());
129 }
130 $layout_node->set_attribute("HorizontalAlign", "Left");
131
132 // caption
133 if ($media_item->getCaption() != "") {
134 $cap_node = $this->dom->create_element("Caption");
135 $cap_node = $item_node->append_child($cap_node);
136 $cap_node->set_attribute("Align", "bottom");
137 $cap_node->set_content($media_item->getCaption());
138 }
139
140 // text representation
141 if ($media_item->getTextRepresentation() != "") {
142 $tr_node = $this->dom->create_element("TextRepresentation");
143 $tr_node = $item_node->append_child($tr_node);
144 $tr_node->set_content($media_item->getTextRepresentation());
145 }
146
147 $pars = $media_item->getParameters();
148 foreach ($pars as $par => $val) {
149 $par_node = $this->dom->create_element("Parameter");
150 $par_node = $item_node->append_child($par_node);
151 $par_node->set_attribute("Name", $par);
152 $par_node->set_attribute("Value", $val);
153 }
154
155 // fullscreen view
156 $fullscreen_item = $this->getMediaObject()->getMediaItem("Fullscreen");
157 if (is_object($fullscreen_item)) {
158 $item_node = $this->dom->create_element("MediaAliasItem");
159 $item_node = $this->mob_node->append_child($item_node);
160 $item_node->set_attribute("Purpose", "Fullscreen");
161
162 // width and height
163 $layout_node = $this->dom->create_element("Layout");
164 $layout_node = $item_node->append_child($layout_node);
165 if ($fullscreen_item->getWidth() > 0) {
166 $layout_node->set_attribute("Width", $fullscreen_item->getWidth());
167 }
168 if ($fullscreen_item->getHeight() > 0) {
169 $layout_node->set_attribute("Height", $fullscreen_item->getHeight());
170 }
171
172 // caption
173 if ($fullscreen_item->getCaption() != "") {
174 $cap_node = $this->dom->create_element("Caption");
175 $cap_node = $item_node->append_child($cap_node);
176 $cap_node->set_attribute("Align", "bottom");
177 $cap_node->set_content($fullscreen_item->getCaption());
178 }
179
180 // text representation
181 if ($fullscreen_item->getTextRepresentation() != "") {
182 $tr_node = $this->dom->create_element("TextRepresentation");
183 $tr_node = $item_node->append_child($tr_node);
184 $tr_node->set_content($fullscreen_item->getTextRepresentation());
185 }
186
187 $pars = $fullscreen_item->getParameters();
188 foreach ($pars as $par => $val) {
189 $par_node = $this->dom->create_element("Parameter");
190 $par_node = $item_node->append_child($par_node);
191 $par_node->set_attribute("Name", $par);
192 $par_node->set_attribute("Value", $val);
193 }
194 }
195 }
196
202 public function updateObjectReference(): void
203 {
204 if (is_object($this->mob_node)) {
205 $this->mal_node = $this->mob_node->first_child();
206 if (is_object($this->mal_node) && $this->mal_node->node_name() == "MediaAlias") {
207 $this->mal_node->set_attribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
208 }
209 }
210 }
211
212 public function dumpXML(): string
213 {
214 $xml = $this->dom->dump_node($this->node);
215 return $xml;
216 }
217
218 public function setClass(string $a_class): void
219 {
220 if (is_object($this->mob_node)) {
221 $mal_node = $this->mob_node->first_child();
222 if (is_object($mal_node)) {
223 if (!empty($a_class)) {
224 $mal_node->set_attribute("Class", $a_class);
225 } else {
226 if ($mal_node->has_attribute("Class")) {
227 $mal_node->remove_attribute("Class");
228 }
229 }
230 }
231 }
232 }
233
237 public function getClass(): string
238 {
239 if (is_object($this->mob_node)) {
240 $mal_node = $this->mob_node->first_child();
241 if (is_object($mal_node)) {
242 $class = $mal_node->get_attribute("Class");
243 return $class;
244 }
245 }
246 return "";
247 }
248
252 public function setCaptionClass(string $a_class): void
253 {
254 if (is_object($this->mob_node)) {
255 $mal_node = $this->mob_node->first_child();
256 if (is_object($mal_node)) {
257 if (!empty($a_class)) {
258 $mal_node->set_attribute("CaptionClass", $a_class);
259 } else {
260 if ($mal_node->has_attribute("CaptionClass")) {
261 $mal_node->remove_attribute("CaptionClass");
262 }
263 }
264 }
265 }
266 }
267
268 public function getCaptionClass(): string
269 {
270 if (is_object($this->mob_node)) {
271 $mal_node = $this->mob_node->first_child();
272 if (is_object($mal_node)) {
273 $class = $mal_node->get_attribute("CaptionClass");
274 return $class;
275 }
276 }
277 return "";
278 }
279
280 public static function getLangVars(): array
281 {
282 return array("pc_mob");
283 }
284
292 public static function afterPageUpdate(
293 ilPageObject $a_page,
294 DOMDocument $a_domdoc,
295 string $a_xml,
296 bool $a_creation
297 ): void {
298 if (!$a_page->getImportMode()) {
300 $a_page->getParentType() . ":pg",
301 $a_page->getId(),
302 0,
303 $a_page->getLanguage()
304 );
305 self::saveMobUsage($a_page, $a_domdoc);
306 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
307 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
308 $mob_obj = new ilObjMediaObject($mob);
309 $usages = $mob_obj->getUsages(false);
310 if (count($usages) == 0) { // delete, if no usage exists
311 $mob_obj->delete();
312 }
313 }
314 }
315 }
316 }
317
318 public static function beforePageDelete(
319 ilPageObject $a_page
320 ): void {
321 $mob_ids = ilObjMediaObject::_getMobsOfObject(
322 $a_page->getParentType() . ":pg",
323 $a_page->getId(),
324 0,
325 $a_page->getLanguage()
326 );
327
329 $a_page->getParentType() . ":pg",
330 $a_page->getId(),
331 null,
332 $a_page->getLanguage()
333 );
334
335 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
336 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
337 $mob_obj = new ilObjMediaObject($mob);
338 $usages = $mob_obj->getUsages(false);
339 if (count($usages) == 0) { // delete, if no usage exists
340 $mob_obj->delete();
341 }
342 }
343 }
344 }
345
353 public static function afterPageHistoryEntry(
354 ilPageObject $a_page,
355 DOMDocument $a_old_domdoc,
356 string $a_old_xml,
357 int $a_old_nr
358 ): void {
359 self::saveMobUsage($a_page, $a_old_domdoc, $a_old_nr);
360 }
361
362 public static function saveMobUsage(
363 ilPageObject $a_page,
364 DOMDocument $a_domdoc,
365 int $a_old_nr = 0
366 ): array {
367 $log = ilLoggerFactory::getLogger("copg");
368
369 $usages = array();
370 // media aliases
371 $xpath = new DOMXPath($a_domdoc);
372 $nodes = $xpath->query('//MediaAlias');
373 foreach ($nodes as $node) {
374 $id_arr = explode("_", $node->getAttribute("OriginId"));
375 $mob_id = $id_arr[count($id_arr) - 1];
376 if ($mob_id > 0 && $id_arr[1] == "") {
377 $usages[$mob_id] = true;
378 }
379 }
380
381 // media objects
382 $xpath = new DOMXPath($a_domdoc);
383 $nodes = $xpath->query('//MediaObject/MetaData/General/Identifier');
384 foreach ($nodes as $node) {
385 $mob_entry = $node->getAttribute("Entry");
386 $mob_arr = explode("_", $mob_entry);
387 $mob_id = $mob_arr[count($mob_arr) - 1];
388 if ($mob_id > 0 && $mob_arr[1] == "") {
389 $usages[$mob_id] = true;
390 }
391 }
392
393 // internal links
394 $xpath = new DOMXPath($a_domdoc);
395 $nodes = $xpath->query("//IntLink[@Type='MediaObject']");
396 foreach ($nodes as $node) {
397 $mob_target = $node->getAttribute("Target");
398 $mob_arr = explode("_", $mob_target);
399 //echo "<br>3<br>";
400 //echo $mob_target."<br>";
401 //var_dump($mob_arr);
402 $mob_id = $mob_arr[count($mob_arr) - 1];
403 if ($mob_id > 0 && $mob_arr[1] == "") {
404 $usages[$mob_id] = true;
405 }
406 }
407
409 $a_page->getParentType() . ":pg",
410 $a_page->getId(),
411 $a_old_nr,
412 $a_page->getLanguage()
413 );
414 $log->debug("Deleted all mob usages page id: " . $a_page->getId() . ", lang" . $a_page->getLanguage() . ", old nr: " . $a_old_nr);
415 foreach ($usages as $mob_id => $val) {
416 // save usage, if object exists...
417 if (ilObject::_lookupType($mob_id) == "mob") {
418 $log->debug("Save usage mob id: " . $mob_id . ", old nr: " . $a_old_nr);
420 $mob_id,
421 $a_page->getParentType() . ":pg",
422 $a_page->getId(),
423 $a_old_nr,
424 $a_page->getLanguage()
425 );
426 }
427 }
428
429 return $usages;
430 }
431
433 string $a_output,
434 string $a_mode,
435 bool $a_abstract_only = false
436 ): string {
437 $ilUser = $this->user;
438
439 if ($a_mode == "offline") {
440 $page = $this->getPage();
441
443 $page->getParentType() . ":pg",
444 $page->getId(),
445 0,
446 $page->getLanguage()
447 );
448 foreach ($mob_ids as $mob_id) {
449 $mob = new ilObjMediaObject($mob_id);
450 $srts = $mob->getSrtFiles();
451 foreach ($srts as $srt) {
452 if ($ilUser->getLanguage() == $srt["language"]) {
453 $srt_content = file_get_contents(ilObjMediaObject::_getDirectory($mob->getId()) . "/" . $srt["full_path"]);
454 $a_output = str_replace("[[[[[mobsubtitle;il__mob_" . $mob->getId() . "_Standard]]]]]", $srt_content, $a_output);
455 }
456 }
457 }
458 }
459
460 if ($a_abstract_only) {
461 return $a_output;
462 }
463
464 if ($a_mode === "edit") {
465 $a_output = str_replace(
466 "{{{{{Unsupported Media Type}}}}}",
467 $this->ui->renderer()->render(
468 $this->ui->factory()->messageBox()->info(
469 $this->lng->txt("copg_unsupported_media_type")
470 )
471 ),
472 $a_output
473 );
474 } else {
475 $a_output = str_replace(
476 "{{{{{Unsupported Media Type}}}}}",
477 "",
478 $a_output
479 );
480 }
481
482 // add fullscreen modals
483 $page = $this->getPage();
484 $suffix = "-" . $page->getParentType() . "-" . $page->getId();
485 $modal = $this->ui->factory()->modal()->roundtrip(
486 $this->lng->txt("cont_fullscreen"),
487 $this->ui->factory()->legacy("<iframe class='il-copg-mob-fullscreen' id='il-copg-mob-fullscreen" . $suffix . "'></iframe>")
488 );
489 $show_signal = $modal->getShowSignal();
490
491 $js = "
492 $(function () {
493 il.COPagePres.setFullscreenModalShowSignal('$show_signal', '$suffix');
494 });
495 ";
496 self::$modal_show_signal = $show_signal;
497 self::$modal_suffix = $suffix;
498 $this->global_tpl->addOnloadCode($js);
499
500 // async ensures to have onloadcode of modal in output
501 // if other main tpl is used, see #32198
502 // note: if always rendered async, $ not defined errors will be thrown in non-async cases
503 if ($this->ctrl->isAsynch()) {
504 $html = $this->ui->renderer()->renderAsync($modal);
505 } else {
506 $html = $this->ui->renderer()->render($modal);
507 }
508 return $a_output . "<div class='il-copg-mob-fullscreen-modal'>" . $html . "</div>";
509 }
510
511 public function getOnloadCode(string $a_mode): array
512 {
513 $onload_code = [];
514 // necessary due to 32198 (other main template used)
515 if (self::$modal_show_signal !== "") {
516 $onload_code[] = "il.COPagePres.setFullscreenModalShowSignal('" . self::$modal_show_signal .
517 "', '" . self::$modal_suffix . "');";
518 }
519 return $onload_code;
520 }
521
522 public function getJavascriptFiles(
523 string $a_mode
524 ): array {
525 $js_files = ilPlayerUtil::getJsFilePaths();
527 return $js_files;
528 }
529
530 public function getCssFiles(
531 string $a_mode
532 ): array {
533 $js_files = ilPlayerUtil::getCssFilePaths();
534
535 return $js_files;
536 }
537
539 {
540 $std_alias_item = new ilMediaAliasItem(
541 $this->dom,
542 $this->getHierId(),
543 "Standard",
544 $this->getPCId()
545 );
546 return $std_alias_item;
547 }
548
550 {
551 $std_alias_item = new ilMediaAliasItem(
552 $this->dom,
553 $this->getHierId(),
554 "Fullscreen",
555 $this->getPCId()
556 );
557 return $std_alias_item;
558 }
559
560 public function checkInstanceEditing(): bool
561 {
562 // if any properties are set on the instance,
563 // that are not accessible through the quick editing screen
564 // -> offer instance editing
565 $std_alias_item = $this->getStandardMediaAliasItem();
566 if ($std_alias_item->hasAnyPropertiesSet()) {
567 return true;
568 }
569 if ($this->getMediaObject()->hasFullscreenItem()) {
570 $full_alias_item = $this->getFullscreenMediaAliasItem();
571 if ($full_alias_item->hasAnyPropertiesSet()) {
572 return true;
573 }
574 }
575
576 // if the media object has any other use outside of the current page
577 // -> offer instance editing
579 $mob = $this->getMediaObject();
580 $page = $this->getPage();
581 if (is_object($mob)) {
582 $usages = $mob->getUsages();
583 $other_usages = array_filter($usages, function ($usage) use ($page) {
584 return ($usage["type"] != $page->getParentType() . ":pg" || $usage["id"] != $page->getId());
585 });
586 if (count($other_usages) > 0) {
587 return true;
588 }
589 }
590 // see https://mantis.ilias.de/view.php?id=38582
591 // we allow instance editing regardless of number of usages
592 return true;
593 }
594
595 public static function deleteHistoryLowerEqualThan(
596 string $parent_type,
597 int $page_id,
598 string $lang,
599 int $delete_lower_than_nr
600 ): void {
601 global $DIC;
602
603 $mob_usage_repo = $DIC->mediaObjects()
604 ->internal()
605 ->repo()
606 ->usage();
607
609
610 $mob_ids = $mob_usage_repo->getHistoryUsagesLowerEqualThan(
611 $parent_type . ":pg",
612 $page_id,
613 $delete_lower_than_nr,
614 $lang
615 );
616
617 $mob_usage_repo->deleteHistoryUsagesLowerEqualThan(
618 $parent_type . ":pg",
619 $page_id,
620 $delete_lower_than_nr,
621 $lang
622 );
623
624 foreach ($mob_ids as $mob_id) {
625 $usages = ilObjMediaObject::lookupUsages($mob_id, true);
626 $log->debug("...check deletion of mob $mob_id. Usages: " . count($usages));
627 if (count($usages) == 0) {
628 if (ilObject::_lookupType($mob_id) === "mob") {
629 $mob = new ilObjMediaObject($mob_id);
630 $log->debug("Deleting Mob ID: " . $mob_id);
631 $mob->delete();
632 }
633 }
634 }
635 }
636}
deleteHistoryUsagesLowerEqualThan(string $a_type, int $a_id, int $a_usage_hist_nr, string $a_lang="-")
getHistoryUsagesLowerEqualThan(string $a_type, int $a_id, int $a_usage_hist_nr, string $a_lang="-")
const IL_INSERT_AFTER
language handling
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static lookupUsages(int $a_id, bool $a_include_history=true)
Lookup usages of media object.
static _getDirectory(int $a_mob_id)
Get absolute directory.
static _deleteAllUsages(string $a_type, int $a_id, ?int $a_usage_hist_nr=0, string $a_lang="-")
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
User class.
static _lookupType(int $id, bool $reference=false)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
Class ilPCMediaObject Media content object (see ILIAS DTD)
setDom(php4DOMDocument $a_dom)
readMediaObject(int $a_mob_id=0)
static deleteHistoryLowerEqualThan(string $parent_type, int $page_id, string $lang, int $delete_lower_than_nr)
Overwrite in derived classes, if old history entries are being deleted.
static saveMobUsage(ilPageObject $a_page, DOMDocument $a_domdoc, int $a_old_nr=0)
ILIAS DI UIServices $ui
updateObjectReference()
Updates the media object referenced by the media alias.
getCssFiles(string $a_mode)
setHierId(string $a_hier_id)
getOnloadCode(string $a_mode)
UsageDBRepository $mob_usage_repo
getClass()
Get characteristic of section.
static beforePageDelete(ilPageObject $a_page)
Before page is being deleted.
ilCtrlInterface $ctrl
createAlias(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="")
setNode(php4DOMElement $a_node)
Set xml node of page content.
static afterPageHistoryEntry(ilPageObject $a_page, DOMDocument $a_old_domdoc, string $a_old_xml, int $a_old_nr)
After page history entry has been created.
php4DOMElement $mob_node
getJavascriptFiles(string $a_mode)
modifyPageContentPostXsl(string $a_output, string $a_mode, bool $a_abstract_only=false)
Modify page content after xsl.
static string $modal_suffix
setClass(string $a_class)
setCaptionClass(string $a_class)
Set caption style class of media object.
static getLangVars()
Get lang vars needed for editing.
setMediaObject(ilObjMediaObject $a_mediaobject)
static string $modal_show_signal
php4DOMElement $mal_node
static afterPageUpdate(ilPageObject $a_page, DOMDocument $a_domdoc, string $a_xml, bool $a_creation)
After page has been updated (or created)
ilObjMediaObject $mediaobject
ilGlobalPageTemplate $global_tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setType(string $a_type)
Set Type.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
insertContent(ilPageContent $a_cont_obj, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="", bool $remove_placeholder=true)
insert a content node before/after a sibling or as first child of a parent
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLocalMaphilightPath()
Get local path of maphilight file.
set_attribute($name, $value)
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$xml
Definition: metadata.php:351
$log
Definition: result.php:33
$lang
Definition: xapiexit.php:26