Plugin Dependencies

Plugin dependencies can be added using the $plugin->dependencies attribute in version.php. There are quite a few examples in the code base, for example: filter_data depends on mod_data so the following appears in the filter/data/version.php: $plugin->dependencies = array(‘mod_data’ => 2014050800);

Move download button and select below table

If you are working with flexibile tables (lib/tablelib.php) in Moodle and have table download enabled, you can move the “Download table data as” select and button below the table by adding the following line before finishing your table setup: $table->show_download_buttons_at(array(TABLE_P_BOTTOM)); By default they appear above the table but this doesn’t always look so good and…

Getting the Moodle base / root directory

The moodle root directory $CFG->dirroot is established in the file lib/setup.php along with other values in the global $CFG object which combines configuraton from config.php as well as the entries in the database table mdl_config. The value is set as follows: $CFG->dirroot = dirname(dirname(__FILE__)); And you can use it reliably establish the base/root moodle directory…

Make a select form field required

When adding a form rule for select (or multiple select) fields, these need to be applied on the client side (not server side). Here’s an example of how to add a rule for a select field: $mform->addRule( ‘selectfield’, get_string(“selectfieldrequiredmessage”, “local_yourplugin”), ‘required’, ”, ‘client’ ); The key is the 5th parameter above which is set to…

Can’t create a new XMLDB file

The Moodle XMLDB plugin does some checks of the db/ folder in your plugin to ensure it has read/write access before allowing certain permissions (e.g. create and load). If it doesn’t find this, the text will appear but it won’t be a hyperlink. Have a look in admin/tool/xmldb/actions/main_view/main_view.class.php For example for the create button it…

Setting up a Moodle Development Environment with Cloud9 IDE

Cloud9 is a cloud based development environment. Behind the scenes it uses Docker containers to provide an awesome IDE together with a Workspace using Ubuntu. What this means it that you can you set up a fully integrated Moodle development environment on Cloud9! This tutorial uses Moodle 3.1 for because as of May 2017 Cloud9…

Unknown Error Upgrading Plugin to Version

If you are getting an error like this when attempting to upgrade a plugin: !!! Unknown error upgrading <pluginname> to version 2017042400, can not continue. !!! !! Error code: upgradeerror !! !! Stack trace: * line 340 of /lib/upgradelib.php: upgrade_exception thrown * line 549 of /lib/upgradelib.php: call to upgrade_plugin_savepoint() * line 1630 of /lib/upgradelib.php: call…

XMLDB Editor and Reserved Names

One (of the many) good reasons to use the XMLDB editor in Moodle rather than hacking the db/install.xml file directly is that it warns you if you use a reserved name. For example: The red Reserved text after [XML] in the screenshot is indicating that the word “column” is reserved and shouldn’t be used. It…

Set User IDs that will always see debug messages

There’s a handy feature in Moodle configuration called $CFG->debugusers = ‘<moodle_user_ids>’ that allows you to set certain user IDs to always see debug messages regardless fo the Site administration > Development > Debugging settings for the site. This can be a comma separated list of Moodle User Ids (from the user table). Set this in…

Course file system space usage query

The following query provides a summary of the file space usage (in bytes) and number of files used by each course in the system from the mdl_files meta table for the data directory. This is just for the space used in the data directory (not the database). select f.contextid, x.instanceid, c.fullname as course_full_name, c.shortname as…

Overriding PHP Settings

There are a few ways to override global PHP (php.ini) settings with local settings. For example you can use a .htaccess file or local php.ini file depending on what options exist for your site hosting. Another way to do it, is to override the settings in config.php. This has the benefit of being part of…

Getting PHP Info for your Moodle Site

There is a handy link under Site adminstration > Server > PHP Info ( or browse to http://yoursite.com/admin/phpinfo.php ) that will return the standard PHP Info phpinfo() results for your server. Very handy for checking things like installed modules and core parameters if you are overriding them ( e.g. error_log, max_execution_time, post_max_size, upload_max_size etc )

Moodle Debug Log Level Values

These are the values you can set for the debug level under Site administration > Development > Debugging for Debug message (debug). You can also query this from the mdl_config table looking at the key debug. 0 = NONE: Do not show any errors or warnings 5 = MINIMAL: Show only fatal errors 15 =…

Extended characters in moodle usernames

If you need to use extended characters in usernames, there is a setting under Site Administration > Security > Site Policies called Allow extended characters in usernames extendedusernamechars. By default moodle usernames must be lowercase and are restricted to alphanumeric characters, the underscore, hypen, period and @ symbol. You don’t need this if you are…

Verify your Moodle Database Schema against XMLDB

There is a very handy CLI tool admin/cli/check_database_schema.php which compares the structure of your Moodle database against the XMLDB metadata and looks for any issues. These might includes issues such as: Missing tables – they exist in XMLDB but not in your DB (not good!) Unexpected tables – tables in your database not defined in…

Web service Access control exception

If you are creating or using a web service in Moodle and see the following message: { “exception”: “webservice_access_exception”, “errorcode”: “accessexception”, “message”: “Access control exception” } It can be a bit hard to troubleshoot. Generally there is a 4th item with debugging info, but this will only appear if you have the correct Moodle debugging…

Plugin is defective or outdated, Self test failed

You may see an error like this when upgrading a block: Default exception handler: Plugin “{block name}” is defective or outdated, can not continue, sorry. Debug: Self test failed. Error code: detectedbrokenplugin * line 833 of /lib/upgradelib.php: plugin_defective_exception thrown * line 425 of /lib/upgradelib.php: call to upgrade_plugins_blocks() * line 1639 of /lib/upgradelib.php: call to upgrade_plugins()…

Finding specific Moodle versions in the Moodle Git Repository

The key to working with the Moodle git repository (https://docs.moodle.org/31/en/Git_for_Administrators) is to look at changes to the version.php file in the main moodle directory which tells you the moodle version. You can use the whatchanged git command to get a summary of all changes and tags. git whatchanged -p version.php But more than likely if…

Updating the Moodle Frontpage Summary through the database

You might have the need to update the Moodle front page summary through the database rather than through the UI. For example you might get an error editing the summary after a clone, or you might want to script the cloning process and change the frontpage text through the database. Here are the steps: Update…

Upgrading database schema using upgrade.php

If you need to upgrade your database schema for a custom plugin, e.g. adding a new table or changing the properties of an existing table, you should do it through db/upgrade.php per the suggestions in the MoodleDocs UpgradeAPI. This isn’t a bad idea to do even when iterating through development as it isn’t a lot…

How to get a list of the Moodle Core Git Branches

Use the following git command to get a list of all the Moodle core git branches. git ls-remote git://git.moodle.org/moodle.git | grep head This returns a bunch of hashes and the refs/heads/[MOODLE_XX_STABLE] branches. You can guess these, so this is just to check what they are without having to clone the entire repository. Plus you’ll know…

$DB->get_record_sql only returning a single row

So you’ve written a query, passed it to $DB->get_records_sql() and, what’s this – only one row? What’s going on, where’s the rest of the query data? First off, make sure you are using the plural form (get_records_sql() and not get_record_sql()). If that’s not the issue then read on. The reason this can happen is that…

Uninstalling a Moodle Plugin and forcing reinstallation

The easiest way to get moodle to re-run installation is to update the version in version.php. However, if you want to be sure it makes any schema changes etc then remove the plugin version from the mdl_config_plugins table. Note you can leave the other configuration there if required to save re-setting up the plugin. The…

Themes and Page Layouts

The types of page layouts – which determine how your moodle page is rendered via: $PAGE->set_pagelayout(‘layout’); These are defined in the base theme (and can be extended by custom themes): Have a look in theme/base/config.php in the $THEME->layouts array for options.

Convert a Moodle URL object to string

There’s a method in the moodle_url class in lib/weblib.php called __toString which will convert a Moodle URL object back to a string if you just need the URL string itself. $PAGE->url->__toString()

Hide Navigation for My Profile > Messages

To hide the navigation: My Profile > Messages for all users: 1. This is set by the configuration value messaging 2. You can either update the value of this in the database (config table) from 1 to 0 OR you can add an entry in config.php where you set $CFG->messaging = 0; 3. Clear the…

Adding a local plugin to site admin navigation

If you want to add a local plugin navigation under the Site Administration block (under Plugins > Local Plugins) add the following to your local plugin settings.php file: // Add the settings page to the navigation block $settings = new admin_settingpage( ‘local_yourplugin’, get_string(‘pluginname’, ‘local_yourplugin’) ); $ADMIN->add(‘localplugins’, $settings);