on
Advance HTML
HTML
- Get link
- X
- Other Apps
Logging and events are covered together because in Moodle they are the same thing. As a module developer, you create events when something that is of our interest occurs. All those events are observed by core log store manager and preserved as logs. On top of it, any other module can register itself to listen (observer pattern) to your events and act accordingly.
To illustrate it - let’s add 2 events to our module and then observer to listen to one of them.
One of the most common log entry to implement is to record when a module has been viewed. All other modules implement it and there is even special core class called course_module_viewed exactly for this. We start by implementing new class \mod_php\event\course_module_viewed. Put the code below into mod/php/classes/event/course_module_viewed.php:
namespace mod_php\event;
defined('MOODLE_INTERNAL') || die();
class course_module_viewed extends \core\event\course_module_viewed {
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'php';
}
}
The minimum information that we need to provide is - the part common for every time we trigger an event:
And some information that depends on the context (the time when event is fired):
Some more information, like current user will be added for us.
With the new event ready, it can now be triggered. We will put the code in the view.php script:
$event = \mod_php\event\course_module_viewed::create(array(
'objectid' => $PAGE->cm->instance,
'context' => $PAGE->context,
));
$event->trigger();
With this code, we start getting Moodle logs that we can review in Reports -> Logs.
Let’s create another event that is triggered after submission is graded. This time we will need to extend base event class \core\event\base. We also need to implement twi extra methods that were done for us in the previous case (we inherited them from course_module_viewed()): get_description() and get_name(). get_name() returns translated language, so we also need to add appropriate line to mod/php/lang/en/php.php.
class submission_graded extends \core\event\base {
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'php';
}
public function get_description() {
return "The user with id '$this->userid' has graded the submission '$thi\
s->objectid' for the user with " .
"id '$this->relateduserid' for the assignment with course module id '$th\
is->contextinstanceid'.";
}
public static function get_name() {
return get_string('eventsubmissiongraded', 'mod_php');
}
}
When we trigger an event, we add extra bit of information:
Information stored with add_record_snapshot() is not used by log store manager - but it might be useful for other observers.
$data = array(
'context' => $context,
'objectid' => $userid,
'relateduserid' => $userid
);
$event = \mod_php\event\submission_graded::create($data);
$event->add_record_snapshot('php_submissions', $submission);
$event->trigger();
Let’s add an observer to listen to submission_graded event. We do it by declaring it in new file mod/php/db/events.php:
$observers = array(
array(
'eventname' => '\mod_php\event\submission_graded',
'callback' => '\mod_php\observer::grading',
),
);
We simply say to which event we want to listen to and which callable code should execute upon event triggering.
The last thing to do is to implement our callable - observer class in mod/php/classes/observer.php:
class observer {
static public function grading(\mod_php\event\submission_graded $event) {
global $DB;
$supportuser = \core_user::get_support_user();
$relateduser = $DB->get_record('user', array('id' => $event->relateduser\
id));
email_to_user($supportuser, $relateduser, "Your PHP submission has been \
graded", "Hi " . fullname($relateduser) .
" visit your Moodle site for the details.");
}
}
We use this opportunity to show how to send an email using Moodle API with email_to_user() function. When developing email functionality, you need to be careful not to trigger sending an email to real users. For the development environment, I’d recommend to alway set up email redirect in config.php;
1 $CFG->divertallemailsto = 'youremail@example.com';
To notify Moodle about new observer you either need to bump module version in version.php and perform an upgrade, or clear the caches. To confirm all is OK, check the report under Site administration -> Reports -> Events list:
Comments
Post a Comment