ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGlossaryDefinition.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/COPage/classes/class.ilPageObject.php");
25 
35 {
36  var $ilias;
37  var $lng;
38  var $tpl;
39 
40  var $id;
41  var $term_id;
42  var $glo_id;
45  var $nr;
46 
51  function ilGlossaryDefinition($a_id = 0)
52  {
53  global $lng, $ilias, $tpl;
54 
55  $this->lng =& $lng;
56  $this->ilias =& $ilias;
57  $this->tpl =& $tpl;
58 
59  $this->id = $a_id;
60  if ($a_id != 0)
61  {
62  $this->read();
63  }
64  }
65 
69  function read()
70  {
71  global $ilDB;
72 
73  $q = "SELECT * FROM glossary_definition WHERE id = ".$ilDB->quote($this->id);
74  $def_set = $this->ilias->db->query($q);
75  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
76 
77  $this->setTermId($def_rec["term_id"]);
78  $this->setShortText($def_rec["short_text"]);
79  $this->setNr($def_rec["nr"]);
80 
81  $this->page_object =& new ilPageObject("gdf", $this->id);
82  }
83 
84  function setId($a_id)
85  {
86  $this->id = $a_id;
87  }
88 
89  function getId()
90  {
91  return $this->id;
92  }
93 
94  function getType()
95  {
96  return "gdf";
97  }
98 
99  function setTermId($a_term_id)
100  {
101  $this->term_id = $a_term_id;
102  }
103 
104  function getTermId()
105  {
106  return $this->term_id;
107  }
108 
109  function setShortText($a_text)
110  {
111  $this->short_text = $a_text;
112  }
113 
114  function getShortText()
115  {
116  return $this->short_text;
117  }
118 
119  function setNr($a_nr)
120  {
121  $this->nr = $a_nr;
122  }
123 
124  function getNr()
125  {
126  return $this->nr;
127  }
128 
129  function assignPageObject(&$a_page_object)
130  {
131  $this->page_object =& $a_page_object;
132  }
133 
134  function &getPageObject()
135  {
136  return $this->page_object;
137  }
138 
144  function getTitle()
145  {
146  return $this->title;
147  }
148 
152  function setTitle($a_title)
153  {
154  $this->title = $a_title;
155  }
156 
162  function getDescription()
163  {
164  return $this->description;
165  }
166 
170  function setDescription($a_description)
171  {
172  $this->description = $a_description;
173  }
174 
175  function create($a_upload = false)
176  {
177  global $ilDB;
178 
179  $term =& new ilGlossaryTerm($this->getTermId());
180 
181  // lock glossary_definition table
182  $q = "LOCK TABLES glossary_definition WRITE";
183  $this->ilias->db->query($q);
184 
185  // get maximum definition number
186  $q = "SELECT max(nr) AS max_nr FROM glossary_definition WHERE term_id = ".$ilDB->quote($this->getTermId());
187  $max_set = $this->ilias->db->query($q);
188  $max_rec = $max_set->fetchRow(DB_FETCHMODE_ASSOC);
189  $max = (int) $max_rec["max_nr"];
190 
191  // insert new definition record
192  $q = "INSERT INTO glossary_definition (term_id, short_text, nr)".
193  " VALUES (".$ilDB->quote($this->getTermId()).",".
194  $ilDB->quote($this->getShortText()).", ".$ilDB->quote(($max + 1)).")";
195  $this->ilias->db->query($q);
196 
197  // unlock glossary definition table
198  $q = "UNLOCK TABLES";
199  $this->ilias->db->query($q);
200 
201  $this->setId($this->ilias->db->getLastInsertId());
202 
203  // get number
204  $q = "SELECT nr FROM glossary_definition WHERE id = ".$ilDB->quote($this->id);
205  $def_set = $this->ilias->db->query($q);
206  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
207  $this->setNr($def_rec["nr"]);
208 
209  // meta data will be created by
210  // import parser
211  if (!$a_upload)
212  {
213  $this->createMetaData();
214  }
215 
216  $this->page_object =& new ilPageObject("gdf");
217  $this->page_object->setId($this->getId());
218  $this->page_object->setParentId($term->getGlossaryId());
219  $this->page_object->create();
220  }
221 
222  function delete()
223  {
224  global $ilDB;
225 
226  // lock glossary_definition table
227  $q = "LOCK TABLES glossary_definition WRITE";
228  $this->ilias->db->query($q);
229 
230  // be sure to get the right number
231  $q = "SELECT * FROM glossary_definition WHERE id = ".$ilDB->quote($this->id);
232  $def_set = $this->ilias->db->query($q);
233  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
234  $this->setNr($def_rec["nr"]);
235 
236  // update numbers of other definitions
237  $q = "UPDATE glossary_definition SET ".
238  " nr = nr - 1 ".
239  " WHERE term_id = ".$ilDB->quote($this->getTermId())." ".
240  " AND nr > ".$ilDB->quote($this->getNr());
241  $this->ilias->db->query($q);
242 
243  // delete current definition
244  $q = "DELETE FROM glossary_definition ".
245  " WHERE id = ".$ilDB->quote($this->getId())." ";
246  $this->ilias->db->query($q);
247 
248  // unlock glossary_definition table
249  $q = "UNLOCK TABLES";
250  $this->ilias->db->query($q);
251 
252  // delete page and meta data
253  $this->page_object->delete();
254 
255  // delete meta data
256  $this->deleteMetaData();
257 /*
258  $nested = new ilNestedSetXML();
259  $nested->init($this->getId(), $this->getType());
260  $nested->deleteAllDBData();
261 */
262  }
263 
264 
265  function moveUp()
266  {
267  global $ilDB;
268 
269  // lock glossary_definition table
270  $q = "LOCK TABLES glossary_definition WRITE";
271  $this->ilias->db->query($q);
272 
273  // be sure to get the right number
274  $q = "SELECT * FROM glossary_definition WHERE id = ".$ilDB->quote($this->id);
275  $def_set = $this->ilias->db->query($q);
276  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
277  $this->setNr($def_rec["nr"]);
278 
279  if ($this->getNr() < 2)
280  {
281  $q = "UNLOCK TABLES";
282  $this->ilias->db->query($q);
283  return;
284  }
285 
286  // update numbers of other definitions
287  $q = "UPDATE glossary_definition SET ".
288  " nr = nr + 1 ".
289  " WHERE term_id = ".$ilDB->quote($this->getTermId())." ".
290  " AND nr = ".$ilDB->quote(($this->getNr() - 1));
291  $this->ilias->db->query($q);
292 
293  // delete current definition
294  $q = "UPDATE glossary_definition SET ".
295  " nr = nr - 1 ".
296  " WHERE term_id = ".$ilDB->quote($this->getTermId())." ".
297  " AND id = ".$ilDB->quote($this->getId());
298  $this->ilias->db->query($q);
299 
300  // unlock glossary_definition table
301  $q = "UNLOCK TABLES";
302  $this->ilias->db->query($q);
303 
304  }
305 
306 
307  function moveDown()
308  {
309  global $ilDB;
310 
311  // lock glossary_definition table
312  $q = "LOCK TABLES glossary_definition WRITE";
313  $this->ilias->db->query($q);
314 
315  // be sure to get the right number
316  $q = "SELECT * FROM glossary_definition WHERE id = ".$ilDB->quote($this->id);
317  $def_set = $this->ilias->db->query($q);
318  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
319  $this->setNr($def_rec["nr"]);
320 
321  // get max number
322  $q = "SELECT max(nr) as max_nr FROM glossary_definition WHERE term_id = ".
323  $ilDB->quote($this->getTermId());
324  $max_set = $this->ilias->db->query($q);
325  $max_rec = $max_set->fetchRow(DB_FETCHMODE_ASSOC);
326 
327  if ($this->getNr() >= $max_rec["max_nr"])
328  {
329  $q = "UNLOCK TABLES";
330  $this->ilias->db->query($q);
331  return;
332  }
333 
334  // update numbers of other definitions
335  $q = "UPDATE glossary_definition SET ".
336  " nr = nr - 1 ".
337  " WHERE term_id = ".$ilDB->quote($this->getTermId())." ".
338  " AND nr = ".$ilDB->quote(($this->getNr() + 1));
339  $this->ilias->db->query($q);
340 
341  // delete current definition
342  $q = "UPDATE glossary_definition SET ".
343  " nr = nr + 1 ".
344  " WHERE term_id = ".$ilDB->quote($this->getTermId())." ".
345  " AND id = ".$ilDB->quote($this->getId());
346  $this->ilias->db->query($q);
347 
348  // unlock glossary_definition table
349  $q = "UNLOCK TABLES";
350  $this->ilias->db->query($q);
351 
352  }
353 
354 
355  function update()
356  {
357  global $ilDB;
358 
359  $this->updateMetaData();
360 
361  $q = "UPDATE glossary_definition SET ".
362  " term_id = ".$ilDB->quote($this->getTermId()).", ".
363  " nr = ".$ilDB->quote($this->getNr()).", ".
364  " short_text = ".$ilDB->quote($this->getShortText())." ".
365  " WHERE id = ".$ilDB->quote($this->getId());
366  $this->ilias->db->query($q);
367  }
368 
369  function updateShortText()
370  {
371  $this->page_object->buildDom();
372  $text = $this->page_object->getFirstParagraphText();
373 
374  //$this->setShortText(ilUtil::shortenText($text, 180, true));
375  $text = str_replace("<br/>", "<br>", $text);
376  $text = strip_tags($text, "<br>");
377  if (is_int(strpos(substr($text, 175, 10), "[tex]")))
378  {
379  $offset = 5;
380  }
381  $short = ilUtil::shortenText($text, 180 + $offset, true);
382 
383  // make short text longer, if tex end tag is missing
384  $ltexs = strrpos($short, "[tex]");
385  $ltexe = strrpos($short, "[/tex]");
386  if ($ltexs > $ltexe)
387  {
388  $ltexe = strpos($text, "[/tex]", $ltexs);
389  if ($ltexe > 0)
390  {
391  $short = ilUtil::shortenText($text, $ltexe+6, true);
392  }
393  }
394  $this->setShortText($short);
395  $this->update();
396  }
397 
401  function getDefinitionList($a_term_id)
402  {
403  global $ilDB;
404 
405  $defs = array();
406  $q = "SELECT * FROM glossary_definition WHERE term_id = ".$ilDB->quote($a_term_id).
407  " ORDER BY nr";
408  $def_set = $ilDB->query($q);
409  while ($def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC))
410  {
411  $defs[] = array("term_id" => $def_rec["term_id"],
412  "page_id" => $def_rec["page_id"], "id" => $def_rec["id"],
413  "short_text" => strip_tags($def_rec["short_text"], "<br>"),
414  "nr" => $def_rec["nr"]);
415  }
416  return $defs;
417  }
418 
422  function exportXML(&$a_xml_writer, $a_inst)
423  {
424  $attrs = array();
425  $a_xml_writer->xmlStartTag("Definition", $attrs);
426 
427  $this->exportXMLMetaData($a_xml_writer);
428  $this->exportXMLDefinition($a_xml_writer, $a_inst);
429 
430  $a_xml_writer->xmlEndTag("Definition");
431  }
432 
433 
440  function exportXMLMetaData(&$a_xml_writer)
441  {
443  include_once("Services/MetaData/classes/class.ilMD2XML.php");
444  $md2xml = new ilMD2XML($glo_id, $this->getId(), $this->getType());
445  $md2xml->setExportMode(true);
446  $md2xml->startExport();
447  $a_xml_writer->appendXML($md2xml->getXML());
448  }
449 
453  function modifyExportIdentifier($a_tag, $a_param, $a_value)
454  {
455  if ($a_tag == "Identifier" && $a_param == "Entry")
456  {
457  $a_value = "il_".IL_INST_ID."_gdf_".$this->getId();
458  }
459 
460  return $a_value;
461  }
462 
463 
470  function exportXMLDefinition(&$a_xml_writer, $a_inst = 0)
471  {
472 
473  $this->page_object->buildDom();
474  $this->page_object->insertInstIntoIDs($a_inst);
475  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
476  $this->files_contained = $this->page_object->collectFileItems();
477  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
478  $xml = str_replace("&","&amp;", $xml);
479  $a_xml_writer->appendXML($xml);
480 
481  $this->page_object->freeDom();
482  }
483 
487  function createMetaData()
488  {
489  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
490 
491  global $ilUser;
492 
495  $md_creator = new ilMDCreator($glo_id,$this->getId(),$this->getType());
496  $md_creator->setTitle($this->getTitle());
497  $md_creator->setTitleLanguage($lang);
498  $md_creator->setDescription($this->getDescription());
499  $md_creator->setDescriptionLanguage($lang);
500  $md_creator->setKeywordLanguage($lang);
501  $md_creator->setLanguage($lang);
502  $md_creator->create();
503 
504  return true;
505  }
506 
510  function updateMetaData()
511  {
512  include_once("Services/MetaData/classes/class.ilMD.php");
513  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
514  include_once("Services/MetaData/classes/class.ilMDDescription.php");
515 
517  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
518  $md_gen =& $md->getGeneral();
519  $md_gen->setTitle($this->getTitle());
520 
521  // sets first description (maybe not appropriate)
522  $md_des_ids =& $md_gen->getDescriptionIds();
523  if (count($md_des_ids) > 0)
524  {
525  $md_des =& $md_gen->getDescription($md_des_ids[0]);
526  $md_des->setDescription($this->getDescription());
527  $md_des->update();
528  }
529  $md_gen->update();
530  }
531 
535  function deleteMetaData()
536  {
537  // Delete meta data
538  include_once('Services/MetaData/classes/class.ilMD.php');
540  $md = new ilMD($glo_id, $this->getId(), $this->getType());
541  $md->deleteAll();
542  }
543 
558  function MDUpdateListener($a_element)
559  {
560  include_once 'Services/MetaData/classes/class.ilMD.php';
561 
562  switch($a_element)
563  {
564  case 'General':
565 
566  // Update Title and description
568  $md =& new ilMD($glo_id, $this->getId(), $this->getType());
569  $md_gen = $md->getGeneral();
570 
571  //ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
572  $this->setTitle($md_gen->getTitle());
573 
574  foreach($md_gen->getDescriptionIds() as $id)
575  {
576  $md_des = $md_gen->getDescription($id);
577  //ilObject::_writeDescription($this->getId(),$md_des->getDescription());
578  $this->setDescription($md_des->getDescription());
579  break;
580  }
581 
582  break;
583 
584  default:
585  }
586  return true;
587  }
588 
594  function _lookupTermId($a_def_id)
595  {
596  global $ilDB;
597 
598  $q = "SELECT * FROM glossary_definition WHERE id = ".$ilDB->quote($a_def_id);
599  $def_set = $ilDB->query($q);
600  $def_rec = $def_set->fetchRow(DB_FETCHMODE_ASSOC);
601 
602  return $def_rec["term_id"];
603  }
604 } // END class ilGlossaryDefinition
605 
606 ?>