Event API in local Plugin

Event API in local Plugin

by Shincy T U -
Number of replies: 4

I have created a local plugin to use event API. Available events are worked for me.  I need to create a custom event which trigger when a quiz is created in the moodle. I am new to moodle and didn't understand where should I write the code for triggering event.

The have complete the following steps

  • Created a local plugin
  • created db/events.php
  • created classes/observer.php
  • created classes/event/quiz_created.php
  • updated version.php with my plugin name.

Anybody please help 

Average of ratings: -
In reply to Shincy T U

Re: Event API in local Plugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
To create a new event trigger, you need to edit the code that does the action that you want to link an event to - so, if you want to create a new event that triggers when a quiz activity is created, then you'd need to edit the mod/quiz/lib.php file and add the event trigger to the quiz_create_instance() function.

However, you don't really need to do that, as there is already an \core\event\course_module_created event, that includes $event->other['modulename']; if you catch this event and check the 'modulename' value (to see if it matches "quiz"), then you've got the equivalent of a "quiz created" event, without having to modify the core code.
Average of ratings: Useful (1)
In reply to Davo Smith

Re: Event API in local Plugin

by Shincy T U -
Thank you Davo.

It's working. Used $event->other['modulename'] to check quiz is created or not. Hope the same way I can check for assignment is created or not
In reply to Shincy T U

Re: Event API in local Plugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Absolutely - just check for $event->other['modulename'] == 'assign' and you'll know that an assignment has been created.
Average of ratings: Useful (2)