Note:

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

Migrating logging calls in plugins

From MoodleDocs
Revision as of 06:29, 21 February 2014 by Marina Glancy (talk | contribs) (Created page with "This document is aimed to assist developers in replacing existing '''add_to_log()''' calls in the plugin with the events. This '''can''' be implemented in Moodle 2.6 and this ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This document is aimed to assist developers in replacing existing add_to_log() calls in the plugin with the events. This can be implemented in Moodle 2.6 and this must be done in 2.7.

As a quick reminder: Events 2 was introduced in Moodle 2.6, new Logging system is being introduced in Moodle 2.7. Function *add_to_log()* becomes deprecated. Log storage that uses the DB table *log* is now called "legacy". When replacing the add_to_log() with triggering of an event developer also must ensure that he also generates an entry to the legacy log. It will only be used if legacy log is enabled since it may be enabled on systems that use too many custom reports relying on presence of log table and it would take time to migrate the reports.

Quick start

If you are replacing common add_to_log() calls such as "view" and "view all" in mod/XXX/view.php and mod/XXX/index.php, see below. Otherwise do the following:

1. Choose a name for event. It should have syntax OBJECT_VERB, for example "entry_added", "content_viewed", etc. It should not have plugin name in it because it is obvious from namespace. See Events 2 for more details on events names.

2. Create an event class in YOURPLUGINDIR/classes/event/SOMETHING_HAPPENED.php , for example

namespace FULLPLUGINNAME\event;
class SOMETHING_HAPPENED extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c'; // c(reate), r(ead), u(pdate), d(elete)
        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
        $this->data['objecttable'] = '...';
    }

    public static function get_name() {
        return get_string('eventSOMETHINGHAPPENED', 'FULLPLUGINNAME');
    }

    public function get_description() {
        return "User {$this->userid} has ... ... ... with id {$this->objectid}.";
    }

    public function get_url() {
        return new \moodle_url('....', array());
    }

    public function get_legacy_log_data() {
        return array($this->courseid, 'PLUGINNAME', 'LOGACTION',
            '...........',
            $this->objectid, $this->contextinstanceid);
    }
}

3. Define language string for the event name in YOURPLUGINDIR/lang/en/FULLPLUGINNAME.php :

$string['eventSOMETHINGHAPPENED] = 'SOMETHING has HAPPENED';

4. Replace add_to_log() with triggering of event. This is a common example for event inside an activity module:

add_to_log($course->id, 'PLUGINNAME', 'LOGACTION', '...........', $objid, $cmid);
with:
$event = \FULLPLUGINNAME\event\SOMETHING_HAPPENED::create(array(
    'objectid' => $objid,
    'context' => context_module::instance($cmid)
));
$event->trigger();

Replacing 'view' and 'view all' events in the modules

Information stored in event

Function init()

Function add_record_snapshot()