ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPCMediaObject.php
Go to the documentation of this file.
1<?php
2
20
27{
30 protected DOMNode $mal_node;
31 protected ilObjUser $user;
32 protected DOMNode $mob_node;
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->mob_usage_repo = $DIC->mediaObjects()
49 ->internal()
50 ->repo()
51 ->usage();
52 $this->ctrl = $DIC->ctrl();
53 }
54
55 public function readMediaObject(int $a_mob_id = 0): void
56 {
57 if ($a_mob_id > 0) {
58 $mob = new ilObjMediaObject($a_mob_id);
59 $this->setMediaObject($mob);
60 }
61 }
62
63 public function setHierId(string $a_hier_id): void
64 {
65 $this->hier_id = $a_hier_id;
66 }
67
68 public function setMediaObject(ilObjMediaObject $a_mediaobject): void
69 {
70 $this->mediaobject = $a_mediaobject;
71 }
72
73 public function getMediaObject(): ?ilObjMediaObject
74 {
75 return $this->mediaobject;
76 }
77
78 public function createMediaObject(): void
79 {
80 $this->setMediaObject(new ilObjMediaObject());
81 }
82
83 public function create(): void
84 {
85 $this->createPageContentNode();
86 }
87
88 public function createAlias(
89 ilPageObject $a_pg_obj,
90 string $a_hier_id,
91 string $a_pc_id = ""
92 ): void {
93 $this->setDomNode($this->dom_doc->createElement("PageContent"));
94 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
95 $mob_node = $this->dom_doc->createElement("MediaObject");
96 $mob_node = $this->getDomNode()->appendChild($mob_node);
97 $this->mal_node = $this->dom_doc->createElement("MediaAlias");
98 $this->mal_node = $mob_node->appendChild($this->mal_node);
99 $this->mal_node->setAttribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
100
101 // standard view
102 $item_node = $this->dom_doc->createElement("MediaAliasItem");
103 $item_node = $mob_node->appendChild($item_node);
104 $item_node->setAttribute("Purpose", "Standard");
105 $media_item = $this->getMediaObject()->getMediaItem("Standard");
106 if (is_null($media_item)) {
107 return;
108 }
109
110 $layout_node = $this->dom_doc->createElement("Layout");
111 $layout_node = $item_node->appendChild($layout_node);
112 if ($media_item->getWidth() > 0) {
113 //$layout_node->set_attribute("Width", $media_item->getWidth());
114 }
115 if ($media_item->getHeight() > 0) {
116 //$layout_node->set_attribute("Height", $media_item->getHeight());
117 }
118 $layout_node->setAttribute("HorizontalAlign", "Left");
119
120 // caption
121 if ($media_item->getCaption() != "") {
122 $cap_node = $this->dom_doc->createElement("Caption");
123 $cap_node = $item_node->appendChild($cap_node);
124 $cap_node->setAttribute("Align", "bottom");
125 $this->dom_util->setContent($cap_node, $media_item->getCaption());
126 }
127
128 // text representation
129 if ($media_item->getTextRepresentation() != "") {
130 $tr_node = $this->dom_doc->createElement("TextRepresentation");
131 $tr_node = $item_node->appendChild($tr_node);
132 $this->dom_util->setContent($tr_node, $media_item->getTextRepresentation());
133 }
134
135 $pars = $media_item->getParameters();
136 foreach ($pars as $par => $val) {
137 $par_node = $this->dom_doc->createElement("Parameter");
138 $par_node = $item_node->appendChild($par_node);
139 $par_node->setAttribute("Name", $par);
140 $par_node->setAttribute("Value", $val);
141 }
142
143 // fullscreen view
144 $fullscreen_item = $this->getMediaObject()->getMediaItem("Fullscreen");
145 if (is_object($fullscreen_item)) {
146 $item_node = $this->dom_doc->createElement("MediaAliasItem");
147 $item_node = $mob_node->appendChild($item_node);
148 $item_node->setAttribute("Purpose", "Fullscreen");
149
150 // width and height
151 $layout_node = $this->dom_doc->createElement("Layout");
152 $layout_node = $item_node->appendChild($layout_node);
153 if ($fullscreen_item->getWidth() > 0) {
154 $layout_node->setAttribute("Width", $fullscreen_item->getWidth());
155 }
156 if ($fullscreen_item->getHeight() > 0) {
157 $layout_node->setAttribute("Height", $fullscreen_item->getHeight());
158 }
159
160 // caption
161 if ($fullscreen_item->getCaption() != "") {
162 $cap_node = $this->dom_doc->createElement("Caption");
163 $cap_node = $item_node->appendChild($cap_node);
164 $cap_node->setAttribute("Align", "bottom");
165 $this->dom_util->setContent($cap_node, $fullscreen_item->getCaption());
166 }
167
168 // text representation
169 if ($fullscreen_item->getTextRepresentation() != "") {
170 $tr_node = $this->dom_doc->createElement("TextRepresentation");
171 $tr_node = $item_node->appendChild($tr_node);
172 $this->dom_util->setContent($tr_node, $fullscreen_item->getTextRepresentation());
173 }
174
175 $pars = $fullscreen_item->getParameters();
176 foreach ($pars as $par => $val) {
177 $par_node = $this->dom_doc->createElement("Parameter");
178 $par_node = $item_node->appendChild($par_node);
179 $par_node->setAttribute("Name", $par);
180 $par_node->setAttribute("Value", $val);
181 }
182 }
183 }
184
185 protected function getMediaAliasNode(): ?DOMNode
186 {
187 if (is_object($this->getChildNode())) {
188 $mal_node = $this->getChildNode()->firstChild;
189 if (is_object($mal_node) && $mal_node->nodeName == "MediaAlias") {
190 return $mal_node;
191 }
192 }
193 return null;
194 }
195
201 public function updateObjectReference(): void
202 {
203 $this->getMediaAliasNode()?->setAttribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
204 }
205
206 public function dumpXML(): string
207 {
208 return $this->dom_util->dump($this->getDomNode());
209 }
210
211 public function setClass(string $a_class): void
212 {
213 $this->dom_util->setAttribute($this->getMediaAliasNode(), "Class", $a_class);
214 }
215
219 public function getClass(): string
220 {
221 return (string) $this->getMediaAliasNode()?->getAttribute("Class");
222 }
223
227 public function setCaptionClass(string $a_class): void
228 {
229 $this->dom_util->setAttribute($this->getMediaAliasNode(), "CaptionClass", $a_class);
230 }
231
232 public function getCaptionClass(): string
233 {
234 return (string) $this->getMediaAliasNode()?->getAttribute("CaptionClass");
235 }
236
237 public static function getLangVars(): array
238 {
239 return array("pc_mob");
240 }
241
249 public static function afterPageUpdate(
250 ilPageObject $a_page,
251 DOMDocument $a_domdoc,
252 string $a_xml,
253 bool $a_creation
254 ): void {
255 if (!$a_page->getImportMode()) {
257 $a_page->getParentType() . ":pg",
258 $a_page->getId(),
259 0,
260 $a_page->getLanguage()
261 );
262 self::saveMobUsage($a_page, $a_domdoc);
263 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
264 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
265 $mob_obj = new ilObjMediaObject($mob);
266 $usages = $mob_obj->getUsages(false);
267 if (count($usages) == 0) { // delete, if no usage exists
268 $mob_obj->delete();
269 }
270 }
271 }
272 }
273 }
274
275 public static function beforePageDelete(
276 ilPageObject $a_page
277 ): void {
278 $mob_ids = ilObjMediaObject::_getMobsOfObject(
279 $a_page->getParentType() . ":pg",
280 $a_page->getId(),
281 0,
282 $a_page->getLanguage()
283 );
284
286 $a_page->getParentType() . ":pg",
287 $a_page->getId(),
288 null,
289 $a_page->getLanguage()
290 );
291
292 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
293 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
294 $mob_obj = new ilObjMediaObject($mob);
295 $usages = $mob_obj->getUsages(false);
296 if (count($usages) == 0) { // delete, if no usage exists
297 $mob_obj->delete();
298 }
299 }
300 }
301 }
302
310 public static function afterPageHistoryEntry(
311 ilPageObject $a_page,
312 DOMDocument $a_old_domdoc,
313 string $a_old_xml,
314 int $a_old_nr
315 ): void {
316 self::saveMobUsage($a_page, $a_old_domdoc, $a_old_nr);
317 }
318
319 public static function saveMobUsage(
320 ilPageObject $a_page,
321 DOMDocument $a_domdoc,
322 int $a_old_nr = 0
323 ): array {
324 $usages = array();
325
326 // media aliases
327 $xpath = new DOMXPath($a_domdoc);
328 $nodes = $xpath->query('//MediaAlias');
329 foreach ($nodes as $node) {
330 $id_arr = explode("_", $node->getAttribute("OriginId"));
331 $mob_id = $id_arr[count($id_arr) - 1];
332 if ($mob_id > 0 && $id_arr[1] == "") {
333 $usages[$mob_id] = true;
334 }
335 }
336
337 // media objects
338 $xpath = new DOMXPath($a_domdoc);
339 $nodes = $xpath->query('//MediaObject/MetaData/General/Identifier');
340 foreach ($nodes as $node) {
341 $mob_entry = $node->getAttribute("Entry");
342 $mob_arr = explode("_", $mob_entry);
343 $mob_id = $mob_arr[count($mob_arr) - 1];
344 if ($mob_id > 0 && $mob_arr[1] == "") {
345 $usages[$mob_id] = true;
346 }
347 }
348
349 // internal links
350 $xpath = new DOMXPath($a_domdoc);
351 $nodes = $xpath->query("//IntLink[@Type='MediaObject']");
352 foreach ($nodes as $node) {
353 $mob_target = $node->getAttribute("Target");
354 $mob_arr = explode("_", $mob_target);
355 //echo "<br>3<br>";
356 //echo $mob_target."<br>";
357 //var_dump($mob_arr);
358 $mob_id = $mob_arr[count($mob_arr) - 1];
359 if ($mob_id > 0 && $mob_arr[1] == "") {
360 $usages[$mob_id] = true;
361 }
362 }
363
365 $a_page->getParentType() . ":pg",
366 $a_page->getId(),
367 $a_old_nr,
368 $a_page->getLanguage()
369 );
370 foreach ($usages as $mob_id => $val) {
371 // save usage, if object exists...
372 if (ilObject::_lookupType($mob_id) == "mob") {
374 $mob_id,
375 $a_page->getParentType() . ":pg",
376 $a_page->getId(),
377 $a_old_nr,
378 $a_page->getLanguage()
379 );
380 }
381 }
382
383 return $usages;
384 }
385
387 string $a_output,
388 string $a_mode,
389 bool $a_abstract_only = false
390 ): string {
391 global $DIC;
392
393 $this->global_tpl = $DIC['tpl'];
394 $ilUser = $this->user;
395
396 if ($a_mode == "offline") {
397 $page = $this->getPage();
398
400 $page->getParentType() . ":pg",
401 $page->getId(),
402 0,
403 $page->getLanguage()
404 );
405 foreach ($mob_ids as $mob_id) {
406 $mob = new ilObjMediaObject($mob_id);
407 $srts = $mob->getSrtFiles();
408 foreach ($srts as $srt) {
409 if ($ilUser->getLanguage() == $srt["language"]) {
410 $srt_content = file_get_contents(ilObjMediaObject::_getDirectory($mob->getId()) . "/" . $srt["full_path"]);
411 $a_output = str_replace("[[[[[mobsubtitle;il__mob_" . $mob->getId() . "_Standard]]]]]", $srt_content, $a_output);
412 }
413 }
414 }
415 }
416
417 if ($a_abstract_only) {
418 return $a_output;
419 }
420
421 if ($a_mode === "edit") {
422 $a_output = str_replace(
423 "{{{{{Unsupported Media Type}}}}}",
424 $this->ui->renderer()->render(
425 $this->ui->factory()->messageBox()->info(
426 $this->lng->txt("copg_unsupported_media_type")
427 )
428 ),
429 $a_output
430 );
431 // currently media objects, when in translation mode
432 $a_output = str_replace(
433 "{{{{{No Media Type}}}}}",
434 '<div class="copg-new-content-placeholder">' .
435 $this->ui->renderer()->render(
436 $this->ui->factory()->symbol()->icon()->standard("pemed", "pemed", 'medium')
437 ) . "</div>",
438 $a_output
439 );
440 } else {
441 $a_output = str_replace(
442 "{{{{{Unsupported Media Type}}}}}",
443 "",
444 $a_output
445 );
446 $a_output = str_replace(
447 "{{{{{No Media Type}}}}}",
448 "",
449 $a_output
450 );
451 }
452
453 // add fullscreen modals
454 $page = $this->getPage();
455 $suffix = "-" . $page->getParentType() . "-" . $page->getId();
456 $modal = $this->ui->factory()->modal()->roundtrip(
457 $this->lng->txt("cont_fullscreen"),
458 $this->ui->factory()->legacy()->content("<iframe class='il-copg-mob-fullscreen' id='il-copg-mob-fullscreen" . $suffix . "'></iframe>")
459 );
460 $show_signal = $modal->getShowSignal();
461
462 $js = "
463 $(function () {
464 il.COPagePres.setFullscreenModalShowSignal('$show_signal', '$suffix');
465 });
466 ";
467 self::$modal_show_signal = $show_signal;
468 self::$modal_suffix = $suffix;
469 $this->global_tpl->addOnloadCode($js);
470
471 // async ensures to have onloadcode of modal in output
472 // if other main tpl is used, see #32198
473 // note: if always rendered async, $ not defined errors will be thrown in non-async cases
474 if ($this->ctrl->isAsynch()) {
475 $html = $this->ui->renderer()->renderAsync($modal);
476 } else {
477 $html = $this->ui->renderer()->render($modal);
478 }
479 return $a_output . "<div class='il-copg-mob-fullscreen-modal'>" . $html . "</div>";
480 }
481
482 public function getOnloadCode(string $a_mode): array
483 {
484 $onload_code = [];
485 // necessary due to 32198 (other main template used)
486 if (self::$modal_show_signal !== "") {
487 $onload_code[] = "il.COPagePres.setFullscreenModalShowSignal('" . self::$modal_show_signal .
488 "', '" . self::$modal_suffix . "');";
489 }
490 return $onload_code;
491 }
492
493 public function getJavascriptFiles(
494 string $a_mode
495 ): array {
496 //$js_files = ilPlayerUtil::getJsFilePaths();
497 $js_files[] = iljQueryUtil::getLocalMaphilightPath();
498 return $js_files;
499 }
500
501 public function getCssFiles(
502 string $a_mode
503 ): array {
504 return [];
505 }
506
508 {
509 $std_alias_item = new ilMediaAliasItem(
510 $this->getDomDoc(),
511 $this->getHierId(),
512 "Standard",
513 $this->getPCId()
514 );
515 return $std_alias_item;
516 }
517
519 {
520 $std_alias_item = new ilMediaAliasItem(
521 $this->getDomDoc(),
522 $this->getHierId(),
523 "Fullscreen",
524 $this->getPCId()
525 );
526 return $std_alias_item;
527 }
528
529 public function checkInstanceEditing(): bool
530 {
531 // if any properties are set on the instance,
532 // that are not accessible through the quick editing screen
533 // -> offer instance editing
534 $std_alias_item = $this->getStandardMediaAliasItem();
535 if ($std_alias_item->hasAnyPropertiesSet()) {
536 return true;
537 }
538 if ($this->getMediaObject()->hasFullscreenItem()) {
539 $full_alias_item = $this->getFullscreenMediaAliasItem();
540 if ($full_alias_item->hasAnyPropertiesSet()) {
541 return true;
542 }
543 }
544
545 // if the media object has any other use outside of the current page
546 // -> offer instance editing
548 $mob = $this->getMediaObject();
549 $page = $this->getPage();
550 if (is_object($mob)) {
551 $usages = $mob->getUsages();
552 $other_usages = array_filter($usages, function ($usage) use ($page) {
553 return ($usage["type"] != $page->getParentType() . ":pg" || $usage["id"] != $page->getId());
554 });
555 if (count($other_usages) > 0) {
556 return true;
557 }
558 }
559 // see https://mantis.ilias.de/view.php?id=38582
560 // we allow instance editing regardless of number of usages
561 return true;
562 }
563
564 public static function deleteHistoryLowerEqualThan(
565 string $parent_type,
566 int $page_id,
567 string $lang,
568 int $delete_lower_than_nr
569 ): void {
570 global $DIC;
571
572 $mob_usage_repo = $DIC->mediaObjects()
573 ->internal()
574 ->repo()
575 ->usage();
576
578
579 $mob_ids = $mob_usage_repo->getHistoryUsagesLowerEqualThan(
580 $parent_type . ":pg",
581 $page_id,
582 $delete_lower_than_nr,
583 $lang
584 );
585
586 $mob_usage_repo->deleteHistoryUsagesLowerEqualThan(
587 $parent_type . ":pg",
588 $page_id,
589 $delete_lower_than_nr,
590 $lang
591 );
592
593 foreach ($mob_ids as $mob_id) {
594 $usages = ilObjMediaObject::lookupUsages($mob_id, true);
595 $log->debug("...check deletion of mob $mob_id. Usages: " . count($usages));
596 if (count($usages) == 0) {
597 if (ilObject::_lookupType($mob_id) === "mob") {
598 $mob = new ilObjMediaObject($mob_id);
599 $log->debug("Deleting Mob ID: " . $mob_id);
600 $mob->delete();
601 }
602 }
603 }
604 }
605
606 public static function handleCopiedContent(
607 DOMDocument $a_domdoc,
608 bool $a_self_ass = true,
609 bool $a_clone_mobs = false,
610 int $new_parent_id = 0,
611 int $obj_copy_id = 0
612 ): void {
613 global $DIC;
614
615 if (!$a_clone_mobs) {
616 return;
617 }
618
619 $dom_util = $DIC->copage()->internal()->domain()->domUtil();
620 $path = "//MediaObject/MediaAlias";
621 $nodes = $dom_util->path($a_domdoc, $path);
622 foreach ($nodes as $node) {
623 $or_id = $node->getAttribute("OriginId");
624
625 $inst_id = ilInternalLink::_extractInstOfTarget($or_id);
627
628 if (!($inst_id > 0)) {
629 if ($mob_id > 0) {
630 $media_object = new ilObjMediaObject($mob_id);
631 $new_mob = $media_object->duplicate();
632 $node->setAttribute("OriginId", "il__mob_" . $new_mob->getId());
633 }
634 }
635 }
636 }
637}
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)
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="")
static afterPageHistoryEntry(ilPageObject $a_page, DOMDocument $a_old_domdoc, string $a_old_xml, int $a_old_nr)
After page history entry has been created.
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 handleCopiedContent(DOMDocument $a_domdoc, bool $a_self_ass=true, bool $a_clone_mobs=false, int $new_parent_id=0, int $obj_copy_id=0)
Handle copied content.
static getLangVars()
Get lang vars needed for editing.
setMediaObject(ilObjMediaObject $a_mediaobject)
static string $modal_show_signal
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
Content object of ilPageObject (see ILIAS DTD).
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setDomNode(DOMNode $node)
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...
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$log
Definition: ltiresult.php:34
$path
Definition: ltiservices.php:30
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
getLanguage()
$lang
Definition: xapiexit.php:25