phpmoodlemoodle-api

How can I run a function when a course module is added or updated in moodle?


I try to add observers on course_module_updated and course_module_created but not triggered any of my Triggering ways

I tried: Purge all caches. -- not work log to file. -- not work log to database. -- not work die() -- not work

Is my code has wrong ?

Is the event triggered but log code not work

db/events.php

defined('MOODLE_INTERNAL') || die();
use moodle_local_plugin\event\events_observer;


$observers = [
    [
        'eventname' => '\core\event\course_module_updated',
        'callback' => 'events_observer::course_module_updated',
        'includefile' => null,
        'internal' => true,
        'priority' => 9999
    ],
    [
        'eventname' => '\core\event\course_module_created',
        'callback' => 'events_observer::course_module_updated',
        'includefile' => null,
        'internal' => true,
        'priority' => 9999
    ],
];

classes/event/observer.php

namespace moodle_local_plugin\event;

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

class events_observer
{
    /**
     * Triggered when 'course_module_updated' event is triggered.
     *
     * @param \core\event\course_module_updated $event
     * @return bool
     */
    public static function course_module_updated(\core\event\course_module_updated $event)
    {
        global $DB, $CFG;
        // Log to DB
        $logdata = new \stdClass();
        $logdata->eventname = $event->eventname;
        $logdata->component = 'local_course_editor';
        $logdata->action = 'updated';
        $logdata->target = 'course_module';
        $logdata->objectid = $event->objectid;
        $logdata->timecreated = time();

        $DB->insert_record('log', $logdata);


        // Log the event data to a file

        $logfile = $CFG->dirroot . '/local/moodle_local_plugin/log.txt';
        $timestamp = date('Y-m-d H:i:s');
        $eventData = json_encode($event->get_data(), JSON_PRETTY_PRINT);
        $logMessage = "[{$timestamp}] Course module updated:\n{$eventData}\n\n";

        file_put_contents($logfile, $logMessage, FILE_APPEND);

        // end
        die(var_dump($event));
        //   return true;
    }
}

Can any one help me with that ?

Thanks!


Solution

  • Firstly, your namespace is wrong - assuming your plugin is in local/myplugin, the namespace should be:

    namespace local_myplugin;
    

    With the namespace moodle_local_plugin\event, the Moodle code will be unable to automatically locate your code - also 'event' is a reserved namespace for event triggers, so you should not put event handlers in that same namespace. Finally, the name of the file that contains the class must match the name of the class (without the namespace), so I'm assuming the class name should be 'observer', not 'events_observer'.

    The 'callback' line is a string, so it cannot, automatically, take account of the 'use' line at the top of the file (this is a PHP feature, not a Moodle issue).

    You can change this (after the other fixes I've listed above) to:

    'callback' => 'local_myplugin\observer::course_module_updated'
    

    Alternatively, you could write:

    use local_myplugin\observer;
    ...
    'callback' => observer::class . '::course_module_updated';
    

    Once you've made all those fixes, you need to increase the version number of your plugin, in order to get Moodle to reread your db/events.php file.