Creating New Modules

In one word: use the template “newmodule”. Okay, that was four words. Anyway, here's the url: http://moodle.org/mod/data/view.php?d=13&rid=715. Simply replace all appearances of string “newmodule” by the name of your own module.

Hint: prepare all your tables in MySQL (or whatever DB you're using) and then use the xmldbeditor to extract the right xml for the newmodule/db/install.xml file.

WARNING: it looks as though underscores in module names (i.e. the directory they reside in) are not allowed.

Modify Existing Tables

Sometimes your mods rely on other tables, but in a slightly modified form. For that case, you can use the mod/yourmod/db/install.php file.

As of Moodle 2.4, the database schema is cached. This may result in completely strange bugs if you do not empty the cache for adding, deleting or modifying columns in your install.php file.

Here's some sample code:

<?php
# local/blockconfiguration/db/install.php

function xmldb_local_blockconfiguration_install() {
    global $CFG, $DB;

    cache_helper::invalidate_by_definition('core', 'databasemeta', array('dbfamily' => $DB->get_dbfamily()));

    $dbman = $DB->get_manager();
    $table = new xmldb_table('block_instances');

    $field = new xmldb_field('standard_collapse', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'configdata');
    if (!$dbman->field_exists($table, $field)) {
        $dbman->add_field($table, $field);
    }
} // function xmldb_local_blockconfiguration_install

Adding An Index to A Column

Use the XMLDB editor to look up an example. The Quiz module for instance provides a wide range of cases.

Here's how to add an index:

function xmldb_local_myplugin_upgrade($oldversion) {
    globaL $DB;
    $dbman = $DB->get_manager();

    if ($oldversion < 2015022301) {
        $table = new xmldb_table('subscribers');
        $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));

        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        upgrade_plugin_savepoint(true, 2015022301, 'local', 'myplugin');
    }
    return true;
} // function xmldb_local_myplugin_upgrade

Personal Tools