ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.MediaObjectManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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  {
92  $mob_ids = $this->collectMediaObjects($dom);
93 
94  // get xml of corresponding media objects
95  $mobs_xml = "";
96  foreach ($mob_ids as $mob_id => $dummy) {
97  if (\ilObject::_lookupType((int) $mob_id) === "mob") {
98  $mob_obj = new \ilObjMediaObject($mob_id);
99  $mobs_xml .= $mob_obj->getXML(IL_MODE_OUTPUT, $a_inst = 0, true, $offline);
100  }
101  }
102  return $mobs_xml;
103  }
104 
108  public function getMediaAliasElement(
109  \DOMDocument $dom,
110  int $a_mob_id,
111  int $a_nr = 1
112  ): string {
113  $path = "//MediaObject/MediaAlias[@OriginId='il__mob_$a_mob_id']";
114  $nodes = $this->dom_util->path($dom, $path);
115  $mal_node = $nodes->item($a_nr - 1);
116  $mob_node = $mal_node->parentNode;
117  return $this->dom_util->dump($mob_node);
118  }
119 
120  public function resolveMediaAliases(
121  \ilPageObject $page,
122  array $a_mapping,
123  bool $a_reuse_existing_by_import = false
124  ): bool {
125  // resolve normal internal links
126  $dom = $page->getDomDoc();
127  $path = "//MediaAlias";
128  $changed = false;
129  $nodes = $this->dom_util->path($dom, $path);
130  foreach ($nodes as $node) {
131  // get the ID of the import file from the xml
132  $old_id = $node->getAttribute("OriginId");
133  $old_id = explode("_", $old_id);
134  $old_id = $old_id[count($old_id) - 1];
135  $new_id = "";
136  $import_id = "";
137  // get the new id from the current mapping
138  if (($a_mapping[$old_id] ?? 0) > 0) {
139  $new_id = $a_mapping[$old_id];
140  if ($a_reuse_existing_by_import) {
141  // this should work, if the lm has been imported in a translation installation and re-exported
142  $import_id = \ilObject::_lookupImportId($new_id);
143  $imp = explode("_", $import_id);
144  if ($imp[1] == IL_INST_ID && $imp[2] == "mob" && \ilObject::_lookupType((int) ($imp[3] ?? 0)) == "mob") {
145  $new_id = $imp[3];
146  }
147  }
148  }
149  // now check, if the translation has been done just by changing text in the exported
150  // translation file
151  if ($import_id == "" && $a_reuse_existing_by_import) {
152  // if the old_id is also referred by the page content of the default language
153  // we assume that this media object is unchanged
154  $med_of_def_lang = \ilObjMediaObject::_getMobsOfObject(
155  $page->getParentType() . ":pg",
156  $page->getId(),
157  0,
158  "-"
159  );
160  if (in_array($old_id, $med_of_def_lang)) {
161  $new_id = $old_id;
162  }
163  }
164  if ($new_id != "") {
165  $node->setAttribute("OriginId", "il__mob_" . $new_id);
166  $changed = true;
167  }
168  }
169  return $changed;
170  }
171 }
const IL_INST_ID
Definition: constants.php:40
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...
getDomDoc()
Get dom doc (DOMDocument)
$path
Definition: ltiservices.php:30
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)
global $DIC
Definition: shib_login.php:25
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
const IL_MODE_OUTPUT
static _lookupType(int $id, bool $reference=false)
collectMediaObjects(\DOMDocument $dom, bool $a_inline_only=true)
get all media objects, that are referenced and used within the page
resolveMediaAliases(\ilPageObject $page, array $a_mapping, bool $a_reuse_existing_by_import=false)
getMediaAliasElement(\DOMDocument $dom, int $a_mob_id, int $a_nr=1)
get complete media object (alias) element