Scorch
Scorch

Reputation: 173

Sending a message to an user in moodle

I am currently trying to implement what should be a simple task and send a message to one and / or more users via Moodle. In the process I found 2 guides, which confused me at first. Messaging 2.0 and Message API. Both have some differences and I didn't understand if my plugin needs to register as a Message Producer or not.

Unfortunately, I just can't get my message to be sent. I logged in with the user and checked the mailbox, no message there. Any help is appreciated.

What I did: Created message.php within my db - folder and inserted the following code:

<?php
defined('MOODLE_INTERNAL') || die();

$messageproviders = array (
    'datenotification' => array (
    )
);

After that I expanded my language file with this:

$string['messageprovider:datenotification'] = 'Reminder for a presentation';

Like the guide advised, I updated my plugin to insert my messageprovider in the table mdl_message_providers. Finally, I implemented the actual sending of the message.

$eventdata = new \core\message\message();
$eventdata->component         = 'local_reminder';    // the component sending the message. Along with name this must exist in the table message_providers
$eventdata->name              = 'datenotification';        // type of message from that module (as module defines it). Along with component this must exist in the table message_providers
$eventdata->userfrom          = core_user::get_noreply_user();      // user object , no-reply
$eventdata->userto            = $user;        // user object from database
$eventdata->subject           = 'Test message';   
$eventdata->fullmessage       = 'This is my test message';      
$eventdata->fullmessageformat = FORMAT_PLAIN;   // text format
$eventdata->fullmessagehtml   = '<p>This is my test message</p>';      
$eventdata->smallmessage      = '';            
$eventdata->courseid = $course_id; // This is required in recent versions, use it from 3.2 on https://tracker.moodle.org/browse/MDL-47162

$result = message_send($eventdata);

Debugging:

var_dump($eventdata); //-> All data is included                                              
var_dump($result); // returns int(5), id of message, no error

Upvotes: 0

Views: 734

Answers (1)

Russell England
Russell England

Reputation: 10241

Adding the comment as an answer for future reference

Check if the message is installed and enabled via site admin > messaging > notification settings or direct to yoursite/admin/message.php

Upvotes: 1

Related Questions