ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjMediaPool.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "./Services/Object/classes/class.ilObject.php";
5 require_once "./Services/MetaData/classes/class.ilMDLanguageItem.php";
6 require_once("./Modules/Folder/classes/class.ilObjFolder.php");
7 require_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
8 
21 class ilObjMediaPool extends ilObject
22 {
23  var $tree;
24 
31  function ilObjMediaPool($a_id = 0,$a_call_by_reference = true)
32  {
33  // this also calls read() method! (if $a_id is set)
34  $this->type = "mep";
35  $this->ilObject($a_id,$a_call_by_reference);
36  }
37 
43  function setDefaultWidth($a_val)
44  {
45  $this->default_width = $a_val;
46  }
47 
53  function getDefaultWidth()
54  {
55  return $this->default_width;
56  }
57 
63  function setDefaultHeight($a_val)
64  {
65  $this->default_height = $a_val;
66  }
67 
73  function getDefaultHeight()
74  {
75  return $this->default_height;
76  }
77 
81  function read()
82  {
83  global $ilDB;
84 
85  parent::read();
86 
87  $set = $ilDB->query("SELECT * FROM mep_data ".
88  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
89  );
90  if ($rec = $ilDB->fetchAssoc($set))
91  {
92  $this->setDefaultWidth($rec["default_width"]);
93  $this->setDefaultHeight($rec["default_height"]);
94  }
95  $this->tree = ilObjMediaPool::getPoolTree($this->getId());
96  }
97 
98 
106  static function getPoolTree($a_obj_id)
107  {
108  $tree = new ilTree($a_obj_id);
109  $tree->setTreeTablePK("mep_id");
110  $tree->setTableNames("mep_tree", "mep_item");
111 
112  return $tree;
113  }
114 
118  function create()
119  {
120  global $ilDB;
121 
122  parent::create();
123 
124  $ilDB->manipulate("INSERT INTO mep_data ".
125  "(id, default_width, default_height) VALUES (".
126  $ilDB->quote($this->getId(), "integer").", ".
127  $ilDB->quote($this->getDefaultWidth(), "integer").", ".
128  $ilDB->quote($this->getDefaultHeight(), "integer").
129  ")");
130 
131  $this->createMepTree();
132  }
133 
140  function createMepTree()
141  {
142  // create media pool tree
143  $this->tree = new ilTree($this->getId());
144  $this->tree->setTreeTablePK("mep_id");
145  $this->tree->setTableNames('mep_tree','mep_item');
146  $this->tree->addTree($this->getId(), 1);
147  }
148 
149 
153  function &getTree()
154  {
155  return $this->tree;
156  }
157 
164  function update()
165  {
166  global $ilDB;
167 
168  if (!parent::update())
169  {
170  return false;
171  }
172 
173  // put here object specific stuff
174  $ilDB->manipulate("UPDATE mep_data SET ".
175  " default_width = ".$ilDB->quote($this->getDefaultWidth(), "integer").",".
176  " default_height = ".$ilDB->quote($this->getDefaultHeight(), "integer").
177  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
178  );
179 
180  return true;
181  }
182 
183 
194  function delete()
195  {
196  // always call parent delete function first!!
197  if (!parent::delete())
198  {
199  return false;
200  }
201 
202  // get childs
203  $childs = $this->tree->getSubTree($this->tree->getNodeData($this->tree->readRootId()));
204 
205  // delete tree
206  $this->tree->removeTree($this->tree->getTreeId());
207 
208  // delete childs
209  foreach ($childs as $child)
210  {
211  $fid = ilMediaPoolItem::lookupForeignId($child["obj_id"]);
212  switch ($child["type"])
213  {
214  case "mob":
215  if (ilObject::_lookupType($fid) == "mob")
216  {
217  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
218  $mob = new ilObjMediaObject($fid);
219  $mob->delete();
220  }
221  break;
222 
223 /* case "fold":
224  if (ilObject::_lookupType($fid) == "fold")
225  {
226  include_once("./Modules/Folder/classes/class.ilObjFolder.php");
227  $fold = new ilObjFolder($fid, false);
228  $fold->delete();
229  }
230  break;*/
231  }
232  }
233 
234  return true;
235  }
236 
246  function initDefaultRoles()
247  {
248  global $rbacadmin;
249 
250  // create a local role folder
251  //$rfoldObj = $this->createRoleFolder("Local roles","Role Folder of forum obj_no.".$this->getId());
252 
253  // create moderator role and assign role to rolefolder...
254  //$roleObj = $rfoldObj->createRole("Moderator","Moderator of forum obj_no.".$this->getId());
255  //$roles[] = $roleObj->getId();
256 
257  //unset($rfoldObj);
258  //unset($roleObj);
259 
260  return $roles ? $roles : array();
261  }
262 
276  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
277  {
278  global $tree;
279 
280  switch ($a_event)
281  {
282  case "link":
283 
284  //var_dump("<pre>",$a_params,"</pre>");
285  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
286  //exit;
287  break;
288 
289  case "cut":
290 
291  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
292  //exit;
293  break;
294 
295  case "copy":
296 
297  //var_dump("<pre>",$a_params,"</pre>");
298  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
299  //exit;
300  break;
301 
302  case "paste":
303 
304  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
305  //exit;
306  break;
307 
308  case "new":
309 
310  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
311  //exit;
312  break;
313  }
314 
315  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
316  if ($a_node_id==$_GET["ref_id"])
317  {
318  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
319  $parent_type = $parent_obj->getType();
320  if($parent_type == $this->getType())
321  {
322  $a_node_id = (int) $tree->getParentId($a_node_id);
323  }
324  }
325 
326  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
327  }
328 
329 
333  function getChilds($obj_id = "", $a_type = "")
334  {
335  $objs = array();
336  $mobs = array();
337  $pgs = array();
338  if ($obj_id == "")
339  {
340  $obj_id = $this->tree->getRootId();
341  }
342 
343  if ($a_type == "fold" || $a_type == "")
344  {
345  $objs = $this->tree->getChildsByType($obj_id, "fold");
346  }
347  if ($a_type == "mob" || $a_type == "")
348  {
349  $mobs = $this->tree->getChildsByType($obj_id, "mob");
350  }
351  foreach($mobs as $key => $mob)
352  {
353  $objs[] = $mob;
354  }
355  if ($a_type == "pg" || $a_type == "")
356  {
357  $pgs = $this->tree->getChildsByType($obj_id, "pg");
358  }
359  foreach($pgs as $key => $pg)
360  {
361  $objs[] = $pg;
362  }
363 
364  return $objs;
365  }
366 
370  function getChildsExceptFolders($obj_id = "")
371  {
372  $objs = array();
373  $mobs = array();
374  if ($obj_id == "")
375  {
376  $obj_id = $this->tree->getRootId();
377  }
378 
379  $objs = $this->tree->getFilteredChilds(array("fold", "dummy"), $obj_id);
380  return $objs;
381  }
382 
386  function getMediaObjects($a_title_filter = "", $a_format_filter = "", $a_keyword_filter = '', $a_caption_filter)
387  {
388  global $ilDB;
389 
390  $query = "SELECT DISTINCT mep_tree.*, object_data.* ".
391  "FROM mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
392  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ";
393 
394  if ($a_format_filter != "" or $a_caption_filter != '')
395  {
396  $query.= " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ";
397  }
398 
399  $query .=
400  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
401  " AND object_data.type = ".$ilDB->quote("mob", "text");
402 
403  // filter
404  if (trim($a_title_filter) != "") // title
405  {
406  $query.= " AND ".$ilDB->like("object_data.title", "text", "%".trim($a_title_filter)."%");
407  }
408  if ($a_format_filter != "") // format
409  {
410  $filter = ($a_format_filter == "unknown")
411  ? ""
412  : $a_format_filter;
413  $query.= " AND ".$ilDB->equals("media_item.format", $filter, "text", true);
414  }
415  if(trim($a_caption_filter))
416  {
417  $query .= 'AND '.$ilDB->like('media_item.caption', 'text', '%'.trim($a_caption_filter).'%');
418  }
419 
420  $query.=
421  " ORDER BY object_data.title";
422 
423  $objs = array();
424  $set = $ilDB->query($query);
425  while ($rec = $ilDB->fetchAssoc($set))
426  {
427  $rec["foreign_id"] = $rec["obj_id"];
428  $rec["obj_id"] = "";
429  $objs[] = $rec;
430  }
431 
432  // Keyword filter
433  if($a_keyword_filter)
434  {
435  include_once './Services/MetaData/classes/class.ilMDKeyword.php';
436  $res = ilMDKeyword::_searchKeywords($a_keyword_filter,'mob',0);
437 
438  foreach($objs as $obj)
439  {
440  if(in_array($obj['foreign_id'],$res))
441  {
442  $filtered[] = $obj;
443  }
444  }
445  return (array) $filtered;
446  }
447  return $objs;
448  }
449 
453  function getAllMobIds($a_id)
454  {
455  global $ilDB;
456 
457  $query = "SELECT foreign_id as id FROM ".
458  " mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
459  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
460  " WHERE mep_tree.mep_id = ".$ilDB->quote($a_id, "integer").
461  " AND mep_item.type = ".$ilDB->quote("mob", "text").
462  " AND object_data.type = ".$ilDB->quote("mob", "text");
463  $set = $ilDB->query($query);
464  $ids = array();
465  while ($rec = $ilDB->fetchAssoc($set))
466  {
467  $ids[] = $rec["id"];
468  }
469  return $ids;
470  }
471 
475  function getUsedFormats()
476  {
477  global $ilDB, $lng;
478 
479  $query = "SELECT DISTINCT media_item.format f FROM mep_tree ".
480  " JOIN mep_item ON (mep_item.obj_id = mep_tree.child) ".
481  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
482  " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ".
483  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
484  " AND object_data.type = ".$ilDB->quote("mob", "text").
485  " ORDER BY f";
486  $formats = array();
487  $set = $ilDB->query($query);
488  while ($rec = $ilDB->fetchAssoc($set))
489  {
490  if ($rec["f"] != "")
491  {
492  $formats[$rec["f"]] = $rec["f"];
493  }
494  else
495  {
496  $formats["unknown"] = $lng->txt("mep_unknown");
497  }
498  }
499 
500  return $formats;
501  }
502 
503  function getParentId($obj_id = "")
504  {
505  if ($obj_id == "")
506  {
507  return false;
508  }
509  if ($obj_id == $this->tree->getRootId())
510  {
511  return false;
512  }
513 
514  return $this->tree->getParentId($obj_id);
515  }
516 
522  function insertInTree($a_obj_id, $a_parent = "")
523  {
524  if (!$this->tree->isInTree($a_obj_id))
525  {
526  $parent = ($a_parent == "")
527  ? $this->tree->getRootId()
528  : $a_parent;
529  $this->tree->insertNode($a_obj_id, $parent);
530  return true;
531  }
532  else
533  {
534  return false;
535  }
536  }
537 
538 
543  function deleteChild($obj_id)
544  {
545  $fid = ilMediaPoolItem::lookupForeignId($obj_id);
548 
549  $node_data = $this->tree->getNodeData($obj_id);
550  $subtree = $this->tree->getSubtree($node_data);
551 
552  // delete tree
553  if($this->tree->isInTree($obj_id))
554  {
555  $this->tree->deleteTree($node_data);
556  }
557 
558  // delete objects
559  foreach ($subtree as $node)
560  {
561  $fid = ilMediaPoolItem::lookupForeignId($node["child"]);
562  if ($node["type"] == "mob")
563  {
564  if (ilObject::_lookupType($fid) == "mob")
565  {
566  $obj =& new ilObjMediaObject($fid);
567  $obj->delete();
568  }
569  }
570 
571  if ($node["type"] == "fold")
572  {
573  if ($fid > 0 && ilObject::_lookupType($fid) == "fold")
574  {
575  $obj = new ilObjFolder($fid, false);
576  $obj->delete();
577  }
578  }
579 
580  if ($node["type"] == "pg")
581  {
582  include_once("./Modules/MediaPool/classes/class.ilMediaPoolPage.php");
583  if (ilMediaPoolPage::_exists($node["child"]))
584  {
585  $pg = new ilMediaPoolPage($node["child"]);
586  $pg->delete();
587  }
588  }
589 
590  include_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
591  $item = new ilMediaPoolItem($node["child"]);
592  $item->delete();
593  }
594  }
595 
602  static function isForeignIdInTree($a_pool_id, $a_foreign_id)
603  {
604  global $ilDB;
605 
606  $set = $ilDB->query("SELECT * FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE ".
607  " foreign_id = ".$ilDB->quote($a_foreign_id, "integer").
608  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer")
609  );
610  if ($rec = $ilDB->fetchAssoc($set))
611  {
612  return true;
613  }
614  return false;
615  }
616 
620  static function isItemIdInTree($a_pool_id, $a_item_id)
621  {
622  global $ilDB;
623 
624  $set = $ilDB->query("SELECT * FROM mep_tree WHERE child = ".
625  $ilDB->quote($a_item_id, "integer").
626  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer"));
627  if ($rec = $ilDB->fetchAssoc($set))
628  {
629  return true;
630  }
631  return false;
632  }
633 
640  function createFolder($a_title, $a_parent = 0)
641  {
642  // perform save
643  $mep_item = new ilMediaPoolItem();
644  $mep_item->setTitle($a_title);
645  $mep_item->setType("fold");
646  $mep_item->create();
647  if ($mep_item->getId() > 0)
648  {
649  $tree = $this->getTree();
650  $parent = $a_parent > 0
651  ? $a_parent
652  : $tree->getRootId();
653  $this->insertInTree($mep_item->getId(), $parent);
654  return $mep_item->getId();
655  }
656  return false;
657  }
658 
665  public function cloneObject($a_target_id,$a_copy_id = 0)
666  {
667  global $ilDB, $ilUser, $ilias;
668 
669  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
670 
671  $new_obj->setTitle($this->getTitle());
672  $new_obj->setDescription($this->getDescription());
673  $new_obj->setDefaultWidth($this->getDefaultWidth());
674  $new_obj->setDefaultHeight($this->getDefaultHeight());
675  $new_obj->update();
676 
677  // copy content
678  $this->copyTreeContent($new_obj, $new_obj->getTree()->readRootId(),
679  $this->getTree()->readRootId());
680 
681  return $new_obj;
682  }
683 
690  function copyTreeContent($a_new_obj, $a_target_parent, $a_source_parent)
691  {
692  include_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
693  include_once("./Modules/MediaPool/classes/class.ilMediaPoolPage.php");
694  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
695 
696  // get all chapters of root lm
697  $nodes = $this->getTree()->getChilds($a_source_parent);
698  foreach ($nodes as $node)
699  {
700  $item = new ilMediaPoolItem();
701  $item->setType($node["type"]);
702  switch ($node["type"])
703  {
704  case "mob":
705  $mob_id = ilMediaPoolItem::lookupForeignId($node["child"]);
706  $mob = new ilObjMediaObject($mob_id);
707  $new_mob = $mob->duplicate();
708  $item->setForeignId($new_mob->getId());
709  $item->setTitle($new_mob->getTitle());
710  $item->create();
711  break;
712 
713  case "pg":
714  $item->setTitle($node["title"]);
715  $item->create();
716  $page = new ilMediaPoolPage($node["child"]);
717  $new_page = new ilMediaPoolPage();
718  $new_page->setId($item->getId());
719  $new_page->create();
720 
721  // todo: make mobs being copied
722  $new_page->setXMLContent($page->copyXMLContent(true));
723  $new_page->buildDom();
724  $new_page->update();
725  break;
726 
727  case "fold":
728  $item->setTitle($node["title"]);
729  $item->create();
730  break;
731  }
732 
733  // insert item into tree
734  $a_new_obj->insertInTree($item->getId(), $a_target_parent);
735 
736  // handle childs
737  $this->copyTreeContent($a_new_obj, $item->getId(), $node["child"]);
738  }
739  }
740 
741 }
742 ?>