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…

Working with dates before epoch

If you use a custom user profile field with a date and want to store a date that falls before epoch, 01/01/1970 (e.g. a date of birth), the data will be stored as a negative number e.g. 20/11/1959 is stored as -319284000 which means 319, 284, 000 seconds BEFORE 01/01/1970. With MySQL you can convert…

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…

Scheduled Tasks: A lock was created but not released

Schedule tasks can use two types of locks: * Database locks — lock_db table * File locks — dirroot/lock The follow error can occur when running a cron through the CLI for example: !!! Coding error detected, it must be fixed by a programmer: A lock was created but not released at: [dirroot]/lib/classes/task/manager.php on line…

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…

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…

Replacing strings in the database through the CLI

There is a tool provided with Moodle to replace strings in your database with another value. This is very useful for things such as site address or domain name changes, and changes from non-SSL (http) to SSL (https). The tool can be accessed via the following relative URL as a site administrator. /admin/tool/replace/ While you…

Resetting a user’s password through the CLI

If you have access to the Moodle server, you can use the admin/cli/reset_password.php script to reset passwords for users with the manual authentication method. To run this script (from the moodle root code directory), note you may need to sudo as a user with write privileges to the $CFG->dataroot directory. php admin/cli/reset_password.php == Password reset…

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…

CLI Maintenance Mode

Newer versions of Moodle now provide the ability to put a site into maintenance mode through the CLI: This can be found under the standard admin CLI location php admin/cli/maintenance.php Options include php admin/cli/maintenance.php –enable php admin/cli/maintenance.php –disable NOTE this disables all web access to the site and can only be restored by disabling maintenance…

Table formats from Antelope to Barracuda

On your environments page, as of around Moodle 2.6 you might see a message like this: unsupported_db_table_row_format if this test fails, it indicates a potential problem Your database has tables using Antelope as the file format. You are recommended to convert the tables to the Barracuda file format. See the documentation Administration via command line…

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…

Moodle User Management URLs

Some handy user management and search URLs, particularly if the site admin block is hidden. User search Note a %20=means a blank space, encoded to work in a URL. user/index.php?id=1&search= http://yourmoodle.com/user/index.php?id=1&search=fred http://yourmoodle.com/user/index.php?id=1&search=Fred%20Jones Create a new user user/editadvanced.php?id=-1 http://yourmoodle.com/user/editadvanced.php?id=-1 Edit a user if you know their moodle internal id user/editadvanced.php?id={moodle_user_id} http://yourmoodle.com/user/editadvanced.php?id=16 Delete an existing user This…

Hide My Profile -> Forum Posts from Navigation

To hide the navigation: My Profile > Forum Posts for all users: 1. This is set by the configuration value navadduserpostslinks 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->navadduserpostslinks = 0; 3. Clear…

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…

Moodle and Timezones

Moodle timezone information in the first instance is loaded from: http://download.moodle.org/timezone/ This returns a text file which is stored in the lib directory as timezone.txt The data is stored in the table mdl_timezone (adjust to suit your db prefix).