Event course_module_created

Event course_module_created

by Helson C -
Number of replies: 8

Hello,

How can I add a new event to SCORM module?

I tried to add a new event to scorm module, then I put inside 


scorm > classes > event > course_module_created.php

namespace mod_scorm\event;

defined('MOODLE_INTERNAL') || die();

class course_module_created extends \core\event\course_module_created {

    protected function init() {

        die('bye world');

    }

}

The code above is never executed when a scorm module is created.



Average of ratings: -
In reply to Helson C

Re: Event course_module_created

by Helson C -

Anyone?

Thanks in advance.

In reply to Helson C

Re: Event course_module_created

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Did you write some code to call this new event?

Events don't just magically get called based on you giving them a name that sounds like they should be called.


In reply to Davo Smith

Re: Event course_module_created

by Helson C -

Well, I put this name based on https://docs.moodle.org/dev/Event_2

I need that event be called when a new scorm module added on course.

I put this new event on scorm module code, then how can I  trigger it?


Thanks.

In reply to Helson C

Re: Event course_module_created

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, if you really want to modify the core code to trigger this new event when a SCORM module is created, then you can read up on it at: https://docs.moodle.org/dev/Event_2#Triggering_events

Really, though, you probably want to write some code that handles the '\core\event\course_module_created' event and then checks to see if the activity created was a SCORM - that's going to be a lot neater and easier to maintain that modifying core code to add a new custom event for a specific activity type.


In reply to Davo Smith

Re: Event course_module_created

by Helson C -

Yes, I understand perfectly.

I was wonder that all events located in https://docs.moodle.org/dev/Event_2#Existing_events

can be added and automatically triggered by core.


Then this events https://docs.moodle.org/dev/Event_2#Existing_events do not automacally called

And if not, the only way to do it is modify core code?


Thank you!

In reply to Helson C

Re: Event course_module_created

by Virgil Ashruf -

You really need an event observer (also called event handler). I suggest you do the following:

  1. Create a local plug-in: https://docs.moodle.org/dev/Local_plugins
  2. Add an events.php to tell Moodle you want to observe an event: https://docs.moodle.org/dev/Event_2#Event_observers
  3. Write the code that handles your observation
  4. Make sure the methods or classnames in your code match the entry in your events.php
  5. Install the plug-in or update it by bumping the version number so the observer is added to the Moodle's event magic.
  6. Perform an action that triggers the event: e.g. add a SCORM module. You will now see your code running.

An example: the assignment plug-in also listens for events:

https://github.com/moodle/moodle/blob/master/mod/assign/db/events.php#L29

It tells Moodle that it listens for "\core\event\course_reset_started" and then wants to run code identified by "\mod_assign\group_observers::course_reset_started". This notation style uses the namespace functionality within PHP so you don't need to refer to files that you need to include. However, if you want to keep stuff simple in your code; you can add two other properties to refer to a PHP file and a function within that file.

The code that is run and identified by "\mod_assign\group_observers::course_reset_started"  can be found here:

https://github.com/moodle/moodle/blob/master/mod/assign/classes/group_observers.php#L52


So in short:

  1. Write code that should react to an event
  2. Tell Moodle to watch for an event and what should happen when that event is triggered (see 1)
  3. ?
  4. Profit


Average of ratings: Useful (2)
In reply to Virgil Ashruf

Re: Event course_module_created

by Helson C -


 Perfect!


Thank you so much for you atenttion.

I'll try it and come back to say the final result..

In reply to Virgil Ashruf

Re: Event course_module_created

by Helson C -

Hello,

Can I get the local files of the module scorm when uploading?

I need modify some files of scorm during upload of activity module.


Thanks in advance! blush