ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilObjMediaPool.php
Go to the documentation of this file.
1 <?php
2 
19 use ILIAS\Export\ExportHandler\I\FactoryInterface as ExportFactoryInterface;
20 use ILIAS\Export\ExportHandler\Factory as ExportFactory;
22 
29 {
30  protected ?int $default_width = null;
31  protected ?int $default_height = null;
32  protected ilTree $mep_tree;
33  protected ExportFactoryInterface $export_factory;
35  public bool $for_translation = false;
36 
37  public function __construct(
38  int $a_id = 0,
39  bool $a_call_by_reference = true
40  ) {
41  global $DIC;
42 
43  $this->export_factory = new ExportFactory();
44  $this->data_factory = new DataFactory();
45  $this->db = $DIC->database();
46  $this->lng = $DIC->language();
47  // this also calls read() method! (if $a_id is set)
48  $this->type = "mep";
49  parent::__construct($a_id, $a_call_by_reference);
50  }
51 
52  public function setDefaultWidth(?int $a_val): void
53  {
54  if ($a_val === 0) {
55  $a_val = null;
56  }
57  $this->default_width = $a_val;
58  }
59 
60  public function getDefaultWidth(): ?int
61  {
62  return $this->default_width;
63  }
64 
65  public function setDefaultHeight(?int $a_val): void
66  {
67  if ($a_val === 0) {
68  $a_val = null;
69  }
70  $this->default_height = $a_val;
71  }
72 
73  public function getDefaultHeight(): ?int
74  {
75  return $this->default_height;
76  }
77 
81  public function setForTranslation(bool $a_val): void
82  {
83  $this->for_translation = $a_val;
84  }
85 
86  public function getForTranslation(): bool
87  {
89  }
90 
91  public function read(): void
92  {
93  $ilDB = $this->db;
94 
95  parent::read();
96 
97  $set = $ilDB->query(
98  "SELECT * FROM mep_data " .
99  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
100  );
101  if ($rec = $ilDB->fetchAssoc($set)) {
102  $this->setDefaultWidth($rec["default_width"]);
103  $this->setDefaultHeight($rec["default_height"]);
104  $this->setForTranslation($rec["for_translation"]);
105  }
106  $this->mep_tree = self::_getPoolTree($this->getId());
107  }
108 
109 
113  public static function _getPoolTree(int $a_obj_id): ilTree
114  {
115  $tree = new ilTree($a_obj_id);
116  $tree->setTreeTablePK("mep_id");
117  $tree->setTableNames("mep_tree", "mep_item");
118 
119  return $tree;
120  }
121 
122  public function getPoolTree(): ilTree
123  {
124  return self::_getPoolTree($this->getId());
125  }
126 
127  public function create(): int
128  {
129  $ilDB = $this->db;
130 
131  $id = parent::create();
132 
133  $this->createMetaData();
134 
135  $ilDB->manipulate("INSERT INTO mep_data " .
136  "(id, default_width, default_height, for_translation) VALUES (" .
137  $ilDB->quote($this->getId(), "integer") . ", " .
138  $ilDB->quote($this->getDefaultWidth(), "integer") . ", " .
139  $ilDB->quote($this->getDefaultHeight(), "integer") . ", " .
140  $ilDB->quote($this->getForTranslation(), "integer") .
141  ")");
142 
143  $this->createMepTree();
144  return $id;
145  }
146 
147  public function createMepTree(): void
148  {
149  // create media pool tree
150  $this->mep_tree = new ilTree($this->getId());
151  $this->mep_tree->setTreeTablePK("mep_id");
152  $this->mep_tree->setTableNames('mep_tree', 'mep_item');
153  $this->mep_tree->addTree($this->getId(), 1);
154  }
155 
156  public function getTree(): ilTree
157  {
158  return $this->mep_tree;
159  }
160 
161  public function update(): bool
162  {
163  $ilDB = $this->db;
164 
165  if (!parent::update()) {
166  return false;
167  }
168 
169  $this->updateMetaData();
170 
171  // put here object specific stuff
172  $ilDB->manipulate(
173  "UPDATE mep_data SET " .
174  " default_width = " . $ilDB->quote($this->getDefaultWidth(), "integer") . "," .
175  " default_height = " . $ilDB->quote($this->getDefaultHeight(), "integer") . "," .
176  " for_translation = " . $ilDB->quote($this->getForTranslation(), "integer") . " " .
177  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
178  );
179 
180  return true;
181  }
182 
183 
184  public function delete(): bool
185  {
186  // always call parent delete function first!!
187  if (!parent::delete()) {
188  return false;
189  }
190 
191  $this->deleteMetaData();
192 
193  // get childs
194  $childs = $this->mep_tree->getSubTree($this->mep_tree->getNodeData($this->mep_tree->readRootId()));
195 
196  // delete tree
197  $this->mep_tree->removeTree($this->mep_tree->getTreeId());
198 
199  // delete childs
200  foreach ($childs as $child) {
201  $fid = ilMediaPoolItem::lookupForeignId($child["obj_id"]);
202  switch ($child["type"]) {
203  case "mob":
204  if (ilObject::_lookupType($fid) === "mob") {
205  $mob = new ilObjMediaObject($fid);
206  $mob->delete();
207  }
208  break;
209  }
210  }
211 
212  return true;
213  }
214 
218  public function getChilds(
219  int $obj_id = 0,
220  string $a_type = ""
221  ): array {
222  $objs = array();
223  $mobs = array();
224  $pgs = array();
225  if ($obj_id === 0) {
226  $obj_id = $this->mep_tree->getRootId();
227  }
228 
229  if ($a_type === "fold" || $a_type === "") {
230  $objs = $this->mep_tree->getChildsByType($obj_id, "fold");
231  }
232  if ($a_type === "mob" || $a_type === "") {
233  $mobs = $this->mep_tree->getChildsByType($obj_id, "mob");
234  }
235  foreach ($mobs as $key => $mob) {
236  $objs[] = $mob;
237  }
238  if ($a_type === "pg" || $a_type === "") {
239  $pgs = $this->mep_tree->getChildsByType($obj_id, "pg");
240  }
241  foreach ($pgs as $key => $pg) {
242  $objs[] = $pg;
243  }
244 
245  return $objs;
246  }
247 
248  public function getChildsExceptFolders(
249  int $obj_id = 0
250  ): array {
251  if ($obj_id === 0) {
252  $obj_id = $this->mep_tree->getRootId();
253  }
254 
255  return $this->mep_tree->getFilteredChilds(array("fold", "dummy"), $obj_id);
256  }
257 
262  public static function getAllMobIds(int $a_id): array
263  {
264  global $DIC;
265 
266  $ilDB = $DIC->database();
267 
268  $query = "SELECT foreign_id as id FROM " .
269  " mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) " .
270  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) " .
271  " WHERE mep_tree.mep_id = " . $ilDB->quote($a_id, "integer") .
272  " AND mep_item.type = " . $ilDB->quote("mob", "text") .
273  " AND object_data.type = " . $ilDB->quote("mob", "text");
274  $set = $ilDB->query($query);
275  $ids = array();
276  while ($rec = $ilDB->fetchAssoc($set)) {
277  $ids[] = (int) $rec["id"];
278  }
279  return $ids;
280  }
281 
285  public function getUsedFormats(): array
286  {
287  $ilDB = $this->db;
288  $lng = $this->lng;
289 
290  $query = "SELECT DISTINCT media_item.format f FROM mep_tree " .
291  " JOIN mep_item ON (mep_item.obj_id = mep_tree.child) " .
292  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) " .
293  " JOIN media_item ON (media_item.mob_id = object_data.obj_id) " .
294  " WHERE mep_tree.mep_id = " . $ilDB->quote($this->getId(), "integer") .
295  " AND object_data.type = " . $ilDB->quote("mob", "text") .
296  " ORDER BY f";
297  $formats = array();
298  $set = $ilDB->query($query);
299  while ($rec = $ilDB->fetchAssoc($set)) {
300  if ($rec["f"] != "") {
301  $formats[$rec["f"]] = $rec["f"];
302  } else {
303  $formats["unknown"] = $lng->txt("mep_unknown");
304  }
305  }
306 
307  return $formats;
308  }
309 
310  public function getParentId(int $obj_id = 0): ?int
311  {
312  if ($obj_id === 0) {
313  return null;
314  }
315  if ($obj_id === $this->mep_tree->getRootId()) {
316  return null;
317  }
318 
319  return (int) $this->mep_tree->getParentId($obj_id);
320  }
321 
325  public function insertInTree(
326  int $a_obj_id,
327  ?int $a_parent = null
328  ): bool {
329  if (!$this->mep_tree->isInTree($a_obj_id)) {
330  $parent = (is_null($a_parent))
331  ? $this->mep_tree->getRootId()
332  : $a_parent;
333  $this->mep_tree->insertNode($a_obj_id, $parent);
334  return true;
335  }
336 
337  return false;
338  }
339 
340 
344  public function deleteChild(int $obj_id): void
345  {
346  $node_data = $this->mep_tree->getNodeData($obj_id);
347  $subtree = $this->mep_tree->getSubTree($node_data);
348 
349  // delete tree
350  if ($this->mep_tree->isInTree($obj_id)) {
351  $this->mep_tree->deleteTree($node_data);
352  }
353 
354  // delete objects
355  foreach ($subtree as $node) {
356  $fid = ilMediaPoolItem::lookupForeignId($node["child"]);
357  if ($node["type"] === "mob" && ilObject::_lookupType($fid) === "mob") {
358  $obj = new ilObjMediaObject($fid);
359  $obj->delete();
360  }
361 
362  if ($node["type"] === "fold" && $fid > 0 && ilObject::_lookupType($fid) === "fold") {
363  $obj = new ilObjFolder($fid, false);
364  $obj->delete();
365  }
366 
367  if ($node["type"] === "pg" && ilPageObject::_exists("mep", $node["child"])) {
368  $pg = new ilMediaPoolPage($node["child"]);
369  $pg->delete();
370  }
371 
372  $item = new ilMediaPoolItem($node["child"]);
373  $item->delete();
374  }
375  }
376 
380  public static function isForeignIdInTree(
381  int $a_pool_id,
382  int $a_foreign_id
383  ): bool {
384  global $DIC;
385 
386  $ilDB = $DIC->database();
387 
388  $set = $ilDB->query(
389  "SELECT * FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE " .
390  " foreign_id = " . $ilDB->quote($a_foreign_id, "integer") .
391  " AND mep_id = " . $ilDB->quote($a_pool_id, "integer")
392  );
393  if ($rec = $ilDB->fetchAssoc($set)) {
394  return true;
395  }
396  return false;
397  }
398 
402  public static function isItemIdInTree(
403  int $a_pool_id,
404  int $a_item_id
405  ): bool {
406  global $DIC;
407 
408  $ilDB = $DIC->database();
409 
410  $set = $ilDB->query("SELECT * FROM mep_tree WHERE child = " .
411  $ilDB->quote($a_item_id, "integer") .
412  " AND mep_id = " . $ilDB->quote($a_pool_id, "integer"));
413  if ($rec = $ilDB->fetchAssoc($set)) {
414  return true;
415  }
416  return false;
417  }
418 
419  public function createFolder(
420  string $a_title,
421  int $a_parent = 0
422  ): ?int {
423  // perform save
424  $mep_item = new ilMediaPoolItem();
425  $mep_item->setTitle($a_title);
426  $mep_item->setType("fold");
427  $mep_item->create();
428  if ($mep_item->getId() > 0) {
429  $tree = $this->getTree();
430  $parent = $a_parent > 0
431  ? $a_parent
432  : $tree->getRootId();
433  $this->insertInTree($mep_item->getId(), $parent);
434  return $mep_item->getId();
435  }
436  return null;
437  }
438 
445  public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree = false): ?ilObject
446  {
448  $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
449 
450  $new_obj->setDescription($this->getDescription());
451  $new_obj->setDefaultWidth($this->getDefaultWidth());
452  $new_obj->setDefaultHeight($this->getDefaultHeight());
453  $new_obj->update();
454 
455  // copy content
456  $this->copyTreeContent(
457  $new_obj,
458  $new_obj->getTree()->readRootId(),
459  $this->getTree()->readRootId()
460  );
461 
462  $this->cloneMetaData($new_obj);
463 
464  return $new_obj;
465  }
466 
467  public function copyTreeContent(
468  ilObjMediaPool $a_new_obj,
469  int $a_target_parent,
470  int $a_source_parent
471  ): void {
472  // get all childs
473  $nodes = $this->getTree()->getChilds($a_source_parent);
474  foreach ($nodes as $node) {
475  $item = new ilMediaPoolItem();
476  $item->setType($node["type"]);
477  switch ($node["type"]) {
478  case "mob":
479  $mob_id = ilMediaPoolItem::lookupForeignId($node["child"]);
480  $mob = new ilObjMediaObject($mob_id);
481  $new_mob = $mob->duplicate();
482  $item->setForeignId($new_mob->getId());
483  $item->setTitle($new_mob->getTitle());
484  $item->create();
485  break;
486 
487  case "pg":
488  $item->setTitle($node["title"]);
489  $item->create();
490  $page = new ilMediaPoolPage($node["child"]);
491  $new_page = new ilMediaPoolPage();
492  $new_page->setParentId($a_new_obj->getId());
493  $new_page->setId($item->getId());
494  $new_page->create(false);
495 
496  // copy page
497  $page->copy($new_page->getId(), $new_page->getParentType(), $new_page->getParentId(), true);
498  break;
499 
500  case "fold":
501  $item->setTitle($node["title"]);
502  $item->create();
503  break;
504  }
505 
506  // insert item into tree
507  $a_new_obj->insertInTree($item->getId(), $a_target_parent);
508 
509  // handle childs
510  $this->copyTreeContent($a_new_obj, $item->getId(), $node["child"]);
511  }
512  }
513 
517  public function exportXML(string $a_mode = ""): void
518  {
519  if (in_array($a_mode, array("master", "masternomedia"))) {
521  $configs = $this->export_factory->consumer()->exportConfig()->allExportConfigs();
522  $config = $configs->getElementByComponent('components/ILIAS/MediaPool');
523  $config->setMasterLanguageOnly(true, ($a_mode === "master"));
524  $this->export_factory->consumer()->handler()->createStandardExport($this->user->getId(), $this->data_factory->objId($this->getId()), $configs);
525  }
526  }
527 
528  public static function getAdvMDSubItemTitle(int $a_obj_id, string $a_sub_type, int $a_sub_id): string
529  {
531  $snippets = $repo->getItems((int) $a_obj_id);
532  foreach ($snippets as $snippet) {
533  if ((int) $snippet['obj_id'] === (int) $a_sub_id) {
534  return $snippet['title'];
535  }
536  }
537  return '';
538  }
539 }
Class ilObjFolder.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
__construct(int $a_id=0, bool $a_call_by_reference=true)
DataFactory $data_factory
static lookupForeignId(int $a_id)
setForTranslation(bool $a_val)
ilTree $tree
setTreeTablePK(string $a_column_name)
set column containing primary key in tree table
static getAdvMDSubItemTitle(int $a_obj_id, string $a_sub_type, int $a_sub_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insertInTree(int $a_obj_id, ?int $a_parent=null)
Insert into tree.
getChilds(int $obj_id=0, string $a_type="")
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setTableNames(string $a_table_tree, string $a_table_obj_data, string $a_table_obj_reference="")
set table names The primary key of the table containing your object_data must be &#39;obj_id&#39; You may use...
getChildsExceptFolders(int $obj_id=0)
cloneMetaData(ilObject $target_obj)
Copy meta data.
createFolder(string $a_title, int $a_parent=0)
Interface for repository objects to use adv md with subitems.
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilLanguage $lng
ilDBInterface $db
static getAllMobIds(int $a_id)
global $DIC
Definition: shib_login.php:26
static isForeignIdInTree(int $a_pool_id, int $a_foreign_id)
Check whether foreign id is in tree.
ExportFactoryInterface $export_factory
getParentId(int $obj_id=0)
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...
static _getPoolTree(int $a_obj_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
deleteChild(int $obj_id)
Delete a child of media tree.
__construct(Container $dic, ilPlugin $plugin)
setDefaultWidth(?int $a_val)
static _lookupType(int $id, bool $reference=false)
copyTreeContent(ilObjMediaPool $a_new_obj, int $a_target_parent, int $a_source_parent)
static isItemIdInTree(int $a_pool_id, int $a_item_id)
Check whether a mep item id is in the media pool.
setDefaultHeight(?int $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...