3

I'm making a plugin for ILIAS and I am gtting this error when I try to call for anything database related. I'm including the dbupdate file in the config file to be able to update it and change things while developping the plugin.

(This is because I believe that ILIAS only uses the dbupdate when installing a plugin.)

The code it gives an error on:

if(!$ilDB->tableColumnExists('rep_robj_xptg_data','id'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'id',
        array(
            'type'           => 'integer',
            'length'         => 4000
        )
    );
}

Column and 'id' do exist in the database. I believe this needs to work to be able to save data to the database in the future.

Update

This is the code that 'calls' :

<?php

    include ("/Customizing/global/plugins/Services/Repository/RepositoryObject/Presentations2Go/sql/dbupdate.php");
    require_once('./Services/Component/classes/class.ilPluginConfigGUI.php');
    require_once('class.ilPresentations2GoPlugin.php');
    require_once('class.ilObjPresentations2Go.php');
    //require ("/Customizing/global/plugins/Services/Repository/RepositoryObject/Presentations2Go/sql/dbupdate.php");

    /**
     * Presentations2Go configuration user interface class
     *
     * @author Bonny van den Bovenkamp
     * @version $Id$
     *
     */
    class ilPresentations2GoConfigGUI extends ilPluginConfigGUI
    {
        /**
        * Handles all commmands, default is "configure"
        */
        function performCommand($cmd)
        {

            switch ($cmd)
            {
                case "configure":
                case "save":
                    $this->$cmd();
                    break;

            }
        }

        /**
         * Configure screen
         */
        function configure()
        {
            global $tpl;

            $form = $this->initConfigurationForm();
            $tpl->setContent($form->getHTML());
        }

        //
        // From here on, this is just an Presentations2Go implementation using
        // a standard form (without saving anything)
        //

        /**
         * Init configuration form.
         *
         * @return object form object
         */
        public function initConfigurationForm()
        {
            global $lng, $ilCtrl;

            $pl = $this->getPluginObject();

            include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
            $form = new ilPropertyFormGUI();

            // setting 1 (a checkbox)
            $cb = new ilCheckboxInputGUI($pl->txt("setting_1"), "setting_1");
            $form->addItem($cb);

            // setting 2 (text)
            $ti = new ilTextInputGUI($pl->txt("setting_2"), "setting_2");
            $ti->setRequired(true);
            $ti->setMaxLength(10);
            $ti->setSize(10);
            $form->addItem($ti);

            $form->addCommandButton("save", $lng->txt("save"));

            $form->setTitle($pl->txt("Presentations2Go configuration"));
            $form->setFormAction($ilCtrl->getFormAction($this));

            return $form;
        }

        /**
         * Save form input (currently does not save anything to db)
         *
         */
        public function save()
        {
            global $tpl, $lng, $ilCtrl;

            $pl = $this->getPluginObject();

            $form = $this->initConfigurationForm();
            if ($form->checkInput())
            {
                $set1 = $form->getInput("setting_1");
                $set2 = $form->getInput("setting_2");


                ilUtil::sendSuccess($pl->txt("saving_invoked"), true);
                $ilCtrl->redirect($this, "configure");
            }
            else
            {
                $form->setValuesByPost();
                $tpl->setContent($form->getHtml());
            }
        }

    }
    ?>

And this is the called file:

<?php


if(!$ilDB->tableColumnExists('rep_robj_xptg_data','id'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'id',
        array(
            'type'           => 'integer',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','is_online'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'is_online',
        array(
            'type'           => 'integer',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_one'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_one',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_two'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_two',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_three'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_three',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
?>
  • The error is stating `$ilDB` is _null_. How do you create this object? – Jeff Lambert May 26 '15 at 14:19
  • @watcher " ILIAS provides a global database object in variable $ilDB to access the database. All database functionality must be accessed through methods of this object. The object wraps a lot of MDB2 methods." I did not make this object. – Bonny van den Bovenkamp May 26 '15 at 14:48
  • something needs to make that object. what happens if you `var_dump($ilDB);` right before your `if` statement? – Jeff Lambert May 26 '15 at 14:50
  • @watcher Good point, I thought the same, but that just results in NULL. Ilias itself makes $ilDB I think – Bonny van den Bovenkamp May 26 '15 at 15:02
  • @watcher I var_dumped a differend plugin and it gives almost a whole page of output. This is the first line: **object(ilDBMySQL)#9 (19) { ["max_allowed_packet_size"]=> string(7) "1048576" ["slave_active":protected]=> bool(false)** – Bonny van den Bovenkamp May 26 '15 at 15:28
  • is this code inside of a function? if so it may be trying to reference a global variable which would require specifying `global $ilDB;` within the function – Jeff Lambert May 26 '15 at 15:45
  • @watcher The code is in a phpfile called ' dbupdate.php' not in a seperate function. It gets called by another file by using an include. – Bonny van den Bovenkamp May 27 '15 at 07:08
  • just to be sure, is the calling code itself anywhere inside of a function? If it is a global variable, then any non-global context will need to use `global` (I'm thinking global function calls global function that creates an object that includes your file, e.g.). Barring that, in order for `$ilDB` to be in scope it will need to be defined within the same scope as the line of code that includes this file, so I would check there. – Jeff Lambert May 27 '15 at 09:06
  • @watcher `global` is only used in the ILIAS basic code. plugin files do call for those ilias classes though. – Bonny van den Bovenkamp May 27 '15 at 09:57
  • @watcher I editted the question with some more code. Maybe its a bit more clear now – Bonny van den Bovenkamp May 27 '15 at 11:39

1 Answers1

2

I solved this by making another class named ilconfig. it contains this very important part:

public function initDB() {
        global $ilDB;
        if (!$ilDB->tableExists($this->getTableName())) {
            $fields = array(
                'config_key' => array(
                    'type' => 'text',
                    'length' => 128,
                    'notnull' => true
                ),
                'config_value' => array(
                    'type' => 'clob',
                    'notnull' => false
                ),
            );
            $ilDB->createTable($this->getTableName(), $fields);
            $ilDB->addPrimaryKey($this->getTableName(), array( "config_key" ));
        }

        return true;
    }

I hope this helps other people trying to develop plugins in ILIAS!