Categories
Development

How to Override a Moodle Course Format Template

Working with Moodle can sometimes be a bit of a nightmare, especially when it comes to knowing what can or can’t be achieved in the platform. The docs, while helpful, are often incomplete and don’t cover everything. Recently, I encountered a challenge with overriding a course format template and wanted to share the process for anyone else facing the same issue. This post specifically focuses on how to override the Moodle grid course format.

Identifying the Problem

At UAL, we aim to not only improve the student experience on Moodle but also the editing experience for our staff. After all, if the staff have a positive experience while creating their content, it should reflect in the final student view.

The grid course format in edit mode outputs a long page with all the content from all the sections, requiring excessive scrolling. This is time-consuming, prone to errors, and generally a bit crap. Feedback from our staff and digital learning teams, received through support tickets, user research interviews, and anecdotally, highlighted this inefficiency and frustration. Our goal was to streamline this editing process. How? Not possible with style alone, we’d have to modify the grid format template itself.

Correct Folder Structure for Template Overrides

When you want to override a template file from a Moodle plugin, you need to place the override file in your theme’s templates directory. However, the directory structure must mimic the plugin’s structure. I read a number of different ways of doing this across Moodle forums and in Moodle docs, but only one folder structure works. Here’s how you do it:

  1. Locate the Original Template Path:
    • For example, the code for each section in editing mode is located at: /course/format/grid/templates/local/content/section/content.mustache
  2. Create the Necessary Directory Structure in Your Theme:
    • You need to replicate the plugin’s directory structure within your theme’s templates folder. The correct path for the override should be: theme/[your_child_theme]/templates/format_grid/local/content/section/content.mustache
  3. Place Your Override File:
    • Copy the content.mustache file from the course format plugin to the newly created directory in your theme and make your modifications.

Extra Steps to Ensure the Override Works

  1. Activate Your Theme (hopefully, an obvious one):
    • Make sure your custom theme is activated in Moodle. Go to Site administration > Appearance > Theme selector and select your theme.
  2. Purge All Caches:
    • After placing the override template, go to Site administration > Development > Purge caches and purge all caches to ensure Moodle picks up the new template. You should also be fine to just purge your theme cache. Alternatively, you can go into theme designer mode. I do this by adding (or uncommenting) a line in my root config.php file: $CFG->themedesignermode = true;
  3. Enable Debugging:
    • If the override doesn’t seem to work, enable debugging in Moodle. This can be done via Site administration > Development > Debugging. When working locally, I will often check my php error log, which for MAMP is located in: /MAMP/logs/php_error.log

Example Override

To verify that your override is effective, add a simple comment or unique text to your content.mustache file:

...
<h2>MY CUSTOM OVERRIDE</h2>
<div id="coursecontentcollapse{{num}}"
    class="content course-content-item-content collapse">
...

After purging caches, check the relevant course page to see if the overridden content is displayed.

Helpful Links

For more detailed information and community support, check out these Moodle resources:

  • Moodle Themes Plugins
  • Moodle Templates – this actually outlines the correct folder structure. If it doesn’t work at first, check and double check for typos. I lost precious minutes (ok, hours) on this.

So there you have it. That’s how you can successfully override Moodle course format templates, making your Moodle site more tailored to your needs. Happy customising!