David Mudrack, of Moodle HQ released a plugin called the “My Todo List” block. While functional, its real purpose is to show off the latest advanced coding techniques available in recent versions of Moodle. See his Tweet. From the readme:
The main purpose of this plugin is to demonstrate usage of advanced coding techniques available in recent Moodle versions. Most notably:
- Rendering HTML output via Mustache templates.
- AJAX based workflow of the elementary CRUD operations.
- Organising JS into AMD modules.
- Organising external functions into traits.
- Low-level access to the database via persistent models.
- Using exporters for handling the data structures in rendering and AJAX.
For this post, I will install and examine the code with the goal of learning better how to use these concepts.
While I have been a plugin developer for many years, I have not been able to keep up with all of the latest techniques to enhance the plugins I maintain. I am personally really interested in getting better with AJAX and AMD, as many of my plugins could benefit from these features. I have already experimented and released code with renderers and Mustache templates (see the series beginning here), so I won’t go deep into those unless I see something new.
To begin, I fork David’s repository into my own Github repository. This gives me place to install and play with the code. Once I have a local copy, I load it into my IDE (I use PhpStorm) and take a look at the code.
I search the development documentation on Moodledocs, and find this function referred to on the “jQuery pre2.9” page. So, it must be a function that has remained even with the new AMD module addition. I think I may want to look for the AMD Module documentation first.
Allows the block to load any JS it requires into the page.
By default this function simply permits the user to dock the block if it is dockable.
In any case, the function passes the arguments:
‘block_todo/control’, ‘init’, [‘instanceid’ => $this->instance->id]
Getting back to the function of the plugin, the main block get_content function, is a very simple function that does two things: gets a list of todo items, and outputs them on the screen. The technique uses one technique I’m familiar with, templates, and two I am unfamiliar with, Moodle persistents and Moodle exporters.
This is what I am seeing in get_content:
// Load the list of persistent todo item models from the database. $items = block_todo\item::get_my_todo_items();
This code uses the persistent class, which appears to be a new technique to manage database data using CRUD techniques. It will be interesting to learn why this exists and when it should be used.
// Prepare the exporter of the todo items list. $list = new block_todo\external\list_exporter([ 'instanceid' => $this->instance->id , ], [ 'items' => $items, 'context' => $this->context ,]);
This code uses exporter class and appears to be a technique to manage data passed in and out of web service functions, used by external applications and AJAX.
// Render the list using a template and exported data. $this->content->text = $OUTPUT->render_from_template('block_todo/content', $list->export($OUTPUT));
This code is using a Mustache template to display the block content and utilizes the exporter class to send the data rather than renderers defined for the plugin, as I am used to. Again, I will need to discover why this technique is used.
I have some concepts to learn, so I install the block just to see what it does and how it works. After adding it to my dashboard page, I play with it, and see that it is as advertises, a simple “To Do” list.
Next post, I will start my functional learning.