Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Messaging consumers

From MoodleDocs
Revision as of 12:15, 26 November 2012 by Tim Hunt (talk | contribs)

A message consumer, also known as a message processor, can be configured by users to process messages they receive. Typically this is to notify the user that they have received a message. For example, the email message processor notifies the user of new messages via email.

A sample custom message processor is available from github.

A custom message processor lives in a directory like /message/output/mymessageprocessor. The directory structure is below.

/message/output/mymessageprocessor
                                  /message_output_mymessageprocessor.php
                                  /version.php
                                  /lib.php (optional)
                                  /lang/
                                       /en/ (en for English)
                                          /message_mymessageprocessor.php
                                  /db/
                                     /install.php
                                     /upgrade.php

It should contain a class that extends message_output. message_output can be found in /message/output/lib.php In the above example the class is called message_output_mymessageprocessor and is defined in message_output_mymessageprocessor.php.

Your class should implement at least these methods

// Process an incoming message send_message($eventdata)

// Load any user preferences related to your message processor and add them to the preferences array load_data(&$preferences, $userid)

// Generate a configuration form for the messaging preferences screen based on the supplied preferences and return it as a string config_form($preferences)

// Process data submitted on the messaging preferences screen and save it as user preferences process_form($form, &$preferences)

Make sure to set a version number for your message processor. This is a number formatted like 201106210.00 Reading this from the left this is 2011, june 21st with several extra digits to allow for multiple versions to be produced on the same day. That sometimes happens but these numbers will usually be zeros. Your version number appears once in mymessageprocessor/version.php and twice in mymessageprocessor/db/upgrade.php

You will see another version number in mymessageprocessor/version.php $plugin->requires = 2011060200.00; This is the Moodle version that your processor requires. Go to Moodle's own version.php in the top level directory of your Moodle install, copy Moodle's version number and paste it over the value currently in $plugin->requires. This means that any Moodle installation using your message processor must be at least the same Moodle version as you are currently using.

When you log in as administrator and you should be prompted to upgrade. The upgrade process will install your new message processor. The upgrade code can be found in /message/output/mymessageprocessor/db/upgrade.php. At a minimum it should insert a row into the message_processors table.

If anything is unclear look at the example message processor implementation and Moodle's default message processors. The email, jabber and popup message processors can be found at /message/output.