Events and Event handlers

Events and Event handlers

by Dinesh Kumar -
Number of replies: 14

Hello moodlers,

I have read the Events API documentations many times. But I could not get the information what I am looking for. Can anybody explain how we can use events listed in Events API page. How we can use/customise listed events there. Are they available already or we need to work on top of that. Any information would be really helpful.

Many thanks,

Dinesh.

 

Average of ratings: -
In reply to Dinesh Kumar

Re: Events and Event handlers

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

One example is in the quiz. The quiz triggers a 'quiz_attempt_submitted' event when a student submits a quiz attempt. It also listens for that event, and when it recieves one, it sends the notification emails. (This is so that if an error occurs when sending the email, it will not prevent the attempts being submitted.)

Another example is in forum. The forum recieves user_enrolled and user_unenrolled events in order to automatically subscribe and unsubscribe people from forums in the course.

We have some custom plugins at the OU which listen for course_added and course_deleted events, in order to clean up data belonging to the plugin that is linked to the course.

In reply to Tim Hunt

Re: Events and Event handlers

by Dinesh Kumar -

Hi tim,

Thank you for the informations. It helps a lot to clear my doubts to proceed with moodle events.


Thanks and regards,

Dinesh.

In reply to Dinesh Kumar

Re: Events and Event handlers

by Ramesh Jambulingam -

Hi tim,

Your comments are really useful. Based on the examples you pointed out, I tried to write an event handler function for assessable_file_uploaded


With given below function I am trying to send a email notification to all administrators.

function assessable_file_uploaded_handler($eventdata) {
    global $DB;

 
        $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST);
     
        if ($admins = $this->get_admins()) {
            foreach ($admins as $admin) {
                $this->send_notification($user, $admin, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified);
            }
        }
        return true;

}
for more information this function is added to mod/assignment/locallib. Also I got a DB entry for this event handler. But yet to send the email notifications.

i hope I need to use the parameter $eventdata. Can anyone point me out to use $eventdata to send the notification mails.

 

Regards,

Ramesh.

In reply to Ramesh Jambulingam

Re: Events and Event handlers

by Hubert Chathi -

$eventdata is arbitrary data that is passed by the code that triggers the event.  I don't think that there is much documentation for the event data that is sent for each type of event, but you can find out easily enough by just searching the code for where the event is triggered (just search for the event name), and seeing what data is passed.

In reply to Hubert Chathi

Re: Events and Event handlers

by Ramesh Jambulingam -

Hi Hubert,

Thank you for the reply. I found the place from where the event is triggered. Its mod/assignment/type/upload(single)/assignment.class.php

  $eventdata = new stdClass();
                    $eventdata->modulename   = 'assignment';
                    $eventdata->cmid         = $this->cm->id;
                    $eventdata->itemid       = $submission->id;
                    $eventdata->courseid     = $this->course->id;
                    $eventdata->userid       = $USER->id;
                    $eventdata->file         = $file; // This is depreceated - please use pathnamehashes instead!
                    $eventdata->pathnamehashes = array($file->get_pathnamehash());
                    events_trigger('assessable_file_uploaded', $eventdata);

I created an entry in mod/assign/db/events.php file as below.

'assignment_file_sent' => array (
        'handlerfile'      => 'mod/assign/locallib.php',
        'handlerfunction'  => 'assessable_file_uploaded_handler',    // argument to call_user_func(), could be an array
        'schedule'         => 'instant',
        'internal'         => 1,
    ),

Then I wrote the event handler function in mod/asssign. But the event handler function is not recognised now. How can I send email notification to admins not to teachers (which is default). Will the below handling code help?

 if ($admins = $this->get_admins()) {
            foreach ($admins as $admin) {
                $this->send_notification($user, $admin, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified);
            }
        }
        return true;   


Thanks and Regards,

Ramesh.

In reply to Ramesh Jambulingam

Re: Events and Event handlers

by Hubert Chathi -

Did you bump the version of your plugin, and go to the Site Administration > Notifications page?  Moodle will not recognize new event handlers until it goes through the install/upgrade process for plugins.

In reply to Hubert Chathi

Re: Events and Event handlers

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, events should be documented in the db/events.php file of the plugin that declares them.

That file has to define an array giving details of the events you want to be notified about.

It should also contain comments explaining all the events your plugin raises, and what the corresponding event data is. E.g. https://github.com/moodle/moodle/blob/master/mod/quiz/db/events.php

In reply to Tim Hunt

Re: Events and Event handlers

by Ramesh Jambulingam -

Hi Tim,


I am working out this on assignment module and my mod/assign/db/events.php file has event definition as below,

$handlers = array(

'assessable_file_uploaded' => array (
        'handlerfile'      => '/mod/assign/locallib.php',
        'handlerfunction'  => 'assessable_file_uploaded_handler',    // argument to call_user_func(), could be an array
        'schedule'         => 'instant',
        'internal'         => 0,
    ),
);

My handler function is placed in mod/assign/locallib.php. Still the function is not recognised. I simulated the logic of sending notication to graders (teachers)  in order to send notification to admins.

if ($admins = $this->get_admins()) {
            foreach ($admins as $admin) {
                $this->send_notification($user, $admin, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified);
            }
        }
        return true; 

}

The function get_admins function is not fetching the admins list? Do I need a change of approach here to send email notification to admins.


Thanks and regards,

Ramesh.

In reply to Ramesh Jambulingam

Re: Events and Event handlers

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm afraid I cannot debug your code for you. Events elsewhere in Moodle work. Your event handing does not work. You need to investigate and work out what the problem is.

In reply to Tim Hunt

Re: Events and Event handlers

by Mansha Maurya -
Hii Tim,

I am a Php Developer and I am recently working on moodle so pls can you guide me about the db/event.php ,I m not getting that why its is essential to use handler array...

$handlers = array(
    'user_created' => array (
        'handlerfile'     => '/local/welcome/event_handlers.php',
        'handlerfunction' => 'send_welcome',
        'schedule'        => 'instant',
    ),
);


Thanks,
Mansha Maurya
In reply to Mansha Maurya

Re: Events and Event handlers

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I am not sure what you mean when you ask "why its is essential to use handler array?"

If you are asking "Why is it essential for a developer to use a hander array if they want to use events in their plugin?" - because your events code will only work if you do!

If you are asking "Why does Moodle require developers to do this?" - Well, think about what the Moodle events system is trying to do. When a certain event happens, it must inform all the plugins that have asked to be told about that event. How does Moodle know which plugins those are? Well, the plugins that want events have to tell Moodle which ones they want. That is what this array is for.

In reply to Tim Hunt

Re: Events and Event handlers

by Mansha Maurya -

Hi tim,

Thank s for the informations. It really clear my doubts to proceed with moodle events.


Thankyou

Mansha


In reply to Tim Hunt

Developing a new pluging

by Mansha Maurya -

Hi Tim,

I actually need you guidance for " Developing new plugins"

I want to create a plugin which display a Hello World message pls show me a way from where I can start.


Thank you,

Mansha



In reply to Mansha Maurya

Re: Developing a new pluging

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

What have your web searches located so far (to avoid us duplicating your existing knowledge)