/installer/installer_votings.php

The installer is one of the most important files of the module. It defines all dependencies, requirements and versions. All needed database-tables are created by the installer, page-elements are registered, the module itself is registered by the installer, too.
See in addition

class class_installer_votings extends class_installer_base implements interface_installer {
public function __construct() {
$arrModule = array();
$arrModule["version"] = "0.1";
$arrModule["name"] = "votings";
$arrModule["name_lang"] = "Module Votings";
$arrModule["moduleId"] = _votings_modul_id_;
parent::__construct($arrModule);
}


The constructor of the module-installer defines a few values and metadata the installer needes during the installation-procedure. So make sure to have all values maintained as above.
It's obvious, that the modules' version may differ from the version of the installed Kajona system.


public function getNeededModules() {
return array("system", "pages");
}


Dependencies are defined viathe method getNeededModules(). The installer verifies that the listed modules are installed. Only if the check proves the modules to be available, the current module can be installed.


public function getMinSystemVersion() {
return "3.3.0";
}


If the module requires certain functionality of Kajona, the minimal version needed can be set up.
The installer checks the version of the module system against the value provided above. Only if the systems' version is equal or greater than the required version, the current module can be installed.


public function hasPostInstalls() {
$strQuery = "SELECT COUNT(*) FROM "._dbprefix_."element WHERE element_name='votings'";
$arrRow = $this->objDB->getRow($strQuery);
if($arrRow["COUNT(*)"] == 0)
return true;

return false;
}


Via the method hasPostInstalls(), the module tells the installer whether there are any post-installs like elements or not. In this case, a check is made if the element “votings” is already installed or not.
The installation of the module itself is done via the method install(). It is called by the installer during the step of module-installations.


public function install() {
$strReturn = "";
//votings
$strReturn .= "Installing table votings_voting...\n";
$arrFields = array();
$arrFields["votings_voting_id"] = array("char20", false);
$arrFields["votings_voting_title"] = array("char254", true);

if(!$this->objDB->createTable("votings_voting", $arrFields, array("votings_voting_id")))
$strReturn .= "An error occured! ...\n";
$strReturn .= "Installing table votings_answer...\n";
$arrFields = array();
$arrFields["votings_answer_id"] = array("char20", false);
$arrFields["votings_answer_text"] = array("text", true);
$arrFields["votings_answer_hits"] = array("int", true);
if(!$this->objDB->createTable("votings_answer", $arrFields, array("votings_answer_id")))
$strReturn .= "An error occured! ...\n";


The previous blocks create both database-tables „votings_voting“ and „votings_answer“.
The definition of the tables in done without using any SQL, the SQL code is created by the framework on the fly for the used target database-system (see in addition http://apidocs.kajona.de/v3.3.0/modul_system/class_db.html#createTable). The parameters of the columns are the only values to define.


//register the module
$strSystemID = $this->registerModule("votings", _votings_modul_id_, "class_modul_votings_portal.php", "class_modul_votings_admin.php", $this->arrModule["version"], true);
After creating the tables, the module can be registered with the system. The call of registerModule() is the only thing to do, all required entries in the database are created by the call, including the integration with the permission-management (see in addition http://apidocs.kajona.de/v3.3.0/modul_system/class_installer_base.html#registerModule).
The parameters to pass are the file-names of the admin- and portal-classes, the module-id defined before, the module-name and the version of the module. The last boolean-parameter decides, if the module appears in the backend-navigation or if the module will remain hidden.
//modify default rights to allow guests to vote
$strReturn .= "Modifying modules' rights node...\n";
$this->objRights->addGroupToRight(_guests_group_id_, $strSystemID, "right1");
return $strReturn;
}


A special requirement is the default permissions-configuration of the module. As stated in the requirements of the module, guests should be able to vote by default. The permission to vote will be represented by the first freely available permission, “right1”. By the invocation of addGroupToRight(), the modules permission node is modified, guests are added to the permission “right1”. All votings created subsequently inherit their permissions from the module-node, the guests are allowed to vote without redefining the permissions.


public function postInstall() {
$strReturn = "";
//Register the element
$strReturn .= "Registering votings-element...\n";
//check, if not already existing
$objElement = null;
try {
$objElement = class_modul_pages_element::getElement("votings");
}
catch (class_exception $objEx) {
}
if($objElement == null) {
$objElement = new class_modul_pages_element();
$objElement->setStrName("votings");
$objElement->setStrClassAdmin("class_element_votings.php");
$objElement->setStrClassPortal("class_element_votings.php");
$objElement->setIntCachetime(-1);
$objElement->setIntRepeat(1);
$objElement->setStrVersion($this->getVersion());
$objElement->updateObjectToDb();
$strReturn .= "Element registered...\n";
}
else {
$strReturn .= "Element already installed!...\n";
}

return $strReturn;
}


Depending on the return-value of hasPostInstalls(), the user can execute additional post-installations. In most cases, this is the installation of related page-elements, like in the method postInstall() above.


public function update() {
$strReturn = "";
//check installed version and to which version we can update
$arrModul = $this->getModuleData($this->arrModule["name"], false);
$strReturn .= "Version found:\n\t Module: ".$arrModul["module_name"].", Version: ".$arrModul["module_version"]."\n\n";
return $strReturn."\n\n";
}
}


The update()-method provides routines to update the current module. Since the votings are created in an initial version, there aren't any updates available yet.
The possibility to update a module is provided by the installer, if the version maintained in the constructors' metadata is higher then the version known by the system via the database.