00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once("content/classes/Media/class.ilMapArea.php");
00025
00036 class ilMediaItem
00037 {
00038 var $ilias;
00039 var $id;
00040 var $purpose;
00041 var $location;
00042 var $location_type;
00043 var $format;
00044 var $width;
00045 var $height;
00046 var $caption;
00047 var $halign;
00048 var $parameters;
00049 var $mob_id;
00050 var $nr;
00051 var $mapareas;
00052 var $map_cnt;
00053 var $map_image;
00054 var $color1;
00055 var $color2;
00056
00057 function ilMediaItem($a_id = 0)
00058 {
00059 global $ilias;
00060
00061 $this->ilias =& $ilias;
00062 $this->parameters = array();
00063 $this->mapareas = array();
00064 $this->map_cnt = 0;
00065
00066 if ($a_id != 0)
00067 {
00068 $this->setId($a_id);
00069 $this->read();
00070 }
00071 }
00072
00078 function setId($a_id)
00079 {
00080 $this->id = $a_id;
00081 }
00082
00088 function getId()
00089 {
00090 return $this->id;
00091 }
00092
00098 function setMobId($a_mob_id)
00099 {
00100 $this->mob_id = $a_mob_id;
00101 }
00102
00108 function getMobId()
00109 {
00110 return $this->mob_id;
00111 }
00112
00116 function setNr($a_nr)
00117 {
00118 $this->nr = $a_nr;
00119 }
00120
00121 function getNr()
00122 {
00123 return $this->nr;
00124 }
00125
00129 function create()
00130 {
00131 global $ilDB;
00132
00133 $query = "INSERT INTO media_item (mob_id, purpose, location, ".
00134 "location_type, format, width, ".
00135 "height, halign, caption, nr) VALUES ".
00136 "(".$ilDB->quote($this->getMobId()).",".
00137 $ilDB->quote($this->getPurpose()).",".
00138 $ilDB->quote($this->getLocation()).",".
00139 $ilDB->quote($this->getLocationType()).",".
00140 $ilDB->quote($this->getFormat()).",".
00141 $ilDB->quote($this->getWidth()).",".
00142 $ilDB->quote($this->getHeight()).",".
00143 $ilDB->quote($this->getHAlign()).",".
00144 $ilDB->quote($this->getCaption()).",".
00145 $ilDB->quote($this->getNr()).")";
00146 $this->ilias->db->query($query);
00147
00148 $item_id = $this->ilias->db->getLastInsertId();
00149 $this->setId($item_id);
00150
00151
00152 $params = $this->getParameters();
00153 foreach($params as $name => $value)
00154 {
00155 $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES ".
00156 "(".$ilDB->quote($item_id).",".
00157 $ilDB->quote($name).",".
00158 $ilDB->quote($value).")";
00159 $this->ilias->db->query($query);
00160 }
00161
00162
00163 for ($i=0; $i < count($this->mapareas); $i++)
00164 {
00165 $this->mapareas[$i]->setItemId($this->getId());
00166 $this->mapareas[$i]->setNr($i + 1);
00167 $this->mapareas[$i]->create();
00168 }
00169 }
00170
00174 function update()
00175 {
00176 global $ilDB;
00177
00178 $query = "UPDATE media_item SET ".
00179 " mob_id = ".$ilDB->quote($this->getMobId()).",".
00180 " purpose = ".$ilDB->quote($this->getPurpose()).",".
00181 " location = ".$ilDB->quote($this->getLocation()).",".
00182 " location_type = ".$ilDB->quote($this->getLocationType()).",".
00183 " format = ".$ilDB->quote($this->getFormat()).",".
00184 " width = ".$ilDB->quote($this->getWidth()).",".
00185 " height = ".$ilDB->quote($this->getHeight()).",".
00186 " halign = ".$ilDB->quote($this->getHAlign()).",".
00187 " caption = ".$ilDB->quote($this->getCaption()).",".
00188 " nr = ".$ilDB->quote($this->getNr()).
00189 " WHERE id = ".$ilDB->quote($this->getId());
00190 $this->ilias->db->query($query);
00191
00192
00193 $query = "DELETE FROM mob_parameter WHERE med_item_id = ".
00194 $ilDB->quote($this->getId());
00195
00196
00197 $params = $this->getParameters();
00198 foreach($params as $name => $value)
00199 {
00200 $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES ".
00201 "(".$ilDB->quote($this->getId()).",".
00202 $ilDB->quote($name).",".
00203 $ilDB->quote($value).")";
00204 $this->ilias->db->query($query);
00205 }
00206 }
00207
00211 function read()
00212 {
00213 $item_id = $this->getId();
00214 $mob_id = $this->getMobId();
00215 $nr = $this->getNr();
00216 $query = "";
00217 if($item_id > 0)
00218 {
00219 $query = "SELECT * FROM media_item WHERE id = '".$this->getId()."'";
00220 }
00221 else if ($mob_id > 0 && $nr > 0)
00222 {
00223 $query = "SELECT * FROM media_item WHERE mob_id = '".$this->getMobId()."' ".
00224 "AND nr='".$this->getNr()."'";
00225 }
00226 if ($query != "")
00227 {
00228 $item_set = $this->ilias->db->query($query);
00229 $item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC);
00230
00231 $this->setLocation($item_rec["location"]);
00232 $this->setLocationType($item_rec["location_type"]);
00233 $this->setFormat($item_rec["format"]);
00234 $this->setWidth($item_rec["width"]);
00235 $this->setHeight($item_rec["height"]);
00236 $this->setHAlign($item_rec["halign"]);
00237 $this->setCaption($item_rec["caption"]);
00238 $this->setPurpose($item_rec["purpose"]);
00239 $this->setNr($item_rec["nr"]);
00240 $this->setMobId($item_rec["mob_id"]);
00241 $this->setId($item_rec["id"]);
00242 $this->setThumbTried($item_rec["tried_thumb"]);
00243
00244
00245 $query = "SELECT * FROM mob_parameter WHERE med_item_id = '".
00246 $this->getId()."'";
00247 $par_set = $this->ilias->db->query($query);
00248 while ($par_rec = $par_set->fetchRow(DB_FETCHMODE_ASSOC))
00249 {
00250 $this->setParameter($par_rec["name"], $par_rec["value"]);
00251 }
00252
00253
00254 $max = ilMapArea::_getMaxNr($this->getId());
00255 for ($i = 1; $i <= $max; $i++)
00256 {
00257 $area =& new ilMapArea($this->getId(), $i);
00258 $this->addMapArea($area);
00259 }
00260 }
00261
00262 }
00263
00267 function writeThumbTried($a_tried)
00268 {
00269 global $ilDB;
00270
00271 $q = "UPDATE media_item SET tried_thumb=".
00272 $ilDB->quote($a_tried).
00273 " WHERE id = ".$ilDB->quote($this->getId());
00274
00275 $ilDB->query($q);
00276 }
00277
00283 function _getMediaItemsOfMOb(&$a_mob)
00284 {
00285
00286 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob->getId()."' ".
00287 "ORDER BY nr";
00288 $item_set = $this->ilias->db->query($query);
00289 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00290 {
00291 $media_item =& new ilMediaItem();
00292 $media_item->setNr($item_rec["nr"]);
00293 $media_item->setId($item_rec["id"]);
00294 $media_item->setLocation($item_rec["location"]);
00295 $media_item->setLocationType($item_rec["location_type"]);
00296 $media_item->setFormat($item_rec["format"]);
00297 $media_item->setWidth($item_rec["width"]);
00298 $media_item->setHeight($item_rec["height"]);
00299 $media_item->setHAlign($item_rec["halign"]);
00300 $media_item->setCaption($item_rec["caption"]);
00301 $media_item->setPurpose($item_rec["purpose"]);
00302 $media_item->setMobId($item_rec["mob_id"]);
00303 $media_item->setThumbTried($item_rec["tried_thumb"]);
00304
00305
00306 $query = "SELECT * FROM mob_parameter WHERE med_item_id = '".
00307 $item_rec["id"]."'";
00308 $par_set = $this->ilias->db->query($query);
00309 while ($par_rec = $par_set->fetchRow(DB_FETCHMODE_ASSOC))
00310 {
00311 $media_item->setParameter($par_rec["name"], $par_rec["value"]);
00312 }
00313
00314
00315 $max = ilMapArea::_getMaxNr($media_item->getId());
00316 for ($i = 1; $i <= $max; $i++)
00317 {
00318 $area =& new ilMapArea($media_item->getId(), $i);
00319 $media_item->addMapArea($area);
00320 }
00321
00322
00323 $a_mob->addMediaItem($media_item);
00324 }
00325 }
00326
00330 function deleteAllItemsOfMob($a_mob_id)
00331 {
00332
00333 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."'";
00334 $item_set = $this->ilias->db->query($query);
00335 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00336 {
00337
00338 $query = "DELETE FROM mob_parameter WHERE med_item_id = '".$item_rec["id"]."'";
00339 $this->ilias->db->query($query);
00340
00341
00342 $query = "DELETE FROM map_area WHERE item_id = '".$item_rec["id"]."'";
00343 $this->ilias->db->query($query);
00344 }
00345
00346
00347 $query = "DELETE FROM media_item WHERE mob_id = '".$a_mob_id."'";
00348 $this->ilias->db->query($query);
00349 }
00350
00351 function setPurpose($a_purpose)
00352 {
00353 $this->purpose = $a_purpose;
00354 }
00355
00356 function getPurpose()
00357 {
00358 return $this->purpose;
00359 }
00360
00361 function setLocation($a_location)
00362 {
00363 $this->location = $a_location;
00364 }
00365
00366 function getLocation()
00367 {
00368 return $this->location;
00369 }
00370
00371 function setLocationType($a_type)
00372 {
00373 $this->location_type = $a_type;
00374 }
00375
00376 function getLocationType()
00377 {
00378 return $this->location_type;
00379 }
00380
00381 function setFormat($a_format)
00382 {
00383 $this->format = $a_format;
00384 }
00385
00386 function getFormat()
00387 {
00388 return $this->format;
00389 }
00390
00391 function setThumbTried($a_tried)
00392 {
00393 $this->tried_thumb = $a_tried;
00394 }
00395
00396 function getThumbTried()
00397 {
00398 return $this->tried_thumb;
00399 }
00400
00401 function addMapArea(&$a_map_area)
00402 {
00403 $this->mapareas[$this->map_cnt] =& $a_map_area;
00404 $this->map_cnt++;
00405 }
00406
00410 function deleteMapArea($nr)
00411 {
00412 for ($i=1; $i<=$this->map_cnt; $i++)
00413 {
00414 if($i > $nr)
00415 {
00416 $this->mapareas[$i-2] =& $this->mapareas[$i-1];
00417 $this->mapareas[$i-2]->setNr($i-1);
00418 }
00419 }
00420 if($nr <= $this->map_cnt)
00421 {
00422 unset($this->mapareas[$this->map_cnt - 1]);
00423 $this->map_cnt--;
00424 }
00425 }
00426
00430 function &getMapArea($nr)
00431 {
00432 return $this->mapareas[$nr-1];
00433 }
00434
00438 function getWidth()
00439 {
00440 return $this->width;
00441 }
00442
00446 function setWidth($a_width)
00447 {
00448 $this->width = $a_width;
00449 }
00450
00454 function getHeight()
00455 {
00456 return $this->height;
00457 }
00458
00462 function setHeight($a_height)
00463 {
00464 $this->height = $a_height;
00465 }
00466
00470 function getOriginalSize()
00471 {
00472 $mob_dir = ilObjMediaObject::_getDirectory($this->getMobId());
00473
00474 if ($this->getLocationType() == "LocalFile" &&
00475 ilUtil::deducibleSize($this->getFormat()))
00476 {
00477 $file = $mob_dir."/".$this->getLocation();
00478 if (is_file($file))
00479 {
00480 $size = getimagesize($file);
00481 }
00482 else
00483 {
00484 $size[0] = $size[1] = 0;
00485 }
00486 $size = array("width" => $size[0], "height" => $size[1]);
00487
00488 return $size;
00489 }
00490
00491 return false;
00492 }
00493
00497 function setCaption($a_caption)
00498 {
00499 $this->caption = $a_caption;
00500 }
00501
00505 function getCaption()
00506 {
00507 return $this->caption;
00508 }
00509
00513 function setHAlign($a_halign)
00514 {
00515 $this->halign = $a_halign;
00516 }
00517
00521 function getHAlign()
00522 {
00523 return $this->halign;
00524 }
00525
00526
00533 function setParameter($a_name, $a_value)
00534 {
00535 $this->parameters[$a_name] = $a_value;
00536 }
00537
00541 function resetParameters()
00542 {
00543 $this->parameters = array();
00544 }
00545
00551 function setParameters($a_par)
00552 {
00553 $this->resetParameters();
00554 $par_arr = ilUtil::extractParameterString($a_par);
00555 if(is_array($par_arr))
00556 {
00557 foreach($par_arr as $par => $val)
00558 {
00559 $this->setParameter($par, $val);
00560 }
00561 }
00562 }
00563
00564
00568 function getParameters()
00569 {
00570 return $this->parameters;
00571 }
00572
00573
00577 function getParameterString()
00578 {
00579 return ilUtil::assembleParameterString($this->parameters);
00580 }
00581
00582
00586 function getParameter($a_name)
00587 {
00588 return $this->parameters[$a_name];
00589 }
00590
00594 function getWorkDirectory()
00595 {
00596 return ilUtil::getDataDir()."/map_workfiles/item_".$this->getId();
00597 }
00598
00602 function createWorkDirectory()
00603 {
00604 if(!@is_dir(ilUtil::getDataDir()."/map_workfiles"))
00605 {
00606 ilUtil::createDirectory(ilUtil::getDataDir()."/map_workfiles");
00607 }
00608 $work_dir = $this->getWorkDirectory();
00609 if(!@is_dir($work_dir))
00610 {
00611 ilUtil::createDirectory($work_dir);
00612 }
00613 }
00614
00618 function getSuffix()
00619 {
00620 $loc_arr = explode(".", $this->getLocation());
00621
00622 return $loc_arr[count($loc_arr) - 1];
00623 }
00624
00628 function getMapWorkCopyType()
00629 {
00630 return ilUtil::getGDSupportedImageType($this->getSuffix());
00631 }
00632
00636 function getMapWorkCopyName()
00637 {
00638 $file_arr = explode("/", $this->getLocation());
00639 $file = $file_arr[count($file_arr) - 1];
00640 $file_arr = explode(".", $file);
00641 unset($file_arr[count($file_arr) - 1]);
00642 $file = implode($file_arr, ".");
00643
00644 return $this->getWorkDirectory()."/".$file.".".$this->getMapWorkCopyType();
00645 }
00646
00650 function getDirectory()
00651 {
00652 return ilObjMediaObject::_getDirectory($this->getMobId());
00653 }
00654
00658 function getThumbnailDirectory($a_mode = "filesystem")
00659 {
00660 return ilObjMediaObject::_getThumbnailDirectory($this->getMobId(), $a_mode);
00661 }
00662
00666 function getThumbnailTarget($a_size = "")
00667 {
00668 if (is_int(strpos($this->getFormat(), "image")))
00669 {
00670 $thumb_file = $this->getThumbnailDirectory()."/".
00671 $this->getPurpose().".jpeg";
00672
00673 $thumb_file_small = $this->getThumbnailDirectory()."/".
00674 $this->getPurpose()."_small.jpeg";
00675
00676
00677 if ($this->getThumbTried() == "n" && $this->getLocationType() == "LocalFile")
00678 {
00679 if (is_file($thumb_file))
00680 {
00681 unlink($thumb_file);
00682 }
00683 if (is_file($thumb_file_small))
00684 {
00685 unlink($thumb_file_small);
00686 }
00687 $this->writeThumbTried("y");
00688 ilObjMediaObject::_createThumbnailDirectory($this->getMobId());
00689 $med_file = $this->getDirectory()."/".$this->getLocation();
00690
00691 if (is_file($med_file))
00692 {
00693 ilUtil::convertImage($med_file, $thumb_file, "jpeg", "80");
00694 ilUtil::convertImage($med_file, $thumb_file_small, "jpeg", "40");
00695 }
00696 }
00697
00698 if ($a_size == "small")
00699 {
00700 if (is_file($thumb_file_small))
00701 {
00702 return $this->getThumbnailDirectory("output")."/".
00703 $this->getPurpose()."_small.jpeg?dummy=".rand(1, 999999);
00704 }
00705 }
00706 else
00707 {
00708 if (is_file($thumb_file))
00709 {
00710 return $this->getThumbnailDirectory("output")."/".
00711 $this->getPurpose().".jpeg?dummy=".rand(1, 999999);
00712 }
00713 }
00714 }
00715
00716 return "";
00717 }
00718
00719
00726 function makeMapWorkCopy($a_area_nr = 0, $a_exclude = false)
00727 {
00728 $this->createWorkDirectory();
00729 ilUtil::convertImage($this->getDirectory()."/".$this->getLocation(),
00730 $this->getMapWorkCopyName(),
00731 $this->getMapWorkCopyType());
00732
00733 $this->buildMapWorkImage();
00734
00735
00736 for ($i=0; $i < count($this->mapareas); $i++)
00737 {
00738 if ( ((($i+1) == $a_area_nr) && !$a_exclude) ||
00739 ((($i+1) != $a_area_nr) && $a_exclude) ||
00740 ($a_area_nr == 0)
00741 )
00742 {
00743 $area =& $this->mapareas[$i];
00744 $area->draw($this->getMapWorkImage(), $this->color1, $this->color2);
00745 }
00746 }
00747
00748 $this->saveMapWorkImage();
00749 }
00750
00751
00758 function addAreaToMapWorkCopy($a_shape, $a_coords)
00759 {
00760 $this->buildMapWorkImage();
00761
00762
00763 $area = new ilMapArea();
00764 $area->setShape($a_shape);
00765
00766 $area->setCoords($a_coords);
00767 $area->draw($this->getMapWorkImage(), $this->color1, $this->color2, false);
00768
00769 $this->saveMapWorkImage();
00770 }
00771
00775 function outputMapWorkCopy()
00776 {
00777 if ($this->getMapWorkCopyType() != "")
00778 {
00779 header("Pragma: no-cache");
00780 header("Expires: 0");
00781 header("Content-type: image/".strtolower($this->getMapWorkCopyType()));
00782 readfile($this->getMapWorkCopyName());
00783 }
00784 exit;
00785 }
00786
00790 function buildMapWorkImage()
00791 {
00792 $im_type = strtolower($this->getMapWorkCopyType());
00793
00794 switch ($im_type)
00795 {
00796 case "gif":
00797 $this->map_image = ImageCreateFromGIF($this->getMapWorkCopyName());
00798 break;
00799
00800 case "jpg":
00801 $this->map_image = ImageCreateFromJPEG($this->getMapWorkCopyName());
00802 break;
00803
00804 case "png":
00805 $this->map_image = ImageCreateFromPNG($this->getMapWorkCopyName());
00806 break;
00807 }
00808
00809
00810 if (imagecolorstotal($this->map_image) > 250)
00811 {
00812 $this->color1 = imagecolorclosest($this->map_image, 0, 0, 0);
00813 $this->color2 = imagecolorclosest($this->map_image, 255, 255, 255);
00814 }
00815 else
00816 {
00817 $this->color1 = imagecolorallocate($this->map_image, 0, 0, 0);
00818 $this->color2 = imagecolorallocate($this->map_image, 255, 255, 255);
00819 }
00820 }
00821
00825 function saveMapWorkImage()
00826 {
00827 $im_type = strtolower($this->getMapWorkCopyType());
00828
00829
00830 switch ($im_type)
00831 {
00832 case "gif":
00833 ImageGIF($this->map_image, $this->getMapWorkCopyName());
00834 break;
00835
00836 case "jpg":
00837 ImageJPEG($this->map_image, $this->getMapWorkCopyName());
00838 break;
00839
00840 case "png":
00841 ImagePNG($this->map_image, $this->getMapWorkCopyName());
00842 break;
00843 }
00844
00845 ImageDestroy($this->map_image);
00846 }
00847
00851 function &getMapWorkImage()
00852 {
00853 return $this->map_image;
00854 }
00855
00856
00860 function getMapAreasXML($a_insert_inst = false, $a_inst = 0)
00861 {
00862 $xml = "";
00863
00864
00865 for ($i=0; $i < count($this->mapareas); $i++)
00866 {
00867 $area =& $this->mapareas[$i];
00868 $xml .= "<MapArea Shape=\"".$area->getShape()."\" Coords=\"".$area->getCoords()."\">";
00869 if ($area->getLinkType() == IL_INT_LINK)
00870 {
00871 $target_frame = $area->getTargetFrame();
00872
00873 if ($area->getType() == "GlossaryItem" && $target_frame == "")
00874 {
00875 $target_frame = "Glossary";
00876 }
00877
00878 $tf_str = ($target_frame == "")
00879 ? ""
00880 : "TargetFrame=\"".$target_frame."\"";
00881
00882 $xml .= "<IntLink Target=\"".$area->getTarget($a_insert_inst, $a_inst)."\" Type=\"".
00883 $area->getType()."\" $tf_str>";
00884 $xml .= $area->getTitle();
00885 $xml .="</IntLink>";
00886 }
00887 else
00888 {
00889 $xml .= "<ExtLink Href=\"".str_replace("&", "&",$area->getHref())."\" Title=\"".
00890 $area->getExtTitle()."\">";
00891 $xml .= $area->getTitle();
00892 $xml .="</ExtLink>";
00893 }
00894 $xml .= "</MapArea>";
00895 }
00896
00897 return $xml;
00898 }
00899
00900
00906 function _resolveMapAreaLinks($a_mob_id)
00907 {
00908
00909
00910 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."' ".
00911 "ORDER BY nr";
00912 $item_set = $this->ilias->db->query($query);
00913 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00914 {
00915 ilMapArea::_resolveIntLinks($item_rec["id"]);
00916 }
00917 }
00918
00924 function _getMapAreasIntLinks($a_mob_id)
00925 {
00926
00927 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."' ".
00928 "ORDER BY nr";
00929 $item_set = $this->ilias->db->query($query);
00930 $links = array();
00931 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00932 {
00933 $map_links = ilMapArea::_getIntLinks($item_rec["id"]);
00934 foreach($map_links as $key => $map_link)
00935 {
00936 $links[$key] = $map_link;
00937 }
00938 }
00939 return $links;
00940 }
00941
00942 }
00943 ?>