ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPlugin.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 
5 include_once("./Services/Component/classes/class.ilComponent.php");
6 include_once("./Services/Component/exceptions/class.ilPluginException.php");
7 
16 abstract class ilPlugin
17 {
21  final function __construct()
22  {
23  $this->__init();
24  }
25 
34  abstract function getComponentType();
35 
44  abstract function getComponentName();
45 
54  abstract function getSlot();
55 
64  abstract function getSlotId();
65 
75  abstract function getPluginName();
76 
82  private final function setId($a_id)
83  {
84  $this->id = $a_id;
85  }
86 
92  final function getId()
93  {
94  return $this->id;
95  }
96 
102  private final function setLastUpdateVersion($a_lastupdateversion)
103  {
104  $this->lastupdateversion = $a_lastupdateversion;
105  }
106 
112  final function getLastUpdateVersion()
113  {
114  return $this->lastupdateversion;
115  }
116 
122  private final function setVersion($a_version)
123  {
124  $this->version = $a_version;
125  }
126 
132  final function getVersion()
133  {
134  return $this->version;
135  }
136 
142  private final function setIliasMinVersion($a_iliasminversion)
143  {
144  $this->iliasminversion = $a_iliasminversion;
145  }
146 
152  final function getIliasMinVersion()
153  {
154  return $this->iliasminversion;
155  }
156 
162  private final function setIliasMaxVersion($a_iliasmaxversion)
163  {
164  $this->iliasmaxversion = $a_iliasmaxversion;
165  }
166 
172  final function getIliasMaxVersion()
173  {
174  return $this->iliasmaxversion;
175  }
176 
182  private final function setActive($a_active)
183  {
184  $this->active = $a_active;
185  }
186 
192  final function getActive()
193  {
194  return $this->active;
195  }
196 
202  protected final function setSlotObject($a_slot)
203  {
204  $this->slot = $a_slot;
205  }
206 
212  protected final function getSlotObject()
213  {
214  return $this->slot;
215  }
216 
222  final function setDBVersion($a_dbversion)
223  {
224  $this->dbversion = $a_dbversion;
225  }
226 
232  final function getDBVersion()
233  {
234  return $this->dbversion;
235  }
236 
242  final function writeDBVersion($a_dbversion)
243  {
244  global $ilDB;
245 
246  $this->setDBVersion($a_dbversion);
247 
248  $q = "UPDATE il_plugin SET db_version = ".$ilDB->quote((int) $this->getDBVersion(), "integer").
249  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
250  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
251  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
252  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
253 
254  $ilDB->manipulate($q);
255  }
256 
257 
263  public final function getDirectory()
264  {
265  return $this->getSlotObject()->getPluginsDirectory()."/".$this->getPluginName();
266  }
267 
271  static public final function _getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname)
272  {
273  return ilPluginSlot::_getPluginsDirectory($a_ctype, $a_cname, $a_slot_id)."/".$a_pname;
274 
275  }
276 
277 
283  protected final function getClassesDirectory()
284  {
285  return $this->getDirectory()."/classes";
286  }
287 
291  public final function includeClass($a_class_file_name)
292  {
293  include_once($this->getClassesDirectory()."/".$a_class_file_name);
294  }
295 
301  protected final function getLanguageDirectory()
302  {
303  return $this->getDirectory()."/lang";
304  }
305 
309  static final function getAvailableLangFiles($a_lang_directory)
310  {
311  $langs = array();
312 
313  if (!@is_dir($a_lang_directory))
314  {
315  return array();
316  }
317 
318  $dir = opendir($a_lang_directory);
319  while($file = readdir($dir))
320  {
321  if ($file != "." and
322  $file != "..")
323  {
324  // directories
325  if (@is_file($a_lang_directory."/".$file))
326  {
327  if (substr($file, 0, 6) == "ilias_" &&
328  substr($file, strlen($file) - 5) == ".lang")
329  {
330  $langs[] = array("key" => substr($file, 6, 2), "file" => $file,
331  "path" => $a_lang_directory."/".$file);
332  }
333  }
334  }
335  }
336 
337  return $langs;
338  }
339 
347  static final function hasConfigureClass($a_slot_dir, $a_name)
348  {
349  if (is_file($a_slot_dir."/".
350  $a_name."/classes/class.il".$a_name."ConfigGUI.php"))
351  {
352  return true;
353  }
354  return false;
355  }
356 
363  static final function getConfigureClassName($a_name)
364  {
365  return "il".$a_name."ConfigGUI";
366  }
367 
371  final function getPrefix()
372  {
373  return $this->getSlotObject()->getPrefix()."_".$this->getId();
374  }
375 
381  static public final function getDBUpdateScriptName($a_ctype, $a_cname, $a_slot_name, $a_pname)
382  {
383  return "Customizing/global/plugins/".$a_ctype."/".$a_cname."/".
384  $a_slot_name."/".$a_pname."/sql/dbupdate.php";
385  }
386 
390  final function getTablePrefix()
391  {
392  return $this->getPrefix();
393  }
394 
398  final public function updateLanguages()
399  {
400  global $ilCtrl;
401 
402  include_once("./Services/Language/classes/class.ilObjLanguage.php");
403 
404  $langs = $this->getAvailableLangFiles($this->getLanguageDirectory());
405 
406  $prefix = $this->getPrefix();
407 
408  foreach($langs as $lang)
409  {
410  $txt = file($this->getLanguageDirectory()."/".$lang["file"]);
411  $lang_array = array();
412 
413  // get language data
414  if (is_array($txt))
415  {
416  foreach ($txt as $row)
417  {
418  if ($row[0] != "#" && strpos($row, "#:#") > 0)
419  {
420  $a = explode("#:#",trim($row));
421  $lang_array[$prefix."_".trim($a[0])] = trim($a[1]);
422  ilObjLanguage::replaceLangEntry($prefix, $prefix."_".trim($a[0]),
423  $lang["key"], trim($a[1]));
424 //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
425  }
426  }
427  }
428 
429  ilObjLanguage::replaceLangModule($lang["key"], $prefix,
430  $lang_array);
431  }
432  }
433 
437  function updateDatabase()
438  {
439  global $ilDB, $lng;
440 
441  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
442  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
443  $this->getComponentName(), $this->getSlotId(),
444  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
445 
446  //$dbupdate->getDBVersionStatus();
447  //$dbupdate->getCurrentVersion();
448 
449  $result = $dbupdate->applyUpdate();
450 
451  if ($dbupdate->updateMsg == "no_changes")
452  {
453  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
454  }
455  else
456  {
457  foreach ($dbupdate->updateMsg as $row)
458  {
459  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
460  }
461  }
462 
463  $this->message.= $message;
464 
465  return $result;
466  }
467 
471  public final function loadLanguageModule()
472  {
473  global $lng;
474 
475  $lng->loadLanguageModule($this->getPrefix());
476  }
477 
481  public final function txt($a_var)
482  {
483  global $lng;
484  return $lng->txt($this->getPrefix()."_".$a_var, $this->getPrefix());
485  }
486 
490  static function lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
491  {
492  global $lng;
493  return $lng->_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
494  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
495  }
496 
500  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
501  {
502  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
503 
504  return $tpl;
505  }
506 
510  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
511  $a_pname, $a_img)
512  {
513  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
514  return $d."/templates/images/".$a_img;
515  }
516 
520  public final function getImagePath($a_img)
521  {
522  return $this->getDirectory()."/templates/images/".$a_img;
523  }
524 
528  public final function getStyleSheetLocation($a_css_file)
529  {
530  return $this->getDirectory()."/templates/".$a_css_file;
531  }
532 
536  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
537  {
538  $a_tpl->addBlockFile($a_var, $a_block,
539  $this->getDirectory()."/templates/".$a_tplname);
540  }
541 
545  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
546  {
547  global $ilDB;
548 
549  // read/set basic data
550  $q = "SELECT * FROM il_plugin".
551  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
552  " AND component_name = ".$ilDB->quote($a_cname, "text").
553  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
554  " AND name = ".$ilDB->quote($a_pname, "text");
555  $set = $ilDB->query($q);
556  if ($rec = $ilDB->fetchAssoc($set))
557  {
558  return $rec;
559  }
560  else // no record? create one
561  {
562  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
563  " VALUES (".$ilDB->quote($a_ctype, "text").",".
564  $ilDB->quote($a_cname, "text").",".
565  $ilDB->quote($a_slot_id, "text").",".
566  $ilDB->quote($a_pname, "text").")";
567  $ilDB->manipulate($q);
568  $q = "SELECT * FROM il_plugin".
569  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
570  " AND component_name = ".$ilDB->quote($a_cname, "text").
571  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
572  " AND name = ".$ilDB->quote($a_pname, "text");
573  $set = $ilDB->query($q);
574  return $ilDB->fetchAssoc($set);
575  }
576  }
577 
581  final private function __init()
582  {
583  global $ilDB, $lng, $ilPluginAdmin;
584 
585  // read/set basic data
587  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
588  $this->setLastUpdateVersion($rec["last_update_version"]);
589  $this->setDBVersion($rec["db_version"]);
590  $this->setActive($rec["active"]);
591 
592  // get id
593  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
594  $this->getComponentName(),
595  $this->getSlotId(),
596  $this->getPluginName()));
597 
598  // get version
599  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
600  $this->getComponentName(),
601  $this->getSlotId(),
602  $this->getPluginName()));
603 
604  // get ilias min version
605  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
606  $this->getComponentName(),
607  $this->getSlotId(),
608  $this->getPluginName()));
609 
610  // get ilias max version
611  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
612  $this->getComponentName(),
613  $this->getSlotId(),
614  $this->getPluginName()));
615 
616  // get slot object
617  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
618  $this->getComponentName(), $this->getSlotId()));
619 
620  // load language module
621 
622  // Fix for authentication plugins
623  if(is_object($lng))
624  {
625  $lng->loadLanguageModule($this->getPrefix());
626  }
627 
628  // call slot and plugin init methods
629  $this->slotInit();
630  $this->init();
631  }
632 
639  abstract protected function slotInit();
640 
645  protected function init()
646  {
647  }
648 
652  public final function isActive()
653  {
654  global $ilPluginAdmin;
655 
656  return $ilPluginAdmin->isActive($this->getComponentType(),
657  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
658  }
659 
663  public final function needsUpdate()
664  {
665  global $ilPluginAdmin;
666 
667  return $ilPluginAdmin->isActive($this->getComponentType(),
668  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
669  }
670 
674  final function activate()
675  {
676  global $lng, $ilDB;
677 
678  $result = true;
679 
680  // check whether update is necessary
681  if ($this->needsUpdate())
682  {
683  //$result = $this->isUpdatePossible();
684 
685  // do update
686  if ($result === true)
687  {
688  $result = $this->update();
689  }
690  }
691  if ($result === true)
692  {
693  $result = $this->beforeActivation();
694  // activate plugin
695  if ($result === true)
696  {
697  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
698  " plugin_id = ".$ilDB->quote($this->getId(), "text").
699  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
700  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
701  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
702  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
703 
704  $ilDB->manipulate($q);
705  $this->afterActivation();
706  }
707  }
708  return $result;
709  }
710 
714  protected function beforeActivation()
715  {
716  return true; // false would indicate that anything went wrong
717  // activation would not proceed
718  // throw an exception in this case
719  //throw new ilPluginException($lng->txt(""));
720  }
721 
725  protected function afterActivation()
726  {
727  }
728 
732  final function deactivate()
733  {
734  global $ilDB;
735 
736  $result = true;
737 
738  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
739  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
740  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
741  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
742  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
743 
744  $ilDB->manipulate($q);
745 
746  return $result;
747  }
748 
752  final function update()
753  {
754  global $ilDB, $ilCtrl;
755 
756  $result = true;
757 
758  // DB update
759  if ($result === true)
760  {
761  $result = $this->updateDatabase();
762  }
763 
764  // Load language files
765  $this->updateLanguages();
766 
767  // load control structure
768  include_once("./setup/classes/class.ilCtrlStructureReader.php");
769  $structure_reader = new ilCtrlStructureReader();
770  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
771  $this->getDirectory());
772 // $ilCtrl->storeCommonStructures();
773 
774  // add config gui to the ctrl calls
775  $ilCtrl->insertCtrlCalls("ilobjcomponentsettingsgui", ilPlugin::getConfigureClassName($this->getPluginName()),
776  $this->getPrefix());
777 
778  // set last update version to current version
779  if ($result === true)
780  {
781  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
782  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
783  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
784  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
785  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
786 
787  $ilDB->manipulate($q);
788  }
789 
790  return $result;
791  }
792 
801  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
802  {
803  global $ilDB;
804 
805  include_once("./Services/Component/classes/class.ilPluginSlot.php");
806  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
807 
808  // this check is done due to security reasons
809  $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
810  " AND name = %s", array("text", "text"),
811  array($a_ctype, $a_cname));
812  if (!$ilDB->fetchAssoc($set))
813  {
814  return null;
815  }
816 
817  $file = "./Customizing/global/plugins/".$a_ctype."/".
818  $a_cname."/".$slot_name."/".
819  $a_pname."/classes/class.il".$a_pname."Plugin.php";
820 
821  if (is_file($file))
822  {
823  include_once($file);
824  $class = "il".$a_pname."Plugin";
825  $plugin = new $class();
826  return $plugin;
827  }
828 
829  return null;
830  }
831 
832 
836  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
837  {
838  global $ilDB;
839 
840  $q = "SELECT * FROM il_plugin WHERE ".
841  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
842  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
843  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
844  " name = ".$ilDB->quote($a_pname, "text");
845 
846  $set = $ilDB->query($q);
847 
848  $rec = $ilDB->fetchAssoc($set);
849 
850  return $rec;
851  }
852 
856  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
857  {
858  global $ilDB, $ilPluginAdmin;
859 
860  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
861  " AND component_name = ".$ilDB->quote($a_cname, "text").
862  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
863  " AND active = ".$ilDB->quote(1, "integer");
864 
865  $set = $ilDB->query($q);
866  $plugins = array();
867  while($rec = $ilDB->fetchAssoc($set))
868  {
869  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
870  {
871  $plugins[] = $rec["name"];
872  }
873  }
874 
875  return $plugins;
876  }
877 
881  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
882  {
883  global $ilDB;
884 
885  $q = "SELECT name FROM il_plugin ".
886  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
887  " AND component_name = ".$ilDB->quote($a_cname, "text").
888  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
889  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
890 
891  $set = $ilDB->query($q);
892  if ($rec = $ilDB->fetchAssoc($set))
893  {
894  return $rec["name"];
895  }
896  }
897 
901  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
902  {
903  global $ilDB;
904 
905  $q = "SELECT plugin_id FROM il_plugin ".
906  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
907  " AND component_name = ".$ilDB->quote($a_cname, "text").
908  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
909  " AND name = ".$ilDB->quote($a_plugin_name, "text");
910 
911  $set = $ilDB->query($q);
912  if ($rec = $ilDB->fetchAssoc($set))
913  {
914  return $rec["plugin_id"];
915  }
916  }
917 
918 }
919 ?>