Events API - course_category_created doesn't seem to work

Events API - course_category_created doesn't seem to work

by Michael Milette -
Number of replies: 11
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

I am trying to observe some Moodle events but it doesn't seem to be working. Can you help?

I tried the following in Moodle 2.5.1...

In my theme's db/events.php file, I added:

$handlers = array (
    'course_category_created' => array (
        'handlerfile' => '/theme/mytheme/lib.php',
        'handlerfunction' => 'mytheme_course_category_created',
        'schedule' => 'instant',
        'internal' => 1,
    ),
);

In my theme's lib.php file, I added:

function mytheme_course_category_created(stdClass $event) {
    error_log('DEBUG MESSAGE - course_category_created event triggered.');
    return true;
}

Next I created a new course category but the event was not triggered.

Then I tried the following in Moodle 2.6.1...

In my theme's db/events.php file, I added:

$observers = array (

    array (

        'eventname' => '\core\event\course_category_created',
        'callback' => 'theme_mytheme_observer:course_:category_created',
    )

);

In my theme's classes/observer.php, I added:

class theme_mytheme_observer {

     public static function course_category_created(\core\event\course_category_created $event) {
        error_log('DEBUG MESSAGE - \core\event\course_category_created event triggered.');
        return;
    }
}

Last I created a new category but again, the event was not triggered.

Any idea why the event is not being triggered? I suspect either:
* My code is wrong;
* Creating a new category doesn't trigger this particular event (observing wrong event)
* There is a bug in Moodle and the event doesn't ever get triggered.

If you have any insights, I would really appreciate hearing from you. I will also need to do this with course_category_updated and course_category_deleted. All of these events are listed in Moodle's own /lib/db/events.php.

My requirement is to get it working in Moodle 2.6 however it would be nice to have it backwards compatible with 2.5.1.

Best regards,

Michael

Average of ratings: -
In reply to Michael Milette

Re: Events API - course_category_created doesn't seem to work

by Ankit Agarwal -

Hi Michael,
For 2.6 code,

'theme_mytheme_observer:course_:category_created' , is incorrect, should be 'theme_mytheme_observer::course_category_created'.

Following code works for me:-

class theme_clean_observer {
    public static function course_category_created(\core\event\course_category_created $event) {
        error_log('DEBUG MESSAGE - \core\event\course_category_created event triggered.');
        return;
    }
}

$observers = array (
    array (
        'eventname' => '\core\event\course_category_created',
        'callback' => 'theme_clean_observer::course_category_created',
    )
);
In reply to Ankit Agarwal

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Thanks Ankit. The extra colon in course_category_created was a typo introduced when I was preparing the message.

I tried your code and unfortunately it didn't work.

Here is what I did. Maybe you can spot where I went wrong. Note that the clean theme was active of course:

1) Created a folder in the clean theme called db and then a file called events.php. In this file I pasted in your code:

$observers = array (
    array (
        'eventname' => '\core\event\course_category_created',
        'callback' => 'theme_clean_observer::course_category_created',
    )
);

2) Next I created a folder in the clean theme called classes and then a file called observer.php. In this file I pasted your code:

class theme_clean_observer {
    public static function course_category_created(\core\event\course_category_created $event) {
        error_log('DEBUG MESSAGE - \core\event\course_category_created event triggered.');
        return;
    }
}

3) Next I logged in to Moodle as a site administrator and clicked Home > Site Administration > Courses > Add a category. I completed the form and clicked the Create category button.

4) I checked my Apache error log. Nothing had been added.

5) Then I replaced the all of the above code with the following in both files:

     die('Found me!');

However it didn't make any difference. I am starting to suspect that Moodle isn't loading the files at all but I don't know why. I even increased the version number of my theme. That triggered a notification and I ran it though the database upgrade but it still doesn't work.

Any suggestions would be most appreciated. I am using is Moodle 2.6+ (Build: 20131122)... just in case it makes a difference.

Best regards,

Michael

In reply to Ankit Agarwal

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

FYI, I just installed a new instance of the latest Moodle 2.6.2 (Build: 20140310) and repeated the above steps. Unfortunately I got the same results. sad

In reply to Michael Milette

Re: Events API - course_category_created doesn't seem to work

by Ankit Agarwal -

Also have a look at MDL-39957 and MDL-40912 to see when various events related to course where introduced.

In reply to Ankit Agarwal

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Thanks Ankit. MDL-39957 was implemented last summer so those changes are integrated. The changes mentioned in MDL-40912 were just added in January so those would not be included in the version I am running as they are only scheduled to only be integrated as of Moodle 2.7.

The fact that you have it working in your Moodle 2.6 using the example you provided in your previous post tells me that this should work without MDL-40912.

In reply to Michael Milette

Re: Events API - course_category_created doesn't seem to work

by Ankit Agarwal -

Sorry Micahel,

I didn't realise, I was testing on Moodle 2.7 instead of 2.6 , the course_category_created event was introduced in MDL-40912, which is present in 2.7 only. Anyway your post made me realise we need make it clear when specific events were added in docs, I will make sure that happens soon.   A easy way for you to know which events are present in your specific install of Moodle is looking at the files in lib/classes/event for core and classes/event for plugins.

Average of ratings: Useful (2)
In reply to Ankit Agarwal

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Thanks Ankit.

I'll admit that I am disappointed to hear that I won't be able to do real-time updates and will have to rely on a periodic cron process instead until we implement 2.7. That being said, I am glad to know that I can stop trying to figure out what I might have been doing wrong.

It will be very useful to know which and when observable events were added to Moodle because the course_category_* events were listed as being available in Moodle 2.5.1's /lib/db/events.php which is now obviously not the case.

Thanks again Ankit. Looking forward to 2.7.

Best regards,

Michael

 

In reply to Michael Milette

Re: Events API - course_category_created doesn't seem to work

by Shashikant Vaishnav -

Hello Michael,

Here are few quick suggestions that might help you.

 

  • Make sure the event file is properly written (no syntax issue)
  • You bumped you're plug-in's version ?
  • The trigged method are accessible.

Here is the even implementation I did with my Gsoc project. I was triggering events whenever a course / category is deleted / updated / added.

Here is the events code - moodle-tool_coursesearch.

Hope it helps.

Good luck,

Shashikant

 

 

In reply to Shashikant Vaishnav

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Shashikant,

Thank you for taking the time to try to help. 

Although your sample code works for changes to courses, it doesn't seem to observe course categories changes. I checked my syntax as you suggested and bumped the version of the theme but didn't notice any change. I suspect as Ankit pointed out, these course_category_* events simply haven't been implemented yet.

Thanks again for your suggestions.

Best regards,

Michael

In reply to Michael Milette

Re: Events API - course_category_created doesn't seem to work

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You need to bump your plugin's version number for your event observer methods to kick in.

Here's an example events file to define your observer methods, with corresponding auto-loaded observer class.

In reply to Paul Holden

Re: Events API - course_category_created doesn't seem to work

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Paul,

Thank you for taking the time to try to help and the links to an example. That's pretty much what I was doing except that I am trying to catch the course_category_* events. I did bumped my plugin version as you suggested. I think Ankit is correct when he said that these course_category_* events simply haven't been implemented yet.

Thanks again for your suggestions. Let me know if you have anymore thoughts.

Best regards,

Michael