Note:

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

Message API: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 30: Line 30:


===How to register as a message producer===
===How to register as a message producer===
The messages produced by a message provider is defined in the /db/messages.php file of a component. Below is the quiz's messages.php as an example.
<code php>
defined('MOODLE_INTERNAL') || die();
$messageproviders = array (
    // Notify teacher that a student has submitted a quiz attempt
    'submission' => array (
        'capability'  => 'mod/quiz:emailnotifysubmission'
    ),
    // Confirm a student's quiz attempt
    'confirmation' => array (
        'capability'  => 'mod/quiz:emailconfirmsubmission'
    )
);
</code>
The quiz can send two kinds of messages, quiz "submission" and "confirmation" notifications. Each message type is only available to users with the appropriate capability. Users who have this capability will have this message listed in their messaging preferences. You can omit the capability section if your message should be visible for all users. For example forum post notifications are available to all users.
<code php>
$messageproviders = array (
    // Ordinary single forum posts
    'posts' => array (
    )
);
</code>
When displaying your message types in a user's messaging preferences it will use a string from your component's language file called "messageprovider:messagename". For example here are the relevant strings from the quiz's language file.
<code php>
$string['messageprovider:confirmation'] = 'Confirmation of your own quiz submissions';
$string['messageprovider:submission'] = 'Notification of quiz submissions';
</code>
Once your messages.php is complete you need to increase the version number of your component in its version.php. That will cause Moodle to check messages.php looking for new or changed message definitions. Log in as an admin and go to /admin/index.php to start the upgrade process.


===How to send a message===
===How to send a message===
Here is example code showing you how to actually send a message.
<code php>
        $eventdata = new stdClass();
        $eventdata->component        = 'mod_quiz'; //your component name
        $eventdata->name              = 'submission'; //this is the message name from messages.php
        $eventdata->userfrom          = $USER;
        $eventdata->userto            = $touser;
        $eventdata->subject          = $subject;
        $eventdata->fullmessage      = $message;
        $eventdata->fullmessageformat = FORMAT_PLAIN;
        $eventdata->fullmessagehtml  = '';
        $eventdata->smallmessage      = '';
        $eventdata->notification      = 1; //this is only set to 0 for personal messages between users
        message_send($eventdata);
</code>

Revision as of 09:26, 12 January 2012

TODO: delete https://docs.moodle.org/dev/Messaging, this document replaces it.

No, don't bother. Start with Messaging_2.0 - rename it here if you must, clean it up a bit, and you are done. Much time saved. It really worries me that I have seen you guys launch into new documentation pages without finding the existing good documentation.--Tim Hunt 17:01, 12 January 2012 (WST)

What is this document?

This document describes how to make use of the Moodle messaging API to send messages to Moodle users.

If you are after a general introduction on using the Moodle messaging system go to https://docs.moodle.org/en/Messaging

If you are looking for implementation details of the messaging system's internal structure go to https://docs.moodle.org/dev/Messaging_2.0

If you are looking for instructions on the implementation of a custom message processor (a component that receives messages sent to a user) go to https://docs.moodle.org/dev/Messaging_custom_components

If you are looking for instructions on sending messages programatically within Moodle then read on...

Overview

Moodle components have the ability to send message to users via the Moodle messaging system. Any type of component, for example a plugin or block, can register as a message producer then send messages to users.

File locations

The Message API is all in lib/messagelib.php and is automatically included for you during the page setup.

Functions

message_send() is the primary point of contact for the message API. Call it to send a message to a user. You can find a full description of the arguments that must be supplied at (link to phpdocs)

Examples

How to register as a message producer

The messages produced by a message provider is defined in the /db/messages.php file of a component. Below is the quiz's messages.php as an example. defined('MOODLE_INTERNAL') || die(); $messageproviders = array (

   // Notify teacher that a student has submitted a quiz attempt
   'submission' => array (
       'capability'  => 'mod/quiz:emailnotifysubmission'
   ),
   // Confirm a student's quiz attempt
   'confirmation' => array (
       'capability'  => 'mod/quiz:emailconfirmsubmission'
   )

);

The quiz can send two kinds of messages, quiz "submission" and "confirmation" notifications. Each message type is only available to users with the appropriate capability. Users who have this capability will have this message listed in their messaging preferences. You can omit the capability section if your message should be visible for all users. For example forum post notifications are available to all users.

$messageproviders = array (

   // Ordinary single forum posts
   'posts' => array (
   )

);

When displaying your message types in a user's messaging preferences it will use a string from your component's language file called "messageprovider:messagename". For example here are the relevant strings from the quiz's language file. $string['messageprovider:confirmation'] = 'Confirmation of your own quiz submissions'; $string['messageprovider:submission'] = 'Notification of quiz submissions';

Once your messages.php is complete you need to increase the version number of your component in its version.php. That will cause Moodle to check messages.php looking for new or changed message definitions. Log in as an admin and go to /admin/index.php to start the upgrade process.

How to send a message

Here is example code showing you how to actually send a message.

       $eventdata = new stdClass();
       $eventdata->component         = 'mod_quiz'; //your component name
       $eventdata->name              = 'submission'; //this is the message name from messages.php
       $eventdata->userfrom          = $USER;
       $eventdata->userto            = $touser;
       $eventdata->subject           = $subject;
       $eventdata->fullmessage       = $message;
       $eventdata->fullmessageformat = FORMAT_PLAIN;
       $eventdata->fullmessagehtml   = ;
       $eventdata->smallmessage      = ;
       $eventdata->notification      = 1; //this is only set to 0 for personal messages between users
       message_send($eventdata);