Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Events API: Difference between revisions

From MoodleDocs
Line 2,319: Line 2,319:


The last word (after the last underscore) must be a verb.
The last word (after the last underscore) must be a verb.
=== Deprecated events ===
Following are the events that were supported in 2.5, but deprecated in 2.6 or later
groups_groupings_deleted (see MDL-41312)
groups_groupings_groups_removed (see MDL-41312)
groups_groups_deleted (see MDL-41312)
groups_members_removed (see MDL-41312)


== Shared events ==
== Shared events ==

Revision as of 07:26, 4 March 2014

Events 2
Project state Implementation in progress
Tracker issue MDL-39797 , MDL-39952, MDL-39846
Discussion https://moodle.org/mod/forum/discuss.php?d=229425
Assignee Backend Team


What are events?

Events are atomic pieces of information describing something that happened in Moodle. Events are primarily the result of user actions, but could also be the result of the cron process or administration actions undertaken via the command line.

When an action takes place, an event is created by a core API or plugin. The Events system then disseminates this event information to observers registered for this event. In this way, the events system acts as a communication backbone throughout the Moodle system.

Event observers can not modify event data or interrupt the dispatching of events, it is a one way communication channel.

Why is a new events system needed?

The need to improve the Events system was prompted by a need for a richer and more efficient logging system, however the benefits of this improvement will be useful to other parts of Moodle that observe event information.

  • The events need to be more strictly defined if we want to use them for new logging and other advanced use cases. They need to contain a lot more information in a standardised way (such as most fields from current log table and log_actions table).
  • Complex data types were allowed in old events which was causing major problems when serialising/storing/unserializing the data.
  • The logging and events contain similar information and are tiggered at the same places, new events would remove this code duplication. All events should be loggable and all current log entries should be triggered as events.
  • The logging system will become an event observer, listening to all events and directing them to logging storage plugins in a controllable way.
  • It will be possible to subscribe to '*' event, which would allow a system to potentially observe, and selectively deal with, all events. Current handlers do not get event name which makes this problematic.
  • Current event handlers may trigger exceptions during site upgrade which would lead to fatal upgrade problems. The new design eliminates this.
  • Failure in handlers blocked dispatching of subsequent events. Instead problems in new observers would be only logged and execution would continue normally.
  • Current execution of external handlers during DB transactions blocks other handlers. This would be eliminated by in-memory buffer for external events.
  • It would good to have observer priority.
  • Nested events are not dispatched sequentially, it would change the order of events received in lower priority handlers.

Performance

Some basic profiling has been conducted.

There is a general plan to complete pre- and post-implementation testing as development happens. The new system will be imemented in parallel with the old one which should help with comparison of new and old logging and event triggering performance on each page.

Our aim is to trigger more events and log more information, which is going to impact on performance. We hope to offset that impact by improving log storage, simplifying event dispatching and adding other core performance improvements. The proposed class structure of the base event should allow some new advanced techniques, which may also improve performance in some scenarios.

More details will be added to this section soon.

Events API

Each plugin will define the events that it can report (trigger) by extending an abstract base class, once for each possible event. This approach will have several benefits.

Events will be active objects
When they are triggered and possibly after they are reinstantiated (say, when they are retrieved from a log), an event object will be able to provide callback functions for various purposes (such as capability checks).
Automatic inclusion
Event class definitions will be automatically included when needed, without having to maintain lists of known event types. New event definitions can be added without the need to upgrade, only purging of MUC cache is required after adding new observer.
Maintainability
It will be easy to add new events and migrate existing events. Code review will be simplified because there will be less duplication of code when triggering events and all event related information/code will be concentrated in one file in fixed locations.
Self documenting
The behaviour of events will be combined with the definition of events in one place (file). It will be easy for event observer writers to know what events a plugin can trigger. This includes support for autocompletion and code inspection in modern IDEs.
Quick, self-validating data structure
As events are instantiated objects, the PHP processor will validate the structure and type of event classes. This does not ensure data value validity, but does give some assurance of consistency and it also detects common typos.

Backwards compatibility and migration

Events:

  • Moodle core and standard plugins will replace all calls to the events_trigger() function with new events classes.
  • For events that already exist in Moodle 2.5 the additional legacy information should be added to the event data (in properties 'legacyeventname' and 'legacyeventdata'.
  • The function events_trigger() will continue working as before, but it will be called automatically after a new event is processed using the 'legacyeventname' and 'legacyeventdata'.
  • The legacy events handling code will be maintained separately and will continue being supported in Moodle 2.x. New legacy events will not be added.
  • Existing legacy event handlers will be migrated to new event handlers accepting new event class instances.
  • More subsystems may be migrated to events-handlers, ex.: gradebook history.

Logging:

  • Function add_to_log() and all logging internals will continue working as before.
  • Existing add_to_log() parameters will be migrated inside new events method get_legacy_log_data() and core_event_base::trigger() will call add_to_log() automatically (this will depend on $CFG->loglifetime setting for performance reasons).

Event dispatching and observers

The new event dispatching system is completely separate from the old events code. Original event handlers are now called observers with the description stored in the same db/events.php file, but as a new array with a different format.

Event observers

The observers are described in db/events.php in the array $observers, the array is not indexed and contains a list of observers defined as an array with the following properties;

  • eventname - event class name or "*" indicating all events. All events must use namespace, ex.: \plugintype_pluginname\event\something_happened.
  • callback - PHP callable type.
  • includefile - optional. File to be included before calling the observer. Path relative to dirroot.
  • priority - optional. Defaults to 0. Observers with higher priority are notified first.
  • internal - optional. Defaults to true. Non-internal observers are not called during database transactions, but instead after a successful commit of the transaction.

$observers = array(

   array(
       'eventname'   => '\core\event\sample_executed',
       'callback'    => 'core_event_sample_observer::observe_one',
   ),
   array(
       'eventname'   => '\core\event\sample_executed',
       'callback'    => 'core_event_sample_observer::external_observer',
       'priority'    => 200,
       'internal'    => false,
   ),
   array(
       'eventname'   => '*',
       'callback'    => 'core_event_sample_observer::observe_all',
       'includefile' => null,
       'internal'    => true,
       'priority'    => 9999,
   ),

);

Event dispatching

A list of available observers is constructed on the fly directly from all available events.php files. Previously handlers were installed only during installation and upgrade. There is no risk of performance regression because the list is already cached in MUC. Observers get events before installation or any upgrade, however observers are not notified during the initial installation of moodle core tables.

Developers of observers must make sure that execution does not end with a fatal error under any condition (before install, before upgrade or normal operation). Exceptions are automatically captured, logged in the PHP error log, and notification of other observers continues. Current handlers must not throw any exceptions at any time.

Observers are notified sequentially in the same order in which events were triggered. This means that events triggered in observers are queued in FIFO buffer and are processed after all observers are notified.

Differences from old event handling

  1. New events contain a lot more structured information.
  2. New event data must not contain any PHP classes.
  3. There is separate context cache which may be used when deleting data or for observer performance improvements.
  4. No database access in new event dispatching code.
  5. There is no support for cron execution - this eliminates performance problems, simplifies events dispatching and prevents abuse of cron events.
  6. Events triggered in observers are processed in a different order.
  7. External events are buffered when a transaction is in progress, instead of being sent to the cron queue.
  8. It is possible to define multiple observers for one event in one events.php file.
  9. It is possible to subscribe an observer to all events.
  10. The new event manager is using frankenstyle autoloading - smaller memory footprint when events are not used on the current page.

Triggering events

  • All event descriptions are objects extending the \core\event\base class.
  • Events are triggered by creating a new instance of the class event and executing $event->trigger().
  • Each event class name is a unique identifier of the event.
  • Class names and namespace follow the identifier scheme \frankenstyle_component\event\some_object_action. Core events have prefix 'core_'.
  • Plugins define each event class in a separate file. File name and location must match the class name, for example: plugindir/classes/event/something_happened.php
  • The event identifier suffix has the form some_object_action (something_happened in the example above) and should follow our standard naming convention. The last word after underscore is automatically parsed as action, the rest of words is object.

Decision: Recommended verb list

Examples: \core\event\course_completed, \mod_assign\event\submission_commented, \mod_forum\event\post_shared, \mod_forum\event\post_responded, etc.

  • Ideally, it should be possible to trigger an event without gathering additional information for the event. To reduce the cost of data gathering, specifically the cost of database reads, at least the minimal values needed to trigger an event should be already available in variables.

An example of triggering an event: $event = \mod_myplugin\event\something_happened::create(array('context' => $context, 'objectid' => YYY, 'other' => ZZZ)); // ... code that may add some record snapshots $event->trigger();


Use of php autoloading

All new OOP APIs in Moodle 2.6 are going to use class auto loading - see Automatic class loading. New events use PHP strictly defined namespaces which concentrate all events classes in classes/event/ subdirectory.

Why separate classes?

There were two alternatives proposed on how to define the event structure. The first is a separate class for each event (extending the base class), the other being each event is based on a generic event instance of the base class.

Decision: Use separate class for each event.

Each plugin creates its own event class for each event

Pros

  • Maintainability - It is much easier to review, debug, integrate.
  • Self documenting, behaviour is combined with definition.
  • It is extremely flexible for plugin developers and core devs too.
  • Automatically lists events without being installed - PHPDocs as events documentation.
  • It is included only when needed using autoloading.
  • Self-validating data structure (by PHP).
  • Some developers will find it easier to copy whole class files as templates.

Cons

  • Big learning curve for developers without OOP skills (all other new subsystems in Moodle already use OOP, you can not code without these skills any more).
  • Some developers may find it harder to copy-and-paste examples because they will need to create new class first and use it afterwards (this can be viewed as a benefit because it forces developers to think more about events).


Each plugin defines events in a list based on a generic object

Pros

  • Easier for some developers (this can be eliminated with developer documentation).
  • Some developers think it gives more control of event structure in core (we can easily solve that with private access and validation in the base class).

Cons

  • It is not flexible enough. PHP code gives developers more freedom.
  • It would not be possible to implement any performance hacks in custom methods. All data would have to be calculated even if it is not used.
  • It would be necessary to define access control callbacks in other code.
  • It would be harder and slower to integrate legacy logging.
  • It would be harder and slower to implement support for legacy events.
  • Event observers could not use event class names as reliable identifiers.
  • Event object and action could not be parsed from class name, it would have to be stored in event properties every time you trigger event.
  • Requires upgrade/install to register an event in DB table with MUC cache. Events could not be triggered earlier.
  • The localised descriptions and names would have to be stored as properties, it would not be possible to store them in any definition.
  • The implementation of an events infrastructure would be significantly more complex and error prone.

Information contained in events

Events have to contain as much information as they can, but this should not affect the performance. That's why part of the information is available in properties, and the rest via methods. This allows for delaying the computation of the data at the time it is really needed, if it ever is.

Properties

List of properties that the developer has to pass to the event upon creation, or automatically generated when possible and cost free. Some of those properties not mandatory.

Property name Title Type Comment
eventname Event name static, automatic from class name Automatically computed by copying class name
component Component static, automatic from top namespace Component declaring the event, automatically computed from class name.
action Action static, automatic from last word in class name Can be automatically computed from class name.
target target of action static, automatic from class name Target on which the action is taken, can be automatically computed from class name.
objecttable Database table name optional database table name where is/was the object stored. Never use a relationship table here.
objectid Object ID optional id of the object record from objecttable
crud Transaction type static mandatory One of [crud] letters. Statically declared in the event class method init().
level / edulevel Level static mandatory Level of educational value of the event. Statically declared in the event class method init(). Changed from level to edulevel in Moodle 2.7 (See below for more details)
contextid Context ID mandatory
contextlevel Context level automatic from context This tells you if it was a course, activity, course category, etc.
contextinstanceid Context instanceid automatic from context Based on context level this may be course id , course module id, course category, etc.
userid User ID defaults to current user User ID, or 0 when not logged in, or -1 when other (System, CLI, Cron, ...)
courseid Affected course defaults to course context from context This is used only for contexts at and bellow course level - this can be used to filter events by course (includes all course activities)
relateduserid Affected user Is this action related to some user? This could be used for some personal timeline view.
other All other data Any other fields needed for event description - scalars or arrays, must be serialisable using json_encode()
timecreated Time of the event automatic
  • static: It is the same for all event instances of this class.
  • mandatory: Is required in order to trigger the event.

Level property

The edulevel property helps defining the educational value of the event. It is intentional that the list is limited to only 3 different constants, having too many options would make it harder for developers to pick the right one(s). We also have to keep in mind that this is not supposed to answer all the questions about a particular event, other event properties like the courseid, the context, the component name can be used with the level to get more granular reports.

Remember that this is event based. If the user that has triggered the event is not really "participating" because he is an admin, or a manager, then it is the job of the report to filter those. The event itself has a static educational level.

Teaching (LEVEL_TEACHING: 1)

Any event/action that is performed by someone (typically a teacher) and has a teaching value (anything that is affecting the learning experience/environment of the students). This should not be combined with "Participating" events.

Valid events:

  • A teacher grading a student
  • A teacher modifying the course settings
  • A teacher adding a new section to the course page
  • A teacher modifying a module settings
  • A teacher adding a page to course
  • A teacher leaving a feedback

INVALID events:

  • A teacher posting in a forum (it might affect the learning experience, but not necessarily, so the teacher is just participating)

Participating (LEVEL_PARTICIPATING: 2)

Any event/action that is performed by a user, that is related (or could be related) to his learning experience.

Valid events:

  • A user posting to a forum
  • A user submitting an assignment
  • A user blogging
  • A user reading someone's blog
  • A user posting a comment
  • A user chatting on a chat activity
  • A user viewing the course page
  • A user deletes a blog post

INVALID events:

  • A user updating his profile
  • A user visiting someone's profile
  • A user viewing his /my/ page
  • A user sending a message to another one

Other (LEVEL_OTHER: 0)

Any other action, whether they are related to the site administration, or are specific to user. They do not have any educational value.

Methods

The computation of this data is not required by default, but can be accessed by any event observer if need be.

Method Comment
get_name() Returns localised name of the event, it is the same for all instances.
get_description() Returns localised description of one particular event.
can_view($user) Can the specified user view the event?
get_url() Returns Moodle URL where the event can be observed afterwards.
get_legacy_eventname() Information necessary for event BC.
get_legacy_eventdata() Information necessary for event BC.
get_legacy_logdata() Information necessary for logging BC.

Record caching

The standard event data may not contain all the information observers need. The built-in record snapshot support in events allows developers to attach more auxiliary information when triggering events, it may be for example course record, some record that was just deleted, etc. The snapshot is meant to be a full database record, as it will be automatically fetched from get_record_snapshot() if not set previously and assuming the property objecttable is set. Please be aware that the snapshots are not stored in the event, and cannot be restored.

   $event = \core\event\role_assigned::create(
       array('context'=>$context, 'objectid'=>$ra->roleid, 'relateduserid'=>$ra->userid,
           'other'=>array('id'=>$ra->id, 'component'=>$ra->component, 'itemid'=>$ra->itemid)));
   $event->add_record_snapshot('role_assignments', $ra);
   $event->trigger();

   $event = \core\event\role_unassigned::create(
       array('context'=>$context, 'objectid'=>$ra->roleid, 'relateduserid'=>$ra->userid,
           'other'=>array('id'=>$ra->id, 'component'=>$ra->component, 'itemid'=>$ra->itemid)));
   $event->add_record_snapshot('role_assignments', $ra);
   $event->trigger();

The related methods are:

  • public function add_record_snapshot($tablename, $record)
  • public function get_record_snapshot($tablename, $id)

Rejected properties and methods

Property Title Why
URL relevant page URL These URLs can be constructed on the fly from other data, external log plugins may use get_url() method.
version Event specification number The event specification should never change
type Type of event (action, error, ...) Event are not intended for error logging or debugging.
actor Whether current execution is cron, cli, user, ... Impossible to track down at a low level
severity Severity following logging standards Our logging does not match this, as we will not (at present) log errors
coursecatname Category name Might be costly to retrieve for little gain
coursename Course name Might be costly to retrieve for little gain
cmname Course module name Might be costly to retrieve for little gain
categoryid Course category id Categories are a tree structure, we can not identify them by one integer. It would have to be a path.
cmid Course module id Can be derived from contextlevel and contextinstanceid
associatedobject Associated object Object associated to the main object. Ie: The user to whom a message is sent.
associatedobjectid Associated object ID Identifier of the associated object
realuserid Real User ID Will be tracked by log plugins only - user who "logged in as", stores the real user ID
origin Origin of the event Will be tracked by log plugins only - CLI, cron, Webservice, ... (optionally with IP address)


Method Comment Why
get_all_affected_users() Returns all the users affected by this event It is expensive to fetch all users and it changes in time, so it would be unreliable too. For now we store only one user who is related to each event.
get_objecturl() Returns the URL to view the object There is usually only one URL where event changes may be observed. The URL may depend on current user capabilities too.
get_associatedobjecturl() Returns the URL to view the associated object No associated user property is present.
get_currenturl() Returns the current URL, uses $PAGE. This information may be added by logger, current page info is not part of events data.
get_useripaddress() Returns the User IP address Page access method is not part of events API, this should be implemented as logdata properties in loggers.

Events naming convention

Clear event names help developers when reading what events are triggered, and defining the events properties when defining the event class.

Decision: \<component>\event\<some_object>_<verb>

Existing events

List of existing events in Moodle code base, along with their 2.5 couterparts.

Fully qualified event name 2.5 name Component Object Action (Verb) Comment
assignsubmission_comments\event\comment_created - assignsubmission_comments comment created
assignsubmission_comments\event\comment_deleted - assignsubmission_comments comment deleted
assignsubmission_file\event\assessable_uploaded assessable_file_uploaded assignsubmission_file assessable uploaded To be deprecated MDL-35197
assignsubmission_file\event\submission_created - assignsubmission_file submission created
assignsubmission_file\event\submission_updated - assignsubmission_file submission updated
assignsubmission_onlinetext\event\assessable_uploaded assessable_content_uploaded assignsubmission_onlinetext assessable uploaded
assignsubmission_onlinetext\event\submission_created - assignsubmission_onlinetext submission created
assignsubmission_onlinetext\event\submission_updated - assignsubmission_onlinetext submission updated
block_comments\event\comment_created - block_comments comment created
block_comments\event\comment_deleted - block_comments comment deleted
booktool_exportimscp\event\book_exported - booktool_exportimscp book exported
booktool_print\event\book_printed - booktool_print book printed
booktool_print\event\chapter_printed - booktool_print chapter printed
core\event\assessable_submitted - core assessable submitted Abstract class
core\event\assessable_uploaded - core assessable uploaded Abstract class
core\event\base - core base ase Abstract class
core\event\blog_association_created - core blog_association created
core\event\blog_comment_created - core blog_comment created
core\event\blog_comment_deleted - core blog_comment deleted
core\event\blog_entries_viewed - core blog_entries viewed
core\event\blog_entry_created blog_entry_added core blog_entry created
core\event\blog_entry_deleted blog_entry_deleted core blog_entry deleted
core\event\blog_entry_updated blog_entry_edited core blog_entry updated
core\event\cohort_created cohort_added core cohort created
core\event\cohort_deleted cohort_deleted core cohort deleted
core\event\cohort_member_added cohort_member_added core cohort_member added
core\event\cohort_member_removed cohort_member_removed core cohort_member removed
core\event\cohort_updated cohort_updated core cohort updated
core\event\comment_created - core comment created Abstract class
core\event\comment_deleted - core comment deleted Abstract class
core\event\comments_viewed - core comments viewed Abstract class
core\event\content_viewed - core content viewed Abstract class
core\event\course_category_created - core course_category created
core\event\course_category_deleted course_category_deleted core course_category deleted
core\event\course_category_updated - core course_category updated
core\event\course_completed course_completed core course completed
core\event\course_completion_updated - core course_completion updated
core\event\course_content_deleted course_content_removed core course_content deleted
core\event\course_created course_created core course created
core\event\course_deleted course_deleted core course deleted
core\event\course_module_completion_updated activity_completion_changed core course_module_completion updated
core\event\course_module_created mod_created core course_module created
core\event\course_module_deleted mod_deleted core course_module deleted
core\event\course_module_instance_list_viewed - core course_module_instance_list viewed Abstract class
core\event\course_module_instances_list_viewed - core course_module_instances_list viewed Abstract class
core\event\course_module_updated mod_updated core course_module updated
core\event\course_module_viewed - core course_module viewed Abstract class
core\event\course_reset_ended - core course_reset ended
core\event\course_reset_started - core course_reset started
core\event\course_restored course_restored core course restored
core\event\course_section_updated - core course_section updated
core\event\course_updated course_updated core course updated
core\event\email_failed - core email failed
core\event\group_created groups_group_created core group created
core\event\group_deleted groups_group_deleted core group deleted
core\event\group_member_added groups_member_added core group_member added
core\event\group_member_removed groups_member_removed core group_member removed
core\event\group_updated groups_group_updated core group updated
core\event\grouping_created groups_grouping_created core grouping created
core\event\grouping_deleted groups_grouping_deleted core grouping deleted
core\event\grouping_updated groups_grouping_updated core grouping updated
core\event\manager - core manager anager
core\event\mnet_access_control_created - core mnet_access_control created
core\event\mnet_access_control_updated - core mnet_access_control updated
core\event\note_created - core note created
core\event\note_deleted - core note deleted
core\event\note_updated - core note updated
core\event\notes_viewed - core notes viewed
core\event\role_allow_assign_updated - core role_allow_assign updated
core\event\role_allow_override_updated - core role_allow_override updated
core\event\role_allow_switch_updated - core role_allow_switch updated
core\event\role_assigned role_assigned core role assigned
core\event\role_capabilities_updated - core role_capabilities updated
core\event\role_deleted - core role deleted
core\event\role_unassigned role_unassigned core role unassigned
core\event\user_created user_created core user created
core\event\user_deleted user_deleted core user deleted
core\event\user_enrolment_created user_enrolled core user_enrolment created
core\event\user_enrolment_deleted user_unenrolled core user_enrolment deleted
core\event\user_enrolment_updated user_enrol_modified core user_enrolment updated
core\event\user_list_viewed - core user_list viewed
core\event\user_loggedin - core user loggedin
core\event\user_loggedinas - core user loggedinas
core\event\user_loggedout user_logout core user loggedout
core\event\user_login_failed - core user_login failed
core\event\user_profile_viewed - core user_profile viewed
core\event\user_updated user_updated core user updated
core\event\webservice_function_called - core webservice_function called
core\event\webservice_login_failed - core webservice_login failed
core\event\webservice_service_created - core webservice_service created
core\event\webservice_service_deleted - core webservice_service deleted
core\event\webservice_service_updated - core webservice_service updated
core\event\webservice_service_user_added - core webservice_service_user added
core\event\webservice_service_user_removed - core webservice_service_user removed
core\event\webservice_token_created - core webservice_token created
core\event\webservice_token_sent - core webservice_token sent
logstore_legacy\event\legacy_logged - logstore_legacy legacy logged
mod_assign\event\all_submissions_downloaded - mod_assign all_submissions downloaded
mod_assign\event\assessable_submitted assessable_submitted mod_assign assessable submitted
mod_assign\event\extension_granted - mod_assign extension granted
mod_assign\event\identities_revealed - mod_assign identities revealed
mod_assign\event\marker_updated - mod_assign marker updated
mod_assign\event\statement_accepted - mod_assign statement accepted
mod_assign\event\submission_created - mod_assign submission created Abstract class
mod_assign\event\submission_duplicated - mod_assign submission duplicated
mod_assign\event\submission_graded - mod_assign submission graded
mod_assign\event\submission_locked - mod_assign submission locked
mod_assign\event\submission_status_updated - mod_assign submission_status updated
mod_assign\event\submission_unlocked - mod_assign submission unlocked
mod_assign\event\submission_updated - mod_assign submission updated Abstract class
mod_assign\event\workflow_state_updated - mod_assign workflow_state updated
mod_book\event\chapter_created - mod_book chapter created
mod_book\event\chapter_deleted - mod_book chapter deleted
mod_book\event\chapter_updated - mod_book chapter updated
mod_book\event\chapter_viewed - mod_book chapter viewed
mod_book\event\course_module_instance_list_viewed - mod_book course_module_instance_list viewed
mod_book\event\course_module_viewed - mod_book course_module viewed
mod_chat\event\course_module_instance_list_viewed - mod_chat course_module_instance_list viewed
mod_chat\event\message_sent - mod_chat message sent
mod_chat\event\sessions_viewed - mod_chat sessions viewed
mod_choice\event\answer_submitted - mod_choice answer submitted
mod_choice\event\answer_updated - mod_choice answer updated
mod_choice\event\course_module_instance_list_viewed - mod_choice course_module_instance_list viewed
mod_choice\event\course_module_viewed - mod_choice course_module viewed
mod_choice\event\report_viewed - mod_choice report viewed
mod_data\event\comment_created - mod_data comment created
mod_data\event\comment_deleted - mod_data comment deleted
mod_data\event\course_module_instance_list_viewed - mod_data course_module_instance_list viewed
mod_data\event\course_module_viewed - mod_data course_module viewed
mod_data\event\field_created - mod_data field created
mod_data\event\field_deleted - mod_data field deleted
mod_data\event\field_updated - mod_data field updated
mod_data\event\record_created - mod_data record created
mod_data\event\record_deleted - mod_data record deleted
mod_data\event\record_updated - mod_data record updated
mod_data\event\template_updated - mod_data template updated
mod_data\event\template_viewed - mod_data template viewed
mod_feedback\event\course_module_instance_list_viewed - mod_feedback course_module_instance_list viewed
mod_feedback\event\course_module_viewed - mod_feedback course_module viewed
mod_feedback\event\response_deleted - mod_feedback response deleted
mod_feedback\event\response_submitted - mod_feedback response submitted
mod_folder\event\course_module_instance_list_viewed - mod_folder course_module_instance_list viewed
mod_folder\event\course_module_viewed - mod_folder course_module viewed
mod_folder\event\folder_updated - mod_folder folder updated
mod_forum\event\assessable_uploaded assessable_content_uploaded mod_forum assessable uploaded
mod_forum\event\course_module_instance_list_viewed - mod_forum course_module_instance_list viewed
mod_forum\event\course_searched - mod_forum course searched
mod_forum\event\discussion_created - mod_forum discussion created
mod_forum\event\discussion_deleted - mod_forum discussion deleted
mod_forum\event\discussion_moved - mod_forum discussion moved
mod_forum\event\discussion_updated - mod_forum discussion updated
mod_forum\event\discussion_viewed - mod_forum discussion viewed
mod_forum\event\forum_viewed - mod_forum forum viewed
mod_forum\event\post_created - mod_forum post created
mod_forum\event\post_deleted - mod_forum post deleted
mod_forum\event\post_updated - mod_forum post updated
mod_forum\event\readtracking_disabled - mod_forum readtracking disabled
mod_forum\event\readtracking_enabled - mod_forum readtracking enabled
mod_forum\event\subscribers_viewed - mod_forum subscribers viewed
mod_forum\event\subscription_created - mod_forum subscription created
mod_forum\event\subscription_deleted - mod_forum subscription deleted
mod_forum\event\userreport_viewed - mod_forum userreport viewed
mod_glossary\event\comment_created - mod_glossary comment created
mod_glossary\event\comment_deleted - mod_glossary comment deleted
mod_lesson\event\course_module_instance_list_viewed - mod_lesson course_module_instance_list viewed
mod_lesson\event\course_module_viewed - mod_lesson course_module viewed
mod_lesson\event\essay_assessed - mod_lesson essay assessed
mod_lesson\event\essay_attempt_viewed - mod_lesson essay_attempt viewed
mod_lesson\event\highscore_added - mod_lesson highscore added
mod_lesson\event\highscores_viewed - mod_lesson highscores viewed
mod_lesson\event\lesson_ended - mod_lesson lesson ended
mod_lesson\event\lesson_started - mod_lesson lesson started
mod_lti\event\course_module_instance_list_viewed - mod_lti course_module_instance_list viewed
mod_lti\event\course_module_viewed - mod_lti course_module viewed
mod_lti\event\unknown_service_api_called lti_unknown_service_api_call mod_lti unknown_service_api called
mod_page\event\course_module_instance_list_viewed - mod_page course_module_instance_list viewed
mod_page\event\course_module_viewed - mod_page course_module viewed
mod_quiz\event\attempt_abandoned quiz_attempt_abandoned mod_quiz attempt abandoned
mod_quiz\event\attempt_becameoverdue quiz_attempt_overdue mod_quiz attempt becameoverdue
mod_quiz\event\attempt_started quiz_attempt_started mod_quiz attempt started
mod_quiz\event\attempt_submitted quiz_attempt_submitted mod_quiz attempt submitted
mod_resource\event\course_module_instance_list_viewed - mod_resource course_module_instance_list viewed
mod_resource\event\course_module_viewed - mod_resource course_module viewed
mod_scorm\event\attempt_deleted - mod_scorm attempt deleted
mod_scorm\event\course_module_instance_list_viewed - mod_scorm course_module_instance_list viewed
mod_scorm\event\course_module_viewed - mod_scorm course_module viewed
mod_scorm\event\interactions_viewed - mod_scorm interactions viewed
mod_scorm\event\report_viewed - mod_scorm report viewed
mod_scorm\event\sco_launched - mod_scorm sco launched
mod_scorm\event\tracks_viewed - mod_scorm tracks viewed
mod_scorm\event\user_report_viewed - mod_scorm user_report viewed
mod_url\event\course_module_instance_list_viewed - mod_url course_module_instance_list viewed
mod_url\event\course_module_viewed - mod_url course_module viewed
mod_wiki\event\comment_created - mod_wiki comment created
mod_wiki\event\comment_deleted - mod_wiki comment deleted
mod_wiki\event\comments_viewed - mod_wiki comments viewed
mod_wiki\event\course_module_instance_list_viewed - mod_wiki course_module_instance_list viewed
mod_wiki\event\course_module_viewed - mod_wiki course_module viewed
mod_wiki\event\page_created - mod_wiki page created
mod_wiki\event\page_deleted - mod_wiki page deleted
mod_wiki\event\page_diff_viewed - mod_wiki page_diff viewed
mod_wiki\event\page_history_viewed - mod_wiki page_history viewed
mod_wiki\event\page_locks_deleted - mod_wiki page_locks deleted
mod_wiki\event\page_map_viewed - mod_wiki page_map viewed
mod_wiki\event\page_updated - mod_wiki page updated
mod_wiki\event\page_version_deleted - mod_wiki page_version deleted
mod_wiki\event\page_version_restored - mod_wiki page_version restored
mod_wiki\event\page_version_viewed - mod_wiki page_version viewed
mod_wiki\event\page_viewed - mod_wiki page viewed
mod_workshop\event\assessable_uploaded assessable_content_uploaded mod_workshop assessable uploaded
mod_workshop\event\assessment_evaluated - mod_workshop assessment evaluated
mod_workshop\event\assessment_evaluations_reset - mod_workshop assessment_evaluations reset
mod_workshop\event\assessment_reevaluated - mod_workshop assessment reevaluated
mod_workshop\event\course_module_viewed workshop_viewed mod_workshop course_module viewed
mod_workshop\event\instances_list_viewed - mod_workshop instances_list viewed
mod_workshop\event\phase_switched - mod_workshop phase switched
mod_workshop\event\submission_assessed - mod_workshop submission assessed
mod_workshop\event\submission_created - mod_workshop submission created
mod_workshop\event\submission_reassessed - mod_workshop submission reassessed
mod_workshop\event\submission_updated - mod_workshop submission updated
mod_workshop\event\submission_viewed - mod_workshop submission viewed
report_log\event\content_viewed - report_log content viewed
report_loglive\event\content_viewed - report_loglive content viewed
report_outline\event\content_viewed - report_outline content viewed
report_participation\event\content_viewed - report_participation content viewed
report_stats\event\content_viewed - report_stats content viewed

Verb list

All events must use a verb from this list. New verbs should be added to this list if required.

verb Explanation Source
abandoned When a attempt is abandoned by user (Quiz attempt) Moodle
accepted Example: Accepting a statement when submitting an assignment. Moodle
added Used to represent "something that already exists is now part of/bound to another entity". Examples: "Admin added role to user X", "Admin added user X to group A". Wrong example: "User added course in category" because it is a 'move' action, except if a course can be part of multiple categories. The good examples work because: A user can have multiple roles, a user can be in multiple groups. Moodle
answered Indicates the actor responded to a Question Tincan
assessed Some submitted material has been assessed Moodle
assigned Assign some privilege or role to user. Moodle
attempted Trying to do an activity. Example: attempting a Math class. Tincan
awarded ex:-teacher awarded student a badge. Moodle
backedup When a backup has been performed. Moodle
becomeoverdue When an activity is overdue Example: Quiz attempt is overdue Moodle
called When a call to something is made like an API @see unknown_service_api_called.php Moodle
commented Offered an opinion or written experience of the activity. Can be used with the learner as the actor or a system as an actor. Comments can be sent from either party with the idea that the other will read and react to the content. Tincan
completed To experience the activity in its entirety. Used to affirm the completion of content. This can be simply experiencing all the content, be tied to objectives or interactions, or determined in any other way. Any content that has been initialized, but not yet completed, should be considered incomplete. There is no verb to 'incomplete' an activity, one would void the statement which completes the activity." Tincan
created Used to represent "something new has been created". Moodle
deleted Used to indicate the object in context was deleted. Moodle
disabled When an activity is disabled. Example: forum read tracking disabled. Moodle
downloaded When a user download file from user. Example submission/report downloaded. Moodle
duplicated For something that has been copied. Moodle
enabled When some setting is enabled. Example: forum read tracking enabled. Moodle
ended When a process ends. Example: Lesson ended or course reset ended. Moodle
evaluated Material has been evaluated. Moodle
exported When a report is exported in certain format. Moodle
failed Learner did not perform the activity to a level of pre-determined satisfaction. Used to affirm the lack of success a learner experienced within the learning content in relation to a threshold. If the user performed below the minimum to the level of this threshold, the content is 'failed'. The opposite of 'passed'. This is also used in case when message sending is failed. Tincan
graded Used to represent an activity was graded. Moodle
granted User is granted some extension or capability. Example: extension granted for submission. Moodle
imported The act of moving an object into another location or system. Tincan
launched When an external object is launched. Try consider started if there is related stopped event. Moodle
locked When an activity is locked. Should have a related unlocked event. Moodle
loggedin/loggedout For login and logout. Moodle
loggedinas If user is logged in as different user. This is used by only one event (user_loggedinas). Adding this verb makes event name more clear, then using loggedin verb. Moodle
locked Moodle
moved Used to indicate the object in context was moved. Moodle
passed Used to affirm the success a learner experienced within the learning content in relation to a threshold. If the user performed at a minimum to the level of this threshold, the content is 'passed'. The opposite of 'failed'. Tincan
printed Something is printed. Moodle
reassessed Submitted material has been assessed again. Moodle
reevaluated Material has been evaluated again. Moodle
removed By opposition to "Added". This does not mean that the object has been deleted, but removed from the entity, or not bound to it any more. Moodle
reset Sets one or more properties back to the default value. Moodle
restored When restoring a backup. Rolling back to a previous state. Moodle
revealed Some identity is revealed. Example: Identities revealed after blind marking. Moodle
searched Something is searched. Example: searched in course. Moodle
sent Message sent. Moodle
started Some activity started Moodle
submitted This is very close to "Attempted". Depends on context which one should be used. For example:- "Admin submitted a form. Student attempted a quiz." is correct, however some cases might not be as clear as the previous example. We can say both "Student submitted an assignment" or "student attempted an assignment". We need to make the difference clear. Moodle
suspended Suspend something. (example a user) Tincan (However the context is different)
switched Something has been switched. For example:- The workshop phase has been switched to assessment" Moodle
unassigned As opposed to assigned. When some role is unassigned. Moodle
unlocked Moodle
upgraded Something was upgraded, some module probably Moodle
updated Used to indicate the object in context was updated. Simple example is "Admin updated course xyz". Moodle
uploaded When an assignment is uploaded. Moodle
viewed Something has been viewed. For example:- "Student viewed chapter 1 of book 1." Moodle

Rules

Use singular

Plurals must be used on objects when it's a One to Many relationship. Ex: bulk import, mass deletion, ... In any other case, use the singular.

Ends with a verb

The last word (after the last underscore) must be a verb.

Deprecated events

Following are the events that were supported in 2.5, but deprecated in 2.6 or later

groups_groupings_deleted (see MDL-41312)

groups_groupings_groups_removed (see MDL-41312)

groups_groups_deleted (see MDL-41312)

groups_members_removed (see MDL-41312)

Shared events

Decision: Not supported at this stage.

In Moodle 2.5 we have a good example of a shared event: 'assessable_content_uploaded' which is triggered in forum, 'assignment and workshop.

The problem with shared events is that we cannot easily track what component triggered them. Of course we could add a new property to the event to keep track of that, but we would soon need more information and more properties. Also, in the case of a logger, the event received would be unique, where in fact it should be considered different depending on the component firing it.

In our first implementation, we will create one specific event per module. This flexibility does not prevent any observer from capturing them, but still makes sure that the consistency and specificity of each event is maintained.

It could happen that some events are defined in core and shared, but this should not really happen as low-level APIs should trigger the event, and a module should call that low API instead of doing the job itself.

One to many

Decision: Each event should have a one to one relationship. We can reconsider this at a later stage, if the performance hit is extremely high.

In 2.5, some events are triggered when an action happens on multiple objects. We have to decide whether we want to keep supporting One to Many events or not.

Keeping a list of all changes for multiple actions may be problematic because you would have to keep them all in memory until all things are processed. This might also result in the order of events being incorrect. The only correct solution seems to be to trigger each item individually and then many things at the end. Performance needs to be improved elsewhere...

Accuracy

When uploading a bunch of users using the CSV upload feature, if only one event is triggered, it means that the observers of user_created won't be triggered. And so some functionality can be lost as, as a plugin developer, I expect this user_created to be triggered regardless of the way they have been uploaded. Of course, the developer could observe the event bulk_user_imported, but that means that he could miss some relevant observers.

This applies to existing events.

Performance

Triggering one event is cheaper then repeating the same events x number of times...

Information tracking

A bulk event, might not be verbose enough to allow for proper logging afterwards. Though this is the responsibility of the logger, we probably want to make it easy to store relevant information.

Double event

In the case of a bulk user import, if we were to trigger an event per user created, we probably want to trigger one event 'user_bulk_upload_started' when the action starts.

Unit Testing

With unit testing for this system we want to assert the following:

  • That event strict validation and custom validation works.
  • Missing event data is auto filled with accurate data.
  • Typos in properties passed to ::create() are captured (if we decide to validate).
  • The legacy methods return the expected values (use assertEventLegacyData() and assertEventLegacyData())
  • The class properties are correctly overridden (crud, level, action, object, ...).
  • The properties automatically generated (component, name, ...) are correct.
  • Events are dispatched to the corresponding observers.
  • Events are dispatched to the corresponding legacy handlers.
  • Events are dispatched to the * observers.
  • Events perform an add_to_log() if it has legacy log data.
  • 'Events restore' restored the whole event data, and does not miss any information.
  • 'Events restore' does not generate any extra information.
  • Event methods should check context object or avoid using it, as context might not be valid at time of event restore (use assertEventContextNotUsed())

Example events

Assignment

Assumption: Course contains groups with students in each group.

1. Teacher creates an assignment with group mode set to 'Separate groups' and Feedback type set to comments and files.

  • Event: User 'Teacher' has created assignment 'B' in course 'C101'.

2. A student views the assignment.

  • Event: User 'Student' has viewed assignment 'B' in course 'C101'.

3. A member from one of the groups submits an assignment

  • Event: User 'Student' has added a submission for assignment 'B' for group 'C' in course 'C101'.
  • Event: Email sent to the teacher and all students in that group.

4. User 'Adrian' adds some changes to the assignment and updates it.

  • Event: User 'Adrian' has updated the submission for assignment 'B' for group 'C' in course 'C101'.
  • Event: Email sent to the teacher and all students in that group.

5. Teacher views the assignment.

  • Event: User 'Teacher' has viewed assignment 'B' in course 'C101'.

6. Teacher clicks on 'View/grade all submissions'

  • Event: User 'Teacher' has viewed the assignment 'B' grade area in course 'C101'.

7. Teacher clicks to grade the student's submission.

  • Event: User 'Teacher' has viewed the submission for user 'student' for assignment 'B' in course 'C101'.

8. Teacher marks the assignment with the setting 'Apply grades and feedback to entire group' set to 'Yes' leaving a comment and a file.

  • Event: User 'Teacher' has marked assignment 'B' for group 'C' in course 'C101'.
  • Event: User 'Teacher' has left a comment for assignment 'B' for group 'C' in course 'C101'.
  • Event: User 'Teacher' has uploaded a feedback file for assignment 'B' for group 'C' in course 'C101'.
  • Event: User 'Teacher' has uploaded a file to the course 'C101'.
  • Event: Email sent to user 'Student' notifying them their submission for assignment 'B' has been marked. - This is done for all users in the group.

9. User 'Adrian' views the feedback.

  • Event: User 'Adrian' has viewed assignment 'B' in course 'C101'.

10. User 'Adrian' opens the feedback file.

  • Event: User 'Adrian' has viewed the file 'A' for assignment 'B' in course 'C101'.

11. User 'Adrian' adds some changes to the assignment insulting the teachers marking and updates it.

  • Event: User 'Adrian' has updated the submission for assignment 'B' for group 'C' in course 'C101'.
  • Event: Email sent to user 'Teacher' notifying them that user 'Adrian' has updated the submission for assignment 'B'.
  • Event: Email sent to user 'Student' notifying them their submission for assignment 'B' has been updated. - This is done for all users in the group.

12. The teacher clicks directly on the link in the email to be taken to the grading page.

  • Event: User 'Teacher' has viewed the submission for user 'student' for assignment 'B' in course 'C101'.

13. The teacher is upset due to the harsh comments and decides to mark Adrian down, but not the rest of the group by setting 'Apply grades and feedback to entire group' set to 'No'.

  • Event: User 'Teacher' has marked assignment 'B' for user 'Adrian' in course 'C101'.
  • Event: User 'Teacher' has left a comment for assignment 'B' for user 'Adrian' in course 'C101'.
  • Event: User 'Teacher' has uploaded a feedback file for assignment 'B' for user 'Adrian' in course 'C101'.
  • Event: Email sent to user 'Adrian' notifying them their submission for assignment 'B' has been marked.

FAQs

Why not use create events in core subsystems?
Because we could not see all core events in one place, it would create problems when naming event classes and finally subsystems are incomplete, we would have to add more now because we could not move the events in the future.

Development stages

Stage 1

  • Finish class loader spec and implement basic Frankenstyle class loader.
  • Describe new observer definition - just few new flags in current db/events.php
  • Describe new event dispatcher.
  • Describe core_event_base class.
  • Current (= legacy) events triggering:
    • Re-factor current event handling code to new self-contained class - do not change functionality, keep events_trigger().
    • Create new event handler management class that deals with installation and upgrades of both legacy and new handlers.
  • New events:
    • Create new core_event_base class.
    • Create new self-contained event dispatcher class with '*' handler support.
    • In function core_event_base::trigger() check if the event has property 'legacyeventname' execute events_trigger($this->legacyeventname, $this->legacyeventdata) after triggering new event.
    • Write unit tests for all new events code.
  • No changes to be made to the current logging system yet.

After completing this stage everything should continue to work as it did before and we can start parallel work on further stages.

Stage 2 (requires completion of Stage 1)

  • Create event classes and replace existing calls to events_trigger() and with new event classes containing legacy information properties.
  • Add more events throughout the standard Moodle package in places where we have add_to_log(). Implement some_event::get_legacy_log_data() which returns original parameters of add_to_log() and remove it. Old add_to_log() function is called in core_event_base::trigger() automatically with original parameters.
  • Add even more new events all over the place.

The difficult part is defining the new event classes properly because we must not change them after the 2.6 release.

Stage 3 (requires partial completion of Stage 2)

  • Migrate current legacy event handlers to new handlers with one event class instance parameter, ex.: enrol plugins.

Stage 4 (requires partial completion of Stage 2)

  • Implement an event logging handler.
  • Implement logging storage plugins.
  • Define logging apis.
  • Create new reports.
  • Switch to new logging everywhere after Stage 2 has been completed and new reports are usable.

See Logging 2

Stage 5

  • Decide how much backwards compatibility we want for old log tables. Most probably they will get only legacy log data.
  • Implement some BC solution for old code that reads log tables directly.

See Logging 2

Stage 6 (requires completion of Stage 4 and 5)

Moodle 2.8dev? This is the ultimate end of old logging via the log table.

  • Deprecate the add_to_log() function with a debug message and do nothing inside.
  • Remove all legacy logging from event classes.

See Logging 2