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 read()
00175 {
00176 $item_id = $this->getId();
00177 $mob_id = $this->getMobId();
00178 $nr = $this->getNr();
00179 $query = "";
00180 if($item_id > 0)
00181 {
00182 $query = "SELECT * FROM media_item WHERE id = '".$this->getId()."'";
00183 }
00184 else if ($mob_id > 0 && $nr > 0)
00185 {
00186 $query = "SELECT * FROM media_item WHERE mob_id = '".$this->getMobId()."' ".
00187 "AND nr='".$this->getNr()."'";
00188 }
00189 if ($query != "")
00190 {
00191 $item_set = $this->ilias->db->query($query);
00192 $item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC);
00193
00194 $this->setLocation($item_rec["location"]);
00195 $this->setLocationType($item_rec["location_type"]);
00196 $this->setFormat($item_rec["format"]);
00197 $this->setWidth($item_rec["width"]);
00198 $this->setHeight($item_rec["height"]);
00199 $this->setHAlign($item_rec["halign"]);
00200 $this->setCaption($item_rec["caption"]);
00201 $this->setPurpose($item_rec["purpose"]);
00202 $this->setNr($item_rec["nr"]);
00203 $this->setMobId($item_rec["mob_id"]);
00204 $this->setId($item_rec["id"]);
00205
00206
00207 $query = "SELECT * FROM mob_parameter WHERE med_item_id = '".
00208 $this->getId()."'";
00209 $par_set = $this->ilias->db->query($query);
00210 while ($par_rec = $par_set->fetchRow(DB_FETCHMODE_ASSOC))
00211 {
00212 $this->setParameter($par_rec["name"], $par_rec["value"]);
00213 }
00214
00215
00216 $max = ilMapArea::_getMaxNr($this->getId());
00217 for ($i = 1; $i <= $max; $i++)
00218 {
00219 $area =& new ilMapArea($this->getId(), $i);
00220 $this->addMapArea($area);
00221 }
00222 }
00223
00224 }
00225
00231 function _getMediaItemsOfMOb(&$a_mob)
00232 {
00233
00234 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob->getId()."' ".
00235 "ORDER BY nr";
00236 $item_set = $this->ilias->db->query($query);
00237 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00238 {
00239 $media_item =& new ilMediaItem();
00240 $media_item->setNr($item_rec["nr"]);
00241 $media_item->setId($item_rec["id"]);
00242 $media_item->setLocation($item_rec["location"]);
00243 $media_item->setLocationType($item_rec["location_type"]);
00244 $media_item->setFormat($item_rec["format"]);
00245 $media_item->setWidth($item_rec["width"]);
00246 $media_item->setHeight($item_rec["height"]);
00247 $media_item->setHAlign($item_rec["halign"]);
00248 $media_item->setCaption($item_rec["caption"]);
00249 $media_item->setPurpose($item_rec["purpose"]);
00250 $media_item->setMobId($item_rec["mob_id"]);
00251
00252
00253 $query = "SELECT * FROM mob_parameter WHERE med_item_id = '".
00254 $item_rec["id"]."'";
00255 $par_set = $this->ilias->db->query($query);
00256 while ($par_rec = $par_set->fetchRow(DB_FETCHMODE_ASSOC))
00257 {
00258 $media_item->setParameter($par_rec["name"], $par_rec["value"]);
00259 }
00260
00261
00262 $max = ilMapArea::_getMaxNr($media_item->getId());
00263 for ($i = 1; $i <= $max; $i++)
00264 {
00265 $area =& new ilMapArea($media_item->getId(), $i);
00266 $media_item->addMapArea($area);
00267 }
00268
00269
00270 $a_mob->addMediaItem($media_item);
00271 }
00272 }
00273
00277 function deleteAllItemsOfMob($a_mob_id)
00278 {
00279
00280 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."'";
00281 $item_set = $this->ilias->db->query($query);
00282 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00283 {
00284
00285 $query = "DELETE FROM mob_parameter WHERE med_item_id = '".$item_rec["id"]."'";
00286 $this->ilias->db->query($query);
00287
00288
00289 $query = "DELETE FROM map_area WHERE item_id = '".$item_rec["id"]."'";
00290 $this->ilias->db->query($query);
00291 }
00292
00293
00294 $query = "DELETE FROM media_item WHERE mob_id = '".$a_mob_id."'";
00295 $this->ilias->db->query($query);
00296 }
00297
00298 function setPurpose($a_purpose)
00299 {
00300 $this->purpose = $a_purpose;
00301 }
00302
00303 function getPurpose()
00304 {
00305 return $this->purpose;
00306 }
00307
00308 function setLocation($a_location)
00309 {
00310 $this->location = $a_location;
00311 }
00312
00313 function getLocation()
00314 {
00315 return $this->location;
00316 }
00317
00318 function setLocationType($a_type)
00319 {
00320 $this->location_type = $a_type;
00321 }
00322
00323 function getLocationType()
00324 {
00325 return $this->location_type;
00326 }
00327
00328 function setFormat($a_format)
00329 {
00330 $this->format = $a_format;
00331 }
00332
00333 function getFormat()
00334 {
00335 return $this->format;
00336 }
00337
00338 function addMapArea(&$a_map_area)
00339 {
00340 $this->mapareas[$this->map_cnt] =& $a_map_area;
00341 $this->map_cnt++;
00342 }
00343
00347 function deleteMapArea($nr)
00348 {
00349 for ($i=1; $i<=$this->map_cnt; $i++)
00350 {
00351 if($i > $nr)
00352 {
00353 $this->mapareas[$i-2] =& $this->mapareas[$i-1];
00354 $this->mapareas[$i-2]->setNr($i-1);
00355 }
00356 }
00357 if($nr <= $this->map_cnt)
00358 {
00359 unset($this->mapareas[$this->map_cnt - 1]);
00360 $this->map_cnt--;
00361 }
00362 }
00363
00367 function &getMapArea($nr)
00368 {
00369 return $this->mapareas[$nr-1];
00370 }
00371
00375 function getWidth()
00376 {
00377 return $this->width;
00378 }
00379
00383 function setWidth($a_width)
00384 {
00385 $this->width = $a_width;
00386 }
00387
00391 function getHeight()
00392 {
00393 return $this->height;
00394 }
00395
00399 function setHeight($a_height)
00400 {
00401 $this->height = $a_height;
00402 }
00403
00407 function setCaption($a_caption)
00408 {
00409 $this->caption = $a_caption;
00410 }
00411
00415 function getCaption()
00416 {
00417 return $this->caption;
00418 }
00419
00423 function setHAlign($a_halign)
00424 {
00425 $this->halign = $a_halign;
00426 }
00427
00431 function getHAlign()
00432 {
00433 return $this->halign;
00434 }
00435
00436
00443 function setParameter($a_name, $a_value)
00444 {
00445 $this->parameters[$a_name] = $a_value;
00446 }
00447
00451 function resetParameters()
00452 {
00453 $this->parameters = array();
00454 }
00455
00461 function setParameters($a_par)
00462 {
00463 $this->resetParameters();
00464 $par_arr = ilUtil::extractParameterString($a_par);
00465 if(is_array($par_arr))
00466 {
00467 foreach($par_arr as $par => $val)
00468 {
00469 $this->setParameter($par, $val);
00470 }
00471 }
00472 }
00473
00474
00478 function getParameters()
00479 {
00480 return $this->parameters;
00481 }
00482
00483
00487 function getParameterString()
00488 {
00489 return ilUtil::assembleParameterString($this->parameters);
00490 }
00491
00492
00496 function getParameter($a_name)
00497 {
00498 return $this->parameters[$a_name];
00499 }
00500
00504 function getWorkDirectory()
00505 {
00506 return ilUtil::getDataDir()."/map_workfiles/item_".$this->getId();
00507 }
00508
00512 function createWorkDirectory()
00513 {
00514 if(!@is_dir(ilUtil::getDataDir()."/map_workfiles"))
00515 {
00516 ilUtil::createDirectory(ilUtil::getDataDir()."/map_workfiles");
00517 }
00518 $work_dir = $this->getWorkDirectory();
00519 if(!@is_dir($work_dir))
00520 {
00521 ilUtil::createDirectory($work_dir);
00522 }
00523 }
00524
00528 function getSuffix()
00529 {
00530 $loc_arr = explode(".", $this->getLocation());
00531
00532 return $loc_arr[count($loc_arr) - 1];
00533 }
00534
00538 function getMapWorkCopyType()
00539 {
00540 return ilUtil::getGDSupportedImageType($this->getSuffix());
00541 }
00542
00546 function getMapWorkCopyName()
00547 {
00548 $file_arr = explode("/", $this->getLocation());
00549 $file = $file_arr[count($file_arr) - 1];
00550 $file_arr = explode(".", $file);
00551 unset($file_arr[count($file_arr) - 1]);
00552 $file = implode($file_arr, ".");
00553
00554 return $this->getWorkDirectory()."/".$file.".".$this->getMapWorkCopyType();
00555 }
00556
00560 function getDirectory()
00561 {
00562 return ilObjMediaObject::_getDirectory($this->getMobId());
00563 }
00564
00565
00572 function makeMapWorkCopy($a_area_nr = 0, $a_exclude = false)
00573 {
00574 $this->createWorkDirectory();
00575 ilUtil::convertImage($this->getDirectory()."/".$this->getLocation(),
00576 $this->getMapWorkCopyName(),
00577 $this->getMapWorkCopyType());
00578
00579 $this->buildMapWorkImage();
00580
00581
00582 for ($i=0; $i < count($this->mapareas); $i++)
00583 {
00584 if ( ((($i+1) == $a_area_nr) && !$a_exclude) ||
00585 ((($i+1) != $a_area_nr) && $a_exclude) ||
00586 ($a_area_nr == 0)
00587 )
00588 {
00589 $area =& $this->mapareas[$i];
00590 $area->draw($this->getMapWorkImage(), $this->color1, $this->color2);
00591 }
00592 }
00593
00594 $this->saveMapWorkImage();
00595 }
00596
00597
00604 function addAreaToMapWorkCopy($a_shape, $a_coords)
00605 {
00606 $this->buildMapWorkImage();
00607
00608
00609 $area = new ilMapArea();
00610 $area->setShape($a_shape);
00611
00612 $area->setCoords($a_coords);
00613 $area->draw($this->getMapWorkImage(), $this->color1, $this->color2, false);
00614
00615 $this->saveMapWorkImage();
00616 }
00617
00621 function outputMapWorkCopy()
00622 {
00623 if ($this->getMapWorkCopyType() != "")
00624 {
00625 header("Pragma: no-cache");
00626 header("Expires: 0");
00627 header("Content-type: image/".strtolower($this->getMapWorkCopyType()));
00628 readfile($this->getMapWorkCopyName());
00629 }
00630 exit;
00631 }
00632
00636 function buildMapWorkImage()
00637 {
00638 $im_type = strtolower($this->getMapWorkCopyType());
00639
00640 switch ($im_type)
00641 {
00642 case "gif":
00643 $this->map_image = ImageCreateFromGIF($this->getMapWorkCopyName());
00644 break;
00645
00646 case "jpg":
00647 $this->map_image = ImageCreateFromJPEG($this->getMapWorkCopyName());
00648 break;
00649
00650 case "png":
00651 $this->map_image = ImageCreateFromPNG($this->getMapWorkCopyName());
00652 break;
00653 }
00654
00655
00656 if (imagecolorstotal($this->map_image) > 250)
00657 {
00658 $this->color1 = imagecolorclosest($this->map_image, 0, 0, 0);
00659 $this->color2 = imagecolorclosest($this->map_image, 255, 255, 255);
00660 }
00661 else
00662 {
00663 $this->color1 = imagecolorallocate($this->map_image, 0, 0, 0);
00664 $this->color2 = imagecolorallocate($this->map_image, 255, 255, 255);
00665 }
00666 }
00667
00671 function saveMapWorkImage()
00672 {
00673 $im_type = strtolower($this->getMapWorkCopyType());
00674
00675
00676 switch ($im_type)
00677 {
00678 case "gif":
00679 ImageGIF($this->map_image, $this->getMapWorkCopyName());
00680 break;
00681
00682 case "jpg":
00683 ImageJPEG($this->map_image, $this->getMapWorkCopyName());
00684 break;
00685
00686 case "png":
00687 ImagePNG($this->map_image, $this->getMapWorkCopyName());
00688 break;
00689 }
00690
00691 ImageDestroy($this->map_image);
00692 }
00693
00697 function &getMapWorkImage()
00698 {
00699 return $this->map_image;
00700 }
00701
00702
00706 function getMapAreasXML($a_insert_inst = false, $a_inst = 0)
00707 {
00708 $xml = "";
00709
00710
00711 for ($i=0; $i < count($this->mapareas); $i++)
00712 {
00713 $area =& $this->mapareas[$i];
00714 $xml .= "<MapArea Shape=\"".$area->getShape()."\" Coords=\"".$area->getCoords()."\">";
00715 if ($area->getLinkType() == IL_INT_LINK)
00716 {
00717 $target_frame = $area->getTargetFrame();
00718
00719 if ($area->getType() == "GlossaryItem" && $target_frame == "")
00720 {
00721 $target_frame = "Glossary";
00722 }
00723
00724 $xml .= "<IntLink Target=\"".$area->getTarget($a_insert_inst, $a_inst)."\" Type=\"".
00725 $area->getType()."\" TargetFrame=\"".$target_frame."\">";
00726 $xml .= $area->getTitle();
00727 $xml .="</IntLink>";
00728 }
00729 else
00730 {
00731 $xml .= "<ExtLink Href=\"".str_replace("&", "&",$area->getHref())."\" Title=\"".
00732 $area->getExtTitle()."\">";
00733 $xml .= $area->getTitle();
00734 $xml .="</ExtLink>";
00735 }
00736 $xml .= "</MapArea>";
00737 }
00738
00739 return $xml;
00740 }
00741
00742
00748 function _resolveMapAreaLinks($a_mob_id)
00749 {
00750
00751
00752 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."' ".
00753 "ORDER BY nr";
00754 $item_set = $this->ilias->db->query($query);
00755 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00756 {
00757 ilMapArea::_resolveIntLinks($item_rec["id"]);
00758 }
00759 }
00760
00766 function _getMapAreasIntLinks($a_mob_id)
00767 {
00768
00769 $query = "SELECT * FROM media_item WHERE mob_id = '".$a_mob_id."' ".
00770 "ORDER BY nr";
00771 $item_set = $this->ilias->db->query($query);
00772 $links = array();
00773 while ($item_rec = $item_set->fetchRow(DB_FETCHMODE_ASSOC))
00774 {
00775 $map_links = ilMapArea::_getIntLinks($item_rec["id"]);
00776 foreach($map_links as $key => $map_link)
00777 {
00778 $links[$key] = $map_link;
00779 }
00780 }
00781 return $links;
00782 }
00783
00784 }
00785 ?>