Moodle development traffic 07/2010

Latest stable version 1.9.7+

During the last week, 23 patches were committed into MOODLE_19_STABLE (commits by Moodle Robot excluded). Many of them sent by Penny Leach are fixing various MNet issues.

Quote of the week

“I commited junk from another file as part of commit. Sorry everyone. I’m to use emacs for the weekend as punishment.”
Dan Poltawski

Output rendering in Moodle 2.0

Outputting HTML in Moodle 2.0 was a big project started by Tim Hunt last year, continued by Nicolas Connault, Sam Hemelryk, Petr Škoda and others. After some refactoring, it seems to be stable now. This is an example of basic usage in your module:

// in mod/mymod/view.php
$output = $PAGE->get_renderer('mod_mymod'); // (1)
$welcome = new mymod_greeting(); // (2)
$welcome->set_message(get_string('helloworld', 'mymod')); // (3)
echo $output->header(); // (4)
echo $output->render($welcome); // (5)

HTML code to be sent to the browser is generated by $output which is an instance of a rendering class. In (1) we get the renderer for our module. This is instance of class mod_mymod_renderer which must be defined in mod/mymod/renderer.php. It knows how to display our module specific page elements.

In (2), we create an object that represents an element to be displayed. It may be a forum post, submitted work by student or anything that your module is dealing with. In our case, the object $welcome represents a welcome message to be displayed. The rule is that the class mymod_greeting must implement the interface renderable. This interface itself is empty but implementing it is a sign for Moodle core that the instances of this class can be passed to the render() method later.

In (3), we set a property of the renderable widget. In this simple case, it would make sense to pass the localized welcome message as a parameter of the $welcome constructor. I intentionally used the explicit way to underline that $welcome is an object and that additional properties may be set or methods to prepare the object can be called.

In (4), we ask our renderer to display HTML page header, navigation trail and other standard components. Our class mod_mymod_renderer does not do this job itself but passes the call to the underlying core renderer automatically.

Finally in (5), we display our welcome message. The method render() accepts a renderable object and calls our own method render_greeting() defined in our renderer. This method accepts the only parameter which must be instance of mymod_greeting.

For the detailed description of the whole system and nice UML diagrams of class dependencies see the documentation page.

About

I love Linux kernel traffic and kernel trap newsletters and enjoy reading its Czech translation. So I decided I could try and write a short blog post every Monday on what happened in Moodle development world last week. Take this as an attempt. Maybe it survives, maybe not. This is the first post in the series. The number 07/2010 means the last week was the 7th week in the year 2010 according to my phone calendar (should be the ISO week numbering scheme).


4 Responses to “Moodle development traffic 07/2010”

  • mudrd8mz mudrd8mz Says:

    [New Post] Moodle development traffic 07/2010 – via @twitoaster http://blog.mudrak.name/2010/02/moodle-d...
    via Twitoaster

  • Olli Savolainen Says:

    I have been wondering that I would love if I could receive status updates from developers so I could know what is going on. Of course for me, this has to do with me wanting to offer help to any projects that have an UI, but in general I think it would be great for everyone to be more aware about what others are doing. So this was welcome indeed.

    I did not know exactly what using the output lib looks like, either, so thanks for this.

  • Hans de Zwart Says:

    Well, I would like to encourage you to keep it up every Monday! I enjoyed it… :-)

  • Andy Hediger Says:

    Hello David!
    your weekly posts are always really informative and helpful and also entertaining and they remain one of my favourite reads every week.
    Great! i’am looking always forward to your next :)
    Thank you again.

Leave a Reply

Spam Protection by WP-SpamFree Plugin