ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjWiki.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
8 include_once "./Services/Object/classes/class.ilObject.php";
9 include_once ("./Modules/Wiki/classes/class.ilWikiUtil.php");
10 
19 class ilObjWiki extends ilObject
20 {
21  protected $online = false;
22  protected $public_notes = true;
23 
30  function ilObjWiki($a_id = 0,$a_call_by_reference = true)
31  {
32  $this->type = "wiki";
33  $this->ilObject($a_id,$a_call_by_reference);
34  }
35 
41  function setOnline($a_online)
42  {
43  $this->online = $a_online;
44  }
45 
51  function getOnline()
52  {
53  return $this->online;
54  }
55 
61  function setRating($a_rating)
62  {
63  $this->rating = (bool)$a_rating;
64  }
65 
71  function getRating()
72  {
73  return $this->rating;
74  }
75 
81  function setRatingAsBlock($a_rating)
82  {
83  $this->rating_block = (bool)$a_rating;
84  }
85 
91  function getRatingAsBlock()
92  {
93  return $this->rating_block;
94  }
95 
101  function setRatingForNewPages($a_rating)
102  {
103  $this->rating_new_pages = (bool)$a_rating;
104  }
105 
112  {
113  return $this->rating_new_pages;
114  }
115 
121  function setRatingCategories($a_rating)
122  {
123  $this->rating_categories = (bool)$a_rating;
124  }
125 
132  {
133  return $this->rating_categories;
134  }
135 
139  public function setPublicNotes($a_val)
140  {
141  $this->public_notes = $a_val;
142  }
143 
147  public function getPublicNotes()
148  {
149  return $this->public_notes;
150  }
151 
157  public function setImportantPages($a_val)
158  {
159  $this->imp_pages = $a_val;
160  }
161 
167  public function getImportantPages()
168  {
169  return $this->imp_pages;
170  }
171 
177  function setStartPage($a_startpage)
178  {
179  $this->startpage = ilWikiUtil::makeDbTitle($a_startpage);
180  }
181 
187  function getStartPage()
188  {
189  return $this->startpage;
190  }
191 
197  function setShortTitle($a_shorttitle)
198  {
199  $this->shorttitle = $a_shorttitle;
200  }
201 
207  function getShortTitle()
208  {
209  return $this->shorttitle;
210  }
211 
217  function setIntroduction($a_introduction)
218  {
219  $this->introduction = $a_introduction;
220  }
221 
227  function getIntroduction()
228  {
229  return $this->introduction;
230  }
231 
235  function getStyleSheetId()
236  {
237  return $this->style_id;
238  }
239 
243  function setStyleSheetId($a_style_id)
244  {
245  $this->style_id = $a_style_id;
246  }
247 
253  public function setPageToc($a_val)
254  {
255  $this->page_toc = $a_val;
256  }
257 
263  public function getPageToc()
264  {
265  return $this->page_toc;
266  }
267 
273  static function isOnlineHelpWiki($a_ref_id)
274  {
275  if ($a_ref_id > 0 && $a_ref_id == OH_REF_ID)
276  {
277 // return true;
278  }
279  return false;
280  }
281 
285  function create($a_prevent_start_page_creation = false)
286  {
287  global $ilDB;
288 
289  parent::create();
290 
291  $ilDB->insert("il_wiki_data", array(
292  "id" => array("integer", $this->getId()),
293  "is_online" => array("integer", (int) $this->getOnline()),
294  "startpage" => array("text", $this->getStartPage()),
295  "short" => array("text", $this->getShortTitle()),
296  "rating" => array("integer", (int) $this->getRating()),
297  "public_notes" => array("integer", (int) $this->getPublicNotes()),
298  "introduction" => array("clob", $this->getIntroduction())
299  ));
300 
301  // create start page
302  if ($this->getStartPage() != "" && !$a_prevent_start_page_creation)
303  {
304  include_once("./Modules/Wiki/classes/class.ilWikiPage.php");
305  $start_page = new ilWikiPage();
306  $start_page->setWikiId($this->getId());
307  $start_page->setTitle($this->getStartPage());
308  $start_page->create();
309  }
310 
311  if (((int) $this->getStyleSheetId()) > 0)
312  {
313  include_once("./Services/Style/classes/class.ilObjStyleSheet.php");
315  }
316  }
317 
324  function update($a_prevent_start_page_creation = false)
325  {
326  global $ilDB;
327 
328  if (!parent::update())
329  {
330  return false;
331  }
332 
333  $ilDB->update("il_wiki_data", array(
334  "is_online" => array("integer", $this->getOnline()),
335  "startpage" => array("text", $this->getStartPage()),
336  "short" => array("text", $this->getShortTitle()),
337  "rating" => array("integer", $this->getRating()),
338  "rating_side" => array("integer", $this->getRatingAsBlock()),
339  "rating_new" => array("integer", $this->getRatingForNewPages()),
340  "rating_ext" => array("integer", $this->getRatingCategories()),
341  "public_notes" => array("integer", $this->getPublicNotes()),
342  "introduction" => array("clob", $this->getIntroduction()),
343  "imp_pages" => array("integer", $this->getImportantPages()),
344  "page_toc" => array("integer", $this->getPageToc())
345  ), array(
346  "id" => array("integer", $this->getId())
347  ));
348 
349  // check whether start page exists
350  include_once("./Modules/Wiki/classes/class.ilWikiPage.php");
351  if (!ilWikiPage::exists($this->getId(), $this->getStartPage())
352  && !$a_prevent_start_page_creation)
353  {
354  $start_page = new ilWikiPage();
355  $start_page->setWikiId($this->getId());
356  $start_page->setTitle($this->getStartPage());
357  $start_page->create();
358  }
359 
360  include_once("./Services/Style/classes/class.ilObjStyleSheet.php");
362 
363  return true;
364  }
365 
369  function read()
370  {
371  global $ilDB;
372 
373  parent::read();
374 
375  $query = "SELECT * FROM il_wiki_data WHERE id = ".
376  $ilDB->quote($this->getId(), "integer");
377  $set = $ilDB->query($query);
378  $rec = $ilDB->fetchAssoc($set);
379 
380  $this->setOnline($rec["is_online"]);
381  $this->setStartPage($rec["startpage"]);
382  $this->setShortTitle($rec["short"]);
383  $this->setRating($rec["rating"]);
384  $this->setRatingAsBlock($rec["rating_side"]);
385  $this->setRatingForNewPages($rec["rating_new"]);
386  $this->setRatingCategories($rec["rating_ext"]);
387  $this->setPublicNotes($rec["public_notes"]);
388  $this->setIntroduction($rec["introduction"]);
389  $this->setImportantPages($rec["imp_pages"]);
390  $this->setPageToc($rec["page_toc"]);
391 
392  include_once("./Services/Style/classes/class.ilObjStyleSheet.php");
394 
395  }
396 
397 
404  function delete()
405  {
406  global $ilDB;
407 
408  // always call parent delete function first!!
409  if (!parent::delete())
410  {
411  return false;
412  }
413 
414  // delete record of table il_wiki_data
415  $query = "DELETE FROM il_wiki_data".
416  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
417  $ilDB->manipulate($query);
418 
419  // remove all notifications
420  include_once "./Services/Notification/classes/class.ilNotification.php";
422 
423  include_once("./Modules/Wiki/classes/class.ilWikiPage.php");
425 
426  return true;
427  }
428 
432  static function checkShortTitleAvailability($a_short_title)
433  {
434  global $ilDB;
435 
436  $res = $ilDB->queryF("SELECT id FROM il_wiki_data WHERE short = %s",
437  array("text"), array($a_short_title));
438  if ($ilDB->fetchAssoc($res))
439  {
440  return false;
441  }
442 
443  return true;
444  }
445 
455  function initDefaultRoles()
456  {
457  global $rbacadmin;
458 
459  // create a local role folder
460  //$rfoldObj = $this->createRoleFolder("Local roles","Role Folder of forum obj_no.".$this->getId());
461 
462  // create moderator role and assign role to rolefolder...
463  //$roleObj = $rfoldObj->createRole("Moderator","Moderator of forum obj_no.".$this->getId());
464  //$roles[] = $roleObj->getId();
465 
466  //unset($rfoldObj);
467  //unset($roleObj);
468 
469  return $roles ? $roles : array();
470  }
471 
485  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
486  {
487  global $tree;
488 
489  switch ($a_event)
490  {
491  case "link":
492 
493  //var_dump("<pre>",$a_params,"</pre>");
494  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
495  //exit;
496  break;
497 
498  case "cut":
499 
500  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
501  //exit;
502  break;
503 
504  case "copy":
505 
506  //var_dump("<pre>",$a_params,"</pre>");
507  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
508  //exit;
509  break;
510 
511  case "paste":
512 
513  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
514  //exit;
515  break;
516 
517  case "new":
518 
519  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
520  //exit;
521  break;
522  }
523 
524  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
525  if ($a_node_id==$_GET["ref_id"])
526  {
527  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
528  $parent_type = $parent_obj->getType();
529  if($parent_type == $this->getType())
530  {
531  $a_node_id = (int) $tree->getParentId($a_node_id);
532  }
533  }
534 
535  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
536  }
537 
545  static function _lookupRating($a_wiki_id)
546  {
547  return ilObjWiki::_lookup($a_wiki_id, "rating");
548  }
549 
557  static function _lookupRatingCategories($a_wiki_id)
558  {
559  return ilObjWiki::_lookup($a_wiki_id, "rating_ext");
560  }
561 
569  static function _lookupRatingAsBlock($a_wiki_id)
570  {
571  return ilObjWiki::_lookup($a_wiki_id, "rating_side");
572  }
573 
581  static function _lookupPublicNotes($a_wiki_id)
582  {
583  return ilObjWiki::_lookup($a_wiki_id, "public_notes");
584  }
585 
594  private static function _lookup($a_wiki_id, $a_field)
595  {
596  global $ilDB;
597 
598  $query = "SELECT $a_field FROM il_wiki_data WHERE id = ".
599  $ilDB->quote($a_wiki_id, "integer");
600  $set = $ilDB->query($query);
601  $rec = $ilDB->fetchAssoc($set);
602  return $rec[$a_field];
603  }
604 
612  static function _lookupStartPage($a_wiki_id)
613  {
614  return ilObjWiki::_lookup($a_wiki_id, "startpage");
615  }
616 
620  static function writeStartPage($a_id, $a_name)
621  {
622  global $ilDB;
623 
624  include_once("./Modules/Wiki/classes/class.ilWikiUtil.php");
625  $ilDB->manipulate("UPDATE il_wiki_data SET ".
626  " startpage = ".$ilDB->quote(ilWikiUtil::makeDbTitle($a_name), "text").
627  " WHERE id = ".$ilDB->quote($a_id, "integer")
628  );
629  }
630 
634  static function _performSearch($a_wiki_id, $a_searchterm)
635  {
636  // query parser
637  include_once 'Services/Search/classes/class.ilQueryParser.php';
638 
639  $query_parser = new ilQueryParser($a_searchterm);
640  $query_parser->setCombination("or");
641  $query_parser->parse();
642 
643  include_once 'Services/Search/classes/class.ilSearchResult.php';
644  $search_result = new ilSearchResult();
645  if($query_parser->validate())
646  {
647 
648  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
649  $wiki_search =& ilObjectSearchFactory::_getWikiContentSearchInstance($query_parser);
650  $wiki_search->setFilter(array('wpg'));
651  $search_result->mergeEntries($wiki_search->performSearch());
652  }
653 
654  $entries = $search_result->getEntries();
655 
656  $found_pages = array();
657  foreach($entries as $entry)
658  {
659  if ($entry["obj_id"] == $a_wiki_id && is_array($entry["child"]))
660  {
661  foreach($entry["child"] as $child)
662  {
663  $found_pages[] = $child;
664  }
665  }
666  }
667 
668  return $found_pages;
669  }
670 
671  //
672  // Important pages
673  //
674 
682  static function _lookupImportantPages($a_wiki_id)
683  {
684  return ilObjWiki::_lookup($a_wiki_id, "imp_pages");
685  }
686 
693  static function _lookupImportantPagesList($a_wiki_id)
694  {
695  global $ilDB;
696 
697  $set = $ilDB->query("SELECT * FROM il_wiki_imp_pages WHERE ".
698  " wiki_id = ".$ilDB->quote($a_wiki_id, "integer")." ORDER BY ord ASC "
699  );
700 
701  $imp_pages = array();
702 
703  while ($rec = $ilDB->fetchAssoc($set))
704  {
705  $imp_pages[] = $rec;
706  }
707  return $imp_pages;
708  }
709 
716  static function _lookupMaxOrdNrImportantPages($a_wiki_id)
717  {
718  global $ilDB;
719 
720  $set = $ilDB->query("SELECT MAX(ord) as m FROM il_wiki_imp_pages WHERE ".
721  " wiki_id = ".$ilDB->quote($a_wiki_id, "integer")
722  );
723 
724  $rec = $ilDB->fetchAssoc($set);
725  return (int) $rec["m"];
726  }
727 
728 
734  function addImportantPage($a_page_id, $a_nr = 0, $a_indent = 0)
735  {
736  global $ilDB;
737 
738  if (!$this->isImportantPage($a_page_id))
739  {
740  if ($a_nr == 0)
741  {
742  $a_nr = ilObjWiki::_lookupMaxOrdNrImportantPages($this->getId()) + 10;
743  }
744 
745  $ilDB->manipulate("INSERT INTO il_wiki_imp_pages ".
746  "(wiki_id, ord, indent, page_id) VALUES (".
747  $ilDB->quote($this->getId(), "integer").",".
748  $ilDB->quote($a_nr, "integer").",".
749  $ilDB->quote($a_indent, "integer").",".
750  $ilDB->quote($a_page_id, "integer").
751  ")");
752  }
753  }
754 
761  function isImportantPage($a_page_id)
762  {
763  global $ilDB;
764 
765  $set = $ilDB->query("SELECT * FROM il_wiki_imp_pages WHERE ".
766  " wiki_id = ".$ilDB->quote($this->getId(), "integer")." AND ".
767  " page_id = ".$ilDB->quote($a_page_id, "integer")
768  );
769  if ($rec = $ilDB->fetchAssoc($set))
770  {
771  return true;
772  }
773  return false;
774  }
775 
781  function removeImportantPage($a_id)
782  {
783  global $ilDB;
784 
785  $ilDB->manipulate("DELETE FROM il_wiki_imp_pages WHERE "
786  ." wiki_id = ".$ilDB->quote($this->getId(), "integer")
787  ." AND page_id = ".$ilDB->quote($a_id, "integer")
788  );
789 
791  }
792 
799  function saveOrderingAndIndentation($a_ord, $a_indent)
800  {
801  global $ilDB;
802 
803  $ipages = ilObjWiki::_lookupImportantPagesList($this->getId());
804 
805  foreach ($ipages as $k => $v)
806  {
807  if (isset($a_ord[$v["page_id"]]))
808  {
809  $ipages[$k]["ord"] = (int) $a_ord[$v["page_id"]];
810  }
811  if (isset($a_indent[$v["page_id"]]))
812  {
813  $ipages[$k]["indent"] = (int) $a_indent[$v["page_id"]];
814  }
815  }
816  $ipages = ilUtil::sortArray($ipages, "ord", "asc", true);
817 
818  // fix indentation: no 2 is allowed after a 0
819  $c_indent = 0;
820  $fixed = false;
821  foreach ($ipages as $k => $v)
822  {
823  if ($ipages[$k]["indent"] == 2 && $c_indent == 0)
824  {
825  $ipages[$k]["indent"] = 1;
826  $fixed = true;
827  }
828  $c_indent = $ipages[$k]["indent"];
829  }
830 
831  $ord = 10;
832  reset($ipages);
833  foreach ($ipages as $k => $v)
834  {
835  $ilDB->manipulate($q = "UPDATE il_wiki_imp_pages SET ".
836  " ord = ".$ilDB->quote($ord, "integer").",".
837  " indent = ".$ilDB->quote($v["indent"], "integer").
838  " WHERE wiki_id = ".$ilDB->quote($v["wiki_id"], "integer").
839  " AND page_id = ".$ilDB->quote($v["page_id"], "integer")
840  );
841  $ord+=10;
842  }
843 
844  return $fixed;
845  }
846 
851  {
852  global $ilDB;
853 
854  $ipages = ilObjWiki::_lookupImportantPagesList($this->getId());
855 
856  // fix indentation: no 2 is allowed after a 0
857  $c_indent = 0;
858  $fixed = false;
859  foreach ($ipages as $k => $v)
860  {
861  if ($ipages[$k]["indent"] == 2 && $c_indent == 0)
862  {
863  $ipages[$k]["indent"] = 1;
864  $fixed = true;
865  }
866  $c_indent = $ipages[$k]["indent"];
867  }
868 
869  $ord = 10;
870  foreach ($ipages as $k => $v)
871  {
872  $ilDB->manipulate($q = "UPDATE il_wiki_imp_pages SET ".
873  " ord = ".$ilDB->quote($ord, "integer").
874  ", indent = ".$ilDB->quote($v["indent"], "integer").
875  " WHERE wiki_id = ".$ilDB->quote($v["wiki_id"], "integer").
876  " AND page_id = ".$ilDB->quote($v["page_id"], "integer")
877  );
878  $ord+=10;
879  }
880 
881  }
882 
883  //
884  // Page TOC
885  //
886 
894  static function _lookupPageToc($a_wiki_id)
895  {
896  return ilObjWiki::_lookup($a_wiki_id, "page_toc");
897  }
898 
905  public function cloneObject($a_target_id,$a_copy_id = 0)
906  {
907  global $ilDB, $ilUser, $ilias;
908 
909  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
910 
911  $new_obj->setTitle($this->getTitle());
912  $new_obj->setStartPage($this->getStartPage());
913  $new_obj->setShortTitle($this->getShortTitle());
914  $new_obj->setRating($this->getRating());
915  $new_obj->setRatingAsBlock($this->getRatingAsBlock());
916  $new_obj->setRatingForNewPages($this->getRatingForNewPages());
917  $new_obj->setRatingCategories($this->getRatingCategories());
918  $new_obj->setPublicNotes($this->getPublicNotes());
919  $new_obj->setIntroduction($this->getIntroduction());
920  $new_obj->setImportantPages($this->getImportantPages());
921  $new_obj->setPageToc($this->getPageToc());
922  $new_obj->update();
923 
924  // set/copy stylesheet
925  include_once("./Services/Style/classes/class.ilObjStyleSheet.php");
926  $style_id = $this->getStyleSheetId();
927  if ($style_id > 0 && !ilObjStyleSheet::_lookupStandard($style_id))
928  {
929  $style_obj = $ilias->obj_factory->getInstanceByObjId($style_id);
930  $new_id = $style_obj->ilClone();
931  $new_obj->setStyleSheetId($new_id);
932  $new_obj->update();
933  }
934 
935  // copy content
936  include_once("./Modules/Wiki/classes/class.ilWikiPage.php");
937  $pages = ilWikiPage::getAllPages($this->getId());
938  if (count($pages) > 0)
939  {
940  // if we have any pages, delete the start page first
941  $pg_id = ilWikiPage::getPageIdForTitle($new_obj->getId(), $new_obj->getStartPage());
942  $start_page = new ilWikiPage($pg_id);
943  $start_page->delete();
944  }
945  $map = array();
946  foreach ($pages as $p)
947  {
948  $page = new ilWikiPage($p["id"]);
949  $new_page = new ilWikiPage();
950  $new_page->setTitle($page->getTitle());
951  $new_page->setWikiId($new_obj->getId());
952  $new_page->setTitle($page->getTitle());
953  $new_page->setBlocked($page->getBlocked());
954  $new_page->setRating($page->getRating());
955  $new_page->create();
956 
957  $new_page->setXMLContent($page->copyXMLContent(true));
958  $new_page->buildDom();
959  $new_page->update();
960  $map[$p["id"]] = $new_page->getId();
961  }
962 
963  // copy important pages
964  foreach (ilObjWiki::_lookupImportantPagesList($this->getId()) as $ip)
965  {
966  $new_obj->addImportantPage($map[$ip["page_id"]], $ip["ord"], $ip["indent"]);
967  }
968 
969  // copy rating categories
970  include_once("./Services/Rating/classes/class.ilRatingCategory.php");
971  foreach (ilRatingCategory::getAllForObject($this->getId()) as $rc)
972  {
973  $new_rc = new ilRatingCategory();
974  $new_rc->setParentId($new_obj->getId());
975  $new_rc->setTitle($rc["title"]);
976  $new_rc->setDescription($rc["description"]);
977  $new_rc->save();
978  }
979 
980  return $new_obj;
981  }
982 
983 }
984 ?>