<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dness</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dness"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Dness"/>
	<updated>2026-11-27T21:02:33Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Using_images_in_a_theme&amp;diff=54512</id>
		<title>Using images in a theme</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Using_images_in_a_theme&amp;diff=54512"/>
		<updated>2018-07-10T14:01:53Z</updated>

		<summary type="html">&lt;p&gt;Dness: /* A little background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Themes}}&lt;br /&gt;
==A little background==&lt;br /&gt;
Moodle has a standard way of making use of images within its pages enabling theme designers to take full control over what images are being used and when possible ensures images are passed through Moodle&#039;s performance caching system to obtain the best possible performance.&lt;br /&gt;
&lt;br /&gt;
==Before we start==&lt;br /&gt;
&lt;br /&gt;
# Make sure you have a theme and images to work with, one that you have a backup of just in case.&lt;br /&gt;
# Take note of your theme&#039;s main layout file e.g. &#039;&#039;standard.php&#039;&#039;.&lt;br /&gt;
# Take note of your theme&#039;s main CSS file e.g. &#039;&#039;core.css&#039;&#039;.&lt;br /&gt;
# Turn on &#039;&#039;&#039;Theme designer mode&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
==Image locations within Moodle==&lt;br /&gt;
&lt;br /&gt;
There are three main areas in which images are located - core, plugin and theme.&lt;br /&gt;
&lt;br /&gt;
Core images are used throughout Moodle and are stored in &#039;&#039;moodle/pix&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The pix directory contains the following subdirectories:&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;a&#039;&#039; - Icons that are not widely used&lt;br /&gt;
:&#039;&#039;c&#039;&#039; - Calendar-related icons&lt;br /&gt;
:&amp;quot;e&amp;quot; - Editor-related icons&lt;br /&gt;
:&#039;&#039;f&#039;&#039; - File icons for different file types&lt;br /&gt;
:&#039;&#039;g&#039;&#039; - Default user icons and thumbnails&lt;br /&gt;
:&#039;&#039;i&#039;&#039; - General icons&lt;br /&gt;
:&#039;&#039;m&#039;&#039; - Currency symbols&lt;br /&gt;
:&#039;&#039;s&#039;&#039; - Smileys&lt;br /&gt;
:&#039;&#039;t&#039;&#039; - General icons&lt;br /&gt;
:&#039;&#039;u&#039;&#039; - User icons and thumbnails&lt;br /&gt;
:&#039;&#039;y&#039;&#039; - YUI icons&lt;br /&gt;
&lt;br /&gt;
Plugin images are used by plugins and are stored within the plugin&#039;s directory e.g. &#039;&#039;mod/forum/*&#039;&#039; or &#039;&#039;blocks/navigation/*&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Theme images are used in themes and are stored in the &#039;&#039;pix&#039;&#039; subdirectory of a theme.&lt;br /&gt;
&lt;br /&gt;
==Adding new images to your theme==&lt;br /&gt;
&lt;br /&gt;
To add new images to your theme, such as a background image or a logo, you should start by creating a pix directory within your themes directory.&lt;br /&gt;
&lt;br /&gt;
Lets assume you have two images, gradient.png and logo.jpg. Copy both files to your themes pix directory.&lt;br /&gt;
&lt;br /&gt;
The plan is to use gradient.png as the background image for your theme (in CSS) and use logo.jpg in the header (in your theme&#039;s layout file).&lt;br /&gt;
&lt;br /&gt;
===Using images within CSS===&lt;br /&gt;
&lt;br /&gt;
Moodle parses all CSS files. When theme designer mode is off, Moodle combines them into a handful of large CSS files that get cached and served. At the same time Moodle also looks at the CSS and replaces special syntax rules. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&#039;padding:10px;background-color:#f7f7f7;border:1px dashed #123456&#039;&amp;gt;&amp;lt;nowiki&amp;gt;[[pix:theme|&amp;lt;/nowiki&amp;gt;&amp;lt;span style=&#039;color:#336699;font-weight:bold;&#039;&amp;gt;&amp;lt;nowiki&amp;gt;path/to/image/&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;span style=&#039;color:#33993A;font-weight:bold;&#039;&amp;gt;imagename&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;]]&amp;lt;/nowiki&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above pattern is looked for by Moodle and gets replaced with the correct image URL when found.&lt;br /&gt;
You should note the following things about this pattern when using your own images within CSS:&lt;br /&gt;
&lt;br /&gt;
# The bits in black don&#039;t change&lt;br /&gt;
# The bit in blue is the path to your image within the pix directory. It shouldn&#039;t start with a / but should end with one.&lt;br /&gt;
# The bit in green is the filename (&#039;&#039;&#039;no need to include the file extension&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
To use gradient.png as the background for our pages, add the following line of CSS to the themes core.css.&lt;br /&gt;
&amp;lt;code css&amp;gt;&lt;br /&gt;
body {background-image:url([[pix:theme|gradient]]);}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before we look at how to use your own images within your themes layout files we should first look at how this changes if the image is in a sub directory.&lt;br /&gt;
&lt;br /&gt;
Lets assume gradient.png is located here: &#039;&#039;&#039;pix/myimages/gradients/gradient.png&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The CSS would need to be as follows:&lt;br /&gt;
&amp;lt;code css&amp;gt;&lt;br /&gt;
body {background-image:url([[pix:theme|myimages/gradients/gradient]]);}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to refer to an image belonging to a plugin, you can do it like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code css&amp;gt;&lt;br /&gt;
body {background-image:url([[pix:quiz|icon]]);}&lt;br /&gt;
body {background-image:url([[pix:qtype_ddmarker|grid]]);}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That refers to mod/quiz/pix/icon.png/gif and question/type/ddmarker/pix/grid.png/gif respectively (or whatever icons you have put in your theme to override these).&lt;br /&gt;
&lt;br /&gt;
===Using your images within your layout files===&lt;br /&gt;
&lt;br /&gt;
To use logo.jpg within the a layout php file or layout mustache template you need to add the url for the image to the template context and then include an image tag in the template using the source from the context.&lt;br /&gt;
&lt;br /&gt;
Images:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
/pix/logo.jpg&lt;br /&gt;
/pix/myimages/logos/logo.png&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;theme/yourtheme/layout/somelayout.php&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
$templatecontext = [&lt;br /&gt;
...&lt;br /&gt;
&#039;imageone&#039; =&amp;gt; $OUTPUT-&amp;gt;image_url(&#039;logo&#039;, &#039;theme&#039;),&lt;br /&gt;
&#039;imagetwo&#039; =&amp;gt; $OUTPUT-&amp;gt;image_url(&#039;myimages/logos/logo&#039;, &#039;theme&#039;),&lt;br /&gt;
...&lt;br /&gt;
];&lt;br /&gt;
&lt;br /&gt;
echo $OUTPUT-&amp;gt;render_from_template(&#039;theme_yourtheme/somelayout&#039;, $templatecontext);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;theme/yourtheme/templates/somelayout.mustache&#039;&#039;&lt;br /&gt;
&amp;lt;code handlebars&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;img src=&amp;quot;{{{imageone}}}&amp;quot; alt=&amp;quot;Please give your image alt text or set the role to presentation&amp;quot; width=&amp;quot;50&amp;quot; height=&amp;quot;50&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;img src=&amp;quot;{{{imagetwo}}}&amp;quot; alt=&amp;quot;Please give your image alt text or set the role to presentation&amp;quot; width=&amp;quot;50&amp;quot; height=&amp;quot;50&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Overriding images in your theme==&lt;br /&gt;
&lt;br /&gt;
Overriding images within your theme can be an important part of creating and/or modifying themes.&lt;br /&gt;
&lt;br /&gt;
As mentioned, there are three main areas for images within Moodle, core, plugins, and themes. Obviously we won&#039;t be overriding theme images as we are in a theme and have full control over that already which just leaves core and plugin images that you may want to override.&lt;br /&gt;
&lt;br /&gt;
Within your theme create the following two directories:&lt;br /&gt;
; /pix_core/ : This is where your images to override core images will need to be.&lt;br /&gt;
; /pix_plugins/ : This is where images to override plugins will need to be.&lt;br /&gt;
&lt;br /&gt;
Next, copy the images that you wish to override into either the pix_core or pix_plugins directory. You need to replicate the directory structure that the images are located in.&lt;br /&gt;
&lt;br /&gt;
The following two examples illustrate how this works for both core and plugin images.&lt;br /&gt;
&lt;br /&gt;
===Overriding core images===&lt;br /&gt;
For this example I am going to override the following two images:&lt;br /&gt;
# &#039;&#039;&#039;moodle/pix/help.gif&#039;&#039;&#039; This image is used for all help image icons and is shown throughout Moodle.&lt;br /&gt;
# &#039;&#039;&#039;moodle/pix/i/info.gif&#039;&#039;&#039; This image is used in several places, most notably the front page if the combo list is being displayed.&lt;br /&gt;
&lt;br /&gt;
So first up help.gif. This is the most basic example of overriding an image. Because the image is directly within Moodle&#039;s pix directory we can simply place our new help image into our themes pix_core directory. There&#039;s nothing more to it!&lt;br /&gt;
&lt;br /&gt;
The second example if not really any more difficult. Because the image is located within the subdirectory &#039;&#039;&#039;i&#039;&#039;&#039; we must create a sub directory within our pix_core directory into which we will copy our new help image. That is, the image ends up at &#039;&#039;&#039;/pix_core/i/info.png&#039;&#039;&#039; inside your theme folder. And again done!&lt;br /&gt;
&lt;br /&gt;
You should also note that, as with any other image in Moodle, the extension doesn&#039;t matter. If you want to replace help.gif with a help.png you can just put the png into the correct directory. As long as the filename is the same Moodle will find it.&lt;br /&gt;
&lt;br /&gt;
===Overriding plugin images===&lt;br /&gt;
This is a little bit more difficult than overriding core images, but not too much so. For this example, let&#039;s say I want to override the following two plugin images:&lt;br /&gt;
# &#039;&#039;&#039;moodle/mod/forum/icon.gif&#039;&#039;&#039; This is the icon that is used everywhere for the forum.&lt;br /&gt;
# &#039;&#039;&#039;moodle/blocks/customblock/activate.png&#039;&#039;&#039; This is an icon in a custom block I have installed.&lt;br /&gt;
&lt;br /&gt;
Just as with core images, we need to put the image in the correct directory structure. In this case it is going to be within our theme&#039;s &#039;&#039;&#039;pix_plugins&#039;&#039;&#039; directory.&lt;br /&gt;
The correct path must use the plugin type [[Frankenstyle]] prefix and the plugin name.&lt;br /&gt;
&lt;br /&gt;
For the examples above the paths to override the plugin images will be:&lt;br /&gt;
# &#039;&#039;&#039;pix_plugins/mod/forum/&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;pix_plugins/block/customblock/&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When looking for an overriding image the following path is used.&lt;br /&gt;
&lt;br /&gt;
# theme (static)&lt;br /&gt;
# themename =&amp;gt; mytheme&lt;br /&gt;
# area =&amp;gt; pix_plugins for plugin images, pix_core for core images, pix for theme images.&lt;br /&gt;
# plugin type =&amp;gt; booktool for the book tool sub plugin for example.&lt;br /&gt;
# plugin name =&amp;gt; print for the name of the sub plugin.&lt;br /&gt;
# file name =&amp;gt; book for the name of the image.&lt;br /&gt;
&lt;br /&gt;
It then searches for the image with the following extensions gif, png, jpg, jpeg in that order.&lt;br /&gt;
&lt;br /&gt;
So you end up with:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
/theme/themename/area/plugintype/pluginname/filename.gif&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Plugins]]&lt;br /&gt;
* [http://youtu.be/g_9VM9OMs3Y Adding Images to your Theme]  MoodleBites video on YouTube&lt;br /&gt;
&lt;br /&gt;
Using Moodle forum discussions:&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=151581 Using images within your themes (and the silk icon theme)]&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=151581#p689883 choosing images based on the users current language]&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=157935&amp;amp;parent=691903 how to add a category(categorias) img-bullet]&lt;/div&gt;</summary>
		<author><name>Dness</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Migrating_logging_calls_in_plugins&amp;diff=54482</id>
		<title>Migrating logging calls in plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Migrating_logging_calls_in_plugins&amp;diff=54482"/>
		<updated>2018-07-05T14:10:22Z</updated>

		<summary type="html">&lt;p&gt;Dness: /* Step 1. Choose a name for the event */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document is aimed to assist developers in replacing existing &#039;&#039;&#039;add_to_log()&#039;&#039;&#039; and &#039;&#039;&#039;events_trigger()&#039;&#039;&#039; calls with events. This can be implemented in Moodle 2.6 and will be required in 2.7.&lt;br /&gt;
&lt;br /&gt;
As a quick reminder: [[Event 2|new events]] were introduced in Moodle 2.6, a new [[Logging 2|logging system]] is being introduced in Moodle 2.7. The &#039;&#039;&#039;add_to_log()&#039;&#039;&#039; function will be deprecated, but the existing log table will still be present with existing data intact. This original logging is now called &#039;&#039;legacy logging&#039;&#039;. The new and legacy logging may coexist in the legacy logging system for purposes of transition, but this is not recommended for performance reasons. When replacing calls to add_to_log() with the triggering of an event, developers must ensure that they also generate an entry for the legacy log. It will only be used if the legacy log is enabled, since it may be enabled on systems that continue to use custom reports relying on presence of the legacy log table and it may take time to migrate such reports.&lt;br /&gt;
&lt;br /&gt;
== Quick guide ==&lt;br /&gt;
&lt;br /&gt;
If you are replacing common add_to_log() calls such as &amp;quot;view&amp;quot; and &amp;quot;view all&amp;quot; in mod/XXX/view.php and mod/XXX/index.php, see below. Otherwise do the following.&lt;br /&gt;
&lt;br /&gt;
=== Step 1. Choose a name for the event ===&lt;br /&gt;
&lt;br /&gt;
Names should follow the syntax OBJECT_VERB, for example &amp;quot;entry_added&amp;quot;, &amp;quot;work_submitted&amp;quot;, etc. It does not need to include a plugin name because this can be obtained from the PHP class namespace. See [[Event 2|the events documentation]] for more details about event name; specifically, there is a restricted [[Event 2#Verb list|list of available verbs]].&lt;br /&gt;
&lt;br /&gt;
Define a language string for the event name in &#039;&#039;&#039;YOURPLUGINDIR/lang/en/FULLPLUGINNAME.php&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$string[&#039;eventEVENTNAME&#039;] = &#039;Something has happened&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2. Create event class ===&lt;br /&gt;
&lt;br /&gt;
For each event you must create an event class in &#039;&#039;&#039;YOURPLUGINDIR/classes/event/EVENTNAME.php&#039;&#039;&#039;, with the following format.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// This file is part of Moodle - http://moodle.org/&lt;br /&gt;
//&lt;br /&gt;
// Moodle is free software: you can redistribute it and/or modify&lt;br /&gt;
// it under the terms of the GNU General Public License as published by&lt;br /&gt;
// the Free Software Foundation, either version 3 of the License, or&lt;br /&gt;
// (at your option) any later version.&lt;br /&gt;
//&lt;br /&gt;
// Moodle is distributed in the hope that it will be useful,&lt;br /&gt;
// but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;
// GNU General Public License for more details.&lt;br /&gt;
//&lt;br /&gt;
// You should have received a copy of the GNU General Public License&lt;br /&gt;
// along with Moodle.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * The EVENTNAME event.&lt;br /&gt;
 *&lt;br /&gt;
 * @package    FULLPLUGINNAME&lt;br /&gt;
 * @copyright  2014 YOUR NAME&lt;br /&gt;
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
namespace FULLPLUGINNAME\event;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
/**&lt;br /&gt;
 * The EVENTNAME event class.&lt;br /&gt;
 *&lt;br /&gt;
 * @property-read array $other {&lt;br /&gt;
 *      Extra information about event.&lt;br /&gt;
 *&lt;br /&gt;
 *      - PUT INFO HERE&lt;br /&gt;
 * }&lt;br /&gt;
 *&lt;br /&gt;
 * @since     Moodle MOODLEVERSION&lt;br /&gt;
 * @copyright 2014 YOUR NAME&lt;br /&gt;
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 **/&lt;br /&gt;
class EVENTNAME extends \core\event\base {&lt;br /&gt;
    protected function init() {&lt;br /&gt;
        $this-&amp;gt;data[&#039;crud&#039;] = &#039;c&#039;; // c(reate), r(ead), u(pdate), d(elete)&lt;br /&gt;
        $this-&amp;gt;data[&#039;edulevel&#039;] = self::LEVEL_PARTICIPATING;&lt;br /&gt;
        $this-&amp;gt;data[&#039;objecttable&#039;] = &#039;...&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static function get_name() {&lt;br /&gt;
        return get_string(&#039;eventEVENTNAME&#039;, &#039;FULLPLUGINNAME&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function get_description() {&lt;br /&gt;
        return &amp;quot;The user with id {$this-&amp;gt;userid} created ... ... ... with id {$this-&amp;gt;objectid}.&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function get_url() {&lt;br /&gt;
        return new \moodle_url(&#039;....&#039;, array(&#039;parameter&#039; =&amp;gt; &#039;value&#039;, ...));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function get_legacy_logdata() {&lt;br /&gt;
        // Override if you are migrating an add_to_log() call.&lt;br /&gt;
        return array($this-&amp;gt;courseid, &#039;PLUGINNAME&#039;, &#039;LOGACTION&#039;,&lt;br /&gt;
            &#039;...........&#039;,&lt;br /&gt;
            $this-&amp;gt;objectid, $this-&amp;gt;contextinstanceid);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static function get_legacy_eventname() {&lt;br /&gt;
        // Override ONLY if you are migrating events_trigger() call.&lt;br /&gt;
        return &#039;MYPLUGIN_OLD_EVENT_NAME&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    protected function get_legacy_eventdata() {&lt;br /&gt;
        // Override if you migrating events_trigger() call.&lt;br /&gt;
        $data = new \stdClass();&lt;br /&gt;
        $data-&amp;gt;id = $this-&amp;gt;objectid;&lt;br /&gt;
        $data-&amp;gt;userid = $this-&amp;gt;relateduserid;&lt;br /&gt;
        return $data;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3. Trigger the event instead of add_to_log() ===&lt;br /&gt;
&lt;br /&gt;
Replace the add_to_log() with an event trigger. The following is a common example of an event trigger inside an activity module.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
add_to_log($course-&amp;gt;id, &#039;PLUGINNAME&#039;, &#039;LOGACTION&#039;, &#039;...........&#039;, $objid, $cmid);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...becomes...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$event = \FULLPLUGINNAME\event\EVENTNAME::create(array(&lt;br /&gt;
    &#039;objectid&#039; =&amp;gt; $objid,&lt;br /&gt;
    &#039;context&#039; =&amp;gt; context_module::instance($cmid)&lt;br /&gt;
));&lt;br /&gt;
$event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need to trigger event multiple times in the code, or just prefer shorter syntax, you can declare your own static create function in the event class that would populate necessary fields and even add snapshots (see examples in mod_assign). In this case you can triger event in one line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
\FULLPLUGINNAME\event\EVENTNAME::create_from_someobject($someobject)-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4. Increase the version number in version.php===&lt;br /&gt;
&lt;br /&gt;
The events that exist are only scanned when a plugin is installed or updated. Therefore when you change events, you need to increase the plugin&#039;s version number in its version.php to prompt an upgrade.&lt;br /&gt;
&lt;br /&gt;
== Replacing &#039;view&#039; events in modules ==&lt;br /&gt;
&lt;br /&gt;
Calls to add_to_log() to report a &#039;view&#039; event are usually found in mod/PLUGINNAME/view.php (or in a lib function included by this file) and indicate that a user viewed the module. &lt;br /&gt;
&lt;br /&gt;
=== Step 1. Choosing the name ===&lt;br /&gt;
&lt;br /&gt;
Because this is a common event, the name is already chosen: &#039;&#039;&#039;course_module_viewed&#039;&#039;&#039; and the language string is defined in core.&lt;br /&gt;
&lt;br /&gt;
=== Step 2. Defining class ===&lt;br /&gt;
&lt;br /&gt;
You must create a class for this event in &#039;&#039;&#039;YOURPLUGINDIR/classes/event/course_module_viewed.php&#039;&#039;&#039; with the following format.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace FULLPLUGINNAME\event;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
class course_module_viewed extends \core\event\course_module_viewed {&lt;br /&gt;
    protected function init() {&lt;br /&gt;
        $this-&amp;gt;data[&#039;objecttable&#039;] = &#039;PLUGINNAME&#039;;&lt;br /&gt;
        parent::init();&lt;br /&gt;
    }&lt;br /&gt;
    // You might need to override get_url() and get_legacy_log_data() if view mode needs to be stored as well.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3. Triggering the event ===&lt;br /&gt;
&lt;br /&gt;
This example takes data from $PAGE object but you may substitute this with ids and objects that you have fetched.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$event = \FULLPLUGINNAME\event\course_module_viewed::create(array(&lt;br /&gt;
    &#039;objectid&#039; =&amp;gt; $PAGE-&amp;gt;cm-&amp;gt;instance,&lt;br /&gt;
    &#039;context&#039; =&amp;gt; $PAGE-&amp;gt;context,&lt;br /&gt;
));&lt;br /&gt;
$event-&amp;gt;add_record_snapshot(&#039;course&#039;, $PAGE-&amp;gt;course);&lt;br /&gt;
// In the next line you can use $PAGE-&amp;gt;activityrecord if you have set it, or skip this line if you don&#039;t have a record.&lt;br /&gt;
$event-&amp;gt;add_record_snapshot($PAGE-&amp;gt;cm-&amp;gt;modname, $activityrecord);&lt;br /&gt;
$event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step 4. Update version.php===&lt;br /&gt;
Don&#039;t forget to update the plugin version number to prompt an upgrade.&lt;br /&gt;
&lt;br /&gt;
== Replacing &#039;view all&#039; events in modules ==&lt;br /&gt;
&lt;br /&gt;
Calls to add_to_log using &#039;view_all&#039; are usually found in mod/PLUGINNAME/index.php (or in a lib function included by this file). These invents indicate that a user viewed the list of all instances of this module within the course. &lt;br /&gt;
&lt;br /&gt;
=== Step 1. Choosing the name ===&lt;br /&gt;
&lt;br /&gt;
Because this is a common event, the name is already chosen: &#039;&#039;&#039;course_module_instance_list_viewed&#039;&#039;&#039; and the language string is defined in core.&lt;br /&gt;
&lt;br /&gt;
=== Step 2. Defining class ===&lt;br /&gt;
&lt;br /&gt;
You must create a class for this event in &#039;&#039;&#039;YOURPLUGINDIR/classes/event/course_module_instance_list_viewed.php&#039;&#039;&#039; with the following structure.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace FULLPLUGINNAME\event;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3. Triggering the event ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$event = \FULLPLUGINNAME\event\course_module_instance_list_viewed::create(array(&lt;br /&gt;
    &#039;context&#039; =&amp;gt; context_course::instance($course-&amp;gt;id)&lt;br /&gt;
));&lt;br /&gt;
$event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What to include in the event ==&lt;br /&gt;
&lt;br /&gt;
=== init() and create() ===&lt;br /&gt;
&lt;br /&gt;
Ideally all information needed when initialising and triggering events should already be available without having to run additional queries. Queries run to fill event objects with data will cause additional performance load, which should be avoided. Information that needs to be gathered from the database should be provided by other event methods, which can be called selectively when needed.&lt;br /&gt;
&lt;br /&gt;
As you noticed in the examples above you can specify additional properties either by the overriding the init() method of the event or when calling create(). The first way is used for properties that are always the same for this event, the second is for dynamic properties that may differ when the event is triggered.&lt;br /&gt;
&lt;br /&gt;
Usually you need to include the following properties.&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;context&#039;&#039;&#039; or &#039;&#039;&#039;contextid&#039;&#039;&#039;&lt;br /&gt;
| required&lt;br /&gt;
| Describes the context where the event took place.&lt;br /&gt;
| If you want to hardcode the system context, do so in init().&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;crud&#039;&#039;&#039;&lt;br /&gt;
| required&lt;br /&gt;
| Describes whether the event reflects creation (c), reading (r), updating (u) or deleting (d). This should be a single character string.&lt;br /&gt;
| Most often are specified in init().&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;edulevel&#039;&#039;&#039;&lt;br /&gt;
| required&lt;br /&gt;
| The level of educational value of the event. Can be LEVEL_TEACHING, LEVEL_PARTICIPATING or LEVEL_OTHER.&lt;br /&gt;
| Most often are specified in init().&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;objecttable&#039;&#039;&#039; and &#039;&#039;&#039;objectid&#039;&#039;&#039;&lt;br /&gt;
| &lt;br /&gt;
| Objecttable is the table that best represents the event object. Usually it is the object where the &amp;quot;CRUD&amp;quot; action was performed. This table is used by events that show the change in one record of one table, which will be the case for the most events.&lt;br /&gt;
| Since &#039;objecttable&#039; is always the same it is usually specified in init().&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;relateduserid&#039;&#039;&#039;&lt;br /&gt;
| &lt;br /&gt;
| The id of the user affected by the event.&lt;br /&gt;
| Only used if it is easy to identify a single user who is affected by this operation. For example a user who is being graded, a user who receives the message, a user being enrolled, etc. &#039;&#039;&#039;This is NOT the user who performs the action&#039;&#039;&#039; (who is identified in the userid field).&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;other&#039;&#039;&#039;&lt;br /&gt;
| &lt;br /&gt;
| Everything else that you may think is important about this event. This property will be serialised and stored by loggers.&lt;br /&gt;
| Include only necessary information. It can be used in get_description(), get_url() and get_legacy_logdata(). This property should only contain an array or scalar value, it &#039;&#039;&#039;can not use objects&#039;&#039;&#039;.&lt;br /&gt;
Example of information stored in &#039;other&#039; can be found in event course_module_deleted:&lt;br /&gt;
* Definition: https://github.com/moodle/moodle/blob/MOODLE_27_STABLE/lib/classes/event/course_module_deleted.php#L71&lt;br /&gt;
* Triggering:https://github.com/moodle/moodle/blob/MOODLE_27_STABLE/course/lib.php#L1737..L1747&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Usually you don&#039;t need to include the following properties as they are deduced by the base class.&lt;br /&gt;
* &#039;userid&#039;: user who performs the action, taken from $USER&lt;br /&gt;
* &#039;courseid&#039;: course affected in the operation, which will be taken from context. It need not be specified at all for events that are not related to a particular course.&lt;br /&gt;
&lt;br /&gt;
See the [[Event_2#Information_contained_in_events|full list of properties]] for more information.&lt;br /&gt;
&lt;br /&gt;
=== get_legacy_logdata() ===&lt;br /&gt;
&lt;br /&gt;
This method is used to add log data to the legacy log. You need only override this method when replacing an add_to_log() call. Since this document is a transition guide from add_to_log() to events, you will most likely need to override this method. This method needs to return an array (with 3-7 elements) that imitates the arguments that used to be passed to the add_to_log() function. From Moodle 2.7, the get_legacy_logdata() method will only be called if legacy logging is enabled through the legacy logging plugin.&lt;br /&gt;
&lt;br /&gt;
=== get_description() and get_url() ===&lt;br /&gt;
&lt;br /&gt;
Most reporting tools will display aggregated event information (for example the count of student logins) so those methods, which describe individual events, are not likely to be called often; they will only be used by detailed reports such as loglive. At the moment, use get_description() to provide a very brief internal description of the action performed, so that it can be used for error recovery like any other system log. The description is hard-coded in English but it may be possible that future versions of Moodle (2.8 or later) will allow the use of translatable language strings. These methods should not make DB queries, access global variables, etc. For example, when a course is renamed or when a user is deleted, do not retrieve the course name or user name, instead simply use their ids. These functions should return exactly the same result whenever they are called, regardless of the environment or state, even after they have been restored from logs. &lt;br /&gt;
&lt;br /&gt;
=== get_legacy_eventname() and get_legacy_eventdata() ===&lt;br /&gt;
&lt;br /&gt;
You will need to override these two functions if you are upgrading events_trigger() calls. These will allow legacy plugins to continue to listen to your new events without upgrading their listeners.&lt;br /&gt;
&lt;br /&gt;
If you need to provide more detailed information to observers, you can choose to:&lt;br /&gt;
* add more information to &#039;other&#039;, but remember that this will be logged and it&#039;s better to keep logs as small as possible;&lt;br /&gt;
* use record snapshots, which are especially useful for delete actions (you can call get_record_snapshot() inside get_legacy_eventdata() and observers are encouraged to get data from snapshots as well);&lt;br /&gt;
* add new properties to your event class and define getter/setter functions, for example set_custom_data() and get_custom_data().&lt;br /&gt;
&lt;br /&gt;
=== add_record_snapshot() ===&lt;br /&gt;
&lt;br /&gt;
A record snapshot can be added for any DB table related to the event. If it is added, it must be an instance of stdClass containing all fields that are present in the corresponding DB table. You must add a record snapshot when you delete something from database. &#039;&#039;&#039;Record snapshots cannot be used from reports, it is intended for event observers only.&#039;&#039;&#039; Usually observers expect a record snapshot identified by &#039;objecttable&#039; and &#039;objectid&#039; but developers may also add snapshots of related tables, i.e. when book chapter is updated the developer may decide to add snapshots of related records in tables book_chapters, book, course_modules and course. &lt;br /&gt;
&lt;br /&gt;
Record snapshots should be added only when you already have an object and do not need to perform any additional DB queries to retrieve it. Otherwise omit it, as the record will be retrieved by get_record_snapshot() automatically, and only if needed. For performance reasons the snapshots are not guaranteed to contain an exact state at the time of event triggering, it may be fetched at any time between the triggering of event and its observation.&lt;br /&gt;
&lt;br /&gt;
== Events DON&#039;Ts ==&lt;br /&gt;
&lt;br /&gt;
Do not put more information in &#039;other&#039; than is needed. For example, do not include a full DB record for delete/create operations or a list of all changed properties in edit operations. If observers are interested in this information, it can requested by calling get_record_snapshot(). Never include large text fields in event data. &#039;&#039;&#039;Please help to keep the log size reasonable.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Do NOT use $USER, $COURSE, $PAGE or other global variables when overriding get_* methods (with the exception of get_legacy_eventdata).&lt;br /&gt;
&lt;br /&gt;
Do NOT call $this-&amp;gt;get_record_snapshot() inside the event class (again with the exception of get_legacy_eventdata). If you need additional information for internal functions that cannot be added to existing properties, add it to the &#039;other&#039; property.&lt;br /&gt;
&lt;br /&gt;
Do NOT use $this-&amp;gt;context inside an event class. &#039;&#039;&#039;Remember that methods get_description() and get_url() may be called on events after they have been restored from logs.&#039;&#039;&#039; It is possible that the original context no longer exists when these functions are called. Instead use $this-&amp;gt;contextid, $this-&amp;gt;contextlevel, $this-&amp;gt;contextinstanceid.&lt;br /&gt;
&lt;br /&gt;
== Validation and testing ==&lt;br /&gt;
&lt;br /&gt;
You may notice that the most of events in Moodle also have function validate_data() . You can add this function for your own safety to ensure that you don&#039;t forget to define all required data when triggering event.&lt;br /&gt;
&lt;br /&gt;
Always use developer debugging mode when doing development. A lot of useful build-in validation will only work in development mode.&lt;br /&gt;
&lt;br /&gt;
We highly recommend to cover your events with unit tests. Search in standard plugins for files with the names events_test.php to see examples.&lt;br /&gt;
&lt;br /&gt;
In order to manually test the event you can perform the action and check how it appears in &#039;&#039;&#039;Course Administration &amp;gt; Reports &amp;gt; Logs&#039;&#039;&#039;. Also look up your event in the &#039;&#039;&#039;Site Administration &amp;gt; Reports &amp;gt; Events list&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;Installation&amp;quot; of events ==&lt;br /&gt;
&lt;br /&gt;
Remember that you have to bump the plugin version in order to prompt an upgrade so that new events will be &amp;quot;installed&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
The [https://github.com/markn86/moodle-mod_certificate/pull/34 &#039;&#039;&#039;add_to_log deprecated, replace with events&#039;&#039;&#039;] issue on Github illustrates how the deprecated &#039;&#039;&#039;add_to_log()&#039;&#039;&#039; calls in the Certificate Module were upgraded to the new events method.&lt;/div&gt;</summary>
		<author><name>Dness</name></author>
	</entry>
</feed>