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_plugineventevents_observer;
$observers = [
[
'eventname' => 'coreeventcourse_module_updated',
'callback' => 'events_observer::course_module_updated',
'includefile' => null,
'internal' => true,
'priority' => 9999
],
[
'eventname' => 'coreeventcourse_module_created',
'callback' => 'events_observer::course_module_updated',
'includefile' => null,
'internal' => true,
'priority' => 9999
],
];
classes/event/observer.php
namespace moodle_local_pluginevent;
defined('MOODLE_INTERNAL') || die();
class events_observer
{
/**
* Triggered when 'course_module_updated' event is triggered.
*
* @param coreeventcourse_module_updated $event
* @return bool
*/
public static function course_module_updated(coreeventcourse_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}nn";
file_put_contents($logfile, $logMessage, FILE_APPEND);
// end
die(var_dump($event));
// return true;
}
}
Can any one help me with that ?
Thanks!