ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjMediaObject.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 define ("IL_MODE_ALIAS", 1);
5 define ("IL_MODE_OUTPUT", 2);
6 define ("IL_MODE_FULL", 3);
7 
8 require_once("./Services/MediaObjects/classes/class.ilMediaItem.php");
9 include_once "./Services/Object/classes/class.ilObject.php";
10 
27 {
28  var $is_alias;
30  var $id;
33 
38  function ilObjMediaObject($a_id = 0)
39  {
40  $this->is_alias = false;
41  $this->media_items = array();
42  $this->contains_int_link = false;
43  $this->type = "mob";
44  parent::ilObject($a_id, false);
45  }
46 
47  function setRefId()
48  {
49  $this->ilias->raiseError("Operation ilObjMedia::setRefId() not allowed.",$this->ilias->error_obj->FATAL);
50  }
51 
52  function getRefId()
53  {
54  return false;
55  }
56 
57  function putInTree()
58  {
59  $this->ilias->raiseError("Operation ilObjMedia::putInTree() not allowed.",$this->ilias->error_obj->FATAL);
60  }
61 
62  function createReference()
63  {
64  $this->ilias->raiseError("Operation ilObjMedia::createReference() not allowed.",$this->ilias->error_obj->FATAL);
65  }
66 
67  function setTitle($a_title)
68  {
69  parent::setTitle($a_title);
70  }
71 
72  function getTitle()
73  {
74  return parent::getTitle();
75  }
76 
84  function _exists($a_id)
85  {
86  global $ilDB;
87 
88  include_once("./Services/COPage/classes/class.ilInternalLink.php");
89  if (is_int(strpos($a_id, "_")))
90  {
92  }
93 
94  return parent::_exists($a_id, false);
95  }
96 
100  function delete()
101  {
102  if (!($this->getId() > 0))
103  {
104  return;
105  }
106 
107  $usages = $this->getUsages();
108 
109  if (count($usages) == 0)
110  {
111  // remove directory
113 
114  // remove thumbnail directory
116 
117  // delete meta data of mob
118  $this->deleteMetaData();
119 
120  // delete media items
122 
123  // delete object
124  parent::delete();
125  }
126  }
127 
133  function getDescription()
134  {
135  return parent::getDescription();
136  }
137 
141  function setDescription($a_description)
142  {
143  parent::setDescription($a_description);
144  }
145 
157  function MDUpdateListener($a_element)
158  {
159  include_once 'Services/MetaData/classes/class.ilMD.php';
160 
161  switch($a_element)
162  {
163  case 'General':
164 
165  // Update Title and description
166  $md = new ilMD(0, $this->getId(), $this->getType());
167  $md_gen = $md->getGeneral();
168 
169  if (is_object($md_gen))
170  {
171  ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
172  $this->setTitle($md_gen->getTitle());
173 
174  foreach($md_gen->getDescriptionIds() as $id)
175  {
176  $md_des = $md_gen->getDescription($id);
177  ilObject::_writeDescription($this->getId(),$md_des->getDescription());
178  $this->setDescription($md_des->getDescription());
179  break;
180  }
181  }
182 
183  break;
184 
185  default:
186  }
187  return true;
188  }
189 
193  function createMetaData()
194  {
195  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
196 
197  global $ilUser;
198 
199  $md_creator = new ilMDCreator(0, $this->getId(), $this->getType());
200  $md_creator->setTitle($this->getTitle());
201  $md_creator->setTitleLanguage($ilUser->getPref('language'));
202  $md_creator->setDescription($this->getDescription());
203  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
204  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
205  $md_creator->setLanguage($ilUser->getPref('language'));
206  $md_creator->create();
207 
208  return true;
209  }
210 
214  function updateMetaData()
215  {
216  include_once("Services/MetaData/classes/class.ilMD.php");
217  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
218  include_once("Services/MetaData/classes/class.ilMDDescription.php");
219 
220  $md =& new ilMD(0, $this->getId(), $this->getType());
221  $md_gen =& $md->getGeneral();
222  $md_gen->setTitle($this->getTitle());
223 
224  // sets first description (maybe not appropriate)
225  $md_des_ids =& $md_gen->getDescriptionIds();
226  if (count($md_des_ids) > 0)
227  {
228  $md_des =& $md_gen->getDescription($md_des_ids[0]);
229  $md_des->setDescription($this->getDescription());
230  $md_des->update();
231  }
232  $md_gen->update();
233 
234  }
235 
239  function deleteMetaData()
240  {
241  // Delete meta data
242  include_once('Services/MetaData/classes/class.ilMD.php');
243  $md = new ilMD(0, $this->getId(), $this->getType());
244  $md->deleteAll();
245  }
246 
247 
253  function addMediaItem($a_item)
254  {
255  $this->media_items[] = $a_item;
256  }
257 
258 
264  function &getMediaItems()
265  {
266  return $this->media_items;
267  }
268 
275  function &getMediaItem($a_purpose)
276  {
277  foreach ($this->media_items as $media_item)
278  {
279  if($media_item->getPurpose() == $a_purpose)
280  {
281  return $media_item;
282  }
283  }
284  return false;
285  }
286 
287 
291  function removeMediaItem($a_purpose)
292  {
293  foreach ($this->media_items as $key => $media_item)
294  {
295  if($media_item->getPurpose() == $a_purpose)
296  {
297  unset($this->media_items[$key]);
298  }
299  }
300  // update numbers and keys
301  $i = 1;
302  $media_items = array();
303  foreach ($this->media_items as $media_item)
304  {
305  $media_items [$i] = $media_item;
306  $media_item->setMobId($this->getId());
307  $media_item->setNr($i);
308  $i++;
309  }
310  $this->media_items = $media_items;
311  }
312 
317  {
318  $this->media_items = array();
319  }
320 
321 
322  function getMediaItemNr($a_purpose)
323  {
324  for($i=0; $i<count($this->media_items); $i++)
325  {
326  if($this->media_items[$i]->getPurpose() == $a_purpose)
327  {
328  return $i + 1;
329  }
330  }
331  return false;
332  }
333 
334 
335  function hasFullscreenItem()
336  {
337  return $this->hasPurposeItem("Fullscreen");
338  }
339 
346  function hasPurposeItem($purpose)
347  {
348  if(is_object($this->getMediaItem($purpose)))
349  {
350  return true;
351  }
352  else
353  {
354  return false;
355  }
356  }
357 
358 
359 
363  function read()
364  {
365 //echo "<br>ilObjMediaObject:read";
366  parent::read();
367 
368  // get media items
370  }
371 
375  function setId($a_id)
376  {
377  $this->id = $a_id;
378  }
379 
380  function getId()
381  {
382  return $this->id;
383  }
384 
388  function setAlias($a_is_alias)
389  {
390  $this->is_alias = $a_is_alias;
391  }
392 
393  function isAlias()
394  {
395  return $this->is_alias;
396  }
397 
398  function setOriginID($a_id)
399  {
400  return $this->origin_id = $a_id;
401  }
402 
403  function getOriginID()
404  {
405  return $this->origin_id;
406  }
407 
408  /*
409  function getimportId()
410  {
411  return $this->meta_data->getImportIdentifierEntryID();
412  }*/
413 
414 
418  function getImportId()
419  {
420  return $this->import_id;
421  }
422 
423  function setImportId($a_id)
424  {
425  $this->import_id = $a_id;
426  }
427 
431  function create($a_create_meta_data = false, $a_save_media_items = true)
432  {
433  parent::create();
434 
435  if (!$a_create_meta_data)
436  {
437  $this->createMetaData();
438  }
439 
440  if ($a_save_media_items)
441  {
442  $media_items =& $this->getMediaItems();
443  for($i=0; $i<count($media_items); $i++)
444  {
445  $item =& $media_items[$i];
446  $item->setMobId($this->getId());
447  $item->setNr($i+1);
448  $item->create();
449  }
450  }
451 
452  }
453 
454 
458  function update($a_upload=false)
459  {
460  parent::update();
461  if(!$a_upload)
462  {
463  $this->updateMetaData();
464  }
466 
467  // iterate all items
468  $media_items =& $this->getMediaItems();
469  $j = 1;
470  foreach($media_items as $key => $val)
471  {
472  $item =& $media_items[$key];
473  if (is_object($item))
474  {
475  $item->setMobId($this->getId());
476  $item->setNr($j);
477  if ($item->getLocationType() == "Reference")
478  {
479  $item->extractUrlParameters();
480  }
481  $item->create();
482  $j++;
483  }
484  }
485  }
486 
492  function _getDirectory($a_mob_id)
493  {
494  return ilUtil::getWebspaceDir()."/mobs/mm_".$a_mob_id;
495  }
496 
502  function _getURL($a_mob_id)
503  {
504  return ilUtil::getHtmlPath(ilUtil::getWebspaceDir()."/mobs/mm_".$a_mob_id);
505  }
506 
512  function _getThumbnailDirectory($a_mob_id, $a_mode = "filesystem")
513  {
514  return ilUtil::getWebspaceDir($a_mode)."/thumbs/mm_".$a_mob_id;
515  }
516 
522  static function _lookupStandardItemPath($a_mob_id, $a_url_encode = false,
523  $a_web = true)
524  {
525  return ilObjMediaObject::_lookupItemPath($a_mob_id, $a_url_encode, $a_web, "Standard");
526  }
527 
533  static function _lookupItemPath($a_mob_id, $a_url_encode = false,
534  $a_web = true, $a_purpose = "")
535  {
536  if ($a_purpose == "")
537  {
538  $a_purpose = "Standard";
539  }
540  $location = ilMediaItem::_lookupLocationForMobId($a_mob_id, $a_purpose);
541  if (preg_match("/https?\:/i",$location))
542  return $location;
543 
544  if ($a_url_encode)
545  $location = rawurlencode($location);
546 
547  $path = ($a_web)
548  ? ILIAS_HTTP_PATH
549  : ".";
550 
551  return $path."/data/".CLIENT_ID."/mobs/mm_".$a_mob_id."/".$location;
552  }
553 
557  function createDirectory()
558  {
560  }
561 
565  function _createThumbnailDirectory($a_obj_id)
566  {
568  ilUtil::createDirectory(ilUtil::getWebspaceDir()."/thumbs/mm_".$a_obj_id);
569  }
570 
577  function getFilesOfDirectory($a_subdir = "")
578  {
579  $a_subdir = str_replace("..", "", $a_subdir);
580  $dir = ilObjMediaObject::_getDirectory($this->getId());
581  if ($a_subdir != "")
582  {
583  $dir.= "/".$a_subdir;
584  }
585 
586  $files = array();
587  if (is_dir($dir))
588  {
589  $entries = ilUtil::getDir($dir);
590  foreach ($entries as $e)
591  {
592  if (is_file($dir."/".$e["entry"]) && $e["entry"] != "." && $e["entry"] != "..")
593  {
594  $files[] = $e["entry"];
595  }
596  }
597  }
598 
599  return $files;
600  }
601 
602 
607  function getXML($a_mode = IL_MODE_FULL, $a_inst = 0)
608  {
609  // TODO: full implementation of all parameters
610 //echo "-".$a_mode."-";
611  switch ($a_mode)
612  {
613  case IL_MODE_ALIAS:
614  $xml = "<MediaObject>";
615  $xml .= "<MediaAlias OriginId=\"il__mob_".$this->getId()."\"/>";
616  $media_items =& $this->getMediaItems();
617  for($i=0; $i<count($media_items); $i++)
618  {
619  $item =& $media_items[$i];
620  $xml .= "<MediaAliasItem Purpose=\"".$item->getPurpose()."\">";
621 
622  // Layout
623  $width = ($item->getWidth() != "")
624  ? "Width=\"".$item->getWidth()."\""
625  : "";
626  $height = ($item->getHeight() != "")
627  ? "Height=\"".$item->getHeight()."\""
628  : "";
629  $halign = ($item->getHAlign() != "")
630  ? "HorizontalAlign=\"".$item->getHAlign()."\""
631  : "";
632  $xml .= "<Layout $width $height $halign />";
633 
634  // Caption
635  if ($item->getCaption() != "")
636  {
637  $xml .= "<Caption Align=\"bottom\">".
638  str_replace("&", "&amp;", $item->getCaption())."</Caption>";
639  }
640 
641  // Text Representation
642  if ($item->getTextRepresentation() != "")
643  {
644  $xml .= "<TextRepresentation>".
645  str_replace("&", "&amp;", $item->getTextRepresentation())."</TextRepresentation>";
646  }
647 
648  // Parameter
649  $parameters = $item->getParameters();
650  foreach ($parameters as $name => $value)
651  {
652  $xml .= "<Parameter Name=\"$name\" Value=\"$value\"/>";
653  }
654  $xml .= $item->getMapAreasXML();
655  $xml .= "</MediaAliasItem>";
656  }
657  break;
658 
659  // for output we need technical sections of meta data
660  case IL_MODE_OUTPUT:
661 
662  // get first technical section
663 // $meta =& $this->getMetaData();
664  $xml = "<MediaObject Id=\"il__mob_".$this->getId()."\">";
665 
666  $media_items =& $this->getMediaItems();
667  for($i=0; $i<count($media_items); $i++)
668  {
669  $item =& $media_items[$i];
670 
671  $xml .= "<MediaItem Purpose=\"".$item->getPurpose()."\">";
672 
673  // Location
674  $xml.= "<Location Type=\"".$item->getLocationType()."\">".
675  $this->handleAmps($item->getLocation())."</Location>";
676 
677  // Format
678  $xml.= "<Format>".$item->getFormat()."</Format>";
679 
680  // Layout
681  $width = ($item->getWidth() != "")
682  ? "Width=\"".$item->getWidth()."\""
683  : "";
684  $height = ($item->getHeight() != "")
685  ? "Height=\"".$item->getHeight()."\""
686  : "";
687  $halign = ($item->getHAlign() != "")
688  ? "HorizontalAlign=\"".$item->getHAlign()."\""
689  : "";
690  $xml .= "<Layout $width $height $halign />";
691 
692  // Caption
693  if ($item->getCaption() != "")
694  {
695  $xml .= "<Caption Align=\"bottom\">".
696  str_replace("&", "&amp;", $item->getCaption())."</Caption>";
697  }
698 
699  // Text Representation
700  if ($item->getTextRepresentation() != "")
701  {
702  $xml .= "<TextRepresentation>".
703  str_replace("&", "&amp;", $item->getTextRepresentation())."</TextRepresentation>";
704  }
705 
706  // Parameter
707  $parameters = $item->getParameters();
708  foreach ($parameters as $name => $value)
709  {
710  $xml .= "<Parameter Name=\"$name\" Value=\"$value\"/>";
711  }
712  $xml .= $item->getMapAreasXML();
713  $xml .= "</MediaItem>";
714  }
715  break;
716 
717  // full xml for export
718  case IL_MODE_FULL:
719 
720 // $meta =& $this->getMetaData();
721  $xml = "<MediaObject>";
722 
723  // meta data
724  include_once("Services/MetaData/classes/class.ilMD2XML.php");
725  $md2xml = new ilMD2XML(0, $this->getId(), $this->getType());
726  $md2xml->setExportMode(true);
727  $md2xml->startExport();
728  $xml.= $md2xml->getXML();
729 
730  $media_items =& $this->getMediaItems();
731  for($i=0; $i<count($media_items); $i++)
732  {
733  $item =& $media_items[$i];
734 
735  // highlight mode
736  $xml .= "<MediaItem Purpose=\"".$item->getPurpose()."\">";
737 
738  // Location
739  $xml.= "<Location Type=\"".$item->getLocationType()."\">".
740  $this->handleAmps($item->getLocation())."</Location>";
741 
742  // Format
743  $xml.= "<Format>".$item->getFormat()."</Format>";
744 
745  // Layout
746  $width = ($item->getWidth() != "")
747  ? "Width=\"".$item->getWidth()."\""
748  : "";
749  $height = ($item->getHeight() != "")
750  ? "Height=\"".$item->getHeight()."\""
751  : "";
752  $halign = ($item->getHAlign() != "")
753  ? "HorizontalAlign=\"".$item->getHAlign()."\""
754  : "";
755  $xml .= "<Layout $width $height $halign />";
756 
757  // Caption
758  if ($item->getCaption() != "")
759  {
760  $xml .= "<Caption Align=\"bottom\">".
761  str_replace("&", "&amp;", $item->getCaption())."</Caption>";
762  }
763 
764  // Text Representation
765  if ($item->getTextRepresentation() != "")
766  {
767  $xml .= "<TextRepresentation>".
768  str_replace("&", "&amp;", $item->getTextRepresentation())."</TextRepresentation>";
769  }
770 
771  // Parameter
772  $parameters = $item->getParameters();
773  foreach ($parameters as $name => $value)
774  {
775  $xml .= "<Parameter Name=\"$name\" Value=\"$value\"/>";
776  }
777  $xml .= $item->getMapAreasXML(true, $a_inst);
778  $xml .= "</MediaItem>";
779  }
780  break;
781  }
782  $xml .= "</MediaObject>";
783  return $xml;
784  }
785 
789  function handleAmps($a_str)
790  {
791  $a_str = str_replace("&amp;", "&", $a_str);
792  $a_str = str_replace("&", "&amp;", $a_str);
793  return $a_str;
794  }
795 
799  function exportXML(&$a_xml_writer, $a_inst = 0)
800  {
801  $a_xml_writer->appendXML($this->getXML(IL_MODE_FULL, $a_inst));
802  }
803 
804 
812  function exportFiles($a_target_dir)
813  {
814  $subdir = "il_".IL_INST_ID."_mob_".$this->getId();
815  ilUtil::makeDir($a_target_dir."/objects/".$subdir);
816 
817  $mobdir = ilUtil::getWebspaceDir()."/mobs/mm_".$this->getId();
818  ilUtil::rCopy($mobdir, $a_target_dir."/objects/".$subdir);
819 //echo "from:$mobdir:to:".$a_target_dir."/objects/".$subdir.":<br>";
820  }
821 
822  function exportMediaFullscreen($a_target_dir, $pg_obj)
823  {
824  $subdir = "il_".IL_INST_ID."_mob_".$this->getId();
825  $a_target_dir = $a_target_dir."/objects/".$subdir;
826  ilUtil::makeDir($a_target_dir);
827  $tpl = new ilTemplate("tpl.fullscreen.html", true, true, "Modules/LearningModule");
828  $tpl->setCurrentBlock("ilMedia");
829 
830  //$int_links = $page_object->getInternalLinks();
831  $med_links = ilMediaItem::_getMapAreasIntLinks($this->getId());
832 
833  // @todo
834  //$link_xml = $this->getLinkXML($med_links, $this->getLayoutLinkTargets());
835 
836  require_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
837  //$media_obj = new ilObjMediaObject($_GET["mob_id"]);
838  require_once("./Services/COPage/classes/class.ilPageObject.php");
839 
840  $xml = "<dummy>";
841  // todo: we get always the first alias now (problem if mob is used multiple
842  // times in page)
843  $xml.= $pg_obj->getMediaAliasElement($this->getId());
844  $xml.= $this->getXML(IL_MODE_OUTPUT);
845  //$xml.= $link_xml;
846  $xml.="</dummy>";
847 
848  //die(htmlspecialchars($xml));
849 
850  $xsl = file_get_contents("./Services/COPage/xsl/page.xsl");
851  $args = array( '/_xml' => $xml, '/_xsl' => $xsl );
852  $xh = xslt_create();
853 
854  //echo "<b>XML:</b>".htmlentities($xml);
855  // determine target frames for internal links
856  $wb_path = "";
857  $enlarge_path = "";
858  $params = array ('mode' => "fullscreen", 'enlarge_path' => $enlarge_path,
859  'link_params' => "ref_id=".$_GET["ref_id"],'fullscreen_link' => "",
860  'ref_id' => $_GET["ref_id"], 'webspace_path' => $wb_path);
861  $output = xslt_process($xh,"arg:/_xml","arg:/_xsl",NULL,$args, $params);
862  //echo xslt_error($xh);
863  xslt_free($xh);
864 
865  // unmask user html
866  include_once("./Services/MediaObjects/classes/class.ilPlayerUtil.php");
867  $tpl->setVariable("LOCATION_CONTENT_STYLESHEET", "../../css/style.css");
868  $tpl->setVariable("LOCATION_STYLESHEET", "../../css/system.css");
869  $tpl->setVariable("MEDIA_CONTENT", $output);
870  $output = $tpl->get();
871  //$output = preg_replace("/\/mobs\/mm_(\d+)\/([^\"]+)/i","$2",$output);
872  $output = preg_replace("/mobs\/mm_(\d+)\/([^\"]+)/i","$2",$output);
873  $output = preg_replace("/\.\/Services\/MediaObjects\/flash_mp3_player/i","../../players",$output);
874  $output = preg_replace("/\.\/".str_replace("/", "\/", ilPlayerUtil::getFlashVideoPlayerDirectory())."/i","../../players",$output);
875  $output = preg_replace("/file=..\/..\/..\//i","file=../objects/".$subdir."/",$output);
876  //die(htmlspecialchars($output));
877  fwrite(fopen($a_target_dir.'/fullscreen.html','w'), $output );
878  }
879 
880  function modifyExportIdentifier($a_tag, $a_param, $a_value)
881  {
882  if ($a_tag == "Identifier" && $a_param == "Entry")
883  {
884  $a_value = ilUtil::insertInstIntoID($a_value);
885  }
886 
887  return $a_value;
888  }
889 
890 
892  // EDIT METHODS: these methods act on the media alias in the dom
894 
901  function setContainsIntLink($a_contains_link)
902  {
903  $this->contains_int_link = $a_contains_link;
904  }
905 
910  function containsIntLink()
911  {
913  }
914 
918  function _deleteAllUsages($a_type, $a_id, $a_usage_hist_nr = 0)
919  {
920  global $ilDB;
921 
922  $q = "DELETE FROM mob_usage WHERE usage_type = ".
923  $ilDB->quote($a_type, "text").
924  " AND usage_id= ".$ilDB->quote($a_id, "integer").
925  " AND usage_hist_nr = ".$ilDB->quote($a_usage_hist_nr, "integer");
926  $ilDB->manipulate($q);
927  }
928 
932  function _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr = 0)
933  {
934  global $ilDB;
935 
936  $q = "SELECT * FROM mob_usage WHERE ".
937  "usage_type = ".$ilDB->quote($a_type, "text")." AND ".
938  "usage_id = ".$ilDB->quote($a_id, "integer")." AND ".
939  "usage_hist_nr = ".$ilDB->quote($a_usage_hist_nr, "integer");
940  $mobs = array();
941  $mob_set = $ilDB->query($q);
942  while($mob_rec = $ilDB->fetchAssoc($mob_set))
943  {
944  if (ilObject::_lookupType($mob_rec["id"]) == "mob")
945  {
946  $mobs[$mob_rec["id"]] = $mob_rec["id"];
947  }
948  }
949 
950  return $mobs;
951  }
952 
956  function _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr = 0)
957  {
958  global $ilDB;
959 
960  $q = "DELETE FROM mob_usage WHERE ".
961  " id = ".$ilDB->quote((int) $a_mob_id, "integer")." AND ".
962  " usage_type = ".$ilDB->quote($a_type, "text")." AND ".
963  " usage_id = ".$ilDB->quote((int) $a_id, "integer")." AND ".
964  " usage_hist_nr = ".$ilDB->quote((int) $a_usage_hist_nr, "integer");
965  $ilDB->manipulate($q);
966  $q = "INSERT INTO mob_usage (id, usage_type, usage_id, usage_hist_nr) VALUES".
967  " (".$ilDB->quote((int) $a_mob_id, "integer").",".
968  $ilDB->quote($a_type, "text").",".
969  $ilDB->quote((int) $a_id, "integer").",".
970  $ilDB->quote((int) $a_usage_hist_nr, "integer").")";
971  $ilDB->manipulate($q);
972  }
973 
977  function _removeUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr = 0)
978  {
979  global $ilDB;
980 
981  $q = "DELETE FROM mob_usage WHERE ".
982  " id = ".$ilDB->quote((int) $a_mob_id, "integer")." AND ".
983  " usage_type = ".$ilDB->quote($a_type, "text")." AND ".
984  " usage_id = ".$ilDB->quote((int) $a_id, "integer")." AND ".
985  " usage_hist_nr = ".$ilDB->quote((int) $a_usage_hist_nr, "integer");
986  $ilDB->manipulate($q);
987  }
988 
992  function getUsages($a_include_history = true)
993  {
994  return $this->lookupUsages($this->getId(), $a_include_history);
995  }
996 
1002  function lookupUsages($a_id, $a_include_history = true)
1003  {
1004  global $ilDB;
1005 
1006  $hist_str = "";
1007  if ($a_include_history)
1008  {
1009  $hist_str = ", usage_hist_nr";
1010  }
1011 
1012  // get usages in pages
1013  $q = "SELECT DISTINCT usage_type, usage_id".$hist_str." FROM mob_usage WHERE id = ".
1014  $ilDB->quote($a_id, "integer");
1015 
1016  if (!$a_include_history)
1017  {
1018  $q.= " AND usage_hist_nr = ".$ilDB->quote(0, "integer");
1019  }
1020 
1021  $us_set = $ilDB->query($q);
1022  $ret = array();
1023  while($us_rec = $ilDB->fetchAssoc($us_set))
1024  {
1025  $ut = "";
1026  if(is_int(strpos($us_rec["usage_type"], ":")))
1027  {
1028  $us_arr = explode(":", $us_rec["usage_type"]);
1029  $ut = $us_arr[1];
1030  $ct = $us_arr[0];
1031  }
1032 
1033  // check whether page exists
1034  $skip = false;
1035  if ($ut == "pg")
1036  {
1037  include_once("./Services/COPage/classes/class.ilPageObject.php");
1038  if (!ilPageObject::_exists($ct, $us_rec["usage_id"]))
1039  {
1040  $skip = true;
1041  }
1042  }
1043 
1044  if (!$skip)
1045  {
1046  $ret[] = array("type" => $us_rec["usage_type"],
1047  "id" => $us_rec["usage_id"],
1048  "hist_nr" => $us_rec["usage_hist_nr"]);
1049  }
1050  }
1051 
1052  // get usages in media pools
1053  $q = "SELECT DISTINCT mep_id FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE mep_item.foreign_id = ".
1054  $ilDB->quote($a_id, "integer")." AND mep_item.type = ".$ilDB->quote("mob", "text");
1055  $us_set = $ilDB->query($q);
1056  while($us_rec = $ilDB->fetchAssoc($us_set))
1057  {
1058  $ret[] = array("type" => "mep",
1059  "id" => $us_rec["mep_id"]);
1060  }
1061 
1062  // get usages in news items (media casts)
1063  include_once("./Services/News/classes/class.ilNewsItem.php");
1064  $news_usages = ilNewsItem::_lookupMediaObjectUsages($a_id);
1065  foreach($news_usages as $nu)
1066  {
1067  $ret[] = $nu;
1068  }
1069 
1070 
1071  // get usages in map areas
1072  $q = "SELECT DISTINCT mob_id FROM media_item it, map_area area ".
1073  " WHERE area.item_id = it.id ".
1074  " AND area.link_type = ".$ilDB->quote("int", "text")." ".
1075  " AND area.target = ".$ilDB->quote("il__mob_".$a_id, "text");
1076  $us_set = $ilDB->query($q);
1077  while($us_rec = $ilDB->fetchAssoc($us_set))
1078  {
1079  $ret[] = array("type" => "map",
1080  "id" => $us_rec["mob_id"]);
1081  }
1082 
1083  // get usages in personal clipboards
1084  $users = ilObjUser::_getUsersForClipboadObject("mob", $a_id);
1085  foreach ($users as $user)
1086  {
1087  $ret[] = array("type" => "clip",
1088  "id" => $user);
1089  }
1090 
1091  return $ret;
1092  }
1093 
1097  function getParentObjectIdForUsage($a_usage, $a_include_all_access_obj_ids = false)
1098  {
1099  if(is_int(strpos($a_usage["type"], ":")))
1100  {
1101  $us_arr = explode(":", $a_usage["type"]);
1102  $type = $us_arr[1];
1103  $cont_type = $us_arr[0];
1104  }
1105  else
1106  {
1107  $type = $a_usage["type"];
1108  }
1109 
1110  $id = $a_usage["id"];
1111  $obj_id = false;
1112 
1113  switch($type)
1114  {
1115  case "html": // "old" category pages
1116  if ($cont_type == "cat")
1117  {
1118  $obj_id = $id;
1119  }
1120  // Test InfoScreen Text
1121  if ($cont_type == "tst" || $cont_type == "svy")
1122  {
1123  $obj_id = $id;
1124  //var_dump($qinfo);
1125  }
1126  // Question Pool *Question* Text (Test)
1127  if ($cont_type == "qpl")
1128  {
1129  include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
1131  if ($qinfo["original_id"] > 0)
1132  {
1133  include_once("./Modules/Test/classes/class.ilObjTest.php");
1134  $obj_id = ilObjTest::_lookupTestObjIdForQuestionId($id); // usage in test
1135  }
1136  else
1137  {
1138  $obj_id = $qinfo["obj_fi"]; // usage in pool
1139  }
1140  }
1141 
1142  // Question Pool *Question* Text (Survey)
1143  if ($cont_type == "spl")
1144  {
1145  include_once("./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php");
1147  if($quest)
1148  {
1149  if ($quest->getOriginalId() > 0)
1150  {
1151  $obj_id = $quest->getSurveyId();
1152  }
1153  else
1154  {
1155  $obj_id = $quest->getObjId(); // usage in pool
1156  }
1157  unset($quest);
1158  }
1159  }
1160 
1161  // Forum
1162  if ($cont_type == "frm")
1163  {
1164  $post_pk = $a_usage['id'];
1165  include_once 'Modules/Forum/classes/class.ilForumPost.php';
1166  include_once 'Modules/Forum/classes/class.ilForum.php';
1167  $oPost = new ilForumPost($post_pk);
1168  $frm_pk = $oPost->getForumId();
1169 
1170  $obj_id = ilForum::_lookupObjIdForForumId($frm_pk);
1171  }
1172 
1173  if ($cont_type == 'frm~')
1174  {
1175  $obj_id = $a_usage['id'];
1176  }
1177 
1178  if ($cont_type == "dcl")
1179  {
1180  $obj_id = $id;
1181  }
1182 
1183  break;
1184 
1185  case "pg":
1186 
1187  // Question Pool Question Pages
1188  if ($cont_type == "qpl")
1189  {
1190  include_once("./Modules/TestQuestionPool/classes/class.assQuestion.php");
1192  if ($qinfo["original_id"] > 0)
1193  {
1194  include_once("./Modules/Test/classes/class.ilObjTest.php");
1195  $obj_id = ilObjTest::_lookupTestObjIdForQuestionId($id); // usage in test
1196  }
1197  else
1198  {
1199  $obj_id = $qinfo["obj_fi"]; // usage in pool
1200  }
1201  }
1202 
1203  // learning modules
1204  if ($cont_type == "lm" || $cont_type == "dbk")
1205  {
1206  include_once("./Modules/LearningModule/classes/class.ilLMObject.php");
1207  $obj_id = ilLMObject::_lookupContObjID($id);
1208  }
1209 
1210  // glossary definition
1211  if ($cont_type == "gdf")
1212  {
1213  include_once("./Modules/Glossary/classes/class.ilGlossaryDefinition.php");
1214  include_once("./Modules/Glossary/classes/class.ilGlossaryTerm.php");
1216  $obj_id = ilGlossaryTerm::_lookGlossaryID($term_id);
1217  }
1218 
1219  // wiki page
1220  if ($cont_type == 'wpg')
1221  {
1222  include_once 'Modules/Wiki/classes/class.ilWikiPage.php';
1224  }
1225 
1226  // sahs page
1227  if ($cont_type == 'sahs')
1228  {
1229  // can this implementation be used for other content types, too?
1230  include_once('./Services/COPage/classes/class.ilPageObject.php');
1231  $obj_id = ilPageObject::lookupParentId($id, 'sahs');
1232  }
1233 
1234  // repository pages
1235  if (in_array($cont_type, array("crs", "grp", "cat", "fold", "root")))
1236  {
1237  $obj_id = $id;
1238  }
1239 
1240  if($cont_type == 'prtf')
1241  {
1242  include_once "Services/Portfolio/classes/class.ilPortfolioPage.php";
1244  }
1245 
1246  if($cont_type == 'blp')
1247  {
1248  include_once('./Services/COPage/classes/class.ilPageObject.php');
1249  $obj_id = ilPageObject::lookupParentId($id, 'blp');
1250  }
1251 
1252  if($cont_type == 'impr')
1253  {
1254  // imprint page - always id 1
1255  $obj_id = $id;
1256  }
1257  break;
1258 
1259  // Media Pool
1260  case "mep":
1261  $obj_id = $id;
1262  break;
1263 
1264  // News Context Object (e.g. MediaCast)
1265  case "news":
1266  include_once("./Services/News/classes/class.ilNewsItem.php");
1268  break;
1269  }
1270 
1271  return $obj_id;
1272  }
1273 
1281  function _resizeImage($a_file, $a_width, $a_height, $a_constrain_prop = false)
1282  {
1283  $file_path = pathinfo($a_file);
1284  $location = substr($file_path["basename"],0,strlen($file_path["basename"]) -
1285  strlen($file_path["extension"]) - 1)."_".
1286  $a_width."_".
1287  $a_height.".".$file_path["extension"];
1288  $target_file = $file_path["dirname"]."/".
1289  $location;
1290  ilUtil::resizeImage($a_file, $target_file,
1291  (int) $a_width, (int) $a_height, $a_constrain_prop);
1292 
1293  return $location;
1294  }
1295 
1303  static function getMimeType($a_file)
1304  {
1305  include_once("./Services/Utilities/classes/class.ilMimeTypeUtil.php");
1306  $mime = ilMimeTypeUtil::getMimeType($a_file);
1307  return $mime;
1308  }
1309 
1313  static function _determineWidthHeight($a_def_width, $a_def_height, $a_format, $a_type,
1314  $a_file, $a_reference, $a_constrain_proportions, $a_use_original,
1315  $a_user_width, $a_user_height)
1316  {
1317  global $lng;
1318 
1319  // determine width and height of known image types
1320  $width = $a_def_width;
1321  $height = $a_def_height;
1322  $info = "";
1323 
1324  if ($a_format == "audio/mpeg")
1325  {
1326  $width = 300;
1327  $height = 20;
1328  }
1329 
1330  if (ilUtil::deducibleSize($a_format))
1331  {
1332  if ($a_type == "File")
1333  {
1334  $size = @getimagesize($a_file);
1335  }
1336  else
1337  {
1338  $size = @getimagesize($a_reference);
1339  }
1340  }
1341 
1342  if ($a_use_original)
1343  {
1344  if ($size[0] > 0 && $size[1] > 0)
1345  {
1346  $width = $size[0];
1347  $height = $size[1];
1348  }
1349  else
1350  {
1351  $info = $lng->txt("cont_could_not_determine_resource_size");
1352  }
1353  }
1354  else
1355  {
1356  $w = (int) $a_user_width;
1357  $h = (int) $a_user_height;
1358  $width = $w;
1359  $height = $h;
1360 //echo "<br>C-$width-$height-";
1361  if (ilUtil::deducibleSize($a_format) && $a_constrain_proportions)
1362  {
1363  if ($size[0] > 0 && $size[1] > 0)
1364  {
1365  if ($w > 0)
1366  {
1367  $wr = $size[0] / $w;
1368  }
1369  if ($h > 0)
1370  {
1371  $hr = $size[1] / $h;
1372  }
1373 //echo "<br>+".$wr."+".$size[0]."+".$w."+";
1374 //echo "<br>+".$hr."+".$size[1]."+".$h."+";
1375  $r = max($wr, $hr);
1376  if ($r > 0)
1377  {
1378  $width = (int) ($size[0]/$r);
1379  $height = (int) ($size[1]/$r);
1380  }
1381  }
1382  }
1383 //echo "<br>D-$width-$height-";
1384  }
1385 //echo "<br>E-$width-$height-";
1386  return array("width" => $width, "height" => $height, "info" => $info);
1387  }
1388 
1393  static function _getSimpleMimeTypes()
1394  {
1395  return array("image/x-ms-bmp", "image/gif", "image/jpeg", "image/x-portable-bitmap",
1396  "image/png", "image/psd", "image/tiff", "application/pdf");
1397  }
1398 
1399  function getDataDirectory()
1400  {
1401  return ilUtil::getWebspaceDir()."/mobs/mm_".$this->object->getId();
1402  }
1403 
1410  static function _useAutoStartParameterOnly($a_loc, $a_format)
1411  {
1412  $lpath = pathinfo($a_loc);
1413  if ($lpath["extension"] == "mp3" && $a_format == "audio/mpeg")
1414  {
1415  return true;
1416  }
1417  if ($lpath["extension"] == "flv")
1418  {
1419  return true;
1420  }
1421  return false;
1422  }
1423 
1427  function &_saveTempFileAsMediaObject($name, $tmp_name, $upload = TRUE)
1428  {
1429  // create dummy object in db (we need an id)
1430  $media_object = new ilObjMediaObject();
1431  $media_object->setTitle($name);
1432  $media_object->setDescription("");
1433  $media_object->create();
1434 
1435  // determine and create mob directory, move uploaded file to directory
1436  $media_object->createDirectory();
1437  $mob_dir = ilObjMediaObject::_getDirectory($media_object->getId());
1438 
1439  $media_item =& new ilMediaItem();
1440  $media_object->addMediaItem($media_item);
1441  $media_item->setPurpose("Standard");
1442 
1443  $file = $mob_dir."/".$name;
1444  if ($upload)
1445  {
1446  ilUtil::moveUploadedFile($tmp_name,$name, $file);
1447  }
1448  else
1449  {
1450  copy($tmp_name, $file);
1451  }
1452  // get mime type
1454  $location = $name;
1455  // set real meta and object data
1456  $media_item->setFormat($format);
1457  $media_item->setLocation($location);
1458  $media_item->setLocationType("LocalFile");
1459  $media_object->setTitle($name);
1460  $media_object->setDescription($format);
1461 
1462  if (ilUtil::deducibleSize($format))
1463  {
1464  $size = getimagesize($file);
1465  $media_item->setWidth($size[0]);
1466  $media_item->setHeight($size[1]);
1467  }
1468  $media_item->setHAlign("Left");
1469 
1470  ilUtil::renameExecutables($mob_dir);
1471  $media_object->update();
1472 
1473  return $media_object;
1474  }
1475 
1479  function uploadAdditionalFile($a_name, $tmp_name, $a_subdir = "")
1480  {
1481  $a_subdir = str_replace("..", "", $a_subdir);
1482  $dir = $mob_dir = ilObjMediaObject::_getDirectory($this->getId());
1483  if ($a_subdir != "")
1484  {
1485  $dir.= "/".$a_subdir;
1486  }
1487  ilUtil::makeDirParents($dir);
1488  ilUtil::moveUploadedFile($tmp_name, $a_name, $dir."/".$a_name);
1489  ilUtil::renameExecutables($mob_dir);
1490  }
1491 
1495  function makeThumbnail($a_file, $a_thumbname, $a_format = "png",
1496  $a_size = "80")
1497  {
1498  $m_dir = ilObjMediaObject::_getDirectory($this->getId());
1501  ilUtil::convertImage($m_dir."/".$a_file,
1502  $t_dir."/".$a_thumbname, $a_format, $a_size);
1503  }
1504 
1511  static function getThumbnailPath($a_mob_id, $a_thumbname)
1512  {
1513  $t_dir = ilObjMediaObject::_getThumbnailDirectory($a_mob_id);
1514  return $t_dir."/".$a_thumbname;
1515  }
1516 
1517 
1521  function removeAdditionalFile($a_file)
1522  {
1523  $file = str_replace("..", "", $a_file);
1525  if (is_file($file))
1526  {
1527  unlink($file);
1528  }
1529  }
1530 
1531 
1535  function getLinkedMediaObjects($a_ignore = "")
1536  {
1537  $linked = array();
1538 
1539  if (!is_array($a_ignore))
1540  {
1541  $a_ignore = array();
1542  }
1543 
1544  // get linked media objects (map areas)
1545  $med_items = $this->getMediaItems();
1546 
1547  foreach($med_items as $med_item)
1548  {
1549  $int_links = ilMapArea::_getIntLinks($med_item->getId());
1550  foreach ($int_links as $k => $int_link)
1551  {
1552  if ($int_link["Type"] == "MediaObject")
1553  {
1554  include_once("./Services/COPage/classes/class.ilInternalLink.php");
1555  $l_id = ilInternalLink::_extractObjIdOfTarget($int_link["Target"]);
1556  if (ilObject::_exists($l_id))
1557  {
1558  if (!in_array($l_id, $linked) &&
1559  !in_array($l_id, $a_ignore))
1560  {
1561  $linked[] = $l_id;
1562  }
1563  }
1564  }
1565  }
1566  }
1567 //var_dump($linked);
1568  return $linked;
1569  }
1570 
1574  static function getRestrictedFileTypes()
1575  {
1576  $mset = new ilSetting("mobs");
1577  $str = $mset->get("restricted_file_types");
1578  $types = explode(",", $str);
1579  $suffixes = array();
1580  if (count($types) > 0)
1581  {
1582  foreach ($types as $k => $t)
1583  {
1584  if (($s = strtolower(trim($t))) != "")
1585  {
1586  $suffixes[] = $s;
1587  }
1588  }
1589  }
1590  return $suffixes;
1591  }
1592 
1596  function duplicate()
1597  {
1598  $new_obj = new ilObjMediaObject();
1599  $new_obj->setTitle($this->getTitle());
1600  $new_obj->setDescription($this->getDescription());
1601 
1602  // media items
1603  foreach($this->getMediaItems() as $key => $val)
1604  {
1605  $new_obj->addMediaItem($val);
1606  }
1607 
1608  $new_obj->create(false, true);
1609 
1610  // files
1611  $new_obj->createDirectory();
1612  self::_createThumbnailDirectory($new_obj->getId());
1614  ilObjMediaObject::_getDirectory($new_obj->getId()));
1615  ilUtil::rCopy(ilObjMediaObject::_getThumbnailDirectory($this->getId()),
1616  ilObjMediaObject::_getThumbnailDirectory($new_obj->getId()));
1617 
1618  // meta data
1619  include_once("Services/MetaData/classes/class.ilMD.php");
1620  $md = new ilMD(0, $this->getId(), "mob");
1621  $new_md = $md->cloneMD(0, $new_obj->getId(), "mob");
1622 
1623  return $new_obj;
1624  }
1625 
1632  function uploadVideoPreviewPic($a_prevpic)
1633  {
1634  $pi = pathinfo($a_prevpic["name"]);
1635  $ext = $pi["extension"];
1636  if (in_array($ext, array("jpg", "jpeg", "png")))
1637  {
1638  $this->uploadAdditionalFile("mob_vpreview.".$ext, $a_prevpic["tmp_name"]);
1639  }
1640  }
1641 
1648  function generatePreviewPic($a_width, $a_height)
1649  {
1650  $item = $this->getMediaItem("Standard");
1651 
1652  if ($item->getLocationType() == "LocalFile" &&
1653  is_int(strpos($item->getFormat(), "image/")))
1654  {
1655  $dir = ilObjMediaObject::_getDirectory($this->getId());
1656  $file = $dir."/".
1657  $item->getLocation();
1658  if (is_file($file))
1659  {
1660  if(ilUtil::isConvertVersionAtLeast("6.3.8-3"))
1661  {
1662  ilUtil::execConvert(ilUtil::escapeShellArg($file)."[0] -geometry ".$a_width."x".$a_height."^ -gravity center -extent ".$a_width."x".$a_height." PNG:".$dir."/mob_vpreview.png");
1663  }
1664  else
1665  {
1666  ilUtil::convertImage($file, $dir."/mob_vpreview.png", "PNG", $a_width."x".$a_height);
1667  }
1668  }
1669  }
1670  }
1671 
1678  function getVideoPreviewPic($a_filename_only = false)
1679  {
1680  $dir = ilObjMediaObject::_getDirectory($this->getId());
1681  $ppics = array("mob_vpreview.jpg",
1682  "mob_vpreview.jpeg",
1683  "mob_vpreview.png");
1684  foreach ($ppics as $p)
1685  {
1686  if (is_file($dir."/".$p))
1687  {
1688  if ($a_filename_only)
1689  {
1690  return $p;
1691  }
1692  else
1693  {
1694  return $dir."/".$p;
1695  }
1696  }
1697  }
1698  return "";
1699  }
1700 
1701 }
1702 ?>