ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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(\DOMDocument $dom): string
88  {
89  $mob_ids = $this->collectMediaObjects($dom);
90 
91  // get xml of corresponding media objects
92  $mobs_xml = "";
93  foreach ($mob_ids as $mob_id => $dummy) {
94  if (\ilObject::_lookupType((int) $mob_id) === "mob") {
95  $mob_obj = new \ilObjMediaObject($mob_id);
96  $mobs_xml .= $mob_obj->getXML(IL_MODE_OUTPUT, $a_inst = 0, true);
97  }
98  }
99  return $mobs_xml;
100  }
101 
105  public function getMediaAliasElement(
106  \DOMDocument $dom,
107  int $a_mob_id,
108  int $a_nr = 1
109  ): string {
110  $path = "//MediaObject/MediaAlias[@OriginId='il__mob_$a_mob_id']";
111  $nodes = $this->dom_util->path($dom, $path);
112  $mal_node = $nodes->item($a_nr - 1);
113  $mob_node = $mal_node->parentNode;
114  return $this->dom_util->dump($mob_node);
115  }
116 
117  public function resolveMediaAliases(
118  \ilPageObject $page,
119  array $a_mapping,
120  bool $a_reuse_existing_by_import = false
121  ): bool {
122  // resolve normal internal links
123  $dom = $page->getDomDoc();
124  $path = "//MediaAlias";
125  $changed = false;
126  $nodes = $this->dom_util->path($dom, $path);
127  foreach ($nodes as $node) {
128  // get the ID of the import file from the xml
129  $old_id = $node->getAttribute("OriginId");
130  $old_id = explode("_", $old_id);
131  $old_id = $old_id[count($old_id) - 1];
132  $new_id = "";
133  $import_id = "";
134  // get the new id from the current mapping
135  if (($a_mapping[$old_id] ?? 0) > 0) {
136  $new_id = $a_mapping[$old_id];
137  if ($a_reuse_existing_by_import) {
138  // this should work, if the lm has been imported in a translation installation and re-exported
139  $import_id = \ilObject::_lookupImportId($new_id);
140  $imp = explode("_", $import_id);
141  if ($imp[1] == IL_INST_ID && $imp[2] == "mob" && \ilObject::_lookupType((int) ($imp[3] ?? 0)) == "mob") {
142  $new_id = $imp[3];
143  }
144  }
145  }
146  // now check, if the translation has been done just by changing text in the exported
147  // translation file
148  if ($import_id == "" && $a_reuse_existing_by_import) {
149  // if the old_id is also referred by the page content of the default language
150  // we assume that this media object is unchanged
151  $med_of_def_lang = \ilObjMediaObject::_getMobsOfObject(
152  $page->getParentType() . ":pg",
153  $page->getId(),
154  0,
155  "-"
156  );
157  if (in_array($old_id, $med_of_def_lang)) {
158  $new_id = $old_id;
159  }
160  }
161  if ($new_id != "") {
162  $node->setAttribute("OriginId", "il__mob_" . $new_id);
163  $changed = true;
164  }
165  }
166  return $changed;
167  }
168 }
const IL_INST_ID
Definition: constants.php:40
getDomDoc()
Get dom doc (DOMDocument)
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
static _lookupImportId(int $obj_id)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
getMultimediaXML(\DOMDocument $dom)
get a xml string that contains all media object elements, that are referenced by any media alias in t...
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
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