ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.MediaObjectManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 protected \ILIAS\COPage\Dom\DomUtil $dom_util;
29
30 public function __construct()
31 {
32 global $DIC;
33 $this->dom_util = $DIC->copage()->internal()->domain()->domUtil();
34 }
35
40 public function collectMediaObjects(
41 \DOMDocument $dom,
42 bool $a_inline_only = true
43 ): array {
44 // determine all media aliases of the page
45 $path = "//MediaObject/MediaAlias";
46 $mob_ids = array();
47 $nodes = $this->dom_util->path($dom, $path);
48 foreach ($nodes as $node) {
49 $id_arr = explode("_", $node->getAttribute("OriginId"));
50 $mob_id = $id_arr[count($id_arr) - 1];
51 $mob_ids[$mob_id] = $mob_id;
52 }
53
54 // determine all media aliases of interactive images
55 $path = "//InteractiveImage/MediaAlias";
56 $nodes = $this->dom_util->path($dom, $path);
57 foreach ($nodes as $node) {
58 $id_arr = explode("_", $node->getAttribute("OriginId"));
59 $mob_id = $id_arr[count($id_arr) - 1];
60 $mob_ids[$mob_id] = $mob_id;
61 }
62
63 // determine all inline internal media links
64 $path = "//IntLink[@Type = 'MediaObject']";
65 $nodes = $this->dom_util->path($dom, $path);
66 foreach ($nodes as $node) {
67 if (($node->getAttribute("TargetFrame") == "") ||
68 (!$a_inline_only)) {
69 $target = $node->getAttribute("Target");
70 $id_arr = explode("_", $target);
71 if (($id_arr[1] == IL_INST_ID) ||
72 (substr($target, 0, 4) == "il__")) {
73 $mob_id = $id_arr[count($id_arr) - 1];
74 if (\ilObject::_exists((int) $mob_id)) {
75 $mob_ids[$mob_id] = $mob_id;
76 }
77 }
78 }
79 }
80 return $mob_ids;
81 }
82
87 public function getMultimediaXML(
88 \DOMDocument $dom,
89 bool $offline = false
90 ): string {
91 $mob_ids = $this->collectMediaObjects($dom);
92
93 // get xml of corresponding media objects
94 $mobs_xml = "";
95 foreach ($mob_ids as $mob_id => $dummy) {
96 if (\ilObject::_lookupType((int) $mob_id) === "mob") {
97 $mob_obj = new \ilObjMediaObject($mob_id);
98 $mobs_xml .= $mob_obj->getXML(IL_MODE_OUTPUT, $a_inst = 0, true, $offline);
99 }
100 }
101 return $mobs_xml;
102 }
103
107 public function getMediaAliasElement(
108 \DOMDocument $dom,
109 int $a_mob_id,
110 int $a_nr = 1
111 ): string {
112 $path = "//MediaObject/MediaAlias[@OriginId='il__mob_$a_mob_id']";
113 $nodes = $this->dom_util->path($dom, $path);
114 $mal_node = $nodes->item($a_nr - 1);
115 $mob_node = $mal_node->parentNode;
116 return $this->dom_util->dump($mob_node);
117 }
118
119 public function resolveMediaAliases(
120 \ilPageObject $page,
121 array $a_mapping,
122 bool $a_reuse_existing_by_import = false
123 ): bool {
124 // resolve normal internal links
125 $dom = $page->getDomDoc();
126 $path = "//MediaAlias";
127 $changed = false;
128 $nodes = $this->dom_util->path($dom, $path);
129 foreach ($nodes as $node) {
130 // get the ID of the import file from the xml
131 $old_id = $node->getAttribute("OriginId");
132 $old_id = explode("_", $old_id);
133 $old_id = $old_id[count($old_id) - 1];
134 $new_id = "";
135 $import_id = "";
136 // get the new id from the current mapping
137 if (($a_mapping[$old_id] ?? 0) > 0) {
138 $new_id = $a_mapping[$old_id];
139 if ($a_reuse_existing_by_import) {
140 // this should work, if the lm has been imported in a translation installation and re-exported
141 $import_id = \ilObject::_lookupImportId($new_id);
142 $imp = explode("_", $import_id);
143 if ($imp[1] == IL_INST_ID && $imp[2] == "mob" && \ilObject::_lookupType((int) ($imp[3] ?? 0)) == "mob") {
144 $new_id = $imp[3];
145 }
146 }
147 }
148 // now check, if the translation has been done just by changing text in the exported
149 // translation file
150 if ($import_id == "" && $a_reuse_existing_by_import) {
151 // if the old_id is also referred by the page content of the default language
152 // we assume that this media object is unchanged
153 $med_of_def_lang = \ilObjMediaObject::_getMobsOfObject(
154 $page->getParentType() . ":pg",
155 $page->getId(),
156 0,
157 "-"
158 );
159 if (in_array($old_id, $med_of_def_lang)) {
160 $new_id = $old_id;
161 }
162 }
163 if ($new_id != "") {
164 $node->setAttribute("OriginId", "il__mob_" . $new_id);
165 $changed = true;
166 }
167 }
168 return $changed;
169 }
170}
getMultimediaXML(\DOMDocument $dom, bool $offline=false)
get a xml string that contains all media object elements, that are referenced by any media alias in t...
collectMediaObjects(\DOMDocument $dom, bool $a_inline_only=true)
get all media objects, that are referenced and used within the page
getMediaAliasElement(\DOMDocument $dom, int $a_mob_id, int $a_nr=1)
get complete media object (alias) element
resolveMediaAliases(\ilPageObject $page, array $a_mapping, bool $a_reuse_existing_by_import=false)
const IL_MODE_OUTPUT
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static _lookupType(int $id, bool $reference=false)
static _lookupImportId(int $obj_id)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
const IL_INST_ID
Definition: constants.php:40
$path
Definition: ltiservices.php:30
global $DIC
Definition: shib_login.php:26