<?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=Maherne</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=Maherne"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Maherne"/>
	<updated>2026-11-25T10:18:10Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Moodle_icons&amp;diff=64064</id>
		<title>Moodle icons</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Moodle_icons&amp;diff=64064"/>
		<updated>2024-01-29T15:58:20Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Font awesome icons */ Removed reference to fontawesome support being new&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox Project&lt;br /&gt;
|name = Complete overhaul of all standard icons in core&lt;br /&gt;
|state = Completed&lt;br /&gt;
|tracker = MDL-34080&lt;br /&gt;
|discussion = [https://moodle.org/mod/forum/discuss.php?d=204278 New default icon set for Moodle 2.4 roadmap]&lt;br /&gt;
|assignee = [[User:Barbara Ramiro|Barbara Ramiro]] and Frédéric Massart&lt;br /&gt;
}}&lt;br /&gt;
{{Moodle 2.4}}&lt;br /&gt;
The default icons were updated for Moodle 2.4. Not only are the icons a little bigger and more colourful but they are SVG format and support full scaling on all devices, so they always look good.&lt;br /&gt;
&lt;br /&gt;
For font-awesome icons in Moodle 3.3 and later, see the relevant section below.&lt;br /&gt;
&lt;br /&gt;
== Summary of changes ==&lt;br /&gt;
&lt;br /&gt;
To make your activity modules look good, you need to upgrade your icons by adding some SVG files. Use the following design guidelines to produce an icon that plays well with the core ones.&lt;br /&gt;
&lt;br /&gt;
* Icon format: SVG preferred and PNG version for fallback&lt;br /&gt;
* Activity icons: Increased in size from 16x16 to 24x24 px colour&lt;br /&gt;
* Action icons: Standardized from various sizes to 12x12 px monochrome&lt;br /&gt;
* All other icons: Standardized from various sizes to 16x16 px monochrome&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
&lt;br /&gt;
* Universal design - commonly used&lt;br /&gt;
* Distinctive shape and form for its function or purpose&lt;br /&gt;
* Works well on light and dark background&lt;br /&gt;
* Scalable for multiple screen resolutions&lt;br /&gt;
&lt;br /&gt;
== Format ==&lt;br /&gt;
&lt;br /&gt;
* Plugin icons: gradient colour, 16x16 px canvas, output 16x16 px. Except for:&lt;br /&gt;
** Activity icons: gradient colour, 24x24 px canvas, output 24x24 px&lt;br /&gt;
* Action icons: monochrome, 12x12 px canvas, output 12x12 px. Except for:&lt;br /&gt;
** Toolbar icons: monochrome, 16x16 px canvas, output 16x16 px&lt;br /&gt;
** Dropdown icons: monochrome, 16x16 px canvas, output 16x16 px&lt;br /&gt;
* Display icons: monochrome, 16x16 px canvas, output 16x16 px&lt;br /&gt;
* SVG (Scalable Vector Graphics) and PNG version in different sizes (12px, 16, 24) for unsupported browsers in 72ppi&lt;br /&gt;
&lt;br /&gt;
== Style ==&lt;br /&gt;
&lt;br /&gt;
* Flat perspective&lt;br /&gt;
* Not too much details on small resolutions&lt;br /&gt;
* Not too few details on higher resolutions&lt;br /&gt;
* Consider space and proportion&lt;br /&gt;
* Source of light: 90 degrees&lt;br /&gt;
* Sharp&lt;br /&gt;
* Consistent curvature on rounded corners&lt;br /&gt;
* Lighting and gradients should have consistent amount and direction&lt;br /&gt;
&lt;br /&gt;
== Activity icons ==&lt;br /&gt;
&lt;br /&gt;
=== Properties ===&lt;br /&gt;
&lt;br /&gt;
* Outline: Outer border - dark gradient&lt;br /&gt;
* Highlight: inner border - light gradient&lt;br /&gt;
* Body - Gradient&lt;br /&gt;
&lt;br /&gt;
[[Image:design-activity-icon.png|200px]]&lt;br /&gt;
&lt;br /&gt;
=== Effects on same colour background ===&lt;br /&gt;
&lt;br /&gt;
* With highlight, it stands out from the background&lt;br /&gt;
* Without highlight it blends into the background&lt;br /&gt;
&lt;br /&gt;
[[Image:design-activity-icon-effects.png|200px]]&lt;br /&gt;
&lt;br /&gt;
=== Samples ===&lt;br /&gt;
&lt;br /&gt;
[[Image:design-activity-icon-samples.png|200px]]&lt;br /&gt;
&lt;br /&gt;
=== Gradient colour palette ===&lt;br /&gt;
&lt;br /&gt;
* Column 1: Outer outline (top to bottom)&lt;br /&gt;
* Column 2: Inner outline - highlight (top to bottom)&lt;br /&gt;
* Column 3: Body (top to bottom)&lt;br /&gt;
&lt;br /&gt;
The palette in a file format suitable for Inkscape can be downloaded from [https://github.com/mudrd8mz/moodle-graphics]&lt;br /&gt;
&lt;br /&gt;
[[Image:design-activity-icon-palette.png|200px]]&lt;br /&gt;
&lt;br /&gt;
== Action and Display Icons ==&lt;br /&gt;
&lt;br /&gt;
=== Dimensions ===&lt;br /&gt;
&lt;br /&gt;
* Action icons: canvas 12x12px - output 12x12 px&lt;br /&gt;
** Toolbar icons: canvas 16x16px - output 16x16 px&lt;br /&gt;
** Dropdown icons: canvas 16x16px - output 16x16 px&lt;br /&gt;
* Display icons: canvas 16x16px - output 16x16 px&lt;br /&gt;
&lt;br /&gt;
=== Style ===&lt;br /&gt;
&lt;br /&gt;
* Simple (not too detailed)&lt;br /&gt;
* Flat&lt;br /&gt;
* Smooth curves&lt;br /&gt;
* Fill entire dimension (rotate to 45 deg for thin shapes)&lt;br /&gt;
* Single layer&lt;br /&gt;
&lt;br /&gt;
=== Colour ===&lt;br /&gt;
&lt;br /&gt;
* #999999 (r:153 g:153 b:153)&lt;br /&gt;
&lt;br /&gt;
=== Samples ===&lt;br /&gt;
&lt;br /&gt;
[[Image:design-action-block-icon-samples.png|200px]]&lt;br /&gt;
&lt;br /&gt;
== SVG settings (Illustrator) ==&lt;br /&gt;
&lt;br /&gt;
* SVG Profile: SVG 1.1&lt;br /&gt;
* Font Subsetting: None (use system fonts)&lt;br /&gt;
* Image Location: Embed Images&lt;br /&gt;
* CSS Property: Style attributes&lt;br /&gt;
* Font Type: SVG&lt;br /&gt;
* Encoding: UTF-8&lt;br /&gt;
* Not compressed (compressed creates svgz and becomes unusable)&lt;br /&gt;
* Vector data precision: 1 decimal&lt;br /&gt;
* Use illustrator effects rather than Photoshop effects (to make it vector rather than raster)&lt;br /&gt;
* Add effect layer rather than direct on the object for easy editing&lt;br /&gt;
&lt;br /&gt;
Illustrator files:&lt;br /&gt;
&lt;br /&gt;
* [https://docs.moodle.org/dev/images_dev/7/72/24px_activity_icons.ai 24px activity icons.ai] (544 KB)&lt;br /&gt;
* [https://docs.moodle.org/dev/images_dev/9/98/16px_icons.ai 16px icons.ai] (1.42 MB)&lt;br /&gt;
* [https://docs.moodle.org/dev/images_dev/5/50/12px_icons.ai 12px icons.ai] (1.33 MB)&lt;br /&gt;
&lt;br /&gt;
== General recommendations ==&lt;br /&gt;
&lt;br /&gt;
Make it easy for designers to edit the SVG file in image editor and for themers to edit it in the text editor:&lt;br /&gt;
&lt;br /&gt;
* Use as few vector points as possible (no unnecessary points)&lt;br /&gt;
* If possible, use basic shapes and the pathfinder tools instead of the pen tool&lt;br /&gt;
* Clean layers: remove unnecessary or unused layers. Group and name it accordingly&lt;br /&gt;
* Use effects that allows roundtrip without rasterization (meaning it should not render raster from svg to illustrator to svg)&lt;br /&gt;
* Use grids: 12, 16 and 24&lt;br /&gt;
&lt;br /&gt;
== Font awesome icons ==&lt;br /&gt;
{{Moodle 3.3}}Moodle 3.3 comes with built in support for Fontawesome icons. This is an icon font with over 470 different icons to choose from. Some advantages of using Fontawesome are, fewer http requests, sharper scaling, color can be adjusted with CSS etc. Font awesome must be supported by the theme in order for it to have an effect. This means the theme must specify:&lt;br /&gt;
&lt;br /&gt;
theme/example/config.php&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$THEME-&amp;gt;iconsystem = \core\output\icon_system::FONTAWESOME;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The value of this config is a class name that extends the abstract class &amp;quot;\core\output\icon_system&amp;quot;. This means it is possible to create a new / different implementation of an icon system entirely in a theme (like inline svg). Creating a new icon system is only recommended for advanced devs because you need to implement the mustache handlers for javascript as well as the php class. The icon_system_fontawesome is a working example to base other implementations on.&lt;br /&gt;
&lt;br /&gt;
When using the font-awesome icon system, Moodle will look for a mapping between regular icons and font-awesome ones every time it renders an icon. This is done by plugin authors who implement a COMPONENTNAME_get_fontawesome_icon_map() callback in their plugin to map all their own custom icons to ones from font-awesome:&lt;br /&gt;
&lt;br /&gt;
mod/forum/lib.php&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  /**                                                                                                                                 &lt;br /&gt;
   * Get icon mapping for font-awesome.                                                                                               &lt;br /&gt;
   */                                                                                                                                 &lt;br /&gt;
  function mod_forum_get_fontawesome_icon_map() {                                                                                     &lt;br /&gt;
      return [                                                                                                                        &lt;br /&gt;
          &#039;mod_forum:i/pinned&#039; =&amp;gt; &#039;fa-map-pin&#039;,                                                                                       &lt;br /&gt;
          &#039;mod_forum:t/selected&#039; =&amp;gt; &#039;fa-check&#039;,                                                                                       &lt;br /&gt;
          &#039;mod_forum:t/subscribed&#039; =&amp;gt; &#039;fa-envelope-o&#039;,                                                                                &lt;br /&gt;
          &#039;mod_forum:t/unsubscribed&#039; =&amp;gt; &#039;fa-envelope-open-o&#039;,                                                                         &lt;br /&gt;
      ];                                                                                                                              &lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Questions:&lt;br /&gt;
&lt;br /&gt;
Does this work for {{#pix}} tags in mustache templates? &lt;br /&gt;
&lt;br /&gt;
YES!&lt;br /&gt;
&lt;br /&gt;
Does this work for {{#pix}} tags in mustache templates rendered in Javascript? &lt;br /&gt;
&lt;br /&gt;
YES!&lt;br /&gt;
&lt;br /&gt;
Does this work for code directly creating image tags with pix_url() ? &lt;br /&gt;
&lt;br /&gt;
No - change your code to use a full pix_icon renderer! Icons do not even have urls any more.&lt;br /&gt;
&lt;br /&gt;
How do I update my old javascript that manually builds image tags? &lt;br /&gt;
&lt;br /&gt;
Call renderPix from the core/templates amd module - it will build you a full chunk of icon HTML.&lt;br /&gt;
&lt;br /&gt;
How do I find a suitable font-awesome icon? &lt;br /&gt;
&lt;br /&gt;
Try: http://fontawesome.io/icons/&lt;br /&gt;
&lt;br /&gt;
How do I get a new icon created? &lt;br /&gt;
&lt;br /&gt;
http://fontawesome.io/community/#requesting-new-icons&lt;br /&gt;
&lt;br /&gt;
What about icons that should really always be images (like activity icons)? &lt;br /&gt;
&lt;br /&gt;
Use $OUTPUT-&amp;gt;image_icon() instead of $OUTPUT-&amp;gt;pix_icon() (or $OUTPUT-&amp;gt;image_url() instead of $OUTPUT-&amp;gt;pix_url()).&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [https://docs.moodle.org/dev/images_dev/e/e8/Moodle_icons_24.pdf Full list of Moodle 2.4 icons.pdf] (718KB)&lt;br /&gt;
* [[2.3 icons versus 2.4]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Themes]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=58973</id>
		<title>version.php</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=58973"/>
		<updated>2021-06-15T14:26:29Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an essential file expected to be located in the root directory of the plugin.&lt;br /&gt;
&lt;br /&gt;
It contains a number of properties, which are used during the plugin installation and upgrade process. It allows to make sure the plugin is compatible with the given Moodle site, as well as spotting whether an upgrade is needed.&lt;br /&gt;
&lt;br /&gt;
The file is a standard PHP file, starting with an opening &#039;&amp;amp;lt;?php&#039; tag and defining the following variables:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; There was an exception for [[Activity modules]]. In the past, activity modules used &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; in their version.php files. The &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; notation support for activity modules was added in Moodle 2.5 (MDL-38121) and became recommended. Support for legacy &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; was dropped in Moodle 3.0 (MDL-43896).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Support for &amp;lt;tt&amp;gt;$plugin-&amp;gt;supported =&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$plugin-&amp;gt;incompatible =&amp;lt;/tt&amp;gt; was added in Moodle 3.9 (MDL-59562).&lt;br /&gt;
&lt;br /&gt;
== Plugin version properties ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Property&lt;br /&gt;
! Type&lt;br /&gt;
! Status&lt;br /&gt;
! Description and example&lt;br /&gt;
|- style=&amp;quot;vertical-align:top&amp;quot;&lt;br /&gt;
| style=&amp;quot;min-width:200px&amp;quot; | &#039;&#039;&#039;$plugin-&amp;gt;component&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| required&amp;lt;sup&amp;gt;*&amp;lt;/sup&amp;gt;&lt;br /&gt;
| The full [[Frankenstyle|frankenstyle]] component name in the form of plugintype_pluginname. It is used during the installation and upgrade process for diagnostics and validation purposes to make sure the plugin code has been deployed to the correct location within the Moodle code tree.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;) Required since Moodle 3.0, strongly recommended for earlier versions (MDL-48494).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;component = &#039;block_foobar&#039;; // Declare the type and name of this plugin.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;version&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| required&lt;br /&gt;
| The version number of the plugin. The format is partially date based with the form YYYYMMDDXX where XX is an incremental counter for the given year (YYYY), month (MM) and date (DD) of the plugin version&#039;s release. Every new plugin version must have this number increased in this file, which is detected by Moodle core and the upgrade process is triggered.&lt;br /&gt;
&lt;br /&gt;
In case of linear development (i.e. there is just one branch of the plugin maintained), the XX counter should stay with the initial 00 value unless multiple versions are released on the same date.&lt;br /&gt;
&lt;br /&gt;
If multiple stable branches of the plugin are maintained, the date part YYYYMMDD should be frozen at the branch forking date and the XX is used for incrementing the value on the given stable branch (allowing up to 100 updates on the given stable branch). The key point is that the version number is always increased both horizontally (within the same stable branch, more recent updates have higher XX than any previous one) and vertically (between any two stable branches, the more recent branch has YYYYMMDD00 higher than the older stable branch). Pay attention to this. It&#039;s easy to mess up and hard to fix.&lt;br /&gt;
&lt;br /&gt;
See [https://moodle.org/mod/forum/discuss.php?d=275635#p1184986] for an example of versioning scheme on multiple branches.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;version = 2014052000; // Plugin released on 20th May 2014.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;requires&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| recommended&lt;br /&gt;
| Specifies the minimum version number of Moodle core that this plugin requires. It is not possible to install it to earlier Moodle version. Moodle core&#039;s version number is defined in the file version.php located in Moodle root directory, in the &amp;lt;tt&amp;gt;$version&amp;lt;/tt&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
Note that Moodle core extends the version numbering scheme and has inbuilt support for decimal versioning (such as &amp;lt;tt&amp;gt;$version = 2014051200.01;&amp;lt;/tt&amp;gt;). The decimal part is used for incremental (weekly) builds. Plugins should not rely on a particular weekly build so they use just the whole number part of the Moodle core&#039;s version in this field.&lt;br /&gt;
&lt;br /&gt;
See [[Releases]] for the full list of Moodle core version numbers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;requires = 2014051200; // Moodle 2.7.0 is required.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;supported&#039;&#039;&#039;&lt;br /&gt;
| array of two ascending integers&lt;br /&gt;
| optional&lt;br /&gt;
| Specifies a range of branch numbers of supported versions, inclusive.&lt;br /&gt;
Moodle versions that are outside of this range will produce a message notifying at install time, but will allow for installation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;supported = [37, 39]; // Moodle 3.7.x, 3.8.x and 3.9.x are supported.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;incompatible&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| optional&lt;br /&gt;
| Specifies the earliest incompatible Moodle major branch.&lt;br /&gt;
Any Moodle versions including and above this will be prevented from installing the plugin, and a message will be given when attempting installation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;incompatible = 36; // Moodle 3.6.x and later are incompatible.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;cron&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| discouraged*&lt;br /&gt;
| Allows to throttle the plugin&#039;s cron function calls. The set value represents a minimal required gap in seconds between two calls of the plugin&#039;s cron function. Note that the cron function is not supported in all plugin types (also note that, while the cron function &#039;&#039;&#039;is&#039;&#039;&#039; supported for authentication plugins, it is run every time the cron script runs, regardless of this value in version.php). This value is stored in the database. After changing this value, the version number must be incremented. For activity modules, if this value is not set or 0 the cron function is disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;) Starting with Moodle 2.7, plugin developers are encouraged to use the [[Task API|scheduled task API]] instead of the cron function feature.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;cron = 300; // Do not execute this plugin&#039;s cron more often than every five minutes.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;maturity&#039;&#039;&#039;&lt;br /&gt;
| enum&lt;br /&gt;
| recommended&lt;br /&gt;
| Declares the maturity level of this plugin version, that is how stable it is. This affects the [[:en:Available update notifications|available update notifications]] feature in Moodle. Administrators can configure their site so that they are not notified about an available update unless it has certain maturity level declared.&lt;br /&gt;
&lt;br /&gt;
Supported value is any of the predefined constants &amp;lt;tt&amp;gt;MATURITY_ALPHA, MATURITY_BETA, MATURITY_RC&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MATURITY_STABLE&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;maturity = MATURITY_STABLE; // This is considered as ready for production sites.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;release&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| recommended&lt;br /&gt;
| Human readable version name that should help to identify each release of the plugin. It can be anything you like although it is recommended to choose a pattern and stick with it. Usually it is a simple version like &amp;lt;tt&amp;gt;2.1&amp;lt;/tt&amp;gt; but some plugin authors use more sophisticated schemes or follow the upstream release name if the plugin represents a wrapper for another program.&lt;br /&gt;
&lt;br /&gt;
It is a good practise to tag the code in the source code management system for each plugin release and then make the tag names matching this release name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;release = &#039;v2.7-r1&#039;; // This is our first revision for Moodle 2.7.x branch.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;dependencies&#039;&#039;&#039;&lt;br /&gt;
| array&lt;br /&gt;
| optional&lt;br /&gt;
| Allows to declare explicit dependency on other plugin(s) for this plugin to work. Moodle core checks these declared dependencies and will not allow the plugin installation and/or upgrade until all dependencies are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each element in the array represents an dependency on another plugin. The key (string) in the array is the component (frankenstyle) name of the other plugin. The value (integer) is the version of the other plugin we require. The predefined constant &amp;lt;tt&amp;gt;ANY_VERSION&amp;lt;/tt&amp;gt; can be used if we do not depend on a particular version (we just need the other plugin present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;dependencies = array(&lt;br /&gt;
    &#039;mod_foo&#039; =&amp;gt; ANY_VERSION,   // The Foo activity must be present (any version).&lt;br /&gt;
    &#039;enrol_bar&#039; =&amp;gt; 2014020300, // The Bar enrolment plugin version 2014020300 or higher must be present.&lt;br /&gt;
);&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File template ==&lt;br /&gt;
&lt;br /&gt;
Here is a template for the plugin&#039;s version.php file to copy and paste:&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;
 * @package   plugintype_pluginname&lt;br /&gt;
 * @copyright 2020, You Name &amp;lt;your@email.address&amp;gt;&lt;br /&gt;
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;version = TODO;&lt;br /&gt;
$plugin-&amp;gt;requires = TODO;&lt;br /&gt;
$plugin-&amp;gt;supported = TODO;   // Available as of Moodle 3.9.0 or later.&lt;br /&gt;
$plugin-&amp;gt;incompatible = TODO;   // Available as of Moodle 3.9.0 or later.&lt;br /&gt;
$plugin-&amp;gt;component = &#039;TODO_FRANKENSTYLE&#039;;&lt;br /&gt;
$plugin-&amp;gt;maturity = MATURITY_STABLE;&lt;br /&gt;
$plugin-&amp;gt;release = &#039;TODO&#039;;&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;dependencies = [&lt;br /&gt;
    &#039;mod_forum&#039; =&amp;gt; ANY_VERSION,&lt;br /&gt;
    &#039;mod_data&#039; =&amp;gt; TODO&lt;br /&gt;
];&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Moodle versions]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=58198</id>
		<title>Adding a web service to a plugin</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=58198"/>
		<updated>2021-01-12T10:45:15Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle_2.0}}&lt;br /&gt;
&lt;br /&gt;
= Quick start =&lt;br /&gt;
== Example ==&lt;br /&gt;
Have a look to the [https://github.com/moodlehq/moodle-local_wstemplate web service plugin template]. This plugin contains a web service hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.&lt;br /&gt;
== File structure ==&lt;br /&gt;
The file structure is explained in these two documents:&lt;br /&gt;
* [[Web_services_API|Web services API]]&lt;br /&gt;
* [[External_functions_API|External function API]]&lt;br /&gt;
&lt;br /&gt;
= Tutorial =&lt;br /&gt;
We will create a web service into a local plugin. This service will contain one &#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; web service function. This web service function will create a group into a Moodle course.&lt;br /&gt;
&lt;br /&gt;
== Write the specification documentation ==&lt;br /&gt;
Before starting coding, let&#039;s identify our needs writing some short specification documents.&lt;br /&gt;
&lt;br /&gt;
=== functional specification===&lt;br /&gt;
&#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; will take a list of groups as parameters and it will return the same groups with their newly created id. If ever one group creation fails, the function will throw an exception, and no creation will happen.&lt;br /&gt;
&lt;br /&gt;
=== technical specification ===&lt;br /&gt;
* &#039;&#039;&#039;the core function the external function will call&#039;&#039;&#039;:  &#039;&#039;groups_create_group()&#039;&#039; from [http://cvs.moodle.org/moodle/group/ /group/lib.php].&lt;br /&gt;
* &#039;&#039;&#039;the parameter types&#039;&#039;&#039;: a list of object. This object are groups, with id/name/courseid.&lt;br /&gt;
* &#039;&#039;&#039;the returned value types&#039;&#039;&#039;:  a list of objects (groups) with their id.&lt;br /&gt;
* &#039;&#039;&#039;the user capabilities to check&#039;&#039;&#039;: &#039;&#039;moodle/course:managegroups&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Write a simple test client ==&lt;br /&gt;
The first thing you should code is a web service test client. You will often discover use cases that you didn&#039;t think about. We are not showing any test client code here, see [[Creating_a_web_service_client|How to create a web service client]].&lt;br /&gt;
&lt;br /&gt;
== Declare the service ==&lt;br /&gt;
This step is optional. You can pre-build a service including any web service functions, so the Moodle administrator doesn&#039;t need to do it. Add into /local/myplugin/db/services.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
  $services = array(&lt;br /&gt;
      &#039;mypluginservice&#039; =&amp;gt; array(                                                // the name of the web service&lt;br /&gt;
          &#039;functions&#039; =&amp;gt; array (&#039;local_myplugin_create_groups&#039;), // web service functions of this service&lt;br /&gt;
          &#039;requiredcapability&#039; =&amp;gt; &#039;&#039;,                // if set, the web service user need this capability to access &lt;br /&gt;
                                                                              // any function of this service. For example: &#039;some/capability:specified&#039;                 &lt;br /&gt;
          &#039;restrictedusers&#039; =&amp;gt; 0,                                             // if enabled, the Moodle administrator must link some user to this service&lt;br /&gt;
                                                                              // into the administration&lt;br /&gt;
          &#039;enabled&#039; =&amp;gt; 1,                                                       // if enabled, the service can be reachable on a default installation&lt;br /&gt;
          &#039;shortname&#039; =&amp;gt;  &#039;&#039;,       // optional – but needed if restrictedusers is set so as to allow logins.&lt;br /&gt;
          &#039;downloadfiles&#039; =&amp;gt; 0,    // allow file downloads.&lt;br /&gt;
          &#039;uploadfiles&#039;  =&amp;gt; 0      // allow file uploads.&lt;br /&gt;
       )&lt;br /&gt;
  );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: it is not possible for an administrator to add/remove any function from a pre-built service.&lt;br /&gt;
&lt;br /&gt;
== Declare the web service function ==&lt;br /&gt;
Following the [[Web_services_API|Web service API]], you must declare the web service function in the &#039;&#039;local/myplugin/db/services.php&#039;&#039; file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$functions = array(&lt;br /&gt;
    &#039;local_myplugin_create_groups&#039; =&amp;gt; array(         //web service function name&lt;br /&gt;
        &#039;classname&#039;   =&amp;gt; &#039;local_myplugin_external&#039;,  //class containing the external function OR namespaced class in classes/external/XXXX.php&lt;br /&gt;
        &#039;methodname&#039;  =&amp;gt; &#039;create_groups&#039;,          //external function name&lt;br /&gt;
        &#039;classpath&#039;   =&amp;gt; &#039;local/myplugin/externallib.php&#039;,  //file containing the class/external function - not required if using namespaced auto-loading classes.&lt;br /&gt;
                                                   // defaults to the service&#039;s externalib.php&lt;br /&gt;
        &#039;description&#039; =&amp;gt; &#039;Creates new groups.&#039;,    //human readable description of the web service function&lt;br /&gt;
        &#039;type&#039;        =&amp;gt; &#039;write&#039;,                  //database rights of the web service function (read, write)&lt;br /&gt;
        &#039;ajax&#039; =&amp;gt; true,        // is the service available to &#039;internal&#039; ajax calls. &lt;br /&gt;
        &#039;services&#039; =&amp;gt; array(MOODLE_OFFICIAL_MOBILE_SERVICE)    // Optional, only available for Moodle 3.1 onwards. List of built-in services (by shortname) where the function will be included.  Services created manually via the Moodle interface are not supported.&lt;br /&gt;
        &#039;capabilities&#039; =&amp;gt; &#039;&#039;, // comma separated list of capabilities used by the function.&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Web service functions should match the [https://docs.moodle.org/dev/Web_services_Roadmap#Naming_convention naming convention].&lt;br /&gt;
&lt;br /&gt;
== Write the external function descriptions ==&lt;br /&gt;
Every web service function is mapped to an external function. External function are described in the [[External_functions_API|External functions API]].&lt;br /&gt;
Each external function is written with two other functions describing the parameters and the return values. These description functions are used by web service servers to:&lt;br /&gt;
* validate the web service function parameters&lt;br /&gt;
* validate the web service function returned values&lt;br /&gt;
* build WSDL files or other protocol documents &lt;br /&gt;
&lt;br /&gt;
These two description functions are located in the same file and the same class mentioned in local/myplugin/db/services.php.&lt;br /&gt;
&lt;br /&gt;
Thus for the web service function &#039;&#039;&#039;local_myplugin_create_groups()&#039;&#039;&#039;, we need write a class named &#039;&#039;&#039;local_myplugin_external&#039;&#039;&#039; in the file &#039;&#039;&#039;local/myplugin/externallib.php&#039;&#039;&#039;. The class will contain:&lt;br /&gt;
* create_groups(...)&lt;br /&gt;
* create_groups_parameters()&lt;br /&gt;
* create_groups_return()&lt;br /&gt;
&lt;br /&gt;
=== create_groups_parameters() ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&amp;quot;$CFG-&amp;gt;libdir/externallib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
class local_myplugin_external extends external_api {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A web service function without parameters will have a parameter description function like that:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function functionname_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
               //if I had any parameters, they would be described here. But I don&#039;t have any, so this array is empty.&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A parameter can be described as: &lt;br /&gt;
* a list               =&amp;gt; external_multiple_structure&lt;br /&gt;
* an object         =&amp;gt; external_single_structure&lt;br /&gt;
* a primary type =&amp;gt; external_value&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our create_groups() function expects one parameter named &#039;&#039;groups&#039;&#039;, so we will first write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; ...&lt;br /&gt;
                &lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This &#039;&#039;groups&#039;&#039; parameter is a list of group. So we will write :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    ...&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An external_multiple_structure object (list) can be constructed with: &lt;br /&gt;
* &#039;&#039;external_multiple_structure&#039;&#039; (list)&lt;br /&gt;
* &#039;&#039;external_single_structure&#039;&#039; (object)&lt;br /&gt;
* &#039;&#039;external_value&#039;&#039; (primary type). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For our function it will be a &#039;&#039;external_single_structure&#039;&#039;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;             &lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )           &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we obtain :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each group values is a &#039;&#039;external_value&#039;&#039; (primary type):&lt;br /&gt;
* &#039;&#039;courseid&#039;&#039; is an integer&lt;br /&gt;
* &#039;&#039;name&#039;&#039; is a string (text only, not tag)&lt;br /&gt;
* &#039;&#039;description&#039;&#039; is a string (can be anything)&lt;br /&gt;
* &#039;&#039;enrolmentkey&#039;&#039; is also a string (can be anything) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We add them to the description :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;), //the second argument is a human readable description text. This text is displayed in the automatically generated documentation.&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== create_groups_returns() ===&lt;br /&gt;
&lt;br /&gt;
It&#039;s similar to create_groups_parameters(), but instead of describing the parameters, it describes the return values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function create_groups_returns() {&lt;br /&gt;
        return new external_multiple_structure(&lt;br /&gt;
            new external_single_structure(&lt;br /&gt;
                array(&lt;br /&gt;
                    &#039;id&#039; =&amp;gt; new external_value(PARAM_INT, &#039;group record id&#039;),&lt;br /&gt;
                    &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                    &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                    &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                    &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Required, Optional or Default value ===&lt;br /&gt;
A value can be VALUE_REQUIRED, VALUE_OPTIONAL, or VALUE_DEFAULT. If not mentioned, a value is VALUE_REQUIRED by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;                 &lt;br /&gt;
                            &#039;yearofstudy&#039; =&amp;gt; new external_value(PARAM_INT, &#039;year of study&#039;,VALUE_DEFAULT, 1979),                        &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* VALUE_REQUIRED - if the value is not supplied =&amp;gt; the server throws an error message&lt;br /&gt;
* VALUE_OPTIONAL - if the value is not supplied =&amp;gt; the value is ignored. Note that VALUE_OPTIONAL can&#039;t be used in top level parameters, it must be used only within array/objects key definition. If you need top level Optional parameters you should use VALUE_DEFAULT instead.&lt;br /&gt;
* VALUE_DEFAULT   - if the value is not supplied =&amp;gt; the default value is used&lt;br /&gt;
&lt;br /&gt;
Note: Because some web service protocols are strict about the number and types of arguments - it is not possible to specify an optional parameter as one of the top-most parameters for a function. Examples:&lt;br /&gt;
&lt;br /&gt;
Not cool:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    public static function get_biscuit_parameters() {                                                                  &lt;br /&gt;
        return new external_function_parameters(                                                                                    &lt;br /&gt;
            array(                                                                                                                  &lt;br /&gt;
                &#039;chocolatechips&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_REQUIRED),&lt;br /&gt;
                &#039;glutenfree&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_DEFAULT, false),&lt;br /&gt;
                &#039;icingsugar&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_OPTIONAL), // ERROR! top level optional parameter!!!&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
             &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cool:&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
    public static function get_biscuit_parameters() {                                                                  &lt;br /&gt;
        return new external_function_parameters(                                                                                    &lt;br /&gt;
            array(&lt;br /&gt;
                &#039;ifeellike&#039; =&amp;gt; new external_single_structure(&lt;br /&gt;
                    array(                                                                                                                  &lt;br /&gt;
                        &#039;chocolatechips&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_REQUIRED),&lt;br /&gt;
                        &#039;glutenfree&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_DEFAULT, false),&lt;br /&gt;
                        &#039;icingsugar&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_OPTIONAL), // ALL GOOD!! We have nested the params in a external_single_structure.&lt;br /&gt;
                    )&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implement the external function ==&lt;br /&gt;
We declared our web service function and we defined the external function parameters and return values. We will now implement the external function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Create groups&lt;br /&gt;
     * @param array $groups array of group description arrays (with keys groupname and courseid)&lt;br /&gt;
     * @return array of newly created groups&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups($groups) { //Don&#039;t forget to set it as static&lt;br /&gt;
        global $CFG, $DB;&lt;br /&gt;
        require_once(&amp;quot;$CFG-&amp;gt;dirroot/group/lib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&lt;br /&gt;
        $transaction = $DB-&amp;gt;start_delegated_transaction(); //If an exception is thrown in the below code, all DB queries in this code will be rollback.&lt;br /&gt;
&lt;br /&gt;
        $groups = array();&lt;br /&gt;
&lt;br /&gt;
        foreach ($params[&#039;groups&#039;] as $group) {&lt;br /&gt;
            $group = (object)$group;&lt;br /&gt;
&lt;br /&gt;
            if (trim($group-&amp;gt;name) == &#039;&#039;) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Invalid group name&#039;);&lt;br /&gt;
            }&lt;br /&gt;
            if ($DB-&amp;gt;get_record(&#039;groups&#039;, array(&#039;courseid&#039;=&amp;gt;$group-&amp;gt;courseid, &#039;name&#039;=&amp;gt;$group-&amp;gt;name))) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            // now security checks&lt;br /&gt;
            $context = get_context_instance(CONTEXT_COURSE, $group-&amp;gt;courseid);&lt;br /&gt;
            self::validate_context($context);&lt;br /&gt;
            require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&lt;br /&gt;
            // finally create the group&lt;br /&gt;
            $group-&amp;gt;id = groups_create_group($group, false);&lt;br /&gt;
            $groups[] = (array)$group;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $transaction-&amp;gt;allow_commit();&lt;br /&gt;
&lt;br /&gt;
        return $groups;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parameter validation ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This &#039;&#039;validate_parameters&#039;&#039; function validates the external function parameters against the description. It will return an exception if some required parameters are missing, if parameters are not well-formed, and check the parameters validity. It is essential that you do this call to avoid potential hack. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important:&#039;&#039;&#039; the parameters of the external function and their declaration in the description &#039;&#039;&#039;must be the same order&#039;&#039;&#039;. In this example we have only one parameter named $groups, so we don&#039;t need to worry about the order.&lt;br /&gt;
&lt;br /&gt;
=== Context and Capability checks ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/// now security checks&lt;br /&gt;
$context = context_course::instance($group-&amp;gt;courseid);&lt;br /&gt;
self::validate_context($context);&lt;br /&gt;
require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: validate_context() is required in all external functions before operating on any data belonging to a context. This function does sanity and security checks on the context that was passed to the external function - and sets up the global $PAGE and $OUTPUT for rendering return values. Do NOT use require_login(), or $PAGE-&amp;gt;set_context() in an external function.&lt;br /&gt;
&lt;br /&gt;
=== Exceptions===&lt;br /&gt;
You can throw exceptions. These are automatically handled by Moodle web service servers.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
//Note: it is good practice to add detailled information in $debuginfo, &lt;br /&gt;
//         and only send back a generic exception message when Moodle DEBUG mode &amp;lt; NORMAL.&lt;br /&gt;
//         It&#039;s what we do here throwing the invalid_parameter_exception($debug) exception&lt;br /&gt;
throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;); &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Correct return values ===&lt;br /&gt;
The return values will be validated by the Moodle web service servers: &lt;br /&gt;
* return values contain some values not described =&amp;gt; these values will be skipped.&lt;br /&gt;
* return values miss some required values (VALUE_REQUIRED) =&amp;gt; the server will return an error.&lt;br /&gt;
* return values types don&#039;t match the description (int != PARAM_ALPHA) =&amp;gt; the server will return an error&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; cast all your returned objects into arrays.&lt;br /&gt;
&lt;br /&gt;
== Making web service accessible through Apache Thrift ==&lt;br /&gt;
This step is optional. If you wish to generate SDK for different programming languages and platforms using [http://thrift.apache.org Apache Thrift framework] then [https://bitbucket.org/hhteam/moodle_thrift_tools/wiki/Home Moodle Thrift tools] can help you. &lt;br /&gt;
&lt;br /&gt;
Two steps should be performed: &lt;br /&gt;
&lt;br /&gt;
* Generate .thrift files for Moodle API using thriftgenerator script.&lt;br /&gt;
* Generate thrift handlers for PHP and copy the php files generated to your plugin source tree.&lt;br /&gt;
&lt;br /&gt;
It is also recommended to include thrift files into the distribution of your plugin in order to simplify creation of client bindings for the users of your API.&lt;br /&gt;
&lt;br /&gt;
That&#039;s it. Now the web API of your plugin is accessible through Apache Thrift Framework.&lt;br /&gt;
&lt;br /&gt;
== Bump the plugin version ==&lt;br /&gt;
Edit your local/myplugin/version.php and increase the plugin version. This should trigger a Moodle upgrade and the new web service should be available in the administration (&#039;&#039;Administration &amp;gt; Plugins &amp;gt; Web Services &amp;gt; Manage services&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
== Deprecation ==&lt;br /&gt;
External functions deprecation process is different from the [[Deprecation|standard deprecation]]. If you are interested in deprecating any of your external functions you should create a FUNCTIONNAME_is_deprecated function in your external function class. Return true if the external function is deprecated. This is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Mark the function as deprecated.&lt;br /&gt;
     * @return bool&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_is_deprecated() {&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
=See also=&lt;br /&gt;
* [[Web_services|Web services developer documentation]]&lt;br /&gt;
* [[:en:Web_services|Web services user documentation]]&lt;br /&gt;
* [[Creating_a_web_service_client|Implement a web service client]]&lt;br /&gt;
* Code example: [https://gist.github.com/timhunt/51987ad386faca61fe013904c242e9b4 Adding a web service, using APIs] by (Tim Hunt)&lt;br /&gt;
&lt;br /&gt;
Specification:&lt;br /&gt;
* [[External services security]]&lt;br /&gt;
* [[External services description]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Web Services]]&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=58197</id>
		<title>Adding a web service to a plugin</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=58197"/>
		<updated>2021-01-12T10:44:51Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle_2.0}}&lt;br /&gt;
&lt;br /&gt;
= Quick start =&lt;br /&gt;
== Example ==&lt;br /&gt;
Have a look to the [https://github.com/moodlehq/moodle-local_wstemplate web service plugin template]. This plugin contains a web service hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.&lt;br /&gt;
== File structure ==&lt;br /&gt;
The file structure is explained in these two documents:&lt;br /&gt;
* [[Web_services_API|Web services API]]&lt;br /&gt;
* [[External_functions_API|External function API]]&lt;br /&gt;
&lt;br /&gt;
= Tutorial =&lt;br /&gt;
We will create a web service into a local plugin. This service will contain one &#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; web service function. This web service function will create a group into a Moodle course.&lt;br /&gt;
&lt;br /&gt;
== Write the specification documentation ==&lt;br /&gt;
Before starting coding, let&#039;s identify our needs writing some short specification documents.&lt;br /&gt;
&lt;br /&gt;
=== functional specification===&lt;br /&gt;
&#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; will take a list of groups as parameters and it will return the same groups with their newly created id. If ever one group creation fails, the function will throw an exception, and no creation will happen.&lt;br /&gt;
&lt;br /&gt;
=== technical specification ===&lt;br /&gt;
* &#039;&#039;&#039;the core function the external function will call&#039;&#039;&#039;:  &#039;&#039;groups_create_group()&#039;&#039; from [http://cvs.moodle.org/moodle/group/ /group/lib.php].&lt;br /&gt;
* &#039;&#039;&#039;the parameter types&#039;&#039;&#039;: a list of object. This object are groups, with id/name/courseid.&lt;br /&gt;
* &#039;&#039;&#039;the returned value types&#039;&#039;&#039;:  a list of objects (groups) with their id.&lt;br /&gt;
* &#039;&#039;&#039;the user capabilities to check&#039;&#039;&#039;: &#039;&#039;moodle/course:managegroups&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Write a simple test client ==&lt;br /&gt;
The first thing you should code is a web service test client. You will often discover use cases that you didn&#039;t think about. We are not showing any test client code here, see [[Creating_a_web_service_client|How to create a web service client]].&lt;br /&gt;
&lt;br /&gt;
== Declare the service ==&lt;br /&gt;
This step is optional. You can pre-build a service including any web service functions, so the Moodle administrator doesn&#039;t need to do it. Add into /local/myplugin/db/services.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
  $services = array(&lt;br /&gt;
      &#039;mypluginservice&#039; =&amp;gt; array(                                                // the name of the web service&lt;br /&gt;
          &#039;functions&#039; =&amp;gt; array (&#039;local_myplugin_create_groups&#039;), // web service functions of this service&lt;br /&gt;
          &#039;requiredcapability&#039; =&amp;gt; &#039;&#039;,                // if set, the web service user need this capability to access &lt;br /&gt;
                                                                              // any function of this service. For example: &#039;some/capability:specified&#039;                 &lt;br /&gt;
          &#039;restrictedusers&#039; =&amp;gt; 0,                                             // if enabled, the Moodle administrator must link some user to this service&lt;br /&gt;
                                                                              // into the administration&lt;br /&gt;
          &#039;enabled&#039; =&amp;gt; 1,                                                       // if enabled, the service can be reachable on a default installation&lt;br /&gt;
          &#039;shortname&#039; =&amp;gt;  &#039;&#039;,       // optional – but needed if restrictedusers is set so as to allow logins.&lt;br /&gt;
          &#039;downloadfiles&#039; =&amp;gt; 0,    // allow file downloads.&lt;br /&gt;
          &#039;uploadfiles&#039;  =&amp;gt; 0      // allow file uploads.&lt;br /&gt;
       )&lt;br /&gt;
  );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: it is not possible for an administrator to add/remove any function from a pre-built service.&lt;br /&gt;
&lt;br /&gt;
== Declare the web service function ==&lt;br /&gt;
Following the [[Web_services_API|Web service API]], you must declare the web service function in the &#039;&#039;local/myplugin/db/services.php&#039;&#039; file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$functions = array(&lt;br /&gt;
    &#039;local_myplugin_create_groups&#039; =&amp;gt; array(         //web service function name&lt;br /&gt;
        &#039;classname&#039;   =&amp;gt; &#039;local_myplugin_external&#039;,  //class containing the external function OR namespaced class in classes/external/XXXX.php&lt;br /&gt;
        &#039;methodname&#039;  =&amp;gt; &#039;create_groups&#039;,          //external function name&lt;br /&gt;
        &#039;classpath&#039;   =&amp;gt; &#039;local/myplugin/externallib.php&#039;,  //file containing the class/external function - not required if using namespaced auto-loading classes.&lt;br /&gt;
                                                   // defaults to the service&#039;s externalib.php&lt;br /&gt;
        &#039;description&#039; =&amp;gt; &#039;Creates new groups.&#039;,    //human readable description of the web service function&lt;br /&gt;
        &#039;type&#039;        =&amp;gt; &#039;write&#039;,                  //database rights of the web service function (read, write)&lt;br /&gt;
        &#039;ajax&#039; =&amp;gt; true,        // is the service available to &#039;internal&#039; ajax calls. &lt;br /&gt;
        &#039;services&#039; =&amp;gt; array(MOODLE_OFFICIAL_MOBILE_SERVICE)    // Optional, only available for Moodle 3.1 onwards. List of built-in services (by shortname) where the function will be included.  Services created manually via the Moodle interface are not supported.&lt;br /&gt;
        &#039;capabilities&#039; =&amp;gt; &#039;&#039;, // comma separated list of capabilities used by the function.&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Web service functions should match the [https://docs.moodle.org/dev/Web_services_Roadmap#Naming_convention naming convention].&lt;br /&gt;
&lt;br /&gt;
== Write the external function descriptions ==&lt;br /&gt;
Every web service function is mapped to an external function. External function are described in the [[External_functions_API|External functions API]].&lt;br /&gt;
Each external function is written with two other functions describing the parameters and the return values. These description functions are used by web service servers to:&lt;br /&gt;
* validate the web service function parameters&lt;br /&gt;
* validate the web service function returned values&lt;br /&gt;
* build WSDL files or other protocol documents &lt;br /&gt;
&lt;br /&gt;
These two description functions are located in the same file and the same class mentioned in local/myplugin/db/services.php.&lt;br /&gt;
&lt;br /&gt;
Thus for the web service function &#039;&#039;&#039;local_myplugin_create_groups()&#039;&#039;&#039;, we need write a class named &#039;&#039;&#039;local_myplugin_external&#039;&#039;&#039; in the file &#039;&#039;&#039;local/myplugin/externallib.php&#039;&#039;&#039;. The class will contain:&lt;br /&gt;
* create_groups(...)&lt;br /&gt;
* create_groups_parameters()&lt;br /&gt;
* create_groups_return()&lt;br /&gt;
&lt;br /&gt;
=== create_groups_parameters() ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&amp;quot;$CFG-&amp;gt;libdir/externallib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
class local_myplugin_external extends external_api {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A web service function without parameters will have a parameter description function like that:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function functionname_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
               //if I had any parameters, they would be described here. But I don&#039;t have any, so this array is empty.&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A parameter can be described as: &lt;br /&gt;
* a list               =&amp;gt; external_multiple_structure&lt;br /&gt;
* an object         =&amp;gt; external_single_structure&lt;br /&gt;
* a primary type =&amp;gt; external_value&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our create_groups() function expects one parameter named &#039;&#039;groups&#039;&#039;, so we will first write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; ...&lt;br /&gt;
                &lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This &#039;&#039;groups&#039;&#039; parameter is a list of group. So we will write :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    ...&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An external_multiple_structure object (list) can be constructed with: &lt;br /&gt;
* &#039;&#039;external_multiple_structure&#039;&#039; (list)&lt;br /&gt;
* &#039;&#039;external_single_structure&#039;&#039; (object)&lt;br /&gt;
* &#039;&#039;external_value&#039;&#039; (primary type). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For our function it will be a &#039;&#039;external_single_structure&#039;&#039;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;             &lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )           &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we obtain :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each group values is a &#039;&#039;external_value&#039;&#039; (primary type):&lt;br /&gt;
* &#039;&#039;courseid&#039;&#039; is an integer&lt;br /&gt;
* &#039;&#039;name&#039;&#039; is a string (text only, not tag)&lt;br /&gt;
* &#039;&#039;description&#039;&#039; is a string (can be anything)&lt;br /&gt;
* &#039;&#039;enrolmentkey&#039;&#039; is also a string (can be anything) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We add them to the description :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;), //the second argument is a human readable description text. This text is displayed in the automatically generated documentation.&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== create_groups_returns() ===&lt;br /&gt;
&lt;br /&gt;
It&#039;s similar to create_groups_parameters(), but instead of describing the parameters, it describes the return values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function create_groups_returns() {&lt;br /&gt;
        return new external_multiple_structure(&lt;br /&gt;
            new external_single_structure(&lt;br /&gt;
                array(&lt;br /&gt;
                    &#039;id&#039; =&amp;gt; new external_value(PARAM_INT, &#039;group record id&#039;),&lt;br /&gt;
                    &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                    &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                    &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                    &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Required, Optional or Default value ===&lt;br /&gt;
A value can be VALUE_REQUIRED, VALUE_OPTIONAL, or VALUE_DEFAULT. If not mentioned, a value is VALUE_REQUIRED by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;                 &lt;br /&gt;
                            &#039;yearofstudy&#039; =&amp;gt; new external_value(PARAM_INT, &#039;year of study&#039;,VALUE_DEFAULT, 1979),                        &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* VALUE_REQUIRED - if the value is not supplied =&amp;gt; the server throws an error message&lt;br /&gt;
* VALUE_OPTIONAL - if the value is not supplied =&amp;gt; the value is ignored. Note that VALUE_OPTIONAL can&#039;t be used in top level parameters, it must be used only within array/objects key definition. If you need top level Optional parameters you should use VALUE_DEFAULT instead.&lt;br /&gt;
* VALUE_DEFAULT   - if the value is not supplied =&amp;gt; the default value is used&lt;br /&gt;
&lt;br /&gt;
Note: Because some web service protocols are strict about the number and types of arguments - it is not possible to specify an optional parameter as one of the top-most parameters for a function. Examples:&lt;br /&gt;
&lt;br /&gt;
Not cool:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    public static function get_biscuit_parameters() {                                                                  &lt;br /&gt;
        return new external_function_parameters(                                                                                    &lt;br /&gt;
            array(                                                                                                                  &lt;br /&gt;
                &#039;chocolatechips&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_REQUIRED),&lt;br /&gt;
                &#039;glutenfree&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_DEFAULT, false),&lt;br /&gt;
                &#039;icingsugar&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_OPTIONAL), // ERROR! top level optional parameter!!!&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
             &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cool:&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
    public static function get_biscuit_parameters() {                                                                  &lt;br /&gt;
        return new external_function_parameters(                                                                                    &lt;br /&gt;
            array(&lt;br /&gt;
                &#039;ifeellike&#039; =&amp;gt; new external_single_structure(&lt;br /&gt;
                    array(                                                                                                                  &lt;br /&gt;
                        &#039;chocolatechips&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_REQUIRED),&lt;br /&gt;
                        &#039;glutenfree&#039; =&amp;gt; new external_value(PARAM_BOOL, PARAM_DEFAULT, false),&lt;br /&gt;
                        &#039;icingsugar&#039; =&amp;gt; new external_value(PARAM_BOOL, VALUE_OPTIONAL), // ALL GOOD!! We have nested the params in a external_single_structure.&lt;br /&gt;
                    )&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implement the external function ==&lt;br /&gt;
We declared our web service function and we defined the external function parameters and return values. We will now implement the external function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Create groups&lt;br /&gt;
     * @param array $groups array of group description arrays (with keys groupname and courseid)&lt;br /&gt;
     * @return array of newly created groups&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups($groups) { //Don&#039;t forget to set it as static&lt;br /&gt;
        global $CFG, $DB;&lt;br /&gt;
        require_once(&amp;quot;$CFG-&amp;gt;dirroot/group/lib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&lt;br /&gt;
        $transaction = $DB-&amp;gt;start_delegated_transaction(); //If an exception is thrown in the below code, all DB queries in this code will be rollback.&lt;br /&gt;
&lt;br /&gt;
        $groups = array();&lt;br /&gt;
&lt;br /&gt;
        foreach ($params[&#039;groups&#039;] as $group) {&lt;br /&gt;
            $group = (object)$group;&lt;br /&gt;
&lt;br /&gt;
            if (trim($group-&amp;gt;name) == &#039;&#039;) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Invalid group name&#039;);&lt;br /&gt;
            }&lt;br /&gt;
            if ($DB-&amp;gt;get_record(&#039;groups&#039;, array(&#039;courseid&#039;=&amp;gt;$group-&amp;gt;courseid, &#039;name&#039;=&amp;gt;$group-&amp;gt;name))) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            // now security checks&lt;br /&gt;
            $context = get_context_instance(CONTEXT_COURSE, $group-&amp;gt;courseid);&lt;br /&gt;
            self::validate_context($context);&lt;br /&gt;
            require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&lt;br /&gt;
            // finally create the group&lt;br /&gt;
            $group-&amp;gt;id = groups_create_group($group, false);&lt;br /&gt;
            $groups[] = (array)$group;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $transaction-&amp;gt;allow_commit();&lt;br /&gt;
&lt;br /&gt;
        return $groups;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parameter validation ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This &#039;&#039;validate_parameters&#039;&#039; function validates the external function parameters against the description. It will return an exception if some required parameters are missing, if parameters are not well-formed, and check the parameters validity. It is essential that you do this call to avoid potential hack. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important:&#039;&#039;&#039; the parameters of the external function and their declaration in the description &#039;&#039;&#039;must be the same order&#039;&#039;&#039;. In this example we have only one parameter named $groups, so we don&#039;t need to worry about the order.&lt;br /&gt;
&lt;br /&gt;
=== Context and Capability checks ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/// now security checks&lt;br /&gt;
$context = context_course::instance($group-&amp;gt;courseid);&lt;br /&gt;
self::validate_context($context);&lt;br /&gt;
require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: validate_context() is required in all external functions before operating on any data belonging to a context. This function does sanity and security checks on the context that was passed to the external function - and sets up the global $PAGE and $OUTPUT for rendering return values. Do NOT use require_login(), or $PAGE-&amp;gt;set_context() in an external function.&lt;br /&gt;
&lt;br /&gt;
=== Exceptions===&lt;br /&gt;
You can throw exceptions. These are automatically handle by Moodle web service servers.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
//Note: it is good practice to add detailled information in $debuginfo, &lt;br /&gt;
//         and only send back a generic exception message when Moodle DEBUG mode &amp;lt; NORMAL.&lt;br /&gt;
//         It&#039;s what we do here throwing the invalid_parameter_exception($debug) exception&lt;br /&gt;
throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;); &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Correct return values ===&lt;br /&gt;
The return values will be validated by the Moodle web service servers: &lt;br /&gt;
* return values contain some values not described =&amp;gt; these values will be skipped.&lt;br /&gt;
* return values miss some required values (VALUE_REQUIRED) =&amp;gt; the server will return an error.&lt;br /&gt;
* return values types don&#039;t match the description (int != PARAM_ALPHA) =&amp;gt; the server will return an error&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; cast all your returned objects into arrays.&lt;br /&gt;
&lt;br /&gt;
== Making web service accessible through Apache Thrift ==&lt;br /&gt;
This step is optional. If you wish to generate SDK for different programming languages and platforms using [http://thrift.apache.org Apache Thrift framework] then [https://bitbucket.org/hhteam/moodle_thrift_tools/wiki/Home Moodle Thrift tools] can help you. &lt;br /&gt;
&lt;br /&gt;
Two steps should be performed: &lt;br /&gt;
&lt;br /&gt;
* Generate .thrift files for Moodle API using thriftgenerator script.&lt;br /&gt;
* Generate thrift handlers for PHP and copy the php files generated to your plugin source tree.&lt;br /&gt;
&lt;br /&gt;
It is also recommended to include thrift files into the distribution of your plugin in order to simplify creation of client bindings for the users of your API.&lt;br /&gt;
&lt;br /&gt;
That&#039;s it. Now the web API of your plugin is accessible through Apache Thrift Framework.&lt;br /&gt;
&lt;br /&gt;
== Bump the plugin version ==&lt;br /&gt;
Edit your local/myplugin/version.php and increase the plugin version. This should trigger a Moodle upgrade and the new web service should be available in the administration (&#039;&#039;Administration &amp;gt; Plugins &amp;gt; Web Services &amp;gt; Manage services&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
== Deprecation ==&lt;br /&gt;
External functions deprecation process is different from the [[Deprecation|standard deprecation]]. If you are interested in deprecating any of your external functions you should create a FUNCTIONNAME_is_deprecated function in your external function class. Return true if the external function is deprecated. This is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Mark the function as deprecated.&lt;br /&gt;
     * @return bool&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_is_deprecated() {&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
=See also=&lt;br /&gt;
* [[Web_services|Web services developer documentation]]&lt;br /&gt;
* [[:en:Web_services|Web services user documentation]]&lt;br /&gt;
* [[Creating_a_web_service_client|Implement a web service client]]&lt;br /&gt;
* Code example: [https://gist.github.com/timhunt/51987ad386faca61fe013904c242e9b4 Adding a web service, using APIs] by (Tim Hunt)&lt;br /&gt;
&lt;br /&gt;
Specification:&lt;br /&gt;
* [[External services security]]&lt;br /&gt;
* [[External services description]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Web Services]]&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Task_API&amp;diff=56287</id>
		<title>Task API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Task_API&amp;diff=56287"/>
		<updated>2019-07-24T12:46:12Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Adhoc task usage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox Project&lt;br /&gt;
|name = Task&lt;br /&gt;
|state = Integrated&lt;br /&gt;
|tracker = MDL-25505&lt;br /&gt;
|discussion = https://moodle.org/mod/forum/discuss.php?d=229139&lt;br /&gt;
|assignee = Damyon&lt;br /&gt;
}}&lt;br /&gt;
{{Moodle 2.7}}&lt;br /&gt;
== Tasks ==&lt;br /&gt;
&lt;br /&gt;
A task is a unit of work that needs to be done later. Good uses for tasks:&lt;br /&gt;
&lt;br /&gt;
* Run a slow operation in the background&lt;br /&gt;
* Run a maintenance task on a regular schedule&lt;br /&gt;
&lt;br /&gt;
In general any operation that takes more than a few seconds should be a candidate for a task.&lt;br /&gt;
&lt;br /&gt;
== Benefits ==&lt;br /&gt;
&lt;br /&gt;
* Better user experience (give them feedback immediately, that their task has been queued)&lt;br /&gt;
* Prevent browser timeouts&lt;br /&gt;
* Better performance for clusters (tasks can be run on separate, non-webserving cluster node, tasks can run in parallel)&lt;br /&gt;
* Failed tasks will be retried&lt;br /&gt;
* A better user interface will prevent users queuing multiple tasks, because they thought it had &amp;quot;got stuck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Types of task ==&lt;br /&gt;
&lt;br /&gt;
=== Scheduled tasks ===&lt;br /&gt;
Scheduled tasks are tasks that will run on a regular schedule. A default schedule can be set, but admins have the ability to change the default schedule if required. Note: Tasks will only run as often as cron is run in Moodle. In 2.7 it is recommended to run cron once per minute to get the benefit from the new task scheduling (don&#039;t worry - it will do much less work each time it runs).&lt;br /&gt;
&lt;br /&gt;
=== Adhoc tasks ===&lt;br /&gt;
Adhoc tasks are for when you need to queue something to run in the background immediately, they would be executed as soon as possible. Adhoc tasks can contain custom data, specific to this specific instance of the task.&lt;br /&gt;
&lt;br /&gt;
== Usage ==&lt;br /&gt;
&lt;br /&gt;
=== Scheduled task usage ===&lt;br /&gt;
&lt;br /&gt;
Scheduled tasks are created by subclassing \core\task\scheduled_task. They also require an entry in &amp;quot;db/tasks.php&amp;quot; for your plugin.&lt;br /&gt;
&lt;br /&gt;
1. Create a subclass of \core\task\scheduled_task that contains your code to run in a schedule (within &amp;quot;&amp;lt;pluginroot&amp;gt;/classes/task/cut_my_toe_nails.php&amp;quot; for the autoloading mechanism to pick up).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace mod_hygene\task;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * An example of a scheduled task.&lt;br /&gt;
 */&lt;br /&gt;
class cut_my_toe_nails extends \core\task\scheduled_task {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the task&#039;s name as shown in admin screens.&lt;br /&gt;
     *&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;cutmytoenails&#039;, &#039;mod_hygene&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute the task.&lt;br /&gt;
     */&lt;br /&gt;
    public function execute() {&lt;br /&gt;
        // Apply fungus cream.&lt;br /&gt;
        // Apply chainsaw.&lt;br /&gt;
        // Apply olive oil.&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
2. Create entry in db/tasks.php for your plugin (then a version bump to your plugin will install the task):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$tasks = [&lt;br /&gt;
    [&lt;br /&gt;
        &#039;classname&#039; =&amp;gt; &#039;mod_hygene\task\cut_my_toe_nails&#039;,&lt;br /&gt;
        &#039;blocking&#039; =&amp;gt; 0,&lt;br /&gt;
        &#039;minute&#039; =&amp;gt; &#039;30&#039;,&lt;br /&gt;
        &#039;hour&#039; =&amp;gt; &#039;17&#039;,&lt;br /&gt;
        &#039;day&#039; =&amp;gt; &#039;*&#039;,&lt;br /&gt;
        &#039;month&#039; =&amp;gt; &#039;1,7&#039;,&lt;br /&gt;
        &#039;dayofweek&#039; =&amp;gt; &#039;0&#039;,&lt;br /&gt;
    ],&lt;br /&gt;
];&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The fields required in the db/tasks.php file are:&lt;br /&gt;
* classname - the fully namespaced classname of your task. This needs to comply with the autoloading rules.&lt;br /&gt;
* blocking - if this is set to 1, no other scheduled task will run at the same time as this task. Do not set this to 1 unless you really need it as it will impact the performance of the task queue.&lt;br /&gt;
* minute, hour, day, dayofweek, month - This is the default schedule for running the task. The syntax matches the syntax of unix cron. Starting with Moodle 2.8, the minute and hour values accept a special &#039;R&#039; syntax which causes a random value to be set in the database at install time (useful to avoid overloading web services).&lt;br /&gt;
&lt;br /&gt;
3. Increase the version number in version.php for your plugin and visit the Site &amp;quot;Administration -&amp;gt; Notifications&amp;quot; page to install the new task.&lt;br /&gt;
&lt;br /&gt;
4. (Optional - since 2.8) Run this task even when the plugin is disabled. In rare cases, you may want the scheduled tasks for a plugin to run, even when the plugin is disabled. Some of the enrolment plugins do this to clean up data. If this is the case, the scheduled task must override the  &amp;quot;get_run_if_component_disabled()&amp;quot; method and return true instead of false. If they do not do this, the scheduled task will not be run while the plugin is disabled.&lt;br /&gt;
&lt;br /&gt;
When called from the command line for testing purposes errors can be hidden and a misleading error about locks can be displayed. To get at the details of an error Moodle 3.2 supports a showdebugging option that will show the actual errors. Read about it here&lt;br /&gt;
https://docs.moodle.org/32/en/Administration_via_command_line#Scheduled_tasks&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Cron syntax examples ====&lt;br /&gt;
* day - Day of month field for task schedule.&lt;br /&gt;
** &amp;quot;*&amp;quot; - Every day&lt;br /&gt;
** &amp;quot;*/2&amp;quot; - Every 2nd day&lt;br /&gt;
** &amp;quot;1&amp;quot; - The first of every month&lt;br /&gt;
** &amp;quot;1,15&amp;quot; - The first and fifteenth of every month&lt;br /&gt;
* dayofweek - Day of week field for task schedule.&lt;br /&gt;
** &amp;quot;*&amp;quot; - Every day&lt;br /&gt;
** &amp;quot;0&amp;quot; - Every Sunday&lt;br /&gt;
** &amp;quot;6&amp;quot; - Every Saturday&lt;br /&gt;
** &amp;quot;1,5&amp;quot; - Every Monday and Friday&lt;br /&gt;
* hour - Hour field for task schedule.&lt;br /&gt;
** &amp;quot;*&amp;quot; - Every hour&lt;br /&gt;
** &amp;quot;*/2&amp;quot; - Every 2 hours&lt;br /&gt;
** &amp;quot;2-10&amp;quot; - Every hour from 2am until 10am (inclusive)&lt;br /&gt;
** &amp;quot;2,6,9&amp;quot; - 2am, 6am and 9am&lt;br /&gt;
** &amp;quot;R&amp;quot; - A random value between 0 and 23 is chosen at task install time (or when reset to default) [Supported since Moodle 2.8]&lt;br /&gt;
* minute - Minute field for task schedule.&lt;br /&gt;
** &amp;quot;*&amp;quot; - Every minute&lt;br /&gt;
** &amp;quot;*/5&amp;quot; - Every 5 minutes&lt;br /&gt;
** &amp;quot;2-10&amp;quot; - Every minute between 2 and 10 minutes past the hour (inclusive)&lt;br /&gt;
** &amp;quot;2,6,9&amp;quot; - 2, 6 and 9 minutes past the hour&lt;br /&gt;
** &amp;quot;R&amp;quot; - A random value between 0 and 59 is chosen at task install time (or when reset to default) [Supported since Moodle 2.8]&lt;br /&gt;
* month - Month field for task schedule.&lt;br /&gt;
** &amp;quot;*&amp;quot; - Every month&lt;br /&gt;
** &amp;quot;*/2&amp;quot; - Every second month&lt;br /&gt;
** &amp;quot;1&amp;quot; - Every January&lt;br /&gt;
** &amp;quot;1,5&amp;quot; - Every January and May&lt;br /&gt;
&lt;br /&gt;
=== Adhoc task usage ===&lt;br /&gt;
&lt;br /&gt;
This is even easier than scheduled tasks.&lt;br /&gt;
&lt;br /&gt;
1. Create a subclass of \core\task\adhoc_task that contains your code to run in the background.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class take_over_the_world extends \core\task\adhoc_task {&lt;br /&gt;
    public function execute() {&lt;br /&gt;
        // gain 100,000,000 friends on facebook.&lt;br /&gt;
        // crash the stock market.&lt;br /&gt;
        // run for president.&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Create an instance of the task and queue it (adding custom data if required).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   // create the instance&lt;br /&gt;
   $domination = new take_over_the_world();&lt;br /&gt;
   // set blocking if required (it probably isn&#039;t)&lt;br /&gt;
   // $domination-&amp;gt;set_blocking(true);&lt;br /&gt;
   // add custom data&lt;br /&gt;
   $domination-&amp;gt;set_custom_data(array(&lt;br /&gt;
       &#039;plansfortomorrownight&#039; =&amp;gt; &#039;The same thing we do every night, Pinky!&#039;&lt;br /&gt;
   ));&lt;br /&gt;
&lt;br /&gt;
   // queue it&lt;br /&gt;
   \core\task\manager::queue_adhoc_task($domination);&lt;br /&gt;
&lt;br /&gt;
   // profit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. There is no 3.&lt;br /&gt;
&lt;br /&gt;
By default, your task will run as admin. If you would like it to run as a specific user, call set_userid().&lt;br /&gt;
&lt;br /&gt;
=== Failures ===&lt;br /&gt;
A task, either scheduled or adhoc can sometimes fail. An example would be updating an RSS field when the network is temporarily down. This is handled by the task system automatically - all the failing task needs to do is throw an exception. The task will be retried after 1 minute. If the task keeps failing, the retry algorithm will add more time between each successive attempts up to a max of 24 hours.&lt;br /&gt;
&lt;br /&gt;
=== Caches ===&lt;br /&gt;
There is one special case that needs to be considered with this new system. If a particular scheduled or adhoc task runs for a long time and updates many DB records - particularly something related to enrolment - the next task in the queue may suffer because various API&#039;s in moodle use static caching to speed up requests, but assume that the data will not change much between the start and end of the request. In this case, you can force the cron to exit after running a task that has done many DB updates. The next cron will be run in the next minute, and will have all static caches cleared because it&#039;s a new process. To do this call \core\task\manager::clear_static_caches();&lt;br /&gt;
&lt;br /&gt;
=== Security ===&lt;br /&gt;
When scheduling a task to run in the background - or creating a scheduled task, the task will run in the context of the cron user (see &amp;quot;cron_setup_user()&amp;quot;). If you need to perform access checks in your background task, you should pass the userid/context in custom_data and then pass that userid to the access check functions (&amp;quot;require_capability(&#039;moodle/course:update&#039;,  $context, $userid)&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== Legacy cron ===&lt;br /&gt;
The older syntax of cron.php or modname_cron() is still supported - but is not as good as this new API. This is because:&lt;br /&gt;
* the legacy cron functions run serially - a long running cron in one plugin will hold up the other plugins crons&lt;br /&gt;
* the legacy cron functions are fragile - a failure in one cron in one plugin will prevent the cron in other functions from running at all&lt;br /&gt;
* the scheduling cannot be changed by admins&lt;br /&gt;
&lt;br /&gt;
Note that Legacy cron has been deprecated for Moodle 3.5 (MDL-52846) and will be deleted for Moodle 3.9 (MDL-61165).&lt;br /&gt;
&lt;br /&gt;
=== Generating output ===&lt;br /&gt;
Since Moodle 3.5 it is safe to use the [[Output_API]] in cron tasks. Prior to this there may be cases where the Output API has not been initialised.&lt;br /&gt;
These issues were addressed in MDL-61800 and were also backported to Moodle 3.4.3 ,and Moodle 3.3.3.&lt;br /&gt;
&lt;br /&gt;
== For Admins ==&lt;br /&gt;
Admins have a new screen where they can adjust the schedules for any scheduled task. They can also reset any scheduled task to its default schedule.&lt;br /&gt;
[[File:task_admin_screenshot.png|Screenshot of admin page]]&lt;br /&gt;
&lt;br /&gt;
 NOTE: You can also run [https://docs.moodle.org/32/en/Administration_via_command_line#Scheduled_tasks Scheduled tasks] via command line, individually.&lt;br /&gt;
&lt;br /&gt;
== Specification ==&lt;br /&gt;
The specification for this feature is here: https://docs.moodle.org/dev/Scheduled_Tasks_Proposal&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Plugins_adoption_programme&amp;diff=56216</id>
		<title>Plugins adoption programme</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Plugins_adoption_programme&amp;diff=56216"/>
		<updated>2019-07-02T15:38:54Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Plugins development}}&lt;br /&gt;
&lt;br /&gt;
The Plugins adoption programme is a process for making it clear that a plugin is orphaned and is looking for a new maintainer. The programme is one of the mechanisms helping to minimise the risks of relying on additional plugins. The programme helps to find new maintainers for plugins whose original authors can&#039;t work on the plugin fully any more.&lt;br /&gt;
&lt;br /&gt;
The programmes started in 2014 by announcing it in [https://moodle.org/mod/forum/discuss.php?d=260354#p1128482 a forum post] and since then, many plugins found their new maintainers since then through it.&lt;br /&gt;
&lt;br /&gt;
=== Motivation ===&lt;br /&gt;
&lt;br /&gt;
Having an additional plugin installed at a Moodle site is always a risk. One of the essential aspects (apart from the code quality itself) that potential plugin users have to consider is how well the plugin is being maintained. Does the maintainer release regular updates and bug fixes? Is the plugin updated every six months for the new Moodle major release? Is there a place to report bugs and feature requests? And when reported, are they reflected?&lt;br /&gt;
&lt;br /&gt;
It&#039;s not that difficult to write a new Moodle plugin these days. Many students do that as a part of their school or thesis projects, for example. But can one rely on the author of plugin to provide sufficient (or at least some) support for it? To be a responsible maintainer of a plugin is much harder than to be an author of it. Many maintainers work on their plugins in their free time. And even if they are lucky enough to be paid for doing that, it&#039;s just time consuming (as everything).&lt;br /&gt;
&lt;br /&gt;
At certain moment, maintainers can realise they are not able to give enough love to their plugins any more. In the essay The Cathedral and the Bazaar, Eric Steven Raymond says &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;quot;When you lose interest in a program, your last duty to it is to hand it off to a competent successor.&amp;quot;&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And that is what Moodle plugins adoption programme is about. &lt;br /&gt;
&lt;br /&gt;
=== Programme rules for plugin maintainers ===&lt;br /&gt;
&lt;br /&gt;
# It is not a shame to give up on a plugin maintenance. Unmaintained plugin is worse than no plugin. We appreciate that you as the original author do not want to harm Moodle reputation just because your old code broke someone&#039;s site.&lt;br /&gt;
# If you decide to offer your plugin for adoption, let the world know via posting into the [https://moodle.org/mod/forum/view.php?id=8149 Plugins traffic forum].&lt;br /&gt;
# Your plugin will be put into a [https://moodle.org/plugins/?q=set:maintainer-needed special set in the Plugins directory].&lt;br /&gt;
# Once there is a volunteer who would like to take over the maintenance, please reply to the forum. It will help if the candidate proves their skills via a reference or a patch for existing issue etc. So we all know the plugin is passed over to good hands.&lt;br /&gt;
# Finally, the successor is given the lead maintainer role for the plugin with all the permissions (edit the plugin record, release new versions etc). The previous maintainer will be still listed as the original author in the directory.&lt;br /&gt;
&lt;br /&gt;
=== Applying to become a maintainer ===&lt;br /&gt;
&lt;br /&gt;
If you would like to become a new maintainer of a plugin that has been put up for the adoption, please reply to the relevant post in the [https://moodle.org/mod/forum/view.php?id=8149 Plugins traffic forum]. It will help to demonstrate that you would be able to maintain the plugin - ideally with existing pull requests or other contributions to the plugin.&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
* The @author tag in the phpDocs block of a file should never be changed even after the whole file is rewritten eventually. It&#039;s GPL legal statement, not a credits line.&lt;br /&gt;
* If the plugin was originally using Github as its repository, it is recommended to transfer the ownership of the repo. That way, all the reported issues in the github tracker, pull requests and all other information is kept.&lt;br /&gt;
&lt;br /&gt;
=== Forced adoption ===&lt;br /&gt;
&lt;br /&gt;
As discussed in MDLSITE-5354, there are cases when a plugin becomes effectively orphaned due to maintainer&#039;s inactivity. In most cases, we can get in touch with the maintainer and agree on putting the plugin up for adoption. In rare cases when the maintainer can&#039;t be reached, the following procedure applies.&lt;br /&gt;
&lt;br /&gt;
* Given that the maintainer has not logged in to the moodle.org site for 90 or more days&lt;br /&gt;
* When the maintainer does not reply to an email sent to their address (obtained from their moodle.org user profile, last commit message and tracker account) within a period of 30 days&lt;br /&gt;
* And the maintainer does not reply to a personal message sent to them via moodle.org messaging features within a period of 30 days&lt;br /&gt;
* Then plugins directory curators can consider the plugin maintainer disappeared and the plugin orphaned. It is then allowed to put the plugin up for adoption on behalf of the maintainer or assign it to another maintainer.&lt;br /&gt;
&lt;br /&gt;
=== Initiating forced adoption ===&lt;br /&gt;
&lt;br /&gt;
If you are aware of a plugin that seems abandoned and you would like to help and become a new maintainer of it:&lt;br /&gt;
&lt;br /&gt;
* Try to contact the current maintainer and let them know about this Plugins adoption programme. It is always better to have the plugin put for adoption by the current maintainers.&lt;br /&gt;
* If you do not get any response within a reasonable period, please publish your offer and intention via a new post in the [https://moodle.org/mod/forum/view.php?id=8149 Plugins traffic forum] to make the community aware of it.&lt;br /&gt;
* The plugins directory curators are subscribed to that forum and will be notified about your post there. They will help you with further process.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Time_API&amp;diff=55885</id>
		<title>Time API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Time_API&amp;diff=55885"/>
		<updated>2019-04-04T14:12:42Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
Internally Moodle always stores all times in unixtime format (number of seconds since epoch) which is independent of timezones.&lt;br /&gt;
&lt;br /&gt;
The Time API is used to display proper date-time depending on user or site timezones.&lt;br /&gt;
&lt;br /&gt;
==Functions==&lt;br /&gt;
There is a class in Moodle to handle most needs of working with times. There are 2 cases to consider when working with time:&lt;br /&gt;
&lt;br /&gt;
System Time&lt;br /&gt;
&lt;br /&gt;
This is when you are dealing with dates on the server e.g. executing scheduled tasks, performing background tasks - anything which does not depend on the timezone of any specific user.&lt;br /&gt;
&lt;br /&gt;
User Time&lt;br /&gt;
&lt;br /&gt;
This is when you are manipulating dates and times and you need to display them to the user in their current timezone (which may be different for each user).&lt;br /&gt;
&lt;br /&gt;
The main API for time is in the class &amp;quot;core_date&amp;quot; which will give you php DateTimeZone objects for either user time or server time as needed. You can then use the php datetime classes to manipulate the time. When finished manipulating the time, get a timestamp with DateTime::getTimestamp().&lt;br /&gt;
&lt;br /&gt;
Example: Get the current server time + 1 day.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$tomorrow = new DateTime(&amp;quot;1 day&amp;quot;, core_date::get_server_timezone_object());&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get a timestamp for storing in the database:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$tomorrowint = $tomorrow-&amp;gt;getTimestamp();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get a timestamp for 3pm tomorrow in the current users timezone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$time = new DateTime(&amp;quot;now&amp;quot;, core_date::get_user_timezone_object());&lt;br /&gt;
$time-&amp;gt;add(new DateInterval(&amp;quot;P1D&amp;quot;));&lt;br /&gt;
$time-&amp;gt;setTime(15, 0, 0);&lt;br /&gt;
&lt;br /&gt;
$timestamp = $time-&amp;gt;getTimestamp();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember! NEVER NEVER NEVER add or subtract timestamps for any reason - you will get it wrong (DST is a killer)!&lt;br /&gt;
&lt;br /&gt;
Other functions related to time api can be found in lib/moodlelib.php. &lt;br /&gt;
# Time API&#039;s for current user&lt;br /&gt;
#* &#039;&#039;&#039;make_timestamp&#039;&#039;&#039; - Given date-time, it produces a GMT timestamp for current user.&lt;br /&gt;
#* &#039;&#039;&#039;userdate&#039;&#039;&#039; - Gets formatted string that represents a date in user time (note that the format required by this function is the [https://www.php.net/manual/en/function.strftime.php strftime()] format, not the more common format used by date())&lt;br /&gt;
#* &#039;&#039;&#039;usertime&#039;&#039;&#039; - Given a GMT timestamp (seconds since epoch), offsets it by the timezone.  eg 3pm in India is 3pm GMT - 5.5 * 3600 seconds&lt;br /&gt;
#* &#039;&#039;&#039;usergetdate&#039;&#039;&#039; - Given a timestamp in GMT, returns an array that represents the date-time in user time&lt;br /&gt;
#* &#039;&#039;&#039;usergetmidnight&#039;&#039;&#039; - Given a date, return the GMT timestamp of the most recent midnight for the current user.&lt;br /&gt;
#* &#039;&#039;&#039;usertimezone&#039;&#039;&#039; - Returns current user&#039;s timezone&lt;br /&gt;
&lt;br /&gt;
# System Time API&lt;br /&gt;
#* &#039;&#039;&#039;format_time&#039;&#039;&#039; - Format a date/time (seconds) as weeks, days, hours etc as needed&lt;br /&gt;
#* &#039;&#039;&#039;dst_offset_on&#039;&#039;&#039; - Calculates the Daylight Saving Offset for a given date/time (timestamp)&lt;br /&gt;
#* &#039;&#039;&#039;find_day_in_month&#039;&#039;&#039; - Calculates when the day appears in specific month&lt;br /&gt;
#* &#039;&#039;&#039;days_in_month&#039;&#039;&#039; - Calculate number of days in a given month&lt;br /&gt;
#* &#039;&#039;&#039;dayofweek&#039;&#039;&#039; - Calculate the position in the week of a specific calendar day&lt;br /&gt;
&lt;br /&gt;
# Older legacy date/time functions. Do not use in new code.&lt;br /&gt;
#* &#039;&#039;&#039;usertime&#039;&#039;&#039; - Appends the users timezone offset to an integer timestamp&lt;br /&gt;
#* &#039;&#039;&#039;get_timezone_offset&#039;&#039;&#039; - Systems&#039;s timezone difference from GMT in seconds&lt;br /&gt;
#* &#039;&#039;&#039;get_user_timezone_offset&#039;&#039;&#039; - Returns user&#039;s timezone difference from GMT in hours&lt;br /&gt;
#* &#039;&#039;&#039;dst_changes_for_year&#039;&#039;&#039; -  Calculates the required DST change and returns a Timestamp Array&lt;br /&gt;
&lt;br /&gt;
==Glossary==&lt;br /&gt;
===Timezone===&lt;br /&gt;
Moodle supports following timezone formats:&lt;br /&gt;
# UTC (specifically UTC−11 to UTC+11)&lt;br /&gt;
# Time offsets from UTC (int +-(0-13) or float +-(0.5-12.5))&lt;br /&gt;
# World timezones (Australia/Perth)&lt;br /&gt;
&lt;br /&gt;
===Location===&lt;br /&gt;
Timezone depends on [[:en:Location]] of the user and can be forced upon by administrator.&lt;br /&gt;
===DST===&lt;br /&gt;
DST is abbreviation of &#039;&#039;&#039;Day light saving&#039;&#039;&#039;. Many countries, and sometimes just certain regions of countries, adopt daylight saving time (also known as &amp;quot;Summer Time&amp;quot;) during part of the year. Moodle automatically calculates DST for current user, depending on user location.&lt;br /&gt;
==Examples==&lt;br /&gt;
===Time API&#039;s for current user===&lt;br /&gt;
Prints a formatted date in user time&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Get current day, month and year for current user.&lt;br /&gt;
$date = new DateTime(&amp;quot;now&amp;quot;, core_date::get_user_timezone_object());&lt;br /&gt;
$date-&amp;gt;setTime(0, 0, 0);&lt;br /&gt;
// Print formatted date in user time.&lt;br /&gt;
echo userdate($date-&amp;gt;getTimestamp(), &amp;quot;Current user time&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===System Time API===&lt;br /&gt;
Find the day of the week for the first day in this month.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$now = new DateTime(&amp;quot;now&amp;quot;, core_date::get_server_timezone_object());&lt;br /&gt;
&lt;br /&gt;
$year = $now-&amp;gt;format(&#039;Y&#039;);&lt;br /&gt;
$month = $now-&amp;gt;format(&#039;m&#039;);&lt;br /&gt;
&lt;br /&gt;
$now-&amp;gt;setDate($year, $month, 1);&lt;br /&gt;
$dayofweek = $now-&amp;gt;format(&#039;N&#039;);&lt;br /&gt;
echo $dayofweek;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[http://php.net/manual/en/class.datetime.php Php DateTime class]]&lt;br /&gt;
* [[Core APIs]]&lt;br /&gt;
&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Persistent&amp;diff=55596</id>
		<title>Persistent</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Persistent&amp;diff=55596"/>
		<updated>2019-02-22T08:51:45Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 3.3}}Moodle persistents are equivalent to models (or active records). They represent an object stored in the database and provide the methods to create, read, update, and delete those objects. Additionally, the persistents validate their own data against automatic and custom validation rules. Persistents are made by extending the abstract class &#039;&#039;core\persistent&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== Properties ==&lt;br /&gt;
&lt;br /&gt;
Each class must define the properties that it contains. This can be done by defining the method &#039;&#039;protected static define_properties()&#039;&#039;. It returns an array where the keys are the names of the properties (and implies the database column names), and their attributes. The &#039;&#039;type&#039;&#039; of each property must be specified using one of the &#039;&#039;PARAM_*&#039;&#039; constants. This type will be used to automatically validate the property&#039;s value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Return the definition of the properties of this model.&lt;br /&gt;
 *&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected static function define_properties() {&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
        )&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Properties attributes ===&lt;br /&gt;
&lt;br /&gt;
;type&lt;br /&gt;
: The only mandatory attribute. It must be one of the many &#039;&#039;PARAM_*&#039;&#039; constants.&lt;br /&gt;
;default&lt;br /&gt;
: The default value to attach to the property when it hasn&#039;t been provided. Without a &#039;&#039;default&#039;&#039;, the property&#039;s value is required. Alternatively this can be a &#039;&#039;Closure&#039;&#039; returning the default value.&lt;br /&gt;
;null&lt;br /&gt;
: Either of the constants &#039;&#039;NULL_ALLOWED&#039;&#039; or &#039;&#039;NULL_NOT_ALLOWED&#039;&#039; determining if the null value is accepted. This defaults to &#039;&#039;NULL_NOT_ALLOWED&#039;&#039;.&lt;br /&gt;
;message&lt;br /&gt;
: The default error message (as a &#039;&#039;lang_string&#039;&#039; instance) to return when the validation of this field fails.&lt;br /&gt;
;choices&lt;br /&gt;
: An array of values which the property must fall in.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&#039;messageformat&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
    &#039;default&#039; =&amp;gt; FORMAT_PLAIN,&lt;br /&gt;
    &#039;choices&#039; =&amp;gt; array(FORMAT_PLAIN, FORMAT_HTML, FORMAT_MOODLE, FORMAT_MARKDOWN)&lt;br /&gt;
),&lt;br /&gt;
&#039;location&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT,&lt;br /&gt;
    &#039;null&#039; =&amp;gt; NULL_ALLOWED,&lt;br /&gt;
    &#039;message&#039; =&amp;gt; new lang_string(&#039;invaliddata&#039;, &#039;error&#039;),&lt;br /&gt;
    &#039;default&#039; =&amp;gt; function() {&lt;br /&gt;
        return get_config(&#039;core&#039;, &#039;default_location&#039;);&lt;br /&gt;
    },&lt;br /&gt;
),&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that you should always use a &#039;&#039;Closure&#039;&#039; for the &#039;&#039;default&#039;&#039; value when you cannot guarantee that it will not change since the start of the request. The list of properties and their attributes is cached, and so failure to use a &#039;&#039;Closure&#039;&#039; can result in using an outdated default value.&lt;br /&gt;
&lt;br /&gt;
=== Mandatory properties ===&lt;br /&gt;
&lt;br /&gt;
Four fields are always added to your persistent and should be reflected in your database table. You must not define those properties in &#039;&#039;define_properties()&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
;id (non-null integer)&lt;br /&gt;
: The primary key of the record.&lt;br /&gt;
;usermodified (non-null integer)&lt;br /&gt;
: The user who created/modified the object. It is automatically set.&lt;br /&gt;
;timecreated (non-null integer)&lt;br /&gt;
: The timestamp at which the record was modified. It is automatically set.&lt;br /&gt;
;timemodified (non-null integer)&lt;br /&gt;
: The timestamp at which the record was modified. It is automatically set, and defaults to 0.&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.7 onwards, the XMLDB tool has an option to add these fields to your table.&lt;br /&gt;
&lt;br /&gt;
== Linking to a database table ==&lt;br /&gt;
&lt;br /&gt;
While the persistent class is helpful for database interactions, it does not automatically fetch the properties from the database, nor does it create the tables. You will need to create the table yourself, as well as pointing the persistent to it. This can be done by defining the class constant &#039;&#039;TABLE&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/** Table name for the persistent. */&lt;br /&gt;
const TABLE = &#039;status&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table name must not contain the Moodle prefix. Also it is common practice to always refer to your table name using the constant.&lt;br /&gt;
&lt;br /&gt;
== Reading property values ==&lt;br /&gt;
&lt;br /&gt;
This can be done using the method &#039;&#039;get()&#039;&#039;. Alternatively you can also use &#039;&#039;to_record()&#039;&#039; which exports the whole object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Create a new object.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
&lt;br /&gt;
// Get the user ID using the generic getter.&lt;br /&gt;
$userid = $persistent-&amp;gt;get(&#039;userid&#039;);&lt;br /&gt;
&lt;br /&gt;
// Get all the properties in an stdClass.&lt;br /&gt;
$data = $persistent-&amp;gt;to_record();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may add a new custom getter to implement your own custom logic. To do this just add a new method get_propertyname() and it will be called from the generic get() method. For instance you could convert the data in another format automatically as a convenience for developers. However, use this sparingly as it may lead to confusion: what you get is not what is stored in the database. Also - to get the actual value from a custom getter - call raw_get(). Calling get() from a custom getter will generate a segfault due to infinite recursion.&lt;br /&gt;
&lt;br /&gt;
It is, however, encouraged to add convenience methods such as the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Returns the user object of the author.&lt;br /&gt;
 *&lt;br /&gt;
 * @return stdClass&lt;br /&gt;
 */&lt;br /&gt;
public function get_author() {&lt;br /&gt;
    return core_user::get_user($this-&amp;gt;get(&#039;userid&#039;));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Assigning values to properties ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to do so.&lt;br /&gt;
&lt;br /&gt;
You use an object (&#039;&#039;stdClass&#039;&#039;) to assign a bunch of properties at once. Use it with the constructor, or the method &#039;&#039;from_record()&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$data = (object) array(&#039;userid&#039; =&amp;gt; 2, &#039;message&#039; =&amp;gt; &#039;Hello world!&#039;);&lt;br /&gt;
&lt;br /&gt;
// Instantiates a new object with value for some properties.&lt;br /&gt;
$persistent = new status(0, $data);&lt;br /&gt;
&lt;br /&gt;
// Is similar to.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
$persistent-&amp;gt;from_record($data);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, you can use the magic setters &#039;&#039;set_&#039;&#039; followed by the property name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Instantiates a blank object.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
&lt;br /&gt;
// Assign a new value to the &#039;message&#039; property.&lt;br /&gt;
$persistent-&amp;gt;set(&#039;message&#039;, &#039;Hello new world!&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Defining a custom setter ===&lt;br /&gt;
&lt;br /&gt;
Though you don&#039;t have to for the code to work, you can define your own &#039;&#039;setter&#039;&#039; methods which will be called from the generic setter. This is useful if you want to extract the data out of a more complex object prior to assigning it. Though note that those setters will then have to use the &#039;&#039;raw_set()&#039;&#039; method to assign the values (if you do not you will see a segfault because of infinite recursion).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Convenience method to set the user ID.&lt;br /&gt;
 *&lt;br /&gt;
 * @param object|int $idorobject The user ID, or a user object.&lt;br /&gt;
 */&lt;br /&gt;
public function set_userid($idorobject) {&lt;br /&gt;
    $userid = $idorobject;&lt;br /&gt;
    if (is_object($idorobject)) {&lt;br /&gt;
        $userid = $idorobject-&amp;gt;id;&lt;br /&gt;
    }&lt;br /&gt;
    $this-&amp;gt;raw_set(&#039;userid&#039;, $userid);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example we will accept an object or an ID, as a convenience for developers we will extract the ID value out of the object passed if any.&lt;br /&gt;
&lt;br /&gt;
Just like custom getters, must be careful so that the following code should not result in a change in the DB.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$value = $persistent-&amp;gt;get(&#039;property&#039;);&lt;br /&gt;
$persistent-&amp;gt;set(&#039;property&#039;, $value);&lt;br /&gt;
$persistent-&amp;gt;update();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can obviously create your own setters which aren&#039;t based on any properties just as a convenience. For instance we could have created &#039;&#039;set_userid_from_user(object $user)&#039;&#039; which is more verbose and more predictable&lt;br /&gt;
&lt;br /&gt;
== Read, save and delete entries ==&lt;br /&gt;
&lt;br /&gt;
The methods to &#039;&#039;create&#039;&#039;, &#039;&#039;read&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; are eponymous. Your object will be validated before you &#039;&#039;create&#039;&#039; or &#039;&#039;update&#039;&#039; it. The &#039;&#039;update&#039;&#039;, &#039;&#039;delete&#039;&#039; and &#039;&#039;read&#039;&#039; methods require your object to contain its ID. And you also won&#039;t be allowed to &#039;&#039;create&#039;&#039; an entry which already had an ID defined.&lt;br /&gt;
&lt;br /&gt;
Here are some code examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Fetches an object from database based on its ID.&lt;br /&gt;
$id = 123;&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
&lt;br /&gt;
// Create object in the database.&lt;br /&gt;
$data = new stdClass();&lt;br /&gt;
$data-&amp;gt;message = &#039;Hello new world&#039;;&lt;br /&gt;
$persistent = new persistent(0, $data);&lt;br /&gt;
$persistent-&amp;gt;create();&lt;br /&gt;
// $persistent-&amp;gt;get(&#039;id&#039;) will now return an id.&lt;br /&gt;
&lt;br /&gt;
// Load an object from the database, and update it.&lt;br /&gt;
$id = 123;&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
$persistent-&amp;gt;set(&#039;message&#039;, &#039;Hello new world!&#039;);&lt;br /&gt;
$persistent-&amp;gt;update();&lt;br /&gt;
&lt;br /&gt;
// Reset the instance to the values in the database.&lt;br /&gt;
$persistent-&amp;gt;read();&lt;br /&gt;
&lt;br /&gt;
// Permanently delete the object from the database.&lt;br /&gt;
$persistent-&amp;gt;delete();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fetching records ==&lt;br /&gt;
&lt;br /&gt;
Once you start using persistents you should never directly interact with the database outside of your class. The persistent class comes with a few handy methods allowing you to retrieve your objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Use the constructor to fetch one object from its ID.&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
&lt;br /&gt;
// Get one record from a set of conditions.&lt;br /&gt;
$persistent = status::get_record([&#039;userid&#039; =&amp;gt; $userid, &#039;message&#039; =&amp;gt; &#039;Hello world!&#039;]);&lt;br /&gt;
&lt;br /&gt;
// Get multiple records from a set of conditions.&lt;br /&gt;
$persistents = status::get_records([&#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
&lt;br /&gt;
// Count the records.&lt;br /&gt;
$count = status::count_records([&#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
&lt;br /&gt;
// Check whether a record exists.&lt;br /&gt;
$exists = status::record_exists($id);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make sure to also check their additional parameters and their variants (&#039;&#039;record_exists_select()&#039;&#039;, &#039;&#039;count_records_select&#039;&#039;, &#039;&#039;get_records_select&#039;&#039;, ...).&lt;br /&gt;
&lt;br /&gt;
=== Custom fetching ===&lt;br /&gt;
&lt;br /&gt;
It&#039;s always a good idea to add more complex queries directly within your persistent. By convention you should always return an instance of your persistent and never an stdClass. Here we add a custom method which allows to directly fetch all records by username.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get all records from a user&#039;s username.&lt;br /&gt;
 *&lt;br /&gt;
 * @param string $username The username.&lt;br /&gt;
 * @return status[]&lt;br /&gt;
 */&lt;br /&gt;
public static function get_records_by_username($username) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
    $sql = &#039;SELECT s.*&lt;br /&gt;
              FROM {&#039; . static::TABLE . &#039;} s&lt;br /&gt;
              JOIN {user} u&lt;br /&gt;
                ON u.id = s.userid&lt;br /&gt;
             WHERE u.username = :username&#039;;&lt;br /&gt;
&lt;br /&gt;
    $persistents = [];&lt;br /&gt;
&lt;br /&gt;
    $recordset = $DB-&amp;gt;get_recordset_sql($sql, [&#039;username&#039; =&amp;gt; $username]);&lt;br /&gt;
    foreach ($recordset as $record) {&lt;br /&gt;
        $persistents[] = new static(0, $record);&lt;br /&gt;
    }&lt;br /&gt;
    $recordset-&amp;gt;close();&lt;br /&gt;
&lt;br /&gt;
    return $persistents;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need to join persistents together and be able to extract their respective properties in a single database query, you should have a look at the following methods:&lt;br /&gt;
&lt;br /&gt;
;get_sql_fields(string $alias, string $prefix = null)&lt;br /&gt;
: Returns the SQL statement to include in the SELECT clause to prefix columns.&lt;br /&gt;
;extract_record(stdClass $row, string $prefix = null)&lt;br /&gt;
: Extracts all the properties from a row based on the given prefix.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Minimalist example.&lt;br /&gt;
$sqlfields = status::get_sql_fields(&#039;s&#039;, &#039;statprops&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT $sqlfields, u.username&lt;br /&gt;
          FROM {&amp;quot; . status::TABLE . &amp;quot;} s&lt;br /&gt;
          JOIN {user} ON s.userid = u.id&lt;br /&gt;
         WHERE s.id = 1&amp;quot;;&lt;br /&gt;
$row = $DB-&amp;gt;get_record($sql, []);&lt;br /&gt;
$statusdata = status::extract_record($row, &#039;statprops&#039;);&lt;br /&gt;
$persistent = new status(0, $statusdata);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Validating ==&lt;br /&gt;
&lt;br /&gt;
Basic validation of the properties values happens automatically based on their type (&#039;&#039;PARAM_*&#039;&#039; constant), however this is not always enough. In order to implement your own custom validation, simply define a &#039;&#039;protected&#039;&#039; method starting with &#039;&#039;validate_&#039;&#039; followed with the property name. This method will be called whenever the model needs to be validated and will receive the data to validate.&lt;br /&gt;
&lt;br /&gt;
A validation method must always return either &#039;&#039;true&#039;&#039; or an instance of &#039;&#039;lang_string&#039;&#039; which contains the error message to send to the user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Validate the user ID.&lt;br /&gt;
 *&lt;br /&gt;
 * @param int $value The value.&lt;br /&gt;
 * @return true|lang_string&lt;br /&gt;
 */&lt;br /&gt;
protected function validate_userid($value) {&lt;br /&gt;
    if (!core_user::is_real_user($value, true)) {&lt;br /&gt;
        return new lang_string(&#039;invaliduserid&#039;, &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example ensures that the &#039;&#039;userid&#039;&#039; property contains a valid user ID.&lt;br /&gt;
&lt;br /&gt;
Note that the basic validation is always performed first, and thus your custom validation method will not be called when the value did not pass the basic validation.&lt;br /&gt;
&lt;br /&gt;
=== Validation results ===&lt;br /&gt;
&lt;br /&gt;
The validation of the object automatically happens upon &#039;&#039;create&#039;&#039; and &#039;&#039;update&#039;&#039;. If the validation did not pass, an &#039;&#039;invalid_persistent_exception&#039;&#039; will be raised. You can validate the object prior to saving the object and get the validation results if you need to.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// We can catch the invalid_persistent_exception.&lt;br /&gt;
try {&lt;br /&gt;
    $persistent = new status();&lt;br /&gt;
    $persistent-&amp;gt;create();&lt;br /&gt;
} catch (invalid_persistent_exception $e) {&lt;br /&gt;
    // Whoops, something wrong happened.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Check whether the object is valid.&lt;br /&gt;
$persistent-&amp;gt;is_valid();        // True or false.&lt;br /&gt;
&lt;br /&gt;
// Get the validation errors.&lt;br /&gt;
$persistent-&amp;gt;get_errors();      // Array where keys are properties and values are errors.&lt;br /&gt;
&lt;br /&gt;
// Validate the object.&lt;br /&gt;
$persistent-&amp;gt;validate();        // Returns true, or an array of errors.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Hooks ==&lt;br /&gt;
&lt;br /&gt;
You can define the following methods to be notified prior to, or after, something happened:&lt;br /&gt;
&lt;br /&gt;
;protected before_validate()&lt;br /&gt;
: Do something before the object is validated.&lt;br /&gt;
;protected before_create()&lt;br /&gt;
: Do something before the object is inserted in the database. Note that values assigned to properties are not longer validated at this point.&lt;br /&gt;
;protected after_create()&lt;br /&gt;
: Do something right after the object was added to the database.&lt;br /&gt;
;protected before_update()&lt;br /&gt;
: Do something before the object is updated in the database. Note that values assigned to properties are not longer validated at this point.&lt;br /&gt;
;protected after_update(bool $result)&lt;br /&gt;
: Do something right after the object was updated in the database.&lt;br /&gt;
;protected before_delete()&lt;br /&gt;
: Do something right before the object is deleted from the database.&lt;br /&gt;
;protected after_delete(bool $result)&lt;br /&gt;
: Do something right after the object was deleted from the database.&lt;br /&gt;
&lt;br /&gt;
== Persistent and forms ==&lt;br /&gt;
&lt;br /&gt;
If your persistents are exposed to your users via forms, head to the [[Persistent form|persistent form page]].&lt;br /&gt;
&lt;br /&gt;
== Common pitfalls ==&lt;br /&gt;
&lt;br /&gt;
# You must create the database table yourself, using the [[XMLDB editor]] and an upgrade script.&lt;br /&gt;
# You must include the [[#Mandatory_properties|mandatory fields]] in your table schema.&lt;br /&gt;
# You must never extend another persistent as this leads to unpredictable errors whenever the parent changes (new properties missing in your table, changes in validation, custom getters and setters, etc...).&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
=== Minimalist ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class status extends \core\persistent {&lt;br /&gt;
&lt;br /&gt;
    /** Table name for the persistent. */&lt;br /&gt;
    const TABLE = &#039;status&#039;;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the definition of the properties of this model.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace example;&lt;br /&gt;
&lt;br /&gt;
use core\persistent;&lt;br /&gt;
use core_user;&lt;br /&gt;
use lang_string;&lt;br /&gt;
&lt;br /&gt;
class status extends persistent {&lt;br /&gt;
&lt;br /&gt;
    /** Table name for the persistent. */&lt;br /&gt;
    const TABLE = &#039;status&#039;;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the definition of the properties of this model.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;messageformat&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
                &#039;default&#039; =&amp;gt; FORMAT_PLAIN,&lt;br /&gt;
                &#039;choices&#039; =&amp;gt; array(FORMAT_PLAIN, FORMAT_HTML, FORMAT_MOODLE, FORMAT_MARKDOWN)&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;location&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT,&lt;br /&gt;
                &#039;null&#039; =&amp;gt; NULL_ALLOWED,&lt;br /&gt;
                &#039;message&#039; =&amp;gt; new lang_string(&#039;invaliddata&#039;, &#039;error&#039;),&lt;br /&gt;
                &#039;default&#039; =&amp;gt; function() {&lt;br /&gt;
                    return get_config(&#039;core&#039;, &#039;default_location&#039;);&lt;br /&gt;
                },&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the user object of the author.&lt;br /&gt;
     *&lt;br /&gt;
     * @return stdClass&lt;br /&gt;
     */&lt;br /&gt;
    public function get_author() {&lt;br /&gt;
        return core_user::get_user($this-&amp;gt;get(&#039;userid&#039;));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Convenience method to set the user ID.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object|int $idorobject The user ID, or a user object.&lt;br /&gt;
     */&lt;br /&gt;
    public function set_userid($idorobject) {&lt;br /&gt;
        $userid = $idorobject;&lt;br /&gt;
        if (is_object($idorobject)) {&lt;br /&gt;
            $userid = $idorobject-&amp;gt;id;&lt;br /&gt;
        }&lt;br /&gt;
        $this-&amp;gt;raw_set(&#039;userid&#039;, $userid);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Validate the user ID.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $value The value.&lt;br /&gt;
     * @return true|lang_string&lt;br /&gt;
     */&lt;br /&gt;
    protected function validate_userid($value) {&lt;br /&gt;
        if (!core_user::is_real_user($value, true)) {&lt;br /&gt;
            return new lang_string(&#039;invaliduserid&#039;, &#039;error&#039;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get all records from a user&#039;s username.&lt;br /&gt;
     *&lt;br /&gt;
     * @param string $username The username.&lt;br /&gt;
     * @return status[]&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_records_by_username($username) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $sql = &#039;SELECT s.*&lt;br /&gt;
                  FROM {&#039; . static::TABLE . &#039;} s&lt;br /&gt;
                  JOIN {user} u&lt;br /&gt;
                    ON u.id = s.userid&lt;br /&gt;
                 WHERE u.username = :username&#039;;&lt;br /&gt;
&lt;br /&gt;
        $persistents = [];&lt;br /&gt;
&lt;br /&gt;
        $recordset = $DB-&amp;gt;get_recordset_sql($sql, [&#039;username&#039; =&amp;gt; $username]);&lt;br /&gt;
        foreach ($recordset as $record) {&lt;br /&gt;
            $persistents[] = new static(0, $record);&lt;br /&gt;
        }&lt;br /&gt;
        $recordset-&amp;gt;close();&lt;br /&gt;
&lt;br /&gt;
        return $persistents;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Persistent form]]&lt;br /&gt;
* [[Exporter]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Persistent&amp;diff=55595</id>
		<title>Persistent</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Persistent&amp;diff=55595"/>
		<updated>2019-02-22T08:50:55Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 3.3}}Moodle persistents are equivalent to models (or active records). They represent an object stored in the database and provide the methods to create, read, update, and delete those objects. Additionally, the persistents validate their own data against automatic and custom validation rules. Persistents are made by extending the abstract class &#039;&#039;core\persistent&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== Properties ==&lt;br /&gt;
&lt;br /&gt;
Each class must define the properties that it contains. This can be done by defining the method &#039;&#039;protected static define_properties()&#039;&#039;. It returns an array where the keys are the names of the properties (and implies the database column names), and their attributes. The &#039;&#039;type&#039;&#039; of each property must be specified using one of the &#039;&#039;PARAM_*&#039;&#039; constants. This type will be used to automatically validate the property&#039;s value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Return the definition of the properties of this model.&lt;br /&gt;
 *&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected static function define_properties() {&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
        )&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Properties attributes ===&lt;br /&gt;
&lt;br /&gt;
;type&lt;br /&gt;
: The only mandatory attribute. It must be one of the many &#039;&#039;PARAM_*&#039;&#039; constants.&lt;br /&gt;
;default&lt;br /&gt;
: The default value to attach to the property when it hasn&#039;t been provided. Without a &#039;&#039;default&#039;&#039;, the property&#039;s value is required. Alternatively this can be a &#039;&#039;Closure&#039;&#039; returning the default value.&lt;br /&gt;
;null&lt;br /&gt;
: Either of the constants &#039;&#039;NULL_ALLOWED&#039;&#039; or &#039;&#039;NULL_NOT_ALLOWED&#039;&#039; determining if the null value is accepted. This defaults to &#039;&#039;NULL_NOT_ALLOWED&#039;&#039;.&lt;br /&gt;
;message&lt;br /&gt;
: The default error message (as a &#039;&#039;lang_string&#039;&#039; instance) to return when the validation of this field fails.&lt;br /&gt;
;choices&lt;br /&gt;
: An array of values which the property must fall in.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&#039;messageformat&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
    &#039;default&#039; =&amp;gt; FORMAT_PLAIN,&lt;br /&gt;
    &#039;choices&#039; =&amp;gt; array(FORMAT_PLAIN, FORMAT_HTML, FORMAT_MOODLE, FORMAT_MARKDOWN)&lt;br /&gt;
),&lt;br /&gt;
&#039;location&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT,&lt;br /&gt;
    &#039;null&#039; =&amp;gt; NULL_ALLOWED,&lt;br /&gt;
    &#039;message&#039; =&amp;gt; new lang_string(&#039;invaliddata&#039;, &#039;error&#039;),&lt;br /&gt;
    &#039;default&#039; =&amp;gt; function() {&lt;br /&gt;
        return get_config(&#039;core&#039;, &#039;default_location&#039;);&lt;br /&gt;
    },&lt;br /&gt;
),&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that you should always use a &#039;&#039;Closure&#039;&#039; for the &#039;&#039;default&#039;&#039; value when you cannot guarantee that it will not change since the start of the request. The list of properties and their attributes is cached, and so failure to use a &#039;&#039;Closure&#039;&#039; can result in using an outdated default value.&lt;br /&gt;
&lt;br /&gt;
=== Mandatory properties ===&lt;br /&gt;
&lt;br /&gt;
Four fields are always added to your persistent and should be reflected in your database table. You must not define those properties in &#039;&#039;define_properties()&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
;id (non-null integer)&lt;br /&gt;
: The primary key of the record.&lt;br /&gt;
;usermodified (non-null integer)&lt;br /&gt;
: The user who created/modified the object. It is automatically set.&lt;br /&gt;
;timecreated (non-null integer)&lt;br /&gt;
: The timestamp at which the record was modified. It is automatically set.&lt;br /&gt;
;timemodified (non-null integer)&lt;br /&gt;
: The timestamp at which the record was modified. It is automatically set, and defaults to 0.&lt;br /&gt;
&lt;br /&gt;
{{Moodle 3.7}} From Moodle 3.7 onwards, the XMLDB tool has an option to add these fields to your table.&lt;br /&gt;
&lt;br /&gt;
== Linking to a database table ==&lt;br /&gt;
&lt;br /&gt;
While the persistent class is helpful for database interactions, it does not automatically fetch the properties from the database, nor does it create the tables. You will need to create the table yourself, as well as pointing the persistent to it. This can be done by defining the class constant &#039;&#039;TABLE&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/** Table name for the persistent. */&lt;br /&gt;
const TABLE = &#039;status&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table name must not contain the Moodle prefix. Also it is common practice to always refer to your table name using the constant.&lt;br /&gt;
&lt;br /&gt;
== Reading property values ==&lt;br /&gt;
&lt;br /&gt;
This can be done using the method &#039;&#039;get()&#039;&#039;. Alternatively you can also use &#039;&#039;to_record()&#039;&#039; which exports the whole object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Create a new object.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
&lt;br /&gt;
// Get the user ID using the generic getter.&lt;br /&gt;
$userid = $persistent-&amp;gt;get(&#039;userid&#039;);&lt;br /&gt;
&lt;br /&gt;
// Get all the properties in an stdClass.&lt;br /&gt;
$data = $persistent-&amp;gt;to_record();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may add a new custom getter to implement your own custom logic. To do this just add a new method get_propertyname() and it will be called from the generic get() method. For instance you could convert the data in another format automatically as a convenience for developers. However, use this sparingly as it may lead to confusion: what you get is not what is stored in the database. Also - to get the actual value from a custom getter - call raw_get(). Calling get() from a custom getter will generate a segfault due to infinite recursion.&lt;br /&gt;
&lt;br /&gt;
It is, however, encouraged to add convenience methods such as the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Returns the user object of the author.&lt;br /&gt;
 *&lt;br /&gt;
 * @return stdClass&lt;br /&gt;
 */&lt;br /&gt;
public function get_author() {&lt;br /&gt;
    return core_user::get_user($this-&amp;gt;get(&#039;userid&#039;));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Assigning values to properties ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to do so.&lt;br /&gt;
&lt;br /&gt;
You use an object (&#039;&#039;stdClass&#039;&#039;) to assign a bunch of properties at once. Use it with the constructor, or the method &#039;&#039;from_record()&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$data = (object) array(&#039;userid&#039; =&amp;gt; 2, &#039;message&#039; =&amp;gt; &#039;Hello world!&#039;);&lt;br /&gt;
&lt;br /&gt;
// Instantiates a new object with value for some properties.&lt;br /&gt;
$persistent = new status(0, $data);&lt;br /&gt;
&lt;br /&gt;
// Is similar to.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
$persistent-&amp;gt;from_record($data);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, you can use the magic setters &#039;&#039;set_&#039;&#039; followed by the property name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Instantiates a blank object.&lt;br /&gt;
$persistent = new status();&lt;br /&gt;
&lt;br /&gt;
// Assign a new value to the &#039;message&#039; property.&lt;br /&gt;
$persistent-&amp;gt;set(&#039;message&#039;, &#039;Hello new world!&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Defining a custom setter ===&lt;br /&gt;
&lt;br /&gt;
Though you don&#039;t have to for the code to work, you can define your own &#039;&#039;setter&#039;&#039; methods which will be called from the generic setter. This is useful if you want to extract the data out of a more complex object prior to assigning it. Though note that those setters will then have to use the &#039;&#039;raw_set()&#039;&#039; method to assign the values (if you do not you will see a segfault because of infinite recursion).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Convenience method to set the user ID.&lt;br /&gt;
 *&lt;br /&gt;
 * @param object|int $idorobject The user ID, or a user object.&lt;br /&gt;
 */&lt;br /&gt;
public function set_userid($idorobject) {&lt;br /&gt;
    $userid = $idorobject;&lt;br /&gt;
    if (is_object($idorobject)) {&lt;br /&gt;
        $userid = $idorobject-&amp;gt;id;&lt;br /&gt;
    }&lt;br /&gt;
    $this-&amp;gt;raw_set(&#039;userid&#039;, $userid);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example we will accept an object or an ID, as a convenience for developers we will extract the ID value out of the object passed if any.&lt;br /&gt;
&lt;br /&gt;
Just like custom getters, must be careful so that the following code should not result in a change in the DB.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$value = $persistent-&amp;gt;get(&#039;property&#039;);&lt;br /&gt;
$persistent-&amp;gt;set(&#039;property&#039;, $value);&lt;br /&gt;
$persistent-&amp;gt;update();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can obviously create your own setters which aren&#039;t based on any properties just as a convenience. For instance we could have created &#039;&#039;set_userid_from_user(object $user)&#039;&#039; which is more verbose and more predictable&lt;br /&gt;
&lt;br /&gt;
== Read, save and delete entries ==&lt;br /&gt;
&lt;br /&gt;
The methods to &#039;&#039;create&#039;&#039;, &#039;&#039;read&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; are eponymous. Your object will be validated before you &#039;&#039;create&#039;&#039; or &#039;&#039;update&#039;&#039; it. The &#039;&#039;update&#039;&#039;, &#039;&#039;delete&#039;&#039; and &#039;&#039;read&#039;&#039; methods require your object to contain its ID. And you also won&#039;t be allowed to &#039;&#039;create&#039;&#039; an entry which already had an ID defined.&lt;br /&gt;
&lt;br /&gt;
Here are some code examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Fetches an object from database based on its ID.&lt;br /&gt;
$id = 123;&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
&lt;br /&gt;
// Create object in the database.&lt;br /&gt;
$data = new stdClass();&lt;br /&gt;
$data-&amp;gt;message = &#039;Hello new world&#039;;&lt;br /&gt;
$persistent = new persistent(0, $data);&lt;br /&gt;
$persistent-&amp;gt;create();&lt;br /&gt;
// $persistent-&amp;gt;get(&#039;id&#039;) will now return an id.&lt;br /&gt;
&lt;br /&gt;
// Load an object from the database, and update it.&lt;br /&gt;
$id = 123;&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
$persistent-&amp;gt;set(&#039;message&#039;, &#039;Hello new world!&#039;);&lt;br /&gt;
$persistent-&amp;gt;update();&lt;br /&gt;
&lt;br /&gt;
// Reset the instance to the values in the database.&lt;br /&gt;
$persistent-&amp;gt;read();&lt;br /&gt;
&lt;br /&gt;
// Permanently delete the object from the database.&lt;br /&gt;
$persistent-&amp;gt;delete();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fetching records ==&lt;br /&gt;
&lt;br /&gt;
Once you start using persistents you should never directly interact with the database outside of your class. The persistent class comes with a few handy methods allowing you to retrieve your objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Use the constructor to fetch one object from its ID.&lt;br /&gt;
$persistent = new status($id);&lt;br /&gt;
&lt;br /&gt;
// Get one record from a set of conditions.&lt;br /&gt;
$persistent = status::get_record([&#039;userid&#039; =&amp;gt; $userid, &#039;message&#039; =&amp;gt; &#039;Hello world!&#039;]);&lt;br /&gt;
&lt;br /&gt;
// Get multiple records from a set of conditions.&lt;br /&gt;
$persistents = status::get_records([&#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
&lt;br /&gt;
// Count the records.&lt;br /&gt;
$count = status::count_records([&#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
&lt;br /&gt;
// Check whether a record exists.&lt;br /&gt;
$exists = status::record_exists($id);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Make sure to also check their additional parameters and their variants (&#039;&#039;record_exists_select()&#039;&#039;, &#039;&#039;count_records_select&#039;&#039;, &#039;&#039;get_records_select&#039;&#039;, ...).&lt;br /&gt;
&lt;br /&gt;
=== Custom fetching ===&lt;br /&gt;
&lt;br /&gt;
It&#039;s always a good idea to add more complex queries directly within your persistent. By convention you should always return an instance of your persistent and never an stdClass. Here we add a custom method which allows to directly fetch all records by username.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get all records from a user&#039;s username.&lt;br /&gt;
 *&lt;br /&gt;
 * @param string $username The username.&lt;br /&gt;
 * @return status[]&lt;br /&gt;
 */&lt;br /&gt;
public static function get_records_by_username($username) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
    $sql = &#039;SELECT s.*&lt;br /&gt;
              FROM {&#039; . static::TABLE . &#039;} s&lt;br /&gt;
              JOIN {user} u&lt;br /&gt;
                ON u.id = s.userid&lt;br /&gt;
             WHERE u.username = :username&#039;;&lt;br /&gt;
&lt;br /&gt;
    $persistents = [];&lt;br /&gt;
&lt;br /&gt;
    $recordset = $DB-&amp;gt;get_recordset_sql($sql, [&#039;username&#039; =&amp;gt; $username]);&lt;br /&gt;
    foreach ($recordset as $record) {&lt;br /&gt;
        $persistents[] = new static(0, $record);&lt;br /&gt;
    }&lt;br /&gt;
    $recordset-&amp;gt;close();&lt;br /&gt;
&lt;br /&gt;
    return $persistents;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need to join persistents together and be able to extract their respective properties in a single database query, you should have a look at the following methods:&lt;br /&gt;
&lt;br /&gt;
;get_sql_fields(string $alias, string $prefix = null)&lt;br /&gt;
: Returns the SQL statement to include in the SELECT clause to prefix columns.&lt;br /&gt;
;extract_record(stdClass $row, string $prefix = null)&lt;br /&gt;
: Extracts all the properties from a row based on the given prefix.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Minimalist example.&lt;br /&gt;
$sqlfields = status::get_sql_fields(&#039;s&#039;, &#039;statprops&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT $sqlfields, u.username&lt;br /&gt;
          FROM {&amp;quot; . status::TABLE . &amp;quot;} s&lt;br /&gt;
          JOIN {user} ON s.userid = u.id&lt;br /&gt;
         WHERE s.id = 1&amp;quot;;&lt;br /&gt;
$row = $DB-&amp;gt;get_record($sql, []);&lt;br /&gt;
$statusdata = status::extract_record($row, &#039;statprops&#039;);&lt;br /&gt;
$persistent = new status(0, $statusdata);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Validating ==&lt;br /&gt;
&lt;br /&gt;
Basic validation of the properties values happens automatically based on their type (&#039;&#039;PARAM_*&#039;&#039; constant), however this is not always enough. In order to implement your own custom validation, simply define a &#039;&#039;protected&#039;&#039; method starting with &#039;&#039;validate_&#039;&#039; followed with the property name. This method will be called whenever the model needs to be validated and will receive the data to validate.&lt;br /&gt;
&lt;br /&gt;
A validation method must always return either &#039;&#039;true&#039;&#039; or an instance of &#039;&#039;lang_string&#039;&#039; which contains the error message to send to the user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Validate the user ID.&lt;br /&gt;
 *&lt;br /&gt;
 * @param int $value The value.&lt;br /&gt;
 * @return true|lang_string&lt;br /&gt;
 */&lt;br /&gt;
protected function validate_userid($value) {&lt;br /&gt;
    if (!core_user::is_real_user($value, true)) {&lt;br /&gt;
        return new lang_string(&#039;invaliduserid&#039;, &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example ensures that the &#039;&#039;userid&#039;&#039; property contains a valid user ID.&lt;br /&gt;
&lt;br /&gt;
Note that the basic validation is always performed first, and thus your custom validation method will not be called when the value did not pass the basic validation.&lt;br /&gt;
&lt;br /&gt;
=== Validation results ===&lt;br /&gt;
&lt;br /&gt;
The validation of the object automatically happens upon &#039;&#039;create&#039;&#039; and &#039;&#039;update&#039;&#039;. If the validation did not pass, an &#039;&#039;invalid_persistent_exception&#039;&#039; will be raised. You can validate the object prior to saving the object and get the validation results if you need to.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// We can catch the invalid_persistent_exception.&lt;br /&gt;
try {&lt;br /&gt;
    $persistent = new status();&lt;br /&gt;
    $persistent-&amp;gt;create();&lt;br /&gt;
} catch (invalid_persistent_exception $e) {&lt;br /&gt;
    // Whoops, something wrong happened.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Check whether the object is valid.&lt;br /&gt;
$persistent-&amp;gt;is_valid();        // True or false.&lt;br /&gt;
&lt;br /&gt;
// Get the validation errors.&lt;br /&gt;
$persistent-&amp;gt;get_errors();      // Array where keys are properties and values are errors.&lt;br /&gt;
&lt;br /&gt;
// Validate the object.&lt;br /&gt;
$persistent-&amp;gt;validate();        // Returns true, or an array of errors.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Hooks ==&lt;br /&gt;
&lt;br /&gt;
You can define the following methods to be notified prior to, or after, something happened:&lt;br /&gt;
&lt;br /&gt;
;protected before_validate()&lt;br /&gt;
: Do something before the object is validated.&lt;br /&gt;
;protected before_create()&lt;br /&gt;
: Do something before the object is inserted in the database. Note that values assigned to properties are not longer validated at this point.&lt;br /&gt;
;protected after_create()&lt;br /&gt;
: Do something right after the object was added to the database.&lt;br /&gt;
;protected before_update()&lt;br /&gt;
: Do something before the object is updated in the database. Note that values assigned to properties are not longer validated at this point.&lt;br /&gt;
;protected after_update(bool $result)&lt;br /&gt;
: Do something right after the object was updated in the database.&lt;br /&gt;
;protected before_delete()&lt;br /&gt;
: Do something right before the object is deleted from the database.&lt;br /&gt;
;protected after_delete(bool $result)&lt;br /&gt;
: Do something right after the object was deleted from the database.&lt;br /&gt;
&lt;br /&gt;
== Persistent and forms ==&lt;br /&gt;
&lt;br /&gt;
If your persistents are exposed to your users via forms, head to the [[Persistent form|persistent form page]].&lt;br /&gt;
&lt;br /&gt;
== Common pitfalls ==&lt;br /&gt;
&lt;br /&gt;
# You must create the database table yourself, using the [[XMLDB editor]] and an upgrade script.&lt;br /&gt;
# You must include the [[#Mandatory_properties|mandatory fields]] in your table schema.&lt;br /&gt;
# You must never extend another persistent as this leads to unpredictable errors whenever the parent changes (new properties missing in your table, changes in validation, custom getters and setters, etc...).&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
=== Minimalist ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class status extends \core\persistent {&lt;br /&gt;
&lt;br /&gt;
    /** Table name for the persistent. */&lt;br /&gt;
    const TABLE = &#039;status&#039;;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the definition of the properties of this model.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace example;&lt;br /&gt;
&lt;br /&gt;
use core\persistent;&lt;br /&gt;
use core_user;&lt;br /&gt;
use lang_string;&lt;br /&gt;
&lt;br /&gt;
class status extends persistent {&lt;br /&gt;
&lt;br /&gt;
    /** Table name for the persistent. */&lt;br /&gt;
    const TABLE = &#039;status&#039;;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the definition of the properties of this model.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;message&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;messageformat&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
                &#039;default&#039; =&amp;gt; FORMAT_PLAIN,&lt;br /&gt;
                &#039;choices&#039; =&amp;gt; array(FORMAT_PLAIN, FORMAT_HTML, FORMAT_MOODLE, FORMAT_MARKDOWN)&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;location&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT,&lt;br /&gt;
                &#039;null&#039; =&amp;gt; NULL_ALLOWED,&lt;br /&gt;
                &#039;message&#039; =&amp;gt; new lang_string(&#039;invaliddata&#039;, &#039;error&#039;),&lt;br /&gt;
                &#039;default&#039; =&amp;gt; function() {&lt;br /&gt;
                    return get_config(&#039;core&#039;, &#039;default_location&#039;);&lt;br /&gt;
                },&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the user object of the author.&lt;br /&gt;
     *&lt;br /&gt;
     * @return stdClass&lt;br /&gt;
     */&lt;br /&gt;
    public function get_author() {&lt;br /&gt;
        return core_user::get_user($this-&amp;gt;get(&#039;userid&#039;));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Convenience method to set the user ID.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object|int $idorobject The user ID, or a user object.&lt;br /&gt;
     */&lt;br /&gt;
    public function set_userid($idorobject) {&lt;br /&gt;
        $userid = $idorobject;&lt;br /&gt;
        if (is_object($idorobject)) {&lt;br /&gt;
            $userid = $idorobject-&amp;gt;id;&lt;br /&gt;
        }&lt;br /&gt;
        $this-&amp;gt;raw_set(&#039;userid&#039;, $userid);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Validate the user ID.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $value The value.&lt;br /&gt;
     * @return true|lang_string&lt;br /&gt;
     */&lt;br /&gt;
    protected function validate_userid($value) {&lt;br /&gt;
        if (!core_user::is_real_user($value, true)) {&lt;br /&gt;
            return new lang_string(&#039;invaliduserid&#039;, &#039;error&#039;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get all records from a user&#039;s username.&lt;br /&gt;
     *&lt;br /&gt;
     * @param string $username The username.&lt;br /&gt;
     * @return status[]&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_records_by_username($username) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $sql = &#039;SELECT s.*&lt;br /&gt;
                  FROM {&#039; . static::TABLE . &#039;} s&lt;br /&gt;
                  JOIN {user} u&lt;br /&gt;
                    ON u.id = s.userid&lt;br /&gt;
                 WHERE u.username = :username&#039;;&lt;br /&gt;
&lt;br /&gt;
        $persistents = [];&lt;br /&gt;
&lt;br /&gt;
        $recordset = $DB-&amp;gt;get_recordset_sql($sql, [&#039;username&#039; =&amp;gt; $username]);&lt;br /&gt;
        foreach ($recordset as $record) {&lt;br /&gt;
            $persistents[] = new static(0, $record);&lt;br /&gt;
        }&lt;br /&gt;
        $recordset-&amp;gt;close();&lt;br /&gt;
&lt;br /&gt;
        return $persistents;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Persistent form]]&lt;br /&gt;
* [[Exporter]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54159</id>
		<title>Privacy API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54159"/>
		<updated>2018-05-14T11:23:19Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Delete for a context */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [https://en.wikipedia.org/wiki/General_Data_Protection_Regulation General Data Protection Regulation] (GDPR) is an EU directive that looks at providing users with more control over their data and how it is processed. This regulation will come into effect on 25th of May 2018 and covers any citizen or permanent resident of the European Union. The directive will be respected by a number of other countries outside of the European Union.&lt;br /&gt;
&lt;br /&gt;
To help institutions become compliant with this new regulation we are adding functionality to Moodle. This includes a number of components, amongst others these include a user’s right to:&lt;br /&gt;
&lt;br /&gt;
* request information on the types of personal data held, the instances of that data, and the deletion policy for each;&lt;br /&gt;
* access all of their data; and&lt;br /&gt;
* be forgotten.&lt;br /&gt;
&lt;br /&gt;
The compliance requirements also extend to installed plugins (including third party plugins). These need to also be able to report what information they store or process regarding users, and have the ability to provide and delete data for a user request.&lt;br /&gt;
&lt;br /&gt;
This document describes the proposed API changes required for plugins which will allow a Moodle installation to become GDPR compliant.&lt;br /&gt;
&lt;br /&gt;
Target Audience: The intended audience for this document is Moodle plugin developers, who are aiming to ensure their plugins are updated to comply with GDPR requirements coming into effect in the EU in May, 2018.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Personal data in Moodle==&lt;br /&gt;
&lt;br /&gt;
From the GDPR Spec, Article 4:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;‘personal data’ means any information relating to an identified or identifiable natural person (‘data subject’); an identifiable natural person is one who can be identified, directly or indirectly, in particular by reference to an identifier such as a name, an identification number, location data, an online identifier or to one or more factors specific to the physical, physiological, genetic, mental, economic, cultural or social identity of that natural person;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Moodle, we need to consider two main types of personal data; information entered by the user and information stored about the user. The key difference being that information stored about the user will have come from a source other than the user themselves. Both types of data can be used to form a profile of the individual.&lt;br /&gt;
&lt;br /&gt;
The most obvious clue to finding personal data entered by the user is the presence of a userid on a database field. Any data on the record (or linked records) pertaining to that user may be deemed personal data for that user, including things like timestamps and record identification numbers. Additionally, any free text field which allows the user to enter information must also be considered to be the personal data of that user.&lt;br /&gt;
&lt;br /&gt;
Data stored about the user includes things like ratings and comments made on a student submission. These may have been made by an assessor or teacher, but are considered the personal data of the student, as they are considered a reflection of the user’s competency in the subject matter and can be used to form a profile of that individual. &lt;br /&gt;
&lt;br /&gt;
The sections that follow outline what you need to do as a plugin developer to ensure any personal data is advertised and can be accessed and deleted according to the GDPR requirements.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
===Architecture overview===&lt;br /&gt;
&lt;br /&gt;
[[File:MoodlePrivacyMetadataUML.png|thumb|UML diagram of the metadata part of the privacy subsystem]]&lt;br /&gt;
[[File:MoodlePrivacyRequestUML.png|thumb|UML diagram of the request providers part of the privacy subsystem]]&lt;br /&gt;
&lt;br /&gt;
A new system for Privacy has been created within Moodle. This is broken down into several main parts and forms the &#039;&#039;core_privacy&#039;&#039; subsystem:&lt;br /&gt;
&lt;br /&gt;
* Some metadata providers - a set of PHP interfaces to be implemented by components for that component to describe the kind of data that it stores, and the purpose for its storage;&lt;br /&gt;
* Some request providers - a set of PHP interfaces to be implemented by components to allow that component to act upon user requests such as the Right to be Forgotten, and a Subject Access Request; and&lt;br /&gt;
* A manager - a concrete class used to bridge components which implement the providers with tools which request their data.&lt;br /&gt;
&lt;br /&gt;
All plugins will implement one metadata provider, and zero, one or two request providers.&lt;br /&gt;
&lt;br /&gt;
The fetching of data is broken into two separate steps:&lt;br /&gt;
&lt;br /&gt;
# Detecting in which Moodle contexts the user has any data; and&lt;br /&gt;
# Exporting all data from each of those contexts.&lt;br /&gt;
&lt;br /&gt;
This has been broken into two steps to later allow administrators to exclude certain contexts from an export - e.g. for courses currently in progress.&lt;br /&gt;
&lt;br /&gt;
A third component will later be added to facilitate the deletion of data within these contexts which will help to satisfy the Right to be Forgotten. This will also use the first step.&lt;br /&gt;
&lt;br /&gt;
Please refer to the inline phpdocs of the [https://github.com/moodle/moodle/blob/v3.5.0-beta/privacy/classes/manager.php#L31 core_privacy::manager class] for detailed description of the interfaces, their hierarchy and meaning.&lt;br /&gt;
&lt;br /&gt;
===Implementing a provider===&lt;br /&gt;
&lt;br /&gt;
All plugins will need to create a concrete class which implements the relevant metadata and request providers. The exact providers you need to implement will depend on what data you store, and the type of plugin. This is covered in more detail in the following sections of the document.&lt;br /&gt;
&lt;br /&gt;
In order to do so:&lt;br /&gt;
&lt;br /&gt;
# You must create a class called &#039;&#039;provider&#039;&#039; within the namespace &#039;&#039;\your_pluginname\privacy&#039;&#039;.&lt;br /&gt;
# This class must be created at &#039;&#039;path/to/your/plugin/classes/privacy/provider.php&#039;&#039;.&lt;br /&gt;
# You must have your class implement the relevant metadata and request interfaces.&lt;br /&gt;
&lt;br /&gt;
==Plugins which do not store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do not store any personal data. This is usually the case for plugins which just add functionality, or which display the data already stored elsewhere in Moodle.&lt;br /&gt;
&lt;br /&gt;
Some examples of plugin types which might fit this criteria include themes, blocks, filters, editor plugins, etc.&lt;br /&gt;
&lt;br /&gt;
Plugins which cause data to be stored elsewhere in Moodle (e.g. via a subsystem call) are considered to store data.&lt;br /&gt;
&lt;br /&gt;
One examples of a plugin which does not store any data would be the Calendar month block which just displays a view of the user’s calendar. It does not store any data itself.&lt;br /&gt;
&lt;br /&gt;
An example of a plugin which must not use the null provider is the Comments block. The comments block is responsible for data subsequently being stored within Moodle. Although the block doesn’t store anything itself, it interacts with the comments subsystem and is the only component which knows how that data maps to a user.&lt;br /&gt;
&lt;br /&gt;
===Implementation requirements===&lt;br /&gt;
&lt;br /&gt;
In order to let Moodle know that you have audited your plugin, and that you do not store any personal user data, you must implement the &#039;&#039;\core_privacy\local\metadata\null_provider&#039;&#039; interface in your plugin’s provider.&lt;br /&gt;
&lt;br /&gt;
These null providers can only be implemented where a plugin has:&lt;br /&gt;
&lt;br /&gt;
* no external links (e.g. sends data to an external service like an LTI provider, repository plugin which you can search on)&lt;br /&gt;
* no database tables which store user data (including IP addresses)&lt;br /&gt;
* no user preferences&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;null_provider&#039;&#039; requires you to define one function &#039;&#039;get_reason()&#039;&#039; which returns the language string identifier within your component.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace block_calendar_month\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
    // This plugin does not store any personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\null_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the language string identifier with the component&#039;s language&lt;br /&gt;
     * file to explain why this plugin stores no data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_reason() : string {&lt;br /&gt;
        return &#039;privacy:metadata&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/lang/en/block_calendar_month.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
$string[&#039;privacy:metadata&#039;] = &#039;The Calendar block only displays existing calendar data.&#039;;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That’s it. Congratulations, your plugin now implements the Privacy API.&lt;br /&gt;
&lt;br /&gt;
==Plugins which store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do store some form of personal data.&lt;br /&gt;
&lt;br /&gt;
In some cases this will be stored within database tables in your plugin, and in other cases this will be in one of Moodle’s core subsystems - for example your plugin may store files, ratings, comments, or tags.&lt;br /&gt;
&lt;br /&gt;
Plugins which do store data will need to:&lt;br /&gt;
&lt;br /&gt;
* Describe the type of data that they store; &lt;br /&gt;
* Provide a way to export that data; and&lt;br /&gt;
* Provide a way to delete that data.&lt;br /&gt;
&lt;br /&gt;
Data is described via a &#039;&#039;metadata&#039;&#039; provider, and it is both exported and deleted via an implementation of a &#039;&#039;request&#039;&#039; provider.&lt;br /&gt;
&lt;br /&gt;
These are both explained in the sections below.&lt;br /&gt;
&lt;br /&gt;
===Describing the type of data you store===&lt;br /&gt;
&lt;br /&gt;
In order to describe the type of data that you store, you must implement the &#039;&#039;\core_privacy\local\metadata\provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
This interfaces requires that you define one function: &#039;&#039;get_metadata&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
There are several types of item to describe the data that you store. These are for:&lt;br /&gt;
&lt;br /&gt;
* Items in the Moodle database;&lt;br /&gt;
* Items stored by you in a Moodle subsystem - for example files, and ratings; and&lt;br /&gt;
* User preferences stored site-wide within Moodle for your plugin&lt;br /&gt;
&lt;br /&gt;
Note: All fields should include a description from a language string within your plugin.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_forum\privacy;&lt;br /&gt;
use core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
        // This plugin does store personal user data.&lt;br /&gt;
        \core_privacy\local\metadata\provider {&lt;br /&gt;
&lt;br /&gt;
    public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
        // Here you will add more items into the collection.&lt;br /&gt;
&lt;br /&gt;
        return $collection;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store content in a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
Many plugins will use one of the core Moodle subsystems to store data.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer we do not expect you to describe those subsystems in detail, but we do need to know that you use them and to know what you use them for.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_subsystem_link()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_subsystem_link(&lt;br /&gt;
        &#039;core_files&#039;,&lt;br /&gt;
        [],&lt;br /&gt;
        &#039;privacy:metadata:core_files&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:core_files&#039;] = &#039;The forum stores files which have been uploaded by the user to form part of a forum post.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Describing data stored in database tables====&lt;br /&gt;
&lt;br /&gt;
Most Moodle plugins will store some form of user data in their own database tables.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each database table, and each field which includes user data.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_database_table(&lt;br /&gt;
        &#039;forum_discussion_subs&#039;,&lt;br /&gt;
         [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:userid&#039;,&lt;br /&gt;
            &#039;discussionid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:discussionid&#039;,&lt;br /&gt;
            &#039;preference&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:preference&#039;,&lt;br /&gt;
&lt;br /&gt;
         ],&lt;br /&gt;
        &#039;privacy:metadata:forum_discussion_subs&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs&#039;] = &#039;Information about the subscriptions to individual forum discussions. This includes when a user has chosen to subscribe to a discussion, or to unsubscribe from one where they would otherwise be subscribed.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:userid&#039;] = &#039;The ID of the user with this subscription preference.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:discussionid&#039;] = &#039;The ID of the discussion that was subscribed to.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:preference&#039;] = &#039;The start time of the subscription.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store site-wide user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins will include one or more user preferences. Unfortunately this is one of Moodle’s older components and many of the values stored are not pure user preferences. Each plugin should be aware of how it handles its own preferences and is best placed to determine whether they are site-wide preferences, or per-instance preferences.&lt;br /&gt;
&lt;br /&gt;
Whilst most of these will have a fixed name (e.g. &#039;&#039;filepicker_recentrepository&#039;&#039;), some will include a variable of some kind (e.g. &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039;). Only the general name needs to be indicated rather than one copy for each preference.&lt;br /&gt;
&lt;br /&gt;
Also, these should only be &#039;&#039;site-wide&#039;&#039; user preferences which do not belong to a specific Moodle context.&lt;br /&gt;
&lt;br /&gt;
In the above examples:&lt;br /&gt;
&lt;br /&gt;
* Preference &#039;&#039;filepicker_recentrepository&#039;&#039; belongs to the file subsystem, and is a site-wide preference affecting the user anywhere that they view the filepicker.&lt;br /&gt;
* Preference &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039; belongs to user tours. User tours are a site-wide feature which can affect many parts of Moodle and cross multiple contexts.&lt;br /&gt;
&lt;br /&gt;
In some cases a value may be stored in the preferences table but is known to belong to a specific context within Moodle. In these cases they should be stored as metadata against that context rather than as a site-wide user preference.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_user_preference()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Any plugin providing user preferences must also implement the &#039;&#039;\core_privacy\local\request\preference_provider&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_user_preference(&#039;tool_usertours_tour_completion_time&#039;,&lt;br /&gt;
        &#039;privacy:metadata:preference:tool_usertours_tour_completion_time&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/lang/en/tool_usertours.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:tool_usertours_tour_completion_time&#039;] = &#039;The time that a specific user tour was last completed by a user.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you export data to an external location====&lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with external systems - for example cloud-based services. Often this external location is configurable within the plugin either at the site or the instance level.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each &#039;&#039;type&#039;&#039; of target destination, alongside a list of each exported field which includes user data.&lt;br /&gt;
The &#039;&#039;actual&#039;&#039; destination does not need to be described as this can change based on configuration.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_external_location_link()&#039;&#039; method on the collection.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_external_location_link(&#039;lti_client&#039;, [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:lti_client:userid&#039;,&lt;br /&gt;
            &#039;fullname&#039; =&amp;gt; &#039;privacy:metadata:lti_client:fullname&#039;,&lt;br /&gt;
        ], &#039;privacy:metadata:lti_client&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/lang/en/lti.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client&#039;] = &#039;In order to integrate with a remote LTI service, user data needs to be exchanged with that service.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:userid&#039;] = &#039;The userid is sent from Moodle to allow you to access your data on the remote system.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:fullname&#039;] = &#039;Your full name is sent to the remote system to allow a better user experience.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to export user data===&lt;br /&gt;
&lt;br /&gt;
In order to export the user data that you store, you must implement the relevant request provider.&lt;br /&gt;
&lt;br /&gt;
We have named these request providers because they are called in response to a specific request from a user to access their information.&lt;br /&gt;
&lt;br /&gt;
There are several different types of request provider, and you may need to implement several of these, depending on the type and nature of your plugin.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking plugins will fit into one of the following categories:&lt;br /&gt;
&lt;br /&gt;
* Plugins which are a subplugin of another plugin. Examples include &#039;&#039;assignsubmission&#039;&#039;, &#039;&#039;atto&#039;&#039;, and &#039;&#039;datafield&#039;&#039;;&lt;br /&gt;
* Plugins which are typically called by a Moodle subsystem. Examples include &#039;&#039;qtype&#039;&#039;, and &#039;&#039;profilefield&#039;&#039;;&lt;br /&gt;
* All other plugins which store data.&lt;br /&gt;
&lt;br /&gt;
Most plugins will fit into this final category, whilst other plugins may fall into several categories.&lt;br /&gt;
Plugins which &#039;&#039;define&#039;&#039; a subplugin will also be responsible for  collecting this data from their subplugins.&lt;br /&gt;
&lt;br /&gt;
A final category exists - plugins which store user preferences. In some cases this may be the &#039;&#039;only&#039;&#039; provider implemented.&lt;br /&gt;
&lt;br /&gt;
====Standard plugins which store data====&lt;br /&gt;
&lt;br /&gt;
A majority of Moodle plugins will fit into this category and will be required to implement the &#039;&#039;\core_privacy\local\request\plugin\provider&#039;&#039; interface. This interface requires that you define four functions (the first two of which are dealt with in this section):&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;get_contexts_for_userid&#039;&#039; - to explain where data is held within Moodle for your plugin; and&lt;br /&gt;
* &#039;&#039;export_user_data&#039;&#039; - to export a user’s personal data from your plugin.&lt;br /&gt;
* &#039;&#039;delete_data_for_all_users_in_context&#039;&#039; - to delete all data for all users in the specified context.&lt;br /&gt;
* &#039;&#039;delete_data_for_user&#039;&#039; - to delete all user data for the specified user, in the specified contexts.&lt;br /&gt;
&lt;br /&gt;
These APIs make use of the Moodle &#039;&#039;context&#039;&#039; system to hierarchically store this data.&lt;br /&gt;
&lt;br /&gt;
=====Retrieving the list of contexts=====&lt;br /&gt;
&lt;br /&gt;
Contexts are retrieved using the &#039;&#039;get_contexts_for_userid&#039;&#039; function which takes the ID of the user being fetched, and returns a list of contexts in which the user has any data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function returns a &#039;&#039;\core_privacy\local\request\contextlist&#039;&#039; which is used to keep a set of contexts together in a fixed fashion.&lt;br /&gt;
&lt;br /&gt;
Because a Subject Access Request covers &#039;&#039;every&#039;&#039; piece of data that is held for a user within Moodle, efficiency and performance is highly important. As a result, contexts are added to the &#039;&#039;contextlist&#039;&#039; by defining one or more SQL queries which return just the contextid. Multiple SQL queries can be added as required.&lt;br /&gt;
&lt;br /&gt;
To decide which type of context to return (CONTEXT_COURSE, CONTEXT_USER etc.) consider the most appropriate type for your plugin. Typically, this will be the same type of context as you would create to check capabilities. For example, an enrolment plugin will return the course contexts in which it is used. &lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with specific subsystems and store data within them.&lt;br /&gt;
These subsystems will also provide a way in which to link the data that you have stored with your own database tables.&lt;br /&gt;
At present these are still a work in progress and only the &#039;&#039;core_ratings&#039;&#039; subsystem includes this.&lt;br /&gt;
&lt;br /&gt;
======Basic example======&lt;br /&gt;
&lt;br /&gt;
The following example simply fetches the contextid for all forums where a user has a single discussion (note: this is an incomplete example):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
        $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
&lt;br /&gt;
        $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                 FROM {context} c&lt;br /&gt;
           INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
           INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
           INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
                WHERE (&lt;br /&gt;
                d.userid        = :discussionuserid&lt;br /&gt;
                )&lt;br /&gt;
        &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        $params = [&lt;br /&gt;
            &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
            &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
            &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        ];&lt;br /&gt;
&lt;br /&gt;
        $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
        return $contextlist;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
======More complete example======&lt;br /&gt;
&lt;br /&gt;
The following example includes a link to core_rating. &lt;br /&gt;
It will find any forum, forum discussion, or forum post where the user has any data, including:&lt;br /&gt;
&lt;br /&gt;
* Per-forum digest preferences;&lt;br /&gt;
* Per-forum subscription preferences;&lt;br /&gt;
* Per-forum read tracking preferences;&lt;br /&gt;
* Per-discussion subscription preferences;&lt;br /&gt;
* Per-post read data (if a user has read a post or not); and&lt;br /&gt;
* Per-post rating data.&lt;br /&gt;
&lt;br /&gt;
In the case of the rating data, this will include any post where the user has rated the post of another user.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int           $userid       The user to search.&lt;br /&gt;
 * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
 */&lt;br /&gt;
public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
    $ratingsql = \core_rating\privacy\provider::get_sql_join(&#039;rat&#039;, &#039;mod_forum&#039;, &#039;post&#039;, &#039;p.id&#039;, $userid);&lt;br /&gt;
    // Fetch all forum discussions, and forum posts.&lt;br /&gt;
    $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                FROM {context} c&lt;br /&gt;
        INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
        INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
        INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_posts} p ON p.discussion = d.id&lt;br /&gt;
            LEFT JOIN {forum_digests} dig ON dig.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_subscriptions} sub ON sub.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_track_prefs} pref ON pref.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_read} hasread ON hasread.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_discussion_subs} dsub ON dsub.forum = f.id&lt;br /&gt;
            {$ratingsql-&amp;gt;join}&lt;br /&gt;
                WHERE (&lt;br /&gt;
                p.userid        = :postuserid OR&lt;br /&gt;
                d.userid        = :discussionuserid OR&lt;br /&gt;
                dig.userid      = :digestuserid OR&lt;br /&gt;
                sub.userid      = :subuserid OR&lt;br /&gt;
                pref.userid     = :prefuserid OR&lt;br /&gt;
                hasread.userid  = :hasreaduserid OR&lt;br /&gt;
                dsub.userid     = :dsubuserid OR&lt;br /&gt;
                {$ratingsql-&amp;gt;userwhere}&lt;br /&gt;
            )&lt;br /&gt;
    &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    $params = [&lt;br /&gt;
        &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
        &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &#039;postuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        &#039;digestuserid&#039;      =&amp;gt; $userid,&lt;br /&gt;
        &#039;subuserid&#039;         =&amp;gt; $userid,&lt;br /&gt;
        &#039;prefuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;hasreaduserid&#039;     =&amp;gt; $userid,&lt;br /&gt;
        &#039;dsubuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
    ];&lt;br /&gt;
    $params += $ratingsql-&amp;gt;params;&lt;br /&gt;
&lt;br /&gt;
    $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
    $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
    return $contextlist;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Exporting user data=====&lt;br /&gt;
&lt;br /&gt;
After determining where in Moodle your plugin holds data about a user, the &#039;&#039;\core_privacy\manager&#039;&#039; will then ask your plugin to export all user data for a subset of those locations.&lt;br /&gt;
&lt;br /&gt;
This is achieved through use of the &#039;&#039;export_user_data&#039;&#039; function which takes the list of approved contexts in a &#039;&#039;\core_privacy\local\request\approved_contextlist&#039;&#039; object.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   approved_contextlist    $contextlist    The approved contexts to export information for.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_data(approved_contextlist $contextlist) {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;approved_contextlist&#039;&#039; includes both the user record, and a list of contexts, which can be retrieved by either processing it as an Iterator, or by calling &#039;&#039;get_contextids()&#039;&#039; or &#039;&#039;get_contexts()&#039;&#039; as required.&lt;br /&gt;
&lt;br /&gt;
Data is exported using a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which is described in further detail below.&lt;br /&gt;
&lt;br /&gt;
====Plugins which store user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins store a variety of user preferences, and must therefore export them.&lt;br /&gt;
&lt;br /&gt;
Since user preferences are a site-wide preference, these are exported separately to other user data.&lt;br /&gt;
In some cases the only data present is user preference data, whilst in others there is a combination of user-provided data, and user preferences.&lt;br /&gt;
&lt;br /&gt;
Storing of user preferences is achieved through implementation of the &#039;&#039;\core_privacy\local\request\user_preference_provider&#039;&#039; interface which defines one required function -- &#039;&#039;export_user_preferences&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user preferences for the plugin.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int         $userid The userid of the user whose data is to be exported.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_preferences(int $userid) {&lt;br /&gt;
    $markasreadonnotification = get_user_preference(&#039;markasreadonnotification&#039;, null, $userid);&lt;br /&gt;
    if (null !== $markasreadonnotification) {&lt;br /&gt;
        switch ($markasreadonnotification) {&lt;br /&gt;
            case 0:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationno&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
            case 1:&lt;br /&gt;
            default:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationyes&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
        }&lt;br /&gt;
        writer::export_user_preference(&#039;mod_forum&#039;, &#039;markasreadonnotification&#039;, $markasreadonnotification, $markasreadonnotificationdescription);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plugins which can have own subplugins ====&lt;br /&gt;
&lt;br /&gt;
Many plugin types are also able to define their own subplugins and will need to define a contract between themselves and their subplugins in order to fetch their data.&lt;br /&gt;
&lt;br /&gt;
This is required as the parent plugin and the child subplugin should be separate entities and the parent plugin must be able to function if one or more of its subplugins are uninstalled.&lt;br /&gt;
&lt;br /&gt;
The parent plugin is responsible for defining the contract,  and for interacting with its subplugins, though we intend to create helpers to make this easier.&lt;br /&gt;
&lt;br /&gt;
The parent plugin should define a new interface for each type of subplugin that it defines. This interface should extend the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
===== When a parent plugin should and should not provide the interface for its subplugins =====&lt;br /&gt;
&lt;br /&gt;
There can be cases when there is no point for a plugin to provide the &amp;quot;subplugin_provider&amp;quot; based interface, even if it has own subplugins. See the Atto or TinyMCE editors as real examples.&lt;br /&gt;
&lt;br /&gt;
If the parent plugin has no data passed through to the subplugins, there is no benefit in defining a subplugin provider. For example, Atto subplugins are just used to enhance the functionality and they never receive anything like a context. Most of the time we need to define a subplugin provider, but in cases where there is no data passed from the plugin to its subplugins, there is no need to define the subplugin provider. If the subplugins still do store personal data that are not related to the parent plugin in any way, then subplugins should define their own standard provider.&lt;br /&gt;
&lt;br /&gt;
Compare with something like mod_assign where the subplugins store data for the parent and that data is contextually relevant to the parent plugin. In those cases the subplugin stores data for the plugin and it only makes sense to do so in the context of its parent plugin.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
The following example defines the contract that assign submission subplugins may be required to implement.&lt;br /&gt;
&lt;br /&gt;
The assignment module is responsible for returning the contexts of all assignments where a user has data, but in some cases it is unaware of all of those cases - for example if a Teacher comments on a student submission it may not be aware of these as the information about this interaction may not be stored within its own tables.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/assign/privacy/assignsubmission_provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_assign\privacy;&lt;br /&gt;
use \core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
interface assignsubmission_provider extends&lt;br /&gt;
    // This Interface defines a subplugin.&lt;br /&gt;
    \core_privacy\local\request\plugin\subplugin_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the SQL required to find all submission items where this user has had any involvements. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  \stdClass                   Object containing the join, params, and where used to select a these records from the database.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_items_with_user_interaction(int $userid) : \stdClass ;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Export all relevant user submissions information which match the combination of userid and attemptid.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @param   \context      $context      The context to export this submission against.&lt;br /&gt;
     * @param   array         $subcontext   The subcontext within the context to export this information&lt;br /&gt;
     * @param   int           $attid        The id of the submission to export.&lt;br /&gt;
     */&lt;br /&gt;
    public static function export_user_submissions(int $userid, \context $context, array $subcontext, int $attid) ;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are subplugins to another plugin====&lt;br /&gt;
&lt;br /&gt;
If you are developing a sub-plugin of another plugin, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subplugin type should define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface and it is up to the parent plugin to define how they will interact with their children.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/pluginname/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace assignsubmission\onlinetext;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does store personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is a subplugin of assign and must meet that contract.&lt;br /&gt;
    \mod_assign\privacy\assignsubmission_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are typically called by a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
There are a number of plugintypes in Moodle which are typically called by a specific Moodle subsystem.&lt;br /&gt;
&lt;br /&gt;
Some of these are &#039;&#039;only&#039;&#039; called by that subsystem, for example plugins which are of the &#039;&#039;plagiarism&#039;&#039; plugintype should never be called directly, but are always invoked via the &#039;&#039;core_plagiarism&#039;&#039; subsystem.&lt;br /&gt;
&lt;br /&gt;
Conversely, there maybe other plugintypes which can be called both via a subsystem, and in some other fashion. We are still determining whether any plugintypes currently fit this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are developing a plugin which belongs to a specific subsystem, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subsystem will define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subsystem_provider&#039;&#039; interface and it is up to that subsystem to define how they will interact with those plugins.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;plagiarism/detectorator/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace plagiarism_detectorator\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does export personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is always linked against another activity module via the Plagiarism API.&lt;br /&gt;
    \core_plagiarism\privacy\plugin_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exporting data====&lt;br /&gt;
&lt;br /&gt;
Any plugin which stores data must also export it.&lt;br /&gt;
&lt;br /&gt;
To cater for this the privacy API includes a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which defines a set of functions to store different types of data.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking data is broken into the following types:&lt;br /&gt;
&lt;br /&gt;
* Data - this is the object being described. For example the post content in a forum post;&lt;br /&gt;
* Related data - this is data related to the object being stored. For example, ratings of a forum post;&lt;br /&gt;
* Metadata - This is metadata about the main object. For example whether you are subscribed to a forum discussion;&lt;br /&gt;
* User preferences - this is data about a site-wide preference;&lt;br /&gt;
* Files - Any files that you are stored within Moodle on behalf of this plugin; and&lt;br /&gt;
* Custom files - For custom file formats - e.g. a calendar feed for calendar data. These should be used sparingly.&lt;br /&gt;
&lt;br /&gt;
Each piece of data is stored against a specific Moodle &#039;&#039;context&#039;&#039;, which will define how the data is structured within the exporter.&lt;br /&gt;
Data, and Related data only accept the &#039;&#039;stdClass&#039;&#039; object, whilst metadata should be stored as a set of key/value pairs which include a description.&lt;br /&gt;
&lt;br /&gt;
In some cases the data being stored belongs within an implicit structure. For example, one forum has many forum discussions, which each have a number of forum posts. This structure is represented by an &#039;&#039;array&#039;&#039; referred to as a &#039;&#039;subcontext&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;content_writer&#039;&#039; must &#039;&#039;always&#039;&#039; be called with a specific context, and can be called as follows:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
writer::with_context($context)&lt;br /&gt;
    -&amp;gt;export_data($subcontext, $post)&lt;br /&gt;
    -&amp;gt;export_area_files($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id)&lt;br /&gt;
    -&amp;gt;export_metadata($subcontext, &#039;postread&#039;, (object) [&#039;firstread&#039; =&amp;gt; $firstread], new \lang_string(&#039;privacy:export:post:postread&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any text field which supports Moodle files must also be rewritten:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
$post-&amp;gt;message = writer::with_context($context)&lt;br /&gt;
    -&amp;gt;rewrite_pluginfile_urls($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id, $post-&amp;gt;message);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to delete user data===&lt;br /&gt;
&lt;br /&gt;
Deleting user data is also implemented in the request interface. There are two methods that need to be created. The first one to remove all user data from a context, the other to remove user data for a specific user in a list of contexts.&lt;br /&gt;
&lt;br /&gt;
====Delete for a context====&lt;br /&gt;
&lt;br /&gt;
A context is given and all user data (for all users) is to be deleted from the plugin. This will be called when the retention period for the context has expired to adhere to the privacy by design requirement. Retention periods are set in the Data registry.&lt;br /&gt;
&lt;br /&gt;
Note that this will be called for &#039;&#039;&#039;any&#039;&#039;&#039; context being expired, not only those the plugin holds data for (as returned by get_contexts_for_userid()), so you must carefully check the contexts given.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Delete all personal data for all users in the specified context.&lt;br /&gt;
 *&lt;br /&gt;
 * @param context $context Context to delete data from.&lt;br /&gt;
 */&lt;br /&gt;
public static function delete_data_for_all_users_in_context(\context $context) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $cm = get_coursemodule_from_id(&#039;choice&#039;, $context-&amp;gt;instanceid);&lt;br /&gt;
    if (!$cm) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $cm-&amp;gt;instance]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Delete personal information for a specific user and context(s)====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;approved_contextlist&#039;&#039; is given and user data related to that user should either be completely deleted, or overwritten if a structure needs to be maintained. This will be called when a user has requested the right to be forgotten. All attempts should be made to delete this data where practical while still allowing the plugin to be used by other users.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/choice/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function delete_data_for_user(approved_contextlist $contextlist) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
    &lt;br /&gt;
    if (empty($contextlist-&amp;gt;count())) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    $userid = $contextlist-&amp;gt;get_user()-&amp;gt;id;&lt;br /&gt;
    foreach ($contextlist-&amp;gt;get_contexts() as $context) {&lt;br /&gt;
        $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid, &#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Difference between Moodle 3.3 and more recent versions==&lt;br /&gt;
Moodle 3.3 has a minimum requirement of php 5.6 and so type hinting and return type declarations are not supported in this version. &lt;br /&gt;
Consequently the privacy API for this version does not have these features.&lt;br /&gt;
==Common Questions==&lt;br /&gt;
===What to do if you have one plugin that supports multiple branches===&lt;br /&gt;
This is something that we have considered and we have put in place a polyfill. This gets around the restrictions of one version having type hinting and return type declarations while another does not.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To use the polyfill include the legacy polyfill trait and create the necessary static methods but with an underscore (shown below).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
    \core_privacy\local\request\plugin\provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What to do if your plugin must implement a subplugin or subsystem plugin provider===&lt;br /&gt;
For subplugins (e.g. assignsubmission, assignfeedback, quiz report, quiz access rules), or subsystems which have a plugintype relationship (portfolio, plagiarism, and others), they will also define their own legacy polyfill.&lt;br /&gt;
&lt;br /&gt;
In this instance you will need to include the trait for both the core polyfill, and the provider polyfill as appropriate.&lt;br /&gt;
==== Example ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin has data and must therefore define the metadata provider in order to describe it.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This is a plagiarism plugin. It interacts with the plagiarism subsystem rather than with core.&lt;br /&gt;
    \core_plagiarism\privacy\plagiarism_provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the metadata provider.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the plagirism provider.&lt;br /&gt;
    use \core_plagiarism\privacy\plagiarism_provider\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // This is one of the polyfilled methods from the plagiarism provider.&lt;br /&gt;
    public static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for development ==&lt;br /&gt;
&lt;br /&gt;
* While implementing the privacy API into your plugin, there are CLI scripts that can help you to test things on the fly. Just don&#039;t forget these are not supposed to replace proper unit tests. See [[Privacy API/Utilities]] for details.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Subject Access Request FAQ]]&lt;br /&gt;
* [[:en:GDPR|GDPR]] in the user documentation&lt;br /&gt;
&lt;br /&gt;
[[Category:Privacy]]&lt;br /&gt;
[[Category:GDPR]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54158</id>
		<title>Privacy API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54158"/>
		<updated>2018-05-14T11:12:51Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Delete for a context */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [https://en.wikipedia.org/wiki/General_Data_Protection_Regulation General Data Protection Regulation] (GDPR) is an EU directive that looks at providing users with more control over their data and how it is processed. This regulation will come into effect on 25th of May 2018 and covers any citizen or permanent resident of the European Union. The directive will be respected by a number of other countries outside of the European Union.&lt;br /&gt;
&lt;br /&gt;
To help institutions become compliant with this new regulation we are adding functionality to Moodle. This includes a number of components, amongst others these include a user’s right to:&lt;br /&gt;
&lt;br /&gt;
* request information on the types of personal data held, the instances of that data, and the deletion policy for each;&lt;br /&gt;
* access all of their data; and&lt;br /&gt;
* be forgotten.&lt;br /&gt;
&lt;br /&gt;
The compliance requirements also extend to installed plugins (including third party plugins). These need to also be able to report what information they store or process regarding users, and have the ability to provide and delete data for a user request.&lt;br /&gt;
&lt;br /&gt;
This document describes the proposed API changes required for plugins which will allow a Moodle installation to become GDPR compliant.&lt;br /&gt;
&lt;br /&gt;
Target Audience: The intended audience for this document is Moodle plugin developers, who are aiming to ensure their plugins are updated to comply with GDPR requirements coming into effect in the EU in May, 2018.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Personal data in Moodle==&lt;br /&gt;
&lt;br /&gt;
From the GDPR Spec, Article 4:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;‘personal data’ means any information relating to an identified or identifiable natural person (‘data subject’); an identifiable natural person is one who can be identified, directly or indirectly, in particular by reference to an identifier such as a name, an identification number, location data, an online identifier or to one or more factors specific to the physical, physiological, genetic, mental, economic, cultural or social identity of that natural person;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Moodle, we need to consider two main types of personal data; information entered by the user and information stored about the user. The key difference being that information stored about the user will have come from a source other than the user themselves. Both types of data can be used to form a profile of the individual.&lt;br /&gt;
&lt;br /&gt;
The most obvious clue to finding personal data entered by the user is the presence of a userid on a database field. Any data on the record (or linked records) pertaining to that user may be deemed personal data for that user, including things like timestamps and record identification numbers. Additionally, any free text field which allows the user to enter information must also be considered to be the personal data of that user.&lt;br /&gt;
&lt;br /&gt;
Data stored about the user includes things like ratings and comments made on a student submission. These may have been made by an assessor or teacher, but are considered the personal data of the student, as they are considered a reflection of the user’s competency in the subject matter and can be used to form a profile of that individual. &lt;br /&gt;
&lt;br /&gt;
The sections that follow outline what you need to do as a plugin developer to ensure any personal data is advertised and can be accessed and deleted according to the GDPR requirements.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
===Architecture overview===&lt;br /&gt;
&lt;br /&gt;
[[File:MoodlePrivacyMetadataUML.png|thumb|UML diagram of the metadata part of the privacy subsystem]]&lt;br /&gt;
[[File:MoodlePrivacyRequestUML.png|thumb|UML diagram of the request providers part of the privacy subsystem]]&lt;br /&gt;
&lt;br /&gt;
A new system for Privacy has been created within Moodle. This is broken down into several main parts and forms the &#039;&#039;core_privacy&#039;&#039; subsystem:&lt;br /&gt;
&lt;br /&gt;
* Some metadata providers - a set of PHP interfaces to be implemented by components for that component to describe the kind of data that it stores, and the purpose for its storage;&lt;br /&gt;
* Some request providers - a set of PHP interfaces to be implemented by components to allow that component to act upon user requests such as the Right to be Forgotten, and a Subject Access Request; and&lt;br /&gt;
* A manager - a concrete class used to bridge components which implement the providers with tools which request their data.&lt;br /&gt;
&lt;br /&gt;
All plugins will implement one metadata provider, and zero, one or two request providers.&lt;br /&gt;
&lt;br /&gt;
The fetching of data is broken into two separate steps:&lt;br /&gt;
&lt;br /&gt;
# Detecting in which Moodle contexts the user has any data; and&lt;br /&gt;
# Exporting all data from each of those contexts.&lt;br /&gt;
&lt;br /&gt;
This has been broken into two steps to later allow administrators to exclude certain contexts from an export - e.g. for courses currently in progress.&lt;br /&gt;
&lt;br /&gt;
A third component will later be added to facilitate the deletion of data within these contexts which will help to satisfy the Right to be Forgotten. This will also use the first step.&lt;br /&gt;
&lt;br /&gt;
Please refer to the inline phpdocs of the [https://github.com/moodle/moodle/blob/v3.5.0-beta/privacy/classes/manager.php#L31 core_privacy::manager class] for detailed description of the interfaces, their hierarchy and meaning.&lt;br /&gt;
&lt;br /&gt;
===Implementing a provider===&lt;br /&gt;
&lt;br /&gt;
All plugins will need to create a concrete class which implements the relevant metadata and request providers. The exact providers you need to implement will depend on what data you store, and the type of plugin. This is covered in more detail in the following sections of the document.&lt;br /&gt;
&lt;br /&gt;
In order to do so:&lt;br /&gt;
&lt;br /&gt;
# You must create a class called &#039;&#039;provider&#039;&#039; within the namespace &#039;&#039;\your_pluginname\privacy&#039;&#039;.&lt;br /&gt;
# This class must be created at &#039;&#039;path/to/your/plugin/classes/privacy/provider.php&#039;&#039;.&lt;br /&gt;
# You must have your class implement the relevant metadata and request interfaces.&lt;br /&gt;
&lt;br /&gt;
==Plugins which do not store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do not store any personal data. This is usually the case for plugins which just add functionality, or which display the data already stored elsewhere in Moodle.&lt;br /&gt;
&lt;br /&gt;
Some examples of plugin types which might fit this criteria include themes, blocks, filters, editor plugins, etc.&lt;br /&gt;
&lt;br /&gt;
Plugins which cause data to be stored elsewhere in Moodle (e.g. via a subsystem call) are considered to store data.&lt;br /&gt;
&lt;br /&gt;
One examples of a plugin which does not store any data would be the Calendar month block which just displays a view of the user’s calendar. It does not store any data itself.&lt;br /&gt;
&lt;br /&gt;
An example of a plugin which must not use the null provider is the Comments block. The comments block is responsible for data subsequently being stored within Moodle. Although the block doesn’t store anything itself, it interacts with the comments subsystem and is the only component which knows how that data maps to a user.&lt;br /&gt;
&lt;br /&gt;
===Implementation requirements===&lt;br /&gt;
&lt;br /&gt;
In order to let Moodle know that you have audited your plugin, and that you do not store any personal user data, you must implement the &#039;&#039;\core_privacy\local\metadata\null_provider&#039;&#039; interface in your plugin’s provider.&lt;br /&gt;
&lt;br /&gt;
These null providers can only be implemented where a plugin has:&lt;br /&gt;
&lt;br /&gt;
* no external links (e.g. sends data to an external service like an LTI provider, repository plugin which you can search on)&lt;br /&gt;
* no database tables which store user data (including IP addresses)&lt;br /&gt;
* no user preferences&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;null_provider&#039;&#039; requires you to define one function &#039;&#039;get_reason()&#039;&#039; which returns the language string identifier within your component.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace block_calendar_month\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
    // This plugin does not store any personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\null_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the language string identifier with the component&#039;s language&lt;br /&gt;
     * file to explain why this plugin stores no data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_reason() : string {&lt;br /&gt;
        return &#039;privacy:metadata&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/lang/en/block_calendar_month.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
$string[&#039;privacy:metadata&#039;] = &#039;The Calendar block only displays existing calendar data.&#039;;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That’s it. Congratulations, your plugin now implements the Privacy API.&lt;br /&gt;
&lt;br /&gt;
==Plugins which store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do store some form of personal data.&lt;br /&gt;
&lt;br /&gt;
In some cases this will be stored within database tables in your plugin, and in other cases this will be in one of Moodle’s core subsystems - for example your plugin may store files, ratings, comments, or tags.&lt;br /&gt;
&lt;br /&gt;
Plugins which do store data will need to:&lt;br /&gt;
&lt;br /&gt;
* Describe the type of data that they store; &lt;br /&gt;
* Provide a way to export that data; and&lt;br /&gt;
* Provide a way to delete that data.&lt;br /&gt;
&lt;br /&gt;
Data is described via a &#039;&#039;metadata&#039;&#039; provider, and it is both exported and deleted via an implementation of a &#039;&#039;request&#039;&#039; provider.&lt;br /&gt;
&lt;br /&gt;
These are both explained in the sections below.&lt;br /&gt;
&lt;br /&gt;
===Describing the type of data you store===&lt;br /&gt;
&lt;br /&gt;
In order to describe the type of data that you store, you must implement the &#039;&#039;\core_privacy\local\metadata\provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
This interfaces requires that you define one function: &#039;&#039;get_metadata&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
There are several types of item to describe the data that you store. These are for:&lt;br /&gt;
&lt;br /&gt;
* Items in the Moodle database;&lt;br /&gt;
* Items stored by you in a Moodle subsystem - for example files, and ratings; and&lt;br /&gt;
* User preferences stored site-wide within Moodle for your plugin&lt;br /&gt;
&lt;br /&gt;
Note: All fields should include a description from a language string within your plugin.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_forum\privacy;&lt;br /&gt;
use core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
        // This plugin does store personal user data.&lt;br /&gt;
        \core_privacy\local\metadata\provider {&lt;br /&gt;
&lt;br /&gt;
    public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
        // Here you will add more items into the collection.&lt;br /&gt;
&lt;br /&gt;
        return $collection;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store content in a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
Many plugins will use one of the core Moodle subsystems to store data.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer we do not expect you to describe those subsystems in detail, but we do need to know that you use them and to know what you use them for.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_subsystem_link()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_subsystem_link(&lt;br /&gt;
        &#039;core_files&#039;,&lt;br /&gt;
        [],&lt;br /&gt;
        &#039;privacy:metadata:core_files&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:core_files&#039;] = &#039;The forum stores files which have been uploaded by the user to form part of a forum post.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Describing data stored in database tables====&lt;br /&gt;
&lt;br /&gt;
Most Moodle plugins will store some form of user data in their own database tables.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each database table, and each field which includes user data.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_database_table(&lt;br /&gt;
        &#039;forum_discussion_subs&#039;,&lt;br /&gt;
         [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:userid&#039;,&lt;br /&gt;
            &#039;discussionid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:discussionid&#039;,&lt;br /&gt;
            &#039;preference&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:preference&#039;,&lt;br /&gt;
&lt;br /&gt;
         ],&lt;br /&gt;
        &#039;privacy:metadata:forum_discussion_subs&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs&#039;] = &#039;Information about the subscriptions to individual forum discussions. This includes when a user has chosen to subscribe to a discussion, or to unsubscribe from one where they would otherwise be subscribed.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:userid&#039;] = &#039;The ID of the user with this subscription preference.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:discussionid&#039;] = &#039;The ID of the discussion that was subscribed to.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:preference&#039;] = &#039;The start time of the subscription.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store site-wide user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins will include one or more user preferences. Unfortunately this is one of Moodle’s older components and many of the values stored are not pure user preferences. Each plugin should be aware of how it handles its own preferences and is best placed to determine whether they are site-wide preferences, or per-instance preferences.&lt;br /&gt;
&lt;br /&gt;
Whilst most of these will have a fixed name (e.g. &#039;&#039;filepicker_recentrepository&#039;&#039;), some will include a variable of some kind (e.g. &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039;). Only the general name needs to be indicated rather than one copy for each preference.&lt;br /&gt;
&lt;br /&gt;
Also, these should only be &#039;&#039;site-wide&#039;&#039; user preferences which do not belong to a specific Moodle context.&lt;br /&gt;
&lt;br /&gt;
In the above examples:&lt;br /&gt;
&lt;br /&gt;
* Preference &#039;&#039;filepicker_recentrepository&#039;&#039; belongs to the file subsystem, and is a site-wide preference affecting the user anywhere that they view the filepicker.&lt;br /&gt;
* Preference &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039; belongs to user tours. User tours are a site-wide feature which can affect many parts of Moodle and cross multiple contexts.&lt;br /&gt;
&lt;br /&gt;
In some cases a value may be stored in the preferences table but is known to belong to a specific context within Moodle. In these cases they should be stored as metadata against that context rather than as a site-wide user preference.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_user_preference()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Any plugin providing user preferences must also implement the &#039;&#039;\core_privacy\local\request\preference_provider&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_user_preference(&#039;tool_usertours_tour_completion_time&#039;,&lt;br /&gt;
        &#039;privacy:metadata:preference:tool_usertours_tour_completion_time&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/lang/en/tool_usertours.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:tool_usertours_tour_completion_time&#039;] = &#039;The time that a specific user tour was last completed by a user.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you export data to an external location====&lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with external systems - for example cloud-based services. Often this external location is configurable within the plugin either at the site or the instance level.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each &#039;&#039;type&#039;&#039; of target destination, alongside a list of each exported field which includes user data.&lt;br /&gt;
The &#039;&#039;actual&#039;&#039; destination does not need to be described as this can change based on configuration.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_external_location_link()&#039;&#039; method on the collection.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_external_location_link(&#039;lti_client&#039;, [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:lti_client:userid&#039;,&lt;br /&gt;
            &#039;fullname&#039; =&amp;gt; &#039;privacy:metadata:lti_client:fullname&#039;,&lt;br /&gt;
        ], &#039;privacy:metadata:lti_client&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/lang/en/lti.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client&#039;] = &#039;In order to integrate with a remote LTI service, user data needs to be exchanged with that service.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:userid&#039;] = &#039;The userid is sent from Moodle to allow you to access your data on the remote system.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:fullname&#039;] = &#039;Your full name is sent to the remote system to allow a better user experience.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to export user data===&lt;br /&gt;
&lt;br /&gt;
In order to export the user data that you store, you must implement the relevant request provider.&lt;br /&gt;
&lt;br /&gt;
We have named these request providers because they are called in response to a specific request from a user to access their information.&lt;br /&gt;
&lt;br /&gt;
There are several different types of request provider, and you may need to implement several of these, depending on the type and nature of your plugin.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking plugins will fit into one of the following categories:&lt;br /&gt;
&lt;br /&gt;
* Plugins which are a subplugin of another plugin. Examples include &#039;&#039;assignsubmission&#039;&#039;, &#039;&#039;atto&#039;&#039;, and &#039;&#039;datafield&#039;&#039;;&lt;br /&gt;
* Plugins which are typically called by a Moodle subsystem. Examples include &#039;&#039;qtype&#039;&#039;, and &#039;&#039;profilefield&#039;&#039;;&lt;br /&gt;
* All other plugins which store data.&lt;br /&gt;
&lt;br /&gt;
Most plugins will fit into this final category, whilst other plugins may fall into several categories.&lt;br /&gt;
Plugins which &#039;&#039;define&#039;&#039; a subplugin will also be responsible for  collecting this data from their subplugins.&lt;br /&gt;
&lt;br /&gt;
A final category exists - plugins which store user preferences. In some cases this may be the &#039;&#039;only&#039;&#039; provider implemented.&lt;br /&gt;
&lt;br /&gt;
====Standard plugins which store data====&lt;br /&gt;
&lt;br /&gt;
A majority of Moodle plugins will fit into this category and will be required to implement the &#039;&#039;\core_privacy\local\request\plugin\provider&#039;&#039; interface. This interface requires that you define four functions (the first two of which are dealt with in this section):&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;get_contexts_for_userid&#039;&#039; - to explain where data is held within Moodle for your plugin; and&lt;br /&gt;
* &#039;&#039;export_user_data&#039;&#039; - to export a user’s personal data from your plugin.&lt;br /&gt;
* &#039;&#039;delete_data_for_all_users_in_context&#039;&#039; - to delete all data for all users in the specified context.&lt;br /&gt;
* &#039;&#039;delete_data_for_user&#039;&#039; - to delete all user data for the specified user, in the specified contexts.&lt;br /&gt;
&lt;br /&gt;
These APIs make use of the Moodle &#039;&#039;context&#039;&#039; system to hierarchically store this data.&lt;br /&gt;
&lt;br /&gt;
=====Retrieving the list of contexts=====&lt;br /&gt;
&lt;br /&gt;
Contexts are retrieved using the &#039;&#039;get_contexts_for_userid&#039;&#039; function which takes the ID of the user being fetched, and returns a list of contexts in which the user has any data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function returns a &#039;&#039;\core_privacy\local\request\contextlist&#039;&#039; which is used to keep a set of contexts together in a fixed fashion.&lt;br /&gt;
&lt;br /&gt;
Because a Subject Access Request covers &#039;&#039;every&#039;&#039; piece of data that is held for a user within Moodle, efficiency and performance is highly important. As a result, contexts are added to the &#039;&#039;contextlist&#039;&#039; by defining one or more SQL queries which return just the contextid. Multiple SQL queries can be added as required.&lt;br /&gt;
&lt;br /&gt;
To decide which type of context to return (CONTEXT_COURSE, CONTEXT_USER etc.) consider the most appropriate type for your plugin. Typically, this will be the same type of context as you would create to check capabilities. For example, an enrolment plugin will return the course contexts in which it is used. &lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with specific subsystems and store data within them.&lt;br /&gt;
These subsystems will also provide a way in which to link the data that you have stored with your own database tables.&lt;br /&gt;
At present these are still a work in progress and only the &#039;&#039;core_ratings&#039;&#039; subsystem includes this.&lt;br /&gt;
&lt;br /&gt;
======Basic example======&lt;br /&gt;
&lt;br /&gt;
The following example simply fetches the contextid for all forums where a user has a single discussion (note: this is an incomplete example):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
        $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
&lt;br /&gt;
        $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                 FROM {context} c&lt;br /&gt;
           INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
           INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
           INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
                WHERE (&lt;br /&gt;
                d.userid        = :discussionuserid&lt;br /&gt;
                )&lt;br /&gt;
        &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        $params = [&lt;br /&gt;
            &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
            &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
            &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        ];&lt;br /&gt;
&lt;br /&gt;
        $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
        return $contextlist;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
======More complete example======&lt;br /&gt;
&lt;br /&gt;
The following example includes a link to core_rating. &lt;br /&gt;
It will find any forum, forum discussion, or forum post where the user has any data, including:&lt;br /&gt;
&lt;br /&gt;
* Per-forum digest preferences;&lt;br /&gt;
* Per-forum subscription preferences;&lt;br /&gt;
* Per-forum read tracking preferences;&lt;br /&gt;
* Per-discussion subscription preferences;&lt;br /&gt;
* Per-post read data (if a user has read a post or not); and&lt;br /&gt;
* Per-post rating data.&lt;br /&gt;
&lt;br /&gt;
In the case of the rating data, this will include any post where the user has rated the post of another user.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int           $userid       The user to search.&lt;br /&gt;
 * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
 */&lt;br /&gt;
public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
    $ratingsql = \core_rating\privacy\provider::get_sql_join(&#039;rat&#039;, &#039;mod_forum&#039;, &#039;post&#039;, &#039;p.id&#039;, $userid);&lt;br /&gt;
    // Fetch all forum discussions, and forum posts.&lt;br /&gt;
    $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                FROM {context} c&lt;br /&gt;
        INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
        INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
        INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_posts} p ON p.discussion = d.id&lt;br /&gt;
            LEFT JOIN {forum_digests} dig ON dig.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_subscriptions} sub ON sub.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_track_prefs} pref ON pref.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_read} hasread ON hasread.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_discussion_subs} dsub ON dsub.forum = f.id&lt;br /&gt;
            {$ratingsql-&amp;gt;join}&lt;br /&gt;
                WHERE (&lt;br /&gt;
                p.userid        = :postuserid OR&lt;br /&gt;
                d.userid        = :discussionuserid OR&lt;br /&gt;
                dig.userid      = :digestuserid OR&lt;br /&gt;
                sub.userid      = :subuserid OR&lt;br /&gt;
                pref.userid     = :prefuserid OR&lt;br /&gt;
                hasread.userid  = :hasreaduserid OR&lt;br /&gt;
                dsub.userid     = :dsubuserid OR&lt;br /&gt;
                {$ratingsql-&amp;gt;userwhere}&lt;br /&gt;
            )&lt;br /&gt;
    &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    $params = [&lt;br /&gt;
        &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
        &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &#039;postuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        &#039;digestuserid&#039;      =&amp;gt; $userid,&lt;br /&gt;
        &#039;subuserid&#039;         =&amp;gt; $userid,&lt;br /&gt;
        &#039;prefuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;hasreaduserid&#039;     =&amp;gt; $userid,&lt;br /&gt;
        &#039;dsubuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
    ];&lt;br /&gt;
    $params += $ratingsql-&amp;gt;params;&lt;br /&gt;
&lt;br /&gt;
    $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
    $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
    return $contextlist;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Exporting user data=====&lt;br /&gt;
&lt;br /&gt;
After determining where in Moodle your plugin holds data about a user, the &#039;&#039;\core_privacy\manager&#039;&#039; will then ask your plugin to export all user data for a subset of those locations.&lt;br /&gt;
&lt;br /&gt;
This is achieved through use of the &#039;&#039;export_user_data&#039;&#039; function which takes the list of approved contexts in a &#039;&#039;\core_privacy\local\request\approved_contextlist&#039;&#039; object.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   approved_contextlist    $contextlist    The approved contexts to export information for.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_data(approved_contextlist $contextlist) {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;approved_contextlist&#039;&#039; includes both the user record, and a list of contexts, which can be retrieved by either processing it as an Iterator, or by calling &#039;&#039;get_contextids()&#039;&#039; or &#039;&#039;get_contexts()&#039;&#039; as required.&lt;br /&gt;
&lt;br /&gt;
Data is exported using a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which is described in further detail below.&lt;br /&gt;
&lt;br /&gt;
====Plugins which store user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins store a variety of user preferences, and must therefore export them.&lt;br /&gt;
&lt;br /&gt;
Since user preferences are a site-wide preference, these are exported separately to other user data.&lt;br /&gt;
In some cases the only data present is user preference data, whilst in others there is a combination of user-provided data, and user preferences.&lt;br /&gt;
&lt;br /&gt;
Storing of user preferences is achieved through implementation of the &#039;&#039;\core_privacy\local\request\user_preference_provider&#039;&#039; interface which defines one required function -- &#039;&#039;export_user_preferences&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user preferences for the plugin.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int         $userid The userid of the user whose data is to be exported.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_preferences(int $userid) {&lt;br /&gt;
    $markasreadonnotification = get_user_preference(&#039;markasreadonnotification&#039;, null, $userid);&lt;br /&gt;
    if (null !== $markasreadonnotification) {&lt;br /&gt;
        switch ($markasreadonnotification) {&lt;br /&gt;
            case 0:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationno&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
            case 1:&lt;br /&gt;
            default:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationyes&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
        }&lt;br /&gt;
        writer::export_user_preference(&#039;mod_forum&#039;, &#039;markasreadonnotification&#039;, $markasreadonnotification, $markasreadonnotificationdescription);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plugins which can have own subplugins ====&lt;br /&gt;
&lt;br /&gt;
Many plugin types are also able to define their own subplugins and will need to define a contract between themselves and their subplugins in order to fetch their data.&lt;br /&gt;
&lt;br /&gt;
This is required as the parent plugin and the child subplugin should be separate entities and the parent plugin must be able to function if one or more of its subplugins are uninstalled.&lt;br /&gt;
&lt;br /&gt;
The parent plugin is responsible for defining the contract,  and for interacting with its subplugins, though we intend to create helpers to make this easier.&lt;br /&gt;
&lt;br /&gt;
The parent plugin should define a new interface for each type of subplugin that it defines. This interface should extend the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
===== When a parent plugin should and should not provide the interface for its subplugins =====&lt;br /&gt;
&lt;br /&gt;
There can be cases when there is no point for a plugin to provide the &amp;quot;subplugin_provider&amp;quot; based interface, even if it has own subplugins. See the Atto or TinyMCE editors as real examples.&lt;br /&gt;
&lt;br /&gt;
If the parent plugin has no data passed through to the subplugins, there is no benefit in defining a subplugin provider. For example, Atto subplugins are just used to enhance the functionality and they never receive anything like a context. Most of the time we need to define a subplugin provider, but in cases where there is no data passed from the plugin to its subplugins, there is no need to define the subplugin provider. If the subplugins still do store personal data that are not related to the parent plugin in any way, then subplugins should define their own standard provider.&lt;br /&gt;
&lt;br /&gt;
Compare with something like mod_assign where the subplugins store data for the parent and that data is contextually relevant to the parent plugin. In those cases the subplugin stores data for the plugin and it only makes sense to do so in the context of its parent plugin.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
The following example defines the contract that assign submission subplugins may be required to implement.&lt;br /&gt;
&lt;br /&gt;
The assignment module is responsible for returning the contexts of all assignments where a user has data, but in some cases it is unaware of all of those cases - for example if a Teacher comments on a student submission it may not be aware of these as the information about this interaction may not be stored within its own tables.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/assign/privacy/assignsubmission_provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_assign\privacy;&lt;br /&gt;
use \core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
interface assignsubmission_provider extends&lt;br /&gt;
    // This Interface defines a subplugin.&lt;br /&gt;
    \core_privacy\local\request\plugin\subplugin_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the SQL required to find all submission items where this user has had any involvements. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  \stdClass                   Object containing the join, params, and where used to select a these records from the database.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_items_with_user_interaction(int $userid) : \stdClass ;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Export all relevant user submissions information which match the combination of userid and attemptid.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @param   \context      $context      The context to export this submission against.&lt;br /&gt;
     * @param   array         $subcontext   The subcontext within the context to export this information&lt;br /&gt;
     * @param   int           $attid        The id of the submission to export.&lt;br /&gt;
     */&lt;br /&gt;
    public static function export_user_submissions(int $userid, \context $context, array $subcontext, int $attid) ;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are subplugins to another plugin====&lt;br /&gt;
&lt;br /&gt;
If you are developing a sub-plugin of another plugin, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subplugin type should define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface and it is up to the parent plugin to define how they will interact with their children.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/pluginname/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace assignsubmission\onlinetext;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does store personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is a subplugin of assign and must meet that contract.&lt;br /&gt;
    \mod_assign\privacy\assignsubmission_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are typically called by a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
There are a number of plugintypes in Moodle which are typically called by a specific Moodle subsystem.&lt;br /&gt;
&lt;br /&gt;
Some of these are &#039;&#039;only&#039;&#039; called by that subsystem, for example plugins which are of the &#039;&#039;plagiarism&#039;&#039; plugintype should never be called directly, but are always invoked via the &#039;&#039;core_plagiarism&#039;&#039; subsystem.&lt;br /&gt;
&lt;br /&gt;
Conversely, there maybe other plugintypes which can be called both via a subsystem, and in some other fashion. We are still determining whether any plugintypes currently fit this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are developing a plugin which belongs to a specific subsystem, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subsystem will define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subsystem_provider&#039;&#039; interface and it is up to that subsystem to define how they will interact with those plugins.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;plagiarism/detectorator/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace plagiarism_detectorator\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does export personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is always linked against another activity module via the Plagiarism API.&lt;br /&gt;
    \core_plagiarism\privacy\plugin_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exporting data====&lt;br /&gt;
&lt;br /&gt;
Any plugin which stores data must also export it.&lt;br /&gt;
&lt;br /&gt;
To cater for this the privacy API includes a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which defines a set of functions to store different types of data.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking data is broken into the following types:&lt;br /&gt;
&lt;br /&gt;
* Data - this is the object being described. For example the post content in a forum post;&lt;br /&gt;
* Related data - this is data related to the object being stored. For example, ratings of a forum post;&lt;br /&gt;
* Metadata - This is metadata about the main object. For example whether you are subscribed to a forum discussion;&lt;br /&gt;
* User preferences - this is data about a site-wide preference;&lt;br /&gt;
* Files - Any files that you are stored within Moodle on behalf of this plugin; and&lt;br /&gt;
* Custom files - For custom file formats - e.g. a calendar feed for calendar data. These should be used sparingly.&lt;br /&gt;
&lt;br /&gt;
Each piece of data is stored against a specific Moodle &#039;&#039;context&#039;&#039;, which will define how the data is structured within the exporter.&lt;br /&gt;
Data, and Related data only accept the &#039;&#039;stdClass&#039;&#039; object, whilst metadata should be stored as a set of key/value pairs which include a description.&lt;br /&gt;
&lt;br /&gt;
In some cases the data being stored belongs within an implicit structure. For example, one forum has many forum discussions, which each have a number of forum posts. This structure is represented by an &#039;&#039;array&#039;&#039; referred to as a &#039;&#039;subcontext&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;content_writer&#039;&#039; must &#039;&#039;always&#039;&#039; be called with a specific context, and can be called as follows:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
writer::with_context($context)&lt;br /&gt;
    -&amp;gt;export_data($subcontext, $post)&lt;br /&gt;
    -&amp;gt;export_area_files($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id)&lt;br /&gt;
    -&amp;gt;export_metadata($subcontext, &#039;postread&#039;, (object) [&#039;firstread&#039; =&amp;gt; $firstread], new \lang_string(&#039;privacy:export:post:postread&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any text field which supports Moodle files must also be rewritten:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
$post-&amp;gt;message = writer::with_context($context)&lt;br /&gt;
    -&amp;gt;rewrite_pluginfile_urls($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id, $post-&amp;gt;message);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to delete user data===&lt;br /&gt;
&lt;br /&gt;
Deleting user data is also implemented in the request interface. There are two methods that need to be created. The first one to remove all user data from a context, the other to remove user data for a specific user in a list of contexts.&lt;br /&gt;
&lt;br /&gt;
====Delete for a context====&lt;br /&gt;
&lt;br /&gt;
A context is given and all user data (for all users) is to be deleted from the plugin. This will be called when the retention period for the context has expired to adhere to the privacy by design requirement. Retention periods are set in the Data registry.&lt;br /&gt;
&lt;br /&gt;
Note that this will be called for &#039;&#039;&#039;any&#039;&#039;&#039; context being expired, not only those the plugin holds data for (as returned by get_contexts_for_userid()), so you must carefully check the contexts given.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Delete all personal data for all users in the specified context.&lt;br /&gt;
 *&lt;br /&gt;
 * @param context $context Context to delete data from.&lt;br /&gt;
 */&lt;br /&gt;
public static function delete_data_for_all_users_in_context(\context $context) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
     if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $cm = get_coursemodule_from_id(&#039;choice&#039;, $context-&amp;gt;instanceid);&lt;br /&gt;
    if (!$cm) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $cm-&amp;gt;instance]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Delete personal information for a specific user and context(s)====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;approved_contextlist&#039;&#039; is given and user data related to that user should either be completely deleted, or overwritten if a structure needs to be maintained. This will be called when a user has requested the right to be forgotten. All attempts should be made to delete this data where practical while still allowing the plugin to be used by other users.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/choice/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function delete_data_for_user(approved_contextlist $contextlist) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
    &lt;br /&gt;
    if (empty($contextlist-&amp;gt;count())) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    $userid = $contextlist-&amp;gt;get_user()-&amp;gt;id;&lt;br /&gt;
    foreach ($contextlist-&amp;gt;get_contexts() as $context) {&lt;br /&gt;
        $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid, &#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Difference between Moodle 3.3 and more recent versions==&lt;br /&gt;
Moodle 3.3 has a minimum requirement of php 5.6 and so type hinting and return type declarations are not supported in this version. &lt;br /&gt;
Consequently the privacy API for this version does not have these features.&lt;br /&gt;
==Common Questions==&lt;br /&gt;
===What to do if you have one plugin that supports multiple branches===&lt;br /&gt;
This is something that we have considered and we have put in place a polyfill. This gets around the restrictions of one version having type hinting and return type declarations while another does not.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To use the polyfill include the legacy polyfill trait and create the necessary static methods but with an underscore (shown below).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
    \core_privacy\local\request\plugin\provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What to do if your plugin must implement a subplugin or subsystem plugin provider===&lt;br /&gt;
For subplugins (e.g. assignsubmission, assignfeedback, quiz report, quiz access rules), or subsystems which have a plugintype relationship (portfolio, plagiarism, and others), they will also define their own legacy polyfill.&lt;br /&gt;
&lt;br /&gt;
In this instance you will need to include the trait for both the core polyfill, and the provider polyfill as appropriate.&lt;br /&gt;
==== Example ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin has data and must therefore define the metadata provider in order to describe it.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This is a plagiarism plugin. It interacts with the plagiarism subsystem rather than with core.&lt;br /&gt;
    \core_plagiarism\privacy\plagiarism_provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the metadata provider.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the plagirism provider.&lt;br /&gt;
    use \core_plagiarism\privacy\plagiarism_provider\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // This is one of the polyfilled methods from the plagiarism provider.&lt;br /&gt;
    public static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for development ==&lt;br /&gt;
&lt;br /&gt;
* While implementing the privacy API into your plugin, there are CLI scripts that can help you to test things on the fly. Just don&#039;t forget these are not supposed to replace proper unit tests. See [[Privacy API/Utilities]] for details.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Subject Access Request FAQ]]&lt;br /&gt;
* [[:en:GDPR|GDPR]] in the user documentation&lt;br /&gt;
&lt;br /&gt;
[[Category:Privacy]]&lt;br /&gt;
[[Category:GDPR]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54157</id>
		<title>Privacy API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54157"/>
		<updated>2018-05-14T11:08:40Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Delete for a context */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [https://en.wikipedia.org/wiki/General_Data_Protection_Regulation General Data Protection Regulation] (GDPR) is an EU directive that looks at providing users with more control over their data and how it is processed. This regulation will come into effect on 25th of May 2018 and covers any citizen or permanent resident of the European Union. The directive will be respected by a number of other countries outside of the European Union.&lt;br /&gt;
&lt;br /&gt;
To help institutions become compliant with this new regulation we are adding functionality to Moodle. This includes a number of components, amongst others these include a user’s right to:&lt;br /&gt;
&lt;br /&gt;
* request information on the types of personal data held, the instances of that data, and the deletion policy for each;&lt;br /&gt;
* access all of their data; and&lt;br /&gt;
* be forgotten.&lt;br /&gt;
&lt;br /&gt;
The compliance requirements also extend to installed plugins (including third party plugins). These need to also be able to report what information they store or process regarding users, and have the ability to provide and delete data for a user request.&lt;br /&gt;
&lt;br /&gt;
This document describes the proposed API changes required for plugins which will allow a Moodle installation to become GDPR compliant.&lt;br /&gt;
&lt;br /&gt;
Target Audience: The intended audience for this document is Moodle plugin developers, who are aiming to ensure their plugins are updated to comply with GDPR requirements coming into effect in the EU in May, 2018.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Personal data in Moodle==&lt;br /&gt;
&lt;br /&gt;
From the GDPR Spec, Article 4:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;‘personal data’ means any information relating to an identified or identifiable natural person (‘data subject’); an identifiable natural person is one who can be identified, directly or indirectly, in particular by reference to an identifier such as a name, an identification number, location data, an online identifier or to one or more factors specific to the physical, physiological, genetic, mental, economic, cultural or social identity of that natural person;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Moodle, we need to consider two main types of personal data; information entered by the user and information stored about the user. The key difference being that information stored about the user will have come from a source other than the user themselves. Both types of data can be used to form a profile of the individual.&lt;br /&gt;
&lt;br /&gt;
The most obvious clue to finding personal data entered by the user is the presence of a userid on a database field. Any data on the record (or linked records) pertaining to that user may be deemed personal data for that user, including things like timestamps and record identification numbers. Additionally, any free text field which allows the user to enter information must also be considered to be the personal data of that user.&lt;br /&gt;
&lt;br /&gt;
Data stored about the user includes things like ratings and comments made on a student submission. These may have been made by an assessor or teacher, but are considered the personal data of the student, as they are considered a reflection of the user’s competency in the subject matter and can be used to form a profile of that individual. &lt;br /&gt;
&lt;br /&gt;
The sections that follow outline what you need to do as a plugin developer to ensure any personal data is advertised and can be accessed and deleted according to the GDPR requirements.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
===Architecture overview===&lt;br /&gt;
&lt;br /&gt;
[[File:MoodlePrivacyMetadataUML.png|thumb|UML diagram of the metadata part of the privacy subsystem]]&lt;br /&gt;
[[File:MoodlePrivacyRequestUML.png|thumb|UML diagram of the request providers part of the privacy subsystem]]&lt;br /&gt;
&lt;br /&gt;
A new system for Privacy has been created within Moodle. This is broken down into several main parts and forms the &#039;&#039;core_privacy&#039;&#039; subsystem:&lt;br /&gt;
&lt;br /&gt;
* Some metadata providers - a set of PHP interfaces to be implemented by components for that component to describe the kind of data that it stores, and the purpose for its storage;&lt;br /&gt;
* Some request providers - a set of PHP interfaces to be implemented by components to allow that component to act upon user requests such as the Right to be Forgotten, and a Subject Access Request; and&lt;br /&gt;
* A manager - a concrete class used to bridge components which implement the providers with tools which request their data.&lt;br /&gt;
&lt;br /&gt;
All plugins will implement one metadata provider, and zero, one or two request providers.&lt;br /&gt;
&lt;br /&gt;
The fetching of data is broken into two separate steps:&lt;br /&gt;
&lt;br /&gt;
# Detecting in which Moodle contexts the user has any data; and&lt;br /&gt;
# Exporting all data from each of those contexts.&lt;br /&gt;
&lt;br /&gt;
This has been broken into two steps to later allow administrators to exclude certain contexts from an export - e.g. for courses currently in progress.&lt;br /&gt;
&lt;br /&gt;
A third component will later be added to facilitate the deletion of data within these contexts which will help to satisfy the Right to be Forgotten. This will also use the first step.&lt;br /&gt;
&lt;br /&gt;
Please refer to the inline phpdocs of the [https://github.com/moodle/moodle/blob/v3.5.0-beta/privacy/classes/manager.php#L31 core_privacy::manager class] for detailed description of the interfaces, their hierarchy and meaning.&lt;br /&gt;
&lt;br /&gt;
===Implementing a provider===&lt;br /&gt;
&lt;br /&gt;
All plugins will need to create a concrete class which implements the relevant metadata and request providers. The exact providers you need to implement will depend on what data you store, and the type of plugin. This is covered in more detail in the following sections of the document.&lt;br /&gt;
&lt;br /&gt;
In order to do so:&lt;br /&gt;
&lt;br /&gt;
# You must create a class called &#039;&#039;provider&#039;&#039; within the namespace &#039;&#039;\your_pluginname\privacy&#039;&#039;.&lt;br /&gt;
# This class must be created at &#039;&#039;path/to/your/plugin/classes/privacy/provider.php&#039;&#039;.&lt;br /&gt;
# You must have your class implement the relevant metadata and request interfaces.&lt;br /&gt;
&lt;br /&gt;
==Plugins which do not store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do not store any personal data. This is usually the case for plugins which just add functionality, or which display the data already stored elsewhere in Moodle.&lt;br /&gt;
&lt;br /&gt;
Some examples of plugin types which might fit this criteria include themes, blocks, filters, editor plugins, etc.&lt;br /&gt;
&lt;br /&gt;
Plugins which cause data to be stored elsewhere in Moodle (e.g. via a subsystem call) are considered to store data.&lt;br /&gt;
&lt;br /&gt;
One examples of a plugin which does not store any data would be the Calendar month block which just displays a view of the user’s calendar. It does not store any data itself.&lt;br /&gt;
&lt;br /&gt;
An example of a plugin which must not use the null provider is the Comments block. The comments block is responsible for data subsequently being stored within Moodle. Although the block doesn’t store anything itself, it interacts with the comments subsystem and is the only component which knows how that data maps to a user.&lt;br /&gt;
&lt;br /&gt;
===Implementation requirements===&lt;br /&gt;
&lt;br /&gt;
In order to let Moodle know that you have audited your plugin, and that you do not store any personal user data, you must implement the &#039;&#039;\core_privacy\local\metadata\null_provider&#039;&#039; interface in your plugin’s provider.&lt;br /&gt;
&lt;br /&gt;
These null providers can only be implemented where a plugin has:&lt;br /&gt;
&lt;br /&gt;
* no external links (e.g. sends data to an external service like an LTI provider, repository plugin which you can search on)&lt;br /&gt;
* no database tables which store user data (including IP addresses)&lt;br /&gt;
* no user preferences&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;null_provider&#039;&#039; requires you to define one function &#039;&#039;get_reason()&#039;&#039; which returns the language string identifier within your component.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace block_calendar_month\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
    // This plugin does not store any personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\null_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the language string identifier with the component&#039;s language&lt;br /&gt;
     * file to explain why this plugin stores no data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_reason() : string {&lt;br /&gt;
        return &#039;privacy:metadata&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/lang/en/block_calendar_month.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
$string[&#039;privacy:metadata&#039;] = &#039;The Calendar block only displays existing calendar data.&#039;;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That’s it. Congratulations, your plugin now implements the Privacy API.&lt;br /&gt;
&lt;br /&gt;
==Plugins which store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do store some form of personal data.&lt;br /&gt;
&lt;br /&gt;
In some cases this will be stored within database tables in your plugin, and in other cases this will be in one of Moodle’s core subsystems - for example your plugin may store files, ratings, comments, or tags.&lt;br /&gt;
&lt;br /&gt;
Plugins which do store data will need to:&lt;br /&gt;
&lt;br /&gt;
* Describe the type of data that they store; &lt;br /&gt;
* Provide a way to export that data; and&lt;br /&gt;
* Provide a way to delete that data.&lt;br /&gt;
&lt;br /&gt;
Data is described via a &#039;&#039;metadata&#039;&#039; provider, and it is both exported and deleted via an implementation of a &#039;&#039;request&#039;&#039; provider.&lt;br /&gt;
&lt;br /&gt;
These are both explained in the sections below.&lt;br /&gt;
&lt;br /&gt;
===Describing the type of data you store===&lt;br /&gt;
&lt;br /&gt;
In order to describe the type of data that you store, you must implement the &#039;&#039;\core_privacy\local\metadata\provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
This interfaces requires that you define one function: &#039;&#039;get_metadata&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
There are several types of item to describe the data that you store. These are for:&lt;br /&gt;
&lt;br /&gt;
* Items in the Moodle database;&lt;br /&gt;
* Items stored by you in a Moodle subsystem - for example files, and ratings; and&lt;br /&gt;
* User preferences stored site-wide within Moodle for your plugin&lt;br /&gt;
&lt;br /&gt;
Note: All fields should include a description from a language string within your plugin.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_forum\privacy;&lt;br /&gt;
use core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
        // This plugin does store personal user data.&lt;br /&gt;
        \core_privacy\local\metadata\provider {&lt;br /&gt;
&lt;br /&gt;
    public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
        // Here you will add more items into the collection.&lt;br /&gt;
&lt;br /&gt;
        return $collection;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store content in a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
Many plugins will use one of the core Moodle subsystems to store data.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer we do not expect you to describe those subsystems in detail, but we do need to know that you use them and to know what you use them for.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_subsystem_link()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_subsystem_link(&lt;br /&gt;
        &#039;core_files&#039;,&lt;br /&gt;
        [],&lt;br /&gt;
        &#039;privacy:metadata:core_files&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:core_files&#039;] = &#039;The forum stores files which have been uploaded by the user to form part of a forum post.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Describing data stored in database tables====&lt;br /&gt;
&lt;br /&gt;
Most Moodle plugins will store some form of user data in their own database tables.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each database table, and each field which includes user data.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_database_table(&lt;br /&gt;
        &#039;forum_discussion_subs&#039;,&lt;br /&gt;
         [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:userid&#039;,&lt;br /&gt;
            &#039;discussionid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:discussionid&#039;,&lt;br /&gt;
            &#039;preference&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:preference&#039;,&lt;br /&gt;
&lt;br /&gt;
         ],&lt;br /&gt;
        &#039;privacy:metadata:forum_discussion_subs&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs&#039;] = &#039;Information about the subscriptions to individual forum discussions. This includes when a user has chosen to subscribe to a discussion, or to unsubscribe from one where they would otherwise be subscribed.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:userid&#039;] = &#039;The ID of the user with this subscription preference.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:discussionid&#039;] = &#039;The ID of the discussion that was subscribed to.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:preference&#039;] = &#039;The start time of the subscription.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store site-wide user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins will include one or more user preferences. Unfortunately this is one of Moodle’s older components and many of the values stored are not pure user preferences. Each plugin should be aware of how it handles its own preferences and is best placed to determine whether they are site-wide preferences, or per-instance preferences.&lt;br /&gt;
&lt;br /&gt;
Whilst most of these will have a fixed name (e.g. &#039;&#039;filepicker_recentrepository&#039;&#039;), some will include a variable of some kind (e.g. &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039;). Only the general name needs to be indicated rather than one copy for each preference.&lt;br /&gt;
&lt;br /&gt;
Also, these should only be &#039;&#039;site-wide&#039;&#039; user preferences which do not belong to a specific Moodle context.&lt;br /&gt;
&lt;br /&gt;
In the above examples:&lt;br /&gt;
&lt;br /&gt;
* Preference &#039;&#039;filepicker_recentrepository&#039;&#039; belongs to the file subsystem, and is a site-wide preference affecting the user anywhere that they view the filepicker.&lt;br /&gt;
* Preference &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039; belongs to user tours. User tours are a site-wide feature which can affect many parts of Moodle and cross multiple contexts.&lt;br /&gt;
&lt;br /&gt;
In some cases a value may be stored in the preferences table but is known to belong to a specific context within Moodle. In these cases they should be stored as metadata against that context rather than as a site-wide user preference.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_user_preference()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Any plugin providing user preferences must also implement the &#039;&#039;\core_privacy\local\request\preference_provider&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_user_preference(&#039;tool_usertours_tour_completion_time&#039;,&lt;br /&gt;
        &#039;privacy:metadata:preference:tool_usertours_tour_completion_time&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/lang/en/tool_usertours.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:tool_usertours_tour_completion_time&#039;] = &#039;The time that a specific user tour was last completed by a user.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you export data to an external location====&lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with external systems - for example cloud-based services. Often this external location is configurable within the plugin either at the site or the instance level.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each &#039;&#039;type&#039;&#039; of target destination, alongside a list of each exported field which includes user data.&lt;br /&gt;
The &#039;&#039;actual&#039;&#039; destination does not need to be described as this can change based on configuration.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_external_location_link()&#039;&#039; method on the collection.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_external_location_link(&#039;lti_client&#039;, [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:lti_client:userid&#039;,&lt;br /&gt;
            &#039;fullname&#039; =&amp;gt; &#039;privacy:metadata:lti_client:fullname&#039;,&lt;br /&gt;
        ], &#039;privacy:metadata:lti_client&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/lang/en/lti.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client&#039;] = &#039;In order to integrate with a remote LTI service, user data needs to be exchanged with that service.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:userid&#039;] = &#039;The userid is sent from Moodle to allow you to access your data on the remote system.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:fullname&#039;] = &#039;Your full name is sent to the remote system to allow a better user experience.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to export user data===&lt;br /&gt;
&lt;br /&gt;
In order to export the user data that you store, you must implement the relevant request provider.&lt;br /&gt;
&lt;br /&gt;
We have named these request providers because they are called in response to a specific request from a user to access their information.&lt;br /&gt;
&lt;br /&gt;
There are several different types of request provider, and you may need to implement several of these, depending on the type and nature of your plugin.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking plugins will fit into one of the following categories:&lt;br /&gt;
&lt;br /&gt;
* Plugins which are a subplugin of another plugin. Examples include &#039;&#039;assignsubmission&#039;&#039;, &#039;&#039;atto&#039;&#039;, and &#039;&#039;datafield&#039;&#039;;&lt;br /&gt;
* Plugins which are typically called by a Moodle subsystem. Examples include &#039;&#039;qtype&#039;&#039;, and &#039;&#039;profilefield&#039;&#039;;&lt;br /&gt;
* All other plugins which store data.&lt;br /&gt;
&lt;br /&gt;
Most plugins will fit into this final category, whilst other plugins may fall into several categories.&lt;br /&gt;
Plugins which &#039;&#039;define&#039;&#039; a subplugin will also be responsible for  collecting this data from their subplugins.&lt;br /&gt;
&lt;br /&gt;
A final category exists - plugins which store user preferences. In some cases this may be the &#039;&#039;only&#039;&#039; provider implemented.&lt;br /&gt;
&lt;br /&gt;
====Standard plugins which store data====&lt;br /&gt;
&lt;br /&gt;
A majority of Moodle plugins will fit into this category and will be required to implement the &#039;&#039;\core_privacy\local\request\plugin\provider&#039;&#039; interface. This interface requires that you define four functions (the first two of which are dealt with in this section):&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;get_contexts_for_userid&#039;&#039; - to explain where data is held within Moodle for your plugin; and&lt;br /&gt;
* &#039;&#039;export_user_data&#039;&#039; - to export a user’s personal data from your plugin.&lt;br /&gt;
* &#039;&#039;delete_data_for_all_users_in_context&#039;&#039; - to delete all data for all users in the specified context.&lt;br /&gt;
* &#039;&#039;delete_data_for_user&#039;&#039; - to delete all user data for the specified user, in the specified contexts.&lt;br /&gt;
&lt;br /&gt;
These APIs make use of the Moodle &#039;&#039;context&#039;&#039; system to hierarchically store this data.&lt;br /&gt;
&lt;br /&gt;
=====Retrieving the list of contexts=====&lt;br /&gt;
&lt;br /&gt;
Contexts are retrieved using the &#039;&#039;get_contexts_for_userid&#039;&#039; function which takes the ID of the user being fetched, and returns a list of contexts in which the user has any data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function returns a &#039;&#039;\core_privacy\local\request\contextlist&#039;&#039; which is used to keep a set of contexts together in a fixed fashion.&lt;br /&gt;
&lt;br /&gt;
Because a Subject Access Request covers &#039;&#039;every&#039;&#039; piece of data that is held for a user within Moodle, efficiency and performance is highly important. As a result, contexts are added to the &#039;&#039;contextlist&#039;&#039; by defining one or more SQL queries which return just the contextid. Multiple SQL queries can be added as required.&lt;br /&gt;
&lt;br /&gt;
To decide which type of context to return (CONTEXT_COURSE, CONTEXT_USER etc.) consider the most appropriate type for your plugin. Typically, this will be the same type of context as you would create to check capabilities. For example, an enrolment plugin will return the course contexts in which it is used. &lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with specific subsystems and store data within them.&lt;br /&gt;
These subsystems will also provide a way in which to link the data that you have stored with your own database tables.&lt;br /&gt;
At present these are still a work in progress and only the &#039;&#039;core_ratings&#039;&#039; subsystem includes this.&lt;br /&gt;
&lt;br /&gt;
======Basic example======&lt;br /&gt;
&lt;br /&gt;
The following example simply fetches the contextid for all forums where a user has a single discussion (note: this is an incomplete example):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
        $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
&lt;br /&gt;
        $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                 FROM {context} c&lt;br /&gt;
           INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
           INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
           INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
                WHERE (&lt;br /&gt;
                d.userid        = :discussionuserid&lt;br /&gt;
                )&lt;br /&gt;
        &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        $params = [&lt;br /&gt;
            &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
            &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
            &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        ];&lt;br /&gt;
&lt;br /&gt;
        $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
        return $contextlist;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
======More complete example======&lt;br /&gt;
&lt;br /&gt;
The following example includes a link to core_rating. &lt;br /&gt;
It will find any forum, forum discussion, or forum post where the user has any data, including:&lt;br /&gt;
&lt;br /&gt;
* Per-forum digest preferences;&lt;br /&gt;
* Per-forum subscription preferences;&lt;br /&gt;
* Per-forum read tracking preferences;&lt;br /&gt;
* Per-discussion subscription preferences;&lt;br /&gt;
* Per-post read data (if a user has read a post or not); and&lt;br /&gt;
* Per-post rating data.&lt;br /&gt;
&lt;br /&gt;
In the case of the rating data, this will include any post where the user has rated the post of another user.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int           $userid       The user to search.&lt;br /&gt;
 * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
 */&lt;br /&gt;
public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
    $ratingsql = \core_rating\privacy\provider::get_sql_join(&#039;rat&#039;, &#039;mod_forum&#039;, &#039;post&#039;, &#039;p.id&#039;, $userid);&lt;br /&gt;
    // Fetch all forum discussions, and forum posts.&lt;br /&gt;
    $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                FROM {context} c&lt;br /&gt;
        INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
        INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
        INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_posts} p ON p.discussion = d.id&lt;br /&gt;
            LEFT JOIN {forum_digests} dig ON dig.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_subscriptions} sub ON sub.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_track_prefs} pref ON pref.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_read} hasread ON hasread.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_discussion_subs} dsub ON dsub.forum = f.id&lt;br /&gt;
            {$ratingsql-&amp;gt;join}&lt;br /&gt;
                WHERE (&lt;br /&gt;
                p.userid        = :postuserid OR&lt;br /&gt;
                d.userid        = :discussionuserid OR&lt;br /&gt;
                dig.userid      = :digestuserid OR&lt;br /&gt;
                sub.userid      = :subuserid OR&lt;br /&gt;
                pref.userid     = :prefuserid OR&lt;br /&gt;
                hasread.userid  = :hasreaduserid OR&lt;br /&gt;
                dsub.userid     = :dsubuserid OR&lt;br /&gt;
                {$ratingsql-&amp;gt;userwhere}&lt;br /&gt;
            )&lt;br /&gt;
    &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    $params = [&lt;br /&gt;
        &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
        &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &#039;postuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        &#039;digestuserid&#039;      =&amp;gt; $userid,&lt;br /&gt;
        &#039;subuserid&#039;         =&amp;gt; $userid,&lt;br /&gt;
        &#039;prefuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;hasreaduserid&#039;     =&amp;gt; $userid,&lt;br /&gt;
        &#039;dsubuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
    ];&lt;br /&gt;
    $params += $ratingsql-&amp;gt;params;&lt;br /&gt;
&lt;br /&gt;
    $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
    $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
    return $contextlist;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Exporting user data=====&lt;br /&gt;
&lt;br /&gt;
After determining where in Moodle your plugin holds data about a user, the &#039;&#039;\core_privacy\manager&#039;&#039; will then ask your plugin to export all user data for a subset of those locations.&lt;br /&gt;
&lt;br /&gt;
This is achieved through use of the &#039;&#039;export_user_data&#039;&#039; function which takes the list of approved contexts in a &#039;&#039;\core_privacy\local\request\approved_contextlist&#039;&#039; object.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   approved_contextlist    $contextlist    The approved contexts to export information for.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_data(approved_contextlist $contextlist) {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;approved_contextlist&#039;&#039; includes both the user record, and a list of contexts, which can be retrieved by either processing it as an Iterator, or by calling &#039;&#039;get_contextids()&#039;&#039; or &#039;&#039;get_contexts()&#039;&#039; as required.&lt;br /&gt;
&lt;br /&gt;
Data is exported using a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which is described in further detail below.&lt;br /&gt;
&lt;br /&gt;
====Plugins which store user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins store a variety of user preferences, and must therefore export them.&lt;br /&gt;
&lt;br /&gt;
Since user preferences are a site-wide preference, these are exported separately to other user data.&lt;br /&gt;
In some cases the only data present is user preference data, whilst in others there is a combination of user-provided data, and user preferences.&lt;br /&gt;
&lt;br /&gt;
Storing of user preferences is achieved through implementation of the &#039;&#039;\core_privacy\local\request\user_preference_provider&#039;&#039; interface which defines one required function -- &#039;&#039;export_user_preferences&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user preferences for the plugin.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int         $userid The userid of the user whose data is to be exported.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_preferences(int $userid) {&lt;br /&gt;
    $markasreadonnotification = get_user_preference(&#039;markasreadonnotification&#039;, null, $userid);&lt;br /&gt;
    if (null !== $markasreadonnotification) {&lt;br /&gt;
        switch ($markasreadonnotification) {&lt;br /&gt;
            case 0:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationno&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
            case 1:&lt;br /&gt;
            default:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationyes&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
        }&lt;br /&gt;
        writer::export_user_preference(&#039;mod_forum&#039;, &#039;markasreadonnotification&#039;, $markasreadonnotification, $markasreadonnotificationdescription);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plugins which can have own subplugins ====&lt;br /&gt;
&lt;br /&gt;
Many plugin types are also able to define their own subplugins and will need to define a contract between themselves and their subplugins in order to fetch their data.&lt;br /&gt;
&lt;br /&gt;
This is required as the parent plugin and the child subplugin should be separate entities and the parent plugin must be able to function if one or more of its subplugins are uninstalled.&lt;br /&gt;
&lt;br /&gt;
The parent plugin is responsible for defining the contract,  and for interacting with its subplugins, though we intend to create helpers to make this easier.&lt;br /&gt;
&lt;br /&gt;
The parent plugin should define a new interface for each type of subplugin that it defines. This interface should extend the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
===== When a parent plugin should and should not provide the interface for its subplugins =====&lt;br /&gt;
&lt;br /&gt;
There can be cases when there is no point for a plugin to provide the &amp;quot;subplugin_provider&amp;quot; based interface, even if it has own subplugins. See the Atto or TinyMCE editors as real examples.&lt;br /&gt;
&lt;br /&gt;
If the parent plugin has no data passed through to the subplugins, there is no benefit in defining a subplugin provider. For example, Atto subplugins are just used to enhance the functionality and they never receive anything like a context. Most of the time we need to define a subplugin provider, but in cases where there is no data passed from the plugin to its subplugins, there is no need to define the subplugin provider. If the subplugins still do store personal data that are not related to the parent plugin in any way, then subplugins should define their own standard provider.&lt;br /&gt;
&lt;br /&gt;
Compare with something like mod_assign where the subplugins store data for the parent and that data is contextually relevant to the parent plugin. In those cases the subplugin stores data for the plugin and it only makes sense to do so in the context of its parent plugin.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
The following example defines the contract that assign submission subplugins may be required to implement.&lt;br /&gt;
&lt;br /&gt;
The assignment module is responsible for returning the contexts of all assignments where a user has data, but in some cases it is unaware of all of those cases - for example if a Teacher comments on a student submission it may not be aware of these as the information about this interaction may not be stored within its own tables.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/assign/privacy/assignsubmission_provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_assign\privacy;&lt;br /&gt;
use \core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
interface assignsubmission_provider extends&lt;br /&gt;
    // This Interface defines a subplugin.&lt;br /&gt;
    \core_privacy\local\request\plugin\subplugin_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the SQL required to find all submission items where this user has had any involvements. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  \stdClass                   Object containing the join, params, and where used to select a these records from the database.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_items_with_user_interaction(int $userid) : \stdClass ;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Export all relevant user submissions information which match the combination of userid and attemptid.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @param   \context      $context      The context to export this submission against.&lt;br /&gt;
     * @param   array         $subcontext   The subcontext within the context to export this information&lt;br /&gt;
     * @param   int           $attid        The id of the submission to export.&lt;br /&gt;
     */&lt;br /&gt;
    public static function export_user_submissions(int $userid, \context $context, array $subcontext, int $attid) ;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are subplugins to another plugin====&lt;br /&gt;
&lt;br /&gt;
If you are developing a sub-plugin of another plugin, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subplugin type should define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface and it is up to the parent plugin to define how they will interact with their children.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/pluginname/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace assignsubmission\onlinetext;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does store personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is a subplugin of assign and must meet that contract.&lt;br /&gt;
    \mod_assign\privacy\assignsubmission_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are typically called by a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
There are a number of plugintypes in Moodle which are typically called by a specific Moodle subsystem.&lt;br /&gt;
&lt;br /&gt;
Some of these are &#039;&#039;only&#039;&#039; called by that subsystem, for example plugins which are of the &#039;&#039;plagiarism&#039;&#039; plugintype should never be called directly, but are always invoked via the &#039;&#039;core_plagiarism&#039;&#039; subsystem.&lt;br /&gt;
&lt;br /&gt;
Conversely, there maybe other plugintypes which can be called both via a subsystem, and in some other fashion. We are still determining whether any plugintypes currently fit this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are developing a plugin which belongs to a specific subsystem, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subsystem will define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subsystem_provider&#039;&#039; interface and it is up to that subsystem to define how they will interact with those plugins.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;plagiarism/detectorator/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace plagiarism_detectorator\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does export personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is always linked against another activity module via the Plagiarism API.&lt;br /&gt;
    \core_plagiarism\privacy\plugin_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exporting data====&lt;br /&gt;
&lt;br /&gt;
Any plugin which stores data must also export it.&lt;br /&gt;
&lt;br /&gt;
To cater for this the privacy API includes a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which defines a set of functions to store different types of data.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking data is broken into the following types:&lt;br /&gt;
&lt;br /&gt;
* Data - this is the object being described. For example the post content in a forum post;&lt;br /&gt;
* Related data - this is data related to the object being stored. For example, ratings of a forum post;&lt;br /&gt;
* Metadata - This is metadata about the main object. For example whether you are subscribed to a forum discussion;&lt;br /&gt;
* User preferences - this is data about a site-wide preference;&lt;br /&gt;
* Files - Any files that you are stored within Moodle on behalf of this plugin; and&lt;br /&gt;
* Custom files - For custom file formats - e.g. a calendar feed for calendar data. These should be used sparingly.&lt;br /&gt;
&lt;br /&gt;
Each piece of data is stored against a specific Moodle &#039;&#039;context&#039;&#039;, which will define how the data is structured within the exporter.&lt;br /&gt;
Data, and Related data only accept the &#039;&#039;stdClass&#039;&#039; object, whilst metadata should be stored as a set of key/value pairs which include a description.&lt;br /&gt;
&lt;br /&gt;
In some cases the data being stored belongs within an implicit structure. For example, one forum has many forum discussions, which each have a number of forum posts. This structure is represented by an &#039;&#039;array&#039;&#039; referred to as a &#039;&#039;subcontext&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;content_writer&#039;&#039; must &#039;&#039;always&#039;&#039; be called with a specific context, and can be called as follows:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
writer::with_context($context)&lt;br /&gt;
    -&amp;gt;export_data($subcontext, $post)&lt;br /&gt;
    -&amp;gt;export_area_files($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id)&lt;br /&gt;
    -&amp;gt;export_metadata($subcontext, &#039;postread&#039;, (object) [&#039;firstread&#039; =&amp;gt; $firstread], new \lang_string(&#039;privacy:export:post:postread&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any text field which supports Moodle files must also be rewritten:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
$post-&amp;gt;message = writer::with_context($context)&lt;br /&gt;
    -&amp;gt;rewrite_pluginfile_urls($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id, $post-&amp;gt;message);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to delete user data===&lt;br /&gt;
&lt;br /&gt;
Deleting user data is also implemented in the request interface. There are two methods that need to be created. The first one to remove all user data from a context, the other to remove user data for a specific user in a list of contexts.&lt;br /&gt;
&lt;br /&gt;
====Delete for a context====&lt;br /&gt;
&lt;br /&gt;
A context is given and all user data (for all users) is to be deleted from the plugin. This will be called when the retention period for the context has expired to adhere to the privacy by design requirement. Retention periods are set in the Data registry.&lt;br /&gt;
&lt;br /&gt;
Note that this will be called for &#039;&#039;&#039;any&#039;&#039;&#039; context being expired, not only those the plugin holds data for (as returned by get_contexts_for_userid()), so you must carefully check the contexts given.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Delete all personal data for all users in the specified context.&lt;br /&gt;
 *&lt;br /&gt;
 * @param context $context Context to delete data from.&lt;br /&gt;
 */&lt;br /&gt;
public static function delete_data_for_all_users_in_context(\context $context) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
     if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $cm = get_coursemodule_from_id(&#039;choice&#039;, $context-&amp;gt;instanceid);&lt;br /&gt;
    if (!$cm) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
    $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Delete personal information for a specific user and context(s)====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;approved_contextlist&#039;&#039; is given and user data related to that user should either be completely deleted, or overwritten if a structure needs to be maintained. This will be called when a user has requested the right to be forgotten. All attempts should be made to delete this data where practical while still allowing the plugin to be used by other users.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/choice/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function delete_data_for_user(approved_contextlist $contextlist) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
    &lt;br /&gt;
    if (empty($contextlist-&amp;gt;count())) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    $userid = $contextlist-&amp;gt;get_user()-&amp;gt;id;&lt;br /&gt;
    foreach ($contextlist-&amp;gt;get_contexts() as $context) {&lt;br /&gt;
        $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid, &#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Difference between Moodle 3.3 and more recent versions==&lt;br /&gt;
Moodle 3.3 has a minimum requirement of php 5.6 and so type hinting and return type declarations are not supported in this version. &lt;br /&gt;
Consequently the privacy API for this version does not have these features.&lt;br /&gt;
==Common Questions==&lt;br /&gt;
===What to do if you have one plugin that supports multiple branches===&lt;br /&gt;
This is something that we have considered and we have put in place a polyfill. This gets around the restrictions of one version having type hinting and return type declarations while another does not.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To use the polyfill include the legacy polyfill trait and create the necessary static methods but with an underscore (shown below).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
    \core_privacy\local\request\plugin\provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What to do if your plugin must implement a subplugin or subsystem plugin provider===&lt;br /&gt;
For subplugins (e.g. assignsubmission, assignfeedback, quiz report, quiz access rules), or subsystems which have a plugintype relationship (portfolio, plagiarism, and others), they will also define their own legacy polyfill.&lt;br /&gt;
&lt;br /&gt;
In this instance you will need to include the trait for both the core polyfill, and the provider polyfill as appropriate.&lt;br /&gt;
==== Example ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin has data and must therefore define the metadata provider in order to describe it.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This is a plagiarism plugin. It interacts with the plagiarism subsystem rather than with core.&lt;br /&gt;
    \core_plagiarism\privacy\plagiarism_provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the metadata provider.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the plagirism provider.&lt;br /&gt;
    use \core_plagiarism\privacy\plagiarism_provider\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // This is one of the polyfilled methods from the plagiarism provider.&lt;br /&gt;
    public static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for development ==&lt;br /&gt;
&lt;br /&gt;
* While implementing the privacy API into your plugin, there are CLI scripts that can help you to test things on the fly. Just don&#039;t forget these are not supposed to replace proper unit tests. See [[Privacy API/Utilities]] for details.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Subject Access Request FAQ]]&lt;br /&gt;
* [[:en:GDPR|GDPR]] in the user documentation&lt;br /&gt;
&lt;br /&gt;
[[Category:Privacy]]&lt;br /&gt;
[[Category:GDPR]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54156</id>
		<title>Privacy API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54156"/>
		<updated>2018-05-14T10:42:36Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Delete for a context */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [https://en.wikipedia.org/wiki/General_Data_Protection_Regulation General Data Protection Regulation] (GDPR) is an EU directive that looks at providing users with more control over their data and how it is processed. This regulation will come into effect on 25th of May 2018 and covers any citizen or permanent resident of the European Union. The directive will be respected by a number of other countries outside of the European Union.&lt;br /&gt;
&lt;br /&gt;
To help institutions become compliant with this new regulation we are adding functionality to Moodle. This includes a number of components, amongst others these include a user’s right to:&lt;br /&gt;
&lt;br /&gt;
* request information on the types of personal data held, the instances of that data, and the deletion policy for each;&lt;br /&gt;
* access all of their data; and&lt;br /&gt;
* be forgotten.&lt;br /&gt;
&lt;br /&gt;
The compliance requirements also extend to installed plugins (including third party plugins). These need to also be able to report what information they store or process regarding users, and have the ability to provide and delete data for a user request.&lt;br /&gt;
&lt;br /&gt;
This document describes the proposed API changes required for plugins which will allow a Moodle installation to become GDPR compliant.&lt;br /&gt;
&lt;br /&gt;
Target Audience: The intended audience for this document is Moodle plugin developers, who are aiming to ensure their plugins are updated to comply with GDPR requirements coming into effect in the EU in May, 2018.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Personal data in Moodle==&lt;br /&gt;
&lt;br /&gt;
From the GDPR Spec, Article 4:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;‘personal data’ means any information relating to an identified or identifiable natural person (‘data subject’); an identifiable natural person is one who can be identified, directly or indirectly, in particular by reference to an identifier such as a name, an identification number, location data, an online identifier or to one or more factors specific to the physical, physiological, genetic, mental, economic, cultural or social identity of that natural person;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Moodle, we need to consider two main types of personal data; information entered by the user and information stored about the user. The key difference being that information stored about the user will have come from a source other than the user themselves. Both types of data can be used to form a profile of the individual.&lt;br /&gt;
&lt;br /&gt;
The most obvious clue to finding personal data entered by the user is the presence of a userid on a database field. Any data on the record (or linked records) pertaining to that user may be deemed personal data for that user, including things like timestamps and record identification numbers. Additionally, any free text field which allows the user to enter information must also be considered to be the personal data of that user.&lt;br /&gt;
&lt;br /&gt;
Data stored about the user includes things like ratings and comments made on a student submission. These may have been made by an assessor or teacher, but are considered the personal data of the student, as they are considered a reflection of the user’s competency in the subject matter and can be used to form a profile of that individual. &lt;br /&gt;
&lt;br /&gt;
The sections that follow outline what you need to do as a plugin developer to ensure any personal data is advertised and can be accessed and deleted according to the GDPR requirements.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
===Architecture overview===&lt;br /&gt;
&lt;br /&gt;
[[File:MoodlePrivacyMetadataUML.png|thumb|UML diagram of the metadata part of the privacy subsystem]]&lt;br /&gt;
[[File:MoodlePrivacyRequestUML.png|thumb|UML diagram of the request providers part of the privacy subsystem]]&lt;br /&gt;
&lt;br /&gt;
A new system for Privacy has been created within Moodle. This is broken down into several main parts and forms the &#039;&#039;core_privacy&#039;&#039; subsystem:&lt;br /&gt;
&lt;br /&gt;
* Some metadata providers - a set of PHP interfaces to be implemented by components for that component to describe the kind of data that it stores, and the purpose for its storage;&lt;br /&gt;
* Some request providers - a set of PHP interfaces to be implemented by components to allow that component to act upon user requests such as the Right to be Forgotten, and a Subject Access Request; and&lt;br /&gt;
* A manager - a concrete class used to bridge components which implement the providers with tools which request their data.&lt;br /&gt;
&lt;br /&gt;
All plugins will implement one metadata provider, and zero, one or two request providers.&lt;br /&gt;
&lt;br /&gt;
The fetching of data is broken into two separate steps:&lt;br /&gt;
&lt;br /&gt;
# Detecting in which Moodle contexts the user has any data; and&lt;br /&gt;
# Exporting all data from each of those contexts.&lt;br /&gt;
&lt;br /&gt;
This has been broken into two steps to later allow administrators to exclude certain contexts from an export - e.g. for courses currently in progress.&lt;br /&gt;
&lt;br /&gt;
A third component will later be added to facilitate the deletion of data within these contexts which will help to satisfy the Right to be Forgotten. This will also use the first step.&lt;br /&gt;
&lt;br /&gt;
Please refer to the inline phpdocs of the [https://github.com/moodle/moodle/blob/v3.5.0-beta/privacy/classes/manager.php#L31 core_privacy::manager class] for detailed description of the interfaces, their hierarchy and meaning.&lt;br /&gt;
&lt;br /&gt;
===Implementing a provider===&lt;br /&gt;
&lt;br /&gt;
All plugins will need to create a concrete class which implements the relevant metadata and request providers. The exact providers you need to implement will depend on what data you store, and the type of plugin. This is covered in more detail in the following sections of the document.&lt;br /&gt;
&lt;br /&gt;
In order to do so:&lt;br /&gt;
&lt;br /&gt;
# You must create a class called &#039;&#039;provider&#039;&#039; within the namespace &#039;&#039;\your_pluginname\privacy&#039;&#039;.&lt;br /&gt;
# This class must be created at &#039;&#039;path/to/your/plugin/classes/privacy/provider.php&#039;&#039;.&lt;br /&gt;
# You must have your class implement the relevant metadata and request interfaces.&lt;br /&gt;
&lt;br /&gt;
==Plugins which do not store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do not store any personal data. This is usually the case for plugins which just add functionality, or which display the data already stored elsewhere in Moodle.&lt;br /&gt;
&lt;br /&gt;
Some examples of plugin types which might fit this criteria include themes, blocks, filters, editor plugins, etc.&lt;br /&gt;
&lt;br /&gt;
Plugins which cause data to be stored elsewhere in Moodle (e.g. via a subsystem call) are considered to store data.&lt;br /&gt;
&lt;br /&gt;
One examples of a plugin which does not store any data would be the Calendar month block which just displays a view of the user’s calendar. It does not store any data itself.&lt;br /&gt;
&lt;br /&gt;
An example of a plugin which must not use the null provider is the Comments block. The comments block is responsible for data subsequently being stored within Moodle. Although the block doesn’t store anything itself, it interacts with the comments subsystem and is the only component which knows how that data maps to a user.&lt;br /&gt;
&lt;br /&gt;
===Implementation requirements===&lt;br /&gt;
&lt;br /&gt;
In order to let Moodle know that you have audited your plugin, and that you do not store any personal user data, you must implement the &#039;&#039;\core_privacy\local\metadata\null_provider&#039;&#039; interface in your plugin’s provider.&lt;br /&gt;
&lt;br /&gt;
These null providers can only be implemented where a plugin has:&lt;br /&gt;
&lt;br /&gt;
* no external links (e.g. sends data to an external service like an LTI provider, repository plugin which you can search on)&lt;br /&gt;
* no database tables which store user data (including IP addresses)&lt;br /&gt;
* no user preferences&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;null_provider&#039;&#039; requires you to define one function &#039;&#039;get_reason()&#039;&#039; which returns the language string identifier within your component.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace block_calendar_month\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
    // This plugin does not store any personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\null_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the language string identifier with the component&#039;s language&lt;br /&gt;
     * file to explain why this plugin stores no data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_reason() : string {&lt;br /&gt;
        return &#039;privacy:metadata&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/lang/en/block_calendar_month.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
$string[&#039;privacy:metadata&#039;] = &#039;The Calendar block only displays existing calendar data.&#039;;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That’s it. Congratulations, your plugin now implements the Privacy API.&lt;br /&gt;
&lt;br /&gt;
==Plugins which store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do store some form of personal data.&lt;br /&gt;
&lt;br /&gt;
In some cases this will be stored within database tables in your plugin, and in other cases this will be in one of Moodle’s core subsystems - for example your plugin may store files, ratings, comments, or tags.&lt;br /&gt;
&lt;br /&gt;
Plugins which do store data will need to:&lt;br /&gt;
&lt;br /&gt;
* Describe the type of data that they store; &lt;br /&gt;
* Provide a way to export that data; and&lt;br /&gt;
* Provide a way to delete that data.&lt;br /&gt;
&lt;br /&gt;
Data is described via a &#039;&#039;metadata&#039;&#039; provider, and it is both exported and deleted via an implementation of a &#039;&#039;request&#039;&#039; provider.&lt;br /&gt;
&lt;br /&gt;
These are both explained in the sections below.&lt;br /&gt;
&lt;br /&gt;
===Describing the type of data you store===&lt;br /&gt;
&lt;br /&gt;
In order to describe the type of data that you store, you must implement the &#039;&#039;\core_privacy\local\metadata\provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
This interfaces requires that you define one function: &#039;&#039;get_metadata&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
There are several types of item to describe the data that you store. These are for:&lt;br /&gt;
&lt;br /&gt;
* Items in the Moodle database;&lt;br /&gt;
* Items stored by you in a Moodle subsystem - for example files, and ratings; and&lt;br /&gt;
* User preferences stored site-wide within Moodle for your plugin&lt;br /&gt;
&lt;br /&gt;
Note: All fields should include a description from a language string within your plugin.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_forum\privacy;&lt;br /&gt;
use core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
        // This plugin does store personal user data.&lt;br /&gt;
        \core_privacy\local\metadata\provider {&lt;br /&gt;
&lt;br /&gt;
    public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
        // Here you will add more items into the collection.&lt;br /&gt;
&lt;br /&gt;
        return $collection;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store content in a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
Many plugins will use one of the core Moodle subsystems to store data.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer we do not expect you to describe those subsystems in detail, but we do need to know that you use them and to know what you use them for.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_subsystem_link()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_subsystem_link(&lt;br /&gt;
        &#039;core_files&#039;,&lt;br /&gt;
        [],&lt;br /&gt;
        &#039;privacy:metadata:core_files&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:core_files&#039;] = &#039;The forum stores files which have been uploaded by the user to form part of a forum post.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Describing data stored in database tables====&lt;br /&gt;
&lt;br /&gt;
Most Moodle plugins will store some form of user data in their own database tables.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each database table, and each field which includes user data.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_database_table(&lt;br /&gt;
        &#039;forum_discussion_subs&#039;,&lt;br /&gt;
         [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:userid&#039;,&lt;br /&gt;
            &#039;discussionid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:discussionid&#039;,&lt;br /&gt;
            &#039;preference&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:preference&#039;,&lt;br /&gt;
&lt;br /&gt;
         ],&lt;br /&gt;
        &#039;privacy:metadata:forum_discussion_subs&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs&#039;] = &#039;Information about the subscriptions to individual forum discussions. This includes when a user has chosen to subscribe to a discussion, or to unsubscribe from one where they would otherwise be subscribed.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:userid&#039;] = &#039;The ID of the user with this subscription preference.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:discussionid&#039;] = &#039;The ID of the discussion that was subscribed to.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:preference&#039;] = &#039;The start time of the subscription.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store site-wide user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins will include one or more user preferences. Unfortunately this is one of Moodle’s older components and many of the values stored are not pure user preferences. Each plugin should be aware of how it handles its own preferences and is best placed to determine whether they are site-wide preferences, or per-instance preferences.&lt;br /&gt;
&lt;br /&gt;
Whilst most of these will have a fixed name (e.g. &#039;&#039;filepicker_recentrepository&#039;&#039;), some will include a variable of some kind (e.g. &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039;). Only the general name needs to be indicated rather than one copy for each preference.&lt;br /&gt;
&lt;br /&gt;
Also, these should only be &#039;&#039;site-wide&#039;&#039; user preferences which do not belong to a specific Moodle context.&lt;br /&gt;
&lt;br /&gt;
In the above examples:&lt;br /&gt;
&lt;br /&gt;
* Preference &#039;&#039;filepicker_recentrepository&#039;&#039; belongs to the file subsystem, and is a site-wide preference affecting the user anywhere that they view the filepicker.&lt;br /&gt;
* Preference &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039; belongs to user tours. User tours are a site-wide feature which can affect many parts of Moodle and cross multiple contexts.&lt;br /&gt;
&lt;br /&gt;
In some cases a value may be stored in the preferences table but is known to belong to a specific context within Moodle. In these cases they should be stored as metadata against that context rather than as a site-wide user preference.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_user_preference()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Any plugin providing user preferences must also implement the &#039;&#039;\core_privacy\local\request\preference_provider&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_user_preference(&#039;tool_usertours_tour_completion_time&#039;,&lt;br /&gt;
        &#039;privacy:metadata:preference:tool_usertours_tour_completion_time&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/lang/en/tool_usertours.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:tool_usertours_tour_completion_time&#039;] = &#039;The time that a specific user tour was last completed by a user.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you export data to an external location====&lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with external systems - for example cloud-based services. Often this external location is configurable within the plugin either at the site or the instance level.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each &#039;&#039;type&#039;&#039; of target destination, alongside a list of each exported field which includes user data.&lt;br /&gt;
The &#039;&#039;actual&#039;&#039; destination does not need to be described as this can change based on configuration.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_external_location_link()&#039;&#039; method on the collection.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_external_location_link(&#039;lti_client&#039;, [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:lti_client:userid&#039;,&lt;br /&gt;
            &#039;fullname&#039; =&amp;gt; &#039;privacy:metadata:lti_client:fullname&#039;,&lt;br /&gt;
        ], &#039;privacy:metadata:lti_client&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/lang/en/lti.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client&#039;] = &#039;In order to integrate with a remote LTI service, user data needs to be exchanged with that service.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:userid&#039;] = &#039;The userid is sent from Moodle to allow you to access your data on the remote system.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:fullname&#039;] = &#039;Your full name is sent to the remote system to allow a better user experience.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to export user data===&lt;br /&gt;
&lt;br /&gt;
In order to export the user data that you store, you must implement the relevant request provider.&lt;br /&gt;
&lt;br /&gt;
We have named these request providers because they are called in response to a specific request from a user to access their information.&lt;br /&gt;
&lt;br /&gt;
There are several different types of request provider, and you may need to implement several of these, depending on the type and nature of your plugin.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking plugins will fit into one of the following categories:&lt;br /&gt;
&lt;br /&gt;
* Plugins which are a subplugin of another plugin. Examples include &#039;&#039;assignsubmission&#039;&#039;, &#039;&#039;atto&#039;&#039;, and &#039;&#039;datafield&#039;&#039;;&lt;br /&gt;
* Plugins which are typically called by a Moodle subsystem. Examples include &#039;&#039;qtype&#039;&#039;, and &#039;&#039;profilefield&#039;&#039;;&lt;br /&gt;
* All other plugins which store data.&lt;br /&gt;
&lt;br /&gt;
Most plugins will fit into this final category, whilst other plugins may fall into several categories.&lt;br /&gt;
Plugins which &#039;&#039;define&#039;&#039; a subplugin will also be responsible for  collecting this data from their subplugins.&lt;br /&gt;
&lt;br /&gt;
A final category exists - plugins which store user preferences. In some cases this may be the &#039;&#039;only&#039;&#039; provider implemented.&lt;br /&gt;
&lt;br /&gt;
====Standard plugins which store data====&lt;br /&gt;
&lt;br /&gt;
A majority of Moodle plugins will fit into this category and will be required to implement the &#039;&#039;\core_privacy\local\request\plugin\provider&#039;&#039; interface. This interface requires that you define four functions (the first two of which are dealt with in this section):&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;get_contexts_for_userid&#039;&#039; - to explain where data is held within Moodle for your plugin; and&lt;br /&gt;
* &#039;&#039;export_user_data&#039;&#039; - to export a user’s personal data from your plugin.&lt;br /&gt;
* &#039;&#039;delete_data_for_all_users_in_context&#039;&#039; - to delete all data for all users in the specified context.&lt;br /&gt;
* &#039;&#039;delete_data_for_user&#039;&#039; - to delete all user data for the specified user, in the specified contexts.&lt;br /&gt;
&lt;br /&gt;
These APIs make use of the Moodle &#039;&#039;context&#039;&#039; system to hierarchically store this data.&lt;br /&gt;
&lt;br /&gt;
=====Retrieving the list of contexts=====&lt;br /&gt;
&lt;br /&gt;
Contexts are retrieved using the &#039;&#039;get_contexts_for_userid&#039;&#039; function which takes the ID of the user being fetched, and returns a list of contexts in which the user has any data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function returns a &#039;&#039;\core_privacy\local\request\contextlist&#039;&#039; which is used to keep a set of contexts together in a fixed fashion.&lt;br /&gt;
&lt;br /&gt;
Because a Subject Access Request covers &#039;&#039;every&#039;&#039; piece of data that is held for a user within Moodle, efficiency and performance is highly important. As a result, contexts are added to the &#039;&#039;contextlist&#039;&#039; by defining one or more SQL queries which return just the contextid. Multiple SQL queries can be added as required.&lt;br /&gt;
&lt;br /&gt;
To decide which type of context to return (CONTEXT_COURSE, CONTEXT_USER etc.) consider the most appropriate type for your plugin. Typically, this will be the same type of context as you would create to check capabilities. For example, an enrolment plugin will return the course contexts in which it is used. &lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with specific subsystems and store data within them.&lt;br /&gt;
These subsystems will also provide a way in which to link the data that you have stored with your own database tables.&lt;br /&gt;
At present these are still a work in progress and only the &#039;&#039;core_ratings&#039;&#039; subsystem includes this.&lt;br /&gt;
&lt;br /&gt;
======Basic example======&lt;br /&gt;
&lt;br /&gt;
The following example simply fetches the contextid for all forums where a user has a single discussion (note: this is an incomplete example):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
        $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
&lt;br /&gt;
        $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                 FROM {context} c&lt;br /&gt;
           INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
           INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
           INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
                WHERE (&lt;br /&gt;
                d.userid        = :discussionuserid&lt;br /&gt;
                )&lt;br /&gt;
        &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        $params = [&lt;br /&gt;
            &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
            &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
            &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        ];&lt;br /&gt;
&lt;br /&gt;
        $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
        return $contextlist;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
======More complete example======&lt;br /&gt;
&lt;br /&gt;
The following example includes a link to core_rating. &lt;br /&gt;
It will find any forum, forum discussion, or forum post where the user has any data, including:&lt;br /&gt;
&lt;br /&gt;
* Per-forum digest preferences;&lt;br /&gt;
* Per-forum subscription preferences;&lt;br /&gt;
* Per-forum read tracking preferences;&lt;br /&gt;
* Per-discussion subscription preferences;&lt;br /&gt;
* Per-post read data (if a user has read a post or not); and&lt;br /&gt;
* Per-post rating data.&lt;br /&gt;
&lt;br /&gt;
In the case of the rating data, this will include any post where the user has rated the post of another user.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int           $userid       The user to search.&lt;br /&gt;
 * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
 */&lt;br /&gt;
public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
    $ratingsql = \core_rating\privacy\provider::get_sql_join(&#039;rat&#039;, &#039;mod_forum&#039;, &#039;post&#039;, &#039;p.id&#039;, $userid);&lt;br /&gt;
    // Fetch all forum discussions, and forum posts.&lt;br /&gt;
    $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                FROM {context} c&lt;br /&gt;
        INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
        INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
        INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_posts} p ON p.discussion = d.id&lt;br /&gt;
            LEFT JOIN {forum_digests} dig ON dig.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_subscriptions} sub ON sub.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_track_prefs} pref ON pref.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_read} hasread ON hasread.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_discussion_subs} dsub ON dsub.forum = f.id&lt;br /&gt;
            {$ratingsql-&amp;gt;join}&lt;br /&gt;
                WHERE (&lt;br /&gt;
                p.userid        = :postuserid OR&lt;br /&gt;
                d.userid        = :discussionuserid OR&lt;br /&gt;
                dig.userid      = :digestuserid OR&lt;br /&gt;
                sub.userid      = :subuserid OR&lt;br /&gt;
                pref.userid     = :prefuserid OR&lt;br /&gt;
                hasread.userid  = :hasreaduserid OR&lt;br /&gt;
                dsub.userid     = :dsubuserid OR&lt;br /&gt;
                {$ratingsql-&amp;gt;userwhere}&lt;br /&gt;
            )&lt;br /&gt;
    &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    $params = [&lt;br /&gt;
        &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
        &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &#039;postuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        &#039;digestuserid&#039;      =&amp;gt; $userid,&lt;br /&gt;
        &#039;subuserid&#039;         =&amp;gt; $userid,&lt;br /&gt;
        &#039;prefuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;hasreaduserid&#039;     =&amp;gt; $userid,&lt;br /&gt;
        &#039;dsubuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
    ];&lt;br /&gt;
    $params += $ratingsql-&amp;gt;params;&lt;br /&gt;
&lt;br /&gt;
    $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
    $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
    return $contextlist;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Exporting user data=====&lt;br /&gt;
&lt;br /&gt;
After determining where in Moodle your plugin holds data about a user, the &#039;&#039;\core_privacy\manager&#039;&#039; will then ask your plugin to export all user data for a subset of those locations.&lt;br /&gt;
&lt;br /&gt;
This is achieved through use of the &#039;&#039;export_user_data&#039;&#039; function which takes the list of approved contexts in a &#039;&#039;\core_privacy\local\request\approved_contextlist&#039;&#039; object.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   approved_contextlist    $contextlist    The approved contexts to export information for.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_data(approved_contextlist $contextlist) {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;approved_contextlist&#039;&#039; includes both the user record, and a list of contexts, which can be retrieved by either processing it as an Iterator, or by calling &#039;&#039;get_contextids()&#039;&#039; or &#039;&#039;get_contexts()&#039;&#039; as required.&lt;br /&gt;
&lt;br /&gt;
Data is exported using a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which is described in further detail below.&lt;br /&gt;
&lt;br /&gt;
====Plugins which store user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins store a variety of user preferences, and must therefore export them.&lt;br /&gt;
&lt;br /&gt;
Since user preferences are a site-wide preference, these are exported separately to other user data.&lt;br /&gt;
In some cases the only data present is user preference data, whilst in others there is a combination of user-provided data, and user preferences.&lt;br /&gt;
&lt;br /&gt;
Storing of user preferences is achieved through implementation of the &#039;&#039;\core_privacy\local\request\user_preference_provider&#039;&#039; interface which defines one required function -- &#039;&#039;export_user_preferences&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user preferences for the plugin.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int         $userid The userid of the user whose data is to be exported.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_preferences(int $userid) {&lt;br /&gt;
    $markasreadonnotification = get_user_preference(&#039;markasreadonnotification&#039;, null, $userid);&lt;br /&gt;
    if (null !== $markasreadonnotification) {&lt;br /&gt;
        switch ($markasreadonnotification) {&lt;br /&gt;
            case 0:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationno&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
            case 1:&lt;br /&gt;
            default:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationyes&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
        }&lt;br /&gt;
        writer::export_user_preference(&#039;mod_forum&#039;, &#039;markasreadonnotification&#039;, $markasreadonnotification, $markasreadonnotificationdescription);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plugins which can have own subplugins ====&lt;br /&gt;
&lt;br /&gt;
Many plugin types are also able to define their own subplugins and will need to define a contract between themselves and their subplugins in order to fetch their data.&lt;br /&gt;
&lt;br /&gt;
This is required as the parent plugin and the child subplugin should be separate entities and the parent plugin must be able to function if one or more of its subplugins are uninstalled.&lt;br /&gt;
&lt;br /&gt;
The parent plugin is responsible for defining the contract,  and for interacting with its subplugins, though we intend to create helpers to make this easier.&lt;br /&gt;
&lt;br /&gt;
The parent plugin should define a new interface for each type of subplugin that it defines. This interface should extend the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
===== When a parent plugin should and should not provide the interface for its subplugins =====&lt;br /&gt;
&lt;br /&gt;
There can be cases when there is no point for a plugin to provide the &amp;quot;subplugin_provider&amp;quot; based interface, even if it has own subplugins. See the Atto or TinyMCE editors as real examples.&lt;br /&gt;
&lt;br /&gt;
If the parent plugin has no data passed through to the subplugins, there is no benefit in defining a subplugin provider. For example, Atto subplugins are just used to enhance the functionality and they never receive anything like a context. Most of the time we need to define a subplugin provider, but in cases where there is no data passed from the plugin to its subplugins, there is no need to define the subplugin provider. If the subplugins still do store personal data that are not related to the parent plugin in any way, then subplugins should define their own standard provider.&lt;br /&gt;
&lt;br /&gt;
Compare with something like mod_assign where the subplugins store data for the parent and that data is contextually relevant to the parent plugin. In those cases the subplugin stores data for the plugin and it only makes sense to do so in the context of its parent plugin.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
The following example defines the contract that assign submission subplugins may be required to implement.&lt;br /&gt;
&lt;br /&gt;
The assignment module is responsible for returning the contexts of all assignments where a user has data, but in some cases it is unaware of all of those cases - for example if a Teacher comments on a student submission it may not be aware of these as the information about this interaction may not be stored within its own tables.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/assign/privacy/assignsubmission_provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_assign\privacy;&lt;br /&gt;
use \core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
interface assignsubmission_provider extends&lt;br /&gt;
    // This Interface defines a subplugin.&lt;br /&gt;
    \core_privacy\local\request\plugin\subplugin_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the SQL required to find all submission items where this user has had any involvements. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  \stdClass                   Object containing the join, params, and where used to select a these records from the database.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_items_with_user_interaction(int $userid) : \stdClass ;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Export all relevant user submissions information which match the combination of userid and attemptid.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @param   \context      $context      The context to export this submission against.&lt;br /&gt;
     * @param   array         $subcontext   The subcontext within the context to export this information&lt;br /&gt;
     * @param   int           $attid        The id of the submission to export.&lt;br /&gt;
     */&lt;br /&gt;
    public static function export_user_submissions(int $userid, \context $context, array $subcontext, int $attid) ;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are subplugins to another plugin====&lt;br /&gt;
&lt;br /&gt;
If you are developing a sub-plugin of another plugin, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subplugin type should define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface and it is up to the parent plugin to define how they will interact with their children.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/pluginname/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace assignsubmission\onlinetext;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does store personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is a subplugin of assign and must meet that contract.&lt;br /&gt;
    \mod_assign\privacy\assignsubmission_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are typically called by a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
There are a number of plugintypes in Moodle which are typically called by a specific Moodle subsystem.&lt;br /&gt;
&lt;br /&gt;
Some of these are &#039;&#039;only&#039;&#039; called by that subsystem, for example plugins which are of the &#039;&#039;plagiarism&#039;&#039; plugintype should never be called directly, but are always invoked via the &#039;&#039;core_plagiarism&#039;&#039; subsystem.&lt;br /&gt;
&lt;br /&gt;
Conversely, there maybe other plugintypes which can be called both via a subsystem, and in some other fashion. We are still determining whether any plugintypes currently fit this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are developing a plugin which belongs to a specific subsystem, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subsystem will define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subsystem_provider&#039;&#039; interface and it is up to that subsystem to define how they will interact with those plugins.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;plagiarism/detectorator/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace plagiarism_detectorator\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does export personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is always linked against another activity module via the Plagiarism API.&lt;br /&gt;
    \core_plagiarism\privacy\plugin_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exporting data====&lt;br /&gt;
&lt;br /&gt;
Any plugin which stores data must also export it.&lt;br /&gt;
&lt;br /&gt;
To cater for this the privacy API includes a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which defines a set of functions to store different types of data.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking data is broken into the following types:&lt;br /&gt;
&lt;br /&gt;
* Data - this is the object being described. For example the post content in a forum post;&lt;br /&gt;
* Related data - this is data related to the object being stored. For example, ratings of a forum post;&lt;br /&gt;
* Metadata - This is metadata about the main object. For example whether you are subscribed to a forum discussion;&lt;br /&gt;
* User preferences - this is data about a site-wide preference;&lt;br /&gt;
* Files - Any files that you are stored within Moodle on behalf of this plugin; and&lt;br /&gt;
* Custom files - For custom file formats - e.g. a calendar feed for calendar data. These should be used sparingly.&lt;br /&gt;
&lt;br /&gt;
Each piece of data is stored against a specific Moodle &#039;&#039;context&#039;&#039;, which will define how the data is structured within the exporter.&lt;br /&gt;
Data, and Related data only accept the &#039;&#039;stdClass&#039;&#039; object, whilst metadata should be stored as a set of key/value pairs which include a description.&lt;br /&gt;
&lt;br /&gt;
In some cases the data being stored belongs within an implicit structure. For example, one forum has many forum discussions, which each have a number of forum posts. This structure is represented by an &#039;&#039;array&#039;&#039; referred to as a &#039;&#039;subcontext&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;content_writer&#039;&#039; must &#039;&#039;always&#039;&#039; be called with a specific context, and can be called as follows:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
writer::with_context($context)&lt;br /&gt;
    -&amp;gt;export_data($subcontext, $post)&lt;br /&gt;
    -&amp;gt;export_area_files($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id)&lt;br /&gt;
    -&amp;gt;export_metadata($subcontext, &#039;postread&#039;, (object) [&#039;firstread&#039; =&amp;gt; $firstread], new \lang_string(&#039;privacy:export:post:postread&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any text field which supports Moodle files must also be rewritten:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
$post-&amp;gt;message = writer::with_context($context)&lt;br /&gt;
    -&amp;gt;rewrite_pluginfile_urls($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id, $post-&amp;gt;message);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to delete user data===&lt;br /&gt;
&lt;br /&gt;
Deleting user data is also implemented in the request interface. There are two methods that need to be created. The first one to remove all user data from a context, the other to remove user data for a specific user in a list of contexts.&lt;br /&gt;
&lt;br /&gt;
====Delete for a context====&lt;br /&gt;
&lt;br /&gt;
A context is given and all user data (for all users) is to be deleted from the plugin. This will be called when the retention period for the context has expired to adhere to the privacy by design requirement. Retention periods are set in the Data registry.&lt;br /&gt;
&lt;br /&gt;
Note that this will be called for &#039;&#039;&#039;any&#039;&#039;&#039; context being expired, not only those the plugin holds data for (as returned by get_contexts_for_userid()).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Delete all personal data for all users in the specified context.&lt;br /&gt;
 *&lt;br /&gt;
 * @param context $context Context to delete data from.&lt;br /&gt;
 */&lt;br /&gt;
public static function delete_data_for_all_users_in_context(\context $context) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
     if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
    $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Delete personal information for a specific user and context(s)====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;approved_contextlist&#039;&#039; is given and user data related to that user should either be completely deleted, or overwritten if a structure needs to be maintained. This will be called when a user has requested the right to be forgotten. All attempts should be made to delete this data where practical while still allowing the plugin to be used by other users.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/choice/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function delete_data_for_user(approved_contextlist $contextlist) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
    &lt;br /&gt;
    if (empty($contextlist-&amp;gt;count())) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    $userid = $contextlist-&amp;gt;get_user()-&amp;gt;id;&lt;br /&gt;
    foreach ($contextlist-&amp;gt;get_contexts() as $context) {&lt;br /&gt;
        $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid, &#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Difference between Moodle 3.3 and more recent versions==&lt;br /&gt;
Moodle 3.3 has a minimum requirement of php 5.6 and so type hinting and return type declarations are not supported in this version. &lt;br /&gt;
Consequently the privacy API for this version does not have these features.&lt;br /&gt;
==Common Questions==&lt;br /&gt;
===What to do if you have one plugin that supports multiple branches===&lt;br /&gt;
This is something that we have considered and we have put in place a polyfill. This gets around the restrictions of one version having type hinting and return type declarations while another does not.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To use the polyfill include the legacy polyfill trait and create the necessary static methods but with an underscore (shown below).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
    \core_privacy\local\request\plugin\provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What to do if your plugin must implement a subplugin or subsystem plugin provider===&lt;br /&gt;
For subplugins (e.g. assignsubmission, assignfeedback, quiz report, quiz access rules), or subsystems which have a plugintype relationship (portfolio, plagiarism, and others), they will also define their own legacy polyfill.&lt;br /&gt;
&lt;br /&gt;
In this instance you will need to include the trait for both the core polyfill, and the provider polyfill as appropriate.&lt;br /&gt;
==== Example ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin has data and must therefore define the metadata provider in order to describe it.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This is a plagiarism plugin. It interacts with the plagiarism subsystem rather than with core.&lt;br /&gt;
    \core_plagiarism\privacy\plagiarism_provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the metadata provider.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the plagirism provider.&lt;br /&gt;
    use \core_plagiarism\privacy\plagiarism_provider\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // This is one of the polyfilled methods from the plagiarism provider.&lt;br /&gt;
    public static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for development ==&lt;br /&gt;
&lt;br /&gt;
* While implementing the privacy API into your plugin, there are CLI scripts that can help you to test things on the fly. Just don&#039;t forget these are not supposed to replace proper unit tests. See [[Privacy API/Utilities]] for details.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Subject Access Request FAQ]]&lt;br /&gt;
* [[:en:GDPR|GDPR]] in the user documentation&lt;br /&gt;
&lt;br /&gt;
[[Category:Privacy]]&lt;br /&gt;
[[Category:GDPR]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54155</id>
		<title>Privacy API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Privacy_API&amp;diff=54155"/>
		<updated>2018-05-14T10:42:24Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [https://en.wikipedia.org/wiki/General_Data_Protection_Regulation General Data Protection Regulation] (GDPR) is an EU directive that looks at providing users with more control over their data and how it is processed. This regulation will come into effect on 25th of May 2018 and covers any citizen or permanent resident of the European Union. The directive will be respected by a number of other countries outside of the European Union.&lt;br /&gt;
&lt;br /&gt;
To help institutions become compliant with this new regulation we are adding functionality to Moodle. This includes a number of components, amongst others these include a user’s right to:&lt;br /&gt;
&lt;br /&gt;
* request information on the types of personal data held, the instances of that data, and the deletion policy for each;&lt;br /&gt;
* access all of their data; and&lt;br /&gt;
* be forgotten.&lt;br /&gt;
&lt;br /&gt;
The compliance requirements also extend to installed plugins (including third party plugins). These need to also be able to report what information they store or process regarding users, and have the ability to provide and delete data for a user request.&lt;br /&gt;
&lt;br /&gt;
This document describes the proposed API changes required for plugins which will allow a Moodle installation to become GDPR compliant.&lt;br /&gt;
&lt;br /&gt;
Target Audience: The intended audience for this document is Moodle plugin developers, who are aiming to ensure their plugins are updated to comply with GDPR requirements coming into effect in the EU in May, 2018.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Personal data in Moodle==&lt;br /&gt;
&lt;br /&gt;
From the GDPR Spec, Article 4:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;‘personal data’ means any information relating to an identified or identifiable natural person (‘data subject’); an identifiable natural person is one who can be identified, directly or indirectly, in particular by reference to an identifier such as a name, an identification number, location data, an online identifier or to one or more factors specific to the physical, physiological, genetic, mental, economic, cultural or social identity of that natural person;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Moodle, we need to consider two main types of personal data; information entered by the user and information stored about the user. The key difference being that information stored about the user will have come from a source other than the user themselves. Both types of data can be used to form a profile of the individual.&lt;br /&gt;
&lt;br /&gt;
The most obvious clue to finding personal data entered by the user is the presence of a userid on a database field. Any data on the record (or linked records) pertaining to that user may be deemed personal data for that user, including things like timestamps and record identification numbers. Additionally, any free text field which allows the user to enter information must also be considered to be the personal data of that user.&lt;br /&gt;
&lt;br /&gt;
Data stored about the user includes things like ratings and comments made on a student submission. These may have been made by an assessor or teacher, but are considered the personal data of the student, as they are considered a reflection of the user’s competency in the subject matter and can be used to form a profile of that individual. &lt;br /&gt;
&lt;br /&gt;
The sections that follow outline what you need to do as a plugin developer to ensure any personal data is advertised and can be accessed and deleted according to the GDPR requirements.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
===Architecture overview===&lt;br /&gt;
&lt;br /&gt;
[[File:MoodlePrivacyMetadataUML.png|thumb|UML diagram of the metadata part of the privacy subsystem]]&lt;br /&gt;
[[File:MoodlePrivacyRequestUML.png|thumb|UML diagram of the request providers part of the privacy subsystem]]&lt;br /&gt;
&lt;br /&gt;
A new system for Privacy has been created within Moodle. This is broken down into several main parts and forms the &#039;&#039;core_privacy&#039;&#039; subsystem:&lt;br /&gt;
&lt;br /&gt;
* Some metadata providers - a set of PHP interfaces to be implemented by components for that component to describe the kind of data that it stores, and the purpose for its storage;&lt;br /&gt;
* Some request providers - a set of PHP interfaces to be implemented by components to allow that component to act upon user requests such as the Right to be Forgotten, and a Subject Access Request; and&lt;br /&gt;
* A manager - a concrete class used to bridge components which implement the providers with tools which request their data.&lt;br /&gt;
&lt;br /&gt;
All plugins will implement one metadata provider, and zero, one or two request providers.&lt;br /&gt;
&lt;br /&gt;
The fetching of data is broken into two separate steps:&lt;br /&gt;
&lt;br /&gt;
# Detecting in which Moodle contexts the user has any data; and&lt;br /&gt;
# Exporting all data from each of those contexts.&lt;br /&gt;
&lt;br /&gt;
This has been broken into two steps to later allow administrators to exclude certain contexts from an export - e.g. for courses currently in progress.&lt;br /&gt;
&lt;br /&gt;
A third component will later be added to facilitate the deletion of data within these contexts which will help to satisfy the Right to be Forgotten. This will also use the first step.&lt;br /&gt;
&lt;br /&gt;
Please refer to the inline phpdocs of the [https://github.com/moodle/moodle/blob/v3.5.0-beta/privacy/classes/manager.php#L31 core_privacy::manager class] for detailed description of the interfaces, their hierarchy and meaning.&lt;br /&gt;
&lt;br /&gt;
===Implementing a provider===&lt;br /&gt;
&lt;br /&gt;
All plugins will need to create a concrete class which implements the relevant metadata and request providers. The exact providers you need to implement will depend on what data you store, and the type of plugin. This is covered in more detail in the following sections of the document.&lt;br /&gt;
&lt;br /&gt;
In order to do so:&lt;br /&gt;
&lt;br /&gt;
# You must create a class called &#039;&#039;provider&#039;&#039; within the namespace &#039;&#039;\your_pluginname\privacy&#039;&#039;.&lt;br /&gt;
# This class must be created at &#039;&#039;path/to/your/plugin/classes/privacy/provider.php&#039;&#039;.&lt;br /&gt;
# You must have your class implement the relevant metadata and request interfaces.&lt;br /&gt;
&lt;br /&gt;
==Plugins which do not store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do not store any personal data. This is usually the case for plugins which just add functionality, or which display the data already stored elsewhere in Moodle.&lt;br /&gt;
&lt;br /&gt;
Some examples of plugin types which might fit this criteria include themes, blocks, filters, editor plugins, etc.&lt;br /&gt;
&lt;br /&gt;
Plugins which cause data to be stored elsewhere in Moodle (e.g. via a subsystem call) are considered to store data.&lt;br /&gt;
&lt;br /&gt;
One examples of a plugin which does not store any data would be the Calendar month block which just displays a view of the user’s calendar. It does not store any data itself.&lt;br /&gt;
&lt;br /&gt;
An example of a plugin which must not use the null provider is the Comments block. The comments block is responsible for data subsequently being stored within Moodle. Although the block doesn’t store anything itself, it interacts with the comments subsystem and is the only component which knows how that data maps to a user.&lt;br /&gt;
&lt;br /&gt;
===Implementation requirements===&lt;br /&gt;
&lt;br /&gt;
In order to let Moodle know that you have audited your plugin, and that you do not store any personal user data, you must implement the &#039;&#039;\core_privacy\local\metadata\null_provider&#039;&#039; interface in your plugin’s provider.&lt;br /&gt;
&lt;br /&gt;
These null providers can only be implemented where a plugin has:&lt;br /&gt;
&lt;br /&gt;
* no external links (e.g. sends data to an external service like an LTI provider, repository plugin which you can search on)&lt;br /&gt;
* no database tables which store user data (including IP addresses)&lt;br /&gt;
* no user preferences&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;null_provider&#039;&#039; requires you to define one function &#039;&#039;get_reason()&#039;&#039; which returns the language string identifier within your component.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 &amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace block_calendar_month\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
    // This plugin does not store any personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\null_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the language string identifier with the component&#039;s language&lt;br /&gt;
     * file to explain why this plugin stores no data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_reason() : string {&lt;br /&gt;
        return &#039;privacy:metadata&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;blocks/calendar_month/lang/en/block_calendar_month.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
$string[&#039;privacy:metadata&#039;] = &#039;The Calendar block only displays existing calendar data.&#039;;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That’s it. Congratulations, your plugin now implements the Privacy API.&lt;br /&gt;
&lt;br /&gt;
==Plugins which store personal data==&lt;br /&gt;
&lt;br /&gt;
Many Moodle plugins do store some form of personal data.&lt;br /&gt;
&lt;br /&gt;
In some cases this will be stored within database tables in your plugin, and in other cases this will be in one of Moodle’s core subsystems - for example your plugin may store files, ratings, comments, or tags.&lt;br /&gt;
&lt;br /&gt;
Plugins which do store data will need to:&lt;br /&gt;
&lt;br /&gt;
* Describe the type of data that they store; &lt;br /&gt;
* Provide a way to export that data; and&lt;br /&gt;
* Provide a way to delete that data.&lt;br /&gt;
&lt;br /&gt;
Data is described via a &#039;&#039;metadata&#039;&#039; provider, and it is both exported and deleted via an implementation of a &#039;&#039;request&#039;&#039; provider.&lt;br /&gt;
&lt;br /&gt;
These are both explained in the sections below.&lt;br /&gt;
&lt;br /&gt;
===Describing the type of data you store===&lt;br /&gt;
&lt;br /&gt;
In order to describe the type of data that you store, you must implement the &#039;&#039;\core_privacy\local\metadata\provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
This interfaces requires that you define one function: &#039;&#039;get_metadata&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
There are several types of item to describe the data that you store. These are for:&lt;br /&gt;
&lt;br /&gt;
* Items in the Moodle database;&lt;br /&gt;
* Items stored by you in a Moodle subsystem - for example files, and ratings; and&lt;br /&gt;
* User preferences stored site-wide within Moodle for your plugin&lt;br /&gt;
&lt;br /&gt;
Note: All fields should include a description from a language string within your plugin.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_forum\privacy;&lt;br /&gt;
use core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
class provider implements &lt;br /&gt;
        // This plugin does store personal user data.&lt;br /&gt;
        \core_privacy\local\metadata\provider {&lt;br /&gt;
&lt;br /&gt;
    public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
        // Here you will add more items into the collection.&lt;br /&gt;
&lt;br /&gt;
        return $collection;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store content in a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
Many plugins will use one of the core Moodle subsystems to store data.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer we do not expect you to describe those subsystems in detail, but we do need to know that you use them and to know what you use them for.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_subsystem_link()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_subsystem_link(&lt;br /&gt;
        &#039;core_files&#039;,&lt;br /&gt;
        [],&lt;br /&gt;
        &#039;privacy:metadata:core_files&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:core_files&#039;] = &#039;The forum stores files which have been uploaded by the user to form part of a forum post.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Describing data stored in database tables====&lt;br /&gt;
&lt;br /&gt;
Most Moodle plugins will store some form of user data in their own database tables.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each database table, and each field which includes user data.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_database_table(&lt;br /&gt;
        &#039;forum_discussion_subs&#039;,&lt;br /&gt;
         [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:userid&#039;,&lt;br /&gt;
            &#039;discussionid&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:discussionid&#039;,&lt;br /&gt;
            &#039;preference&#039; =&amp;gt; &#039;privacy:metadata:forum_discussion_subs:preference&#039;,&lt;br /&gt;
&lt;br /&gt;
         ],&lt;br /&gt;
        &#039;privacy:metadata:forum_discussion_subs&#039;&lt;br /&gt;
    );&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/lang/en/forum.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs&#039;] = &#039;Information about the subscriptions to individual forum discussions. This includes when a user has chosen to subscribe to a discussion, or to unsubscribe from one where they would otherwise be subscribed.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:userid&#039;] = &#039;The ID of the user with this subscription preference.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:discussionid&#039;] = &#039;The ID of the discussion that was subscribed to.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:forum_discussion_subs:preference&#039;] = &#039;The start time of the subscription.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you store site-wide user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins will include one or more user preferences. Unfortunately this is one of Moodle’s older components and many of the values stored are not pure user preferences. Each plugin should be aware of how it handles its own preferences and is best placed to determine whether they are site-wide preferences, or per-instance preferences.&lt;br /&gt;
&lt;br /&gt;
Whilst most of these will have a fixed name (e.g. &#039;&#039;filepicker_recentrepository&#039;&#039;), some will include a variable of some kind (e.g. &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039;). Only the general name needs to be indicated rather than one copy for each preference.&lt;br /&gt;
&lt;br /&gt;
Also, these should only be &#039;&#039;site-wide&#039;&#039; user preferences which do not belong to a specific Moodle context.&lt;br /&gt;
&lt;br /&gt;
In the above examples:&lt;br /&gt;
&lt;br /&gt;
* Preference &#039;&#039;filepicker_recentrepository&#039;&#039; belongs to the file subsystem, and is a site-wide preference affecting the user anywhere that they view the filepicker.&lt;br /&gt;
* Preference &#039;&#039;tool_usertours_tour_completion_time_2&#039;&#039; belongs to user tours. User tours are a site-wide feature which can affect many parts of Moodle and cross multiple contexts.&lt;br /&gt;
&lt;br /&gt;
In some cases a value may be stored in the preferences table but is known to belong to a specific context within Moodle. In these cases they should be stored as metadata against that context rather than as a site-wide user preference.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_user_preference()&#039;&#039; method on the &#039;&#039;collection&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Any plugin providing user preferences must also implement the &#039;&#039;\core_privacy\local\request\preference_provider&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_user_preference(&#039;tool_usertours_tour_completion_time&#039;,&lt;br /&gt;
        &#039;privacy:metadata:preference:tool_usertours_tour_completion_time&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;admin/tool/usertours/lang/en/tool_usertours.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:tool_usertours_tour_completion_time&#039;] = &#039;The time that a specific user tour was last completed by a user.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Indicating that you export data to an external location====&lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with external systems - for example cloud-based services. Often this external location is configurable within the plugin either at the site or the instance level.&lt;br /&gt;
&lt;br /&gt;
As a plugin developer you will need to describe each &#039;&#039;type&#039;&#039; of target destination, alongside a list of each exported field which includes user data.&lt;br /&gt;
The &#039;&#039;actual&#039;&#039; destination does not need to be described as this can change based on configuration.&lt;br /&gt;
&lt;br /&gt;
You can indicate this by calling the &#039;&#039;add_external_location_link()&#039;&#039; method on the collection.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_metadata(collection $collection) : collection {&lt;br /&gt;
&lt;br /&gt;
    $collection-&amp;gt;add_external_location_link(&#039;lti_client&#039;, [&lt;br /&gt;
            &#039;userid&#039; =&amp;gt; &#039;privacy:metadata:lti_client:userid&#039;,&lt;br /&gt;
            &#039;fullname&#039; =&amp;gt; &#039;privacy:metadata:lti_client:fullname&#039;,&lt;br /&gt;
        ], &#039;privacy:metadata:lti_client&#039;);&lt;br /&gt;
&lt;br /&gt;
    return $collection;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/lti/lang/en/lti.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client&#039;] = &#039;In order to integrate with a remote LTI service, user data needs to be exchanged with that service.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:userid&#039;] = &#039;The userid is sent from Moodle to allow you to access your data on the remote system.&#039;;&lt;br /&gt;
$string[&#039;privacy:metadata:lti_client:fullname&#039;] = &#039;Your full name is sent to the remote system to allow a better user experience.&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to export user data===&lt;br /&gt;
&lt;br /&gt;
In order to export the user data that you store, you must implement the relevant request provider.&lt;br /&gt;
&lt;br /&gt;
We have named these request providers because they are called in response to a specific request from a user to access their information.&lt;br /&gt;
&lt;br /&gt;
There are several different types of request provider, and you may need to implement several of these, depending on the type and nature of your plugin.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking plugins will fit into one of the following categories:&lt;br /&gt;
&lt;br /&gt;
* Plugins which are a subplugin of another plugin. Examples include &#039;&#039;assignsubmission&#039;&#039;, &#039;&#039;atto&#039;&#039;, and &#039;&#039;datafield&#039;&#039;;&lt;br /&gt;
* Plugins which are typically called by a Moodle subsystem. Examples include &#039;&#039;qtype&#039;&#039;, and &#039;&#039;profilefield&#039;&#039;;&lt;br /&gt;
* All other plugins which store data.&lt;br /&gt;
&lt;br /&gt;
Most plugins will fit into this final category, whilst other plugins may fall into several categories.&lt;br /&gt;
Plugins which &#039;&#039;define&#039;&#039; a subplugin will also be responsible for  collecting this data from their subplugins.&lt;br /&gt;
&lt;br /&gt;
A final category exists - plugins which store user preferences. In some cases this may be the &#039;&#039;only&#039;&#039; provider implemented.&lt;br /&gt;
&lt;br /&gt;
====Standard plugins which store data====&lt;br /&gt;
&lt;br /&gt;
A majority of Moodle plugins will fit into this category and will be required to implement the &#039;&#039;\core_privacy\local\request\plugin\provider&#039;&#039; interface. This interface requires that you define four functions (the first two of which are dealt with in this section):&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;get_contexts_for_userid&#039;&#039; - to explain where data is held within Moodle for your plugin; and&lt;br /&gt;
* &#039;&#039;export_user_data&#039;&#039; - to export a user’s personal data from your plugin.&lt;br /&gt;
* &#039;&#039;delete_data_for_all_users_in_context&#039;&#039; - to delete all data for all users in the specified context.&lt;br /&gt;
* &#039;&#039;delete_data_for_user&#039;&#039; - to delete all user data for the specified user, in the specified contexts.&lt;br /&gt;
&lt;br /&gt;
These APIs make use of the Moodle &#039;&#039;context&#039;&#039; system to hierarchically store this data.&lt;br /&gt;
&lt;br /&gt;
=====Retrieving the list of contexts=====&lt;br /&gt;
&lt;br /&gt;
Contexts are retrieved using the &#039;&#039;get_contexts_for_userid&#039;&#039; function which takes the ID of the user being fetched, and returns a list of contexts in which the user has any data.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function returns a &#039;&#039;\core_privacy\local\request\contextlist&#039;&#039; which is used to keep a set of contexts together in a fixed fashion.&lt;br /&gt;
&lt;br /&gt;
Because a Subject Access Request covers &#039;&#039;every&#039;&#039; piece of data that is held for a user within Moodle, efficiency and performance is highly important. As a result, contexts are added to the &#039;&#039;contextlist&#039;&#039; by defining one or more SQL queries which return just the contextid. Multiple SQL queries can be added as required.&lt;br /&gt;
&lt;br /&gt;
To decide which type of context to return (CONTEXT_COURSE, CONTEXT_USER etc.) consider the most appropriate type for your plugin. Typically, this will be the same type of context as you would create to check capabilities. For example, an enrolment plugin will return the course contexts in which it is used. &lt;br /&gt;
&lt;br /&gt;
Many plugins will interact with specific subsystems and store data within them.&lt;br /&gt;
These subsystems will also provide a way in which to link the data that you have stored with your own database tables.&lt;br /&gt;
At present these are still a work in progress and only the &#039;&#039;core_ratings&#039;&#039; subsystem includes this.&lt;br /&gt;
&lt;br /&gt;
======Basic example======&lt;br /&gt;
&lt;br /&gt;
The following example simply fetches the contextid for all forums where a user has a single discussion (note: this is an incomplete example):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
        $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
&lt;br /&gt;
        $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                 FROM {context} c&lt;br /&gt;
           INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
           INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
           INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
                WHERE (&lt;br /&gt;
                d.userid        = :discussionuserid&lt;br /&gt;
                )&lt;br /&gt;
        &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        $params = [&lt;br /&gt;
            &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
            &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
            &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        ];&lt;br /&gt;
&lt;br /&gt;
        $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
        return $contextlist;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
======More complete example======&lt;br /&gt;
&lt;br /&gt;
The following example includes a link to core_rating. &lt;br /&gt;
It will find any forum, forum discussion, or forum post where the user has any data, including:&lt;br /&gt;
&lt;br /&gt;
* Per-forum digest preferences;&lt;br /&gt;
* Per-forum subscription preferences;&lt;br /&gt;
* Per-forum read tracking preferences;&lt;br /&gt;
* Per-discussion subscription preferences;&lt;br /&gt;
* Per-post read data (if a user has read a post or not); and&lt;br /&gt;
* Per-post rating data.&lt;br /&gt;
&lt;br /&gt;
In the case of the rating data, this will include any post where the user has rated the post of another user.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the list of contexts that contain user information for the specified user.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int           $userid       The user to search.&lt;br /&gt;
 * @return  contextlist   $contextlist  The list of contexts used in this plugin.&lt;br /&gt;
 */&lt;br /&gt;
public static function get_contexts_for_userid(int $userid) : contextlist {&lt;br /&gt;
    $ratingsql = \core_rating\privacy\provider::get_sql_join(&#039;rat&#039;, &#039;mod_forum&#039;, &#039;post&#039;, &#039;p.id&#039;, $userid);&lt;br /&gt;
    // Fetch all forum discussions, and forum posts.&lt;br /&gt;
    $sql = &amp;quot;SELECT c.id&lt;br /&gt;
                FROM {context} c&lt;br /&gt;
        INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel&lt;br /&gt;
        INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname&lt;br /&gt;
        INNER JOIN {forum} f ON f.id = cm.instance&lt;br /&gt;
            LEFT JOIN {forum_discussions} d ON d.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_posts} p ON p.discussion = d.id&lt;br /&gt;
            LEFT JOIN {forum_digests} dig ON dig.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_subscriptions} sub ON sub.forum = f.id&lt;br /&gt;
            LEFT JOIN {forum_track_prefs} pref ON pref.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_read} hasread ON hasread.forumid = f.id&lt;br /&gt;
            LEFT JOIN {forum_discussion_subs} dsub ON dsub.forum = f.id&lt;br /&gt;
            {$ratingsql-&amp;gt;join}&lt;br /&gt;
                WHERE (&lt;br /&gt;
                p.userid        = :postuserid OR&lt;br /&gt;
                d.userid        = :discussionuserid OR&lt;br /&gt;
                dig.userid      = :digestuserid OR&lt;br /&gt;
                sub.userid      = :subuserid OR&lt;br /&gt;
                pref.userid     = :prefuserid OR&lt;br /&gt;
                hasread.userid  = :hasreaduserid OR&lt;br /&gt;
                dsub.userid     = :dsubuserid OR&lt;br /&gt;
                {$ratingsql-&amp;gt;userwhere}&lt;br /&gt;
            )&lt;br /&gt;
    &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    $params = [&lt;br /&gt;
        &#039;modname&#039;           =&amp;gt; &#039;forum&#039;,&lt;br /&gt;
        &#039;contextlevel&#039;      =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &#039;postuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;discussionuserid&#039;  =&amp;gt; $userid,&lt;br /&gt;
        &#039;digestuserid&#039;      =&amp;gt; $userid,&lt;br /&gt;
        &#039;subuserid&#039;         =&amp;gt; $userid,&lt;br /&gt;
        &#039;prefuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
        &#039;hasreaduserid&#039;     =&amp;gt; $userid,&lt;br /&gt;
        &#039;dsubuserid&#039;        =&amp;gt; $userid,&lt;br /&gt;
    ];&lt;br /&gt;
    $params += $ratingsql-&amp;gt;params;&lt;br /&gt;
&lt;br /&gt;
    $contextlist = new \core_privacy\local\request\contextlist();&lt;br /&gt;
    $contextlist-&amp;gt;add_from_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
    return $contextlist;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Exporting user data=====&lt;br /&gt;
&lt;br /&gt;
After determining where in Moodle your plugin holds data about a user, the &#039;&#039;\core_privacy\manager&#039;&#039; will then ask your plugin to export all user data for a subset of those locations.&lt;br /&gt;
&lt;br /&gt;
This is achieved through use of the &#039;&#039;export_user_data&#039;&#039; function which takes the list of approved contexts in a &#039;&#039;\core_privacy\local\request\approved_contextlist&#039;&#039; object.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   approved_contextlist    $contextlist    The approved contexts to export information for.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_data(approved_contextlist $contextlist) {}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;approved_contextlist&#039;&#039; includes both the user record, and a list of contexts, which can be retrieved by either processing it as an Iterator, or by calling &#039;&#039;get_contextids()&#039;&#039; or &#039;&#039;get_contexts()&#039;&#039; as required.&lt;br /&gt;
&lt;br /&gt;
Data is exported using a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which is described in further detail below.&lt;br /&gt;
&lt;br /&gt;
====Plugins which store user preferences====&lt;br /&gt;
&lt;br /&gt;
Many plugins store a variety of user preferences, and must therefore export them.&lt;br /&gt;
&lt;br /&gt;
Since user preferences are a site-wide preference, these are exported separately to other user data.&lt;br /&gt;
In some cases the only data present is user preference data, whilst in others there is a combination of user-provided data, and user preferences.&lt;br /&gt;
&lt;br /&gt;
Storing of user preferences is achieved through implementation of the &#039;&#039;\core_privacy\local\request\user_preference_provider&#039;&#039; interface which defines one required function -- &#039;&#039;export_user_preferences&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Export all user preferences for the plugin.&lt;br /&gt;
 *&lt;br /&gt;
 * @param   int         $userid The userid of the user whose data is to be exported.&lt;br /&gt;
 */&lt;br /&gt;
public static function export_user_preferences(int $userid) {&lt;br /&gt;
    $markasreadonnotification = get_user_preference(&#039;markasreadonnotification&#039;, null, $userid);&lt;br /&gt;
    if (null !== $markasreadonnotification) {&lt;br /&gt;
        switch ($markasreadonnotification) {&lt;br /&gt;
            case 0:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationno&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
            case 1:&lt;br /&gt;
            default:&lt;br /&gt;
                $markasreadonnotificationdescription = get_string(&#039;markasreadonnotificationyes&#039;, &#039;mod_forum&#039;);&lt;br /&gt;
                break;&lt;br /&gt;
        }&lt;br /&gt;
        writer::export_user_preference(&#039;mod_forum&#039;, &#039;markasreadonnotification&#039;, $markasreadonnotification, $markasreadonnotificationdescription);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plugins which can have own subplugins ====&lt;br /&gt;
&lt;br /&gt;
Many plugin types are also able to define their own subplugins and will need to define a contract between themselves and their subplugins in order to fetch their data.&lt;br /&gt;
&lt;br /&gt;
This is required as the parent plugin and the child subplugin should be separate entities and the parent plugin must be able to function if one or more of its subplugins are uninstalled.&lt;br /&gt;
&lt;br /&gt;
The parent plugin is responsible for defining the contract,  and for interacting with its subplugins, though we intend to create helpers to make this easier.&lt;br /&gt;
&lt;br /&gt;
The parent plugin should define a new interface for each type of subplugin that it defines. This interface should extend the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface.&lt;br /&gt;
&lt;br /&gt;
===== When a parent plugin should and should not provide the interface for its subplugins =====&lt;br /&gt;
&lt;br /&gt;
There can be cases when there is no point for a plugin to provide the &amp;quot;subplugin_provider&amp;quot; based interface, even if it has own subplugins. See the Atto or TinyMCE editors as real examples.&lt;br /&gt;
&lt;br /&gt;
If the parent plugin has no data passed through to the subplugins, there is no benefit in defining a subplugin provider. For example, Atto subplugins are just used to enhance the functionality and they never receive anything like a context. Most of the time we need to define a subplugin provider, but in cases where there is no data passed from the plugin to its subplugins, there is no need to define the subplugin provider. If the subplugins still do store personal data that are not related to the parent plugin in any way, then subplugins should define their own standard provider.&lt;br /&gt;
&lt;br /&gt;
Compare with something like mod_assign where the subplugins store data for the parent and that data is contextually relevant to the parent plugin. In those cases the subplugin stores data for the plugin and it only makes sense to do so in the context of its parent plugin.&lt;br /&gt;
&lt;br /&gt;
=====Example=====&lt;br /&gt;
&lt;br /&gt;
The following example defines the contract that assign submission subplugins may be required to implement.&lt;br /&gt;
&lt;br /&gt;
The assignment module is responsible for returning the contexts of all assignments where a user has data, but in some cases it is unaware of all of those cases - for example if a Teacher comments on a student submission it may not be aware of these as the information about this interaction may not be stored within its own tables.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/assign/privacy/assignsubmission_provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
&lt;br /&gt;
namespace mod_assign\privacy;&lt;br /&gt;
use \core_privacy\local\metadata\collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
interface assignsubmission_provider extends&lt;br /&gt;
    // This Interface defines a subplugin.&lt;br /&gt;
    \core_privacy\local\request\plugin\subplugin_provider {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the SQL required to find all submission items where this user has had any involvements. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @return  \stdClass                   Object containing the join, params, and where used to select a these records from the database.&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_items_with_user_interaction(int $userid) : \stdClass ;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Export all relevant user submissions information which match the combination of userid and attemptid.&lt;br /&gt;
     *&lt;br /&gt;
     * @param   int           $userid       The user to search.&lt;br /&gt;
     * @param   \context      $context      The context to export this submission against.&lt;br /&gt;
     * @param   array         $subcontext   The subcontext within the context to export this information&lt;br /&gt;
     * @param   int           $attid        The id of the submission to export.&lt;br /&gt;
     */&lt;br /&gt;
    public static function export_user_submissions(int $userid, \context $context, array $subcontext, int $attid) ;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are subplugins to another plugin====&lt;br /&gt;
&lt;br /&gt;
If you are developing a sub-plugin of another plugin, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subplugin type should define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subplugin_provider&#039;&#039; interface and it is up to the parent plugin to define how they will interact with their children.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/pluginname/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace assignsubmission\onlinetext;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does store personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is a subplugin of assign and must meet that contract.&lt;br /&gt;
    \mod_assign\privacy\assignsubmission_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Plugins which are typically called by a Moodle subsystem====&lt;br /&gt;
&lt;br /&gt;
There are a number of plugintypes in Moodle which are typically called by a specific Moodle subsystem.&lt;br /&gt;
&lt;br /&gt;
Some of these are &#039;&#039;only&#039;&#039; called by that subsystem, for example plugins which are of the &#039;&#039;plagiarism&#039;&#039; plugintype should never be called directly, but are always invoked via the &#039;&#039;core_plagiarism&#039;&#039; subsystem.&lt;br /&gt;
&lt;br /&gt;
Conversely, there maybe other plugintypes which can be called both via a subsystem, and in some other fashion. We are still determining whether any plugintypes currently fit this pattern.&lt;br /&gt;
&lt;br /&gt;
If you are developing a plugin which belongs to a specific subsystem, then you will have to look at the relevant plugin in order to determine the exact contract.&lt;br /&gt;
&lt;br /&gt;
Each subsystem will define a new interface which extends the &#039;&#039;\core_privacy\local\request\plugin\subsystem_provider&#039;&#039; interface and it is up to that subsystem to define how they will interact with those plugins.&lt;br /&gt;
&lt;br /&gt;
The principles remain the same, but the exact implementation will differ depending upon requirements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;plagiarism/detectorator/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
namespace plagiarism_detectorator\privacy;&lt;br /&gt;
&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin does export personal user data.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This plugin is always linked against another activity module via the Plagiarism API.&lt;br /&gt;
    \core_plagiarism\privacy\plugin_provider {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exporting data====&lt;br /&gt;
&lt;br /&gt;
Any plugin which stores data must also export it.&lt;br /&gt;
&lt;br /&gt;
To cater for this the privacy API includes a &#039;&#039;\core_privacy\local\request\content_writer&#039;&#039;, which defines a set of functions to store different types of data.&lt;br /&gt;
&lt;br /&gt;
Broadly speaking data is broken into the following types:&lt;br /&gt;
&lt;br /&gt;
* Data - this is the object being described. For example the post content in a forum post;&lt;br /&gt;
* Related data - this is data related to the object being stored. For example, ratings of a forum post;&lt;br /&gt;
* Metadata - This is metadata about the main object. For example whether you are subscribed to a forum discussion;&lt;br /&gt;
* User preferences - this is data about a site-wide preference;&lt;br /&gt;
* Files - Any files that you are stored within Moodle on behalf of this plugin; and&lt;br /&gt;
* Custom files - For custom file formats - e.g. a calendar feed for calendar data. These should be used sparingly.&lt;br /&gt;
&lt;br /&gt;
Each piece of data is stored against a specific Moodle &#039;&#039;context&#039;&#039;, which will define how the data is structured within the exporter.&lt;br /&gt;
Data, and Related data only accept the &#039;&#039;stdClass&#039;&#039; object, whilst metadata should be stored as a set of key/value pairs which include a description.&lt;br /&gt;
&lt;br /&gt;
In some cases the data being stored belongs within an implicit structure. For example, one forum has many forum discussions, which each have a number of forum posts. This structure is represented by an &#039;&#039;array&#039;&#039; referred to as a &#039;&#039;subcontext&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;content_writer&#039;&#039; must &#039;&#039;always&#039;&#039; be called with a specific context, and can be called as follows:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
writer::with_context($context)&lt;br /&gt;
    -&amp;gt;export_data($subcontext, $post)&lt;br /&gt;
    -&amp;gt;export_area_files($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id)&lt;br /&gt;
    -&amp;gt;export_metadata($subcontext, &#039;postread&#039;, (object) [&#039;firstread&#039; =&amp;gt; $firstread], new \lang_string(&#039;privacy:export:post:postread&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any text field which supports Moodle files must also be rewritten:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/forum/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// …&lt;br /&gt;
use \core_privacy\local\request\writer;&lt;br /&gt;
&lt;br /&gt;
$post-&amp;gt;message = writer::with_context($context)&lt;br /&gt;
    -&amp;gt;rewrite_pluginfile_urls($subcontext, &#039;mod_forum&#039;, &#039;post&#039;, $post-&amp;gt;id, $post-&amp;gt;message);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Providing a way to delete user data===&lt;br /&gt;
&lt;br /&gt;
Deleting user data is also implemented in the request interface. There are two methods that need to be created. The first one to remove all user data from a context, the other to remove user data for a specific user in a list of contexts.&lt;br /&gt;
&lt;br /&gt;
====Delete for a context====&lt;br /&gt;
&lt;br /&gt;
A context is given and all user data (for all users) is to be deleted from the plugin. This will be called when the retention period for the context has expired to adhere to the privacy by design requirement. Retention periods are set in the Data registry.&lt;br /&gt;
&lt;br /&gt;
Note that this will be called for *any* context being expired, not only those the plugin holds data for (as returned by get_contexts_for_userid()).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Delete all personal data for all users in the specified context.&lt;br /&gt;
 *&lt;br /&gt;
 * @param context $context Context to delete data from.&lt;br /&gt;
 */&lt;br /&gt;
public static function delete_data_for_all_users_in_context(\context $context) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
&lt;br /&gt;
     if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
    $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Delete personal information for a specific user and context(s)====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;approved_contextlist&#039;&#039; is given and user data related to that user should either be completely deleted, or overwritten if a structure needs to be maintained. This will be called when a user has requested the right to be forgotten. All attempts should be made to delete this data where practical while still allowing the plugin to be used by other users.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;mod/choice/classes/privacy/provider.php&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function delete_data_for_user(approved_contextlist $contextlist) {&lt;br /&gt;
    global $DB;&lt;br /&gt;
    &lt;br /&gt;
    if (empty($contextlist-&amp;gt;count())) {&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    $userid = $contextlist-&amp;gt;get_user()-&amp;gt;id;&lt;br /&gt;
    foreach ($contextlist-&amp;gt;get_contexts() as $context) {&lt;br /&gt;
        $instanceid = $DB-&amp;gt;get_field(&#039;course_modules&#039;, &#039;instance&#039;, [&#039;id&#039; =&amp;gt; $context-&amp;gt;instanceid], MUST_EXIST);&lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;choice_answers&#039;, [&#039;choiceid&#039; =&amp;gt; $instanceid, &#039;userid&#039; =&amp;gt; $userid]);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Difference between Moodle 3.3 and more recent versions==&lt;br /&gt;
Moodle 3.3 has a minimum requirement of php 5.6 and so type hinting and return type declarations are not supported in this version. &lt;br /&gt;
Consequently the privacy API for this version does not have these features.&lt;br /&gt;
==Common Questions==&lt;br /&gt;
===What to do if you have one plugin that supports multiple branches===&lt;br /&gt;
This is something that we have considered and we have put in place a polyfill. This gets around the restrictions of one version having type hinting and return type declarations while another does not.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To use the polyfill include the legacy polyfill trait and create the necessary static methods but with an underscore (shown below).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
    \core_privacy\local\request\plugin\provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What to do if your plugin must implement a subplugin or subsystem plugin provider===&lt;br /&gt;
For subplugins (e.g. assignsubmission, assignfeedback, quiz report, quiz access rules), or subsystems which have a plugintype relationship (portfolio, plagiarism, and others), they will also define their own legacy polyfill.&lt;br /&gt;
&lt;br /&gt;
In this instance you will need to include the trait for both the core polyfill, and the provider polyfill as appropriate.&lt;br /&gt;
==== Example ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class provider implements&lt;br /&gt;
    // This plugin has data and must therefore define the metadata provider in order to describe it.&lt;br /&gt;
    \core_privacy\local\metadata\provider,&lt;br /&gt;
&lt;br /&gt;
    // This is a plagiarism plugin. It interacts with the plagiarism subsystem rather than with core.&lt;br /&gt;
    \core_plagiarism\privacy\plagiarism_provider {&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the metadata provider.&lt;br /&gt;
    use \core_privacy\local\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // This trait must be included to provide the relevant polyfill for the plagirism provider.&lt;br /&gt;
    use \core_plagiarism\privacy\plagiarism_provider\legacy_polyfill;&lt;br /&gt;
&lt;br /&gt;
    // The required methods must be in this format starting with an underscore.&lt;br /&gt;
    public static function _get_metadata(collection $collection) {&lt;br /&gt;
        // Code for returning metadata goes here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // This is one of the polyfilled methods from the plagiarism provider.&lt;br /&gt;
    public static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for development ==&lt;br /&gt;
&lt;br /&gt;
* While implementing the privacy API into your plugin, there are CLI scripts that can help you to test things on the fly. Just don&#039;t forget these are not supposed to replace proper unit tests. See [[Privacy API/Utilities]] for details.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Subject Access Request FAQ]]&lt;br /&gt;
* [[:en:GDPR|GDPR]] in the user documentation&lt;br /&gt;
&lt;br /&gt;
[[Category:Privacy]]&lt;br /&gt;
[[Category:GDPR]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=XMLDB_defining_an_XML_structure&amp;diff=54014</id>
		<title>XMLDB defining an XML structure</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=XMLDB_defining_an_XML_structure&amp;diff=54014"/>
		<updated>2018-04-10T09:04:06Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Removed reference to enum and enumvalues (removed in 2.1)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[XMLDB Documentation|XMLDB Documentation]] &amp;gt; [[XMLDB roadmap|Roadmap]] &amp;gt; Defining one XML structure&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Justification ==&lt;br /&gt;
&lt;br /&gt;
Before Moodle 1.7, all the DB install and upgrade was developed twice (once to handle MySQL installations and another to handle PostgreSQL installations). This approach, although working, has caused some headaches in the past, mainly because it was really difficult to keep both lines of development 100% on sync. Some developers do they work against one RDBMS and it was complex to develop to the other one (two test environments, skills on both databases, slower development cycle...). And all this was happening with &#039;&#039;only&#039;&#039; two supported RDBMS!&lt;br /&gt;
&lt;br /&gt;
One of the main objectives of Moodle 1.7 is to extend the the number of supported RDBMS to other flavours (more exactly, to Oracle and MSSQL). And the old approach (one line of development for each DB) could become an absolute nightmare. &lt;br /&gt;
&lt;br /&gt;
Because of this we have planned to build one structure to define all the DB objects used by Moodle. This structure will provide the necessary level of abstraction to be shared by all the RDBMS systems, so the &amp;quot;multiple lines of development&amp;quot; explained in the previous paragraph will be out forever, giving us one robust and well defined way to handle DB objects independently of the underlying RDBMS being used.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Initially all our best wishes were to use the [http://phplens.com/lens/adodb/docs-datadict.htm#xmlschema AdoDB XML Schema]. As Moodle is using ADOdb libraries to communicate with databases it sounded like the natural approach to solve the problem. But, finally, two reasons prevented us to use it:&lt;br /&gt;
&lt;br /&gt;
# Although working, it seems to be one feature in progress, with important changes/evolutions arriving at the time of write this document.&lt;br /&gt;
# Its lack of support for &amp;quot;prefixes&amp;quot; (one Moodle key feature, to allow multiple instances to run in the same server), would force us to create some awful tricks to generate the objects.&lt;br /&gt;
&lt;br /&gt;
So, finally, we decided to build our own XML files, with everything we need to define every object present in the DB.&lt;br /&gt;
&lt;br /&gt;
== The XMLDB editor ==&lt;br /&gt;
[[XMLDB_editor | Main article]]&lt;br /&gt;
&lt;br /&gt;
Although the XML is pretty simple to read (and to write), one of the major drawbacks was its easy and error-prone adoption by the developers. Also some problems with versioning systems getting crazy with XML files (thanks ML!) pointed us to the requirement to use one high-density format (it means, physically &#039;&#039;&#039;long lines&#039;&#039;&#039;) in our XML files. &lt;br /&gt;
&lt;br /&gt;
After some intense thoughts we decided to build one specialised editor for our XML format. This editor should be easy to use and provide support for all the objects present one Moodle DB. And it&#039;s done (and will support future enhancements easily, we hope).&lt;br /&gt;
&lt;br /&gt;
The XMLDB Editor makes the addition of tables/fields/keys/indexes practically a trivial task, allowing the developer to spend  the time coding and improving things instead of fighting against XML files and the errors caused by manual editing (of course, the developer is free to use such extra-time as desired, beers, dance, books, music...) ;-)&lt;br /&gt;
&lt;br /&gt;
All the new &#039;&#039;&#039;install.xml&#039;&#039;&#039; files, present under each &#039;&#039;&#039;db&#039;&#039;&#039; directory in Moodle can be edited (and we recommend it) with just some clicks and keystrokes. Those &#039;&#039;&#039;install.xml&#039;&#039;&#039; will contain all the info needed to generate the specific objects needed for each RDBMS supported. Obviously, such files, are the neutral replacement for all the *.sql files used until now.&lt;br /&gt;
&lt;br /&gt;
=== Launching ===&lt;br /&gt;
&lt;br /&gt;
Just login to your server as an administrator and, under the Miscellaneous tab of the Administration Block, you&#039;ll see a new link pointing to the &amp;quot;XMLDB Editor&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One &#039;&#039;&#039;important note&#039;&#039;&#039; is that, to be able to handle files properly, the web server needs write access to all those &amp;quot;db&amp;quot; directories where the &amp;quot;install.xml&amp;quot; files reside (and to the files themselves, of course). ;-)&lt;br /&gt;
&lt;br /&gt;
That&#039;s all!&lt;br /&gt;
&lt;br /&gt;
=== Use===&lt;br /&gt;
&lt;br /&gt;
We really think the XMLDB Editor is pretty easy to use, so here you won&#039;t see a complete guide to use it. We highly recommend you to play with it for a while, viewing how it works and how it modifies the &#039;&#039;&#039;install.xml&#039;&#039;&#039; files.&lt;br /&gt;
&lt;br /&gt;
It&#039;s organised in a top-botton structure, where you start &#039;&#039;&#039;loading&#039;&#039;&#039; (or &#039;&#039;&#039;creating&#039;&#039;&#039;) a new XMLDB file. Then, you can &#039;&#039;&#039;edit&#039;&#039;&#039; such file and its &#039;&#039;&#039;general structure&#039;&#039;&#039; will be showed. This structure have two type of elements, &#039;&#039;&#039;tables&#039;&#039;&#039; and &#039;&#039;&#039;statements&#039;&#039;&#039; and the XMLDB Editor allows you to &#039;&#039;&#039;add&#039;&#039;&#039;, &#039;&#039;&#039;edit&#039;&#039;&#039;, &#039;&#039;&#039;delete&#039;&#039;&#039;, and &#039;&#039;&#039;move&#039;&#039;&#039; them easily. Also, for initial creation of tables, one small but effective &#039;&#039;&#039;reverse-enginery&#039;&#039;&#039; tool has been developed (only under MySQL) allowing you to retrofit any table from the DB to the XMLDB Editor.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: If you can&#039;t click on the create links....&#039;&#039;&#039; you must first create the /db folder (as shown in the list, but it may not really exist) and then make sure it is writeable by the webserver&lt;br /&gt;
&lt;br /&gt;
While editing tables you will see their &#039;&#039;&#039;fields&#039;&#039;&#039;, &#039;&#039;&#039;keys&#039;&#039;&#039; and &#039;&#039;&#039;indexes&#039;&#039;&#039; and you&#039;ll be able to handle all them easily. Note that some fields can be no-editable. It uses to be because they are being used in some way (part of one key or index) and the idea is to warn you about that.&lt;br /&gt;
&lt;br /&gt;
Fields can be edited and you can specify their &#039;&#039;&#039;name&#039;&#039;&#039;, &#039;&#039;&#039;type&#039;&#039;&#039;, &#039;&#039;&#039;length&#039;&#039;&#039;, &#039;&#039;&#039;decimals&#039;&#039;&#039;, &#039;&#039;&#039;null-ability&#039;&#039;&#039;, &#039;&#039;&#039;defaults&#039;&#039;&#039; and so one. Exactly the same for both &#039;&#039;&#039;keys&#039;&#039;&#039; and &#039;&#039;&#039;indexes&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
While editing statements, you must think about them like &amp;quot;collections of sentences&amp;quot;. Once you select the &#039;&#039;&#039;type&#039;&#039;&#039; (only inserts are allowed for now) and &#039;&#039;&#039;table&#039;&#039;&#039; you are interested you&#039;ll be able to introduce the exact values easily, being able to &#039;&#039;&#039;duplicate&#039;&#039;&#039; them easily to gain some speed if you have a lot of sentences in your development. Sentences can be &#039;&#039;&#039;edited&#039;&#039;&#039; and &#039;&#039;&#039;deleted&#039;&#039;&#039; easily too.&lt;br /&gt;
&lt;br /&gt;
One interesting feature is that all the XMLDB Editor pages allow you to enter one &#039;&#039;&#039;comment&#039;&#039;&#039; about the item being modified (table, index, key, field, statement...). Use it at your entire needs, sure it helps other developers to understand a bit more the DB model.&lt;br /&gt;
&lt;br /&gt;
Please, don&#039;t forget to read and understand the next section, where we talk about &#039;&#039;&#039;some important guidelines&#039;&#039;&#039; to create and handle XMLDB files.&lt;br /&gt;
&lt;br /&gt;
== Conventions ==&lt;br /&gt;
&lt;br /&gt;
Apart of the [[Database| Database Structures guidelines]], some more conventions should be followed:&lt;br /&gt;
&lt;br /&gt;
# About names:&lt;br /&gt;
## All lowercase names (tables, indexes, keys and fields).&lt;br /&gt;
## Table names and field names must use only a-z, 0-9 and _ chars. Table names can be at most 28 characters long; column names at most 30 characters.&lt;br /&gt;
## Key and index names under the XMLDB Files must be formed by concatenating the name of the fields present in the key/index with the &#039;&amp;quot;-&amp;quot; (minus) character.&lt;br /&gt;
## Primary key always must be named &amp;quot;primary&amp;quot; (this is one exception to the previous convention).&lt;br /&gt;
## It&#039;s highly recommended to avoid [[XMLDB_reserved_words|reserved words]] completely. We know we have some of them now but they should be completely out for next releases.&lt;br /&gt;
# About NULLS&lt;br /&gt;
## Avoid to create all the fields as NOT NULL with the &#039;&#039;silly&#039;&#039; default value &amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt; (empty string). The underlying code used to create tables will handle it properly but the XMLDB structure must be REAL. Read more in the [[XMLDB_Problems#NOT_NULL_fields_using_a_DEFAULT_.27.27_clause|Problems Page]].&lt;br /&gt;
# About FOREIGN KEYS&lt;br /&gt;
## Under the tables of every XMLDB file, you must define the existing &#039;&#039;&#039;Foreign Keys&#039;&#039;&#039; (FK) properly. This will allow everybody to know a bit better the structure, allow to evolve to a better constrained system in the future and will provide the underlying code with the needed info to create the proper indexes. &lt;br /&gt;
## Note that, if you define any field combination as FK you won&#039;t have to create any index on that fields, the code will do it automatically! &lt;br /&gt;
## Respect Convention 1.3&lt;br /&gt;
# About UNIQUE KEYS&lt;br /&gt;
## Declare any fields as UNIQUE KEY (UK) only if they are going to be used as target for one FK. Create unique indexes instead.&lt;br /&gt;
## Respect Convention 1.3&lt;br /&gt;
&lt;br /&gt;
== One example: the assignment module ==&lt;br /&gt;
&lt;br /&gt;
Here we are going to examine the [http://cvs.moodle.org/moodle/mod/assignment/db/install.xml?view=markup current implementation of the XMLDB Schema for the assignment module] (a simple one). It has been completely generated with the XMLDB Editor but it&#039;s nice to know a bit more about the XML internals.&lt;br /&gt;
&lt;br /&gt;
As you can see the structure is pretty simple:&lt;br /&gt;
&lt;br /&gt;
* XMLDB&lt;br /&gt;
** TABLES, one or more, each one with&lt;br /&gt;
*** FIELDS&lt;br /&gt;
*** KEYS&lt;br /&gt;
*** INDEXES&lt;br /&gt;
** STATEMENTS, none or more, each one with&lt;br /&gt;
*** SENTENCES&lt;br /&gt;
&lt;br /&gt;
First of all you should note that all the elements contain the &#039;&#039;&#039;PREVIOUS&#039;&#039;&#039; and &#039;&#039;&#039;NEXT&#039;&#039;&#039; attributes. They allow us to keep everything ordered although it isn&#039;t meaningful at all from the RDBMS perspective. Also the &#039;&#039;&#039;COMMENT&#039;&#039;&#039; field is present everywhere to be used as desired.&lt;br /&gt;
&lt;br /&gt;
=== The TABLE element ===&lt;br /&gt;
&lt;br /&gt;
We can ignore the TABLE element, as it&#039;s simply one container for the internals (FIELDS, KEYS and INDEXES). Let&#039;s go to examine them a bit more:&lt;br /&gt;
&lt;br /&gt;
==== The FIELD element ====&lt;br /&gt;
&lt;br /&gt;
It maps with one field in the DB (obviously). For each field you can define its &#039;&#039;&#039;name&#039;&#039;&#039;, &#039;&#039;&#039;type&#039;&#039;&#039; (from a list of [[XMLDB column types|neutral types]]), &#039;&#039;&#039;length&#039;&#039;&#039;, &#039;&#039;&#039;decimals&#039;&#039;&#039; (for some types), &#039;&#039;&#039;notnull&#039;&#039;&#039; (true/false), &#039;&#039;&#039;unsigned&#039;&#039;&#039; (true/false), &#039;&#039;&#039;sequence&#039;&#039;&#039; (if it&#039;s autonumeric or serial, true/false) and &#039;&#039;&#039;default&#039;&#039;&#039; (to assign a default value).&lt;br /&gt;
&lt;br /&gt;
So, in our example, we have two tables, assignment and assignment_submissions, each one with its own fields, defining all the information related above. Please note that naming conventions are followed.&lt;br /&gt;
&lt;br /&gt;
==== The KEY element ====&lt;br /&gt;
&lt;br /&gt;
Here is where all the PRIMARY KEYS (PK), UNIQUE KEYS (UK) and FOREIGN KEYS (FK) will be defined. For each key we define its &#039;&#039;&#039;name&#039;&#039;&#039;, &#039;&#039;&#039;type&#039;&#039;&#039;, &#039;&#039;&#039;fields&#039;&#039;&#039; (that belongs to it) and optionally (if the key is one FK) the target &#039;&#039;&#039;reftable&#039;&#039;&#039; and &#039;&#039;&#039;reffields&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
In our example, the assignment table has one (mandatory!) PK (called, &amp;quot;primary&amp;quot;, rules are rules) built with the &amp;quot;id&amp;quot; field. &lt;br /&gt;
&lt;br /&gt;
The other table, the &amp;quot;assignment_submissions&amp;quot; one, also has its PK (called &amp;quot;primary&amp;quot; once more) and one FK, with the field &amp;quot;assignment&amp;quot; pointing to the field &amp;quot;id&amp;quot; of the table &amp;quot;assignment&amp;quot;. Note that the FK follows the name conventions and its name is, simply, the name of the fields being part of it (&amp;quot;assignment&amp;quot;). Also, the FK has as target to one PK of the same module.&lt;br /&gt;
&lt;br /&gt;
Finally, note that there isn&#039;t any index created for all these keys. Moodle will generate them automatically when the table is created. All the keys will have their corresponding index. Point. ;-)&lt;br /&gt;
&lt;br /&gt;
==== The INDEX element ====&lt;br /&gt;
&lt;br /&gt;
Where all the indexes will be defined. For each index you can define its &#039;&#039;&#039;name&#039;&#039;&#039;, &#039;&#039;&#039;unique&#039;&#039;&#039; (true/false) and the &#039;&#039;&#039;fields&#039;&#039;&#039; (as a comma-separated string) that it comprises. Please note that naming conventions are followed.&lt;br /&gt;
&lt;br /&gt;
Also, some &amp;quot;obvious index&amp;quot;, like the one based in the &amp;quot;assignment&amp;quot; field of the &amp;quot;assignment_submissions&amp;quot; table doesn&#039;t exist. Yes, you know why: Because such column has been defined as a FK and the index will be automatically created (see previous section).&lt;br /&gt;
&lt;br /&gt;
=== The STATEMENT element ===&lt;br /&gt;
&lt;br /&gt;
This is the other &#039;&#039;&#039;big container&#039;&#039;&#039; in the XMLDB Schema (at the same level as the &#039;&#039;&#039;TABLES&#039;&#039;&#039; one) and we can define its &#039;&#039;&#039;name&#039;&#039;&#039;, &#039;&#039;&#039;type&#039;&#039;&#039; (only insert allowed for now) and &#039;&#039;&#039;table&#039;&#039;&#039; (against the sentences will be executed).&lt;br /&gt;
&lt;br /&gt;
Every statement is a collection of &#039;&#039;&#039;sentences&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
==== The SENTENCE element ====&lt;br /&gt;
&lt;br /&gt;
Each sentence implies one simple action to be performed against the DB and it can be defined as the &amp;quot;missing part of the SQL statement&amp;quot;. In our example, we have one statement, of type &amp;quot;insert&amp;quot; on table &amp;quot;log_display&amp;quot;. With this Moodle knows the initial part of the sentence, i.e:&lt;br /&gt;
&lt;br /&gt;
 INSERT INTO log_display &lt;br /&gt;
&lt;br /&gt;
and then the text will be added to create this:&lt;br /&gt;
&lt;br /&gt;
 INSERT INTO log_display &lt;br /&gt;
   (module, action, mtable, field) &lt;br /&gt;
 VALUES &lt;br /&gt;
   (&#039;assignment&#039;, &#039;view&#039;, &#039;assignment&#039;, &#039;name&#039;)&lt;br /&gt;
&lt;br /&gt;
There is one important trick when handling sentences, although they aren&#039;t in the assignment example. Take a look to the [http://cvs.moodle.org/moodle/lib/db/install.xml?view=co Core Tables XML Schema] (it&#039;s a huge one!). If you go near the end, to the statements section, you will see some sentences like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;SENTENCE TEXT=&amp;quot;....VALUES (&#039;user&#039;, &#039;view&#039;, &#039;user&#039;, &#039;CONCAT(firstname,&amp;quot; &amp;quot;,lastname)&#039;)&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such &amp;quot;CONCAT&amp;quot; function isn&#039;t standard at all (only MySQL supports it), but don&#039;t worry, we&#039;ll transform it to the correct concatenation operators for other RDBMS. Just be sure to use the syntax showed above.&lt;br /&gt;
&lt;br /&gt;
== DTD and XML schema ==&lt;br /&gt;
&lt;br /&gt;
Not sure if this will be usable for somebody but here you can find one [http://cvs.moodle.org/moodle/lib/xmldb/xmldb.dtd?view=co automatically generated DTD] for the XMLDB files. Also one [http://cvs.moodle.org/moodle/lib/xmldb/xmldb.xsd?view=co automatically generated XML Schema] is available. Any improvement/fix to them will be welcome!&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[XMLB List of files to create|List of files to create]]: The list of files to be created from scratch. Used to follow the progress.&lt;br /&gt;
* http://www.hitsw.com/xml_utilites/: One online XML-DTD-Schema converter.&lt;br /&gt;
&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:DB]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Analytics_API&amp;diff=53705</id>
		<title>Analytics API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Analytics_API&amp;diff=53705"/>
		<updated>2018-02-14T22:52:11Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Indicator (core_analytics\local\indicator\base) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
&lt;br /&gt;
The Moodle Analytics API allows Moodle site managers to define prediction models that combine indicators and a target. The target is the event we want to predict. The indicators are what we think will lead to an accurate prediction of the target. Moodle is able to evaluate these models and, if the prediction accuracy is high enough, Moodle internally trains a machine learning algorithm by using calculations based on the defined indicators within the site data. Once new data that matches the criteria defined by the model is available, Moodle starts predicting the probability that the target event will occur. Targets are free to define what actions will be performed for each prediction, from sending messages or feeding reports to building new adaptive learning activities.&lt;br /&gt;
&lt;br /&gt;
An obvious example of a model you may be interested in is prevention of [https://docs.moodle.org/34/en/Students_at_risk_of_dropping_out students at risk of dropping out]: Lack of participation or bad grades in previous activities could be indicators, and the target would be whether the student is able to complete the course or not. Moodle calculates these indicators and the target for each student in a finished course and predicts which students are at risk of dropping out in ongoing courses.&lt;br /&gt;
&lt;br /&gt;
=== API components ===&lt;br /&gt;
&lt;br /&gt;
This diagram shows the main components of the analytics API and the interactions between them.&lt;br /&gt;
 &lt;br /&gt;
[[File:Inspire_API_components.png]]&lt;br /&gt;
&lt;br /&gt;
=== Data flow ===&lt;br /&gt;
&lt;br /&gt;
The diagram below shows the different stages data goes through, from the data a Moodle site contains to actionable insights. &lt;br /&gt;
&lt;br /&gt;
[[File:Inspire_data_flow.png]]&lt;br /&gt;
&lt;br /&gt;
=== API classes diagram ===&lt;br /&gt;
&lt;br /&gt;
This is a summary of the API classes and their relationships. It groups the different parts of the framework that can be extended by 3rd parties to create your own prediction models.&lt;br /&gt;
&lt;br /&gt;
[[File:Analytics_API_classes_diagram_(summary).svg]]&lt;br /&gt;
&lt;br /&gt;
(Click on the image to expand, it is an SVG)&lt;br /&gt;
&lt;br /&gt;
== Built-in models ==&lt;br /&gt;
&lt;br /&gt;
People use Moodle in very different ways and even courses on the same site can vary significantly. Moodle core will only include models that have been proven to be good at predicting in a wide range of sites and courses. Moodle 3.4 provides two built-in models:&lt;br /&gt;
&lt;br /&gt;
* [https://docs.moodle.org/34/en/Students_at_risk_of_dropping_out Students at risk of dropping out]&lt;br /&gt;
* [https://docs.moodle.org/34/en/Analytics#No_teaching No teaching]&lt;br /&gt;
&lt;br /&gt;
To diversify the samples and to cover a wider range of cases, the Moodle HQ research team is collecting anonymised Moodle site datasets from collaborating institutions and partners to train the machine learning algorithms with them. The models that Moodle is shipped with will obviously better at predicting on the sites of participating institutions, although some other datasets are used as test data for the machine learning algorithm to ensure that the models are good enough to predict accurately in any Moodle site. If you are interested in collaborating please contact [[user:emdalton1|Elizabeth Dalton]] at [[mailto:elizabeth@moodle.com elizabeth@moodle.com]] to get information about the process.&lt;br /&gt;
&lt;br /&gt;
Even if the models we include in Moodle core are already trained by Moodle HQ, each different site will continue training that site machine learning algorithms with its own data, which should lead to better prediction accuracy over time.&lt;br /&gt;
&lt;br /&gt;
== Concepts == &lt;br /&gt;
&lt;br /&gt;
The following definitions are included for people not familiar with machine learning concepts: &lt;br /&gt;
&lt;br /&gt;
=== Training ===&lt;br /&gt;
&lt;br /&gt;
This is the process to be run on a Moodle site before being able to predict anything. This process records the relationships found in site data from the past so the analytics system can predict what is likely to happen under the same circumstances in the future. What we train are machine learning algorithms.&lt;br /&gt;
&lt;br /&gt;
=== Samples ===&lt;br /&gt;
&lt;br /&gt;
The machine learning backends we use to make predictions need to know what sort of patterns to look for, and where in the Moodle data to look. A sample is a set of calculations we make using a collection of Moodle site data. These samples are unrelated to testing data or phpunit data, and they are identified by an id matching the data element on which the calculations are based. The id of a sample can be any Moodle entity id: a course, a user, an enrolment, a quiz attempt, etc. and the calculations the sample contains depend on that element. Each type of Moodle entity used as a sample helps develop the predictions that involve that kind of entity. For example, samples based on Quiz attempts will help develop the potential insights that the analytics might offer that are related to the Quiz attempts by a particular group of students. See [[Analytics_API#Analyser]] for more information on how to use analyser classes to define what is a sample.&lt;br /&gt;
&lt;br /&gt;
=== Prediction model ===&lt;br /&gt;
&lt;br /&gt;
As explained above, a prediction model is a combination of indicators and a target. System models can be viewed in &#039;&#039;&#039;Site administration &amp;gt; Analytics &amp;gt; Analytics models&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The relationship between indicators and targets is stored in &#039;&#039;analytics_models&#039;&#039; database table.&lt;br /&gt;
&lt;br /&gt;
The class &#039;&#039;&#039;\core_analytics\model&#039;&#039;&#039; manages all of a model&#039;s related actions. &#039;&#039;evaluate()&#039;&#039;, &#039;&#039;train()&#039;&#039; and &#039;&#039;predict()&#039;&#039; forward the calculated indicators to the machine learning backends. &#039;&#039;&#039;\core_analytics\model&#039;&#039;&#039; delegates all heavy processing to analysers and machine learning backends. It also manages prediction models evaluation logs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\core_analytics\model&#039;&#039;&#039; class is not expected to be extended.&lt;br /&gt;
&lt;br /&gt;
==== Static models ====&lt;br /&gt;
&lt;br /&gt;
Some prediction models do not need a powerful machine learning algorithm behind them processing large quantities of data to make accurate predictions. There are obvious events that different stakeholders may be interested in knowing that we can easily calculate. These *Static model* predictions are directly calculated based on indicator values. They are based on the assumptions defined in the target, but they should still be based on indicators so all these indicators can still be reused across different prediction models. For this reason, static models are not editable through &#039;&#039;&#039;Site administration &amp;gt; Analytics &amp;gt; Analytics models&#039;&#039;&#039; user interface.&lt;br /&gt;
&lt;br /&gt;
Some examples of possible static models:&lt;br /&gt;
* [https://docs.moodle.org/en/Analytics#No_teaching Courses without teaching activity]&lt;br /&gt;
* Courses with students submissions requiring attention and no teachers accessing the course&lt;br /&gt;
* Courses that started 1 month ago and never accessed by anyone&lt;br /&gt;
* Students that have never logged into the system&lt;br /&gt;
* ....&lt;br /&gt;
&lt;br /&gt;
Moodle could already generate notifications for the examples above, but there are some benefits on doing it using the Moodle analytics API:&lt;br /&gt;
* Everything is easier and faster to code from a dev point of view as the analytics subsystem provides APIs for everything&lt;br /&gt;
* New Indicators will be part of the core indicators pool that researchers (and 3rd party developers in general) can reuse in their own models&lt;br /&gt;
* Existing core indicators can be reused as well (the same indicators used for insights that depend on machine learning backends)&lt;br /&gt;
* Notifications are displayed using the core insights system, which is also responsible of sending the notifications and all related actions.&lt;br /&gt;
* The Analytics API tracks user actions after viewing the predictions, so we can know if insights result in actions, which insights are not useful, etc. User responses to insights could themselves be defined as an indicator.&lt;br /&gt;
&lt;br /&gt;
=== Analyser ===&lt;br /&gt;
&lt;br /&gt;
Analysers are responsible for creating the dataset files that will be sent to the machine learning processors. They are coded as PHP classes. Moodle core includes some analysers that you can use in your models.&lt;br /&gt;
&lt;br /&gt;
The base class &#039;&#039;&#039;\core_analytics\local\analyser\base&#039;&#039;&#039; does most of the work. It contains a key abstract method, &#039;&#039;get_all_samples()&#039;&#039;. This method is what defines the sample unique identifier across the site. Analyser classes are also responsible of including all site data related to that sample id; this data will be used when indicators are calculated. e.g. A sample id &#039;&#039;user enrolment&#039;&#039; would include data about the &#039;&#039;course&#039;&#039;, the course &#039;&#039;context&#039;&#039; and the &#039;&#039;user&#039;&#039;. Samples are nothing by themselves, just a list of ids with related data. They are used in calculations once they are combined with the target and the indicator classes.&lt;br /&gt;
&lt;br /&gt;
Other analyser class responsibilities:&lt;br /&gt;
* Define the context of the predictions&lt;br /&gt;
* Discard invalid data&lt;br /&gt;
* Filter out already trained samples&lt;br /&gt;
* Include the time factor (time range processors, explained below)&lt;br /&gt;
* Forward calculations to indicators and target classes&lt;br /&gt;
* Record all calculations in a file&lt;br /&gt;
* Record all analysed sample ids in the database&lt;br /&gt;
&lt;br /&gt;
If you are introducing a new analyser, there is an important non-obvious fact you should know about: for scalability reasons, all calculations at course level are executed in per-course basis and the resulting datasets are merged together once all site courses analysis is complete. This is for performance reasons: depending on the sites&#039; size it could take hours to complete the analysis of the entire site. This is a good way to break the process up into pieces. When coding a new analyser you need to decide if you want to extend &#039;&#039;&#039;\core_analytics\local\analyser\by_course&#039;&#039;&#039; (your analyser will process a list of courses), &#039;&#039;&#039;\core_analytics\local\analyser\sitewide&#039;&#039;&#039; (your analyser will receive just one analysable element, the site) or create your own analyser for activities, categories or any other Moodle entity.&lt;br /&gt;
&lt;br /&gt;
=== Target ===&lt;br /&gt;
&lt;br /&gt;
Targets are the key element that defines the model. As a PHP class, targets represent the event the model is attempting to predict (the [https://en.wikipedia.org/wiki/Dependent_and_independent_variables dependent variable in supervised learning]). They also define the actions to perform depending on the received predictions.&lt;br /&gt;
&lt;br /&gt;
Targets depend on analysers, because analysers provide them with the samples they need. Analysers are separate entities from targets because analysers can be reused across different targets. Each target needs to specify which analyser it is using. Here are a few examples to clarify the difference between analysers, samples and targets:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Target&#039;&#039;&#039;: &#039;students at risk of dropping out&#039;. &#039;&#039;&#039;Analyser provides sample&#039;&#039;&#039;: &#039;course enrolments&#039; &lt;br /&gt;
* &#039;&#039;&#039;Target&#039;&#039;&#039;: &#039;spammer&#039;. &#039;&#039;&#039;Analyser provides sample&#039;&#039;&#039;: &#039;site users&#039;&lt;br /&gt;
* &#039;&#039;&#039;Target&#039;&#039;&#039;: &#039;ineffective course&#039;. &#039;&#039;&#039;Analyser provides sample&#039;&#039;&#039;: &#039;courses&#039;&lt;br /&gt;
* &#039;&#039;&#039;Target&#039;&#039;&#039;: &#039;difficulties to pass a specific quiz&#039;. &#039;&#039;&#039;Analyser provides sample&#039;&#039;&#039;: &#039;quiz attempts in a specific quiz&#039;&lt;br /&gt;
&lt;br /&gt;
A callback defined by the target will be executed once new predictions start coming so each target have control over the prediction results.&lt;br /&gt;
&lt;br /&gt;
The API supports binary classification, multiclass classification and regression, but the machine learning backends included in core do not yet support multiclass classification or regression, so only binary classifications will be initially fully supported. See MDL-59044 and MDL-60523 for more information.&lt;br /&gt;
&lt;br /&gt;
Although there is no technical restriction against using core targets in your own models, in most cases each model will implement a new target. One possible case in which targets might be reused would be to create a new model using the same target and a different sets of indicators, for A/B testing&lt;br /&gt;
&lt;br /&gt;
==== Insights ====&lt;br /&gt;
&lt;br /&gt;
Another aspect controlled by targets is insight generation. Insights represent predictions made about a specific element of the sample within the context of the analyser model. This context will be used to notify users with &#039;&#039;&#039;moodle/analytics:listinsights&#039;&#039;&#039; capability (the teacher role by default) about new insights being available. These users will receive a notification with a link to the predictions page where all predictions of that context are listed.&lt;br /&gt;
&lt;br /&gt;
A set of suggested actions will be available for each prediction. In cases like &#039;&#039;[https://docs.moodle.org/en/Students_at_risk_of_dropping_out Students at risk of dropping out]&#039;&#039; the actions can be things like sending a message to the student, viewing the student&#039;s course activity report, etc.&lt;br /&gt;
&lt;br /&gt;
=== Indicator ===&lt;br /&gt;
&lt;br /&gt;
Indicator PHP classes are responsible for calculating indicators (predictor value or [https://en.wikipedia.org/wiki/Dependent_and_independent_variables independent variable in supervised learning]) using the provided sample. Moodle core includes a set of indicators that can be used in your models without additional PHP coding (unless you want to extend their functionality).&lt;br /&gt;
&lt;br /&gt;
Indicators are not limited to a single analyser like targets are. This makes indicators easier to reuse in different models. Indicators specify a minimum set of data they need to perform the calculation. The indicator developer should also make an effort to imagine how the indicator will work when different analysers are used. For example an indicator named &#039;&#039;Posts in any forum&#039;&#039; could be initially coded for a &#039;&#039;Shy students in a course&#039;&#039; target; this target would use &#039;&#039;course enrolments&#039;&#039; analyser, so the indicator developer knows that a &#039;&#039;course&#039;&#039; and an &#039;&#039;enrolment&#039;&#039; will be provided by that analyser, but this indicator can be easily coded so the indicator can be reused by other analysers like &#039;&#039;courses&#039;&#039; or &#039;&#039;users&#039;. In this case the developer can chose to require &#039;&#039;course&#039;&#039; &#039;&#039;&#039;or&#039;&#039;&#039; &#039;&#039;user&#039;&#039;, and the name of the indicator would change according to that. For example, &#039;&#039;User posts in any forum&#039;&#039; could be used in a user-based model like &#039;&#039;Inactive users&#039;&#039; and in any other model where the analyser provides &#039;&#039;user&#039;&#039; data; &#039;&#039;Posts in any of the course forums&#039;&#039; could be used in a course-based model like &#039;&#039;Low participation courses.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The calculated value can go from -1 (minimum) to 1 (maximum). This requirement prevents the creation of &amp;quot;raw number&amp;quot; indicators like &#039;&#039;absolute number of write actions,&#039;&#039; because we must limit the calculation to a range, e.g. -1 = 0 actions, -0.33 = some basic activity, 0.33 = activity, 1 = plenty of activity. Raw counts of an event like &amp;quot;posts to a forum&amp;quot; must be calculated in a proportion of an expected number of posts. There are several ways of doing this. One is to define a minimum desired number of events, e.g. 3 posts in a forum represents &amp;quot;some&amp;quot; activity, 6 posts represents adequate activity, and 10 or more posts represents the maximum expected activity. Another way is to compare the number of events per individual user to the mean or median value of events by all users in the same context, using statistical values. For example, a value of 0 would represent that the student posted the same number of posts as the mean of all student posts in that context; a value of -1 would indicate that the student is 2 or 3 standard deviations below the mean, and a +1 would indicate that the student is 2 or 3 standard deviations above the mean. &#039;&#039;(Note that this kind of comparative calculation has implications in pedagogy: it suggests that there is a ranking of students from best to worst, rather than a defined standard all students can reach.)&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Time splitting methods ===&lt;br /&gt;
&lt;br /&gt;
A time splitting method is what defines when the system will calculate predictions and the portion of activity logs that will be considered for those predictions. They are coded as PHP classes and Moodle core includes some time splitting methods you can use in your models.&lt;br /&gt;
&lt;br /&gt;
In some cases the time factor is not important and we just want to classify a sample. This is relatively simple. Things get more complicated when we want to predict what will happen in future. For example, predictions about [https://docs.moodle.org/en/Students_at_risk_of_dropping_out Students at risk of dropping out] are not useful once the course is over or when it is too late for any intervention.&lt;br /&gt;
&lt;br /&gt;
Calculations involving time ranges can be a challenging aspect of some prediction models. Indicators need to be designed with this in mind and we need to include time-dependent indicators within the calculated indicators so machine learning algorithms are smart enough to avoid mixing calculations belonging to the beginning of the course with calculations belonging to the end of the course.&lt;br /&gt;
&lt;br /&gt;
There are many different ways to split up a course into time ranges: in weeks, quarters, 8 parts, ten parts (tenths), ranges with longer periods at the beginning and shorter periods at the end... And the ranges can be accumulative (each one inclusive from the beginning of the course) or only from the start of the time range.&lt;br /&gt;
&lt;br /&gt;
The time-splitting methods included in Moodle 3.4 assume that there is a fixed start and end date for each course, so the course can be divided into segments of equal length. This allows courses of different lengths to be included in the same prediction model, but makes these time-splitting methods useless for courses without fixed start or end dates, e.g. self-paced courses. These courses might instead use fixed time lengths such as weeks to define the boundaries of prediction calculations.&lt;br /&gt;
&lt;br /&gt;
=== Machine learning backends ===&lt;br /&gt;
&lt;br /&gt;
Documentation available in [https://docs.moodle.org/dev/Machine_learning_backends Machine learning backends].&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
&lt;br /&gt;
The system is designed as a Moodle subsystem and API. It lives in &#039;&#039;&#039;analytics/&#039;&#039;&#039;. All analytics base classes are located here.&lt;br /&gt;
&lt;br /&gt;
[https://docs.moodle.org/dev/Machine_learning_backends Machine learning backends] is a new Moodle plugin type. They are stored in &#039;&#039;&#039;lib/mlbackend&#039;&#039;&#039;. &lt;br /&gt;
&lt;br /&gt;
Uses of the analytics API are located in different Moodle components, being core (&#039;&#039;&#039;lib/classes/analytics&#039;&#039;&#039;) the component that hosts general purpose uses of the API.&lt;br /&gt;
&lt;br /&gt;
=== Interfaces ===&lt;br /&gt;
&lt;br /&gt;
This API aims to be as extendable as possible. Any moodle component, including third party plugins, is be able to define indicators, targets, analysers and time splitting processors. Analytics API will be able to find them as long as they follow the namespace conventions described below. &lt;br /&gt;
&lt;br /&gt;
An example of a possible extension would be a plugin with indicators that fetch student academic records from the Universities&#039; student information system; the site admin could build a new model on top of the built-in &#039;students at risk of drop out detection&#039; adding the SIS indicators to improve the model accuracy or for research purposes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Note that this section do not include Machine learning backend interfaces, they are available in https://docs.moodle.org/dev/Machine_learning_backends#Interfaces.&lt;br /&gt;
&lt;br /&gt;
==== Analysable (core_analytics\analysable) ====&lt;br /&gt;
&lt;br /&gt;
Analysables are those elements in Moodle that contain samples. In most of the cases an analysable will be a course, although it can also be the site or any other Moodle element, e.g. an activity. Moodle core include two analysers &#039;&#039;&#039;\core_analytics\course&#039;&#039;&#039; and &#039;&#039;&#039;\core_analytics\site&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
They list of methods that need to be implemented is quite simple and does not require much explanation.&lt;br /&gt;
&lt;br /&gt;
It is also important to mention that analysable elements should be lazy loaded, otherwise you may have PHP memory issues. The reason is that analysers load all analysable elements in the site to calculate which ones are going to be calculated next (skipping the ones processed recently and stuff like that) You can take core_analytics\course as an example.&lt;br /&gt;
&lt;br /&gt;
Methods to implement:&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * The analysable unique identifier in the site.&lt;br /&gt;
     *&lt;br /&gt;
     * @return int.&lt;br /&gt;
     */&lt;br /&gt;
    public function get_id();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * The analysable human readable name&lt;br /&gt;
     *&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_name();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * The analysable context.&lt;br /&gt;
     *&lt;br /&gt;
     * @return \context&lt;br /&gt;
     */&lt;br /&gt;
    public function get_context();&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;get_start&#039;&#039;&#039; and &#039;&#039;&#039;get_end&#039;&#039;&#039; define the start and end times that indicators will use for their calculations.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * The start of the analysable if there is one.&lt;br /&gt;
     *&lt;br /&gt;
     * @return int|false&lt;br /&gt;
     */&lt;br /&gt;
    public function get_start();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * The end of the analysable if there is one.&lt;br /&gt;
     *&lt;br /&gt;
     * @return int|false&lt;br /&gt;
     */&lt;br /&gt;
    public function get_end();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Analyser (core_analytics\local\analyser\base) ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;get_analysables&#039;&#039;&#039; returns the whole list of analysable elements in the site. Each model will later be able to discard analysables that do not match their expectations. &#039;&#039;e.g. if your model is only interested in quizzes with a time close the analyser will return all quizzes, your model will exclude the ones without a time close. This approach is supposed to make analysers more reusable.&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the list of analysable elements available on the site.&lt;br /&gt;
     *&lt;br /&gt;
     * @return \core_analytics\analysable[] Array of analysable elements using the analysable id as array key.&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function get_analysables();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;get_all_samples&#039;&#039;&#039; and &#039;&#039;&#039;get_samples&#039;&#039;&#039; should return data associated with the sample ids they provide. This is important for 2 reasons:&lt;br /&gt;
* The data they provide alongside the sample origin is used to filter out indicators that are not related to what this analyser analyses. &#039;&#039;e.g. courses analysers do provide courses and information about courses, but not information about users, a &#039;&#039;&#039;is user profile complete&#039;&#039;&#039; indicator will require the user object to be available. A model using a courses analyser will not be able to use the &#039;&#039;&#039;is user profile complete&#039;&#039;&#039; indicator.&lt;br /&gt;
* The data included here is cached in PHP static vars; on one hand this reduces the amount of db queries indicators need to perform. On the other hand, if not well balanced, it can lead to PHP memory issues.  &lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * This function returns this analysable list of samples.&lt;br /&gt;
     *&lt;br /&gt;
     * @param \core_analytics\analysable $analysable&lt;br /&gt;
     * @return array array[0] = int[] (sampleids) and array[1] = array (samplesdata)&lt;br /&gt;
     */&lt;br /&gt;
    abstract protected function get_all_samples(\core_analytics\analysable $analysable);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * This function returns the samples data from a list of sample ids.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int[] $sampleids&lt;br /&gt;
     * @return array array[0] = int[] (sampleids) and array[1] = array (samplesdata)&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function get_samples($sampleids);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;get_sample_analysable&#039;&#039;&#039; method is executing during prediction:&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the analysable of a sample.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @return \core_analytics\analysable&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function get_sample_analysable($sampleid);&lt;br /&gt;
&lt;br /&gt;
The sample origin is the moodle database table that uses the sample id as primary key.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the sample&#039;s origin in moodle database.&lt;br /&gt;
     *&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function get_samples_origin();&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sample_access_context&#039;&#039;&#039; associates a context to a sampleid. This is important because this sample predictions will only be available for users with &#039;&#039;moodle/analytics:listinsights&#039;&#039; capability in that context.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the context of a sample.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @return \context&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function sample_access_context($sampleid);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sample_description&#039;&#039;&#039; is used to display samples in &#039;&#039;Insights&#039;&#039; report:&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Describes a sample with a description summary and a \renderable (an image for example)&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @param int $contextid&lt;br /&gt;
     * @param array $sampledata&lt;br /&gt;
     * @return array array(string, \renderable)&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function sample_description($sampleid, $contextid, $sampledata);&lt;br /&gt;
&lt;br /&gt;
==== Indicator (core_analytics\local\indicator\base) ====&lt;br /&gt;
&lt;br /&gt;
Indicators should generally extend one of these 3 classes, depending on the values they can return: &#039;&#039;core_analytics\local\indicator\binary&#039;&#039; for &#039;&#039;&#039;yes/no&#039;&#039;&#039; indicators, &#039;&#039;core_analytics\local\indicator\linear&#039;&#039; for indicators that return linear values and &#039;&#039;core_analytics\local\indicator\discrete&#039;&#039; for categorised indicators.&lt;br /&gt;
&lt;br /&gt;
You can use &#039;&#039;&#039;required_sample_data&#039;&#039;&#039; to specify what your indicator needs to be calculated; you may need a &#039;&#039;user&#039;&#039; object, a &#039;&#039;course&#039;&#039;, a &#039;&#039;grade item&#039;&#039;... The default implementation does not require anything. Models which analysers do not return the required data will not be able to use your indicator so only list here what you really need. e.g. if you need a grade_grades record mark it as required, but there is no need to require the &#039;&#039;user&#039;&#039; object and the &#039;&#039;course&#039;&#039; as well because you can obtain them from the grade_grades item. It is very likely that the analyser will provide them as well because the principle they follow is to include as much related data as possible but do not flag related objects as required because an analyser may, for example, chose to not include the &#039;&#039;user&#039;&#039; object because it is too big and sites can have memory problems.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Allows indicators to specify data they need.&lt;br /&gt;
     *&lt;br /&gt;
     * e.g. A model using courses as samples will not provide users data, but an indicator like&lt;br /&gt;
     * &amp;quot;user is hungry&amp;quot; needs user data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return null|string[] Name of the required elements (use the database tablename)&lt;br /&gt;
     */&lt;br /&gt;
    public static function required_sample_data() {&lt;br /&gt;
        return null;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
A single method must be implemented, &#039;&#039;&#039;calculate_sample&#039;&#039;&#039;. Most indicators make use of $starttime and $endtime to restrict the time period they consider for their calculations (e.g. read actions during $starttime - $endtime period) but some indicators may not need to apply any restriction (e.g. does this user have a user picture and profile description?) &#039;&#039;self::MIN_VALUE&#039;&#039; is -1 and &#039;&#039;self::MAX_VALUE&#039;&#039; is 1. We do not recommend changing these values.&lt;br /&gt;
 &lt;br /&gt;
    /**&lt;br /&gt;
     * Calculates the sample.&lt;br /&gt;
     *&lt;br /&gt;
     * Return a value from self::MIN_VALUE to self::MAX_VALUE or null if the indicator can not be calculated for this sample.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @param string $sampleorigin&lt;br /&gt;
     * @param integer $starttime Limit the calculation to this timestart&lt;br /&gt;
     * @param integer $endtime Limit the calculation to this timeend&lt;br /&gt;
     * @return float|null&lt;br /&gt;
     */&lt;br /&gt;
    abstract protected function calculate_sample($sampleid, $sampleorigin, $starttime, $endtime);&lt;br /&gt;
&lt;br /&gt;
Note that performance here is critical as it runs once for each sample and for each range in the time-splitting method; some tips:  &lt;br /&gt;
* To avoid performance issues or repeated db queries analyser classes provide information about the samples that you can use for your calculations to save some database queries. You can retrieve information about a sample with &#039;&#039;&#039;$user = $this-&amp;gt;retrieve(&#039;user&#039;, $sampleid)&#039;&#039;&#039;. &#039;&#039;retrieve()&#039;&#039; will return false if the requested data is not available.&lt;br /&gt;
* You can also overwrite &#039;&#039;fill_per_analysable_caches&#039;&#039; method if necessary (keep in mind though that PHP memory is not unlimited).&lt;br /&gt;
* Indicator instances are reset for each analysable and time range that is processed. This helps keeping the memory usage acceptably low and prevents hard-to-trace caching bugs.&lt;br /&gt;
&lt;br /&gt;
==== Target (core_analytics\local\target\base) ====&lt;br /&gt;
&lt;br /&gt;
Targets must extend &#039;&#039;&#039;\core_analytics\local\target\base&#039;&#039;&#039; or its main child class &#039;&#039;&#039;\core_analytics\local\target\binary&#039;&#039;&#039;. Even if Moodle core includes &#039;&#039;&#039;\core_analytics\local\target\discrete&#039;&#039;&#039; and &#039;&#039;&#039;\core_analytics\local\target\linear&#039;&#039;&#039; Moodle 3.4 machine learning backends only support binary classifications. So unless you are using your own machine learning backend you need to extend &#039;&#039;&#039;\core_analytics\local\target\binary&#039;&#039;&#039;. Technically targets could be reused between models although it is not very recommendable and you should focus instead in having a single model with a single set of indicators that work together towards predicting accurately. The only valid use case I can think of for models in production is using different time-splitting methods for it although, again, the proper way to solve this is by using a single time-splitting method specific for your needs.&lt;br /&gt;
&lt;br /&gt;
The first thing a target must define is the analyser class that it will use. The analyser class is specified in &#039;&#039;&#039;get_analyser_class&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the analyser class that should be used along with this target.&lt;br /&gt;
     *&lt;br /&gt;
     * @return string The full class name as a string&lt;br /&gt;
     */&lt;br /&gt;
    abstract public function get_analyser_class();&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;is_valid_analysable&#039;&#039;&#039; and &#039;&#039;&#039;is_valid_sample&#039;&#039;&#039; are used to discard elements that are not valid for your target.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Allows the target to verify that the analysable is a good candidate.&lt;br /&gt;
     *&lt;br /&gt;
     * This method can be used as a quick way to discard invalid analysables.&lt;br /&gt;
     * e.g. Imagine that your analysable don&#039;t have students and you need them.&lt;br /&gt;
     *&lt;br /&gt;
     * @param \core_analytics\analysable $analysable&lt;br /&gt;
     * @param bool $fortraining&lt;br /&gt;
     * @return true|string&lt;br /&gt;
     */&lt;br /&gt;
    public function is_valid_analysable(\core_analytics\analysable $analysable, $fortraining = true);&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Is this sample from the $analysable valid?&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @param \core_analytics\analysable $analysable&lt;br /&gt;
     * @param bool $fortraining&lt;br /&gt;
     * @return bool&lt;br /&gt;
     */&lt;br /&gt;
    public function is_valid_sample($sampleid, \core_analytics\analysable $analysable, $fortraining = true);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;calculate_sample&#039;&#039;&#039; is the method that calculates the target value.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Calculates this target for the provided samples.&lt;br /&gt;
     *&lt;br /&gt;
     * In case there are no values to return or the provided sample is not applicable just return null.&lt;br /&gt;
     *&lt;br /&gt;
     * @param int $sampleid&lt;br /&gt;
     * @param \core_analytics\analysable $analysable&lt;br /&gt;
     * @param int|false $starttime Limit calculations to start time&lt;br /&gt;
     * @param int|false $endtime Limit calculations to end time&lt;br /&gt;
     * @return float|null&lt;br /&gt;
     */&lt;br /&gt;
    protected function calculate_sample($sampleid, \core_analytics\analysable $analysable, $starttime = false, $endtime = false);&lt;br /&gt;
&lt;br /&gt;
==== Time-splitting method (core_analytics\local\time_splitting\base) ====&lt;br /&gt;
&lt;br /&gt;
Time-splitting methods are useful to define when the analytics API will train the predictions processor and when it will generate predictions. As explained above in [[Analytics_API#Time_splitting_methods]], they define time ranges based on analysable elements start and end timestamps.&lt;br /&gt;
&lt;br /&gt;
The base class is &#039;&#039;&#039;\core_analytics\local\time_splitting\base&#039;&#039;&#039;; if what you are after is to split the analysable duration in equal parts or in cumulative parts you can extend &#039;&#039;&#039;\core_analytics\local\time_splitting\equal_parts&#039;&#039;&#039; or &#039;&#039;&#039;\core_analytics\local\time_splitting\accumulative_parts&#039;&#039;&#039; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;define_ranges&#039;&#039;&#039; is the main method to implement and its values mostly depend on the current analysable element (available in &#039;&#039;&#039;$this-&amp;gt;analysable&#039;&#039;&#039;). An array of time ranges should be returned, each of these ranges should contain 3 attributes: A start time (&#039;start&#039;) and an end time (&#039;end&#039;) that will be passed to indicators so they can limit the amount of activity logs they read; the 3rd attribute is &#039;time&#039;, which value will determine when the range will be executed.&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Define the time splitting methods ranges.&lt;br /&gt;
     *&lt;br /&gt;
     * &#039;time&#039; value defines when predictions are executed, their values will be compared with&lt;br /&gt;
     * the current time in ready_to_predict&lt;br /&gt;
     *&lt;br /&gt;
     * @return array(&#039;start&#039; =&amp;gt; time(), &#039;end&#039; =&amp;gt; time(), &#039;time&#039; =&amp;gt; time())&lt;br /&gt;
     */&lt;br /&gt;
    protected function define_ranges();&lt;br /&gt;
&lt;br /&gt;
A name and description should also be specified:&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns a lang_string object representing the name for the time splitting method.&lt;br /&gt;
     *&lt;br /&gt;
     * Used as column identificator.&lt;br /&gt;
     *&lt;br /&gt;
     * If there is a corresponding &#039;_help&#039; string this will be shown as well.&lt;br /&gt;
     *&lt;br /&gt;
     * @return \lang_string&lt;br /&gt;
     */&lt;br /&gt;
    public static function get_name() : \lang_string;&lt;br /&gt;
&lt;br /&gt;
==== Calculable (core_analytics\calculable) ====&lt;br /&gt;
&lt;br /&gt;
Leaving this interface for the end because it is already implemented by &#039;&#039;&#039;\core_analytics\local\indicator\base&#039;&#039;&#039; and &#039;&#039;&#039;\core_analytics\local\target\base&#039;&#039;&#039; but you can still code targets or indicators from the &#039;&#039;&#039;\core_analytics\calculable&#039;&#039;&#039; base if you need more control.&lt;br /&gt;
&lt;br /&gt;
Both indicators and targets must implement this interface. It defines the data element to be used in calculations, whether as independent (indicator) or dependent (target) variables.&lt;br /&gt;
&lt;br /&gt;
== How to create a model ==&lt;br /&gt;
&lt;br /&gt;
=== Define the problem ===&lt;br /&gt;
&lt;br /&gt;
Start by defining what you want to predict (the target) and the subjects of these predictions (the samples). You can find the descriptions of these concepts above. The API can be used for all kinds of models, though if you want to predict something like &amp;quot;student success,&amp;quot; this definition should probably have some basis in pedagogy. (For example, the included model [https://docs.moodle.org/34/en/Students_at_risk_of_dropping_out Students at risk of dropping out] is based on the Community of Inquiry theoretical framework, and attempts to predict that students will complete a course based on indicators designed to represent the three components of the CoI framework (teaching presence, social presence, and cognitive presence)). Start by being clear about how the target will be defined. It must be trained using known examples. This means that if, for example, you want to predict the final grade of a course per student, the courses being used to train the model must include accurate final grades.&lt;br /&gt;
&lt;br /&gt;
=== How many predictions for each sample? ===&lt;br /&gt;
&lt;br /&gt;
The next decision should be how many predictions you want to get for each sample (e.g. just one prediction before the course starts or a prediction every week). A single prediction for each sample is simpler than multiple predictions at different points in time in terms of how deep into the API you will need to go to code it.&lt;br /&gt;
&lt;br /&gt;
These are not absolute statements, but in general:&lt;br /&gt;
* If you want a single prediction for each sample at a specific point in time you can reuse a sitewide analyser or define your own and control samples validity through your target&#039;s &#039;&#039;is_valid_sample()&#039;&#039; method.&lt;br /&gt;
* If you want multiple predictions at different points in time for each sample reuse an analysable element or define your own (extending &#039;&#039;&#039;\core_analytics\analysable&#039;&#039;&#039;) and reuse or define your own analyser to retrieve these analysable elements. You can control analysers validity through your target&#039;s &#039;&#039;is_valid_analysable()&#039;&#039; method.&lt;br /&gt;
* If you want predictions at activity level use a &amp;quot;by_course&amp;quot; analyser as otherwise you may have scalability problems (imagine storing in memory calculations for each grade_grades record in your site, though processing elements by courses help as we clean memory after each course is processed) &lt;br /&gt;
&lt;br /&gt;
This decision is important because:&lt;br /&gt;
* Time splitting methods are applied to analysable time start and time end (e.g. &#039;&#039;&#039;Quarters&#039;&#039;&#039; can split the duration of a course in 4 parts).&lt;br /&gt;
* Prediction results are grouped by analysable in the admin interface to list predictions.&lt;br /&gt;
* By default, insights are notified to users with &#039;&#039;&#039;moodle/analytics:listinsights&#039;&#039;&#039; capability at analysable level (though this is only a default behaviour you can overwrite in your target).&lt;br /&gt;
&lt;br /&gt;
Note that the existing time splitting methods are proportional to the length of the course, e.g. quarters, tenths, etc. This allows courses with different lengths to be included in the same sample, but requires courses to have defined start and end dates. Other time splitting methods are possible which do not depend on the defined length of the course, e.g. weekly. These would be more appropriate for self-paced courses without fixed start and end dates.&lt;br /&gt;
&lt;br /&gt;
You do not need to require a single time splitting method at this stage, and they can be changed whenever the model is trained. You do need to define whether the model will make a single prediction or multiple predictions per analysable.&lt;br /&gt;
&lt;br /&gt;
=== Create the target ===&lt;br /&gt;
&lt;br /&gt;
As specified in https://docs.moodle.org/dev/Analytics_API#Target_.28core_analytics.5Clocal.5Ctarget.5Cbase.29.&lt;br /&gt;
&lt;br /&gt;
=== Create the model ===&lt;br /&gt;
&lt;br /&gt;
You can create the model by specifying at least its target and, optionally, a set of indicators and a time splitting method:&lt;br /&gt;
&lt;br /&gt;
    // Instantiate the target: classify users as spammers&lt;br /&gt;
    $target = \core_analytics\manager::get_target(&#039;\mod_yours\analytics\target\spammer_users&#039;);&lt;br /&gt;
&lt;br /&gt;
    // Instantiate indicators: two different indicators that predict that the user is a spammer&lt;br /&gt;
    $indicator1 = \core_analytics\manager::get_indicator(&#039;\mod_yours\analytics\indicator\posts_straight_after_new_account_created&#039;);&lt;br /&gt;
    $indicator2 = \core_analytics\manager::get_indicator(&#039;\mod_yours\analytics\indicator\posts_contain_important_viagra&#039;);&lt;br /&gt;
    $indicators = array($indicator1-&amp;gt;get_id() =&amp;gt; $indicator1, $indicator2-&amp;gt;get_id() =&amp;gt; $indicator2);&lt;br /&gt;
&lt;br /&gt;
    // Create the model.&lt;br /&gt;
    $model = \core_analytics\model::create($target, $indicators, &#039;\core\analytics\time_splitting\single_range&#039;);&lt;br /&gt;
&lt;br /&gt;
Models are disabled by default because you may be interested in evaluating how good the model is at predicting before enabling them. You can enable models using Moodle UI or the analytics API:&lt;br /&gt;
 &lt;br /&gt;
    $model-&amp;gt;enable();&lt;br /&gt;
&lt;br /&gt;
=== Indicators ===&lt;br /&gt;
&lt;br /&gt;
You already know the analyser your target needs (the analyser is what provides samples) and, more or less, what time splitting method may be better for you. You can now select a list of indicators that you think will lead to accurate predictions. You may need to create your own indicators specific to the target you want to predict.&lt;br /&gt;
&lt;br /&gt;
You can use &amp;quot;&#039;Site administration &amp;gt; Analytics &amp;gt; Analytics models&amp;quot;&#039; to see the list of available indicators and add some of them to your model.&lt;br /&gt;
&lt;br /&gt;
== API usage examples ==&lt;br /&gt;
&lt;br /&gt;
This is a list of prediction models and how they could be coded using the Analytics API:&lt;br /&gt;
&lt;br /&gt;
[https://docs.moodle.org/34/en/Students_at_risk_of_dropping_out Students at risk of dropping out] (based on student&#039;s activity, included in [https://docs.moodle.org/34/en/Analytics Moodle 3.4])&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; quarters, quarters accumulative, deciles, deciles accumulative... &lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; student enrolments (analysable elements are courses)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039;&lt;br /&gt;
** For prediction = ongoing courses&lt;br /&gt;
** For training = finished courses with activity&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, predictions should be based on finished courses data&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Not engaging course contents (based on the course contents)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; single range&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; courses (the analysable elements is the site)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039;&lt;br /&gt;
** For prediction = the course is close to the start date&lt;br /&gt;
** For training = no training&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: yes, a simple look at the course activities should be enough (e.g. are the course activities engaging?)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
No teaching (courses close to the start date without a teacher assigned, included in Moodle 3.4)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; single range&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; courses (the analysable elements is the site) it would also work using course as analysable&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039;&lt;br /&gt;
** For prediction = course close to the start date&lt;br /&gt;
** For training = no training&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: yes, just check if there are teachers&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Late assignment submissions (based on student&#039;s activity)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; close to the analysable end date (1 week before, X days before...)&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; assignment submissions (analysable elements are activities)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039;&lt;br /&gt;
** For prediction = the assignment is open for submissions&lt;br /&gt;
** For training = past assignment due date&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, predictions should be based on previous students activity&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Spam users (based on suspicious activity)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; 2 parts, one after 4h since user creation and another one after 2 days (just an example)&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; users (analysable elements are users)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039;&lt;br /&gt;
** For prediction = 4h or 2 days passed since the user was created&lt;br /&gt;
** For training = 2 days passed since the user was created (spammer flag somewhere recorded to calculate target = 1, otherwise no spammer)&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, predictions should be based on users activity logs, although this could also be done as a static model&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Students having a bad time&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; quarters accumulative or deciles accumulative&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; student enrolments (analysable elements are courses)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039;&lt;br /&gt;
** For prediction = ongoing and course activity&lt;br /&gt;
** For training = finished courses&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, ideally it should be based on previous cases&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Course not engaging for students (checking student&#039;s activity)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; quarters, quarters accumulative, deciles...&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; course (analysable elements are courses)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039; = true&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039;&lt;br /&gt;
** For prediction = ongoing course&lt;br /&gt;
** For training = finished courses&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, it should be based on activity logs&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Student will fail a quiz (based on things like other students quiz attempts, the student in other activities, number of attempts to pass the quiz...)&lt;br /&gt;
* &#039;&#039;&#039;Time splitting:&#039;&#039;&#039; single range&lt;br /&gt;
* &#039;&#039;&#039;Analyser samples:&#039;&#039;&#039; student grade on an activity, aka grade_grades id (analysable elements are courses)&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_analysable&#039;&#039;&#039;&lt;br /&gt;
** For prediction = ongoing course with quizzes&lt;br /&gt;
** For training = ongoing course with quizzes&lt;br /&gt;
* &#039;&#039;&#039;target::is_valid_sample&#039;&#039;&#039;&lt;br /&gt;
** For prediction = more than the X% of the course students attempted the quiz and the sample (a student) has not attempted it yet&lt;br /&gt;
** For training = the student has attempted the quiz at least X times (X because if after X attempts has not passed counts as failed) or the student passed the quiz&lt;br /&gt;
* &#039;&#039;&#039;Based on assumptions (static)&#039;&#039;&#039;: no, it is based on previous students records&lt;br /&gt;
&lt;br /&gt;
== Clustered environments ==&lt;br /&gt;
&lt;br /&gt;
&#039;analytics/outputdir&#039; setting can be used by Moodle sites with multiple frontend nodes to specify a shared directory across nodes. This directory can be used by machine learning backends to store trained algorithms (its internal variables weights and stuff like that) to use them later to get predictions. Moodle cron lock will prevent multiple executions of the analytics tasks that train machine learning algorithms and get predictions from them.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Templates&amp;diff=53545</id>
		<title>Templates</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Templates&amp;diff=53545"/>
		<updated>2017-12-19T16:21:58Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* How do I call a template from javascript? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.9}}&lt;br /&gt;
&lt;br /&gt;
= Templates =&lt;br /&gt;
&lt;br /&gt;
== What is a template? ==&lt;br /&gt;
A template is an alternative to writing blocks of HTML directly in javascript / php by concatenating strings. The end result is the same, but templates have a number of advantages:&lt;br /&gt;
* It is easier to see the final result of the template because the code for a template is very close to what the final HTML will look like&lt;br /&gt;
* Because the templating language is intentionally limited, it is hard to introduce complex logic into a template. This makes it far easier for a theme designer to override a template, without breaking the logic&lt;br /&gt;
* Templates can be rendered from javascript. This allows ajax operations to re-render a portion of the page.&lt;br /&gt;
&lt;br /&gt;
== How do I write a template? ==&lt;br /&gt;
Templates are written in a language called &amp;quot;[http://mustache.github.io/mustache.5.html Mustache]&amp;quot;. Mustache is written as HTML with additional tags used to format the display of the data. Mustache tags are made of 2 opening and closing curly braces &amp;lt;code xml&amp;gt;{{tag}}&amp;lt;/code&amp;gt;. There are a few variations of these tags that behave differently.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{raiden}}&amp;lt;/code&amp;gt; This is a simple variable substitution. The variable named &amp;quot;raiden&amp;quot; will be searched for in the current context (and any parent contexts) and when a value is found, the entire tag will be replaced by the variable (HTML escaped).&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{{galaga}}}&amp;lt;/code&amp;gt; This is an unescaped variable substitution. Instead of escaping the variable before replacing it in the template, the variable is included raw. This is useful when the variable contains a block of HTML (for example).&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{#lemmings}} jump off cliff {{/lemmings}}&amp;lt;/code&amp;gt; These are opening and closing section tags. If the lemmings variable exists and evaluates to &amp;quot;not false&amp;quot; value, the variable is pushed on the stack, the contents of the section are parsed and included in the result. If the variable does not exist, or evaluates to false - the section will be skipped. If the variable lemmings evaluates to an array, the section will be repeated for each item in the array with the items of the array on the context. This is how to output a list.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{^lemmings}} enjoy view {{/lemmings}}&amp;lt;/code&amp;gt; Equivalent of &amp;quot;if-not&amp;quot; block, there is no &amp;quot;else&amp;quot; in mustache.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{&amp;gt; franken_style/name_of_template }}&amp;lt;/code&amp;gt; This is a partial. Think of it like an include. Templates can include other templates using this tag. In the called template, the data it sees (for including values is the same as the data available where the partial is included.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{$blockvar}} ... {{/blockvar}}&amp;lt;/code&amp;gt; This is a block variable. It defines a section of the template that can be overridden when it&#039;s included in another template.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{&amp;lt; template_name}} ... {{/template_name}}&amp;lt;/code&amp;gt; This is similar to including a partial but specifically indicates that you&#039;d like to override one or more block variables defined within the template you&#039;re including. You can override the block variables by defining a block variable within these tags that matches the name of the block variable you&#039;d like to override in the included template.&lt;br /&gt;
&lt;br /&gt;
So - putting this all together:&lt;br /&gt;
&lt;br /&gt;
recipe.mustache&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;h3&amp;gt;{{recipename}}&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;{{description}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Ingredients&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
{{#ingredients}}&lt;br /&gt;
&amp;lt;li&amp;gt;{{.}}&amp;lt;/li&amp;gt;&lt;br /&gt;
{{/ingredients}}&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Steps&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
{{#steps}}&lt;br /&gt;
&amp;lt;li&amp;gt;{{{.}}}&amp;lt;/li&amp;gt;&lt;br /&gt;
{{/steps}}&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
{{ &amp;gt; ratethisrecipe }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When given this data:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  recipename: &amp;quot;Cheese sandwich&amp;quot;,&lt;br /&gt;
  description: &amp;quot;Who doesn&#039;t like a good cheese sandwich?&amp;quot;,&lt;br /&gt;
  ingredients: [&amp;quot;bread&amp;quot;, &amp;quot;cheese&amp;quot;, &amp;quot;butter&amp;quot;],&lt;br /&gt;
  steps: [&amp;quot;&amp;lt;p&amp;gt;Step 1 is to spread the butter on the bread&amp;lt;/p&amp;gt;&amp;quot;, &amp;quot;&amp;lt;p&amp;gt;Step 2 is to put the cheese &amp;amp;quot;in&amp;amp;quot; the bread (not on top, or underneath)&amp;lt;/p&amp;gt;&amp;quot;]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Gives this: &amp;lt;span style=&amp;quot;font-size:4em&amp;quot;&amp;gt;😋&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More info - there are much clearer explanations of templates on the [http://mustache.github.io/mustache.5.html Mustache] website. Try reading those pages &amp;quot;before&amp;quot; posting on stack overflow :) .&lt;br /&gt;
&lt;br /&gt;
=== Blocks (Moodle 3.0 onwards) ===&lt;br /&gt;
Blocks are a feature of Mustache that deserves a special mention. The are used as a form of inheritance - and are crucial to building a library of re-usable templates. To make use of &amp;quot;blocks&amp;quot; you define a parent template with replaceable sections. Each of those sections is marked with a &amp;quot;blocks&amp;quot; tag like this (A blocks tag looks like a regular tag, but the variable name is preceded with $):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;section&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;{{$sectionheading}}Default heading{{/sectionheading}}&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
{{$content}}Content for section{{/content}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now - wherever I need to re-use this template I can include it and replace the content of those sections at the same time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;lt; section}}&lt;br /&gt;
{{$sectionheading}}Latest News{{/sectionheading}}&lt;br /&gt;
{{$content}}Nothing happened today - sorry!{{/content}}&lt;br /&gt;
{{/section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when I include a template and I want to make use of blocks - the include tag points the other way:&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;lt; section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Not&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;gt; section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Blocks support looping and many other cool things - for more info see https://github.com/bobthecow/mustache.php/wiki/BLOCKS-pragma&lt;br /&gt;
&lt;br /&gt;
== Where do I put my templates? ==&lt;br /&gt;
&lt;br /&gt;
Templates go in the &amp;lt;componentdir&amp;gt;/templates folder and must have a .mustache file extension. When loading templates the template name is &amp;lt;componentname&amp;gt;/&amp;lt;filename&amp;gt; (no file extension). &lt;br /&gt;
&lt;br /&gt;
So &amp;quot;mod_lesson/timer&amp;quot; would load the template at mod/lesson/templates/timer.mustache.&lt;br /&gt;
&lt;br /&gt;
Note: Do not try and put your templates in sub folders under the &amp;quot;/templates&amp;quot; directory. This is not supported and will not work.&lt;br /&gt;
&lt;br /&gt;
== How do I call a template from javascript? ==&lt;br /&gt;
&lt;br /&gt;
Rendering a template from javascript is fairly easy. There is a new AMD module that can load/cache and render a template for you. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// This is AMD code for loading the &amp;quot;core/templates&amp;quot; module. see [Javascript Modules].&lt;br /&gt;
require([&#039;core/templates&#039;], function(templates) {&lt;br /&gt;
&lt;br /&gt;
    // This will be the context for our template. So {{name}} in the template will resolve to &amp;quot;Tweety bird&amp;quot;.&lt;br /&gt;
    var context = { name: &#039;Tweety bird&#039;, intelligence: 2 };&lt;br /&gt;
&lt;br /&gt;
    // This will call the function to load and render our template. &lt;br /&gt;
    templates.render(&#039;block_looneytunes/profile&#039;, context)&lt;br /&gt;
&lt;br /&gt;
    // It returns a promise that needs to be resoved.&lt;br /&gt;
            .then(function(html, js) {&lt;br /&gt;
                // Here eventually I have my compiled template, and any javascript that it generated.&lt;br /&gt;
                // The templates object has append, prepend and replace functions.&lt;br /&gt;
                templates.appendNodeContents(&#039;.block_looneytunes .content&#039;, source, javascript);&lt;br /&gt;
            }).fail(function(ex) {&lt;br /&gt;
                // Deal with this exception (I recommend core/notify exception function for this).&lt;br /&gt;
            });&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Under the hood, this did many clever things for us. It loaded the template via an ajax call if it was not cached. It found any missing lang strings in the template and loaded them in a single ajax request, it split the JS from the HTML and returned us both in easy to use way. Read on for how to nicely deal with the javascript parameter.&lt;br /&gt;
&lt;br /&gt;
Note: with some nice chaining and sugar, we can shorten the above example quite a bit:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
require([&#039;core/templates&#039;, &#039;core/notification&#039;], function(templates, notification) {&lt;br /&gt;
    var context = { name: &#039;Tweety bird&#039;, intelligence: 2 };&lt;br /&gt;
    templates.render(&#039;block_looneytunes/profile&#039;, context)&lt;br /&gt;
        .then(doneCallback)&lt;br /&gt;
        .fail(notification.exception);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What if a template contains javascript? ==&lt;br /&gt;
&lt;br /&gt;
Sometimes a template requires that some JS be run when it is added to the page in order to give it more features. In the template we can include blocks of javascript, but we should use a special section tag that has a &amp;quot;helper&amp;quot; method registered to handle javascript carefully. &lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
profile.mustache&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;profile&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Name: {{name}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Intelligence: {{intelligence}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
require([&#039;jquery&#039;], function($) {&lt;br /&gt;
    // Effects! Can we have &amp;quot;blink&amp;quot;?&lt;br /&gt;
    $(&#039;#profile&#039;).slideDown();&lt;br /&gt;
});&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If this template is rendered by PHP, the javascript is separated from the HTML, and is appended to a special section in the footer of the page &amp;quot;after&amp;quot; requirejs has loaded. This provides the optimal page loading speed. If the template is rendered by javascript, the javascript source will be passed to the &amp;quot;done&amp;quot; handler from the promise. Then, when the &amp;quot;done&amp;quot; handler has added the template to the DOM, it can call &lt;br /&gt;
&amp;lt;code javascript&amp;gt;templates.runTemplateJS(javascript);&amp;lt;/code&amp;gt; &lt;br /&gt;
which will run the javascript (by creating a new script tag and appending it to the page head).&lt;br /&gt;
&lt;br /&gt;
== What other helpers can I use? ==&lt;br /&gt;
&lt;br /&gt;
The implementation of these helpers is in classes like [https://github.com/moodle/moodle/blob/master/lib/classes/output/mustache_string_helper.php#L31 \core\output\mustache_string_helper]. This is set up in the [https://github.com/moodle/moodle/blob/master/lib/outputrenderers.php#L100 get_mustache method in renderer_base] (There should be no need to look at the implementation, unless you are interested.) If you are considering adding a new helper, it should be limited to display logic - and you need to be able to create identical helpers for the javascript and php implementations (javascript ones go in lib/amd/src/templates.js). &lt;br /&gt;
&lt;br /&gt;
=== {{# str }} ===&lt;br /&gt;
&lt;br /&gt;
There is a string helper for loading language strings.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} helloworld, mod_greeting {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 2 parameters are the string id and the component name. So this is effectively Mustache variant of &amp;lt;code&amp;gt;get_string(&#039;helloworld&#039;, &#039;mod_greeting&#039;)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The optional third parameter defines the value for the string&#039;s &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, David Beckham {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example would effectively do what &amp;lt;code&amp;gt;get_string(&#039;iscool&#039;, &#039;mod_cool&#039;, &#039;David Beckham&#039;)&amp;lt;/code&amp;gt; does in Moodle PHP code.&lt;br /&gt;
&lt;br /&gt;
Variable tags are allowed to define the value of the &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, {{ name }} {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For strings that accept complex placeholder, see the following section.&lt;br /&gt;
&lt;br /&gt;
=== {{# quote }} ===&lt;br /&gt;
&lt;br /&gt;
As shown in the previous section, the &amp;lt;code&amp;gt;{{# str }}&amp;lt;/code&amp;gt; helper may need complex data structures passed as the value of the &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder. You can use a JSON object syntax in that case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;: &amp;quot;David&amp;quot;, &amp;quot;lastname&amp;quot;: &amp;quot;Beckham&amp;quot; } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to use the context values instead of literal strings, you might intuitively use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{! DO NOT DO THIS !}}&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;: &amp;quot;{{ firstname }}&amp;quot;, &amp;quot;lastname&amp;quot;: &amp;quot;{{ lastname }}&amp;quot; } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is a potential problem though. If the variable tag &amp;lt;code&amp;gt;{{ firstname }}&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;{{ lastname }}&amp;lt;/code&amp;gt; evaluates to a string containing the double quote character, that will break the JSON syntax. We need to escape the double quotes potentially appearing in the variable tags. For this, use the &amp;lt;code&amp;gt;{{# quote }}&amp;lt;/code&amp;gt; helper:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{! This is OK !}}&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;:  {{# quote }}{{ firstname }}{{/ quote }}, &amp;quot;lastname&amp;quot;: {{# quote }}{{ lastname }}{{/ quote }} } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See MDL-52136 for details.&lt;br /&gt;
=== {{# pix }} ===&lt;br /&gt;
&lt;br /&gt;
There is a pix icon helper for generating pix icon tags.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# pix }} t/edit, core, Edit David Beckham {{/ pix }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 2 parameters are the string id and the component name, the rest is the alt text for the image.&lt;br /&gt;
&lt;br /&gt;
=== {{# userdate }} (Moodle 3.3 onwards) ===&lt;br /&gt;
{{Moodle 3.3}}&lt;br /&gt;
This mustache template helper will format unix timestamps into a given human readable date format while using the user&#039;s timezone settings configured (if any) in Moodle. The helper will accept hardcoded values, context variables, or other helpers. &lt;br /&gt;
&lt;br /&gt;
The recommended way to use this helper is to use the string helper to get one of the core Moodle formats because they have been translated into other languages so you&#039;ll get multi-lang support for free (that&#039;s a pretty good deal!).&lt;br /&gt;
&lt;br /&gt;
==== Using the string helper (recommended) ====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
// Assuming we had a context variable named &amp;quot;time&amp;quot; set to 1293876000.&lt;br /&gt;
{{#userdate}} {{time}}, {{#str}} strftimedate {{/str}} {{/userdate}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This will ask the Moodle server for the string &amp;quot;strftimedate&amp;quot; and will use the value (which in this case is &amp;quot;%d %B %Y&amp;quot;)  to format the user date. So the resulting formatted timestamp from the userdate helper will be &amp;quot;01 January 2011&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==== Using context variables ====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
// Assuming we had a context variable named &amp;quot;time&amp;quot; set to 1293876000.&lt;br /&gt;
{{#userdate}} {{time}}, %A, %d %B %Y, %I:%M %p {{/userdate}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will produce &amp;quot;Saturday, 01 January 2011, 10:00 AM&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Using hardcoded values ====&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{#userdate}} 1293876000, %A, %d %B %Y, %I:%M %p {{/userdate}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will produce &amp;quot;Saturday, 01 January 2011, 10:00 AM&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== {{# shortentext }} (Moodle 3.3 onwards) ===&lt;br /&gt;
{{Moodle 3.3}}&lt;br /&gt;
This helper can be used to shorten a large amount of text to a specified length and will append a trailing ellipsis to signify that the text has been shortened. &lt;br /&gt;
&lt;br /&gt;
The algorithm will attempt to preserve words while shortening to text. Words, for the purposes of the helper, are considered to be groups of consecutive characters broken by a space or, in the case of a multi-byte character, after the completion of the multi-byte character (rather than in the middle of the character).&lt;br /&gt;
&lt;br /&gt;
It will also attempt to preserve HTML in the text by keeping the opening and closing tags. Only text within the tags will be considered when calculating how much should be truncated to reach the desired length.&lt;br /&gt;
&lt;br /&gt;
The helper takes two comma separated arguments. The first is the desired length and the second is the text to be shortened. Both can be provided as context variables.&lt;br /&gt;
&lt;br /&gt;
==== Plain text ====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
{{#shortentext}} 30, long text without any tags blah de blah blah blah what {{/shortentext}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will produce:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
long text without any tags ...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== HTML text ====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
{{#shortentext}} 30, &amp;lt;div class=&#039;frog&#039;&amp;gt;&amp;lt;p&amp;gt;&amp;lt;blockquote&amp;gt;Long text with tags that will be chopped off but &amp;lt;b&amp;gt;should be added back again&amp;lt;/b&amp;gt;&amp;lt;/blockquote&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/p&amp;gt; {{/shortentext}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will produce:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;div class=&#039;frog&#039;&amp;gt;&amp;lt;p&amp;gt;&amp;lt;blockquote&amp;gt;Long text with tags that ...&amp;lt;/blockquote&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Multi-byte text ====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
{{#shortentext}} 6, 𠮟𠮟𠮟𠮟𠮟𠮟𠮟𠮟𠮟𠮟𠮟𠮟 {{/shortentext}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will produce:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
𠮟𠮟𠮟...&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do I call a template from php? ==&lt;br /&gt;
&lt;br /&gt;
The templates in php are attached to the renderers. There is a renderer method &amp;quot;render_from_template($templatename, $context)&amp;quot; that does the trick.&lt;br /&gt;
&lt;br /&gt;
Note $context is nothing to do with normal Moodle &#039;contexts&#039;. It is the data passed to the template (the &#039;context&#039; mentioned in mustache documentation).&lt;br /&gt;
&lt;br /&gt;
== How do templates work with renderers? ==&lt;br /&gt;
&lt;br /&gt;
Extra care must be taken to ensure that the data passed to the context parameter is useful to the templating language. The template language cannot:&lt;br /&gt;
* Call functions&lt;br /&gt;
* Perform any boolean logic&lt;br /&gt;
* Render renderables&lt;br /&gt;
* Do capability checks&lt;br /&gt;
* Make DB queries&lt;br /&gt;
&lt;br /&gt;
So - I have &amp;quot;some&amp;quot; data in my renderable and some logic and HTML generation in my render method for that renderable - how do I refactor this to use a template?&lt;br /&gt;
&lt;br /&gt;
The first thing to note, is that you don&#039;t have to use a template if you don&#039;t want to. It just means that themers will still have to override your render method, instead of just overriding the template. But if you DO want to use a template, you will earn &amp;quot;cred&amp;quot; with themers, and you will be able to re-render parts of your interface from javascript in response to ajax requests without reloading the whole page (that&#039;s cool).&lt;br /&gt;
&lt;br /&gt;
There is a simple pattern to use to hook a template into a render method. If you make your renderable implement templatable as well as renderable - it will have to implement a new method &amp;quot;export_for_template(renderer_base $output)&amp;quot;. This method takes the data stored in the renderable and &amp;quot;flattens it&amp;quot; so it can be used in a template. If there is some nested data in the renderable (like other renderables) and they do not support templates, they can be &amp;quot;rendered&amp;quot; into the flat data structure using the renderer parameter. It should return an stdClass with properties that are only made of simple types: int, string, bool, float, stdClass or arrays of these types. Then the render method can updated to export the data and render it with the template.&lt;br /&gt;
&lt;br /&gt;
In the renderable:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 /**&lt;br /&gt;
     * Export this data so it can be used as the context for a mustache template.&lt;br /&gt;
     *&lt;br /&gt;
     * @return stdClass&lt;br /&gt;
     */&lt;br /&gt;
    public function export_for_template(renderer_base $output) {&lt;br /&gt;
        $data = new stdClass();&lt;br /&gt;
        $data-&amp;gt;canmanage = $this-&amp;gt;canmanage;&lt;br /&gt;
        $data-&amp;gt;things = array();&lt;br /&gt;
        foreach ($this-&amp;gt;things as $thing) {&lt;br /&gt;
            $data-&amp;gt;things[] = $thing-&amp;gt;to_record();&lt;br /&gt;
        }&lt;br /&gt;
        $data-&amp;gt;navigation = array();&lt;br /&gt;
        foreach ($this-&amp;gt;navigation as $button) {&lt;br /&gt;
            $data-&amp;gt;navigation[] = $output-&amp;gt;render($button);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $data;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the renderer class:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Defer to template.&lt;br /&gt;
     *&lt;br /&gt;
     * @param mywidget $widget&lt;br /&gt;
     *&lt;br /&gt;
     * @return string HTML for the page&lt;br /&gt;
     */&lt;br /&gt;
    render(mywidget $widget) {&lt;br /&gt;
        $data = $widget-&amp;gt;export_for_template($this);&lt;br /&gt;
        return $this-&amp;gt;render_from_template(&#039;mywidget&#039;, $data);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to I override a template in my theme? ==&lt;br /&gt;
&lt;br /&gt;
Templates can be overridden a bit easier than overriding a renderer. First - find the template that you want to change. E.g. &amp;quot;mod/wiki/templates/ratingui.mustache&amp;quot;. Now, create a sub-folder under your themes &amp;quot;templates&amp;quot; directory with the component name of the plugin you are overriding. E.g &amp;quot;theme/timtam/templates/mod_wiki&amp;quot;. Finally, copy the ratingui.mustache file into the newly created &amp;quot;theme/timtam/templates/mod_wiki&amp;quot; and edit it. You should see your changes immediately if theme designer mode is on. Note: templates are cached just like CSS, so if you are not using theme designer mode you will need to purge all caches to see the latest version of an edited template. If the template you are overriding contains a documentation comment (see next section) it is recommended to remove it, it will still show the documentation in the template library.&lt;br /&gt;
&lt;br /&gt;
== Should I document my templates? ==&lt;br /&gt;
&lt;br /&gt;
Yes!!!! Theme designers need to know the limits of what they can expect to change without breaking anything. As a further benefit - your beautiful new template can be displayed in the &amp;quot;Template Library&amp;quot; tool shipped with Moodle. In order to provide nice documentation and examples for the Template Library, you should follow these conventions when documenting your template.&lt;br /&gt;
&lt;br /&gt;
=== Add a documentation comment to your template ===&lt;br /&gt;
Mustache comments look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  {{! &lt;br /&gt;
   I am a comment.&lt;br /&gt;
   I can span multiple lines.&lt;br /&gt;
  }}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The template library will look for a mustache comment that contains this special marker as the documentation to display, and the source of an example context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
@template component/templatename&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Useful things to include in the documentation for a template ====&lt;br /&gt;
===== Classes required for JS =====&lt;br /&gt;
This is a list of classes that are used by the javascript for this template. If removing a class from an element in the template will break the javascript, list it here.&lt;br /&gt;
&lt;br /&gt;
===== Data attributes required for JS =====&lt;br /&gt;
This is a list of data attributes (e.g. data-enhance=&amp;quot;true&amp;quot;) that are used by the javascript for this template. If removing a data attribute from an element in the template will break the javascript, list it here.&lt;br /&gt;
&lt;br /&gt;
===== Context variables required for this template =====&lt;br /&gt;
This is a description of the data that may be contained in the context that is passed to the template. Be explicit and document every attribute.&lt;br /&gt;
&lt;br /&gt;
===== Example context (JSON) =====&lt;br /&gt;
The Template Library will look for this data in your documentation comment as it allows it to render a &amp;quot;preview&amp;quot; of the template right in the Template Library. This is useful for theme designers to test all the available templates in their new theme to make sure they look nice in a new theme. It is also useful to make sure the template responds to different screen sizes, languages and devices. The format is a JSON-encoded object that is passed directly into the render method for this template. &lt;br /&gt;
&lt;br /&gt;
==== A full example ====&lt;br /&gt;
lib/templates/pix_icon.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  {{!                                                                                                                                 &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;
    @template core/pix_icon                                                                                                         &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Moodle pix_icon template.                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    The purpose of this template is to render a pix_icon.                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Classes required for JS:                                                                                                        &lt;br /&gt;
    * none                                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Data attributes required for JS:                                                                                                &lt;br /&gt;
    * none                                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Context variables required for this template:                                                                                   &lt;br /&gt;
    * attributes Array of name / value pairs.                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Example context (json):                                                                                                         &lt;br /&gt;
    {                                                                                                                               &lt;br /&gt;
        &amp;quot;attributes&amp;quot;: [                                                                                                             &lt;br /&gt;
            { &amp;quot;name&amp;quot;: &amp;quot;src&amp;quot;, &amp;quot;value&amp;quot;: &amp;quot;http://moodle.com/wp-content/themes/moodle/images/logo-hat2.png&amp;quot; },                          &lt;br /&gt;
            { &amp;quot;name&amp;quot;: &amp;quot;class&amp;quot;, &amp;quot;value&amp;quot;: &amp;quot;iconsmall&amp;quot; }                                                                               &lt;br /&gt;
        ]                                                                                                                           &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
  }}                                                                                                                                  &lt;br /&gt;
  &amp;lt;img {{#attributes}}{{name}}=&amp;quot;{{value}}&amp;quot; {{/attributes}}/&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Coding style for templates ==&lt;br /&gt;
This section documents some coding style guidelines to follow when writing templates. The reason for these guidelines is to promote consistency, and interoperability of the templates.&lt;br /&gt;
&lt;br /&gt;
=== Include GPL at the top of each template ===&lt;br /&gt;
&lt;br /&gt;
Templates are a form of code and it is appropriate to license them like any other code.&lt;br /&gt;
&lt;br /&gt;
===Include a documentation comment for each template===&lt;br /&gt;
&lt;br /&gt;
The exception is when you are overriding a template, if the documentation from the parent still applies, you do not need to copy it to the overridden template.&lt;br /&gt;
&lt;br /&gt;
===Use data-attributes for JS hooks===&lt;br /&gt;
&lt;br /&gt;
Data attributes are ideal for adding javascript hooks to templates because:&lt;br /&gt;
* Classes are meant for styling - theme designers should be able to change the classes at will without breaking any functionality.&lt;br /&gt;
* IDs must be unique in the page, but it is not possible to control how many times the same template might be included in the page.&lt;br /&gt;
* Data attributes can have meaningful names and can be efficiently queried with a selector&lt;br /&gt;
&lt;br /&gt;
===Avoid custom CSS for templates===&lt;br /&gt;
&lt;br /&gt;
This is not a hard rule, but a preference. We already have too much CSS in Moodle - where ever possible we should try and re-use the existing CSS instead of adding new CSS to support every new template.&lt;br /&gt;
&lt;br /&gt;
===Re-use core templates as much as possible===&lt;br /&gt;
&lt;br /&gt;
First we need to build the core set of reusable templates - but once that is in place we should always try to re-use those core templates to build interfaces. This will make Moodle more consistent, attractive and customisable.&lt;br /&gt;
&lt;br /&gt;
===Do use the CSS framework classes directly in the templates===&lt;br /&gt;
&lt;br /&gt;
We have bootstrap in core - so lets make the most of it. There is no problem using bootstrap classes in core templates, as long as the &amp;quot;base&amp;quot; theme is also tested, and an overridden template is added there if required.&lt;br /&gt;
&lt;br /&gt;
===Avoid IDs for styling or javascript===&lt;br /&gt;
&lt;br /&gt;
IDs should never evet be used for styling as they have a high CSS specificity, and so are hard to override. In addition, IDs should be unique in the page, which implies that a template could only be used once in a page. IDs are also not ideal for javascript, for the same reason (must be unique in a page).&lt;br /&gt;
&lt;br /&gt;
The only acceptable case to use an ID is you need to create a one to one connection between the JS and template. In this case use the uniqid helper to generate an ID that will not conflict with any other template on the page, and use it as part of the ID.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{uniqid}}-somethingspecific&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    callFunction(&#039;{{uniqid}}-somethingspecific&#039;);&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow CSS coding style===&lt;br /&gt;
&lt;br /&gt;
https://docs.moodle.org/dev/CSS_coding_style&lt;br /&gt;
&lt;br /&gt;
Use hyphens as word-separators for class names. &lt;br /&gt;
Use lower case class names.&lt;br /&gt;
&lt;br /&gt;
===Wrap each template in one node with a classname that matches the template name===&lt;br /&gt;
&lt;br /&gt;
Generate a class name by combining the component and template names and separating words with underscore.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;core_user_header&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:AJAX]]&lt;br /&gt;
[[Category:Javascript]]&lt;br /&gt;
[[Category:Output]]&lt;br /&gt;
&lt;br /&gt;
===Iterating over php arrays in a mustache template===&lt;br /&gt;
&lt;br /&gt;
Mustache treats hashes and arrays differently because of cross language compatibility&lt;br /&gt;
In php arrays and hashes are the same, but mustache treats them differently&lt;br /&gt;
It decides a php array is a hash and will not iterate over it if it is non 0 indexed and/or has a gap in the key numbers&lt;br /&gt;
so in short&lt;br /&gt;
you need to&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$datafortemplate-&amp;gt;mylist =  array_values($myarraywithnonnumerickeys)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
you could also use &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$datafortemplate-&amp;gt;mylist = new ArrayIterator($myarraywithnonnumerickeys);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
BUT this fails when $myarraywithnonnumerickeys is empty and you try to use&lt;br /&gt;
&amp;lt;code html5&amp;gt;&lt;br /&gt;
{{#mylist}}&lt;br /&gt;
with an array iterator if mylist is empty this block will not run&lt;br /&gt;
{{/mylist}}&lt;br /&gt;
{{^mylist}}&lt;br /&gt;
with an array iterator mylist will not run this block either because it is not quite empty&lt;br /&gt;
{{/mylist}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Test for not empty array in a mustache template===&lt;br /&gt;
&lt;br /&gt;
Short answer you can&#039;t. Include a specific property in the context like &amp;quot;hasusers&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In php &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{{#users.0}} blah {{/users.0}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will work but please don&#039;t use this syntax, it will break if the template is used in javascript AND it mucks up the context.&lt;br /&gt;
&lt;br /&gt;
In javascript&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{{#users.length}} blah {{/users.length}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will work but please don&#039;t use this syntax, it will break if the template is used in php.&lt;br /&gt;
&lt;br /&gt;
===Squash whitespace in a template===&lt;br /&gt;
Sometimes whitespace is significant, for example inside a link it will show with an underline. If you want 2 mustache tags from separate lines to be rendered with no whitespace between them you can use mustache comments to squash the whitespace.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;a href=&amp;quot;blah&amp;quot;&amp;gt;{{!&lt;br /&gt;
}}{{icon}}{{!&lt;br /&gt;
}}{{name}}&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do I write core templates? ==&lt;br /&gt;
Core templates should ideally be simple generic components that can be used within other templates to create more complex page layouts. They should be flexible enough for developers and themers to easily use without having to replace the template. The templates should attempt to encapsulate some core structure for the element as well as key classes while allowing the content to be easily overridden. Ultimately we want to avoid having duplicate HTML copied from template to template where possible, particularly if the HTML element has some classes associated with it.&lt;br /&gt;
&lt;br /&gt;
Mustache relies on variables to substitute context data into the template but unfortunately it&#039;s very unlikely that the the names of the context data will match what the template is expecting for all the places that the template might be used. So in order to allow easy extensibility and avoid having to duplicate templates just to rename the variables we can wrap them in block variables which would allow the template that is including our template to replace that variable with one from it&#039;s own context inline.&lt;br /&gt;
&lt;br /&gt;
There are a few key points to keep in mind when writing a core template:&lt;br /&gt;
* Consider how your template will actually be used. Try writing a test page that uses your template to help discover some of the assumptions you might have in the template.&lt;br /&gt;
* The example context you provide in the template is mostly just for showing the template in the template library and is likely not how your template will actually be used. Most uses of the template will have a different context all together.&lt;br /&gt;
* Try to enforce a core structure but avoid enforcing a specific context. Content should be overridable.&lt;br /&gt;
* Use block variables to indicate sections of your template that people are likely to want to change. Typically where they will be wanting to substitute in their own content.&lt;br /&gt;
* Try to keep any javascript that accompanies the template as decoupled from the HTML / CSS structure of the template as possible. Instead of relying on the existence of certain HTML elements or CSS classes it is generally better to leverage data-attributes which can be added to any element.&lt;br /&gt;
&lt;br /&gt;
=== An example: tabs ===&lt;br /&gt;
Let&#039;s go through an example to illustrate how you might build a core template. For the example we&#039;ll be building a tabs template, since it&#039;s a fairly complex component that requires the use of block variables and javascript.&lt;br /&gt;
&lt;br /&gt;
First we can create a basic template to get the general structure down, let&#039;s call it tabs. Here&#039;s what it might look like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
		{{# tabs }}&lt;br /&gt;
			&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
					data-target=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
					data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
					aria-controls=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
					aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
				&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{{ name }}}&amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;lt;/li&amp;gt;&lt;br /&gt;
		{{/ tabs }}&lt;br /&gt;
	&amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
		{{# tabs }}&lt;br /&gt;
			&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
				class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
				id=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
				{{{ content }}}&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		{{/ tabs }}&lt;br /&gt;
	&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The template requires a context that looks something like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;tabs&amp;quot;: [&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab1&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 1&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 1 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab2&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 2&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 2 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab3&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 3&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 3 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;}&lt;br /&gt;
	]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The javascript required to power the tabs element (keyboard navigation, show / hide panels etc) is written as an AMD module and is included by the template. The javascript is a little too large to go through here, but some key points to consider when writing it are:&lt;br /&gt;
It should ideally be independent of the HTML structure, so if someone wants to completely rewrite the tabs to be different elements (e.g. buttons or a set of divs) then the same javascript can be used without needing to change it. In order to achieve this it is important to identify the key components of the template.&lt;br /&gt;
&lt;br /&gt;
In this case it is a tab list, a tab and it&#039;s content. One way to identify these components would be to inspect the structure of the DOM, for example you might say &amp;quot;find me the ul element&amp;quot; when looking for the tab list and then &amp;quot;find my the child li elements&amp;quot; to find the tabs. While this would work, it couples your javascript to the HTML structure and makes it difficult to change later. A different approach would be to use the element attributes, for example you might say &amp;quot;find my the element with the role &#039;tablist&#039;&amp;quot; to get the tab list and then &amp;quot;find me the elements with the role &#039;tab&#039;&amp;quot; to get the tabs. This allows the HTML structure to change without breaking the javascript (as long as the correct attributes are set, of course).&lt;br /&gt;
&lt;br /&gt;
Another point of consideration for this example is what class to apply to a tab when it is selected. It makes sense to just apply something like &amp;quot;active&amp;quot; in the javascript, but that once again couples it to a particular CSS framework which makes it more difficult to change without modifying the javascript. In this case I chose to add a data attribute to the element to indicate which class will be set when the tab is selected. This means the javascript doesn&#039;t have to guess what the appropriate class is, it can just get it from the template.&lt;br /&gt;
&lt;br /&gt;
Ok, so we&#039;ve got our basic template. It&#039;s time to use it! Let&#039;s say we want to create a simple user profile page that might show 2 tabs, the first tab will be the user&#039;s name and the second tab will be the user&#039;s email address (please excuse the contrived example).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s what the page might look like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;lt; core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That looks pretty simple! The only problem is, how do I get my content there? I would have to supply a context like this in order to display the tabs I want:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;tabs&amp;quot;: [&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab1&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Name&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;Your name is Mr. Test User.&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab2&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Email&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;Your email is testuser@example.com&amp;quot;},&lt;br /&gt;
	]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let&#039;s assume that the context for this page doesn&#039;t match what the tabs template is expecting though (as will be the case most of the time). Let&#039;s assume the tabs template is being rendered with this context:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;name&amp;quot;:&amp;quot;Mr. Test User&amp;quot;,&lt;br /&gt;
	&amp;quot;email&amp;quot;:&amp;quot;testuser@example.com&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately, we&#039;ll almost certainly never have complete control over all of the contexts that our template will be rendered in which means we&#039;ll be expecting people to write new webservices to supply the same data in different formats every time they want to use a template. It becomes an unmanageable problem.&lt;br /&gt;
&lt;br /&gt;
Enter blocks! We can make the template more flexible by defining sections of the template that can be overriden when they are included. Pretty neat! This will allow us to enforce a certain core structure but not enforce a context on the template that is including the tabs.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s have another go at that template, this time leverging blocks:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
	{{$ tabheader }}&lt;br /&gt;
		&amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
							data-target=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
							data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
							aria-controls=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
							aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
						&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{{ name }}}&amp;lt;/a&amp;gt;&lt;br /&gt;
					&amp;lt;/li&amp;gt;&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
		&amp;lt;/ul&amp;gt;&lt;br /&gt;
	{{/ tabheader }}&lt;br /&gt;
	{{$ tabbody }}&lt;br /&gt;
		&amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
						class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
						id=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
						{{{ content }}}&lt;br /&gt;
					&amp;lt;/div&amp;gt;&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		&amp;lt;/div&amp;gt;&lt;br /&gt;
	{{/ tabbody }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A summary of what we&#039;ve changed:&lt;br /&gt;
* Added a $tabheader block around the tab list, in case someone wants to change the ul element to something else.&lt;br /&gt;
* Added a $tablist block around the group of tabs to allow them to be overriden on incldue.&lt;br /&gt;
* Added a $tabbody block around the content, in case someone wants to change the content elements from divs.&lt;br /&gt;
* Added a $tabcontent block around the tab variable for the content to allow the content to be overriden on inlcude.&lt;br /&gt;
&lt;br /&gt;
Now let&#039;s see what using this template looks like for your User Profile page:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;gt; core/tabs }}&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
						data-target=&amp;quot;{{ uniqid }}-tab1&amp;quot;&lt;br /&gt;
						data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
						aria-controls=&amp;quot;{{ uniqid }}-tab1&amp;quot;&lt;br /&gt;
						aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Name&amp;lt;/a&amp;gt;&lt;br /&gt;
				&amp;lt;/li&amp;gt;&lt;br /&gt;
				&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
						data-target=&amp;quot;{{ uniqid }}-tab2&amp;quot;&lt;br /&gt;
						data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
						aria-controls=&amp;quot;{{ uniqid }}-tab2&amp;quot;&lt;br /&gt;
						aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Email&amp;lt;/a&amp;gt;&lt;br /&gt;
				&amp;lt;/li&amp;gt;&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
					class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
					id=&amp;quot;{{ uniqid }}-tab1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					Your name is {{ name }}.&lt;br /&gt;
				&amp;lt;/div&amp;gt;&lt;br /&gt;
				&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
					class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
					id=&amp;quot;{{ uniqid }}-tab2&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					Your email address is {{ email }}.&lt;br /&gt;
				&amp;lt;/div&amp;gt;&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		{{/ core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That looks a bit better! Now we&#039;ve been able to use the blocks to successfully change the template to use the context available to this page, we no longer need a &amp;quot;tabs&amp;quot; array with &amp;quot;name&amp;quot; and &amp;quot;content&amp;quot;. Even the javascript will continue to work because we&#039;ve kept the correct element attributes. &lt;br /&gt;
&lt;br /&gt;
We&#039;ve still got a slight problem though... In order to change the data for the template we&#039;ve had to copy &amp;amp; paste the HTML from the original template into our blocks as we do the override. While this works fine in this example, it means we don&#039;t quite get the encapsulation we want within the templates since we&#039;re leaking internal implementation details. If we ever wanted to change the CSS framework we use for Moodle (say from bootstrap 2 to boostrap 3 or 4) we&#039;d have to find all the places in the code where this tabs template is used and make sure that the HTML is correct in their block overrides.&lt;br /&gt;
&lt;br /&gt;
With that in mind, let&#039;s take one more pass at this template and see if we can improve it slightly again. This time we&#039;re doing to split the template out into 3 templates.&lt;br /&gt;
&lt;br /&gt;
tabs.mustache:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
	{{$ tabheader }}&lt;br /&gt;
		&amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					{{&amp;gt; core/tab_header_item }}		&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
		&amp;lt;/ul&amp;gt;&lt;br /&gt;
	{{/ tabheader }}&lt;br /&gt;
	{{$ tabbody }}&lt;br /&gt;
		&amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					{{&amp;gt; core/tab_content_item }}&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		&amp;lt;/div&amp;gt;&lt;br /&gt;
	{{/ tabbody }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tab_header_item.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
		data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
		aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{$ tabname }}{{{ name }}}{{/ tabname }}&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tab_content_item.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
	class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	{{$ tabpanelcontent }}{{{ content }}}{{/ tabpanelcontent }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A summary of the changes:&lt;br /&gt;
* Split the template into 3, moving the tab into it&#039;s own template and the content into it&#039;s own and then including them in the tabs template.&lt;br /&gt;
* Removed the ids from the tabs and content. The javascript would be updating to assign these ids at runtime so that they don&#039;t need to be provided as part of the template context.&lt;br /&gt;
* Added a $tabname block for in the tab_header_item template to make the name flexible on import.&lt;br /&gt;
* Added a $tabpanelcontant block in the tab_content_item template to make the content flexible on import.&lt;br /&gt;
&lt;br /&gt;
Cool, so let&#039;s see what that looks like in our example now:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;gt; core/tabs }}&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{&amp;lt; core/tab_header_item }}&lt;br /&gt;
					{{$ tabname }}Name{{/ tabname }}&lt;br /&gt;
				{{/ core/tab_header_item }}&lt;br /&gt;
				{{&amp;lt; core/tab_header_item }}&lt;br /&gt;
					{{$ tabname }}Email{{/ tabname }}&lt;br /&gt;
				{{/ core/tab_header_item }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{&amp;lt; core/tab_content_item }}&lt;br /&gt;
					{{$ tabpanelcontent }}Your name is {{ name }}.{{/ tabpanelcontent }}&lt;br /&gt;
				{{/ core/tab_content_item }}&lt;br /&gt;
				{{&amp;lt; core/tab_content_item }}&lt;br /&gt;
					{{$ tabpanelcontent }}Your email address is {{ email }}.{{/ tabpanelcontent }}&lt;br /&gt;
				{{/ core/tab_content_item }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		{{/ core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we&#039;re done! After making the changes above we&#039;ve been able to keep the benefits of the previous change to allow the context changes but we&#039;ve also removed the need to copy &amp;amp; paste the HTML everywhere. Instead we&#039;re able to use the child templates with a few additional blocks defined to get the content in there.&lt;br /&gt;
&lt;br /&gt;
Now if we want to change tabs HTML or CSS frameworks we can just change the core tabs templates and this page will receive the updates for free.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Moodle_3.3.1_release_notes&amp;diff=52957</id>
		<title>Moodle 3.3.1 release notes</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Moodle_3.3.1_release_notes&amp;diff=52957"/>
		<updated>2017-09-21T14:43:23Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Releases]] &amp;gt; {{FULLPAGENAME}}&lt;br /&gt;
 &lt;br /&gt;
Release date: 10 July 2017&lt;br /&gt;
 &lt;br /&gt;
Here is [https://tracker.moodle.org/secure/IssueNavigator!executeAdvanced.jspa?jqlQuery=project+%3D+mdl+AND+resolution+%3D+fixed+AND+fixVersion+in+%28%223.3.1%22%29+ORDER+BY+priority+DESC&amp;amp;runQuery=true&amp;amp;clear=true the full list of fixed issues in 3.3.1].&lt;br /&gt;
 &lt;br /&gt;
===Highlights===&lt;br /&gt;
&lt;br /&gt;
* MDL-58136 - Show only &amp;quot;in progress&amp;quot; courses in the My courses list in Boost flat navigation&lt;br /&gt;
* MDL-56046 - Fixed bug when downloading Quiz statistics report and other multiple-sheet reports&lt;br /&gt;
* MDL-58646, MDL-59122 - Number of performance improvements in Boost cache rebuilding&lt;br /&gt;
* MDL-58310, MDL-59312, MDL-58103 - Correctly display AJAX errors and ignore interrupted requests caused by page unload (occasional &amp;quot;undefined&amp;quot; popup)&lt;br /&gt;
* MDL-44961 - When restoring course with rolling start date never change log dates&lt;br /&gt;
&lt;br /&gt;
===Security issues===&lt;br /&gt;
&lt;br /&gt;
* [https://moodle.org/mod/forum/discuss.php?d=355554 MSA-17-0006] User fullname disclosure on user preferences page&lt;br /&gt;
* [https://moodle.org/mod/forum/discuss.php?d=355555 MSA-17-0014] Course overview block reveals activities in hidden courses&lt;br /&gt;
* [https://moodle.org/mod/forum/discuss.php?d=355556 MSA-17-0015] Course creators are able to change system default settings for courses&lt;br /&gt;
* [https://moodle.org/mod/forum/discuss.php?d=355557 MSA-17-0016] Authentication bypass vulnerability with old CAS servers&lt;br /&gt;
&lt;br /&gt;
===Fixes and improvements===&lt;br /&gt;
&lt;br /&gt;
* MDL-46322 - Assignment: Only enrolled users may be assigned as markers, if admins/managers can view course but are not enrolled they will not be assigned&lt;br /&gt;
* MDL-58907 - Course overview: Remember last view mode (Timeline/Courses), add a setting for a default mode&lt;br /&gt;
* MDL-58729 - Performance impovement in MySQL collation change script (follow up for Full UTF-8 Support in MySQL)&lt;br /&gt;
* MDL-57957 - Assignment: Fixed bug with feedback files not being shown to students if assignment has no grading&lt;br /&gt;
* MDL-57021 - Use normal password form field during sign up, adding new user and enrolling in a course&lt;br /&gt;
* MDL-49988 - Wiki: line breaks in HTML source code should not affect page layout&lt;br /&gt;
* MDL-58811 - Quiz: fixed bug preventing quiz duplication if questions have file links in their texts&lt;br /&gt;
&lt;br /&gt;
===For developers===&lt;br /&gt;
&lt;br /&gt;
* MDL-58911 - Change of behavior when writing unittests for the dashboard events - now callback from module are executed in unittests same way they would be executed on the dashboard&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==See also==&lt;br /&gt;
*[[Moodle 3.3 release notes]]&lt;br /&gt;
 &lt;br /&gt;
[[Category:Release notes]]&lt;br /&gt;
[[Category:Moodle 3.3]]&lt;br /&gt;
 &lt;br /&gt;
[[fr:Notes de mise à jour de Moodle 3.3.1]]&lt;br /&gt;
[[es:Notas de Moodle 3.3.1]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Authentication_plugins&amp;diff=52626</id>
		<title>Authentication plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Authentication_plugins&amp;diff=52626"/>
		<updated>2017-06-21T10:34:22Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
This page first gives an &#039;&#039;&#039;overview&#039;&#039;&#039; of the &#039;&#039;authentication process&#039;&#039; and then explains how &#039;&#039;authentication modules&#039;&#039; can be &#039;&#039;&#039;created&#039;&#039;&#039; using hooks to take over from the native authentication in Moodle.&lt;br /&gt;
=== Overview of Moodle authentication process ===&lt;br /&gt;
[[File:1.9.11_login_element.png|203px||thumb|right|The login UI element in Moodle 1.9]]The authentication use case in Moodle starts when a user clicks on the Login link in the UI or if they try to access a protected page. There are two broad classes of authentication plugins, the regular type where moodle handles the password and ones where the password is handled by a 3rd party page eg SAML, OpenID etc.&lt;br /&gt;
&lt;br /&gt;
For the regular plugins the following happens (skipping some minor details and rarer scenarios):&lt;br /&gt;
&lt;br /&gt;
# The default login page (&amp;lt;tt&amp;gt;/login/index.php&amp;lt;/tt&amp;gt;) is displayed. OR, if a system administrator has set the Alternate Login URL on the &amp;quot;Manage authentication&amp;quot; page, that URL will be displayed.&lt;br /&gt;
# A user enters their credentials and submits the form.&lt;br /&gt;
# The handler code in &amp;lt;tt&amp;gt;/login/index.php&amp;lt;/tt&amp;gt; runs:&lt;br /&gt;
## Gets a list of enabled authentication plugins.&lt;br /&gt;
## Runs &amp;lt;tt&amp;gt;loginpage_hook()&amp;lt;/tt&amp;gt; for each plugin, in case any of them needs to intercept the login request.&lt;br /&gt;
## Checks to make sure that the username meets Moodle&#039;s criteria (alphanumeric, with periods and hyphens allowed).&lt;br /&gt;
## Calls &amp;lt;tt&amp;gt;authenticate_user_login()&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;/lib/moodlelib.php&amp;lt;/tt&amp;gt;, which returns a &amp;lt;code&amp;gt;$user&amp;lt;/code&amp;gt; object. (Details of this code follow this main outline.)&lt;br /&gt;
## Determines whether authentication was successful (by checking whether &amp;lt;code&amp;gt;$user&amp;lt;/code&amp;gt; is a valid object) and, if not, sends them back to the login page with an error message. Otherwise, it figures out where to send the user based on their original page request, whether their password is expired, etc., and redirects them there.&lt;br /&gt;
&lt;br /&gt;
For the 3rd party auth plugins the process could look like this (eg CAS, SAML, OpenID etc):&lt;br /&gt;
# Access a protected page but isn&#039;t logged in, so moodle calls &amp;lt;tt&amp;gt;pre_loginpage_hook()&amp;lt;/tt&amp;gt; on each enabled plugin, which may redirect to the 3rd party login page&lt;br /&gt;
# Or if they go directly to the login page moodle calls &amp;lt;tt&amp;gt;loginpage_hook()&amp;lt;/tt&amp;gt; on each enabled plugin, which may redirect to the 3rd party login page&lt;br /&gt;
# The user enters their credentials and authenticates on the 3rd party page&lt;br /&gt;
# The remote authentication service redirects back to moodle with an assertion, such as a single use token or encrypted response in a query param&lt;br /&gt;
# The auth plugin now validates the token or decrypts the assertion, does any other checking as required and then logs the user in using &amp;lt;tt&amp;gt;complete_user_login($user)&amp;lt;/tt&amp;gt;. If this happens inside &amp;lt;tt&amp;gt;pre_loginpage_hook()&amp;lt;/tt&amp;gt; then the user continues on their way, or if inside &amp;lt;tt&amp;gt;loginpage_hook()&amp;lt;/tt&amp;gt; or another custom page then the plugin should redirect to the wantsurl.&lt;br /&gt;
&lt;br /&gt;
Note that in this scenario above &amp;lt;tt&amp;gt;user_login($username, $password)&amp;lt;/tt&amp;gt; is never called and should probably return false.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
Authentication plugins exist from 1.9&lt;br /&gt;
==Example==&lt;br /&gt;
[http://moodle.org/plugins/view.php?plugin=auth_googleoauth2 Google / Facebook / Messenger Oauth2 Authentication plugin]&lt;br /&gt;
&lt;br /&gt;
==Template==&lt;br /&gt;
Please see Moodle &#039;&#039;&#039;none&#039;&#039;&#039; authentication plugin (auth/none), it&#039;s the perfect plugin template to start with.&lt;br /&gt;
&lt;br /&gt;
==Naming convention==&lt;br /&gt;
==File structure==&lt;br /&gt;
# Choose a name for your plugin.  We&#039;ll use &#039;sentry&#039; as an example below; change it to whatever name you have chosen.&lt;br /&gt;
# Under your Moodle installation root, create the directory &amp;lt;tt&amp;gt;/auth/sentry&amp;lt;/tt&amp;gt;.  It should be sibling to existing auth plugin directories: &#039;db&#039;, &#039;nologin&#039;, &#039;none&#039;, etc.&lt;br /&gt;
# Create the file &amp;lt;tt&amp;gt;/auth/sentry/auth.php&amp;lt;/tt&amp;gt;.  Within the file, create a class &amp;lt;tt&amp;gt;auth_plugin_sentry&amp;lt;/tt&amp;gt; that extends &amp;lt;tt&amp;gt;auth_plugin_base&amp;lt;/tt&amp;gt; from &amp;lt;tt&amp;gt;/lib/authlib.php&amp;lt;/tt&amp;gt;.  (You will need to &amp;lt;code&amp;gt;require_once&amp;lt;/code&amp;gt; the authlib file.)&lt;br /&gt;
# Implement the &amp;lt;tt&amp;gt;user_login()&amp;lt;/tt&amp;gt; function in your &amp;lt;tt&amp;gt;auth.php&amp;lt;/tt&amp;gt; file, and create or override additional functions based on your plugin&#039;s requirements.&lt;br /&gt;
# Log in to your Moodle installation as a site administrator and find, in the site administrator block, the page &amp;quot;Users -&amp;gt; Authentication -&amp;gt; Manage authentication&amp;quot;.  You will see your plugin in the list, appearing as &amp;lt;nowiki&amp;gt;[[auth_sentrytitle]]&amp;lt;/nowiki&amp;gt;.  You can enable it and move it up and down in the order.  &#039;&#039;&#039;At this point, with the plugin enabled, your plugin is registered and will be used by Moodle in its authentication process.&#039;&#039;&#039;&lt;br /&gt;
# If you don&#039;t like seeing &amp;lt;nowiki&amp;gt;[[auth_sentrytitle]]&amp;lt;/nowiki&amp;gt; as the name of your plugin in the Moodle UI, you&#039;ll need to create language files for your plugin.  Do this by creating the directory &amp;lt;tt&amp;gt;/auth/sentry/lang&amp;lt;/tt&amp;gt;, and under it, a directory for each language that your installation needs to support.  (Example: &amp;lt;tt&amp;gt;/auth/sentry/lang/en&amp;lt;/tt&amp;gt;.)  Within each of these language directories, create a file called &amp;lt;tt&amp;gt;auth_sentry.php&amp;lt;/tt&amp;gt;.  That file should set the desired value for &amp;lt;code&amp;gt;$string[&#039;auth_sentrytitle&#039;] for that language (use $string[&#039;pluginname&#039;] for Moodle2)&amp;lt;/code&amp;gt;.  You can also set the plugin description by setting &amp;lt;code&amp;gt;$string[&#039;auth_sentrydescription&#039;]&amp;lt;/code&amp;gt;, and you can also assign other translatable strings that your plugin uses, in these files.  &#039;&#039;&#039;Note:&#039;&#039;&#039; A folder named like &#039;&#039;&#039;en&#039;&#039;&#039; may not work for everyone. Please refer to the lang folder under your moodle installation directory to get an idea about the language/locale codes used in your moodle installation. For example, the lang folder in own users system contained folders named &#039;&#039;&#039;en&#039;&#039;&#039; and &#039;&#039;&#039;zh-cn&#039;&#039;&#039;. He emulated the same in the lang folder of the auth plugin and the plugin picked up the title and description strings immediately. ([[Places_to_search_for_lang_strings|More info on how Moodle handles language]]).&lt;br /&gt;
# If you want to configure your plugin through the Moodle UI, implement &amp;lt;tt&amp;gt;config_form()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;process_config()&amp;lt;/tt&amp;gt; in the plugin class.  You might find it convenient to use the &#039;db&#039; plugin as a model for this.  The plugin&#039;s config settings can then be managed through the Manage authentication page by clicking on the Settings link for that plugin, and the values will be stored in the &amp;lt;tt&amp;gt;mdl_config_plugins&amp;lt;/tt&amp;gt; table in the database.&lt;br /&gt;
&lt;br /&gt;
==Interfacing to API&#039;s==&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;authenticate_user_login()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
That&#039;s the main outline, but a lot of interesting stuff happens in &amp;lt;tt&amp;gt;authenticate_user_login()&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
# It gets a list of enabled authentication plugins.&lt;br /&gt;
# It looks up the username in the mdl_user table to see if they are allowed to log in, and which authentication plugin handles their login requests. (This will be the plugin that handled their first-ever login request.)&lt;br /&gt;
# It creates a user object, which will contain the data from &amp;lt;tt&amp;gt;mdl_user&amp;lt;/tt&amp;gt; if the username is known; if not, it will be an empty object.&lt;br /&gt;
# It does the following with the authentication plugin (note that for a username unknown to Moodle, it will do these steps for each authenticated plugin until one succeeds or it has tried them all):&lt;br /&gt;
## Calls the &amp;lt;tt&amp;gt;user_login()&amp;lt;/tt&amp;gt; function provided by that plugin, which returns a boolean value based on whether the credentials authenticate or not. If the result is false (not authenticated), skips the rest of the steps below and continues to the next plugin.&lt;br /&gt;
## If the plugin authenticates against an external system (not Moodle&#039;s user database), its &amp;lt;tt&amp;gt;update_user_record()&amp;lt;/tt&amp;gt; function is called to get the user&#039;s name, contact info, etc.&lt;br /&gt;
## Creates the Moodle user record if it doesn&#039;t already exist.&lt;br /&gt;
## Calls the plugin&#039;s &amp;lt;tt&amp;gt;sync_roles()&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
## Notifies each enabled authentication plugin that the user successfully authenticated, by calling each one&#039;s &amp;lt;tt&amp;gt;user_authenticated_hook()&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
# It returns the user object if everything was successful, or false if no plugin was able to successfully authenticate the credentials.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_login($username, $password)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
This must be rewritten by plugin to return boolean value, returns true if the username and password work and false if they are wrong or don&#039;t exist.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_change_password()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if this authentication plugin can change users&#039; password.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : false&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;change_password_url()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns the URL for changing the users&#039; passwords, or empty if the default URL can be used.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_edit_profile()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if this authentication plugin can edit the users&#039; profile.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : true&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;edit_profile_url()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns the URL for editing users&#039; profile, or empty if the defaults URL can be used.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;is_internal()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if this authentication plugin is &amp;quot;internal&amp;quot;.  Internal plugins use password hashes from Moodle user table for authentication.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : true&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;prevent_local_passwords()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Indicates if password hashes should be stored in local moodle database.  This function automatically returns the opposite boolean of what is_internal() returns.  Returning true means MD5 password hashes will be stored in the user table.  Returning false means flag &#039;not_cached&#039; will be stored there instead.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : !$this-&amp;gt;is_internal()&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;is_synchronised_with_external()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Indicates if moodle should automatically update internal user records with data from external sources using the information from get_userinfo() method.  This function automatically returns the opposite boolean of what is_internal() returns.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : !$this-&amp;gt;is_internal()&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_update_password($user, $newpassword)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Update the user&#039;s password.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_update($olduser, $newuser)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Called when the user record is updated. It will modify the user information in external database.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_delete($olduser)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
User delete requested. Internal user record had been deleted.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_reset_password()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if plugin allows resetting of internal password.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : false&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_signup()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if plugin allows resetting of internal password.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : false&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_signup($user, $notify=true)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Sign up a new user ready for confirmation, password is passed in plaintext.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_confirm()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns true if plugin allows confirming of new users.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Return type&#039;&#039;&#039; : boolean&lt;br /&gt;
; &#039;&#039;&#039;Default return value&#039;&#039;&#039; : false&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_confirm($username, $confirmsecret)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Confirm the new user as registered.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_exists($username)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Checks if user exists in external db.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;password_expire($username)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Returns number of days to user password expires.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;sync_roles()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Sync roles for this user - usually creator&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;get_userinfo($username)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Read user information from external database and returns it as array.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;config_form($config, $err, $user_fields)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Prints a form for configuring this authentication plugin. It&#039;s called from admin/auth.php, and outputs a full page with a form for configuring this plugin.  &lt;br /&gt;
This function has been deprecated in Moodle 3.3.  You should use the [[Admin_settings]] API instead.  For more information see [https://tracker.moodle.org/browse/MDL-12689]&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;validate_form($form, $err)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Validate form data.&lt;br /&gt;
This function has been deprecated in Moodle 3.3.  You should use the [[Admin_settings]] API instead.  For more information see [https://tracker.moodle.org/browse/MDL-12689]&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;process_config($config)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Processes and stores configuration data for this authentication plugin.&lt;br /&gt;
This function has been deprecated in Moodle 3.3.  You should use the [[Admin_settings]] API instead.  For more information see [https://tracker.moodle.org/browse/MDL-12689]&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;loginpage_hook()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Hook for overriding behaviour of login page.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;pre_loginpage_hook()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Hook for overriding behaviour of prior to redirecting to the login page, eg redirecting to an external login url for SAML or OpenID authentication. If you implement this you should also implement loginpage_hook as the user may go directly to the login page.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;user_authenticated_hook($user, $username, $password)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Post authentication hook. This method is called from authenticate_user_login() for all enabled auth plugins.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;prelogout_hook()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Pre logout hook.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;postlogout_hook()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
This method replace the prelogout_hook method to avoid authentication plugins redirects before the user logout event being triggered. &lt;br /&gt;
At the moment the only authentication plugin using this method is CAS (SSO).&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;logoutpage_hook()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Hook for overriding behaviour of logout page.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;can_be_manually_set()&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
This function was introduced in the base class and returns false by default. If overriden by an authentication plugin to return true, the authentication plugin will be able to be manually set for users. For example, when bulk uploading users you will be able to select it as the authentication method they use.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt; loginpage_idp_list() &amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Override this method and return a list of Identification Providers (IDPs) that your authentication plugin supports. An array of associative arrays containing url, icon and name for the IDP. These will be displayed on the login page and in the login block.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;pre_user_login_hook(&amp;amp;$user)&amp;lt;/tt&amp;gt;===&lt;br /&gt;
This method is called from authenticate_user_login() right after the user object is generated. This gives the auth plugin an option to make modification to the user object before the verification process starts.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;test_settings()&amp;lt;/tt&amp;gt;===&lt;br /&gt;
(From 3.3 onwards) If this method exists, the &amp;quot;manage authentication plugins&amp;quot; page will show a &amp;quot;test settings&amp;quot; link. The method should output notifications to let the user know whether the settings are correct, and what to do to rectify them if not.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Authentication API]]&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=102070 Overview of entire authentication code flow] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/view.php?id=42 User authentication forum]&lt;br /&gt;
* Moodle Docs [[:en:Manage authentication|Manage authentication]]&lt;br /&gt;
* [[:en:Authentication FAQ|Authentication FAQ]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[fr:Méthodes_d&#039;authentification]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=File_Converters&amp;diff=52322</id>
		<title>File Converters</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=File_Converters&amp;diff=52322"/>
		<updated>2017-05-09T14:14:00Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 3.3}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Users submit a wide range of files, and it is a common requirement to convert these to alternative formats.&lt;br /&gt;
The biggest example of where this currently happens in core is in the &amp;lt;tt&amp;gt;assignfeedback_editpdf&amp;lt;/tt&amp;gt; plugin which allows for conversion from a variety of document types into PDF to facilitate annotation.&lt;br /&gt;
&lt;br /&gt;
The file converters distributed with Moodle currently are:&lt;br /&gt;
* Unoconv&lt;br /&gt;
* Google Drive&lt;br /&gt;
&lt;br /&gt;
The file converter API deals allows for conversion via multiple plugins and will automatically fallback to another suitable plugin upon failure.&lt;br /&gt;
&lt;br /&gt;
The API is designed to be called asynchronously as many cloud services offering document conversion offer an asynchronous API themselves.&lt;br /&gt;
&lt;br /&gt;
=Using the file converter API=&lt;br /&gt;
&lt;br /&gt;
A file conversion is performed by the &amp;lt;tt&amp;gt;core_files\converter&amp;lt;/tt&amp;gt; API and a single conversion is represented by the &amp;lt;tt&amp;gt;core\files\conversion&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
Individual file conversions should always be accessed by the &amp;lt;tt&amp;gt;core_files\converter&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
A file conversion is fetched or created by calling the &amp;lt;tt&amp;gt;start_conversion&amp;lt;/tt&amp;gt; function on the converter API and passing in an existing &amp;lt;tt&amp;gt;stored_file&amp;lt;/tt&amp;gt; record, along with the target format.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
$converter = new \core_files\converter();&lt;br /&gt;
$conversion = $converter-&amp;gt;start_conversion($file, &#039;pdf&#039;);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an existing file conversion matching the file and target format exists, the conversion record for this file will be returned, otherwise a new conversion is created and returned.&lt;br /&gt;
&lt;br /&gt;
To force a fresh conversion, a third boolean parameter can be passed, though this should not normally be necessary.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
$converter = new \core_files\converter();&lt;br /&gt;
$conversion = $converter-&amp;gt;start_conversion($file, &#039;pdf&#039;, true);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Polling for updates on an existing conversion==&lt;br /&gt;
When the &amp;lt;tt&amp;gt;start_conversion&amp;lt;/tt&amp;gt; function is called, it automatically polls for any update on the conversion so it should not normally be necessary to poll the status separately.&lt;br /&gt;
&lt;br /&gt;
It is however possible to do so:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
$converter = new \core_files\converter();&lt;br /&gt;
$conversion = $converter-&amp;gt;start_conversion($file, &#039;pdf&#039;);&lt;br /&gt;
$converter-&amp;gt;poll_conversion($conversion);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Checking status of a conversion==&lt;br /&gt;
File conversions can have one of four states:&lt;br /&gt;
* &amp;lt;tt&amp;gt;STATUS_PENDING&amp;lt;/tt&amp;gt; - The conversion has not yet started;&lt;br /&gt;
* &amp;lt;tt&amp;gt;STATUS_IN_PROGRESS&amp;lt;/tt&amp;gt; - A conversion has been picked up by a file converter and is currently in progress;&lt;br /&gt;
* &amp;lt;tt&amp;gt;STATUS_COMPLETE&amp;lt;/tt&amp;gt; - The conversion was successful and the converted file is available; and&lt;br /&gt;
* &amp;lt;tt&amp;gt;STATUS_FAILED&amp;lt;/tt&amp;gt; - All attempts to convert the file have failed.&lt;br /&gt;
&lt;br /&gt;
The conversion API provides a way to check the status of the conversion with the &amp;lt;tt&amp;gt;$conversion-&amp;gt;get_status()&amp;lt;/tt&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    $converter = new \core_files\converter();&lt;br /&gt;
    $conversion = $converter-&amp;gt;start_conversion($file, &#039;pdf&#039;);&lt;br /&gt;
&lt;br /&gt;
    switch ($conversion-&amp;gt;get_status()) {&lt;br /&gt;
        case \core_files\conversion::STATUS_COMPLETE:&lt;br /&gt;
            // The file is ready. Do something with it.&lt;br /&gt;
        case \core_files\conversion::STATUS_PENDING:&lt;br /&gt;
        case \core_files\conversion::STATUS_IN_PROGRESS:&lt;br /&gt;
            // Conversion still ongoing. Display spinner to the user.&lt;br /&gt;
        case \core_files\conversion::STATUS_FAILED:&lt;br /&gt;
            // Permanent failure - handle to the user.&lt;br /&gt;
    }&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fetching the target file==&lt;br /&gt;
Following a conversion, the target file is stored as a &amp;lt;tt&amp;gt;stored_file&amp;lt;/tt&amp;gt; record and can be fetched for consumption elsewhere in your API:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
if ($conversion-&amp;gt;get_status() === \core_files\conversion::STATUS_COMPLETE) {&lt;br /&gt;
    $file = $conversion-&amp;gt;get_destfile();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Developing file conversion plugins=&lt;br /&gt;
&lt;br /&gt;
==Plugin structure==&lt;br /&gt;
&lt;br /&gt;
File conversion plugins are located in the &#039;&#039;&#039;files/converter&#039;&#039;&#039; directory and follow the standard structure of a Moodle Plugin.&lt;br /&gt;
&lt;br /&gt;
They must provide a &amp;lt;tt&amp;gt;converter&amp;lt;/tt&amp;gt; plugin which implements the &amp;lt;tt&amp;gt;\core_files\converter_interface&amp;lt;/tt&amp;gt; interface and be named as &amp;lt;tt&amp;gt;fileconverter_PLUGINNAME\converter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The following notes apply:&lt;br /&gt;
* &amp;lt;tt&amp;gt;are_requirements_met&amp;lt;/tt&amp;gt; should return  a status of whether the &#039;&#039;system&#039;&#039; requirements are met. For example, whether appropriate API keys are present. It should be lightweight to call and cache where required.&lt;br /&gt;
* &amp;lt;tt&amp;gt;start_document_conversion&amp;lt;/tt&amp;gt; should kick off the conversion, whilst &amp;lt;tt&amp;gt;poll_conversion_status&amp;lt;/tt&amp;gt; should poll for any status update:&lt;br /&gt;
** If any failures occur, it should set the conversion status to &amp;lt;tt&amp;gt;conversion::STATUS_FAILED&amp;lt;/tt&amp;gt; and immediately return.  There is no need to update the &amp;lt;tt&amp;gt;$conversion&amp;lt;/tt&amp;gt; record in this situation.&lt;br /&gt;
** When the conversion process starts, the status should be set to &amp;lt;tt&amp;gt;conversion::STATUS_IN_PROGRESS&amp;lt;/tt&amp;gt; and the record &#039;&#039;&#039;must&#039;&#039;&#039; be updated. This ensures that, should the process take a long time, the current status is accurately reflected.&lt;br /&gt;
** Upon successful completion, the status should be updated to &amp;lt;tt&amp;gt;conversion::STATUS_COMPLETE&amp;lt;/tt&amp;gt; and the newly created &amp;lt;tt&amp;gt;stored_file&amp;lt;/tt&amp;gt; should be stored against the conversion using either the &amp;lt;tt&amp;gt;store_destfile_from_string&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;store_destfile_from_path&amp;lt;/tt&amp;gt; function as appropriate.&lt;br /&gt;
* The &amp;lt;tt&amp;gt;supports&amp;lt;/tt&amp;gt; function should return whether the supported source and target conversion is possible.&lt;br /&gt;
* The &amp;lt;tt&amp;gt;get_supported_conversion&amp;lt;/tt&amp;gt; function is used for informational display to administrators only.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Talk:Communication_Between_Components&amp;diff=51814</id>
		<title>Talk:Communication Between Components</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Talk:Communication_Between_Components&amp;diff=51814"/>
		<updated>2017-01-31T09:21:10Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The diagram in https://docs.moodle.org/dev/Communication_Between_Components#General_rules_for_inter-component_communications seems to imply that subplugins can&#039;t communicate with each other regardless of whether they declare a dependency, which I don&#039;t think is the case. I wonder if it would be worth clarifying this by adding a third subplugin which does declare a dependency?&lt;br /&gt;
&lt;br /&gt;
Yes sub-plugins can communicate but only if they have an explicit dependency (version.php). I went to update the diagram - but it would have made it too cluttered and less clear so I&#039;ll just add a note about it underneath.&lt;br /&gt;
&lt;br /&gt;
Thanks, Damyon. That&#039;s made it a lot clearer.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Exporter&amp;diff=51798</id>
		<title>Exporter</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Exporter&amp;diff=51798"/>
		<updated>2017-01-30T10:19:50Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 3.3}}Moodle exporters are classes which receive data and serialise it to a simple pre-defined structure. They ensure that the format of the data exported is uniform and easily maintainable. They are also used to generate the signature (parameters and return values) of [[External functions API|external functions]].&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
When dealing with external functions (Ajax, web services, ...) and rendering methods we often hit a situation where the same object is used, and exported, from multiple places. When that situation arises, the code that serialises our objects gets duplicated, or becomes inconsistent.&lt;br /&gt;
&lt;br /&gt;
We made the exporters to remedy to this situation. An exporter clearly defines what data it exports, and contains the logic which transform the incoming data into the exported data. As it knows everything, it can generate the structure required by external functions automatically.&lt;br /&gt;
&lt;br /&gt;
This means that not only developers have less code to maintain, but they also have a more robust structure which can easily evolve with their needs. If a new property needs to be exported, it is simply added to the exporter class, and automatically all usage of the exporter inherits this added property.&lt;br /&gt;
&lt;br /&gt;
== Defining properties ==&lt;br /&gt;
&lt;br /&gt;
The method &#039;&#039;define_properties()&#039;&#039; returns a list of the properties expected when instantiating your exporter, and the ones it will export at the same time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Return the list of properties.&lt;br /&gt;
 *&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected static function define_properties() {&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;id&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_INT&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;username&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT&lt;br /&gt;
        ),&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These properties will allow us to generate a &#039;&#039;create&#039;&#039; or &#039;&#039;update&#039;&#039; structure for external functions, [[#In_external_functions|more on this later]]. Oh, and if you are using persistent you do not need to do this: [[#Exporters_and_persistent|check this out]].&lt;br /&gt;
&lt;br /&gt;
=== Property attributes ===&lt;br /&gt;
&lt;br /&gt;
Each property is configured using the following attributes:&lt;br /&gt;
&lt;br /&gt;
;type&lt;br /&gt;
: The only mandatory attribute. It must either be one of the many PARAM_* constants, or an array of properties.&lt;br /&gt;
;default&lt;br /&gt;
: The default value when the value was not provided. When not specified, a value is required.&lt;br /&gt;
;null&lt;br /&gt;
: Either of the constants NULL_ALLOWED or NULL_NOT_ALLOWED telling if the null value is accepted. This defaults to NULL_NOT_ALLOWED.&lt;br /&gt;
;optional&lt;br /&gt;
: Whether the property can be omitted completely. Defaults to false.&lt;br /&gt;
;multiple&lt;br /&gt;
: Whether there will be more one or more entries under this property. Defaults to false.&lt;br /&gt;
&lt;br /&gt;
Although this is not a &#039;&#039;rule&#039;&#039;, it is recommended that the &#039;&#039;standard properties&#039;&#039; (by opposition to [[#Additional_properties|additional properties]]) only use the &#039;&#039;type&#039;&#039; [[#Property_attributes|attribute]], and only with &#039;&#039;PARAM_*&#039;&#039; constants.&lt;br /&gt;
&lt;br /&gt;
== Using an exporter ==&lt;br /&gt;
&lt;br /&gt;
Once we&#039;ve got a [[#Minimalist|minimalist exporter]] set-up, here is how to use it.&lt;br /&gt;
&lt;br /&gt;
=== Exporting the data ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$data = (object) [&#039;id&#039; =&amp;gt; 123, &#039;username&#039; =&amp;gt; &#039;batman&#039;];&lt;br /&gt;
&lt;br /&gt;
// The only time we can give data to our exporter is when instantiating it.&lt;br /&gt;
$ue = new user_exporter($data);&lt;br /&gt;
&lt;br /&gt;
// To export, we must pass the reference to a renderer.&lt;br /&gt;
$data = $ue-&amp;gt;export($OUTPUT);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we print the content of &#039;&#039;$data&#039;&#039;, we will obtain this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
stdClass Object&lt;br /&gt;
(&lt;br /&gt;
    [id] =&amp;gt; 123&lt;br /&gt;
    [username] =&amp;gt; batman&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, I agree that this is not quite impressive. But wait until you read about [[#Abiding_to_text_formatting_rules|automatically formatting text]], and [[#In_external_functions|usage in external functions]].&lt;br /&gt;
&lt;br /&gt;
=== In external functions ===&lt;br /&gt;
&lt;br /&gt;
Let&#039;s imagine that we have an external function &#039;&#039;get_users&#039;&#039; which returns a list of users. For now we only want to export the user ID and their user name, so we&#039;ll use our exporter. Let&#039;s ask our exporter to create the external structure for us:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_users_returns() {&lt;br /&gt;
    return external_multiple_structure(&lt;br /&gt;
        user_exporter::get_read_structure()&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that this is done, we must use our exporter to export our users&#039; data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function get_users() {&lt;br /&gt;
    global $DB, $PAGE;&lt;br /&gt;
    $output = $PAGE-&amp;gt;get_renderer(&#039;core&#039;);&lt;br /&gt;
    $users = $DB-&amp;gt;get_records(&#039;user&#039;, null, &#039;&#039;, &#039;id, username&#039;, 0, 10); // Get 10 users.&lt;br /&gt;
    $result = [];&lt;br /&gt;
    foreach ($users as $userdata) {&lt;br /&gt;
        $exporter = new user_exporter($userdata);&lt;br /&gt;
        $result[] = $exporter-&amp;gt;export($output);&lt;br /&gt;
    }&lt;br /&gt;
    return $result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Lastly, if you had another external function to create users, you could use the exporter to get the structure of the incoming data. That helps if you want your external functions to require more information to create your &#039;&#039;users&#039;&#039; as your needs grow. The following indicates that the external function requires the field &#039;username&#039; to be passed in the key &#039;user&#039;. The &#039;&#039;create&#039;&#039; and &#039;&#039;update&#039;&#039; structures include all of the &#039;&#039;standard&#039;&#039; properties, none of the &#039;&#039;[[#Additional_properties|other]]&#039;&#039; ones. Note that the &#039;&#039;create&#039;&#039; structure does not include the &#039;&#039;id&#039;&#039; property. Use &#039;&#039; user_exporter::get_update_structure()&#039;&#039; if to update a user and thus receive the ID.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function create_user_parameters() {&lt;br /&gt;
    return new external_function_parameters([&lt;br /&gt;
        &#039;user&#039; =&amp;gt; user_exporter::get_create_structure()&lt;br /&gt;
    ]);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static function create_user($user) {&lt;br /&gt;
    // Mandatory parameters validation.&lt;br /&gt;
    $params = self::validate_parameters(self::create_user_parameters(), [&#039;user&#039; =&amp;gt; $user]);&lt;br /&gt;
    $user = $params[&#039;user&#039;];&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Important note: when used in the &#039;&#039;parameters&#039;&#039;, the exporter&#039;s structure must always be included under another key, above we used &#039;&#039;user&#039;&#039;. Else this would not be flexible, and may not generate a valid structure for some webservice protocols.&lt;br /&gt;
&lt;br /&gt;
== Abiding to text formatting rules ==&lt;br /&gt;
&lt;br /&gt;
If we had to pass the &#039;&#039;$OUTPUT&#039;&#039; during export as seen previously, that is because we are handling the text formatting automatically for you. Remember the functions &#039;&#039;format_text()&#039;&#039; and &#039;&#039;format_string()&#039;&#039;? They are used to apply [[Filters|filters]] on the content typically submitted by users, but also to convert it from a few given formats to HTML.&lt;br /&gt;
&lt;br /&gt;
Upon export, the exporter looks at the &#039;&#039;type&#039;&#039; of all your properties. When it finds a property of type &#039;&#039;PARAM_TEXT&#039;&#039;, it will use &#039;&#039;format_string()&#039;&#039;. However, if it finds a property using &#039;&#039;PARAM_RAW&#039;&#039; and there is another property of the same name but ending with &#039;&#039;format&#039;&#039; it will use &#039;&#039;format_text()&#039;&#039;. If you are not familiar with the rules for outputting text in Moodle see [[Output functions]] and [[lib/formslib.php_Form_Definition#Most_Commonly_Used_PARAM_.2A_Types| Most Commonly Used PARAM_* Types]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&#039;description&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
),&lt;br /&gt;
&#039;descriptionformat&#039; =&amp;gt; array(&lt;br /&gt;
    &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
),&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With the two above properties added, let&#039;s see what happens when the user&#039;s description is in the Markdown format and we export it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$data = (object) [&lt;br /&gt;
    &#039;id&#039; =&amp;gt; 123,&lt;br /&gt;
    &#039;username&#039; =&amp;gt; &#039;batman&#039;,&lt;br /&gt;
    &#039;description&#039; =&amp;gt; &#039;Hello __world__!&#039;,&lt;br /&gt;
    &#039;descriptionformat&#039; =&amp;gt; FORMAT_MARKDOWN&lt;br /&gt;
];&lt;br /&gt;
$ue = new user_exporter($data, [&#039;context&#039; =&amp;gt; context_user::instance(123)]);&lt;br /&gt;
$data = $ue-&amp;gt;export($OUTPUT);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unsurprisingly, this is what comes out of it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
stdClass Object&lt;br /&gt;
(&lt;br /&gt;
    [id] =&amp;gt; 123&lt;br /&gt;
    [username] =&amp;gt; batman&lt;br /&gt;
    [description] =&amp;gt; &amp;lt;p&amp;gt;Hello &amp;lt;strong&amp;gt;world&amp;lt;/strong&amp;gt;!&amp;lt;/p&amp;gt;&lt;br /&gt;
    [descriptionformat] =&amp;gt; 1   // Corresponds to FORMAT_HTML.&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Psst... we&#039;ve cheating a bit above. Did you notice that we&#039;ve passed a context to the exporter? We passed a [[#Related_objects|related object]], more on them later.&lt;br /&gt;
&lt;br /&gt;
=== Formatting parameters ===&lt;br /&gt;
&lt;br /&gt;
Formatting requires a context. If you&#039;ve defined a [[#Related_objects|related object]] with the name &#039;&#039;context&#039;&#039; we will automatically use this one. But there are cases where you need multiple contexts, or you need to rewrite the pluginfile URLs. When that is the case, you will define a method called &#039;get_format_parameters_for_&#039; followed with the name of the property. The latter also accepts the various options which both &#039;&#039;format_text&#039;&#039; and &#039;&#039;format_string&#039;&#039; support.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the formatting parameters for the description.&lt;br /&gt;
 *&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected function get_format_parameters_for_description() {&lt;br /&gt;
    return [&lt;br /&gt;
        &#039;component&#039; =&amp;gt; &#039;core_user&#039;,&lt;br /&gt;
        &#039;filearea&#039; =&amp;gt; &#039;description&#039;,&lt;br /&gt;
        &#039;itemid&#039; =&amp;gt; $this-&amp;gt;data-&amp;gt;id&lt;br /&gt;
    ];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a complete list of options, refer to the PHP documentation of the method [https://github.com/moodle/moodle/blob/master/lib/classes/external/exporter.php core\external\exporter::get_format_parameters()].&lt;br /&gt;
&lt;br /&gt;
Note that when you return a context from the above method, it is advised that you get it from the related objects. Doing so will prevent contexts from being loaded from the database from within the exporter and cause unwanted performance issues. The &#039;&#039;system context&#039;&#039; is an exception is this.&lt;br /&gt;
&lt;br /&gt;
== Additional properties ==&lt;br /&gt;
&lt;br /&gt;
The list of &#039;&#039;other&#039;&#039; properties are to be seen as &#039;&#039;additional&#039;&#039; properties which do not need to be given to the exporter for it to export them. They are dynamically generated from the data provided (and the [[#Related objects|related objects]], more on that later). For example, if we wanted our exporter to provide the URL to a user&#039;s profile, we wouldn&#039;t need the developer to pass it beforehand, we could generate it based on the ID which was already provided.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Other&#039;&#039; properties are only included in the &#039;&#039;read&#039;&#039; structure of an object (&#039;&#039;get_read_structure&#039;&#039; and &#039;&#039;read_properties_definition&#039;&#039;) as they are dynamically generated. They are not required, nor needed, to &#039;&#039;create&#039;&#039; or &#039;&#039;update&#039;&#039; an object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Return the list of additional properties.&lt;br /&gt;
&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected static function define_other_properties() {&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;profileurl&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; PARAM_URL&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;statuses&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;type&#039; =&amp;gt; status_exporter::read_properties_definition(),&lt;br /&gt;
            &#039;multiple&#039; =&amp;gt; true,&lt;br /&gt;
            &#039;optional&#039; =&amp;gt; true&lt;br /&gt;
        ),&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The snippet above defines that we will always export a URL under the property &#039;&#039;profileurl&#039;&#039;, and that we will either export a list of &#039;&#039;status_exporters&#039;&#039;, or not. As you can see, the &#039;&#039;type&#039;&#039; can use the &#039;&#039;read&#039;&#039; properties of another exporter which allows exporters to be nested.&lt;br /&gt;
&lt;br /&gt;
If you have defined &#039;&#039;other&#039;&#039; properties, then you must also add the logic to export them. This is done by adding the method &#039;&#039;get_other_values(renderer_base $output)&#039;&#039; to your exporter. Here is an example in which we ignored the &#039;&#039;statuses&#039;&#039; as they are optional.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Get the additional values to inject while exporting.&lt;br /&gt;
 *&lt;br /&gt;
 * @param renderer_base $output The renderer.&lt;br /&gt;
 * @return array Keys are the property names, values are their values.&lt;br /&gt;
 */&lt;br /&gt;
protected function get_other_values(renderer_base $output) {&lt;br /&gt;
    $profileurl = new moodle_url(&#039;/user/profile.php&#039;, [&#039;id&#039; =&amp;gt; $this-&amp;gt;data-&amp;gt;id]);&lt;br /&gt;
    return [&lt;br /&gt;
        &#039;profileurl&#039; =&amp;gt; $profileurl-&amp;gt;out(false)&lt;br /&gt;
    ];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Important note: &#039;&#039;additional properties&#039;&#039; cannot override &#039;&#039;standard&#039;&#039; properties, so make sure the names do not conflict.&lt;br /&gt;
&lt;br /&gt;
== Related objects ==&lt;br /&gt;
&lt;br /&gt;
There are times we need more information inside the exporter in order to export it. That may be as simple as the &#039;&#039;context&#039;&#039;, but it can also be other objects to be used when exporting &#039;&#039;other&#039;&#039; properties. As much as possible you will want to avoid calling APIs, or querying the database upon export. Exporters can be used in loops and thus subsequent queries could dramatically affect performance, hence the need for related objects.&lt;br /&gt;
&lt;br /&gt;
Related objects need to be defined within the exporter, that is to ensure that they are always provided and are of the right type. In some occasions they can be marked as optional, generally when the related object does not exist. For instance, if we had an issue exporter, an optional related object could be the peer reviewer, as we don&#039;t always have a peer reviewer.&lt;br /&gt;
&lt;br /&gt;
Use the method &#039;&#039;protected static define_related()&#039;&#039; as follows. The keys are an arbitrary name for the related object, and the values are the fully qualified name of the class. The name of the class can be followed by &#039;&#039;[]&#039;&#039; and/or &#039;&#039;?&#039;&#039; to respectively indicate a list of these objects, and an optional related.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Returns a list of objects that are related.&lt;br /&gt;
 *&lt;br /&gt;
 * @return array&lt;br /&gt;
 */&lt;br /&gt;
protected static function define_related() {&lt;br /&gt;
    return array(&lt;br /&gt;
        &#039;context&#039; =&amp;gt; &#039;context&#039;,                     // Must be an instance of context.&lt;br /&gt;
        &#039;statuses&#039; =&amp;gt; &#039;some\\namespace\\status[]&#039;,  // Must be an array of status instances.&lt;br /&gt;
        &#039;mother&#039; =&amp;gt; &#039;family\\mother?&#039;,              // Can be a mother instance, or null.&lt;br /&gt;
        &#039;brothers&#039; =&amp;gt; &#039;family\\brother[]?&#039;,         // Can be null, or an array of brother instances.&lt;br /&gt;
    );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We give the related objects to the &#039;&#039;exporter&#039;&#039; when we instantiate it, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$data = (object) [&#039;id&#039; =&amp;gt; 123, &#039;username&#039; =&amp;gt; &#039;batman&#039;];&lt;br /&gt;
$relateds = [&lt;br /&gt;
    &#039;context&#039; =&amp;gt; context_system::instance(),&lt;br /&gt;
    &#039;statuses&#039; =&amp;gt; [&lt;br /&gt;
        new some\namespace\status(&#039;Hello&#039;),&lt;br /&gt;
        new some\namespace\status(&#039;World!&#039;),&lt;br /&gt;
    ],&lt;br /&gt;
    &#039;mother&#039; =&amp;gt; null,&lt;br /&gt;
    &#039;brothers&#039; =&amp;gt; null&lt;br /&gt;
];&lt;br /&gt;
$ue = new user_exporter($data, $relateds);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that optional relateds must still be provided but as &#039;&#039;null&#039;&#039;. Related objects are accessible from within the exporter using &#039;&#039;$this-&amp;gt;related[&#039;nameOfRelated&#039;]&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== Exporters and persistent ==&lt;br /&gt;
&lt;br /&gt;
Great news, if you have a [[Persistent|persistent]] and you want to export them, all the work is pretty much done for you. All of the persistent&#039;s properties will automatically be added to your exporter if you extend the class &#039;&#039;core\external\persistent_exporter&#039;&#039;, and add the method &#039;&#039;define_class()&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class status_exporter extends core\external\persistent {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the specific class the persistent should be an instance of.&lt;br /&gt;
     *&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_class() {&lt;br /&gt;
        return \some\namespace\status::class; // PHP 5.5+ only&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
And if you wanted to add more to it, there is always the [[#Additional_properties|other properties]].&lt;br /&gt;
&lt;br /&gt;
== Common pitfalls ==&lt;br /&gt;
&lt;br /&gt;
# Exporters must not extend other exporters. They would become too unpredictable.&lt;br /&gt;
# Exporters do not validate your data. They use the properties&#039; attributes to generate the structure required by the [[External functions API]], the latter is responsible for using the structure to validate the data.&lt;br /&gt;
# When exporters are nested, the &#039;&#039;type&#039;&#039; to use should be &#039;&#039;other_exporter::read_properties_definition()&#039;&#039; and not &#039;&#039;other_exporter::get_read_structure()&#039;&#039;.&lt;br /&gt;
# Adding required relateds to an exporter once it has been released can cause &#039;&#039;coding_exceptions&#039;&#039;. Each instance will require the new related, which it will not have in 3rd party code/plugins. Preferably an exporter&#039;s relateds should never change, but if they must, consider this:&lt;br /&gt;
#* Creating a brand new exporter class, and leaving the other one unchanged.&lt;br /&gt;
#* Adding the new related, and assigning it a default value in your &#039;&#039;constructor&#039;&#039; if undefined. Also print a debugging message for developers to fix their code.&lt;br /&gt;
#** Note: Adding the related as &#039;&#039;optional&#039;&#039; does not work. Optional relateds still need to be passed to the constructor, but as &#039;&#039;null&#039;&#039;.&lt;br /&gt;
#* Adding the new related, and advertising that all of its usage must be updated.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
=== Minimalist ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class user_exporter extends core\external\exporter {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the list of properties.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;id&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;username&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
namespace example\external;&lt;br /&gt;
&lt;br /&gt;
use core\external\exporter;&lt;br /&gt;
use renderer_base;&lt;br /&gt;
use moodle_url;&lt;br /&gt;
&lt;br /&gt;
class user_exporter extends exporter {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the list of properties.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;id&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;username&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_ALPHANUMEXT&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;description&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_RAW,&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;descriptionformat&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_INT,&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the list of additional properties.&lt;br /&gt;
&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_other_properties() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;profileurl&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; PARAM_URL&lt;br /&gt;
            ),&lt;br /&gt;
            &#039;statuses&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;type&#039; =&amp;gt; status_exporter::read_properties_definition(),&lt;br /&gt;
                &#039;multiple&#039; =&amp;gt; true,&lt;br /&gt;
                &#039;optional&#039; =&amp;gt; true&lt;br /&gt;
            ),&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns a list of objects that are related.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected static function define_related() {&lt;br /&gt;
        return array(&lt;br /&gt;
            &#039;context&#039; =&amp;gt; &#039;context&#039;,&lt;br /&gt;
            &#039;statuses&#039; =&amp;gt; &#039;some\\namespace\\status[]&#039;,&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the formatting parameters for the description.&lt;br /&gt;
     *&lt;br /&gt;
     * @return array&lt;br /&gt;
     */&lt;br /&gt;
    protected function get_format_parameters_for_description() {&lt;br /&gt;
        return [&lt;br /&gt;
            &#039;component&#039; =&amp;gt; &#039;core_user&#039;,&lt;br /&gt;
            &#039;filearea&#039; =&amp;gt; &#039;description&#039;,&lt;br /&gt;
            &#039;itemid&#039; =&amp;gt; $this-&amp;gt;data-&amp;gt;id&lt;br /&gt;
        ];&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the additional values to inject while exporting.&lt;br /&gt;
     *&lt;br /&gt;
     * @param renderer_base $output The renderer.&lt;br /&gt;
     * @return array Keys are the property names, values are their values.&lt;br /&gt;
     */&lt;br /&gt;
    protected function get_other_values(renderer_base $output) {&lt;br /&gt;
        $statuses = [];&lt;br /&gt;
        foreach ($this-&amp;gt;related[&#039;statuses&#039;] as $status) {&lt;br /&gt;
            $exporter = new status_exporter($status);&lt;br /&gt;
            $statuses[] = $exporter-&amp;gt;export($output);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $profileurl = new moodle_url(&#039;/user/profile.php&#039;, [&#039;id&#039; =&amp;gt; $this-&amp;gt;data-&amp;gt;id]);&lt;br /&gt;
&lt;br /&gt;
        return [&lt;br /&gt;
            &#039;profileurl&#039; =&amp;gt; $profileurl-&amp;gt;out(false),&lt;br /&gt;
            &#039;statuses&#039; =&amp;gt; $statuses&lt;br /&gt;
        ];&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Talk:Communication_Between_Components&amp;diff=51797</id>
		<title>Talk:Communication Between Components</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Talk:Communication_Between_Components&amp;diff=51797"/>
		<updated>2017-01-30T10:13:17Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Clarify whether subplugins can communicate&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The diagram in https://docs.moodle.org/dev/Communication_Between_Components#General_rules_for_inter-component_communications seems to imply that subplugins can&#039;t communicate with each other regardless of whether they declare a dependency, which I don&#039;t think is the case. I wonder if it would be worth clarifying this by adding a third subplugin which does declare a dependency?&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Plugin_files&amp;diff=51135</id>
		<title>Plugin files</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Plugin_files&amp;diff=51135"/>
		<updated>2016-11-25T09:16:29Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Plugins development}}&lt;br /&gt;
&lt;br /&gt;
There are some files that work the same in all [[Plugin types|plugin types]] (if they are present).&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
Meta-data about the plugin (like the version number, dependencies or the maturity level) are defined here.&lt;br /&gt;
&lt;br /&gt;
* [[version.php]] page provides detailed description of the file contents&lt;br /&gt;
&lt;br /&gt;
=== lang/en/plugintype_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
English strings for the plugin are defined here. At least &amp;lt;tt&amp;gt;$string[&#039;pluginname&#039;]&amp;lt;/tt&amp;gt; must be present. There is an exception for the [[Activity modules]], their expected file name is just &amp;lt;tt&amp;gt;pluginname.php&amp;lt;/tt&amp;gt; (without the &amp;lt;tt&amp;gt;mod_&amp;lt;/tt&amp;gt; prefix).&lt;br /&gt;
&lt;br /&gt;
* [[String API]] provides information about the string files&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
The interface between the Moodle core and the plugin is defined here for the most plugin types. The expected contents of the file depends on the particular plugin type.&lt;br /&gt;
&lt;br /&gt;
Moodle core often (but not always) loads all the &amp;lt;tt&amp;gt;lib.php&amp;lt;/tt&amp;gt; files of the given plugin types. For the performance reasons, it is strongly recommended to keep this file as small as possible and have just required code implemented in it. All the plugin&#039;s internal logic should be implemented in the auto-loaded classes or in the &amp;lt;tt&amp;gt;locallib.php&amp;lt;/tt&amp;gt; file.&lt;br /&gt;
&lt;br /&gt;
* [[Coding style#Functions and Methods]]&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
The plugin&#039;s database scheme (tables, fields, indexes and keys) are defined here. You should always use the in-built XMLDB editor to generate this file.&lt;br /&gt;
&lt;br /&gt;
* [[XMLDB Documentation]]&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
Upgrade steps (such as database scheme changes and other things that must happen when the plugin is being upgraded) are defined here. The in-built [[XMLDB]] editor can be used to generate the code to change the database scheme.&lt;br /&gt;
&lt;br /&gt;
* [[Upgrade API]]&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
Plugin capabilities are defined here.&lt;br /&gt;
&lt;br /&gt;
* [[Access_API]]&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
Allows you to execute a PHP code right after the plugin&#039;s database scheme has been installed.&lt;br /&gt;
&lt;br /&gt;
=== db/uninstall.php ===&lt;br /&gt;
&lt;br /&gt;
Allows you to execute a PHP code before the plugin&#039;s database tables and data are dropped during the plugin uninstallation.&lt;br /&gt;
&lt;br /&gt;
=== db/events.php ===&lt;br /&gt;
&lt;br /&gt;
Event handlers (subscriptions) are defined here. It lists all the events that your plugin want to observe and be notified about.&lt;br /&gt;
&lt;br /&gt;
* [[Events API]]&lt;br /&gt;
&lt;br /&gt;
=== db/messages.php ===&lt;br /&gt;
&lt;br /&gt;
Allow to declare your plugin as the messaging provider.&lt;br /&gt;
&lt;br /&gt;
* [[Message API]]&lt;br /&gt;
&lt;br /&gt;
=== db/services.php ===&lt;br /&gt;
&lt;br /&gt;
External functions and web services provided by your plugin are described here.&lt;br /&gt;
&lt;br /&gt;
* [[Adding a web service to a plugin]]&lt;br /&gt;
* [[Web services API]]&lt;br /&gt;
* [[External functions API]]&lt;br /&gt;
&lt;br /&gt;
=== db/renamedclasses.php ===&lt;br /&gt;
&lt;br /&gt;
Details of classes that have been renamed to fit in with autoloading. See [https://moodle.org/mod/forum/discuss.php?d=262403 forum discussion] for details&lt;br /&gt;
&lt;br /&gt;
=== classes/ ===&lt;br /&gt;
&lt;br /&gt;
* [[Automatic class loading]]&lt;br /&gt;
* [[Coding style#Namespaces]]&lt;br /&gt;
&lt;br /&gt;
=== cli/ ===&lt;br /&gt;
&lt;br /&gt;
It is a convention in the Moodle code that all [[CLI scripts]] are put into the &amp;lt;tt&amp;gt;cli/&amp;lt;/tt&amp;gt; folder inside the plugin directory. Make sure all these files declare themselves as CLI scripts properly (the constant must be defined prior to loading the config.php):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
define(&#039;CLI_SCRIPT&#039;, true);&lt;br /&gt;
require(__DIR__.&#039;/../../../config.php&#039;);&lt;br /&gt;
require_once($CFG-&amp;gt;libdir.&#039;/clilib.php&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
Describes the administration configuration for your plugin. &lt;br /&gt;
&lt;br /&gt;
The setting names are supposed to be &amp;lt;tt&amp;gt;plugintype_pluginname/settingname&amp;lt;/tt&amp;gt; (note the slash) and not plugintype_pluginname_settingname or even just settingname. This makes them to be stored in the &amp;lt;tt&amp;gt;config_plugins&amp;lt;/tt&amp;gt; table without polluting the global &amp;lt;tt&amp;gt;$CFG&amp;lt;/tt&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
* [[Admin settings]]&lt;br /&gt;
&lt;br /&gt;
=== amd/ ===&lt;br /&gt;
&lt;br /&gt;
Plugin&#039;s Javascript modules written using the Asynchronous Module Definition (AMD, supported since Moodle 2.9).&lt;br /&gt;
&lt;br /&gt;
* [[Javascript Modules]]&lt;br /&gt;
&lt;br /&gt;
=== yui/ ===&lt;br /&gt;
&lt;br /&gt;
Plugin&#039;s YUI modules (recommended way to include Javascript in Moodle 2.8 and older).&lt;br /&gt;
&lt;br /&gt;
* [[YUI/Modules]]&lt;br /&gt;
* [[YUI]]&lt;br /&gt;
&lt;br /&gt;
=== jquery/ ===&lt;br /&gt;
&lt;br /&gt;
Plugin&#039;s jQuery modules (Moodle 2.8 and older)&lt;br /&gt;
&lt;br /&gt;
* [[jQuery pre2.9]]&lt;br /&gt;
&lt;br /&gt;
=== pix/icon.svg ===&lt;br /&gt;
&lt;br /&gt;
The plugin&#039;s icon. [[Activity modules]] should provide 24 x 24 px colour icons. All other plugin files should be represented by 16 x 16 px monochrome icon. There should be a fallback &amp;lt;tt&amp;gt;pix/icon.png&amp;lt;/tt&amp;gt; version of the icon for legacy browsers.&lt;br /&gt;
&lt;br /&gt;
* [[Moodle icons]] provides icons design guidelines&lt;br /&gt;
&lt;br /&gt;
=== thirdpartylibs.xml ===&lt;br /&gt;
&lt;br /&gt;
The file should list all 3rd party libraries bundled with a plugin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;libraries&amp;gt;&lt;br /&gt;
    &amp;lt;library&amp;gt;&lt;br /&gt;
        &amp;lt;location&amp;gt;javascript/html5shiv.js&amp;lt;/location&amp;gt;&lt;br /&gt;
        &amp;lt;name&amp;gt;Html5Shiv&amp;lt;/name&amp;gt;&lt;br /&gt;
        &amp;lt;version&amp;gt;3.6.2&amp;lt;/version&amp;gt;&lt;br /&gt;
        &amp;lt;license&amp;gt;Apache&amp;lt;/license&amp;gt;&lt;br /&gt;
        &amp;lt;licenseversion&amp;gt;2.0&amp;lt;/licenseversion&amp;gt;&lt;br /&gt;
    &amp;lt;/library&amp;gt;&lt;br /&gt;
    &amp;lt;library&amp;gt;&lt;br /&gt;
        &amp;lt;location&amp;gt;vendor/guzzle/guzzle/&amp;lt;/location&amp;gt;&lt;br /&gt;
        &amp;lt;name&amp;gt;guzzle&amp;lt;/name&amp;gt;&lt;br /&gt;
        &amp;lt;version&amp;gt;v3.9.3&amp;lt;/version&amp;gt;&lt;br /&gt;
        &amp;lt;license&amp;gt;MIT&amp;lt;/license&amp;gt;&lt;br /&gt;
        &amp;lt;licenseversion&amp;gt;&amp;lt;/licenseversion&amp;gt;&lt;br /&gt;
    &amp;lt;/library&amp;gt;&lt;br /&gt;
&amp;lt;/libraries&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;location&amp;lt;/tt&amp;gt; is a path to the library directory or file, relative to the plugin&#039;s root directory. Please note that &amp;lt;tt&amp;gt;license&amp;lt;/tt&amp;gt; must be always [http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses compatible with the GNU GPLv3].&lt;br /&gt;
&lt;br /&gt;
Locations defined in this file are excluded from the coding style prechecks that are executed during the [[Plugin validation]].&lt;br /&gt;
&lt;br /&gt;
=== environment.xml ===&lt;br /&gt;
&lt;br /&gt;
Plugin can declare its additional environment requirements via this file - such as compulsory presence of some otherwise optional PHP extension. See [[Environment checking#Configuration file overview]] for details of the syntax.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;&lt;br /&gt;
&amp;lt;COMPATIBILITY_MATRIX&amp;gt;&lt;br /&gt;
  &amp;lt;PLUGIN name=&amp;quot;plugintype_pluginname&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;PHP_EXTENSIONS&amp;gt;&lt;br /&gt;
      &amp;lt;PHP_EXTENSION name=&amp;quot;soap&amp;quot; level=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;/PHP_EXTENSION&amp;gt;&lt;br /&gt;
    &amp;lt;/PHP_EXTENSIONS&amp;gt;&lt;br /&gt;
  &amp;lt;/PLUGIN&amp;gt;&lt;br /&gt;
&amp;lt;/COMPATIBILITY_MATRIX&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== README ===&lt;br /&gt;
&lt;br /&gt;
The file &amp;lt;tt&amp;gt;README.md&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;README.txt&amp;lt;/tt&amp;gt; should contain useful information about the plugin. Ideally, it should act as an offline version of all the informations that are available at the plugin&#039;s page in the [[Plugins directory]].&lt;br /&gt;
&lt;br /&gt;
=== CHANGES ===&lt;br /&gt;
&lt;br /&gt;
If the file &amp;lt;tt&amp;gt;CHANGES.md&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;CHANGES.txt&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;CHANGES.html&amp;lt;/tt&amp;gt; or just &amp;lt;tt&amp;gt;CHANGES&amp;lt;/tt&amp;gt; alternatively) is found when uploading a new plugin version into the [[Plugins directory]], then the contents of this file is used to pre-fill the release notes field for the new version.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Templates&amp;diff=51060</id>
		<title>Templates</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Templates&amp;diff=51060"/>
		<updated>2016-11-11T10:15:38Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* How do I call a template from javascript? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.9}}&lt;br /&gt;
&lt;br /&gt;
= Templates =&lt;br /&gt;
&lt;br /&gt;
== What is a template? ==&lt;br /&gt;
A template is an alternative to writing blocks of HTML directly in javascript / php by concatenating strings. The end result is the same, but templates have a number of advantages:&lt;br /&gt;
* It is easier to see the final result of the template because the code for a template is very close to what the final HTML will look like&lt;br /&gt;
* Because the templating language is intentionally limited, it is hard to introduce complex logic into a template. This makes it far easier for a theme designer to override a template, without breaking the logic&lt;br /&gt;
* Templates can be rendered from javascript. This allows ajax operations to re-render a portion of the page.&lt;br /&gt;
&lt;br /&gt;
== How do I write a template? ==&lt;br /&gt;
Templates are written in a language called &amp;quot;[http://mustache.github.io/mustache.5.html Mustache]&amp;quot;. Mustache is written as HTML with additional tags used to format the display of the data. Mustache tags are made of 2 opening and closing curly braces &amp;lt;code xml&amp;gt;{{tag}}&amp;lt;/code&amp;gt;. There are a few variations of these tags that behave differently.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{raiden}}&amp;lt;/code&amp;gt; This is a simple variable substitution. The variable named &amp;quot;raiden&amp;quot; will be searched for in the current context (and any parent contexts) and when a value is found, the entire tag will be replaced by the variable (HTML escaped).&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{{galaga}}}&amp;lt;/code&amp;gt; This is an unescaped variable substitution. Instead of escaping the variable before replacing it in the template, the variable is included raw. This is useful when the variable contains a block of HTML (for example).&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{#lemmings}} jump off cliff {{/lemmings}}&amp;lt;/code&amp;gt; These are opening and closing section tags. If the lemmings variable exists and evaluates to &amp;quot;not false&amp;quot; value, the variable is pushed on the stack, the contents of the section are parsed and included in the result. If the variable does not exist, or evaluates to false - the section will be skipped. If the variable lemmings evaluates to an array, the section will be repeated for each item in the array with the items of the array on the context. This is how to output a list.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{^lemmings}} enjoy view {{/lemmings}}&amp;lt;/code&amp;gt; Equivalent of &amp;quot;if-not&amp;quot; block, there is no &amp;quot;else&amp;quot; in mustache.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{&amp;gt; pacman }}&amp;lt;/code&amp;gt; This is a partial. Think of it like an include. Templates can include other templates using this tag.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{$blockvar}} ... {{/blockvar}}&amp;lt;/code&amp;gt; This is a block variable. It defines a section of the template that can be overridden when it&#039;s included in another template.&lt;br /&gt;
* &amp;lt;code xml&amp;gt;{{&amp;lt; template_name}} ... {{/template_name}}&amp;lt;/code&amp;gt; This is similar to including a partial but specifically indicates that you&#039;d like to override one or more black variables defined within the template you&#039;re including. You can override the black variables by defining a block variable within these tags that matches the name of the block variable you&#039;d like to override in the included template.&lt;br /&gt;
&lt;br /&gt;
So - putting this all together:&lt;br /&gt;
&lt;br /&gt;
recipe.mustache&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;h3&amp;gt;{{recipename}}&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;{{description}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Ingredients&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
{{#ingredients}}&lt;br /&gt;
&amp;lt;li&amp;gt;{{.}}&amp;lt;/li&amp;gt;&lt;br /&gt;
{{/ingredients}}&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Steps&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
{{#steps}}&lt;br /&gt;
&amp;lt;li&amp;gt;{{{.}}}&amp;lt;/li&amp;gt;&lt;br /&gt;
{{/steps}}&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
{{ &amp;gt; ratethisrecipe }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When given this data:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  recipename: &amp;quot;Cheese sandwich&amp;quot;,&lt;br /&gt;
  description: &amp;quot;Who doesn&#039;t like a good cheese sandwich?&amp;quot;,&lt;br /&gt;
  ingredients: [&amp;quot;bread&amp;quot;, &amp;quot;cheese&amp;quot;, &amp;quot;butter&amp;quot;],&lt;br /&gt;
  steps: [&amp;quot;&amp;lt;p&amp;gt;Step 1 is to spread the butter on the bread&amp;lt;/p&amp;gt;&amp;quot;, &amp;quot;&amp;lt;p&amp;gt;Step 2 is to put the cheese &amp;amp;quot;in&amp;amp;quot; the bread (not on top, or underneath)&amp;lt;/p&amp;gt;&amp;quot;]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Gives this: &amp;lt;span style=&amp;quot;font-size:4em&amp;quot;&amp;gt;😋&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More info - there are much clearer explanations of templates on the [http://mustache.github.io/mustache.5.html Mustache] website. Try reading those pages &amp;quot;before&amp;quot; posting on stack overflow :) .&lt;br /&gt;
&lt;br /&gt;
=== Blocks (Moodle 3.0 onwards) ===&lt;br /&gt;
Blocks are a feature of Mustache that deserves a special mention. The are used as a form of inheritance - and are crucial to building a library of re-usable templates. To use make use of &amp;quot;blocks&amp;quot; you define a parent template with replaceable sections. Each of those sections is marked with a &amp;quot;blocks&amp;quot; tag like this (A blocks tag looks like a regular tag, but the variable name is preceded with $):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;section&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;{{$sectionheading}}Default heading{{/sectionheading}}&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
{{$content}}Content for section{{/content}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now - wherever I need to re-use this template I can include it and replace the content of those sections at the same time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;lt; section}}&lt;br /&gt;
{{$sectionheading}}Latest News{{/sectionheading}}&lt;br /&gt;
{{$content}}Nothing happened today - sorry!{{/content}}&lt;br /&gt;
{{/section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when I include a template and I want to make use of blocks - the include tag points the other way:&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;lt; section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Not&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{&amp;gt; section}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Blocks support looping and many other cool things - for more info see https://github.com/bobthecow/mustache.php/wiki/BLOCKS-pragma&lt;br /&gt;
&lt;br /&gt;
== Where do I put my templates? ==&lt;br /&gt;
&lt;br /&gt;
Templates go in the &amp;lt;componentdir&amp;gt;/templates folder and must have a .mustache file extension. When loading templates the template name is &amp;lt;componentname&amp;gt;/&amp;lt;filename&amp;gt; (no file extension). &lt;br /&gt;
&lt;br /&gt;
So &amp;quot;mod_lesson/timer&amp;quot; would load the template at mod/lesson/templates/timer.mustache.&lt;br /&gt;
&lt;br /&gt;
Note: Do not try and put your templates in sub folders under the &amp;quot;/templates&amp;quot; directory. This is not supported and will not work.&lt;br /&gt;
&lt;br /&gt;
== How do I call a template from javascript? ==&lt;br /&gt;
&lt;br /&gt;
Rendering a template from javascript is fairly easy. There is a new AMD module that can load/cache and render a template for you. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// This is AMD code for loading the &amp;quot;core/templates&amp;quot; module. see [Javascript Modules].&lt;br /&gt;
require([&#039;core/templates&#039;], function(templates) {&lt;br /&gt;
&lt;br /&gt;
    // This will be the context for our template. So {{name}} in the template will resolve to &amp;quot;Tweety bird&amp;quot;.&lt;br /&gt;
    var context = { name: &#039;Tweety bird&#039;, intelligence: 2 };&lt;br /&gt;
&lt;br /&gt;
    // This will call the function to load and render our template. &lt;br /&gt;
    var promise = templates.render(&#039;block_looneytunes/profile&#039;, context);&lt;br /&gt;
&lt;br /&gt;
    // The promise object returned by this function means &amp;quot;I&#039;ve considered your request and will finish it later - I PROMISE!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    // How we deal with promise objects is by adding callbacks.&lt;br /&gt;
    promise.done(function(source, javascript) {&lt;br /&gt;
        // Here eventually I have my compiled template, and any javascript that it generated.&lt;br /&gt;
&lt;br /&gt;
        // I can execute the javascript (probably after adding the HTML to the DOM) like this:&lt;br /&gt;
        templates.runTemplateJS(javascript);&lt;br /&gt;
    });&lt;br /&gt;
  &lt;br /&gt;
    // Sometimes things fail&lt;br /&gt;
    promise.fail(function(ex) {&lt;br /&gt;
        // Deal with this exception (I recommend core/notify exception function for this).&lt;br /&gt;
    });&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Under the hood, this did many clever things for us. It loaded the template via an ajax call if it was not cached. It found any missing lang strings in the template and loaded them in a single ajax request, it split the JS from the HTML and returned us both in easy to use way. Read on for how to nicely deal with the javascript parameter.&lt;br /&gt;
&lt;br /&gt;
Note: with some nice chaining and sugar, we can shorten the above example quite a bit:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
require([&#039;core/templates&#039;, &#039;core/notification&#039;], function(templates, notification) {&lt;br /&gt;
    var context = { name: &#039;Tweety bird&#039;, intelligence: 2 };&lt;br /&gt;
    templates.render(&#039;block_looneytunes/profile&#039;, context)&lt;br /&gt;
        .done(doneCallback)&lt;br /&gt;
        .fail(notification.exception);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What if a template contains javascript? ==&lt;br /&gt;
&lt;br /&gt;
Sometimes a template requires that some JS be run when it is added to the page in order to give it more features. In the template we can include blocks of javascript, but we should use a special section tag that has a &amp;quot;helper&amp;quot; method registered to handle javascript carefully. &lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
profile.mustache&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;profile&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Name: {{name}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Intelligence: {{intelligence}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
require(&#039;jquery&#039;, function($) {&lt;br /&gt;
    // Effects! Can we have &amp;quot;blink&amp;quot;?&lt;br /&gt;
    $(&#039;#profile&#039;).slideDown();&lt;br /&gt;
});&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If this template is rendered by PHP, the javascript is separated from the HTML, and is appended to a special section in the footer of the page &amp;quot;after&amp;quot; requirejs has loaded. This provides the optimal page loading speed. If the template is rendered by javascript, the javascript source will be passed to the &amp;quot;done&amp;quot; handler from the promise. Then, when the &amp;quot;done&amp;quot; handler has added the template to the DOM, it can call &lt;br /&gt;
&amp;lt;code javascript&amp;gt;templates.runTemplateJS(javascript);&amp;lt;/code&amp;gt; &lt;br /&gt;
which will run the javascript (by creating a new script tag and appending it to the page head).&lt;br /&gt;
&lt;br /&gt;
== What other helpers can I use? ==&lt;br /&gt;
&lt;br /&gt;
=== {{# str }} ===&lt;br /&gt;
&lt;br /&gt;
There is a string helper for loading language strings.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} helloworld, mod_greeting {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 2 parameters are the string id and the component name. So this is effectively Mustache variant of &amp;lt;code&amp;gt;get_string(&#039;helloworld&#039;, &#039;mod_greeting&#039;)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The optional third parameter defines the value for the string&#039;s &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, David Beckham {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example would effectively do what &amp;lt;code&amp;gt;get_string(&#039;iscool&#039;, &#039;mod_cool&#039;, &#039;David Beckham&#039;)&amp;lt;/code&amp;gt; does in Moodle PHP code.&lt;br /&gt;
&lt;br /&gt;
Variable tags are allowed to define the value of the &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, {{ name }} {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For strings that accept complex placeholder, see the following section.&lt;br /&gt;
&lt;br /&gt;
=== {{# quote }} ===&lt;br /&gt;
&lt;br /&gt;
As shown in the previous section, the &amp;lt;code&amp;gt;{{# str }}&amp;lt;/code&amp;gt; helper may need complex data structures passed as the value of the &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; placeholder. You can use a JSON object syntax in that case:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;: &amp;quot;David&amp;quot;, &amp;quot;lastname&amp;quot;: &amp;quot;Beckham&amp;quot; } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to use the context values instead of literal strings, you might intuitively use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{! DO NOT DO THIS !}}&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;: &amp;quot;{{ firstname }}&amp;quot;, &amp;quot;lastname&amp;quot;: &amp;quot;{{ lastname }}&amp;quot; } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is a potential problem though. If the variable tag &amp;lt;code&amp;gt;{{ firstname }}&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;{{ lastname }}&amp;lt;/code&amp;gt; evaluates to a string containing the double quote character, that will break the JSON syntax. We need to escape the double quotes potentially appearing in the variable tags. For this, use the &amp;lt;code&amp;gt;{{# quote }}&amp;lt;/code&amp;gt; helper:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{! This is OK !}}&lt;br /&gt;
{{# str }} iscool, mod_cool, { &amp;quot;firstname&amp;quot;:  {{# quote }}{{ firstname }}{{/ quote }}, &amp;quot;lastname&amp;quot;: {{# quote }}{{ lastname }}{{/ quote }} } {{/ str }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See MDL-52136 for details.&lt;br /&gt;
=== {{# pix }} ===&lt;br /&gt;
&lt;br /&gt;
There is a pix icon helper for generating pix icon tags.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
{{# pix }} t/edit, core, Edit David Beckham {{/ pix }}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 2 parameters are the string id and the component name, the rest is the alt text for the image.&lt;br /&gt;
&lt;br /&gt;
== How do I call a template from php? ==&lt;br /&gt;
&lt;br /&gt;
The templates in php are attached to the renderers. There is a renderer method &amp;quot;render_from_template($templatename, $context)&amp;quot; that does the trick.&lt;br /&gt;
&lt;br /&gt;
== How do templates work with renderers? ==&lt;br /&gt;
&lt;br /&gt;
Extra care must be taken to ensure that the data passed to the context parameter is useful to the templating language. The template language cannot:&lt;br /&gt;
* Call functions&lt;br /&gt;
* Perform any boolean logic&lt;br /&gt;
* Render renderables&lt;br /&gt;
* Do capability checks&lt;br /&gt;
* Make DB queries&lt;br /&gt;
&lt;br /&gt;
So - I have &amp;quot;some&amp;quot; data in my renderable and some logic and HTML generation in my render method for that renderable - how do I refactor this to use a template?&lt;br /&gt;
&lt;br /&gt;
The first thing to note, is that you don&#039;t have to use a template if you don&#039;t want to. It just means that themers will still have to override your render method, instead of just overriding the template. But if you DO want to use a template, you will earn &amp;quot;cred&amp;quot; with themers, and you will be able to re-render parts of your interface from javascript in response to ajax requests without reloading the whole page (that&#039;s cool).&lt;br /&gt;
&lt;br /&gt;
There is a simple pattern to use to hook a template into a render method. If you make your renderable implement templatable as well as renderable - it will have to implement a new method &amp;quot;export_for_template(renderer_base $output)&amp;quot;. This method takes the data stored in the renderable and &amp;quot;flattens it&amp;quot; so it can be used in a template. If there is some nested data in the renderable (like other renderables) and they do not support templates, they can be &amp;quot;rendered&amp;quot; into the flat data structure using the renderer parameter. It should return an stdClass with properties that are only made of simple types: int, string, bool, float, stdClass or arrays of these types. Then the render method can updated to export the data and render it with the template.&lt;br /&gt;
&lt;br /&gt;
In the renderable:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 /**&lt;br /&gt;
     * Export this data so it can be used as the context for a mustache template.&lt;br /&gt;
     *&lt;br /&gt;
     * @return stdClass&lt;br /&gt;
     */&lt;br /&gt;
    public function export_for_template(renderer_base $output) {&lt;br /&gt;
        $data = new stdClass();&lt;br /&gt;
        $data-&amp;gt;canmanage = $this-&amp;gt;canmanage;&lt;br /&gt;
        $data-&amp;gt;things = array();&lt;br /&gt;
        foreach ($this-&amp;gt;things as $thing) {&lt;br /&gt;
            $data-&amp;gt;things[] = $thing-&amp;gt;to_record();&lt;br /&gt;
        }&lt;br /&gt;
        $data-&amp;gt;navigation = array();&lt;br /&gt;
        foreach ($this-&amp;gt;navigation as $button) {&lt;br /&gt;
            $data-&amp;gt;navigation[] = $output-&amp;gt;render($button);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $data;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the renderer class:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Defer to template.&lt;br /&gt;
     *&lt;br /&gt;
     * @param mywidget $widget&lt;br /&gt;
     *&lt;br /&gt;
     * @return string HTML for the page&lt;br /&gt;
     */&lt;br /&gt;
    render(mywidget $widget) {&lt;br /&gt;
        $data = $widget-&amp;gt;export_for_template($this);&lt;br /&gt;
        return $this-&amp;gt;render_from_template(&#039;mywidget&#039;, $data);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to I override a template in my theme? ==&lt;br /&gt;
&lt;br /&gt;
Templates can be overridden a bit easier than overriding a renderer. First - find the template that you want to change. E.g. &amp;quot;mod/wiki/templates/ratingui.mustache&amp;quot;. Now, create a sub-folder under your themes &amp;quot;templates&amp;quot; directory with the component name of the plugin you are overriding. E.g &amp;quot;theme/timtam/templates/mod_wiki&amp;quot;. Finally, copy the ratingui.mustache file into the newly created &amp;quot;theme/timtam/templates/mod_wiki&amp;quot; and edit it. You should see your changes immediately if theme designer mode is on. Note: templates are cached just like CSS, so if you are not using theme designer mode you will need to purge all caches to see the latest version of an edited template. If the template you are overriding contains a documentation comment (see next section) it is recommended to remove it, it will still show the documentation in the template library.&lt;br /&gt;
&lt;br /&gt;
== Should I document my templates? ==&lt;br /&gt;
&lt;br /&gt;
Yes!!!! Theme designers need to know the limits of what they can expect to change without breaking anything. As a further benefit - your beautiful new template can be displayed in the &amp;quot;Template Library&amp;quot; tool shipped with Moodle. In order to provide nice documentation and examples for the Template Library, you should follow these conventions when documenting your template.&lt;br /&gt;
&lt;br /&gt;
=== Add a documentation comment to your template ===&lt;br /&gt;
Mustache comments look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  {{! &lt;br /&gt;
   I am a comment.&lt;br /&gt;
   I can span multiple lines.&lt;br /&gt;
  }}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The template library will look for a mustache comment that contains this special marker as the documentation to display, and the source of an example context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
@template component/templatename&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Useful things to include in the documentation for a template ====&lt;br /&gt;
===== Classes required for JS =====&lt;br /&gt;
This is a list of classes that are used by the javascript for this template. If removing a class from an element in the template will break the javascript, list it here.&lt;br /&gt;
&lt;br /&gt;
===== Data attributes required for JS =====&lt;br /&gt;
This is a list of data attributes (e.g. data-enhance=&amp;quot;true&amp;quot;) that are used by the javascript for this template. If removing a data attribute from an element in the template will break the javascript, list it here.&lt;br /&gt;
&lt;br /&gt;
===== Context variables required for this template =====&lt;br /&gt;
This is a description of the data that may be contained in the context that is passed to the template. Be explicit and document every attribute.&lt;br /&gt;
&lt;br /&gt;
===== Example context (JSON) =====&lt;br /&gt;
The Template Library will look for this data in your documentation comment as it allows it to render a &amp;quot;preview&amp;quot; of the template right in the Template Library. This is useful for theme designers to test all the available templates in their new theme to make sure they look nice in a new theme. It is also useful to make sure the template responds to different screen sizes, languages and devices. The format is a JSON-encoded object that is passed directly into the render method for this template. &lt;br /&gt;
&lt;br /&gt;
==== A full example ====&lt;br /&gt;
lib/templates/pix_icon.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  {{!                                                                                                                                 &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;
    @template core/pix_icon                                                                                                         &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Moodle pix_icon template.                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    The purpose of this template is to render a pix_icon.                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Classes required for JS:                                                                                                        &lt;br /&gt;
    * none                                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Data attributes required for JS:                                                                                                &lt;br /&gt;
    * none                                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Context variables required for this template:                                                                                   &lt;br /&gt;
    * attributes Array of name / value pairs.                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    Example context (json):                                                                                                         &lt;br /&gt;
    {                                                                                                                               &lt;br /&gt;
        &amp;quot;attributes&amp;quot;: [                                                                                                             &lt;br /&gt;
            { &amp;quot;name&amp;quot;: &amp;quot;src&amp;quot;, &amp;quot;value&amp;quot;: &amp;quot;http://moodle.com/wp-content/themes/moodle/images/logo-hat2.png&amp;quot; },                          &lt;br /&gt;
            { &amp;quot;name&amp;quot;: &amp;quot;class&amp;quot;, &amp;quot;value&amp;quot;: &amp;quot;iconsmall&amp;quot; }                                                                               &lt;br /&gt;
        ]                                                                                                                           &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
  }}                                                                                                                                  &lt;br /&gt;
  &amp;lt;img {{#attributes}}{{name}}=&amp;quot;{{value}}&amp;quot; {{/attributes}}/&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Coding style for templates ==&lt;br /&gt;
This section documents some coding style guidelines to follow when writing templates. The reason for these guidelines is to promote consistency, and interoperability of the templates.&lt;br /&gt;
&lt;br /&gt;
=== Include GPL at the top of each template ===&lt;br /&gt;
&lt;br /&gt;
Templates are a form of code and it is appropriate to license them like any other code.&lt;br /&gt;
&lt;br /&gt;
===Include a documentation comment for each template===&lt;br /&gt;
&lt;br /&gt;
The exception is when you are overriding a template, if the documentation from the parent still applies, you do not need to copy it to the overridden template.&lt;br /&gt;
&lt;br /&gt;
===Use data-attributes for JS hooks===&lt;br /&gt;
&lt;br /&gt;
Data attributes are ideal for adding javascript hooks to templates because:&lt;br /&gt;
* Classes are meant for styling - theme designers should be able to change the classes at will without breaking any functionality.&lt;br /&gt;
* IDs must be unique in the page, but it is not possible to control how many times the same template might be included in the page.&lt;br /&gt;
* Data attributes can have meaningful names and can be efficiently queried with a selector&lt;br /&gt;
&lt;br /&gt;
===Avoid custom CSS for templates===&lt;br /&gt;
&lt;br /&gt;
This is not a hard rule, but a preference. We already have too much CSS in Moodle - where ever possible we should try and re-use the existing CSS instead of adding new CSS to support every new template.&lt;br /&gt;
&lt;br /&gt;
===Re-use core templates as much as possible===&lt;br /&gt;
&lt;br /&gt;
First we need to build the core set of reusable templates - but once that is in place we should always try to re-use those core templates to build interfaces. This will make Moodle more consistent, attractive and customisable.&lt;br /&gt;
&lt;br /&gt;
===Do use the CSS framework classes directly in the templates===&lt;br /&gt;
&lt;br /&gt;
We have bootstrap in core - so lets make the most of it. There is no problem using bootstrap classes in core templates, as long as the &amp;quot;base&amp;quot; theme is also tested, and an overridden template is added there if required.&lt;br /&gt;
&lt;br /&gt;
===Avoid IDs for styling or javascript===&lt;br /&gt;
&lt;br /&gt;
IDs should never evet be used for styling as they have a high CSS specificity, and so are hard to override. In addition, IDs should be unique in the page, which implies that a template could only be used once in a page. IDs are also not ideal for javascript, for the same reason (must be unique in a page).&lt;br /&gt;
&lt;br /&gt;
The only acceptable case to use an ID is you need to create a one to one connection between the JS and template. In this case use the uniqid helper to generate an ID that will not conflict with any other template on the page, and use it as part of the ID.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{uniqid}}-somethingspecific&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    callFunction(&#039;{{uniqid}}-somethingspecific&#039;);&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Follow CSS coding style===&lt;br /&gt;
&lt;br /&gt;
https://docs.moodle.org/dev/CSS_coding_style&lt;br /&gt;
&lt;br /&gt;
Use hyphens as word-separators for class names. &lt;br /&gt;
Use lower case class names.&lt;br /&gt;
&lt;br /&gt;
===Wrap each template in one node with a classname that matches the template name===&lt;br /&gt;
&lt;br /&gt;
Generate a class name by combining the component and template names and separating words with underscore.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;core_user_header&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:AJAX]]&lt;br /&gt;
[[Category:Javascript]]&lt;br /&gt;
[[Category:Output]]&lt;br /&gt;
&lt;br /&gt;
===Iterating over php arrays in a mustache template===&lt;br /&gt;
&lt;br /&gt;
Mustache treats hashes and arrays differently because of cross language compatibility&lt;br /&gt;
In php arrays and hashes are the same, but mustache treats them differently&lt;br /&gt;
It decides a php array is a hash and will not iterate over it if it is non 0 indexed and/or has a gap in the key numbers&lt;br /&gt;
so in short&lt;br /&gt;
you need to&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$datafortemplate-&amp;gt;mylist =  array_values($myarraywithnonnumerickeys)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
you could also use &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$datafortemplate-&amp;gt;mylist = new ArrayIterator($myarraywithnonnumerickeys);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
BUT this fails when $myarraywithnonnumerickeys is empty and you try to use&lt;br /&gt;
&amp;lt;code html5&amp;gt;&lt;br /&gt;
{{#mylist}}&lt;br /&gt;
with an array iterator if mylist is empty this block will not run&lt;br /&gt;
{{/mylist}}&lt;br /&gt;
{{^mylist}}&lt;br /&gt;
with an array iterator mylist will not run this block either because it is not quite empty&lt;br /&gt;
{{/mylist}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do I write core templates? ==&lt;br /&gt;
Core templates should ideally be simple generic components that can be used within other templates to create more complex page layouts. They should be flexible enough for developers and themers to easily use without having to replace the template. The templates should attempt to encapsulate some core structure for the element as well as key classes while allowing the content to be easily overridden. Ultimately we want to avoid having duplicate HTML copied from template to template where possible, particularly if the HTML element has some classes associated with it.&lt;br /&gt;
&lt;br /&gt;
Mustache relies on variables to substitute context data into the template but unfortunately it&#039;s very unlikely that the the names of the context data will match what the template is expecting for all the places that the template might be used. So in order to allow easy extensibility and avoid having to duplicate templates just to rename the variables we can wrap them in block variables which would allow the template that is including our template to replace that variable with one from it&#039;s own context inline.&lt;br /&gt;
&lt;br /&gt;
There are a few key points to keep in mind when writing a core template:&lt;br /&gt;
* Consider how your template will actually be used. Try writing a test page that uses your template to help discover some of the assumptions you might have in the template.&lt;br /&gt;
* The example context you provide in the template is mostly just for showing the template in the template library and is likely not how your template will actually be used. Most uses of the template will have a different context all together.&lt;br /&gt;
* Try to enforce a core structure but avoid enforcing a specific context. Content should be overridable.&lt;br /&gt;
* Use block variables to indicate sections of your template that people are likely to want to change. Typically where they will be wanting to substitute in their own content.&lt;br /&gt;
* Try to keep any javascript that accompanies the template as decoupled from the HTML / CSS structure of the template as possible. Instead of relying on the existence of certain HTML elements or CSS classes it is generally better to leverage data-attributes which can be added to any element.&lt;br /&gt;
&lt;br /&gt;
=== An example: tabs ===&lt;br /&gt;
Let&#039;s go through an example to illustrate how you might build a core template. For the example we&#039;ll be building a tabs template, since it&#039;s a fairly complex component that requires the use of block variables and javascript.&lt;br /&gt;
&lt;br /&gt;
First we can create a basic template to get the general structure down, let&#039;s call it tabs. Here&#039;s what it might look like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
		{{# tabs }}&lt;br /&gt;
			&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
					data-target=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
					data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
					aria-controls=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
					aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
				&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{{ name }}}&amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;lt;/li&amp;gt;&lt;br /&gt;
		{{/ tabs }}&lt;br /&gt;
	&amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
		{{# tabs }}&lt;br /&gt;
			&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
				class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
				id=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
				{{{ content }}}&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		{{/ tabs }}&lt;br /&gt;
	&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The template requires a context that looks something like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;tabs&amp;quot;: [&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab1&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 1&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 1 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab2&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 2&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 2 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab3&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Tab 3&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;This is tab 3 content &amp;lt;a href=\&amp;quot;#\&amp;quot;&amp;gt;test&amp;lt;/a&amp;gt;&amp;quot;}&lt;br /&gt;
	]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The javascript required to power the tabs element (keyboard navigation, show / hide panels etc) is written as an AMD module and is included by the template. The javascript is a little too large to go through here, but some key points to consider when writing it are:&lt;br /&gt;
It should ideally be independent of the HTML structure, so if someone wants to completely rewrite the tabs to be different elements (e.g. buttons or a set of divs) then the same javascript can be used without needing to change it. In order to achieve this it is important to identify the key components of the template.&lt;br /&gt;
&lt;br /&gt;
In this case it is a tab list, a tab and it&#039;s content. One way to identify these components would be to inspect the structure of the DOM, for example you might say &amp;quot;find me the ul element&amp;quot; when looking for the tab list and then &amp;quot;find my the child li elements&amp;quot; to find the tabs. While this would work, it couples your javascript to the HTML structure and makes it difficult to change later. A different approach would be to use the element attributes, for example you might say &amp;quot;find my the element with the role &#039;tablist&#039;&amp;quot; to get the tab list and then &amp;quot;find me the elements with the role &#039;tab&#039;&amp;quot; to get the tabs. This allows the HTML structure to change without breaking the javascript (as long as the correct attributes are set, of course).&lt;br /&gt;
&lt;br /&gt;
Another point of consideration for this example is what class to apply to a tab when it is selected. It makes sense to just apply something like &amp;quot;active&amp;quot; in the javascript, but that once again couples it to a particular CSS framework which makes it more difficult to change without modifying the javascript. In this case I chose to add a data attribute to the element to indicate which class will be set when the tab is selected. This means the javascript doesn&#039;t have to guess what the appropriate class is, it can just get it from the template.&lt;br /&gt;
&lt;br /&gt;
Ok, so we&#039;ve got our basic template. It&#039;s time to use it! Let&#039;s say we want to create a simple user profile page that might show 2 tabs, the first tab will be the user&#039;s name and the second tab will be the user&#039;s email address (please excuse the contrived example).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s what the page might look like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;lt; core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That looks pretty simple! The only problem is, how do I get my content there? I would have to supply a context like this in order to display the tabs I want:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;tabs&amp;quot;: [&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab1&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Name&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;Your name is Mr. Test User.&amp;quot;},&lt;br /&gt;
		{&amp;quot;id&amp;quot;:&amp;quot;tab2&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Email&amp;quot;,&amp;quot;content&amp;quot;:&amp;quot;Your email is testuser@example.com&amp;quot;},&lt;br /&gt;
	]&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let&#039;s assume that the context for this page doesn&#039;t match what the tabs template is expecting though (as will be the case most of the time). Let&#039;s assume the tabs template is being rendered with this context:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
	&amp;quot;name&amp;quot;:&amp;quot;Mr. Test User&amp;quot;,&lt;br /&gt;
	&amp;quot;email&amp;quot;:&amp;quot;testuser@example.com&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately, we&#039;ll almost certainly never have complete control over all of the contexts that our template will be rendered in which means we&#039;ll be expecting people to write new webservices to supply the same data in different formats every time they want to use a template. It becomes an unmanageable problem.&lt;br /&gt;
&lt;br /&gt;
Enter blocks! We can make the template more flexible by defining sections of the template that can be overriden when they are included. Pretty neat! This will allow us to enforce a certain core structure but not enforce a context on the template that is including the tabs.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s have another go at that template, this time leverging blocks:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
	{{$ tabheader }}&lt;br /&gt;
		&amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
							data-target=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
							data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
							aria-controls=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&lt;br /&gt;
							aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
						&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{{ name }}}&amp;lt;/a&amp;gt;&lt;br /&gt;
					&amp;lt;/li&amp;gt;&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
		&amp;lt;/ul&amp;gt;&lt;br /&gt;
	{{/ tabheader }}&lt;br /&gt;
	{{$ tabbody }}&lt;br /&gt;
		&amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
						class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
						id=&amp;quot;{{ uniqid }}-{{ id }}&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
						{{{ content }}}&lt;br /&gt;
					&amp;lt;/div&amp;gt;&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		&amp;lt;/div&amp;gt;&lt;br /&gt;
	{{/ tabbody }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A summary of what we&#039;ve changed:&lt;br /&gt;
* Added a $tabheader block around the tab list, in case someone wants to change the ul element to something else.&lt;br /&gt;
* Added a $tablist block around the group of tabs to allow them to be overriden on incldue.&lt;br /&gt;
* Added a $tabbody block around the content, in case someone wants to change the content elements from divs.&lt;br /&gt;
* Added a $tabcontent block around the tab variable for the content to allow the content to be overriden on inlcude.&lt;br /&gt;
&lt;br /&gt;
Now let&#039;s see what using this template looks like for your User Profile page:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;gt; core/tabs }}&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
						data-target=&amp;quot;{{ uniqid }}-tab1&amp;quot;&lt;br /&gt;
						data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
						aria-controls=&amp;quot;{{ uniqid }}-tab1&amp;quot;&lt;br /&gt;
						aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Name&amp;lt;/a&amp;gt;&lt;br /&gt;
				&amp;lt;/li&amp;gt;&lt;br /&gt;
				&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
						data-target=&amp;quot;{{ uniqid }}-tab2&amp;quot;&lt;br /&gt;
						data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
						aria-controls=&amp;quot;{{ uniqid }}-tab2&amp;quot;&lt;br /&gt;
						aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Email&amp;lt;/a&amp;gt;&lt;br /&gt;
				&amp;lt;/li&amp;gt;&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
					class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
					id=&amp;quot;{{ uniqid }}-tab1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					Your name is {{ name }}.&lt;br /&gt;
				&amp;lt;/div&amp;gt;&lt;br /&gt;
				&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
					class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
					id=&amp;quot;{{ uniqid }}-tab2&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
					Your email address is {{ email }}.&lt;br /&gt;
				&amp;lt;/div&amp;gt;&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		{{/ core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That looks a bit better! Now we&#039;ve been able to use the blocks to successfully change the template to use the context available to this page, we no longer need a &amp;quot;tabs&amp;quot; array with &amp;quot;name&amp;quot; and &amp;quot;content&amp;quot;. Even the javascript will continue to work because we&#039;ve kept the correct element attributes. &lt;br /&gt;
&lt;br /&gt;
We&#039;ve still got a slight problem though... In order to change the data for the template we&#039;ve had to copy &amp;amp; paste the HTML from the original template into our blocks as we do the override. While this works fine in this example, it means we don&#039;t quite get the encapsulation we want within the templates since we&#039;re leaking internal implementation details. If we ever wanted to change the CSS framework we use for Moodle (say from bootstrap 2 to boostrap 3 or 4) we&#039;d have to find all the places in the code where this tabs template is used and make sure that the HTML is correct in their block overrides.&lt;br /&gt;
&lt;br /&gt;
With that in mind, let&#039;s take one more pass at this template and see if we can improve it slightly again. This time we&#039;re doing to split the template out into 3 templates.&lt;br /&gt;
&lt;br /&gt;
tabs.mustache:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;{{ uniqid }}-tab-container&amp;quot;&amp;gt;&lt;br /&gt;
	{{$ tabheader }}&lt;br /&gt;
		&amp;lt;ul role=&amp;quot;tablist&amp;quot; class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					{{&amp;lt; core/tab_header_item }}		&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
		&amp;lt;/ul&amp;gt;&lt;br /&gt;
	{{/ tabheader }}&lt;br /&gt;
	{{$ tabbody }}&lt;br /&gt;
		&amp;lt;div class=&amp;quot;tab-content&amp;quot;&amp;gt;&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{# tabs }}&lt;br /&gt;
					{{&amp;lt; core/tab_content_item }}&lt;br /&gt;
				{{/ tabs }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		&amp;lt;/div&amp;gt;&lt;br /&gt;
	{{/ tabbody }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
    require([&#039;jquery&#039;,&#039;core/tabs&#039;], function($, tabs) {&lt;br /&gt;
&lt;br /&gt;
        var container = $(&amp;quot;#{{ uniqid }}-tab-container&amp;quot;);&lt;br /&gt;
        tabs.create(container);&lt;br /&gt;
    });&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tab_header_item.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;li role=&amp;quot;tab&amp;quot;&lt;br /&gt;
		data-selected-class=&amp;quot;active&amp;quot;&lt;br /&gt;
		aria-selected=&amp;quot;false&amp;quot; tabindex=&amp;quot;-1&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;{{$ tabname }}{{{ name }}}{{/ tabname }}&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tab_content_item.mustache&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;div role=&amp;quot;tabpanel&amp;quot;&lt;br /&gt;
	class=&amp;quot;tab-pane&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	{{$ tabpanelcontent }}{{{ content }}}{{/ tabpanelcontent }}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A summary of the changes:&lt;br /&gt;
* Split the template into 3, moving the tab into it&#039;s own template and the content into it&#039;s own and then including them in the tabs template.&lt;br /&gt;
* Removed the ids from the tabs and content. The javascript would be updating to assign these ids at runtime so that they don&#039;t need to be provided as part of the template context.&lt;br /&gt;
* Added a $tabname block for in the tab_header_item template to make the name flexible on import.&lt;br /&gt;
* Added a $tabpanelcontant block in the tab_content_item template to make the content flexible on import.&lt;br /&gt;
&lt;br /&gt;
Cool, so let&#039;s see what that looks like in our example now:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
    &amp;lt;header&amp;gt;&amp;lt;title&amp;gt;User Profile&amp;lt;/title&amp;gt;&amp;lt;/header&amp;gt;&lt;br /&gt;
    &amp;lt;body&amp;gt;&lt;br /&gt;
        {{&amp;gt; core/tabs }}&lt;br /&gt;
			{{$ tablist }}&lt;br /&gt;
				{{&amp;gt; core/tab_header_item }}&lt;br /&gt;
					{{$ tabname }}Name{{/ tabname }}&lt;br /&gt;
				{{/ core/tab_header_item }}&lt;br /&gt;
				{{&amp;gt; core/tab_header_item }}&lt;br /&gt;
					{{$ tabname }}Email{{/ tabname }}&lt;br /&gt;
				{{/ core/tab_header_item }}&lt;br /&gt;
			{{/ tablist }}&lt;br /&gt;
			{{$ tabcontent }}&lt;br /&gt;
				{{&amp;gt; core/tab_content_item }}&lt;br /&gt;
					{{$ tabpanelcontent }}Your name is {{ name }}.{{/ tabpanelcontent }}&lt;br /&gt;
				{{/ core/tab_content_item }}&lt;br /&gt;
				{{&amp;gt; core/tab_content_item }}&lt;br /&gt;
					{{$ tabpanelcontent }}Your email address is {{ email }}.{{/ tabpanelcontent }}&lt;br /&gt;
				{{/ core/tab_content_item }}&lt;br /&gt;
			{{/ tabcontent }}&lt;br /&gt;
		{{/ core/tabs }}&lt;br /&gt;
    &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we&#039;re done! After making the changes above we&#039;ve been able to keep the benefits of the previous change to allow the context changes but we&#039;ve also removed the need to copy &amp;amp; paste the HTML everywhere. Instead we&#039;re able to use the child templates with a few additional blocks defined to get the content in there.&lt;br /&gt;
&lt;br /&gt;
Now if we want to change tabs HTML or CSS frameworks we can just change the core tabs templates and this page will receive the updates for free.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=51026</id>
		<title>PHPUnit</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=51026"/>
		<updated>2016-11-04T12:19:22Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Explicitly mention that some tests require a web connection&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.3}}&lt;br /&gt;
&lt;br /&gt;
=What is PHPUnit=&lt;br /&gt;
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP. It is installed as Composer dependency and is not part of Moodle installation. To run PHPUnit tests, you have to manually install it on your development computer or test server.&lt;br /&gt;
&lt;br /&gt;
Read the excellent guide at&lt;br /&gt;
* [http://phpunit.de/manual/current/en/index.html PHPUnit Manual]&lt;br /&gt;
&lt;br /&gt;
=Installation of PHPUnit via Composer=&lt;br /&gt;
&lt;br /&gt;
Windows may require additional installation steps.&lt;br /&gt;
On Windows go to https://getcomposer.org/download/ and download the Composer-Setup.exe file&lt;br /&gt;
&lt;br /&gt;
* Execute Composer installer&lt;br /&gt;
&lt;br /&gt;
 cd /your/moodle/dirroot&lt;br /&gt;
 php composer.phar install&lt;br /&gt;
&lt;br /&gt;
(If that gives you connection problems try...)&lt;br /&gt;
&lt;br /&gt;
 php composer.phar install --prefer-source&lt;br /&gt;
&lt;br /&gt;
Troubleshooting:&lt;br /&gt;
* On Windows if you are behind a proxy you will need to setup an environment variable called HTTP_PROXY with a value detailing your HTTP Proxy address and port before composer will correctly download files.&lt;br /&gt;
* You may be prompted for github credentials when installing composer dependencies.&lt;br /&gt;
** This is used to generate an personal access token to avoid being rate limited by github.&lt;br /&gt;
** If you have Two Factor Authentication enabled on your github account, or do not wish to supply your own credentials you will need to generate a token manually:&lt;br /&gt;
*** Visit https://github.com/settings/applications and request personal access token&lt;br /&gt;
*** Copy this token and add it to your [https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]&lt;br /&gt;
** ( See [https://github.com/composer/composer/issues/2280 composer issue #2280] for further details of this bug.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [http://getcomposer.org/doc/00-intro.md Composer documentation]&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [[PHPUnit installation in Windows]]&lt;br /&gt;
* [[PHPUnit installation in OS X]]&lt;br /&gt;
&lt;br /&gt;
== Uninstalling previous PEAR based version ==&lt;br /&gt;
&lt;br /&gt;
Before using composer, this page used to suggest to install phpunit via PEAR. If you did so, you may wish to uninstall that package now. This should work:&lt;br /&gt;
&lt;br /&gt;
   $ pear uninstall phpunit/DbUnit&lt;br /&gt;
   $ pear uninstall phpunit/PHPUnit&lt;br /&gt;
&lt;br /&gt;
=Initialisation of test environment=&lt;br /&gt;
&lt;br /&gt;
Our PHPUnit integration requires a dedicated database and dataroot.  First, add a new dataroot directory and prefix into your config.php, you can find examples in config-dist.php (scroll down to &#039;Section 9&#039;).&lt;br /&gt;
&lt;br /&gt;
 $CFG-&amp;gt;phpunit_prefix = &#039;phpu_&#039;;&lt;br /&gt;
 $CFG-&amp;gt;phpunit_dataroot = &#039;/home/example/phpu_moodledata&#039;;&lt;br /&gt;
&lt;br /&gt;
Some PHPUnit tests require a live internet connection. If your system does not have a direct connection to the Internet, you also need to specify your proxy in config.php - even though you normally specify it by using the admin settings user interface. (If you do not use a proxy, you can skip this step.) Check the settings on the relevant admin settings page, or from the mdl_config table in your database, if you are unsure of the correct values.&lt;br /&gt;
 &lt;br /&gt;
 // Normal proxy settings&lt;br /&gt;
 $CFG-&amp;gt;proxyhost = &#039;wwwcache.example.org&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxyport = 80;&lt;br /&gt;
 $CFG-&amp;gt;proxytype = &#039;HTTP&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxybypass = &#039;localhost, 127.0.0.1, .example.org&#039;;&lt;br /&gt;
 // Omit the next lines if your proxy doesn&#039;t need a username/password:&lt;br /&gt;
 $CFG-&amp;gt;proxyuser = &#039;systemusername&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxypassword = &#039;systempassword&#039;;&lt;br /&gt;
&lt;br /&gt;
From Moodle 2.8.5 onwards, you can also provide specific database settings for unit testing (if these are not provided, the standard database settings will be used):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$CFG-&amp;gt;phpunit_dbtype    = &#039;pgsql&#039;;      // &#039;pgsql&#039;, &#039;mariadb&#039;, &#039;mysqli&#039;, &#039;mssql&#039;, &#039;sqlsrv&#039; or &#039;oci&#039;&lt;br /&gt;
$CFG-&amp;gt;phpunit_dblibrary = &#039;native&#039;;     // &#039;native&#039; only at the moment&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbhost    = &#039;127.0.0.1&#039;;  // eg &#039;localhost&#039; or &#039;db.isp.com&#039; or IP&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbname    = &#039;mytestdb&#039;;     // database name, eg moodle&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbuser    = &#039;postgres&#039;;   // your database username&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbpass    = &#039;some_password&#039;;   // your database password&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you need to initialise the test environment using following command.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 php admin/tool/phpunit/cli/init.php&lt;br /&gt;
&lt;br /&gt;
This command has to be repeated after any upgrade, plugin (un)installation or if you have added tests to a plugin you are developing:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; make sure that your php cli executable (or the one you want to use) is correctly on your path as the individual init scripts will call it repeatedly. Also, ensure en_AU locale is installed on your server.&lt;br /&gt;
&lt;br /&gt;
== LDAP ==&lt;br /&gt;
If you want to run LDAP unit tests you must have a working, configured LDAP environment on your test server. There must be a basic LDAP tree structure in place or tests will fail with &amp;quot;Search: No such object&amp;quot;. Build an LDAP tree structure in a new shell prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ldapadd -H ldap://127.0.0.1 -D &amp;quot;cn=admin,dc=yourcomputer,dc=local&amp;quot; -W&lt;br /&gt;
dn:dc=yourcomputer,dc=local&lt;br /&gt;
objectClass:dcObject&lt;br /&gt;
objectClass:organizationalUnit&lt;br /&gt;
dc:yourcomputer&lt;br /&gt;
ou:YOURCOMPUTER&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In config.php tell Moodle where to look for your test LDAP environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_HOST_URL&#039;, &#039;ldap://127.0.0.1&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_BIND_DN&#039;, &#039;cn=admin,dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_BIND_PW&#039;, &#039;*&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_DOMAIN&#039;, &#039;dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Test execution=&lt;br /&gt;
&lt;br /&gt;
To execute all test suites from main configuration file execute the &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; script from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 vendor/bin/phpunit&lt;br /&gt;
&lt;br /&gt;
The rest of examples uses &amp;lt;code&amp;gt;phpunit&amp;lt;/code&amp;gt;, please substitute it with &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; or create a shortcut in your dirroot.&lt;br /&gt;
&lt;br /&gt;
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to &amp;lt;code&amp;gt;phpunit.xml&amp;lt;/code&amp;gt; from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is an alternative script for running of tests via web interface: &amp;lt;code&amp;gt;admin/tool/phpunit/webrunner.php&amp;lt;/code&amp;gt;. Use this as the last resort only when you cannot use the command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to run only some tests===&lt;br /&gt;
&lt;br /&gt;
==== Running a single test quickly ====&lt;br /&gt;
&lt;br /&gt;
The fastest way to run a single test is:&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit my_test_class_name my/tests/filename.php&lt;br /&gt;
&lt;br /&gt;
This is faster than other methods because it avoids the need to search for the test file, but you should be careful because it may be possible to run tests this way which are not included in the normal run. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.&lt;br /&gt;
&lt;br /&gt;
Please note the path name is optional if the testcase class and file names are standardised as described in [[Writing PHPUnit tests]].&lt;br /&gt;
&lt;br /&gt;
==== Using the @group annotation ====&lt;br /&gt;
&lt;br /&gt;
If you add annotations like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for {@link stack_cas_keyval}.&lt;br /&gt;
 * @group qtype_stack&lt;br /&gt;
 */&lt;br /&gt;
class qtype_stack_cas_keyval_exception_testcase extends basic_testcase {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to all the classes in your plugin, then you can run just the tests for your plugin by doing&lt;br /&gt;
&lt;br /&gt;
 phpunit --group qtype_stack&lt;br /&gt;
&lt;br /&gt;
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.&lt;br /&gt;
&lt;br /&gt;
==== Using multiple phpunit.xml files ====&lt;br /&gt;
&lt;br /&gt;
It&#039;s easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml&lt;br /&gt;
&lt;br /&gt;
Also, for commodity, you can use this command:&lt;br /&gt;
&lt;br /&gt;
 php admin/tool/phpunit/cli/util.php --buildcomponentconfigs&lt;br /&gt;
&lt;br /&gt;
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // Note that it&#039;s not needed to specify the name of the file (if it is &#039;phpunit.xml&#039;).&lt;br /&gt;
 vendor/bin/phpunit -c question&lt;br /&gt;
 vendor/bin/phpunit -c question/type/calculated&lt;br /&gt;
 vendor/bin/phpunit -c backup&lt;br /&gt;
 vendor/bin/phpunit -c lib/dml&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
or, also&lt;br /&gt;
&lt;br /&gt;
 cd directory/with/phpunit.xml&lt;br /&gt;
 phpunit&lt;br /&gt;
&lt;br /&gt;
=External test resources=&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
By default Moodle phpunit tests contact http://download.moodle.org server when testing curl related functionality. Optionally you may checkout a local copy of the test scripts and access it instead:&lt;br /&gt;
&lt;br /&gt;
# clone https://github.com/moodlehq/moodle-exttests to web directory&lt;br /&gt;
# add to your config.php or modify phpunit.xml file &amp;lt;code php&amp;gt;define(&#039;TEST_EXTERNAL_FILES_HTTP_URL&#039;, &#039;http://localhost/moodle-exttests&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Writing new tests=&lt;br /&gt;
* read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation]&lt;br /&gt;
* see [[Writing PHPUnit tests]]&lt;br /&gt;
&lt;br /&gt;
=Conversion of existing SimpleTests=&lt;br /&gt;
* see [[SimpleTest conversion]]&lt;br /&gt;
&lt;br /&gt;
=PHPUnit support in IDEs=&lt;br /&gt;
&lt;br /&gt;
* [[Setting up Eclipse]]&lt;br /&gt;
* [[Setting up Netbeans]]&lt;br /&gt;
* [[Setting up PhpStorm]]&lt;br /&gt;
&lt;br /&gt;
=Common Unit Test Problems=&lt;br /&gt;
[[Common unit test problems]]&lt;br /&gt;
&lt;br /&gt;
=Performance=&lt;br /&gt;
&lt;br /&gt;
A typical run of full PHPUnit tests for Moodle 2.7 takes 10-20 minutes depending on the machine. If tests run slowly for you:&lt;br /&gt;
&lt;br /&gt;
* Ensure you are using a database and filesystem running on the same machine that is running the tests (not on a remote server).&lt;br /&gt;
* Apply developer-only performance settings to your database: [[Postgres Tuning For Developers]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Unit testing]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Core_APIs&amp;diff=51000</id>
		<title>Core APIs</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Core_APIs&amp;diff=51000"/>
		<updated>2016-10-26T13:25:32Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Moodle has a number of core APIs that provide tools for Moodle scripts.&lt;br /&gt;
&lt;br /&gt;
They are essential when writing [[Plugins|Moodle plugins]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Most-used General APIs==&lt;br /&gt;
&lt;br /&gt;
These APIs are critical and will be used by nearly every Moodle plugin.&lt;br /&gt;
&lt;br /&gt;
=== Access API (access) ===&lt;br /&gt;
&lt;br /&gt;
The [[Access API]] gives you functions so you can determine what the current user is allowed to do, and it allows modules to extend Moodle with new capabilities.&lt;br /&gt;
&lt;br /&gt;
=== Data manipulation API (dml) ===&lt;br /&gt;
&lt;br /&gt;
The [[Data manipulation API]] allows you to read/write to databases in a consistent and safe way.&lt;br /&gt;
&lt;br /&gt;
=== File API (files) ===&lt;br /&gt;
&lt;br /&gt;
The [[File API]] controls the storage of files in connection to various plugins.&lt;br /&gt;
&lt;br /&gt;
=== Form API (form) ===&lt;br /&gt;
&lt;br /&gt;
The [[Form API]] defines and handles user data via web forms.&lt;br /&gt;
&lt;br /&gt;
=== Logging API (log) ===&lt;br /&gt;
&lt;br /&gt;
The [[Event 2]] API allows you to log events in Moodle, while [[Logging 2]] describes how logs are stored and retrieved.&lt;br /&gt;
&lt;br /&gt;
=== Navigation API (navigation) ===&lt;br /&gt;
&lt;br /&gt;
The [[Navigation API]] allows you to manipulate the navigation tree to add and remove items as you wish.&lt;br /&gt;
&lt;br /&gt;
=== Page API (page) ===&lt;br /&gt;
&lt;br /&gt;
The [[Page API]] is used to set up the current page, add JavaScript, and configure how things will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
=== Output API (output) ===&lt;br /&gt;
&lt;br /&gt;
The [[Output API]] is used to render the HTML for all parts of the page.&lt;br /&gt;
&lt;br /&gt;
=== String API (string) ===&lt;br /&gt;
&lt;br /&gt;
The [[String API]] is how you get language text strings to use in the user interface.   It handles any language translations that might be available.&lt;br /&gt;
&lt;br /&gt;
=== Upgrade API (upgrade) ===&lt;br /&gt;
&lt;br /&gt;
The [[Upgrade API]] is how your module installs and upgrades itself, by keeping track of its own version.&lt;br /&gt;
&lt;br /&gt;
=== Moodlelib API (core) ===&lt;br /&gt;
&lt;br /&gt;
The [[Moodlelib API]] is the central library file of miscellaneous general-purpose Moodle functions. Functions can over the handling of request parameters, configs, user preferences, time, login, mnet, plugins, strings and others. There are plenty of defined constants too.&lt;br /&gt;
&lt;br /&gt;
==Other General APIs==&lt;br /&gt;
&lt;br /&gt;
=== Admin settings (admin) ===&lt;br /&gt;
&lt;br /&gt;
The [[Admin settings]] API deals with providing configuration options for each plugin and Moodle core.&lt;br /&gt;
&lt;br /&gt;
=== Availability (availability) ===&lt;br /&gt;
&lt;br /&gt;
The [[Availability API]] controls access to activities and sections.&lt;br /&gt;
&lt;br /&gt;
=== Backup API (backup) ===&lt;br /&gt;
&lt;br /&gt;
The [[Backup API]] defines exactly how to convert course data into XML for backup purposes, and the [[Restore API]] describes how to convert it back the other way.&lt;br /&gt;
&lt;br /&gt;
=== Cache API (cache) ===&lt;br /&gt;
&lt;br /&gt;
The [[The Moodle Universal Cache (MUC)]] is the structure for storing cache data within Moodle. [[Cache_API]] explains some of what is needed to use a cache in your code.&lt;br /&gt;
&lt;br /&gt;
=== Calendar API (calendar) ===&lt;br /&gt;
&lt;br /&gt;
The [[Calendar API]] allows you to add and modify events in the calendar for user, groups, courses, or the whole site.&lt;br /&gt;
&lt;br /&gt;
=== Comment API (comment) ===&lt;br /&gt;
&lt;br /&gt;
The [[Comment API]] allows you to save and retrieve user comments, so that you can easily add commenting to any of your code.&lt;br /&gt;
&lt;br /&gt;
=== Competency API (competency) ===&lt;br /&gt;
&lt;br /&gt;
The [[Competency API]] allows you to list and add evidence of competencies to learning plans, learning plan templates, frameworks, courses and activities.&lt;br /&gt;
&lt;br /&gt;
=== Data definition API (ddl) ===&lt;br /&gt;
&lt;br /&gt;
The [[Data definition API]] is what you use to create, change and delete tables and fields in the database during upgrades.&lt;br /&gt;
&lt;br /&gt;
=== Enrolment API (enrol) ===&lt;br /&gt;
&lt;br /&gt;
The [[Enrolment API]] deals with course participants.&lt;br /&gt;
&lt;br /&gt;
=== Events API (event) ===&lt;br /&gt;
&lt;br /&gt;
The [[Event 2]] allows to define &amp;quot;events&amp;quot; with payload data to be fired whenever you like, and it also allows you to define handlers to react to these events when they happen.  This is the recommended form of inter-plugin communication. This also forms the basis for logging in Moodle.&lt;br /&gt;
&lt;br /&gt;
=== External functions API (external) ===&lt;br /&gt;
&lt;br /&gt;
The [[External functions API]] allows you to create fully parametrised methods that can be accessed by external programs (such as [[Web services]]).&lt;br /&gt;
&lt;br /&gt;
=== Lock API (lock) ===&lt;br /&gt;
&lt;br /&gt;
The [[Lock API]] lets you synchronise processing between multiple requests, even for separate nodes in a cluster.&lt;br /&gt;
&lt;br /&gt;
=== Message API (message) ===&lt;br /&gt;
&lt;br /&gt;
The [[Message API]] lets you post messages to users.  They decide how they want to receive them.&lt;br /&gt;
&lt;br /&gt;
=== Media API (media) ===&lt;br /&gt;
&lt;br /&gt;
The [[Media embedding]] API can be used to embed media items such as audio, video, and Flash.&lt;br /&gt;
&lt;br /&gt;
=== My profile API ===&lt;br /&gt;
&lt;br /&gt;
The [[My profile API]] is used to add things to the profile page.&lt;br /&gt;
&lt;br /&gt;
=== Preference API (preference) ===&lt;br /&gt;
&lt;br /&gt;
The [[Preference API]] is a simple way to store and retrieve preferences for individual users.&lt;br /&gt;
&lt;br /&gt;
=== Portfolio API (portfolio) ===&lt;br /&gt;
&lt;br /&gt;
The [[Portfolio API]] allows you to add portfolio interfaces on your pages and allows users to package up data to send to their portfolios.&lt;br /&gt;
&lt;br /&gt;
=== Rating API (rating) ===&lt;br /&gt;
&lt;br /&gt;
The [[Rating API]] lets you create AJAX rating interfaces so that users can rate items in your plugin.  In an activity module, you may choose to aggregate ratings to form grades.&lt;br /&gt;
&lt;br /&gt;
=== RSS API (rss) ===&lt;br /&gt;
&lt;br /&gt;
The [[RSS API]] allows you to create secure RSS feeds of data in your module.&lt;br /&gt;
&lt;br /&gt;
=== Search API (search) ===&lt;br /&gt;
&lt;br /&gt;
The [[Search API]] allows you to index contents in a search engine and query the search engine for results.&lt;br /&gt;
&lt;br /&gt;
=== Tag API (tag) ===&lt;br /&gt;
&lt;br /&gt;
The [[Tag API]] allows you to store tags (and a tag cloud) to items in your module.&lt;br /&gt;
&lt;br /&gt;
=== Task API (task) ===&lt;br /&gt;
&lt;br /&gt;
The [[Task API]] lets you run jobs in the background. Either once off, or on a regular schedule.&lt;br /&gt;
&lt;br /&gt;
=== Time API (time) ===&lt;br /&gt;
&lt;br /&gt;
The [[Time API]] takes care of translating and displaying times between users in the site.&lt;br /&gt;
&lt;br /&gt;
=== Testing API (test) ===&lt;br /&gt;
&lt;br /&gt;
The testing API contains the Unit test API ([[PHPUnit]]) and Acceptance test API ([[Acceptance testing]]). Ideally all new code should have unit tests written FIRST.&lt;br /&gt;
&lt;br /&gt;
=== User-related APIs (user) ===&lt;br /&gt;
&lt;br /&gt;
This is a rather informal grouping of miscellaneous [[User-related APIs]] relating to sorting and searching lists of users.&lt;br /&gt;
&lt;br /&gt;
=== Web services API (webservice) ===&lt;br /&gt;
&lt;br /&gt;
The [[Web services API]] allows you to expose particular functions (usually external functions) as web services.&lt;br /&gt;
&lt;br /&gt;
=== Badges API (OpenBadges) ===&lt;br /&gt;
&lt;br /&gt;
The [https://docs.moodle.org/dev/OpenBadges_User_Documentation Badges] user documentation (is a temp page until we compile a proper page with all the classes and APIs that allows you to manage particular badges and OpenBadges Backpack).&lt;br /&gt;
&lt;br /&gt;
== Activity module APIs ==&lt;br /&gt;
&lt;br /&gt;
Activity modules are the most important plugin in Moodle.  There are several core APIs that service only Activity modules.&lt;br /&gt;
&lt;br /&gt;
=== Activity completion API (completion) ===&lt;br /&gt;
&lt;br /&gt;
The [[Activity completion API]] is to indicate to the system how activities are completed.&lt;br /&gt;
&lt;br /&gt;
=== Advanced grading API (grading) ===&lt;br /&gt;
&lt;br /&gt;
The [[Advanced grading API]] allows you to add more advanced grading interfaces (such as rubrics) that can produce simple grades for the gradebook.&lt;br /&gt;
&lt;br /&gt;
=== Conditional activities API (condition) - deprecated in 2.7 ===&lt;br /&gt;
&lt;br /&gt;
The deprecated [[Conditional activities API]] used to provide conditional access to modules and sections in Moodle 2.6 and below. It has been replaced by the [[Availability API]].&lt;br /&gt;
&lt;br /&gt;
=== Groups API (group) ===&lt;br /&gt;
&lt;br /&gt;
The [[Groups API]] allows you to check the current activity group mode and set the current group.&lt;br /&gt;
&lt;br /&gt;
=== Gradebook API (grade) ===&lt;br /&gt;
&lt;br /&gt;
The [[Gradebook API]] allows you to read and write from the gradebook.  It also allows you to provide an interface for detailed grading information.&lt;br /&gt;
&lt;br /&gt;
=== Plagiarism API (plagiarism) ===&lt;br /&gt;
&lt;br /&gt;
The [[Plagiarism API]] allows your activity module to send files and data to external services to have them checked for plagiarism.&lt;br /&gt;
&lt;br /&gt;
=== Question API (question) ===&lt;br /&gt;
&lt;br /&gt;
The [[Question API]] (which can be divided into the Question bank API and the Question engine API), can be used by activities that want to use questions from the question bank.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Plugins]] - plugin types also have their own APIs&lt;br /&gt;
* [[Coding style]] - general information about writing PHP code for Moodle&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Talk:Coding_style&amp;diff=50957</id>
		<title>Talk:Coding style</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Talk:Coding_style&amp;diff=50957"/>
		<updated>2016-09-30T13:41:32Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;Note: Please avoid using this talk page to propose changes to the coding style. Instead [https://tracker.moodle.org/secure/CreateIssueDetails!init.jspa?pid=10020&amp;amp;issuetype=1&amp;amp;components=12132&amp;amp;summary=coding%20style%20change file a coding style bug]. See the forum discussion [http://moodle.org/mod/forum/discuss.php?d=202406 Using Moodle: Proposing changes to moodle coding style] for more details.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The link on this page to the docblock templates documentation (http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblocktemplate) is no longer valid, but I&#039;m having trouble finding up-to-date documentation of this feature to change it to. Is it possible that it&#039;s an obsolete feature and can be removed from the Coding Style doc page?&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=50272</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=50272"/>
		<updated>2016-05-24T08:49:20Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Add calendar events */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;assignfeedback_file.php&amp;quot;). It should at least define strings for &amp;quot;pluginname&amp;quot;, &amp;quot;enabled&amp;quot; and &amp;quot;enabled_help&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled_help&#039;] = &#039;If enabled, the teacher will be able to provide awesome feedback &#039;; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_feedback_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_feedback_modified(stdClass $grade, stdClass $data) {&lt;br /&gt;
        $commenttext = &#039;&#039;;&lt;br /&gt;
        if ($grade) {&lt;br /&gt;
            $feedbackcomments = $this-&amp;gt;get_feedback_comments($grade-&amp;gt;id);&lt;br /&gt;
            if ($feedbackcomments) {&lt;br /&gt;
                $commenttext = $feedbackcomments-&amp;gt;commenttext;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        if ($commenttext == $data-&amp;gt;assignfeedbackcomments_editor[&#039;text&#039;]) {&lt;br /&gt;
            return false;&lt;br /&gt;
        } else {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The is_feedback_modified function is called before feedback is saved. If feedback has not been modified then the save() method is not called. This function takes the grade object and submitted data from the grading form. In this example we are comparing the existing text comments made with the new ones. This function must return a boolean; True if the feedback has been modified; False if there has been no modification made. If this method is not overwritten then it will default to returning True.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;br /&gt;
&lt;br /&gt;
== Other features ==&lt;br /&gt;
&lt;br /&gt;
=== Add calendar events ===&lt;br /&gt;
&lt;br /&gt;
{{Moodle 3.1}}&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.1 onwards, feedback plugins can add events to the Moodle calendar without side effects. These will be hidden and deleted in line with the assignment module. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        // Add release date to calendar&lt;br /&gt;
        $calendarevent = new stdClass();&lt;br /&gt;
        $calendarevent-&amp;gt;name         = get_string(&#039;calendareventname&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;description  = get_string(&#039;calendareventdesc&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;courseid     = $courseid;&lt;br /&gt;
        $calendarevent-&amp;gt;groupid      = 0;&lt;br /&gt;
        $calendarevent-&amp;gt;userid       = $userid;&lt;br /&gt;
        $calendarevent-&amp;gt;modulename   = &#039;assign&#039;;&lt;br /&gt;
        $calendarevent-&amp;gt;instance     = $instanceid;&lt;br /&gt;
        $calendarevent-&amp;gt;eventtype    = &#039;something_release&#039;; // For activity module&#039;s events, this can be used to set the alternative text of the event icon. Set it to &#039;pluginname&#039; unless you have a better string.&lt;br /&gt;
        $calendarevent-&amp;gt;timestart    = $releasedate;&lt;br /&gt;
        $calendarevent-&amp;gt;visible      = true;&lt;br /&gt;
        $calendarevent-&amp;gt;timeduration = 0;&lt;br /&gt;
&lt;br /&gt;
        calendar_event::create($calendarevent);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50271</id>
		<title>Assign submission plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50271"/>
		<updated>2016-05-24T08:48:59Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Add calendar events */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment submission plugims within the assignment module.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment submission plugin ===&lt;br /&gt;
An assignment submission plugin is used to display custom form fields to a student when they are editing their assignment submission. It also has full control over the display the submitted assignment to graders and students. Plugins participate in all assignment features including backup/restore, upgrades from 2.2, offline grading, group assignments and blind marking.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
File:assignsubmission_plugin_overview1.png|An assignment submission plugin can add settings to the module settings page.&lt;br /&gt;
File:assignsubmission_plugin_overview2.png|An assignment submission plugin can show a summary of the submission to students and graders.&lt;br /&gt;
File:assignsubmission_plugin_overview3.png|An assignment submission plugin can add form fields to the student submission page.&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment submission plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A great example is the &amp;quot;onlinetext&amp;quot; submission plugin included with Moodle core. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom submission plugin sit under &amp;quot;mod/assign/submission/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder. &lt;br /&gt;
&lt;br /&gt;
Note: The plugin name should be no longer than 11 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignsubmission_&amp;quot; + pluginname (17 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignsubmission_&amp;quot; to identify this as a submission plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignsubmission_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All submission settings should be named &#039;assignsubmission_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All submission plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
This example from the submission_file plugin also checks to see if there is a maxbytes setting for this moodle installation and if found, it adds a new admin setting to the settings page. The name of the setting should begin with the plugin component name (&amp;quot;assignsubmission_file&amp;quot;) in this case. The strings are specified in this plugins language file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Note: This is on by default.                                                                                                     &lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignsubmission_file/default&#039;,                                                    &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignsubmission_file&#039;),                                                             &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignsubmission_file&#039;), 1));                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
if (isset($CFG-&amp;gt;maxbytes)) {                                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $name = new lang_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
    $description = new lang_string(&#039;configmaxbytes&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $element = new admin_setting_configselect(&#039;assignsubmission_file/maxbytes&#039;,                                                     &lt;br /&gt;
                                              $name,                                                                                &lt;br /&gt;
                                              $description,                                                                         &lt;br /&gt;
                                              1048576,                                                                              &lt;br /&gt;
                                              get_max_upload_sizes($CFG-&amp;gt;maxbytes));                                                &lt;br /&gt;
    $settings-&amp;gt;add($element);                                                                                                       &lt;br /&gt;
}                                                                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/submission_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;submission_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome submissions&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignsubmission/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_submission_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignsubmission&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/submission/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/submission/file&amp;quot;           &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignsubmission_file&amp;quot; COMMENT=&amp;quot;Info about file submissions for assignments&amp;quot;&amp;gt;                                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files the student submitted.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The unique id for this submission info.&amp;quot;/&amp;gt;                          &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this submission relates to&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;submission&amp;quot; REFTABLE=&amp;quot;assign_submission&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The submission this file submission relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                           &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This example is from the submission_comments plugin. It shows how to run custom code on installation of the plugin. In this case it makes the comments plugin the last of the three submission plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Code run after the plugin database tables have been created.&lt;br /&gt;
 */&lt;br /&gt;
function xmldb_assignsubmission_comments_install() {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    // do the install&lt;br /&gt;
&lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/locallib.php&#039;);&lt;br /&gt;
    // set the correct initial order for the plugins&lt;br /&gt;
    $assignment = new assignment();&lt;br /&gt;
    $plugin = $assignment-&amp;gt;get_submission_plugin_by_type(&#039;comments&#039;);&lt;br /&gt;
    if ($plugin) {&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_submission_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All submission plugins MUST define a class with the component name of the plugin that extends assign_submission_plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_name()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignsubmission_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in submission_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
       global $CFG, $COURSE;                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $defaultmaxfilesubmissions = $this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;);                                                       &lt;br /&gt;
        $defaultmaxsubmissionsizebytes = $this-&amp;gt;get_config(&#039;maxsubmissionsizebytes&#039;);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings = array();                                                                                                        &lt;br /&gt;
        $options = array();                                                                                                         &lt;br /&gt;
        for ($i = 1; $i &amp;lt;= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {                                                                  &lt;br /&gt;
            $options[$i] = $i;                                                                                                      &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maxfilessubmission&#039;, &#039;assignsubmission_file&#039;);                                                          &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxfiles&#039;, $name, $options);                                            &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxfiles&#039;,                                                                     &lt;br /&gt;
                              &#039;maxfilessubmission&#039;,                                                                                 &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxfiles&#039;, $defaultmaxfilesubmissions);                                           &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxfiles&#039;, &#039;assignsubmission_file_enabled&#039;, &#039;notchecked&#039;);                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $choices = get_max_upload_sizes($CFG-&amp;gt;maxbytes,                                                                             &lt;br /&gt;
                                        $COURSE-&amp;gt;maxbytes,                                                                          &lt;br /&gt;
                                        get_config(&#039;assignsubmission_file&#039;, &#039;maxbytes&#039;));                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings[] = array(&#039;type&#039; =&amp;gt; &#039;select&#039;,                                                                                     &lt;br /&gt;
                            &#039;name&#039; =&amp;gt; &#039;maxsubmissionsizebytes&#039;,                                                                     &lt;br /&gt;
                            &#039;description&#039; =&amp;gt; get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;),                          &lt;br /&gt;
                            &#039;options&#039;=&amp;gt; $choices,                                                                                   &lt;br /&gt;
                            &#039;default&#039;=&amp;gt; $defaultmaxsubmissionsizebytes);                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                       &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxsizebytes&#039;, $name, $choices);                                        &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                 &lt;br /&gt;
                              &#039;maximumsubmissionsize&#039;,                                                                              &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxsizebytes&#039;, $defaultmaxsubmissionsizebytes);                                   &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                    &lt;br /&gt;
                           &#039;assignsubmission_file_enabled&#039;,                                                                         &lt;br /&gt;
                           &#039;notchecked&#039;);                             &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings are prefixed by the plugin name which is good practice to avoid conflicts with other plugins. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $data-&amp;gt;assignsubmission_file_maxfiles);                                             &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $data-&amp;gt;assignsubmission_file_maxsizebytes);                                     &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_form_elements()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;) &amp;lt;= 0) {                                                                         &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $submissionid = $submission ? $submission-&amp;gt;id : 0;                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  &#039;files&#039;,                                                                          &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignsubmission_file&#039;,                                                          &lt;br /&gt;
                                                  ASSIGNSUBMISSION_FILE_FILEAREA,                                                   &lt;br /&gt;
                                                  $submissionid);                                                                   &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;files_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                          &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                    &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the submission form. It functions identically to the get_settings function except that the submission object is available (if there is a submission) to associate the settings with a single submission. This example also shows how to use a filemanager within a submission plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $submission, stdClass $data) {                                                                    &lt;br /&gt;
        global $USER, $DB;                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     &#039;files&#039;,                                                                       &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignsubmission_file&#039;,                                                       &lt;br /&gt;
                                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                &lt;br /&gt;
                                                     $submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filesubmission = $this-&amp;gt;get_file_submission($submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Plagiarism code event trigger when files are uploaded.                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Send files to event system.                                                                                              &lt;br /&gt;
        // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).                                &lt;br /&gt;
        $eventdata = new stdClass();                                                                                                &lt;br /&gt;
        $eventdata-&amp;gt;modulename = &#039;assign&#039;;                                                                                          &lt;br /&gt;
        $eventdata-&amp;gt;cmid = $this-&amp;gt;assignment-&amp;gt;get_course_module()-&amp;gt;id;                                                              &lt;br /&gt;
        $eventdata-&amp;gt;itemid = $submission-&amp;gt;id;                                                                                       &lt;br /&gt;
        $eventdata-&amp;gt;courseid = $this-&amp;gt;assignment-&amp;gt;get_course()-&amp;gt;id;                                                                 &lt;br /&gt;
        $eventdata-&amp;gt;userid = $USER-&amp;gt;id;                                                                                             &lt;br /&gt;
        if ($count &amp;gt; 1) {                                                                                                           &lt;br /&gt;
            $eventdata-&amp;gt;files = $files;                                                                                             &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        $eventdata-&amp;gt;file = $files;                                    &lt;br /&gt;
        $eventdata-&amp;gt;pathnamehashes = array_keys($files);                                                                            &lt;br /&gt;
        events_trigger(&#039;assessable_file_uploaded&#039;, $eventdata);                                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($filesubmission) {                                                                                                      &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                    &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filesubmission = new stdClass();                                                                                       &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $submission-&amp;gt;id;                                                                          &lt;br /&gt;
            $filesubmission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                    &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission) &amp;gt; 0;                                                &lt;br /&gt;
        }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a user submission. The parameters are the submission object and the data from the submission form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this submission, it then uses the event api to trigger an assessable_file_uploaded event for the plagiarism api. It then records the number of files in the plugin specific &amp;quot;assignsubmission_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_files()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_files($submission) {&lt;br /&gt;
        $result = array();                                                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;timemodified&#039;,                                                                                &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $result[$file-&amp;gt;get_filename()] = $file;                                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return $result;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this submission plugin produces one or more files, it should implement &amp;quot;get_files&amp;quot; so that the portfolio API can export a list of all the files from all of the plugins for this assignment submission. This is also used by the offline grading feature in the assignment.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view_summary()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $submission, &amp;amp; $showviewlink) {                                                           &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Show we show a link to view all files for this plugin?                                                                   &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;                                                             &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {                                                                      &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                    &lt;br /&gt;
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             &lt;br /&gt;
                                                        $submission-&amp;gt;id);                                                           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignsubmission_file&#039;, $count);                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the submission to both markers and students. It counts the number of files submitted and if it is more that a set number, it only displays a count of how many files are in the submission - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full submission on the submission status page. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view($submission) {&lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                        &lt;br /&gt;
                                                    ASSIGNSUBMISSION_FILE_FILEAREA,                                                 &lt;br /&gt;
                                                    $submission-&amp;gt;id);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire submission to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; can_upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {&lt;br /&gt;
&lt;br /&gt;
        $uploadsingle_type =&#039;uploadsingle&#039;;&lt;br /&gt;
        $upload_type =&#039;upload&#039;;&lt;br /&gt;
&lt;br /&gt;
        if (($type == $uploadsingle_type || $type == $upload_type) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($oldassignment-&amp;gt;assignmenttype == &#039;uploadsingle&#039;) {                                                                     &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, 1);                                                                             &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        } else if ($oldassignment-&amp;gt;assignmenttype == &#039;upload&#039;) {                                                                    &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $oldassignment-&amp;gt;var1);                                                          &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Advanced file upload uses a different setting to do the same thing.                                                  &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;submissiondrafts&#039;,                                                                                      &lt;br /&gt;
                           $oldassignment-&amp;gt;var4,                                                                                    &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Convert advanced file upload &amp;quot;hide description before due date&amp;quot; setting.                                             &lt;br /&gt;
            $alwaysshow = 0;                                                                                                        &lt;br /&gt;
            if (!$oldassignment-&amp;gt;var3) {                                                                                            &lt;br /&gt;
                $alwaysshow = 1;                                                                                                    &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;alwaysshowdescription&#039;,                                                                                 &lt;br /&gt;
                           $alwaysshow,                                                                                             &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                     &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it sets the maxbytes, maxfiles and alwaysshowdescription configuration settings. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade($oldcontext,$oldassignment, $oldsubmission, $submission, &amp;amp; $log) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $file_submission = new stdClass();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        $file_submission-&amp;gt;numfiles = $oldsubmission-&amp;gt;numfiles;&lt;br /&gt;
        $file_submission-&amp;gt;submission = $submission-&amp;gt;id;&lt;br /&gt;
        $file_submission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;&lt;br /&gt;
&lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assign_submission_file&#039;, $file_submission) &amp;gt; 0) {&lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertsubmission&#039;, &#039;mod_assign&#039;, $submission-&amp;gt;userid);&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        // now copy the area files&lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assignment&#039;,&lt;br /&gt;
                                                        &#039;submission&#039;,&lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,&lt;br /&gt;
                                                        // New file area&lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assign&#039;,&lt;br /&gt;
                                                        ASSIGN_FILEAREA_SUBMISSION_FILES,&lt;br /&gt;
                                                        $submission-&amp;gt;id);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_fields()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;    public function () {                                                                                           &lt;br /&gt;
        return array(&#039;onlinetext&#039; =&amp;gt; get_string(&#039;pluginname&#039;, &#039;assignsubmission_comments&#039;));                                        &lt;br /&gt;
    }       &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_fields&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. If a plugin supports multiple text areas it can return the name of each of them here.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_text()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_text($name, $submissionid) {                                                                         &lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {                                                                                                &lt;br /&gt;
            $onlinetextsubmission = $this-&amp;gt;get_onlinetext_submission($submissionid);                                                &lt;br /&gt;
            if ($onlinetextsubmission) {                                                                                            &lt;br /&gt;
                return $onlinetextsubmission-&amp;gt;onlinetext;                                                                           &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }             &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_text&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_format()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_format($name, $submissionid) {&lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {&lt;br /&gt;
            $onlinetext_submission = $this-&amp;gt;get_onlinetext_submission($submissionid);&lt;br /&gt;
            if ($onlinetext_submission) {&lt;br /&gt;
                return $onlinetext_submission-&amp;gt;onlineformat;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext. For the same reason as the previous function, if the plugin uses a text editor, it is ideal if the plugin implements &amp;quot;get_editor_format&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; is_empty()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no submission data to show - it can return true from the is_empty function. This prevents a table row being added to the submission summary for this plugin. It is also used to check if a student has tried to save an assignment with no data.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_file_areas()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNSUBMISSION_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                            &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; copy_submission()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the files across.                                                                                                   &lt;br /&gt;
        $contextid = $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id;                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($contextid,                                                                                    &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $sourcesubmission-&amp;gt;id,                                                                         &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $fieldupdates = array(&#039;itemid&#039; =&amp;gt; $destsubmission-&amp;gt;id);                                                                 &lt;br /&gt;
            $fs-&amp;gt;create_file_from_storedfile($fieldupdates, $file);                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the assignsubmission_file record.                                                                                   &lt;br /&gt;
        if ($filesubmission = $this-&amp;gt;get_file_submission($sourcesubmission-&amp;gt;id)) {                                                  &lt;br /&gt;
            unset($filesubmission-&amp;gt;id);                                                                                             &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $destsubmission-&amp;gt;id;                                                                      &lt;br /&gt;
            $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Moodle 2.5 - a students submission can be copied to create a new submission attempt. Plugins should implement this function if they store data associated with the submission (most plugins).&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; format_for_log()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function (stdClass $submission) {                                                                          &lt;br /&gt;
        // format the info for each submission plugin add_to_log                                                                    &lt;br /&gt;
        $filecount = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                           &lt;br /&gt;
        $fileloginfo = &#039;&#039;;                                                                                                          &lt;br /&gt;
        $fileloginfo .= &#039; the number of file(s) : &#039; . $filecount . &amp;quot; file(s).&amp;lt;br&amp;gt;&amp;quot;;                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return $fileloginfo;                                                                                                        &lt;br /&gt;
    }                      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The format_for_log function lets a plugin produce a really short summary of a submission suitable for adding to a log message. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; delete_instance()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignsubmission_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}                                            &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Useful classes ==&lt;br /&gt;
A submission plugin has access to a number of useful classes in the assignment module. See the phpdocs (or the code) for more information on these classes.&lt;br /&gt;
&lt;br /&gt;
=== assign_plugin ===&lt;br /&gt;
This abstract class is the base class for all assignment plugins (feedback or submission plugins).&lt;br /&gt;
&lt;br /&gt;
It provides access to the assign class which represents the current assignment instance through &amp;quot;$this-&amp;gt;assignment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== assign_submission_plugin ===&lt;br /&gt;
This is the base class all assignment submission plugins must extend. It contains a small number of additional function that only apply to submission plugins. &lt;br /&gt;
&lt;br /&gt;
=== assign ===&lt;br /&gt;
This is the main class for interacting with the assignment module. &lt;br /&gt;
&lt;br /&gt;
It contains public functions that are useful for listing users, loading and updating submissions, loading and updating grades, displaying users etc.&lt;br /&gt;
&lt;br /&gt;
== Other features ==&lt;br /&gt;
&lt;br /&gt;
=== Add calendar events ===&lt;br /&gt;
&lt;br /&gt;
{{Moodle 3.1}}&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.1 onwards, submission plugins can add events to the Moodle calendar without side effects. These will be hidden and deleted in line with the assignment module. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        // Add release date to calendar&lt;br /&gt;
        $calendarevent = new stdClass();&lt;br /&gt;
        $calendarevent-&amp;gt;name         = get_string(&#039;calendareventname&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;description  = get_string(&#039;calendareventdesc&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;courseid     = $courseid;&lt;br /&gt;
        $calendarevent-&amp;gt;groupid      = 0;&lt;br /&gt;
        $calendarevent-&amp;gt;userid       = $userid;&lt;br /&gt;
        $calendarevent-&amp;gt;modulename   = &#039;assign&#039;;&lt;br /&gt;
        $calendarevent-&amp;gt;instance     = $instanceid;&lt;br /&gt;
        $calendarevent-&amp;gt;eventtype    = &#039;something_release&#039;; // For activity module&#039;s events, this can be used to set the alternative text of the event icon. Set it to &#039;pluginname&#039; unless you have a better string.&lt;br /&gt;
        $calendarevent-&amp;gt;timestart    = $releasedate;&lt;br /&gt;
        $calendarevent-&amp;gt;visible      = true;&lt;br /&gt;
        $calendarevent-&amp;gt;timeduration = 0;&lt;br /&gt;
&lt;br /&gt;
        calendar_event::create($calendarevent);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=50246</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=50246"/>
		<updated>2016-05-23T08:42:55Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Document adding of calendar events.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;assignfeedback_file.php&amp;quot;). It should at least define strings for &amp;quot;pluginname&amp;quot;, &amp;quot;enabled&amp;quot; and &amp;quot;enabled_help&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled_help&#039;] = &#039;If enabled, the teacher will be able to provide awesome feedback &#039;; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_feedback_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_feedback_modified(stdClass $grade, stdClass $data) {&lt;br /&gt;
        $commenttext = &#039;&#039;;&lt;br /&gt;
        if ($grade) {&lt;br /&gt;
            $feedbackcomments = $this-&amp;gt;get_feedback_comments($grade-&amp;gt;id);&lt;br /&gt;
            if ($feedbackcomments) {&lt;br /&gt;
                $commenttext = $feedbackcomments-&amp;gt;commenttext;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        if ($commenttext == $data-&amp;gt;assignfeedbackcomments_editor[&#039;text&#039;]) {&lt;br /&gt;
            return false;&lt;br /&gt;
        } else {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The is_feedback_modified function is called before feedback is saved. If feedback has not been modified then the save() method is not called. This function takes the grade object and submitted data from the grading form. In this example we are comparing the existing text comments made with the new ones. This function must return a boolean; True if the feedback has been modified; False if there has been no modification made. If this method is not overwritten then it will default to returning True.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;br /&gt;
&lt;br /&gt;
== Other features ==&lt;br /&gt;
&lt;br /&gt;
=== Add calendar events ===&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.1 onwards, feedback plugins can add events to the Moodle calendar without side effects. These will be hidden and deleted in line with the assignment module. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        // Add release date to calendar&lt;br /&gt;
        $calendarevent = new stdClass();&lt;br /&gt;
        $calendarevent-&amp;gt;name         = get_string(&#039;calendareventname&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;description  = get_string(&#039;calendareventdesc&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;courseid     = $courseid;&lt;br /&gt;
        $calendarevent-&amp;gt;groupid      = 0;&lt;br /&gt;
        $calendarevent-&amp;gt;userid       = $userid;&lt;br /&gt;
        $calendarevent-&amp;gt;modulename   = &#039;assign&#039;;&lt;br /&gt;
        $calendarevent-&amp;gt;instance     = $instanceid;&lt;br /&gt;
        $calendarevent-&amp;gt;eventtype    = &#039;something_release&#039;; // For activity module&#039;s events, this can be used to set the alternative text of the event icon. Set it to &#039;pluginname&#039; unless you have a better string.&lt;br /&gt;
        $calendarevent-&amp;gt;timestart    = $releasedate;&lt;br /&gt;
        $calendarevent-&amp;gt;visible      = true;&lt;br /&gt;
        $calendarevent-&amp;gt;timeduration = 0;&lt;br /&gt;
&lt;br /&gt;
        calendar_event::create($calendarevent);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50245</id>
		<title>Assign submission plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50245"/>
		<updated>2016-05-23T08:33:03Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment submission plugims within the assignment module.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment submission plugin ===&lt;br /&gt;
An assignment submission plugin is used to display custom form fields to a student when they are editing their assignment submission. It also has full control over the display the submitted assignment to graders and students. Plugins participate in all assignment features including backup/restore, upgrades from 2.2, offline grading, group assignments and blind marking.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
File:assignsubmission_plugin_overview1.png|An assignment submission plugin can add settings to the module settings page.&lt;br /&gt;
File:assignsubmission_plugin_overview2.png|An assignment submission plugin can show a summary of the submission to students and graders.&lt;br /&gt;
File:assignsubmission_plugin_overview3.png|An assignment submission plugin can add form fields to the student submission page.&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment submission plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A great example is the &amp;quot;onlinetext&amp;quot; submission plugin included with Moodle core. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom submission plugin sit under &amp;quot;mod/assign/submission/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder. &lt;br /&gt;
&lt;br /&gt;
Note: The plugin name should be no longer than 11 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignsubmission_&amp;quot; + pluginname (17 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignsubmission_&amp;quot; to identify this as a submission plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignsubmission_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All submission settings should be named &#039;assignsubmission_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All submission plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
This example from the submission_file plugin also checks to see if there is a maxbytes setting for this moodle installation and if found, it adds a new admin setting to the settings page. The name of the setting should begin with the plugin component name (&amp;quot;assignsubmission_file&amp;quot;) in this case. The strings are specified in this plugins language file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Note: This is on by default.                                                                                                     &lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignsubmission_file/default&#039;,                                                    &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignsubmission_file&#039;),                                                             &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignsubmission_file&#039;), 1));                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
if (isset($CFG-&amp;gt;maxbytes)) {                                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $name = new lang_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
    $description = new lang_string(&#039;configmaxbytes&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $element = new admin_setting_configselect(&#039;assignsubmission_file/maxbytes&#039;,                                                     &lt;br /&gt;
                                              $name,                                                                                &lt;br /&gt;
                                              $description,                                                                         &lt;br /&gt;
                                              1048576,                                                                              &lt;br /&gt;
                                              get_max_upload_sizes($CFG-&amp;gt;maxbytes));                                                &lt;br /&gt;
    $settings-&amp;gt;add($element);                                                                                                       &lt;br /&gt;
}                                                                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/submission_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;submission_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome submissions&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignsubmission/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_submission_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignsubmission&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/submission/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/submission/file&amp;quot;           &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignsubmission_file&amp;quot; COMMENT=&amp;quot;Info about file submissions for assignments&amp;quot;&amp;gt;                                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files the student submitted.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The unique id for this submission info.&amp;quot;/&amp;gt;                          &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this submission relates to&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;submission&amp;quot; REFTABLE=&amp;quot;assign_submission&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The submission this file submission relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                           &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This example is from the submission_comments plugin. It shows how to run custom code on installation of the plugin. In this case it makes the comments plugin the last of the three submission plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Code run after the plugin database tables have been created.&lt;br /&gt;
 */&lt;br /&gt;
function xmldb_assignsubmission_comments_install() {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    // do the install&lt;br /&gt;
&lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/locallib.php&#039;);&lt;br /&gt;
    // set the correct initial order for the plugins&lt;br /&gt;
    $assignment = new assignment();&lt;br /&gt;
    $plugin = $assignment-&amp;gt;get_submission_plugin_by_type(&#039;comments&#039;);&lt;br /&gt;
    if ($plugin) {&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_submission_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All submission plugins MUST define a class with the component name of the plugin that extends assign_submission_plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_name()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignsubmission_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in submission_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
       global $CFG, $COURSE;                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $defaultmaxfilesubmissions = $this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;);                                                       &lt;br /&gt;
        $defaultmaxsubmissionsizebytes = $this-&amp;gt;get_config(&#039;maxsubmissionsizebytes&#039;);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings = array();                                                                                                        &lt;br /&gt;
        $options = array();                                                                                                         &lt;br /&gt;
        for ($i = 1; $i &amp;lt;= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {                                                                  &lt;br /&gt;
            $options[$i] = $i;                                                                                                      &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maxfilessubmission&#039;, &#039;assignsubmission_file&#039;);                                                          &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxfiles&#039;, $name, $options);                                            &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxfiles&#039;,                                                                     &lt;br /&gt;
                              &#039;maxfilessubmission&#039;,                                                                                 &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxfiles&#039;, $defaultmaxfilesubmissions);                                           &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxfiles&#039;, &#039;assignsubmission_file_enabled&#039;, &#039;notchecked&#039;);                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $choices = get_max_upload_sizes($CFG-&amp;gt;maxbytes,                                                                             &lt;br /&gt;
                                        $COURSE-&amp;gt;maxbytes,                                                                          &lt;br /&gt;
                                        get_config(&#039;assignsubmission_file&#039;, &#039;maxbytes&#039;));                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings[] = array(&#039;type&#039; =&amp;gt; &#039;select&#039;,                                                                                     &lt;br /&gt;
                            &#039;name&#039; =&amp;gt; &#039;maxsubmissionsizebytes&#039;,                                                                     &lt;br /&gt;
                            &#039;description&#039; =&amp;gt; get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;),                          &lt;br /&gt;
                            &#039;options&#039;=&amp;gt; $choices,                                                                                   &lt;br /&gt;
                            &#039;default&#039;=&amp;gt; $defaultmaxsubmissionsizebytes);                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                       &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxsizebytes&#039;, $name, $choices);                                        &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                 &lt;br /&gt;
                              &#039;maximumsubmissionsize&#039;,                                                                              &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxsizebytes&#039;, $defaultmaxsubmissionsizebytes);                                   &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                    &lt;br /&gt;
                           &#039;assignsubmission_file_enabled&#039;,                                                                         &lt;br /&gt;
                           &#039;notchecked&#039;);                             &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings are prefixed by the plugin name which is good practice to avoid conflicts with other plugins. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $data-&amp;gt;assignsubmission_file_maxfiles);                                             &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $data-&amp;gt;assignsubmission_file_maxsizebytes);                                     &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_form_elements()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;) &amp;lt;= 0) {                                                                         &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $submissionid = $submission ? $submission-&amp;gt;id : 0;                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  &#039;files&#039;,                                                                          &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignsubmission_file&#039;,                                                          &lt;br /&gt;
                                                  ASSIGNSUBMISSION_FILE_FILEAREA,                                                   &lt;br /&gt;
                                                  $submissionid);                                                                   &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;files_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                          &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                    &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the submission form. It functions identically to the get_settings function except that the submission object is available (if there is a submission) to associate the settings with a single submission. This example also shows how to use a filemanager within a submission plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $submission, stdClass $data) {                                                                    &lt;br /&gt;
        global $USER, $DB;                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     &#039;files&#039;,                                                                       &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignsubmission_file&#039;,                                                       &lt;br /&gt;
                                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                &lt;br /&gt;
                                                     $submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filesubmission = $this-&amp;gt;get_file_submission($submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Plagiarism code event trigger when files are uploaded.                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Send files to event system.                                                                                              &lt;br /&gt;
        // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).                                &lt;br /&gt;
        $eventdata = new stdClass();                                                                                                &lt;br /&gt;
        $eventdata-&amp;gt;modulename = &#039;assign&#039;;                                                                                          &lt;br /&gt;
        $eventdata-&amp;gt;cmid = $this-&amp;gt;assignment-&amp;gt;get_course_module()-&amp;gt;id;                                                              &lt;br /&gt;
        $eventdata-&amp;gt;itemid = $submission-&amp;gt;id;                                                                                       &lt;br /&gt;
        $eventdata-&amp;gt;courseid = $this-&amp;gt;assignment-&amp;gt;get_course()-&amp;gt;id;                                                                 &lt;br /&gt;
        $eventdata-&amp;gt;userid = $USER-&amp;gt;id;                                                                                             &lt;br /&gt;
        if ($count &amp;gt; 1) {                                                                                                           &lt;br /&gt;
            $eventdata-&amp;gt;files = $files;                                                                                             &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        $eventdata-&amp;gt;file = $files;                                    &lt;br /&gt;
        $eventdata-&amp;gt;pathnamehashes = array_keys($files);                                                                            &lt;br /&gt;
        events_trigger(&#039;assessable_file_uploaded&#039;, $eventdata);                                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($filesubmission) {                                                                                                      &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                    &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filesubmission = new stdClass();                                                                                       &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $submission-&amp;gt;id;                                                                          &lt;br /&gt;
            $filesubmission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                    &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission) &amp;gt; 0;                                                &lt;br /&gt;
        }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a user submission. The parameters are the submission object and the data from the submission form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this submission, it then uses the event api to trigger an assessable_file_uploaded event for the plagiarism api. It then records the number of files in the plugin specific &amp;quot;assignsubmission_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_files()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_files($submission) {&lt;br /&gt;
        $result = array();                                                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;timemodified&#039;,                                                                                &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $result[$file-&amp;gt;get_filename()] = $file;                                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return $result;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this submission plugin produces one or more files, it should implement &amp;quot;get_files&amp;quot; so that the portfolio API can export a list of all the files from all of the plugins for this assignment submission. This is also used by the offline grading feature in the assignment.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view_summary()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $submission, &amp;amp; $showviewlink) {                                                           &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Show we show a link to view all files for this plugin?                                                                   &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;                                                             &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {                                                                      &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                    &lt;br /&gt;
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             &lt;br /&gt;
                                                        $submission-&amp;gt;id);                                                           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignsubmission_file&#039;, $count);                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the submission to both markers and students. It counts the number of files submitted and if it is more that a set number, it only displays a count of how many files are in the submission - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full submission on the submission status page. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view($submission) {&lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                        &lt;br /&gt;
                                                    ASSIGNSUBMISSION_FILE_FILEAREA,                                                 &lt;br /&gt;
                                                    $submission-&amp;gt;id);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire submission to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; can_upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {&lt;br /&gt;
&lt;br /&gt;
        $uploadsingle_type =&#039;uploadsingle&#039;;&lt;br /&gt;
        $upload_type =&#039;upload&#039;;&lt;br /&gt;
&lt;br /&gt;
        if (($type == $uploadsingle_type || $type == $upload_type) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($oldassignment-&amp;gt;assignmenttype == &#039;uploadsingle&#039;) {                                                                     &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, 1);                                                                             &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        } else if ($oldassignment-&amp;gt;assignmenttype == &#039;upload&#039;) {                                                                    &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $oldassignment-&amp;gt;var1);                                                          &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Advanced file upload uses a different setting to do the same thing.                                                  &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;submissiondrafts&#039;,                                                                                      &lt;br /&gt;
                           $oldassignment-&amp;gt;var4,                                                                                    &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Convert advanced file upload &amp;quot;hide description before due date&amp;quot; setting.                                             &lt;br /&gt;
            $alwaysshow = 0;                                                                                                        &lt;br /&gt;
            if (!$oldassignment-&amp;gt;var3) {                                                                                            &lt;br /&gt;
                $alwaysshow = 1;                                                                                                    &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;alwaysshowdescription&#039;,                                                                                 &lt;br /&gt;
                           $alwaysshow,                                                                                             &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                     &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it sets the maxbytes, maxfiles and alwaysshowdescription configuration settings. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade($oldcontext,$oldassignment, $oldsubmission, $submission, &amp;amp; $log) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $file_submission = new stdClass();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        $file_submission-&amp;gt;numfiles = $oldsubmission-&amp;gt;numfiles;&lt;br /&gt;
        $file_submission-&amp;gt;submission = $submission-&amp;gt;id;&lt;br /&gt;
        $file_submission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;&lt;br /&gt;
&lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assign_submission_file&#039;, $file_submission) &amp;gt; 0) {&lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertsubmission&#039;, &#039;mod_assign&#039;, $submission-&amp;gt;userid);&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        // now copy the area files&lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assignment&#039;,&lt;br /&gt;
                                                        &#039;submission&#039;,&lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,&lt;br /&gt;
                                                        // New file area&lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assign&#039;,&lt;br /&gt;
                                                        ASSIGN_FILEAREA_SUBMISSION_FILES,&lt;br /&gt;
                                                        $submission-&amp;gt;id);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_fields()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;    public function () {                                                                                           &lt;br /&gt;
        return array(&#039;onlinetext&#039; =&amp;gt; get_string(&#039;pluginname&#039;, &#039;assignsubmission_comments&#039;));                                        &lt;br /&gt;
    }       &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_fields&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. If a plugin supports multiple text areas it can return the name of each of them here.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_text()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_text($name, $submissionid) {                                                                         &lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {                                                                                                &lt;br /&gt;
            $onlinetextsubmission = $this-&amp;gt;get_onlinetext_submission($submissionid);                                                &lt;br /&gt;
            if ($onlinetextsubmission) {                                                                                            &lt;br /&gt;
                return $onlinetextsubmission-&amp;gt;onlinetext;                                                                           &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }             &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_text&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_format()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_format($name, $submissionid) {&lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {&lt;br /&gt;
            $onlinetext_submission = $this-&amp;gt;get_onlinetext_submission($submissionid);&lt;br /&gt;
            if ($onlinetext_submission) {&lt;br /&gt;
                return $onlinetext_submission-&amp;gt;onlineformat;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext. For the same reason as the previous function, if the plugin uses a text editor, it is ideal if the plugin implements &amp;quot;get_editor_format&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; is_empty()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no submission data to show - it can return true from the is_empty function. This prevents a table row being added to the submission summary for this plugin. It is also used to check if a student has tried to save an assignment with no data.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_file_areas()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNSUBMISSION_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                            &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; copy_submission()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the files across.                                                                                                   &lt;br /&gt;
        $contextid = $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id;                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($contextid,                                                                                    &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $sourcesubmission-&amp;gt;id,                                                                         &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $fieldupdates = array(&#039;itemid&#039; =&amp;gt; $destsubmission-&amp;gt;id);                                                                 &lt;br /&gt;
            $fs-&amp;gt;create_file_from_storedfile($fieldupdates, $file);                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the assignsubmission_file record.                                                                                   &lt;br /&gt;
        if ($filesubmission = $this-&amp;gt;get_file_submission($sourcesubmission-&amp;gt;id)) {                                                  &lt;br /&gt;
            unset($filesubmission-&amp;gt;id);                                                                                             &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $destsubmission-&amp;gt;id;                                                                      &lt;br /&gt;
            $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Moodle 2.5 - a students submission can be copied to create a new submission attempt. Plugins should implement this function if they store data associated with the submission (most plugins).&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; format_for_log()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function (stdClass $submission) {                                                                          &lt;br /&gt;
        // format the info for each submission plugin add_to_log                                                                    &lt;br /&gt;
        $filecount = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                           &lt;br /&gt;
        $fileloginfo = &#039;&#039;;                                                                                                          &lt;br /&gt;
        $fileloginfo .= &#039; the number of file(s) : &#039; . $filecount . &amp;quot; file(s).&amp;lt;br&amp;gt;&amp;quot;;                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return $fileloginfo;                                                                                                        &lt;br /&gt;
    }                      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The format_for_log function lets a plugin produce a really short summary of a submission suitable for adding to a log message. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; delete_instance()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignsubmission_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}                                            &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Useful classes ==&lt;br /&gt;
A submission plugin has access to a number of useful classes in the assignment module. See the phpdocs (or the code) for more information on these classes.&lt;br /&gt;
&lt;br /&gt;
=== assign_plugin ===&lt;br /&gt;
This abstract class is the base class for all assignment plugins (feedback or submission plugins).&lt;br /&gt;
&lt;br /&gt;
It provides access to the assign class which represents the current assignment instance through &amp;quot;$this-&amp;gt;assignment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== assign_submission_plugin ===&lt;br /&gt;
This is the base class all assignment submission plugins must extend. It contains a small number of additional function that only apply to submission plugins. &lt;br /&gt;
&lt;br /&gt;
=== assign ===&lt;br /&gt;
This is the main class for interacting with the assignment module. &lt;br /&gt;
&lt;br /&gt;
It contains public functions that are useful for listing users, loading and updating submissions, loading and updating grades, displaying users etc.&lt;br /&gt;
&lt;br /&gt;
== Other features ==&lt;br /&gt;
&lt;br /&gt;
=== Add calendar events ===&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.1 onwards, submission plugins can add events to the Moodle calendar without side effects. These will be hidden and deleted in line with the assignment module. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        // Add release date to calendar&lt;br /&gt;
        $calendarevent = new stdClass();&lt;br /&gt;
        $calendarevent-&amp;gt;name         = get_string(&#039;calendareventname&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;description  = get_string(&#039;calendareventdesc&#039;, &#039;assignsubmission_something&#039;);&lt;br /&gt;
        $calendarevent-&amp;gt;courseid     = $courseid;&lt;br /&gt;
        $calendarevent-&amp;gt;groupid      = 0;&lt;br /&gt;
        $calendarevent-&amp;gt;userid       = $userid;&lt;br /&gt;
        $calendarevent-&amp;gt;modulename   = &#039;assign&#039;;&lt;br /&gt;
        $calendarevent-&amp;gt;instance     = $instanceid;&lt;br /&gt;
        $calendarevent-&amp;gt;eventtype    = &#039;something_release&#039;; // For activity module&#039;s events, this can be used to set the alternative text of the event icon. Set it to &#039;pluginname&#039; unless you have a better string.&lt;br /&gt;
        $calendarevent-&amp;gt;timestart    = $releasedate;&lt;br /&gt;
        $calendarevent-&amp;gt;visible      = true;&lt;br /&gt;
        $calendarevent-&amp;gt;timeduration = 0;&lt;br /&gt;
&lt;br /&gt;
        calendar_event::create($calendarevent);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50244</id>
		<title>Assign submission plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=50244"/>
		<updated>2016-05-23T08:32:03Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Document adding of calendar events.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment submission plugims within the assignment module.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment submission plugin ===&lt;br /&gt;
An assignment submission plugin is used to display custom form fields to a student when they are editing their assignment submission. It also has full control over the display the submitted assignment to graders and students. Plugins participate in all assignment features including backup/restore, upgrades from 2.2, offline grading, group assignments and blind marking.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
File:assignsubmission_plugin_overview1.png|An assignment submission plugin can add settings to the module settings page.&lt;br /&gt;
File:assignsubmission_plugin_overview2.png|An assignment submission plugin can show a summary of the submission to students and graders.&lt;br /&gt;
File:assignsubmission_plugin_overview3.png|An assignment submission plugin can add form fields to the student submission page.&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment submission plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A great example is the &amp;quot;onlinetext&amp;quot; submission plugin included with Moodle core. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom submission plugin sit under &amp;quot;mod/assign/submission/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder. &lt;br /&gt;
&lt;br /&gt;
Note: The plugin name should be no longer than 11 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignsubmission_&amp;quot; + pluginname (17 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignsubmission_&amp;quot; to identify this as a submission plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignsubmission_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All submission settings should be named &#039;assignsubmission_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All submission plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
This example from the submission_file plugin also checks to see if there is a maxbytes setting for this moodle installation and if found, it adds a new admin setting to the settings page. The name of the setting should begin with the plugin component name (&amp;quot;assignsubmission_file&amp;quot;) in this case. The strings are specified in this plugins language file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Note: This is on by default.                                                                                                     &lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignsubmission_file/default&#039;,                                                    &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignsubmission_file&#039;),                                                             &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignsubmission_file&#039;), 1));                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
if (isset($CFG-&amp;gt;maxbytes)) {                                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $name = new lang_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
    $description = new lang_string(&#039;configmaxbytes&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $element = new admin_setting_configselect(&#039;assignsubmission_file/maxbytes&#039;,                                                     &lt;br /&gt;
                                              $name,                                                                                &lt;br /&gt;
                                              $description,                                                                         &lt;br /&gt;
                                              1048576,                                                                              &lt;br /&gt;
                                              get_max_upload_sizes($CFG-&amp;gt;maxbytes));                                                &lt;br /&gt;
    $settings-&amp;gt;add($element);                                                                                                       &lt;br /&gt;
}                                                                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/submission_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;submission_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome submissions&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignsubmission/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_submission_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignsubmission&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/submission/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/submission/file&amp;quot;           &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignsubmission_file&amp;quot; COMMENT=&amp;quot;Info about file submissions for assignments&amp;quot;&amp;gt;                                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files the student submitted.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The unique id for this submission info.&amp;quot;/&amp;gt;                          &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this submission relates to&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;submission&amp;quot; REFTABLE=&amp;quot;assign_submission&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The submission this file submission relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                           &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This example is from the submission_comments plugin. It shows how to run custom code on installation of the plugin. In this case it makes the comments plugin the last of the three submission plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Code run after the plugin database tables have been created.&lt;br /&gt;
 */&lt;br /&gt;
function xmldb_assignsubmission_comments_install() {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    // do the install&lt;br /&gt;
&lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/locallib.php&#039;);&lt;br /&gt;
    // set the correct initial order for the plugins&lt;br /&gt;
    $assignment = new assignment();&lt;br /&gt;
    $plugin = $assignment-&amp;gt;get_submission_plugin_by_type(&#039;comments&#039;);&lt;br /&gt;
    if ($plugin) {&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_submission_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All submission plugins MUST define a class with the component name of the plugin that extends assign_submission_plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_name()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignsubmission_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in submission_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
       global $CFG, $COURSE;                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $defaultmaxfilesubmissions = $this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;);                                                       &lt;br /&gt;
        $defaultmaxsubmissionsizebytes = $this-&amp;gt;get_config(&#039;maxsubmissionsizebytes&#039;);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings = array();                                                                                                        &lt;br /&gt;
        $options = array();                                                                                                         &lt;br /&gt;
        for ($i = 1; $i &amp;lt;= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {                                                                  &lt;br /&gt;
            $options[$i] = $i;                                                                                                      &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maxfilessubmission&#039;, &#039;assignsubmission_file&#039;);                                                          &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxfiles&#039;, $name, $options);                                            &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxfiles&#039;,                                                                     &lt;br /&gt;
                              &#039;maxfilessubmission&#039;,                                                                                 &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxfiles&#039;, $defaultmaxfilesubmissions);                                           &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxfiles&#039;, &#039;assignsubmission_file_enabled&#039;, &#039;notchecked&#039;);                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $choices = get_max_upload_sizes($CFG-&amp;gt;maxbytes,                                                                             &lt;br /&gt;
                                        $COURSE-&amp;gt;maxbytes,                                                                          &lt;br /&gt;
                                        get_config(&#039;assignsubmission_file&#039;, &#039;maxbytes&#039;));                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings[] = array(&#039;type&#039; =&amp;gt; &#039;select&#039;,                                                                                     &lt;br /&gt;
                            &#039;name&#039; =&amp;gt; &#039;maxsubmissionsizebytes&#039;,                                                                     &lt;br /&gt;
                            &#039;description&#039; =&amp;gt; get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;),                          &lt;br /&gt;
                            &#039;options&#039;=&amp;gt; $choices,                                                                                   &lt;br /&gt;
                            &#039;default&#039;=&amp;gt; $defaultmaxsubmissionsizebytes);                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                       &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxsizebytes&#039;, $name, $choices);                                        &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                 &lt;br /&gt;
                              &#039;maximumsubmissionsize&#039;,                                                                              &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxsizebytes&#039;, $defaultmaxsubmissionsizebytes);                                   &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                    &lt;br /&gt;
                           &#039;assignsubmission_file_enabled&#039;,                                                                         &lt;br /&gt;
                           &#039;notchecked&#039;);                             &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings are prefixed by the plugin name which is good practice to avoid conflicts with other plugins. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $data-&amp;gt;assignsubmission_file_maxfiles);                                             &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $data-&amp;gt;assignsubmission_file_maxsizebytes);                                     &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_form_elements()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;) &amp;lt;= 0) {                                                                         &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $submissionid = $submission ? $submission-&amp;gt;id : 0;                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  &#039;files&#039;,                                                                          &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignsubmission_file&#039;,                                                          &lt;br /&gt;
                                                  ASSIGNSUBMISSION_FILE_FILEAREA,                                                   &lt;br /&gt;
                                                  $submissionid);                                                                   &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;files_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                          &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                    &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the submission form. It functions identically to the get_settings function except that the submission object is available (if there is a submission) to associate the settings with a single submission. This example also shows how to use a filemanager within a submission plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $submission, stdClass $data) {                                                                    &lt;br /&gt;
        global $USER, $DB;                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     &#039;files&#039;,                                                                       &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignsubmission_file&#039;,                                                       &lt;br /&gt;
                                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                &lt;br /&gt;
                                                     $submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filesubmission = $this-&amp;gt;get_file_submission($submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Plagiarism code event trigger when files are uploaded.                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Send files to event system.                                                                                              &lt;br /&gt;
        // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).                                &lt;br /&gt;
        $eventdata = new stdClass();                                                                                                &lt;br /&gt;
        $eventdata-&amp;gt;modulename = &#039;assign&#039;;                                                                                          &lt;br /&gt;
        $eventdata-&amp;gt;cmid = $this-&amp;gt;assignment-&amp;gt;get_course_module()-&amp;gt;id;                                                              &lt;br /&gt;
        $eventdata-&amp;gt;itemid = $submission-&amp;gt;id;                                                                                       &lt;br /&gt;
        $eventdata-&amp;gt;courseid = $this-&amp;gt;assignment-&amp;gt;get_course()-&amp;gt;id;                                                                 &lt;br /&gt;
        $eventdata-&amp;gt;userid = $USER-&amp;gt;id;                                                                                             &lt;br /&gt;
        if ($count &amp;gt; 1) {                                                                                                           &lt;br /&gt;
            $eventdata-&amp;gt;files = $files;                                                                                             &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        $eventdata-&amp;gt;file = $files;                                    &lt;br /&gt;
        $eventdata-&amp;gt;pathnamehashes = array_keys($files);                                                                            &lt;br /&gt;
        events_trigger(&#039;assessable_file_uploaded&#039;, $eventdata);                                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($filesubmission) {                                                                                                      &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                    &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filesubmission = new stdClass();                                                                                       &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $submission-&amp;gt;id;                                                                          &lt;br /&gt;
            $filesubmission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                    &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission) &amp;gt; 0;                                                &lt;br /&gt;
        }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a user submission. The parameters are the submission object and the data from the submission form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this submission, it then uses the event api to trigger an assessable_file_uploaded event for the plagiarism api. It then records the number of files in the plugin specific &amp;quot;assignsubmission_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_files()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_files($submission) {&lt;br /&gt;
        $result = array();                                                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;timemodified&#039;,                                                                                &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $result[$file-&amp;gt;get_filename()] = $file;                                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return $result;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this submission plugin produces one or more files, it should implement &amp;quot;get_files&amp;quot; so that the portfolio API can export a list of all the files from all of the plugins for this assignment submission. This is also used by the offline grading feature in the assignment.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view_summary()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $submission, &amp;amp; $showviewlink) {                                                           &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Show we show a link to view all files for this plugin?                                                                   &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;                                                             &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {                                                                      &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                    &lt;br /&gt;
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             &lt;br /&gt;
                                                        $submission-&amp;gt;id);                                                           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignsubmission_file&#039;, $count);                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the submission to both markers and students. It counts the number of files submitted and if it is more that a set number, it only displays a count of how many files are in the submission - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full submission on the submission status page. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view($submission) {&lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                        &lt;br /&gt;
                                                    ASSIGNSUBMISSION_FILE_FILEAREA,                                                 &lt;br /&gt;
                                                    $submission-&amp;gt;id);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire submission to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; can_upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {&lt;br /&gt;
&lt;br /&gt;
        $uploadsingle_type =&#039;uploadsingle&#039;;&lt;br /&gt;
        $upload_type =&#039;upload&#039;;&lt;br /&gt;
&lt;br /&gt;
        if (($type == $uploadsingle_type || $type == $upload_type) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($oldassignment-&amp;gt;assignmenttype == &#039;uploadsingle&#039;) {                                                                     &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, 1);                                                                             &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        } else if ($oldassignment-&amp;gt;assignmenttype == &#039;upload&#039;) {                                                                    &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $oldassignment-&amp;gt;var1);                                                          &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Advanced file upload uses a different setting to do the same thing.                                                  &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;submissiondrafts&#039;,                                                                                      &lt;br /&gt;
                           $oldassignment-&amp;gt;var4,                                                                                    &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Convert advanced file upload &amp;quot;hide description before due date&amp;quot; setting.                                             &lt;br /&gt;
            $alwaysshow = 0;                                                                                                        &lt;br /&gt;
            if (!$oldassignment-&amp;gt;var3) {                                                                                            &lt;br /&gt;
                $alwaysshow = 1;                                                                                                    &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;alwaysshowdescription&#039;,                                                                                 &lt;br /&gt;
                           $alwaysshow,                                                                                             &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                     &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it sets the maxbytes, maxfiles and alwaysshowdescription configuration settings. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade($oldcontext,$oldassignment, $oldsubmission, $submission, &amp;amp; $log) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $file_submission = new stdClass();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        $file_submission-&amp;gt;numfiles = $oldsubmission-&amp;gt;numfiles;&lt;br /&gt;
        $file_submission-&amp;gt;submission = $submission-&amp;gt;id;&lt;br /&gt;
        $file_submission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;&lt;br /&gt;
&lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assign_submission_file&#039;, $file_submission) &amp;gt; 0) {&lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertsubmission&#039;, &#039;mod_assign&#039;, $submission-&amp;gt;userid);&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        // now copy the area files&lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assignment&#039;,&lt;br /&gt;
                                                        &#039;submission&#039;,&lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,&lt;br /&gt;
                                                        // New file area&lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assign&#039;,&lt;br /&gt;
                                                        ASSIGN_FILEAREA_SUBMISSION_FILES,&lt;br /&gt;
                                                        $submission-&amp;gt;id);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_fields()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;    public function () {                                                                                           &lt;br /&gt;
        return array(&#039;onlinetext&#039; =&amp;gt; get_string(&#039;pluginname&#039;, &#039;assignsubmission_comments&#039;));                                        &lt;br /&gt;
    }       &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_fields&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. If a plugin supports multiple text areas it can return the name of each of them here.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_text()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_text($name, $submissionid) {                                                                         &lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {                                                                                                &lt;br /&gt;
            $onlinetextsubmission = $this-&amp;gt;get_onlinetext_submission($submissionid);                                                &lt;br /&gt;
            if ($onlinetextsubmission) {                                                                                            &lt;br /&gt;
                return $onlinetextsubmission-&amp;gt;onlinetext;                                                                           &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }             &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_text&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_format()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_format($name, $submissionid) {&lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {&lt;br /&gt;
            $onlinetext_submission = $this-&amp;gt;get_onlinetext_submission($submissionid);&lt;br /&gt;
            if ($onlinetext_submission) {&lt;br /&gt;
                return $onlinetext_submission-&amp;gt;onlineformat;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext. For the same reason as the previous function, if the plugin uses a text editor, it is ideal if the plugin implements &amp;quot;get_editor_format&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; is_empty()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no submission data to show - it can return true from the is_empty function. This prevents a table row being added to the submission summary for this plugin. It is also used to check if a student has tried to save an assignment with no data.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_file_areas()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNSUBMISSION_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                            &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; copy_submission()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the files across.                                                                                                   &lt;br /&gt;
        $contextid = $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id;                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($contextid,                                                                                    &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $sourcesubmission-&amp;gt;id,                                                                         &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $fieldupdates = array(&#039;itemid&#039; =&amp;gt; $destsubmission-&amp;gt;id);                                                                 &lt;br /&gt;
            $fs-&amp;gt;create_file_from_storedfile($fieldupdates, $file);                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the assignsubmission_file record.                                                                                   &lt;br /&gt;
        if ($filesubmission = $this-&amp;gt;get_file_submission($sourcesubmission-&amp;gt;id)) {                                                  &lt;br /&gt;
            unset($filesubmission-&amp;gt;id);                                                                                             &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $destsubmission-&amp;gt;id;                                                                      &lt;br /&gt;
            $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Moodle 2.5 - a students submission can be copied to create a new submission attempt. Plugins should implement this function if they store data associated with the submission (most plugins).&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; format_for_log()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function (stdClass $submission) {                                                                          &lt;br /&gt;
        // format the info for each submission plugin add_to_log                                                                    &lt;br /&gt;
        $filecount = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                           &lt;br /&gt;
        $fileloginfo = &#039;&#039;;                                                                                                          &lt;br /&gt;
        $fileloginfo .= &#039; the number of file(s) : &#039; . $filecount . &amp;quot; file(s).&amp;lt;br&amp;gt;&amp;quot;;                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return $fileloginfo;                                                                                                        &lt;br /&gt;
    }                      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The format_for_log function lets a plugin produce a really short summary of a submission suitable for adding to a log message. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; delete_instance()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignsubmission_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}                                            &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Useful classes ==&lt;br /&gt;
A submission plugin has access to a number of useful classes in the assignment module. See the phpdocs (or the code) for more information on these classes.&lt;br /&gt;
&lt;br /&gt;
=== assign_plugin ===&lt;br /&gt;
This abstract class is the base class for all assignment plugins (feedback or submission plugins).&lt;br /&gt;
&lt;br /&gt;
It provides access to the assign class which represents the current assignment instance through &amp;quot;$this-&amp;gt;assignment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== assign_submission_plugin ===&lt;br /&gt;
This is the base class all assignment submission plugins must extend. It contains a small number of additional function that only apply to submission plugins. &lt;br /&gt;
&lt;br /&gt;
=== assign ===&lt;br /&gt;
This is the main class for interacting with the assignment module. &lt;br /&gt;
&lt;br /&gt;
It contains public functions that are useful for listing users, loading and updating submissions, loading and updating grades, displaying users etc.&lt;br /&gt;
&lt;br /&gt;
== Other features ==&lt;br /&gt;
&lt;br /&gt;
=== Add calendar events ===&lt;br /&gt;
&lt;br /&gt;
From Moodle 3.1 onwards, submission plugins can add events to the Moodle calendar without side effects. These will be hidden and deleted in line with the assignment module. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        // Add release date to calendar&lt;br /&gt;
        $calendarevent = new stdClass();&lt;br /&gt;
        $calendarevent-&amp;gt;name         = get_string(&#039;calendareventname&#039;, &#039;assignsubmission_something&#039;, $other[&#039;name&#039;]);&lt;br /&gt;
        $calendarevent-&amp;gt;description  = get_string(&#039;calendareventdesc&#039;, &#039;assignsubmission_something&#039;, $other[&#039;name&#039;]);&lt;br /&gt;
        $calendarevent-&amp;gt;courseid     = $courseid;&lt;br /&gt;
        $calendarevent-&amp;gt;groupid      = 0;&lt;br /&gt;
        $calendarevent-&amp;gt;userid       = $userid;&lt;br /&gt;
        $calendarevent-&amp;gt;modulename   = &#039;assign&#039;;&lt;br /&gt;
        $calendarevent-&amp;gt;instance     = $instanceid;&lt;br /&gt;
        $calendarevent-&amp;gt;eventtype    = &#039;something_release&#039;; // For activity module&#039;s events, this can be used to set the alternative text of the event icon. Set it to &#039;pluginname&#039; unless you have a better string.&lt;br /&gt;
        $calendarevent-&amp;gt;timestart    = $releasedate;&lt;br /&gt;
        $calendarevent-&amp;gt;visible      = true;&lt;br /&gt;
        $calendarevent-&amp;gt;timeduration = 0;&lt;br /&gt;
&lt;br /&gt;
        calendar_event::create($calendarevent);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Activity_modules&amp;diff=50048</id>
		<title>Activity modules</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Activity_modules&amp;diff=50048"/>
		<updated>2016-05-11T14:25:20Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Removing documentation of grade column as required, as it isn&amp;#039;t (several core mods don&amp;#039;t have or need it)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Activity modules reside in the &#039;&#039;&#039;/mod&#039;&#039;&#039; directory.&lt;br /&gt;
&lt;br /&gt;
Each module is in a separate subdirectory and consists of a number of &#039;&#039;&#039;&#039;mandatory files&#039;&#039;&#039;&#039; and any other files the developer is going to use. The below image is an example of the certificate module&#039;s filestructure. Please note, any reference to &amp;lt;modname&amp;gt; in this documentation should be replaced by the name of your module.&lt;br /&gt;
&lt;br /&gt;
Below is an example of the file structure for the certificate plugin.&lt;br /&gt;
&lt;br /&gt;
[[File:Activities_file_structure_example.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Standard Files and their Functions ==&lt;br /&gt;
&lt;br /&gt;
There are several files that are crucial to Moodle. These files are used to install your module and then integrate it into the Moodle system. Each file has a particular function, some of the files are not necessary and are only created when wanting to use the functionality it offers. Below are the list of most commonly used files.&lt;br /&gt;
&lt;br /&gt;
=== Backup Folder ===&lt;br /&gt;
&lt;br /&gt;
This is the location to place the files that define how your module is going to behave when a course backup or restore is performed. You may have added a lot of information to the database for a specific module that you do not wish to enter again when restoring a course, this is where you define what should be saved during a backup and how to then restore this information from the Moodle backup.&lt;br /&gt;
&lt;br /&gt;
To read more on this please visit [[Backup_2.0_for_developers]] and [[Restore_2.0_for_developers]].&lt;br /&gt;
&lt;br /&gt;
=== DB Folder ===&lt;br /&gt;
&lt;br /&gt;
==== access.php ====&lt;br /&gt;
&lt;br /&gt;
This is where you define what capabilities your plugin will create. Note, if you add new capabilities to this file after your plugin has been installed you will need to increase the version number in your version.php file (discussed later) in order for them to be installed.&lt;br /&gt;
&lt;br /&gt;
An example of the file is below -&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
&lt;br /&gt;
    &#039;mod/certificate:addinstance&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.3 a new capability called &#039;addinstance&#039; was added for all core modules. Moodle will look for this capability by searching for &#039;mod/&amp;lt;modname&amp;gt;:addinstance&#039; and if it is not created then it assumed that the user does have this capability with a debug message being displayed that the capability is missing. To avoid this situation it is good to add this to your module.&lt;br /&gt;
&lt;br /&gt;
We also recommend that every activity module also provides a &#039;mod/&amp;lt;modname&amp;gt;:view&#039; capability to control visibility.  (See MDL-40854)&lt;br /&gt;
&lt;br /&gt;
For further information on what each attribute in that capabilities array means visit [[NEWMODULE_Adding_capabilities]].&lt;br /&gt;
&lt;br /&gt;
==== events.php ====&lt;br /&gt;
Setup observer(s) for monitoring events&lt;br /&gt;
&lt;br /&gt;
Once we got the observer in place, now we need to tell Moodle about it. We do this by adding an entry in admin/tool/mylog/db/events.php&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$observers = array(&lt;br /&gt;
    array(&lt;br /&gt;
        &#039;eventname&#039; =&amp;gt; &#039;*&#039;,&lt;br /&gt;
        &#039;callback&#039;  =&amp;gt; &#039;\tool_mylog\log\observer::store&#039;,&lt;br /&gt;
        &#039;internal&#039;  =&amp;gt; false, // This means that we get events only after transaction commit.&lt;br /&gt;
        &#039;priority&#039;  =&amp;gt; 1000,&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(More info about this file can be found on [https://docs.moodle.org/dev/Writing_log_managers#I_am_watching_you])&lt;br /&gt;
&lt;br /&gt;
==== install.xml ====&lt;br /&gt;
&lt;br /&gt;
This file is used on installation of your module; it defines the associated database tables.  For each module, the database must have a main table with the same name as the module itself.  In addition, other database tables may be defined.  In order to create the XML file please view [[XMLDB_editor]]. Please note, in the XML file the table names are listed without the config.php prefix, this is automatically used when creating the tables and does not need to be specified.&lt;br /&gt;
&lt;br /&gt;
An example of the file generated by the [[XMLDB_editor]] is below - it contains the structure of the table certificate and certificate_issues:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;&lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/certificate/db&amp;quot; VERSION=&amp;quot;20120925&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/certificate&amp;quot;&lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../lib/xmldb/xmldb.xsd&amp;quot;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
  &amp;lt;TABLES&amp;gt;&lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;certificate&amp;quot; COMMENT=&amp;quot;Defines certificates&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;course&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;name&amp;quot; TYPE=&amp;quot;char&amp;quot; LENGTH=&amp;quot;255&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;intro&amp;quot; TYPE=&amp;quot;text&amp;quot; LENGTH=&amp;quot;small&amp;quot; NOTNULL=&amp;quot;false&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;introformat&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;4&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;emailteachers&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;1&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;emailothers&amp;quot; TYPE=&amp;quot;text&amp;quot; LENGTH=&amp;quot;small&amp;quot; NOTNULL=&amp;quot;false&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;savecert&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;1&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;reportcert&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;1&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;delivery&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;3&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;requiredtime&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;type&amp;quot; TYPE=&amp;quot;char&amp;quot; LENGTH=&amp;quot;50&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;orientation&amp;quot; TYPE=&amp;quot;char&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;width&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;height&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;backgroundimage&amp;quot; TYPE=&amp;quot;char&amp;quot; LENGTH=&amp;quot;255&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;timecreated&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;timemodified&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;&lt;br /&gt;
      &amp;lt;KEYS&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Primary key for certificate&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;&lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;&lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;certificate_issues&amp;quot; COMMENT=&amp;quot;Info about issued certificates&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;userid&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;certificateid&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;timecreated&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; UNSIGNED=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;&lt;br /&gt;
      &amp;lt;KEYS&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Primary key for certificate_issues&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;certificate&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;certificateid&amp;quot; REFTABLE=&amp;quot;certificate&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;&lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;&lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;&lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are some fields that the Moodle core expects to be present in every activity module&#039;s main table (or just became de-facto standard):&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Field name&lt;br /&gt;
! Properties&lt;br /&gt;
! Notes&lt;br /&gt;
|-&lt;br /&gt;
| id&lt;br /&gt;
| INT(10),  auto sequence&lt;br /&gt;
| primary key for the table (as any other table in Moodle)&lt;br /&gt;
|-&lt;br /&gt;
| course&lt;br /&gt;
| INT(10), foreign key to the &amp;lt;tt&amp;gt;course&amp;lt;/tt&amp;gt; table&lt;br /&gt;
| id of the course this activity is part of&lt;br /&gt;
|-&lt;br /&gt;
| name&lt;br /&gt;
| CHAR(255)&lt;br /&gt;
| holds the name of the activity module instance&lt;br /&gt;
|-&lt;br /&gt;
| timemodified&lt;br /&gt;
| INT(10)&lt;br /&gt;
| timestamp of when the instance was last modified&lt;br /&gt;
|-&lt;br /&gt;
| intro&lt;br /&gt;
| TEXT&lt;br /&gt;
| standard field to hold the activity&#039;s description (see FEATURE_MOD_INTRO)&lt;br /&gt;
|-&lt;br /&gt;
| introformat&lt;br /&gt;
| INT(4)&lt;br /&gt;
| the format of the text in the intro field&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== upgrade.php ====&lt;br /&gt;
&lt;br /&gt;
This file handles upgrading the module to match the latest version. After creating a module and using it extensively on your site (and others) you may want to extend the functionality of your module. Using the certificate example, a suggestion was made that a unique code could be generated for each certificate issued and displayed if a setting was selected. This requires two new database fields, one to store whether the creator of the certificate has chosen to display the code on the certificate and another to store the actual code for each student. This is where the upgrade.php script becomes used. The install.xml file is only executed once, that is when your module is first installed, so adding these two extra columns to this file does not change the database structure for users who have already installed the module. So, to perform this upgrade you need to do three things.&lt;br /&gt;
&lt;br /&gt;
1. Add the new columns to the install.xml file so that any users who install the module after this point are given the new table structure.&amp;lt;br /&amp;gt;&lt;br /&gt;
2. Add the instructions for the upgrade.php files.&amp;lt;br /&amp;gt;&lt;br /&gt;
3. Update the version number in your version.php file.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example we are only adding two new columns to the database, so we can use the [[XMLDB_editor]] to change the install.xml file AND create the upgrade path.&lt;br /&gt;
&lt;br /&gt;
An example of the upgrade.php file is as follows -&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function xmldb_certificate_upgrade($oldversion=0) {&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Add new fields to certificate table.&lt;br /&gt;
        $table = new xmldb_table(&#039;certificate&#039;);&lt;br /&gt;
        $field = new xmldb_field(&#039;showcode&#039;);&lt;br /&gt;
        $field-&amp;gt;set_attributes(XMLDB_TYPE_INTEGER, &#039;1&#039;, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, &#039;0&#039;, &#039;savecert&#039;);&lt;br /&gt;
        if (!$dbman-&amp;gt;field_exists($table, $field)) {&lt;br /&gt;
            $dbman-&amp;gt;add_field($table, $field);&lt;br /&gt;
        }&lt;br /&gt;
        // Add new fields to certificate_issues table.&lt;br /&gt;
        $table = new xmldb_table(&#039;certificate_issues&#039;);&lt;br /&gt;
        $field = new xmldb_field(&#039;code&#039;);&lt;br /&gt;
        $field-&amp;gt;set_attributes(XMLDB_TYPE_CHAR, &#039;50&#039;, null, null, null, null, &#039;certificateid&#039;);&lt;br /&gt;
        if (!$dbman-&amp;gt;field_exists($table, $field)) {&lt;br /&gt;
            $dbman-&amp;gt;add_field($table, $field);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Certificate savepoint reached.&lt;br /&gt;
        upgrade_mod_savepoint(true, 2012091800, &#039;certificate&#039;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you change the version in your version.php module Moodle will look to see if anything needs to be done. The version that is stored in the database is passed to the xmldb_&amp;lt;modname&amp;gt;_upgrade function as the variable $oldversion in this case. In this example lets say that the initial version was 2012091600, since this is less than 2012091800 (the new value we put in the version.php file) we will execute the code in the if statement which will then update the version stored in the database meaning this if statement is never executed again. For more extensive details on this please see [[Upgrade_API]].&lt;br /&gt;
&lt;br /&gt;
=== Lang Folder ===&lt;br /&gt;
&lt;br /&gt;
This is where you store any strings you are going to use in your plugin. Each language has a specific folder that needs to be created in order for it to be used with your module. In this case we are going to use the English language (since unfortunately it is the only one I know). A folder called en is created in your lang folder that contains a file called &amp;lt;modname&amp;gt;.php that lists the translations of your string. For example, your module may have a setting called &#039;User preferences&#039;, rather than hard coding this term in your form, you use a placeholder that will then retrieve the appropriate string depending on the language being used on your Moodle site. There is one mandatory placeholder for modules called &#039;pluginname&#039; that Moodle will use when listing this module as an option to add to a course and other various pages. To keep with Moodle standards you should order your strings alphabetically by the placeholder name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Certificate&#039;;&lt;br /&gt;
$string[&#039;userpreferences&#039;] = &#039;User preferences&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to add the French translation you would create a folder called fr and add the file &amp;lt;modname&amp;gt;.php to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Certificat&#039;;&lt;br /&gt;
$string[&#039;userpreferences&#039;] = &#039;Préférences d\&#039;utilisateur&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when using the string &#039;User preferences&#039; we will use the Moodle function get_string instead, which will get the appropriate string depending on the language being used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
get_string(&#039;userpreferences&#039;, &#039;certificate&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pix folder ===&lt;br /&gt;
&lt;br /&gt;
Here you simply store the icon you wish Moodle to display next to the name of your module. The name of the file should be icon.gif and is displayed with the resolution of 16 * 16. Feel free to use this folder to store other pictures you may use.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update:&#039;&#039;&#039; Since 2.4 SVG files were introduced with all core Moodle activities. Please see [[Moodle_icons_2.4]] for more details on this.&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
The list of functions that are possible to override in the lib.php are listed here [[NEWMODULE_Documentation#lib.php]]. The essential ones are listed below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function certificate_add_instance($certificate);&lt;br /&gt;
function certificate_update_instance($certificate);&lt;br /&gt;
function certificate_delete_instance($id);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;modname&amp;gt;_add_instance function is passed the variables from the mod_form.php file (discussed later) as an object when you first create an activity and click submit. This is where you can take that data, do what you want with it and then insert it into the database if you wish. This is only called once when the module instance is first created, so this is where you should place the logic to add the activity.&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
The &amp;lt;modname&amp;gt;_update_instance function is passed the variables from the mod_form.php file as an object whenever you update an activity and click submit. The id of the instance you are editing is passed as the attribute instance and can be used to edit any existing values in the database for that instance.&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
The &amp;lt;modname&amp;gt;_delete_instance function is passed the id of your module which you can use to delete the records from any database tables associated with that id. For example, in the certificate module the id in the certificate table is passed, and then used to delete the certificate from the database, any issues of this certificate and any files associated with it on the filesystem.&lt;br /&gt;
&lt;br /&gt;
=== mod_form.php ===&lt;br /&gt;
&lt;br /&gt;
This file is used when adding/editing a module to a course. It contains the elements that will be displayed on the form responsible for creating/installing an instance of your module. The class in the file should be called mod_&amp;lt;modname&amp;gt;_mod_form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (!defined(&#039;MOODLE_INTERNAL&#039;)) {&lt;br /&gt;
    die(&#039;Direct access to this script is forbidden.&#039;);    ///  It must be included from a Moodle page&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
require_once($CFG-&amp;gt;dirroot.&#039;/course/moodleform_mod.php&#039;);&lt;br /&gt;
require_once($CFG-&amp;gt;dirroot.&#039;/mod/certificate/lib.php&#039;);&lt;br /&gt;
&lt;br /&gt;
class mod_certificate_mod_form extends moodleform_mod {&lt;br /&gt;
&lt;br /&gt;
    function definition() {&lt;br /&gt;
        global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
        $mform =&amp;amp; $this-&amp;gt;_form;&lt;br /&gt;
&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;text&#039;, &#039;name&#039;, get_string(&#039;certificatename&#039;, &#039;certificate&#039;), array(&#039;size&#039;=&amp;gt;&#039;64&#039;));&lt;br /&gt;
        $mform-&amp;gt;setType(&#039;name&#039;, PARAM_TEXT);&lt;br /&gt;
        $mform-&amp;gt;addRule(&#039;name&#039;, null, &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
&lt;br /&gt;
        $ynoptions = array(0 =&amp;gt; get_string(&#039;no&#039;),&lt;br /&gt;
                           1 =&amp;gt; get_string(&#039;yes&#039;));&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;usecode&#039;, get_string(&#039;usecode&#039;, &#039;certificate&#039;), $ynoptions);&lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;usecode&#039;, 0);&lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;usecode&#039;, &#039;usecode&#039;, &#039;certificate&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;standard_coursemodule_elements();&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;add_action_buttons();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example does not contain the full file, just enough to provide you with an idea. First we create a text element called &#039;name&#039; that is required, this is obviously the name of the instance. I then created another element that stores whether a user wishes to display the unique code issued to a user when they receive the certificate or not with a default value of 0 and a help button explaining what this setting does. The function standard_coursemodule_elements adds the elements common to all modules, such as the conditional fields. The add_action_buttons function adds the submit and cancel buttons to the form. This data will be passed to either &amp;lt;modname&amp;gt;_add_instance or &amp;lt;modname&amp;gt;_update_instance depending on whether you are adding a new instance or updating a current one. You can also add validation to this form, just like any other form in Moodle. For more information on how to create forms in Moodle see [[Form_API]].&lt;br /&gt;
&lt;br /&gt;
=== index.php ===&lt;br /&gt;
&lt;br /&gt;
This page is used by Moodle when listing all the instances of your module that are in a particular course with the course id being passed to this script. The beginning of the page should contain the following -&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&#039;../../config.php&#039;);&lt;br /&gt;
&lt;br /&gt;
$id = required_param(&#039;id&#039;, PARAM_INT);           // Course ID&lt;br /&gt;
&lt;br /&gt;
// Ensure that the course specified is valid&lt;br /&gt;
if (!$course = $DB-&amp;gt;get_record(&#039;course&#039;, array(&#039;id&#039;=&amp;gt; $id))) {&lt;br /&gt;
    print_error(&#039;Course ID is incorrect&#039;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You are then free to display the list of instances as you wish.&lt;br /&gt;
&lt;br /&gt;
=== view.php ===&lt;br /&gt;
&lt;br /&gt;
When a course renders its page layout and activities it generates the links to view them using the view.php script, so the links will look like &amp;lt;wwwrootofyoursite&amp;gt;/mod/&amp;lt;modname&amp;gt;/view.php?id=4, where 4 is the course module id. For the certificate example the beginning of the view.php page looks like the following -&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&#039;../../config.php&#039;);&lt;br /&gt;
require_once(&#039;lib.php&#039;);&lt;br /&gt;
&lt;br /&gt;
$id = required_param(&#039;id&#039;, PARAM_INT);    // Course Module ID&lt;br /&gt;
&lt;br /&gt;
if (!$cm = get_coursemodule_from_id(&#039;certificate&#039;, $id)) {&lt;br /&gt;
    print_error(&#039;Course Module ID was incorrect&#039;); // NOTE this is invalid use of print_error, must be a lang string id&lt;br /&gt;
}&lt;br /&gt;
if (!$course = $DB-&amp;gt;get_record(&#039;course&#039;, array(&#039;id&#039;=&amp;gt; $cm-&amp;gt;course))) {&lt;br /&gt;
    print_error(&#039;course is misconfigured&#039;);  // NOTE As above&lt;br /&gt;
}&lt;br /&gt;
if (!$certificate = $DB-&amp;gt;get_record(&#039;certificate&#039;, array(&#039;id&#039;=&amp;gt; $cm-&amp;gt;instance))) {&lt;br /&gt;
    print_error(&#039;course module is incorrect&#039;); // NOTE As above&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The course module id is passed to this script and set as $id, which is then used to retrieve the course data and the information from the certificate table which we can use later on as we wish.&lt;br /&gt;
&lt;br /&gt;
If you are using Moodle 2.8 and above, there is a better way to achieve most of this using a new function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require(&#039;../../config.php&#039;);&lt;br /&gt;
require_once(&#039;lib.php&#039;);&lt;br /&gt;
&lt;br /&gt;
$id = required_param(&#039;id&#039;, PARAM_INT);&lt;br /&gt;
list ($course, $cm) = get_course_and_cm_from_cmid($id, &#039;certificate&#039;);&lt;br /&gt;
$certificate = $DB-&amp;gt;get_record(&#039;certificate&#039;, array(&#039;id&#039;=&amp;gt; $cm-&amp;gt;instance), &#039;*&#039;, MUST_EXIST);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
The version.php file keeps track of the version of your module, and other attributes, such as what version of Moodle it requires. For a full list of the attributes please see [[version.php]].&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[NEWMODULE_Documentation]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=49758</id>
		<title>Adding a web service to a plugin</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Adding_a_web_service_to_a_plugin&amp;diff=49758"/>
		<updated>2016-04-04T09:19:50Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* create_groups_return() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle_2.0}}&lt;br /&gt;
&lt;br /&gt;
= Quick start =&lt;br /&gt;
== Example ==&lt;br /&gt;
Have a look to the [https://github.com/moodlehq/moodle-local_wstemplate web service plugin template]. This plugin contains a web service hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.&lt;br /&gt;
== File structure ==&lt;br /&gt;
The file structure is explained in these two documents:&lt;br /&gt;
* [[Web_services_API|Web services API]]&lt;br /&gt;
* [[External_functions_API|External function API]]&lt;br /&gt;
&lt;br /&gt;
= Tutorial =&lt;br /&gt;
We will create a web service into a local plugin. This service will contain one &#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; web service function. This web service function will create a group into a Moodle course.&lt;br /&gt;
&lt;br /&gt;
== Write the specification documentation ==&lt;br /&gt;
Before starting coding, let&#039;s identify our needs writing some short specification documents.&lt;br /&gt;
&lt;br /&gt;
=== functional specification===&lt;br /&gt;
&#039;&#039;local_myplugin_create_groups($groups)&#039;&#039; will take a list of groups as parameters and it will return the same groups with their newly created id. If ever one group creation fails, the function will throw an exception, and no creation will happen.&lt;br /&gt;
&lt;br /&gt;
=== technical specification ===&lt;br /&gt;
* &#039;&#039;&#039;the core function the external function will call&#039;&#039;&#039;:  &#039;&#039;groups_create_group()&#039;&#039; from [http://cvs.moodle.org/moodle/group/ /group/lib.php].&lt;br /&gt;
* &#039;&#039;&#039;the parameter types&#039;&#039;&#039;: a list of object. This object are groups, with id/name/courseid.&lt;br /&gt;
* &#039;&#039;&#039;the returned value types&#039;&#039;&#039;:  a list of objects (groups) with their id.&lt;br /&gt;
* &#039;&#039;&#039;the user capabilities to check&#039;&#039;&#039;: &#039;&#039;moodle/course:managegroups&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Write a simple test client ==&lt;br /&gt;
The first thing you should code is a web service test client. You will often discover use cases that you didn&#039;t think about. We are not showing any test client code here, see [[Creating_a_web_service_client|How to create a web service client]].&lt;br /&gt;
&lt;br /&gt;
== Declare the service ==&lt;br /&gt;
This step is optional. You can pre-build a service including any web service functions, so the Moodle administrator doesn&#039;t need to do it. Add into /local/myplugin/db/services.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
  $services = array(&lt;br /&gt;
      &#039;mypluginservice&#039; =&amp;gt; array(                                                //the name of the web service&lt;br /&gt;
          &#039;functions&#039; =&amp;gt; array (&#039;local_myplugin_create_groups&#039;), //web service functions of this service&lt;br /&gt;
          &#039;requiredcapability&#039; =&amp;gt; &#039;&#039;,                //if set, the web service user need this capability to access &lt;br /&gt;
                                                                              //any function of this service. For example: &#039;some/capability:specified&#039;                 &lt;br /&gt;
          &#039;restrictedusers&#039; =&amp;gt;0,                                             //if enabled, the Moodle administrator must link some user to this service&lt;br /&gt;
                                                                              //into the administration&lt;br /&gt;
          &#039;enabled&#039;=&amp;gt;1,                                                       //if enabled, the service can be reachable on a default installation&lt;br /&gt;
       )&lt;br /&gt;
  );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: it is not possible for an administrator to add/remove any function from a pre-built service.&lt;br /&gt;
&lt;br /&gt;
== Declare the web service function ==&lt;br /&gt;
Following the [[Web_services_API|Web service API]], you must declare the web service function in the &#039;&#039;local/myplugin/db/services.php&#039;&#039; file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$functions = array(&lt;br /&gt;
    &#039;local_myplugin_create_groups&#039; =&amp;gt; array(         //web service function name&lt;br /&gt;
        &#039;classname&#039;   =&amp;gt; &#039;local_myplugin_external&#039;,  //class containing the external function&lt;br /&gt;
        &#039;methodname&#039;  =&amp;gt; &#039;create_groups&#039;,          //external function name&lt;br /&gt;
        &#039;classpath&#039;   =&amp;gt; &#039;local/myplugin/externallib.php&#039;,  //file containing the class/external function&lt;br /&gt;
        &#039;description&#039; =&amp;gt; &#039;Creates new groups.&#039;,    //human readable description of the web service function&lt;br /&gt;
        &#039;type&#039;        =&amp;gt; &#039;write&#039;,                  //database rights of the web service function (read, write)&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Web service functions should match the [https://docs.moodle.org/dev/Web_services_Roadmap#Naming_convention naming convention].&lt;br /&gt;
&lt;br /&gt;
== Write the external function descriptions ==&lt;br /&gt;
Every web service function is mapped to an external function. External function are described in the [[External_functions_API|External functions API]].&lt;br /&gt;
Each external function is written with two other functions describing the parameters and the return values. These description functions are used by web service servers to:&lt;br /&gt;
* validate the web service function parameters&lt;br /&gt;
* validate the web service function returned values&lt;br /&gt;
* build WSDL files or other protocol documents &lt;br /&gt;
&lt;br /&gt;
These two description functions are located in the same file and the same class mentioned in local/myplugin/db/services.php.&lt;br /&gt;
&lt;br /&gt;
Thus for the web service function &#039;&#039;&#039;local_myplugin_create_groups()&#039;&#039;&#039;, we need write a class named &#039;&#039;&#039;local_myplugin_external&#039;&#039;&#039; in the file &#039;&#039;&#039;local/myplugin/externallib.php&#039;&#039;&#039;. The class will contain:&lt;br /&gt;
* create_groups(...)&lt;br /&gt;
* create_groups_parameters()&lt;br /&gt;
* create_groups_return()&lt;br /&gt;
&lt;br /&gt;
=== create_groups_parameters() ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&amp;quot;$CFG-&amp;gt;libdir/externallib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
class local_myplugin_external extends external_api {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A web service function without parameters will have a parameter description function like that:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function functionname_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
               //if I had any parameters, they would be described here. But I don&#039;t have any, so this array is empty.&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A parameter can be described as: &lt;br /&gt;
* a list               =&amp;gt; external_multiple_structure&lt;br /&gt;
* an object         =&amp;gt; external_single_structure&lt;br /&gt;
* a primary type =&amp;gt; external_value&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Our create_groups() function expects one parameter named &#039;&#039;groups&#039;&#039;, so we will first write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns description of method parameters&lt;br /&gt;
     * @return external_function_parameters&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups_parameters() {&lt;br /&gt;
        return new external_function_parameters(&lt;br /&gt;
            array(&lt;br /&gt;
                &#039;groups&#039; =&amp;gt; ...&lt;br /&gt;
                &lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This &#039;&#039;groups&#039;&#039; parameter is a list of group. So we will write :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    ...&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An external_multiple_structure object (list) can be constructed with: &lt;br /&gt;
* &#039;&#039;external_multiple_structure&#039;&#039; (list)&lt;br /&gt;
* &#039;&#039;external_single_structure&#039;&#039; (object)&lt;br /&gt;
* &#039;&#039;external_value&#039;&#039; (primary type). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For our function it will be a &#039;&#039;external_single_structure&#039;&#039;: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;             &lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )           &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we obtain :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; ...,&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; ...,&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each group values is a &#039;&#039;external_value&#039;&#039; (primary type):&lt;br /&gt;
* &#039;&#039;courseid&#039;&#039; is an integer&lt;br /&gt;
* &#039;&#039;name&#039;&#039; is a string (text only, not tag)&lt;br /&gt;
* &#039;&#039;description&#039;&#039; is a string (can be anything)&lt;br /&gt;
* &#039;&#039;enrolmentkey&#039;&#039; is also a string (can be anything) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We add them to the description :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
                &#039;groups&#039; =&amp;gt; new external_multiple_structure(&lt;br /&gt;
                    new external_single_structure(&lt;br /&gt;
                        array(&lt;br /&gt;
                            &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;), //the second argument is a human readable description text. This text is displayed in the automatically generated documentation.&lt;br /&gt;
                            &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                            &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                            &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                        )&lt;br /&gt;
                    )&lt;br /&gt;
                )          &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== create_groups_returns() ===&lt;br /&gt;
&lt;br /&gt;
It&#039;s similar to create_groups_parameters(), but instead of describing the parameters, it describes the return values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public static function create_groups_returns() {&lt;br /&gt;
        return new external_multiple_structure(&lt;br /&gt;
            new external_single_structure(&lt;br /&gt;
                array(&lt;br /&gt;
                    &#039;id&#039; =&amp;gt; new external_value(PARAM_INT, &#039;group record id&#039;),&lt;br /&gt;
                    &#039;courseid&#039; =&amp;gt; new external_value(PARAM_INT, &#039;id of course&#039;),&lt;br /&gt;
                    &#039;name&#039; =&amp;gt; new external_value(PARAM_TEXT, &#039;multilang compatible name, course unique&#039;),&lt;br /&gt;
                    &#039;description&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group description text&#039;),&lt;br /&gt;
                    &#039;enrolmentkey&#039; =&amp;gt; new external_value(PARAM_RAW, &#039;group enrol secret phrase&#039;),&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Required, Optional or Default value ===&lt;br /&gt;
A value can be VALUE_REQUIRED, VALUE_OPTIONAL, or VALUE_DEFAULT. If not mentioned, a value is VALUE_REQUIRED by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;                 &lt;br /&gt;
                            &#039;yearofstudy&#039; =&amp;gt; new external_value(PARAM_INT, &#039;year of study&#039;,VALUE_DEFAULT, 1979),                        &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* VALUE_REQUIRED - if the value is not supplied =&amp;gt; the server throws an error message&lt;br /&gt;
* VALUE_OPTIONAL - if the value is not supplied =&amp;gt; the value is ignored. Note that VALUE_OPTIONAL can&#039;t be used in parameters, it must be used only in array/objects key definition. If you need Optional parameters you should use VALUE_DEFAULT instead.&lt;br /&gt;
* VALUE_DEFAULT   - if the value is not supplied =&amp;gt; the default value is used&lt;br /&gt;
&lt;br /&gt;
== Implement the external function ==&lt;br /&gt;
We declared our web service function and we defined the external function parameters and return values. We will now implement the external function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Create groups&lt;br /&gt;
     * @param array $groups array of group description arrays (with keys groupname and courseid)&lt;br /&gt;
     * @return array of newly created groups&lt;br /&gt;
     */&lt;br /&gt;
    public static function create_groups($groups) { //Don&#039;t forget to set it as static&lt;br /&gt;
        global $CFG, $DB;&lt;br /&gt;
        require_once(&amp;quot;$CFG-&amp;gt;dirroot/group/lib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&lt;br /&gt;
        $transaction = $DB-&amp;gt;start_delegated_transaction(); //If an exception is thrown in the below code, all DB queries in this code will be rollback.&lt;br /&gt;
&lt;br /&gt;
        $groups = array();&lt;br /&gt;
&lt;br /&gt;
        foreach ($params[&#039;groups&#039;] as $group) {&lt;br /&gt;
            $group = (object)$group;&lt;br /&gt;
&lt;br /&gt;
            if (trim($group-&amp;gt;name) == &#039;&#039;) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Invalid group name&#039;);&lt;br /&gt;
            }&lt;br /&gt;
            if ($DB-&amp;gt;get_record(&#039;groups&#039;, array(&#039;courseid&#039;=&amp;gt;$group-&amp;gt;courseid, &#039;name&#039;=&amp;gt;$group-&amp;gt;name))) {&lt;br /&gt;
                throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            // now security checks&lt;br /&gt;
            $context = get_context_instance(CONTEXT_COURSE, $group-&amp;gt;courseid);&lt;br /&gt;
            self::validate_context($context);&lt;br /&gt;
            require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&lt;br /&gt;
            // finally create the group&lt;br /&gt;
            $group-&amp;gt;id = groups_create_group($group, false);&lt;br /&gt;
            $groups[] = (array)$group;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $transaction-&amp;gt;allow_commit();&lt;br /&gt;
&lt;br /&gt;
        return $groups;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parameter validation ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $params = self::validate_parameters(self::create_groups_parameters(), array(&#039;groups&#039;=&amp;gt;$groups));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This &#039;&#039;validate_parameters&#039;&#039; function validates the external function parameters against the description. It will return an exception if some required parameters are missing, if parameters are not well-formed, and check the parameters validity. It is essential that you do this call to avoid potential hack. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important:&#039;&#039;&#039; the parameters of the external function and their declaration in the description &#039;&#039;&#039;must be the same order&#039;&#039;&#039;. In this example we have only one parameter named $groups, so we don&#039;t need to worry about the order.&lt;br /&gt;
&lt;br /&gt;
=== Context and Capability checks ===&lt;br /&gt;
&lt;br /&gt;
In a long term, in Moodle 2.0, all context/capability checks will disappear from the core functions and will be located into the external functions. Don&#039;t forget them.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/// now security checks&lt;br /&gt;
$context = get_context_instance(CONTEXT_COURSE, $group-&amp;gt;courseid);&lt;br /&gt;
self::validate_context($context);&lt;br /&gt;
require_capability(&#039;moodle/course:managegroups&#039;, $context);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Exceptions===&lt;br /&gt;
You can throw exceptions. There are automatically handle by Moodle web service servers.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
//Note: it is good practice to add detailled information in $debuginfo, &lt;br /&gt;
//         and only send back a generic exception message when Moodle DEBUG mode &amp;lt; NORMAL.&lt;br /&gt;
//         It&#039;s what we do here throwing the invalid_parameter_exception($debug) exception&lt;br /&gt;
throw new invalid_parameter_exception(&#039;Group with the same name already exists in the course&#039;); &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Correct return values ===&lt;br /&gt;
The return values will be validated by the Moodle web service servers: &lt;br /&gt;
* return values contain some values not described =&amp;gt; these values will be skipped.&lt;br /&gt;
* return values miss some required values (VALUE_REQUIRED) =&amp;gt; the server will return an error.&lt;br /&gt;
* return values types don&#039;t match the description (int != PARAM_ALPHA) =&amp;gt; the server will return an error&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; cast all your returned objects into arrays.&lt;br /&gt;
&lt;br /&gt;
== Making web service accessible through Apache Thrift ==&lt;br /&gt;
This step is optional. If you wish to generate SDK for different programming languages and platforms using [http://thrift.apache.org Apache Thrift framework] then [https://bitbucket.org/hhteam/moodle_thrift_tools/wiki/Home Moodle Thrift tools] can help you. &lt;br /&gt;
&lt;br /&gt;
Two steps should be performed: &lt;br /&gt;
&lt;br /&gt;
* Generate .thrift files for Moodle API using thriftgenerator script.&lt;br /&gt;
* Generate thrift handlers for PHP and copy the php files generated to your plugin source tree.&lt;br /&gt;
&lt;br /&gt;
It is also recommended to include thrift files into the distribution of your plugin in order to simplify creation of client bindings for the users of your API.&lt;br /&gt;
&lt;br /&gt;
That&#039;s it. Now the web API of your plugin is accessible through Apache Thrift Framework.&lt;br /&gt;
&lt;br /&gt;
== Bump the plugin version ==&lt;br /&gt;
Edit your local/myplugin/version.php and increase the plugin version. This should trigger a Moodle upgrade and the new web service should be available in the administration (&#039;&#039;Administration &amp;gt; Plugins &amp;gt; Web Services &amp;gt; Manage services&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
=See also=&lt;br /&gt;
* [[Web_services|Web services developer documentation]]&lt;br /&gt;
* [[:en:Web_services|Web services user documentation]]&lt;br /&gt;
* [[Creating_a_web_service_client|Implement a web service client]]&lt;br /&gt;
&lt;br /&gt;
Specification:&lt;br /&gt;
* [[External services security]]&lt;br /&gt;
* [[External services description]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Web Services]]&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Javascript_Modules&amp;diff=49470</id>
		<title>Javascript Modules</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Javascript_Modules&amp;diff=49470"/>
		<updated>2016-02-15T16:12:48Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Install grunt */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.9}}&lt;br /&gt;
&lt;br /&gt;
= Javascript Modules =&lt;br /&gt;
&lt;br /&gt;
== What is a Javascript module and why do I care? ==&lt;br /&gt;
&lt;br /&gt;
A Javascript module is nothing more than a collection of Javascript code that can be used (reliably) from other pieces of Javascript. &lt;br /&gt;
&lt;br /&gt;
== Why should I package my code as a module? ==&lt;br /&gt;
&lt;br /&gt;
By packaging your code as a module you break your code up into smaller reusable pieces. This is good because:&lt;br /&gt;
&lt;br /&gt;
a) Each smaller piece is simpler to understand / debug&lt;br /&gt;
&lt;br /&gt;
b) Each smaller piece is simpler to test&lt;br /&gt;
&lt;br /&gt;
c) You can re-use common code instead of duplicating it&lt;br /&gt;
&lt;br /&gt;
= How do I write a Javascript module in Moodle? =&lt;br /&gt;
&lt;br /&gt;
Since version 2.9, Moodle supports Javascript modules written using the Asynchronous Module Definition ([https://github.com/amdjs/amdjs-api/wiki/AMD AMD]) API. This is a standard API for creating Javascript modules and you will find many useful third party libraries that are already using this format. &lt;br /&gt;
&lt;br /&gt;
To edit or create an AMD module in Moodle you need to do a couple of things. &lt;br /&gt;
&lt;br /&gt;
== Install grunt ==&lt;br /&gt;
&lt;br /&gt;
The AMD modules in Moodle must be processed by some build tools before they will be visible to your web browser. We use &amp;quot;[http://gruntjs.com/ grunt]&amp;quot; as a build tool to wrap our different processes. Grunt is a build tool written in Javascript that runs in the &amp;quot;[http://nodejs.org/ nodejs]&amp;quot; environment.&lt;br /&gt;
&lt;br /&gt;
This means you first have to &#039;&#039;&#039;install nodejs&#039;&#039;&#039; - and its package manager [https://www.npmjs.com/ npm]. The details of how to install those packages will vary by operating system, but on Linux it&#039;s probably similar to &amp;quot;sudo apt-get install nodejs npm&amp;quot;. There are downloadable packages for other operating systems here: http://nodejs.org/download/.&lt;br /&gt;
&lt;br /&gt;
Once this is done, you can &#039;&#039;&#039;run the command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
 npm install&lt;br /&gt;
 npm install -g grunt-cli&lt;br /&gt;
&lt;br /&gt;
from the top of the Moodle directory to install all of the required tools. (You may need extra permissions to use the -g option.)&lt;br /&gt;
&lt;br /&gt;
== Running grunt ==&lt;br /&gt;
&lt;br /&gt;
See [[Grunt#Running_grunt]] for more details of specific grunt commands which can be used.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;Hello World&amp;quot; I am a Javascript Module ==&lt;br /&gt;
Lets now create a simple Javascript module so we can see how to lay things out. &lt;br /&gt;
&lt;br /&gt;
Each Javascript module is contained in a single source file in the &amp;lt;componentdir&amp;gt;/amd/src folder. The final name of the module is taken from the file name and the component name. E.g. block_overview/amd/src/helloworld.js would be a module named &amp;quot;block_overview/helloworld&amp;quot;. the name of the module is important when you want to call it from somewhere else in the code. &lt;br /&gt;
&lt;br /&gt;
After running grunt - the minified Javascript files are stored in the &amp;lt;componentdir&amp;gt;/amd/build folder. The javascript files are renamed to show that they are minified (helloworld.js becomes helloworld.min.js). &lt;br /&gt;
&lt;br /&gt;
Don&#039;t forget to add the built files (the ones in amd/build) to your git commits, or in production no-one will see your changes. &lt;br /&gt;
&lt;br /&gt;
Lets create a simple module now:&lt;br /&gt;
&lt;br /&gt;
blocks/overview/amd/src/helloworld.js&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// Standard license block omitted.&lt;br /&gt;
/*&lt;br /&gt;
 * @package    block_overview&lt;br /&gt;
 * @copyright  2015 Someone cool&lt;br /&gt;
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
  * @module block_overview/helloworld&lt;br /&gt;
  */&lt;br /&gt;
define([&#039;jquery&#039;], function($) {&lt;br /&gt;
&lt;br /&gt;
     /** &lt;br /&gt;
      * Give me blue.&lt;br /&gt;
      * @access private&lt;br /&gt;
      * @return {string}&lt;br /&gt;
      */&lt;br /&gt;
     var makeItBlue = function() {&lt;br /&gt;
          // We can use our jquery dependency here.&lt;br /&gt;
          return $(&#039;.blue&#039;).show();&lt;br /&gt;
     };&lt;br /&gt;
      &lt;br /&gt;
    /**&lt;br /&gt;
     * @constructor&lt;br /&gt;
     * @alias module:block_overview/helloworld&lt;br /&gt;
     */&lt;br /&gt;
    var greeting = function() {&lt;br /&gt;
        /** @access private */&lt;br /&gt;
        var privateThoughts = &#039;I like the colour blue&#039;;&lt;br /&gt;
        &lt;br /&gt;
        /** @access public */&lt;br /&gt;
        this.publicThoughts = &#039;I like the colour orange&#039;;&lt;br /&gt;
&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * A formal greeting.&lt;br /&gt;
     * @access public&lt;br /&gt;
     * @return {string}&lt;br /&gt;
     */&lt;br /&gt;
    greeting.prototype.formal = function() {&lt;br /&gt;
        return &#039;How do you do?&#039;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * An informal greeting.&lt;br /&gt;
     * @access public&lt;br /&gt;
     * @return {string}&lt;br /&gt;
     */&lt;br /&gt;
    greeting.prototype.informal = function() {&lt;br /&gt;
        return &#039;Wassup!&#039;;&lt;br /&gt;
    };&lt;br /&gt;
    return greeting;&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The most interesting line above is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
define([&#039;jquery&#039;], function($) {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All AMD modules must call &amp;quot;define()&amp;quot; as the first and only global scoped piece of code. This ensures the javascript code contains no global variables and will not conflict with any other loaded module. The name of the module does not need to be specified because it is determined from the filename and component (but it can be listed in a comment for JSDoc as shown here). &lt;br /&gt;
&lt;br /&gt;
The first argument to &amp;quot;define&amp;quot; is the list of dependencies for the module. This argument must be passed as an array, even if there is only one. In this example &amp;quot;jquery&amp;quot; is a dependency. &amp;quot;jquery&amp;quot; is shipped as a core module is available to all AMD modules. &lt;br /&gt;
&lt;br /&gt;
The second argument to &amp;quot;define&amp;quot; is the function that defines the module. This function will receive as arguments, each of the requested dependencies in the same order they were requested. In this example we receive JQuery as an argument and we name the variable &amp;quot;$&amp;quot; (it&#039;s a JQuery thing). We can then access JQuery normally through the $ variable which is in scope for any code in our module. &lt;br /&gt;
&lt;br /&gt;
The rest of the code in this example is a standard way to define a Javascript module with public/private variables and methods. There are many ways to do this, this is only one.&lt;br /&gt;
&lt;br /&gt;
It is important that we are returning &#039;greeting&#039;. If there is no return then your module will be declared as undefined.&lt;br /&gt;
&lt;br /&gt;
== Loading modules dynamically ==&lt;br /&gt;
What do you do if you don&#039;t know in advance which modules will be required? Stuffing all possible required modules in the define call is one solution, but it&#039;s ugly and it only works for code that is in an AMD module (what about inline code in the page?). AMD lets you load a dependency any time you like. &lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Load a new dependency.&lt;br /&gt;
require([&#039;mod_wiki/timer&#039;], function(timer) {&lt;br /&gt;
   // timer is available to do my bidding.&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Including an external javascript/jquery library ==&lt;br /&gt;
If you want to include a javascript / jquery library downloaded from the internet you can do so as follows:&lt;br /&gt;
&lt;br /&gt;
If the library is in AMD format and has a define:&lt;br /&gt;
e.g. i want to include the jquery final countdown timer on my page ( hilios.github.io/jQuery.countdown/ )&lt;br /&gt;
* download the module in both norman and miniifed versions&lt;br /&gt;
* place the modules in your moodle install e.g. your custom theme dir, or plugin dir&lt;br /&gt;
* /theme/mytheme/amd/src/jquery.countdown.js&lt;br /&gt;
&lt;br /&gt;
you can now include the module and initialise it (there are multiple ways to do this)&lt;br /&gt;
php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// 1. Create your own amd module and initialise it:&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;requires-&amp;gt;js_call_amd(&#039;theme_mytheme/countdowntimer&#039;, &#039;initialise&#039;, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
javascript:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
//1. put this code in theme/mytheme/amd/src/countdowntimer.js&lt;br /&gt;
require([&#039;jquery&#039;, &#039;theme_mytheme/jquery.countdown&#039;], function($) {&lt;br /&gt;
    return {&lt;br /&gt;
        initialise: function ($params) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
        }&lt;br /&gt;
    };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. put the javascript into a mustache template&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// /theme/mytheme/templates/countdowntimer.mustache&lt;br /&gt;
&amp;lt;span id=&amp;quot;clock&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
require([&#039;jquery&#039;, &#039;theme_mytheme/jquery.countdown&#039;], function($) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
});&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. call the javascript directly from php -- although who would want to put javascript into php? ergh..&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_amd_inline(&#039;&lt;br /&gt;
require([&#039;theme_mytheme/jquery.countdown&#039;], function(min) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Embedding AMD code in a page ==&lt;br /&gt;
So you have created lots of cool Javascript modules. Great. How do we actually call them? Any javascript code that calls an AMD module must execute AFTER the requirejs module loader has finished loading. We have provided a function &amp;quot;js_call_amd&amp;quot; that will call a single function from an AMD module with parameters.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_call_amd($modulename, $functionname, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
that will &amp;quot;do the right thing&amp;quot; with your block of AMD code and execute it at the end of the page, after our AMD module loader has loaded. &lt;br /&gt;
Notes:&lt;br /&gt;
* the $modulename is the &#039;componentname/modulename&#039; discussed above&lt;br /&gt;
* the $functionname is the name of a public function exposed by the amd module. &lt;br /&gt;
* the $params is an array of params passed as arguments to the function. These should be simple types that can be handled by json_encode (no recursive arrays, or complex classes please). &lt;br /&gt;
* if the size of the params array is too large (&amp;gt; 1Kb), this will produce a developer warning. Do not attempt to pass large amounts of data through this function, it will pollute the page size. A preferred approach is to pass css selectors for DOM elements that contain data-attributes for any required data, or fetch data via ajax in the background.&lt;br /&gt;
&lt;br /&gt;
AMD / JS code can also be embedded on a page via mustache templates&lt;br /&gt;
see here: https://docs.moodle.org/dev/Templates#What_if_a_template_contains_javascript.3F&lt;br /&gt;
&lt;br /&gt;
== But I have a mega JS file I don&#039;t want loaded on every page? ==&lt;br /&gt;
Loading all JS files at once and stuffing them in the browser cache is the right choice for MOST js files, there are probably some exceptions. For these files, you can rename the javascript file to end with the suffix &amp;quot;-lazy.js&amp;quot; which indicates that the module will not be loaded by default, it will be requested the first time it is used. There is no difference in usage for lazy loaded modules, the require() call looks exactly the same, it&#039;s just that the module name will also have the &amp;quot;-lazy&amp;quot; suffix.&lt;br /&gt;
&lt;br /&gt;
[[Category:AJAX]]&lt;br /&gt;
[[Category:Javascript]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Javascript_Modules&amp;diff=49469</id>
		<title>Javascript Modules</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Javascript_Modules&amp;diff=49469"/>
		<updated>2016-02-15T15:45:02Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* How do I write a Javascript module in Moodle? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.9}}&lt;br /&gt;
&lt;br /&gt;
= Javascript Modules =&lt;br /&gt;
&lt;br /&gt;
== What is a Javascript module and why do I care? ==&lt;br /&gt;
&lt;br /&gt;
A Javascript module is nothing more than a collection of Javascript code that can be used (reliably) from other pieces of Javascript. &lt;br /&gt;
&lt;br /&gt;
== Why should I package my code as a module? ==&lt;br /&gt;
&lt;br /&gt;
By packaging your code as a module you break your code up into smaller reusable pieces. This is good because:&lt;br /&gt;
&lt;br /&gt;
a) Each smaller piece is simpler to understand / debug&lt;br /&gt;
&lt;br /&gt;
b) Each smaller piece is simpler to test&lt;br /&gt;
&lt;br /&gt;
c) You can re-use common code instead of duplicating it&lt;br /&gt;
&lt;br /&gt;
= How do I write a Javascript module in Moodle? =&lt;br /&gt;
&lt;br /&gt;
Since version 2.9, Moodle supports Javascript modules written using the Asynchronous Module Definition ([https://github.com/amdjs/amdjs-api/wiki/AMD AMD]) API. This is a standard API for creating Javascript modules and you will find many useful third party libraries that are already using this format. &lt;br /&gt;
&lt;br /&gt;
To edit or create an AMD module in Moodle you need to do a couple of things. &lt;br /&gt;
&lt;br /&gt;
== Install grunt ==&lt;br /&gt;
&lt;br /&gt;
The AMD modules in Moodle must be processed by some build tools before they will be visible to your web browser. We use &amp;quot;[http://gruntjs.com/ grunt]&amp;quot; as a build tool to wrap our different processes. Grunt is a build tool written in Javascript that runs in the &amp;quot;[http://nodejs.org/ nodejs]&amp;quot; environment.&lt;br /&gt;
&lt;br /&gt;
This means you first have to &#039;&#039;&#039;install nodejs&#039;&#039;&#039; - and it&#039;s package manager [https://www.npmjs.com/ npm]. The details of how to install those packages will vary by operating system, but on Linux it&#039;s probably similar to &amp;quot;sudo apt-get install nodejs npm&amp;quot;. There are downloadable packages for other operating systems here: http://nodejs.org/download/.&lt;br /&gt;
&lt;br /&gt;
Once this is done, you can &#039;&#039;&#039;run the command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
 npm install&lt;br /&gt;
 npm install -g grunt-cli&lt;br /&gt;
&lt;br /&gt;
from the top of the Moodle directory to install all of the required tools. (You may need extra permissions to use the -g option.)&lt;br /&gt;
&lt;br /&gt;
== Running grunt ==&lt;br /&gt;
&lt;br /&gt;
See [[Grunt#Running_grunt]] for more details of specific grunt commands which can be used.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;Hello World&amp;quot; I am a Javascript Module ==&lt;br /&gt;
Lets now create a simple Javascript module so we can see how to lay things out. &lt;br /&gt;
&lt;br /&gt;
Each Javascript module is contained in a single source file in the &amp;lt;componentdir&amp;gt;/amd/src folder. The final name of the module is taken from the file name and the component name. E.g. block_overview/amd/src/helloworld.js would be a module named &amp;quot;block_overview/helloworld&amp;quot;. the name of the module is important when you want to call it from somewhere else in the code. &lt;br /&gt;
&lt;br /&gt;
After running grunt - the minified Javascript files are stored in the &amp;lt;componentdir&amp;gt;/amd/build folder. The javascript files are renamed to show that they are minified (helloworld.js becomes helloworld.min.js). &lt;br /&gt;
&lt;br /&gt;
Don&#039;t forget to add the built files (the ones in amd/build) to your git commits, or in production no-one will see your changes. &lt;br /&gt;
&lt;br /&gt;
Lets create a simple module now:&lt;br /&gt;
&lt;br /&gt;
blocks/overview/amd/src/helloworld.js&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// Standard license block omitted.&lt;br /&gt;
/*&lt;br /&gt;
 * @package    block_overview&lt;br /&gt;
 * @copyright  2015 Someone cool&lt;br /&gt;
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
  * @module block_overview/helloworld&lt;br /&gt;
  */&lt;br /&gt;
define([&#039;jquery&#039;], function($) {&lt;br /&gt;
&lt;br /&gt;
     /** &lt;br /&gt;
      * Give me blue.&lt;br /&gt;
      * @access private&lt;br /&gt;
      * @return {string}&lt;br /&gt;
      */&lt;br /&gt;
     var makeItBlue = function() {&lt;br /&gt;
          // We can use our jquery dependency here.&lt;br /&gt;
          return $(&#039;.blue&#039;).show();&lt;br /&gt;
     };&lt;br /&gt;
      &lt;br /&gt;
    /**&lt;br /&gt;
     * @constructor&lt;br /&gt;
     * @alias module:block_overview/helloworld&lt;br /&gt;
     */&lt;br /&gt;
    var greeting = function() {&lt;br /&gt;
        /** @access private */&lt;br /&gt;
        var privateThoughts = &#039;I like the colour blue&#039;;&lt;br /&gt;
        &lt;br /&gt;
        /** @access public */&lt;br /&gt;
        this.publicThoughts = &#039;I like the colour orange&#039;;&lt;br /&gt;
&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * A formal greeting.&lt;br /&gt;
     * @access public&lt;br /&gt;
     * @return {string}&lt;br /&gt;
     */&lt;br /&gt;
    greeting.prototype.formal = function() {&lt;br /&gt;
        return &#039;How do you do?&#039;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * An informal greeting.&lt;br /&gt;
     * @access public&lt;br /&gt;
     * @return {string}&lt;br /&gt;
     */&lt;br /&gt;
    greeting.prototype.informal = function() {&lt;br /&gt;
        return &#039;Wassup!&#039;;&lt;br /&gt;
    };&lt;br /&gt;
    return greeting;&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The most interesting line above is:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
define([&#039;jquery&#039;], function($) {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All AMD modules must call &amp;quot;define()&amp;quot; as the first and only global scoped piece of code. This ensures the javascript code contains no global variables and will not conflict with any other loaded module. The name of the module does not need to be specified because it is determined from the filename and component (but it can be listed in a comment for JSDoc as shown here). &lt;br /&gt;
&lt;br /&gt;
The first argument to &amp;quot;define&amp;quot; is the list of dependencies for the module. This argument must be passed as an array, even if there is only one. In this example &amp;quot;jquery&amp;quot; is a dependency. &amp;quot;jquery&amp;quot; is shipped as a core module is available to all AMD modules. &lt;br /&gt;
&lt;br /&gt;
The second argument to &amp;quot;define&amp;quot; is the function that defines the module. This function will receive as arguments, each of the requested dependencies in the same order they were requested. In this example we receive JQuery as an argument and we name the variable &amp;quot;$&amp;quot; (it&#039;s a JQuery thing). We can then access JQuery normally through the $ variable which is in scope for any code in our module. &lt;br /&gt;
&lt;br /&gt;
The rest of the code in this example is a standard way to define a Javascript module with public/private variables and methods. There are many ways to do this, this is only one.&lt;br /&gt;
&lt;br /&gt;
It is important that we are returning &#039;greeting&#039;. If there is no return then your module will be declared as undefined.&lt;br /&gt;
&lt;br /&gt;
== Loading modules dynamically ==&lt;br /&gt;
What do you do if you don&#039;t know in advance which modules will be required? Stuffing all possible required modules in the define call is one solution, but it&#039;s ugly and it only works for code that is in an AMD module (what about inline code in the page?). AMD lets you load a dependency any time you like. &lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Load a new dependency.&lt;br /&gt;
require([&#039;mod_wiki/timer&#039;], function(timer) {&lt;br /&gt;
   // timer is available to do my bidding.&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Including an external javascript/jquery library ==&lt;br /&gt;
If you want to include a javascript / jquery library downloaded from the internet you can do so as follows:&lt;br /&gt;
&lt;br /&gt;
If the library is in AMD format and has a define:&lt;br /&gt;
e.g. i want to include the jquery final countdown timer on my page ( hilios.github.io/jQuery.countdown/ )&lt;br /&gt;
* download the module in both norman and miniifed versions&lt;br /&gt;
* place the modules in your moodle install e.g. your custom theme dir, or plugin dir&lt;br /&gt;
* /theme/mytheme/amd/src/jquery.countdown.js&lt;br /&gt;
&lt;br /&gt;
you can now include the module and initialise it (there are multiple ways to do this)&lt;br /&gt;
php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// 1. Create your own amd module and initialise it:&lt;br /&gt;
$this-&amp;gt;page-&amp;gt;requires-&amp;gt;js_call_amd(&#039;theme_mytheme/countdowntimer&#039;, &#039;initialise&#039;, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
javascript:&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
//1. put this code in theme/mytheme/amd/src/countdowntimer.js&lt;br /&gt;
require([&#039;jquery&#039;, &#039;theme_mytheme/jquery.countdown&#039;], function($) {&lt;br /&gt;
    return {&lt;br /&gt;
        initialise: function ($params) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
        }&lt;br /&gt;
    };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. put the javascript into a mustache template&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// /theme/mytheme/templates/countdowntimer.mustache&lt;br /&gt;
&amp;lt;span id=&amp;quot;clock&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
{{#js}}&lt;br /&gt;
require([&#039;jquery&#039;, &#039;theme_mytheme/jquery.countdown&#039;], function($) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
});&lt;br /&gt;
{{/js}}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. call the javascript directly from php -- although who would want to put javascript into php? ergh..&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_amd_inline(&#039;&lt;br /&gt;
require([&#039;theme_mytheme/jquery.countdown&#039;], function(min) {&lt;br /&gt;
           $(&#039;#clock&#039;).countdown(&#039;2020/10/10&#039;, function(event) {&lt;br /&gt;
             $(this).html(event.strftime(&#039;%D days %H:%M:%S&#039;));&lt;br /&gt;
           });&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Embedding AMD code in a page ==&lt;br /&gt;
So you have created lots of cool Javascript modules. Great. How do we actually call them? Any javascript code that calls an AMD module must execute AFTER the requirejs module loader has finished loading. We have provided a function &amp;quot;js_call_amd&amp;quot; that will call a single function from an AMD module with parameters.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_call_amd($modulename, $functionname, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
that will &amp;quot;do the right thing&amp;quot; with your block of AMD code and execute it at the end of the page, after our AMD module loader has loaded. &lt;br /&gt;
Notes:&lt;br /&gt;
* the $modulename is the &#039;componentname/modulename&#039; discussed above&lt;br /&gt;
* the $functionname is the name of a public function exposed by the amd module. &lt;br /&gt;
* the $params is an array of params passed as arguments to the function. These should be simple types that can be handled by json_encode (no recursive arrays, or complex classes please). &lt;br /&gt;
* if the size of the params array is too large (&amp;gt; 1Kb), this will produce a developer warning. Do not attempt to pass large amounts of data through this function, it will pollute the page size. A preferred approach is to pass css selectors for DOM elements that contain data-attributes for any required data, or fetch data via ajax in the background.&lt;br /&gt;
&lt;br /&gt;
AMD / JS code can also be embedded on a page via mustache templates&lt;br /&gt;
see here: https://docs.moodle.org/dev/Templates#What_if_a_template_contains_javascript.3F&lt;br /&gt;
&lt;br /&gt;
== But I have a mega JS file I don&#039;t want loaded on every page? ==&lt;br /&gt;
Loading all JS files at once and stuffing them in the browser cache is the right choice for MOST js files, there are probably some exceptions. For these files, you can rename the javascript file to end with the suffix &amp;quot;-lazy.js&amp;quot; which indicates that the module will not be loaded by default, it will be requested the first time it is used. There is no difference in usage for lazy loaded modules, the require() call looks exactly the same, it&#039;s just that the module name will also have the &amp;quot;-lazy&amp;quot; suffix.&lt;br /&gt;
&lt;br /&gt;
[[Category:AJAX]]&lt;br /&gt;
[[Category:Javascript]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49131</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49131"/>
		<updated>2015-12-09T11:58:00Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Added required language strings&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;assignfeedback_file.php&amp;quot;). It should at least define strings for &amp;quot;pluginname&amp;quot;, &amp;quot;enabled&amp;quot; and &amp;quot;enabled_help&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled&#039;] = &#039;Awesome feedback&#039;;&lt;br /&gt;
$string[&#039;enabled_help&#039;] = &#039;If enabled, the teacher will be able to provide awesome feedback &#039;; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_feedback_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49130</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49130"/>
		<updated>2015-12-09T11:52:55Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Fixed extended class name&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;assignfeedback_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_feedback_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49129</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49129"/>
		<updated>2015-12-09T11:51:15Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;assignfeedback_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49128</id>
		<title>Assign feedback plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_feedback_plugins&amp;diff=49128"/>
		<updated>2015-12-09T11:50:48Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Fixed language file name&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment feedback plugins.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment feedback plugin ===&lt;br /&gt;
An assignment feedback plugin can do many things including providing feedback to students about a submission. The grading interface for the assignment module provides many hooks that allow plugins to add their own entries and participate in the grading workflow. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment feedback plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A good example is the &amp;quot;file&amp;quot; feedback plugin included with core because it uses most of the features of feedback plugins. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom feedback plugin sit under &amp;quot;mod/assign/feedback/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder.&lt;br /&gt;
Note: The plugin name should be no longer than 13 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignfeedback_&amp;quot; + pluginname (15 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignfeedback_&amp;quot; to identify this as a feedback plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignfeedback_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All feedback settings should be named &#039;assignfeedback_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All feedback plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignfeedback_file/default&#039;,                                                      &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignfeedback_file&#039;),                                                               &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignfeedback_file&#039;), 0));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/assignfeedback_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;feedback_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome feedback&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignfeedback/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_feedback_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignfeedback&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/feedback/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/feedback/file&amp;quot;               &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignfeedback_file&amp;quot; COMMENT=&amp;quot;Stores info about the number of files submitted by a grader.&amp;quot;&amp;gt;                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files uploaded by a grader.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;Unique id for this feedback value.&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;grade&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;grade&amp;quot; REFTABLE=&amp;quot;assign_grades&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The grade instance this feedback relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This file contains custom code to run on installation of the plugin. In this case it makes the files plugin the second of the feedback plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                                                                                                           &lt;br /&gt;
function xmldb_assignfeedback_file_install() {                                                                                      &lt;br /&gt;
    global $CFG;                                                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/adminlib.php&#039;);                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Set the correct initial order for the plugins.                                                                               &lt;br /&gt;
    $pluginmanager = new assign_plugin_manager(&#039;assignfeedback&#039;);                                                                   &lt;br /&gt;
    $pluginmanager-&amp;gt;move_plugin(&#039;file&#039;, &#039;down&#039;);                                                                                    &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    return true;                                                                                                                    &lt;br /&gt;
}                      &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_feedback_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All feedback plugins MUST define a class with the component name of the plugin that extends assign_feedback_plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignfeedback_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in feedback_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;assignfeedback_file_fileextensions&#039;, get_string(&#039;allowedfileextensions&#039;, &#039;assignfeedback_file&#039;));&lt;br /&gt;
         $mform-&amp;gt;setType(&#039;assignfeedback_file_fileextensions&#039;, PARAM_FILE);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings should be prefixed by the plugin name which is good practice to avoid conflicts with other plugins. (None of the core feedback plugins have any instance settings, so this example is fictional).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;allowedfileextensions&#039;, $data-&amp;gt;allowedfileextensions);                                             &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid)                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $gradeid = $grade ? $grade-&amp;gt;id : 0;                                                                                         &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  $elementname,                                                                     &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignfeedback_file&#039;,                                                            &lt;br /&gt;
                                                  ASSIGNFEEDBACK_FILE_FILEAREA,                                                     &lt;br /&gt;
                                                  $gradeid);                                                                        &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, $elementname . &#039;_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                              &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the feedback form. It functions identically to the get_settings function except that the grade object is available (if there is a grade) to associate the settings with a single grade attempt. This example also shows how to use a filemanager within a feedback plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. Notice there is an older version of this function &amp;quot;get_form_elements&amp;quot; which does not accept a userid as a parameter - this version is less useful - not recommended. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $grade, stdClass $data) {                                                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $userid = $grade-&amp;gt;userid;                                                                                                   &lt;br /&gt;
        $elementname = &#039;files_&#039; . $userid;                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     $elementname,                                                                  &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignfeedback_file&#039;,                                                         &lt;br /&gt;
                                                     ASSIGNFEEDBACK_FILE_FILEAREA,                                                  &lt;br /&gt;
                                                     $grade-&amp;gt;id);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filefeedback = $this-&amp;gt;get_file_feedback($grade-&amp;gt;id);                                                                       &lt;br /&gt;
        if ($filefeedback) {                                                                                                        &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignfeedback_file&#039;, $filefeedback);                                                        &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filefeedback = new stdClass();                                                                                         &lt;br /&gt;
            $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                 &lt;br /&gt;
            $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                      &lt;br /&gt;
            $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                      &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0;                                                    &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                                         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a graders feedback. The parameters are the grade object and the data from the feedback form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this feedback. It then records the number of files in the plugin specific &amp;quot;assignfeedback_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $grade, &amp;amp; $showviewlink) {                                                                &lt;br /&gt;
        $count = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                                      &lt;br /&gt;
        // show a view all link if the number of files is over this limit                                                           &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {                                                                        &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignfeedback_file&#039;, $count);                                                         &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the feedback to both markers and students. It counts the number of files and if it is more that a set number, it only displays a count of how many files are in the feedback - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full feedback on the submission status page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view(stdClass $grade) {                                                                                         &lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignfeedback_file&#039;, ASSIGNFEEDBACK_FILE_FILEAREA, $grade-&amp;gt;id);               &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire feedback to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {                                                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if (($type == &#039;upload&#039; || $type == &#039;uploadsingle&#039;) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {                                             &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        // first upgrade settings (nothing to do)                                                                                   &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it returns true as there are no settings to upgrade. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, &amp;amp; $log) {       &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now copy the area files                                                                                                  &lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,                                                             &lt;br /&gt;
                                                        &#039;mod_assignment&#039;,                                                           &lt;br /&gt;
                                                        &#039;response&#039;,                                                                 &lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,                                                         &lt;br /&gt;
                                                        // New file area                                                            &lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                       &lt;br /&gt;
                                                        &#039;assignfeedback_file&#039;,                                                      &lt;br /&gt;
                                                        ASSIGNFEEDBACK_FILE_FILEAREA,                                               &lt;br /&gt;
                                                        $grade-&amp;gt;id);                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // now count them!                                                                                                          &lt;br /&gt;
        $filefeedback = new stdClass();                                                                                             &lt;br /&gt;
        $filefeedback-&amp;gt;numfiles = $this-&amp;gt;count_files($grade-&amp;gt;id, ASSIGNFEEDBACK_FILE_FILEAREA);                                     &lt;br /&gt;
        $filefeedback-&amp;gt;grade = $grade-&amp;gt;id;                                                                                          &lt;br /&gt;
        $filefeedback-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                          &lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assignfeedback_file&#039;, $filefeedback) &amp;gt; 0) {                                                        &lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertgrade&#039;, &#039;mod_assign&#039;, $grade-&amp;gt;userid);                                               &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no data to show - it can return true from the is_empty function. This prevents a table row being added to the feedback summary for this plugin. It is also used to check if a grader has tried to save feedback with no data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNFEEDBACK_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                              &lt;br /&gt;
    }      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignfeedback_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }         &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function format_for_gradebook(stdClass $grade) {                                                                         &lt;br /&gt;
        return FORMAT_MOODLE;                                                                                                       &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function text_for_gradebook(stdClass $grade) {                                                                           &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Only one feedback plugin can push comments to the gradebook. Usually this is the feedback_comments plugin - but it can be configured to be any feedback plugin. If the current plugin is the plugin chosen to generate comments for the gradebook, the comment text and format will be taken from these two functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Override to indicate a plugin supports quickgrading                                                                          &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return boolean - True if the plugin supports quickgrading                                                                   &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function supports_quickgrading() {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Get quickgrading form elements as html                                                                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param mixed $grade grade or null - The grade data. May be null if there are no grades for this user (yet)                   &lt;br /&gt;
     * @return mixed - A html string containing the html form elements required for quickgrading or false to indicate this plugin does not support quickgrading&lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_quickgrading_html($userid, $grade) {                                                                        &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Has the plugin quickgrading form element been modified in the current form submission?                                       &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the quickgrading form element has been modified                                                    &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function is_quickgrading_modified($userid, $grade) {                                                                     &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Save quickgrading changes                                                                                                    &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param int $userid The user id in the table this quickgrading element relates to                                             &lt;br /&gt;
     * @param stdClass $grade The grade                                                                                             &lt;br /&gt;
     * @return boolean - true if the grade changes were saved correctly                                                             &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function save_quickgrading_changes($userid, $grade) {                                                                    &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These 4 functions can be implemented to allow a plugin to support quickgrading. The feedback comments plugin is the only example of this in core. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Run cron for this plugin                                                                                                     &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public static function cron() {                                                                                                 &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A plugin can run code when cron runs by implementing this method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the grading actions supported by this plugin.                                                               &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * A grading action is a page that is not specific to a user but to the whole assignment.                                       &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_action.                                                                 &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_actions() {                                                                                         &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }                  &lt;br /&gt;
&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Show a grading action form                                                                                                   &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $gradingaction The action chosen from the grading actions menu                                                 &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_action($gradingaction) {                                                                                &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;       &lt;br /&gt;
&lt;br /&gt;
Grading actions appear in the select menu above the grading table and apply to the whole assignment. An example is &amp;quot;Upload grading worksheet&amp;quot;. When a grading action is selected, the grading_action will be called with the action that was chosen (so plugins can have multiple entries in the list).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**                                                                                                                             &lt;br /&gt;
     * Return a list of the batch grading operations supported by this plugin.                                                      &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @return array - An array of action and description strings.                                                                  &lt;br /&gt;
     *                 The action will be passed to grading_batch_operation.                                                        &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function get_grading_batch_operations() {                                                                                &lt;br /&gt;
        return array();                                                                                                             &lt;br /&gt;
    }     &lt;br /&gt;
&lt;br /&gt;
    /**                                                                                                                             &lt;br /&gt;
     * Show a batch operations form                                                                                                 &lt;br /&gt;
     *                                                                                                                              &lt;br /&gt;
     * @param string $action The action chosen from the batch operations menu                                                       &lt;br /&gt;
     * @param array $users The list of selected userids                                                                             &lt;br /&gt;
     * @return string The page containing the form                                                                                  &lt;br /&gt;
     */                                                                                                                             &lt;br /&gt;
    public function grading_batch_operation($action, $users) {                                                                      &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }              &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These two callbacks allow adding entries to the batch grading operations list (where you select multiple users in the table and choose e.g. &amp;quot;Lock submissions&amp;quot; for every user). The action is passed to &amp;quot;grading_batch_operation&amp;quot; so that multiple entries can be supported by a plugin.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Using_the_File_API_in_Moodle_forms&amp;diff=48745</id>
		<title>Using the File API in Moodle forms</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Using_the_File_API_in_Moodle_forms&amp;diff=48745"/>
		<updated>2015-10-14T09:03:04Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Add file manager element */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
This document shows you exactly how to use Moodle forms to get files from users in a standard and secure way.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 all files are stored in a central database accessible via the [[File API|File API]], and every file is associated with a component and a &amp;quot;file area&amp;quot; in Moodle, such as a particular module.&lt;br /&gt;
&lt;br /&gt;
A common use case is to provide a form (using Moodle&#039;s [[lib/formslib.php|Forms API]]) which allows users to upload or import files as attachments or media embedded into HTML.&lt;br /&gt;
&lt;br /&gt;
Normally this works like this:&lt;br /&gt;
# User starts creation or re-edits an existing item in Moodle (eg forum post, resource, glossary entry etc)&lt;br /&gt;
# User presses some sort of button to browse for new files to attach or embed&lt;br /&gt;
# User sees our &amp;quot;Choose file...&amp;quot; dialog, which contains one or more repository instances. &lt;br /&gt;
# User chooses a file, the corresponding [[Repository plugins|Repository plugin]] takes care of copying the file into a &amp;quot;draft file area&amp;quot; within Moodle&lt;br /&gt;
# File appears in the text or as an attachment in the form.&lt;br /&gt;
# When the user hits save, the [[File API|File API]] is invoked to move the file from the draft file area into a permanent file area associated with that data &lt;br /&gt;
&lt;br /&gt;
This document shows you exactly how to use Moodle forms to interact with users in a standard and secure way.&lt;br /&gt;
&lt;br /&gt;
If you just want to write code to manipulate Moodle files internally (without user input) then see [[File API]].&lt;br /&gt;
&lt;br /&gt;
==Form elements== &lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 there are three file-related form elements for interacting with users:&lt;br /&gt;
&lt;br /&gt;
# filemanager - the way to attach one or more files as a set&lt;br /&gt;
# editor - the way to specify a textarea with a HTML editor, and all the handling of images and movies within that HTML&lt;br /&gt;
# filepicker - a way to specify one file for the case when you want to process the file and throw it away &lt;br /&gt;
&lt;br /&gt;
In Moodle 1.9 there were two other types which are now &#039;&#039;&#039;deprecated&#039;&#039;&#039; (they work, but please do not use these anymore)&lt;br /&gt;
# file - used to just allow a normal file upload from the desktop only.&lt;br /&gt;
# htmleditor - this old method of embedding a HTML editor in a textarea is not able to support repositories etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===filepicker===&lt;br /&gt;
&lt;br /&gt;
File picker (&#039;&#039;filepicker&#039;&#039;) is a direct replacement of the older &#039;&#039;file&#039;&#039; formslib element. &lt;br /&gt;
&lt;br /&gt;
It is intended for situations when you want the user to upload &#039;&#039;&#039;one&#039;&#039;&#039; file so you can process it and delete it, such as when you are importing data from a CSV file.&lt;br /&gt;
&lt;br /&gt;
==== Using the filepicker element ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;filepicker&#039;, &#039;userfile&#039;, get_string(&#039;file&#039;), null,&lt;br /&gt;
                   array(&#039;maxbytes&#039; =&amp;gt; $maxbytes, &#039;accepted_types&#039; =&amp;gt; &#039;*&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Obtain the chosen file ====&lt;br /&gt;
&lt;br /&gt;
To get the contents of the file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$content = $mform-&amp;gt;get_file_content(&#039;userfile&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get the name of the chosen file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$name = $mform-&amp;gt;get_new_filename(&#039;userfile&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To save the chosen file to the server filesystem (such as to moodledata folder):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$success = $mform-&amp;gt;save_file(&#039;userfile&#039;, $fullpath, $override);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To store the chosen file in the Moodle files pool:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$storedfile = $mform-&amp;gt;save_stored_file(&#039;userfile&#039;, ...);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== filemanager ===&lt;br /&gt;
&lt;br /&gt;
The File Manager element improves on file picker by allowing you to manage more than one file.  It is expected that the files will be stored permanently for future use (such as forum and glossary attachments).&lt;br /&gt;
&lt;br /&gt;
==== Add file manager element ====&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;attachments&#039;, get_string(&#039;attachment&#039;, &#039;moodle&#039;), null,&lt;br /&gt;
                    array(&#039;subdirs&#039; =&amp;gt; 0, &#039;maxbytes&#039; =&amp;gt; $maxbytes, &#039;areamaxbytes&#039; =&amp;gt; 10485760, &#039;maxfiles&#039; =&amp;gt; 50,&lt;br /&gt;
                          &#039;accepted_types&#039; =&amp;gt; array(&#039;document&#039;), &#039;return_types&#039;=&amp;gt; FILE_INTERNAL | FILE_EXTERNAL));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here are the fields for filemanager:&lt;br /&gt;
&lt;br /&gt;
;&#039;filemanager&#039;:This is a filemanager element :)&lt;br /&gt;
;elementname:The unique name of the element in the form&lt;br /&gt;
;elementlabel:The label string that users see &lt;br /&gt;
;attributes:(leave it as null)&lt;br /&gt;
;options: an array of further options for the filepicker (see below)&lt;br /&gt;
&lt;br /&gt;
The options array can contain:&lt;br /&gt;
&lt;br /&gt;
;subdirs:(Default 1) Are subdirectories allowed?  (true or false)&lt;br /&gt;
;maxbytes:(Default 0) Restricts the size of each individual file.&lt;br /&gt;
;areamaxbytes:(Default 0) Restricts the total size of all the files.&lt;br /&gt;
;maxfiles:(Default -1) Restricts the total number of files.&lt;br /&gt;
;accepted_types:(Default *) You can specify what file types are accepted by filemanager.  All current file types are listed in this file: [http://cvs.moodle.org/moodle/lib/filestorage/file_types.mm moodle/lib/filestorage/file_types.mm].  This is a [http://freemind.sourceforge.net/wiki/index.php/Main_Page freemind] file: if it is edited the changes will be immediately reflected in Moodle.&lt;br /&gt;
&lt;br /&gt;
As of 2.3 file_types.mm is no longer used, instead the file types are listed in &amp;lt;code&amp;gt;get_mimetypes_array()&amp;lt;/code&amp;gt; in filelib.php (https://github.com/moodle/moodle/blob/master/lib/classes/filetypes.php).&lt;br /&gt;
&lt;br /&gt;
Example usage:  &#039;&#039;&#039;array(&#039;audio&#039;, &#039;video&#039;, &#039;document&#039;)&#039;&#039;&#039;, you can include file extensions as well, for example: &#039;&#039;&#039;array(&#039;.txt&#039;, &#039;.jpg&#039;, &#039;audio&#039;)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==== Load existing files into draft area ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (empty($entry-&amp;gt;id)) {&lt;br /&gt;
    $entry = new stdClass;&lt;br /&gt;
    $entry-&amp;gt;id = null;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$draftitemid = file_get_submitted_draft_itemid(&#039;attachments&#039;);&lt;br /&gt;
&lt;br /&gt;
file_prepare_draft_area($draftitemid, $context-&amp;gt;id, &#039;mod_glossary&#039;, &#039;attachment&#039;, $entry-&amp;gt;id,&lt;br /&gt;
                        array(&#039;subdirs&#039; =&amp;gt; 0, &#039;maxbytes&#039; =&amp;gt; $maxbytes, &#039;maxfiles&#039; =&amp;gt; 50));&lt;br /&gt;
&lt;br /&gt;
$entry-&amp;gt;attachments = $draftitemid;&lt;br /&gt;
&lt;br /&gt;
$mform-&amp;gt;set_data($entry);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Store updated set of files ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if ($data = $mform-&amp;gt;get_data()) {&lt;br /&gt;
    // ... store or update $entry&lt;br /&gt;
    file_save_draft_area_files($data-&amp;gt;attachments, $context-&amp;gt;id, &#039;mod_glossary&#039;, &#039;attachment&#039;,&lt;br /&gt;
                   $entry-&amp;gt;id, array(&#039;subdirs&#039; =&amp;gt; 0, &#039;maxbytes&#039; =&amp;gt; $maxbytes, &#039;maxfiles&#039; =&amp;gt; 50));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===editor===&lt;br /&gt;
There are two ways of using the editor element in code, the first one is easier but expects some standardized fields. The second method is more low level.&lt;br /&gt;
&lt;br /&gt;
====Simple use====&lt;br /&gt;
# name database fields: &#039;&#039;textfield&#039;&#039;, &#039;&#039;textfieldformat&#039;&#039; (and &#039;&#039;textfieldtrust&#039;&#039; if required)&lt;br /&gt;
# create options array.  note that context is the best, most local context you have available.  &amp;lt;code php&amp;gt;$textfieldoptions = array(&#039;trusttext&#039;=&amp;gt;true, &#039;subdirs&#039;=&amp;gt;true, &#039;maxfiles&#039;=&amp;gt;$maxfiles,&lt;br /&gt;
                          &#039;maxbytes&#039;=&amp;gt;$maxbytes, &#039;context&#039;=&amp;gt;$context);&amp;lt;/code&amp;gt;&lt;br /&gt;
# add editor &#039;&#039;textfield_editor&#039;&#039; to moodle form, pass options through custom data in form constructor, set $data-&amp;gt;id to null if data not exist yet &amp;lt;code php&amp;gt;$mform-&amp;gt;addElement(&#039;editor&#039;, &#039;textfield_editor&#039;, get_string(&#039;fieldname&#039;, &#039;somemodule&#039;),&lt;br /&gt;
                   null, $textfieldoptions);&amp;lt;/code&amp;gt;&lt;br /&gt;
# prepare data &amp;lt;code php&amp;gt;$data = file_prepare_standard_editor($data, &#039;textfield&#039;, $textfieldoptions, $context,&lt;br /&gt;
                                     &#039;mod_somemodule&#039;, &#039;somearea&#039;, $data-&amp;gt;id);&amp;lt;/code&amp;gt;&lt;br /&gt;
# get submitted data and after inserting/updating of data &amp;lt;code php&amp;gt;$data = file_postupdate_standard_editor($data, &#039;textfield&#039;, $textfieldoptions, $context,&lt;br /&gt;
                                        &#039;mod_somemodule&#039;, &#039;somearea&#039;, $data-&amp;gt;id);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Real world examples are in mod/glossary/edit.php and mod/glossary/comment.php&lt;br /&gt;
&lt;br /&gt;
====Low level use====&lt;br /&gt;
&lt;br /&gt;
When using editor element you  need to preprocess and postprocess the data:&lt;br /&gt;
# detect if form was already submitted (usually means draft is area already exists) - &#039;&#039;file_get_submitted_draft_itemid()&#039;&#039;&lt;br /&gt;
# prepare draft file area, temporary storage of all files attached to the text - &#039;&#039;file_prepare_draft_area()&#039;&#039;&lt;br /&gt;
# convert encoded relative links to absolute links - &#039;&#039;file_prepare_draft_area()&#039;&#039;&lt;br /&gt;
# create form and set current data&lt;br /&gt;
# after submission the changed files must be merged back into original area - &#039;&#039;file_save_draft_area_files()&#039;&#039;&lt;br /&gt;
# absolute links have to be replaced by relative links - &#039;&#039;file_save_draft_area_files()&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=====Replace old htmleditor with editor=====&lt;br /&gt;
&lt;br /&gt;
The file picker has been integrated with with TinyMCE to make the editor element. This new element should support all types on editors and should be able to switch them on-the-fly. Instances of the old htmleditor element in your forms should be replaced by the new editor element, this may need adding of new format and trusttext columns. For example:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;editor&#039;, &#039;entry&#039;, get_string(&#039;definition&#039;, &#039;glossary&#039;), null,&lt;br /&gt;
        array(&#039;maxfiles&#039; =&amp;gt; EDITOR_UNLIMITED_FILES));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The editor element can take following options: maxfiles, maxbytes, subdirs and changeformat. Please note that the embedded files is optional feature and is not expected be used everywhere.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: the editor element now includes text format option. You should no longer use the separate format element type.&lt;br /&gt;
&lt;br /&gt;
=====Prepare current data - text and files=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (empty($entry-&amp;gt;id)) {&lt;br /&gt;
  $entry = new object();&lt;br /&gt;
  $entry-&amp;gt;id = null;&lt;br /&gt;
  $entry-&amp;gt;definition = &#039;&#039;;&lt;br /&gt;
  $entry-&amp;gt;format = FORMAT_HTML;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$draftid_editor = file_get_submitted_draft_itemid(&#039;entry&#039;);&lt;br /&gt;
$currenttext = file_prepare_draft_area($draftid_editor, $context-&amp;gt;id, &#039;mod_glossary&#039;, &#039;entry&#039;,&lt;br /&gt;
                                       $entry-&amp;gt;id, array(&#039;subdirs&#039;=&amp;gt;true), $entry-&amp;gt;definition);&lt;br /&gt;
$entry-&amp;gt;entry = array(&#039;text&#039;=&amp;gt;$currenttext, &#039;format&#039;=&amp;gt;$entry-&amp;gt;format, &#039;itemid&#039;=&amp;gt;$draftid_editor);&lt;br /&gt;
&lt;br /&gt;
$mform-&amp;gt;set_data($entry);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If there are multiple files, they will share the same itemid.&lt;br /&gt;
&lt;br /&gt;
=====Obtain text, format and save draft files=====&lt;br /&gt;
&lt;br /&gt;
To retrieve editor content, you need to use following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if ($fromform = $mform-&amp;gt;get_data()) {&lt;br /&gt;
    // content of editor&lt;br /&gt;
    $messagetext = $fromform-&amp;gt;entry[&#039;text&#039;];&lt;br /&gt;
    // format of content&lt;br /&gt;
    $messageformat  = $fromform-&amp;gt;entry[&#039;format&#039;];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a user selects a file using the file picker, the file is initially stored in a draft file area, and a URL is inserted into the HTML in the editor that lets the person editing the content (but no one else) see the file.&lt;br /&gt;
&lt;br /&gt;
When the user submits the form, we then need to save the draft files to the correct place in permanent storage. (Just like you have to call $DB-&amp;gt;update_record(&#039;tablename&#039;, $data); to have the other parts of the form submission stored correctly.)&lt;br /&gt;
&lt;br /&gt;
The save_files_from_draft_area function and replace absolute links with internal relative links do:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$messagetext = file_save_draft_area_files($draftid_editor, $context-&amp;gt;id, &#039;mod_glossary&#039;, &#039;entry&#039;,&lt;br /&gt;
                                          $entry-&amp;gt;id, array(&#039;subdirs&#039;=&amp;gt;true), $messagetext);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
; $context-&amp;gt;id, &#039;component&#039;, &#039;proper_file_area&#039; and $entry-&amp;gt;id : correspond to the contextid, filearea and itemid columns in the [[File_API#Table:_files|files table]].&lt;br /&gt;
; $messagetext : this is the message text. As the files are saved to the real file area, the URLs in this content are rewritten.&lt;br /&gt;
&lt;br /&gt;
All URLs in content that point to files managed to the File API are converted to a form that starts &#039;@@PLUGINFILE@@/&#039; before the content is stored in the database. That is what we mean by rewriting.&lt;br /&gt;
&lt;br /&gt;
== File serving==&lt;br /&gt;
&lt;br /&gt;
=== Convert internal relative links to absolute links ===&lt;br /&gt;
&lt;br /&gt;
Before text content is displayed to the user, any URLs in the &#039;@@PLUGINFILE@@/&#039; form in the content need to be rewritten to the real URL where the user can access the files. &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$messagetext = file_rewrite_pluginfile_urls($messagetext, &#039;pluginfile.php&#039;,&lt;br /&gt;
        $context-&amp;gt;id, &#039;mod_mymodule&#039;, &#039;proper_file_area&#039;, $itemid);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
; $messagetext : is the content containing the @@PLUGINFILE@@ URLs from the database.&lt;br /&gt;
; &#039;pluginfile.php&#039; : there are a number of different scripts that can serve files with different permissions checks. You need to specify which one to use.&lt;br /&gt;
; $context-&amp;gt;id, &#039;mod_mymodule&#039;, &#039;proper_file_area&#039;, $itemid : uniquely identifies the file area, as before.&lt;br /&gt;
&lt;br /&gt;
=== Implement file serving access control ===&lt;br /&gt;
&lt;br /&gt;
Attachments and embedded images should have the same access control like the text itself, in majority of cases these files are served using pluginfile.php. Access control is defined in &#039;&#039;module/lib.php&#039;&#039; file in function &#039;&#039;module_pluginfile()&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== File browsing support ==&lt;br /&gt;
Only owner of each file area is allowed to use low level File API function to access files, other parts of Moodle should use file browsing API.&lt;br /&gt;
&lt;br /&gt;
Activities may specify browsing support in own module/lib.php file by implementing functions module_get_file_areas() and module_get_file_info().&lt;br /&gt;
&lt;br /&gt;
== Upgrading your code ==&lt;br /&gt;
Here I will attempt to describe some simple steps you can take to upgrade your file-handling form elements from pre-2.0 code to 2.0. We will use the example of glossary, since it has been used above.&lt;br /&gt;
&lt;br /&gt;
=== Preparing your options ===&lt;br /&gt;
Unless you are happy with the defaults, you will need to define an array of options for each file-handling form element. You could define it at different places, but it&#039;s best to put it in one place and make the array(s) available to other files if they need it. In the majority of cases, this will be in a file like edit.php&lt;br /&gt;
&lt;br /&gt;
Previous code in mod/glossary/edit.php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform =&amp;amp; new mod_glossary_entry_form(null, compact(&#039;cm&#039;, &#039;glossary&#039;, &#039;hook&#039;, &#039;mode&#039;, &#039;e&#039;, &#039;context&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
New code:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$maxbytes = $course-&amp;gt;maxbytes;&lt;br /&gt;
// Could also use $CFG-&amp;gt;maxbytes if you are not coding within a course context&lt;br /&gt;
&lt;br /&gt;
$definitionoptions = array(&#039;subdirs&#039;=&amp;gt;false, &#039;maxfiles&#039;=&amp;gt;99, &#039;maxbytes&#039;=&amp;gt;$maxbytes, &#039;trusttext&#039;=&amp;gt;true,&lt;br /&gt;
                           &#039;context&#039;=&amp;gt;$context);&lt;br /&gt;
$attachmentoptions = array(&#039;subdirs&#039;=&amp;gt;false, &#039;maxfiles&#039;=&amp;gt;99, &#039;maxbytes&#039;=&amp;gt;$maxbytes);&lt;br /&gt;
$mform = new mod_glossary_entry_form(null, array(&#039;current&#039;=&amp;gt;$entry, &#039;cm&#039;=&amp;gt;$cm, &#039;glossary&#039;=&amp;gt;$glossary,&lt;br /&gt;
                                                 &#039;definitionoptions&#039;=&amp;gt;$definitionoptions, &lt;br /&gt;
                                                 &#039;attachmentoptions&#039;=&amp;gt;$attachmentoptions));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Note that the data being passed to the form constructor have changed also, but this is not part of the file API changes, I just include them to avoid confusion.&lt;br /&gt;
&lt;br /&gt;
These options are for the htmleditor (definition field) and the filemanager (attachment field). They are used by a file called edit_form.php.&lt;br /&gt;
&lt;br /&gt;
=== Element preparation ===&lt;br /&gt;
Before we look at this, however, we need to &amp;quot;prepare&amp;quot; the elements so that they can correctly display existing embedded images and attached files when you are editing a record instead of just creating one. So, let&#039;s take the code we&#039;ve got so far in edit.php and add to it:&lt;br /&gt;
&lt;br /&gt;
Currently upgraded code in edit.php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform = new mod_glossary_entry_form(null, array(&lt;br /&gt;
        &#039;current&#039;=&amp;gt;$entry, &lt;br /&gt;
        &#039;cm&#039;=&amp;gt;$cm, &lt;br /&gt;
        &#039;glossary&#039;=&amp;gt;$glossary,&lt;br /&gt;
        &#039;definitionoptions&#039;=&amp;gt;$definitionoptions, &lt;br /&gt;
        &#039;attachmentoptions&#039;=&amp;gt;$attachmentoptions));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
New code with element preparation:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$entry = file_prepare_standard_editor($entry, &#039;definition&#039;, $definitionoptions, $context,&lt;br /&gt;
                                      &#039;mod_glossary&#039;, &#039;entry&#039;, $entry-&amp;gt;id);&lt;br /&gt;
$entry = file_prepare_standard_filemanager($entry, &#039;attachment&#039;, $attachmentoptions, $context,&lt;br /&gt;
                                           &#039;mod_glossary&#039;, &#039;attachment&#039;, $entry-&amp;gt;id);&lt;br /&gt;
$mform = new mod_glossary_entry_form(null, array(&#039;current&#039;=&amp;gt;$entry, &#039;cm&#039;=&amp;gt;$cm, &#039;glossary&#039;=&amp;gt;$glossary,&lt;br /&gt;
                                                 &#039;definitionoptions&#039;=&amp;gt;$definitionoptions, &lt;br /&gt;
                                                 &#039;attachmentoptions&#039;=&amp;gt;$attachmentoptions));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Things to note:&lt;br /&gt;
* $entry in this case is simply a stdClass object which may either represent a new glossary entry or an existing one.&lt;br /&gt;
* $entry-&amp;gt;id must be the unique identifier for the current object. If we are creating a new entry, it will be null, but in all cases it must be defined.&lt;br /&gt;
* These two functions (file_prepare_standard_editor and file_prepare_standard_filemanager) are shortcuts functions that take care of some of the tedious setting up for you, but they make a couple of assumptions:&lt;br /&gt;
*# You &#039;&#039;&#039;must&#039;&#039;&#039; name the form element as {element}_editor or {element}_filemanager (see next section)&lt;br /&gt;
*# You &#039;&#039;&#039;must&#039;&#039;&#039; have at least the following fields in the database: {element} and {element}summary, as described earlier in this documentation&lt;br /&gt;
&lt;br /&gt;
We can now look at the upgrades needed in the form definition file.&lt;br /&gt;
&lt;br /&gt;
=== Form definition ===&lt;br /&gt;
Previous code in mod/glossary/edit_form.php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;htmleditor&#039;, &#039;definition&#039;, get_string(&#039;definition&#039;, &#039;glossary&#039;),&lt;br /&gt;
                    array(&#039;rows&#039;=&amp;gt;20));&lt;br /&gt;
$mform-&amp;gt;setType(&#039;definition&#039;, PARAM_RAW);&lt;br /&gt;
$mform-&amp;gt;addRule(&#039;definition&#039;, null, &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
$mform-&amp;gt;setHelpButton(&#039;definition&#039;, array(&#039;writing&#039;, &#039;richtext&#039;), false, &#039;editorhelpbutton&#039;);&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;format&#039;);&lt;br /&gt;
// a bit further...&lt;br /&gt;
$this-&amp;gt;set_upload_manager(new upload_manager(&#039;attachment&#039;, true, false, $COURSE, false, 0,&lt;br /&gt;
                                             true, true, false));&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;file&#039;, &#039;attachment&#039;, get_string(&#039;attachment&#039;, &#039;forum&#039;));&lt;br /&gt;
$mform-&amp;gt;setHelpButton(&#039;attachment&#039;, array(&#039;attachment&#039;, get_string(&#039;attachment&#039;, &#039;glossary&#039;),&lt;br /&gt;
                      &#039;glossary&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
New code:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$definitionoptions = $this-&amp;gt;_customdata[&#039;definitionoptions&#039;];&lt;br /&gt;
$attachmentoptions = $this-&amp;gt;_customdata[&#039;attachmentoptions&#039;];&lt;br /&gt;
// a bit further...&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;editor&#039;, &#039;definition_editor&#039;, get_string(&#039;definition&#039;, &#039;glossary&#039;), null,&lt;br /&gt;
                   $definitionoptions);&lt;br /&gt;
$mform-&amp;gt;setType(&#039;definition_editor&#039;, PARAM_RAW);&lt;br /&gt;
$mform-&amp;gt;addRule(&#039;definition_editor&#039;, get_string(&#039;required&#039;), &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
// a bit further...&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;attachment_filemanager&#039;, get_string(&#039;attachment&#039;, &#039;glossary&#039;),&lt;br /&gt;
                   null, $attachmentoptions);&lt;br /&gt;
$mform-&amp;gt;setHelpButton(&#039;attachment_filemanager&#039;, array(&#039;attachment2&#039;,&lt;br /&gt;
                      get_string(&#039;attachment&#039;, &#039;glossary&#039;), &#039;glossary&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the following:&lt;br /&gt;
* The format element and the help button are no longer required for the HTML editor element&lt;br /&gt;
* The name of the form element needs to be changed by adding &#039;_editor&#039; or &#039;_manager&#039; to the original name. This is a naming convention that is used by a couple of functions we will look at shortly&lt;br /&gt;
* Make sure $definitionoptions has context parameter, else system context is used and editor will not respect filter settings.&lt;br /&gt;
&lt;br /&gt;
=== Handling submitted data ===&lt;br /&gt;
The final step is to handle the submitted data properly, i.e. retrieve the files and save them to disk, associating them with the record we have just created (a glossary entry in our example). This happens in edit.php:&lt;br /&gt;
&lt;br /&gt;
Previous code in edit.php:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Section that updates an entry:&lt;br /&gt;
$todb-&amp;gt;id = $e;&lt;br /&gt;
$dir = glossary_file_area_name($todb);&lt;br /&gt;
if ($mform-&amp;gt;save_files($dir) and $newfilename = $mform-&amp;gt;get_new_filename()) {&lt;br /&gt;
    $todb-&amp;gt;attachment = $newfilename;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Section that adds an entry:&lt;br /&gt;
if ($todb-&amp;gt;id = insert_record(&amp;quot;glossary_entries&amp;quot;, $todb)) {&lt;br /&gt;
    $e = $todb-&amp;gt;id;&lt;br /&gt;
    $dir = glossary_file_area_name($todb);&lt;br /&gt;
    if ($mform-&amp;gt;save_files($dir) and $newfilename = $mform-&amp;gt;get_new_filename()) {&lt;br /&gt;
        set_field(&amp;quot;glossary_entries&amp;quot;, &amp;quot;attachment&amp;quot;, $newfilename, &amp;quot;id&amp;quot;, $todb-&amp;gt;id);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
New code:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// $todb was renamed to $entry, and the code was refactored &lt;br /&gt;
// so that the file-handling code is only used once for either an add or an update action.&lt;br /&gt;
// If an entry is being added, $DB-&amp;gt;insert() has already been called, so we have a valid $entry-&amp;gt;id&lt;br /&gt;
$entry = file_postupdate_standard_editor($entry, &#039;definition&#039;, $definitionoptions, $context,&lt;br /&gt;
                                         &#039;mod_glossary&#039;, &#039;entry&#039;, $entry-&amp;gt;id);&lt;br /&gt;
$entry = file_postupdate_standard_filemanager($entry, &#039;attachment&#039;, $attachmentoptions, $context,&lt;br /&gt;
                                              &#039;mod_glossary&#039;, &#039;attachment&#039;, $entry-&amp;gt;id);&lt;br /&gt;
// store the updated value values&lt;br /&gt;
$DB-&amp;gt;update_record(&#039;glossary_entries&#039;, $entry);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Things to note:&lt;br /&gt;
* If you are adding a new record, you will still need to call update_record after calling the file_postupdate* functions&lt;br /&gt;
&lt;br /&gt;
=== Gotchas ===&lt;br /&gt;
A few things to keep in mind:&lt;br /&gt;
* Make sure that you instantiate the moodle form before any call to $OUTPUT-&amp;gt;header()&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[File API]]&lt;br /&gt;
* [[Using the file API]]&lt;br /&gt;
* [[Repository plugins]]&lt;br /&gt;
* [[Portfolio API]]&lt;br /&gt;
* MDL-14589 - File API Meta issue&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=207748 Adding a text editor to a Moodle form]&lt;br /&gt;
&lt;br /&gt;
[[Category:Files]]&lt;br /&gt;
[[Category:Repositories]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=47594</id>
		<title>PHPUnit</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=47594"/>
		<updated>2015-04-14T12:03:11Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Added note about overriding database settings&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.3}}&lt;br /&gt;
&lt;br /&gt;
=What is PHPUnit=&lt;br /&gt;
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP. It is installed as Composer dependency and is not part of Moodle installation. To run PHPUnit tests, you have to manually install it on your development computer or test server.&lt;br /&gt;
&lt;br /&gt;
Read the excellent guide at&lt;br /&gt;
* [http://phpunit.de/manual/current/en/index.html PHPUnit Manual]&lt;br /&gt;
&lt;br /&gt;
=Installation of PHPUnit via Composer=&lt;br /&gt;
&lt;br /&gt;
The installation procedure depends on your operating system.&lt;br /&gt;
* Install Composer dependency manager&lt;br /&gt;
&lt;br /&gt;
 cd /your/moodle/dirroot&lt;br /&gt;
 curl -s https://getcomposer.org/installer | php&lt;br /&gt;
&lt;br /&gt;
On Windows go to https://getcomposer.org/download/ and download the Composer-Setup.exe file&lt;br /&gt;
&lt;br /&gt;
* Execute Composer installer&lt;br /&gt;
&lt;br /&gt;
 cd /your/moodle/dirroot&lt;br /&gt;
 php composer.phar install&lt;br /&gt;
&lt;br /&gt;
(If that gives you connection problems try...)&lt;br /&gt;
&lt;br /&gt;
 php composer.phar install --dev --prefer-source&lt;br /&gt;
&lt;br /&gt;
Troubleshooting:&lt;br /&gt;
* On Windows if you are behind a proxy you will need to setup an environment variable called HTTP_PROXY with a value detailing your HTTP Proxy address and port before composer will correctly download files.&lt;br /&gt;
* You may be prompted for github credentials when installing composer dependencies.&lt;br /&gt;
** This is used to generate an personal access token to avoid being rate limited by github.&lt;br /&gt;
** If you have Two Factor Authentication enabled on your github account, or do not wish to supply your own credentials you will need to generate a token manually:&lt;br /&gt;
*** Visit https://github.com/settings/applications and request personal access token&lt;br /&gt;
*** Copy this token and add it to your [https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]&lt;br /&gt;
** ( See [https://github.com/composer/composer/issues/2280 composer issue #2280] for further details of this bug.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [http://getcomposer.org/doc/00-intro.md Composer documentation]&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [[PHPUnit installation in Windows]]&lt;br /&gt;
* [[PHPUnit installation in OS X]]&lt;br /&gt;
&lt;br /&gt;
== Uninstalling previous PEAR based version ==&lt;br /&gt;
&lt;br /&gt;
Before using composer, this page used to suggest to install phpunit via PEAR. If you did so, you may wish to uninstall that package now. This should work:&lt;br /&gt;
&lt;br /&gt;
   $ pear uninstall phpunit/DbUnit&lt;br /&gt;
   $ pear uninstall phpunit/PHPUnit&lt;br /&gt;
&lt;br /&gt;
=Initialisation of test environment=&lt;br /&gt;
&lt;br /&gt;
Our PHPUnit integration requires a dedicated database and dataroot.  First, add a new dataroot directory and prefix into your config.php, you can find examples in config-dist.php (scroll down to &#039;Section 9&#039;).&lt;br /&gt;
&lt;br /&gt;
 $CFG-&amp;gt;phpunit_prefix = &#039;phpu_&#039;;&lt;br /&gt;
 $CFG-&amp;gt;phpunit_dataroot = &#039;/home/example/phpu_moodledata&#039;;&lt;br /&gt;
&lt;br /&gt;
If your system does not have a direct connection to the Internet, you also need to specify your proxy in config.php - even though you normally specify it by using the admin settings user interface. (If you do not use a proxy, you can skip this step.) Check the settings on the relevant admin settings page, or from the mdl_config table in your database, if you are unsure of the correct values.&lt;br /&gt;
 &lt;br /&gt;
 // Normal proxy settings&lt;br /&gt;
 $CFG-&amp;gt;proxyhost = &#039;wwwcache.example.org&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxyport = 80;&lt;br /&gt;
 $CFG-&amp;gt;proxytype = &#039;HTTP&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxybypass = &#039;localhost, 127.0.0.1, .example.org&#039;;&lt;br /&gt;
 // Omit the next lines if your proxy doesn&#039;t need a username/password:&lt;br /&gt;
 $CFG-&amp;gt;proxyuser = &#039;systemusername&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxypassword = &#039;systempassword&#039;;&lt;br /&gt;
&lt;br /&gt;
From Moodle 2.8.5 onwards, you can also provide specific database settings for unit testing (if these are not provided, the standard database settings will be used):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$CFG-&amp;gt;phpunit_dbtype    = &#039;pgsql&#039;;      // &#039;pgsql&#039;, &#039;mariadb&#039;, &#039;mysqli&#039;, &#039;mssql&#039;, &#039;sqlsrv&#039; or &#039;oci&#039;&lt;br /&gt;
$CFG-&amp;gt;phpunit_dblibrary = &#039;native&#039;;     // &#039;native&#039; only at the moment&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbhost    = &#039;127.0.0.1&#039;;  // eg &#039;localhost&#039; or &#039;db.isp.com&#039; or IP&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbname    = &#039;mytestdb&#039;;     // database name, eg moodle&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbuser    = &#039;postgres&#039;;   // your database username&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbpass    = &#039;some_password&#039;;   // your database password&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you need to initialise the test environment using following command.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 php admin/tool/phpunit/cli/init.php&lt;br /&gt;
&lt;br /&gt;
This command has to be repeated after any upgrade, plugin (un)installation or if you have added tests to a plugin you are developing:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; make sure that your php cli executable (or the one you want to use) is correctly on your path as the individual init scripts will call it repeatedly.&lt;br /&gt;
&lt;br /&gt;
=Test execution=&lt;br /&gt;
&lt;br /&gt;
To execute all test suites from main configuration file execute the &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; script from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 vendor/bin/phpunit&lt;br /&gt;
&lt;br /&gt;
The rest of examples uses &amp;lt;code&amp;gt;phpunit&amp;lt;/code&amp;gt;, please substitute it with &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; or create a shortcut in your dirroot.&lt;br /&gt;
&lt;br /&gt;
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to &amp;lt;code&amp;gt;phpunit.xml&amp;lt;/code&amp;gt; from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is an alternative script for running of tests via web interface: &amp;lt;code&amp;gt;admin/tool/phpunit/webrunner.php&amp;lt;/code&amp;gt;. Use this as the last resort only when you cannot use the command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to run only some tests===&lt;br /&gt;
&lt;br /&gt;
==== Running a single test quickly ====&lt;br /&gt;
&lt;br /&gt;
The fastest way to run a single test is:&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit my_test_class_name my/tests/filename.php&lt;br /&gt;
&lt;br /&gt;
This is faster than other methods because it avoids the need to search for the test file, but you should be careful because it may be possible to run tests this way which are not included in the normal run. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.&lt;br /&gt;
&lt;br /&gt;
Please note the path name is optional if the testcase class and file names are standardised as described in [[Writing PHPUnit tests]].&lt;br /&gt;
&lt;br /&gt;
==== Using the @group annotation ====&lt;br /&gt;
&lt;br /&gt;
If you add annotations like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for {@link stack_cas_keyval}.&lt;br /&gt;
 * @group qtype_stack&lt;br /&gt;
 */&lt;br /&gt;
class qtype_stack_cas_keyval_exception_testcase extends basic_testcase {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to all the classes in your plugin, then you can run just the tests for your plugin by doing&lt;br /&gt;
&lt;br /&gt;
 phpunit --group qtype_stack&lt;br /&gt;
&lt;br /&gt;
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.&lt;br /&gt;
&lt;br /&gt;
==== Using multiple phpunit.xml files ====&lt;br /&gt;
&lt;br /&gt;
It&#039;s easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml&lt;br /&gt;
&lt;br /&gt;
Also, for commodity, you can use this command:&lt;br /&gt;
&lt;br /&gt;
 php admin/tool/phpunit/cli/util.php --buildcomponentconfigs&lt;br /&gt;
&lt;br /&gt;
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // Note that it&#039;s not needed to specify the name of the file (if it is &#039;phpunit.xml&#039;).&lt;br /&gt;
 vendor/bin/phpunit -c question&lt;br /&gt;
 vendor/bin/phpunit -c question/type/calculated&lt;br /&gt;
 vendor/bin/phpunit -c backup&lt;br /&gt;
 vendor/bin/phpunit -c lib/dml&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
or, also&lt;br /&gt;
&lt;br /&gt;
 cd directory/with/phpunit.xml&lt;br /&gt;
 phpunit&lt;br /&gt;
&lt;br /&gt;
=External test resources=&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
By default Moodle phpunit tests contact http://download.moodle.org server when testing curl related functionality. Optionally you may checkout a local copy of the test scripts and access it instead:&lt;br /&gt;
&lt;br /&gt;
# clone https://github.com/moodlehq/moodle-exttests to web directory&lt;br /&gt;
# add to your config.php or modify phpunit.xml file &amp;lt;code php&amp;gt;define(&#039;TEST_EXTERNAL_FILES_HTTP_URL&#039;, &#039;http://localhost/moodle-exttests&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Writing new tests=&lt;br /&gt;
* read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation]&lt;br /&gt;
* see [[Writing PHPUnit tests]]&lt;br /&gt;
&lt;br /&gt;
=Conversion of existing SimpleTests=&lt;br /&gt;
* see [[SimpleTest conversion]]&lt;br /&gt;
&lt;br /&gt;
=PHPUnit support in IDEs=&lt;br /&gt;
&lt;br /&gt;
* [[Setting up Eclipse]]&lt;br /&gt;
* [[Setting up Netbeans]]&lt;br /&gt;
* [[Setting up PhpStorm]]&lt;br /&gt;
&lt;br /&gt;
=Common Unit Test Problems=&lt;br /&gt;
[[Common unit test problems]]&lt;br /&gt;
&lt;br /&gt;
=Performance=&lt;br /&gt;
&lt;br /&gt;
A typical run of full PHPUnit tests for Moodle 2.7 takes 10-20 minutes depending on the machine. If tests run slowly for you:&lt;br /&gt;
&lt;br /&gt;
* Ensure you are using a database and filesystem running on the same machine that is running the tests (not on a remote server).&lt;br /&gt;
* Apply developer-only performance settings to your database: [[Postgres Tuning For Developers]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Unit testing]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Events_API&amp;diff=47369</id>
		<title>Events API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Events_API&amp;diff=47369"/>
		<updated>2015-02-12T12:40:54Z</updated>

		<summary type="html">&lt;p&gt;Maherne: Added note about floating point numbers not being allowed in &amp;quot;other&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox Project&lt;br /&gt;
|name = Events 2&lt;br /&gt;
|state = Completed&lt;br /&gt;
|tracker = MDL-39797 , MDL-39952, MDL-39846&lt;br /&gt;
|discussion = https://moodle.org/mod/forum/discuss.php?d=229425&lt;br /&gt;
|assignee = Backend Team&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= What are events? =&lt;br /&gt;
&lt;br /&gt;
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 [[:en:Cron|cron]] process or administration actions [[:en:Administration via command line|undertaken via the command line]].&lt;br /&gt;
&lt;br /&gt;
When an action takes place, an event is created by a [[Core APIs|core API]] or [[Plugins|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.&lt;br /&gt;
&lt;br /&gt;
= Why was a new events system needed? =&lt;br /&gt;
&lt;br /&gt;
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 are useful to other parts of Moodle that observe event information.&lt;br /&gt;
&lt;br /&gt;
* The events need to be more strictly defined for logging and other advanced use cases. They need to contain more information, organised in a standardised way (in addition to the fields from the legacy log table and log_actions table).&lt;br /&gt;
* Complex data types were allowed in old events, which was causing major problems when serialising/storing/unserialising the data.&lt;br /&gt;
* The logging tables and events contain similar information and were triggered at the same places; utilising events for logging would remove this code duplication. All events should be loggable and all current log entries should be triggered as events.&lt;br /&gt;
* The logging system is an event observer, listening to all events and directing them to logging storage plugins in a controllable way.&lt;br /&gt;
* It is possible to subscribe to &#039;*&#039; event, which would allow a system to potentially observe, and selectively deal with, all events. Previously, handlers did not receive event names which made this problematic.&lt;br /&gt;
* Previously, event handlers could trigger exceptions during site upgrades, which would lead to fatal upgrade problems. The new design eliminates this.&lt;br /&gt;
* Failure in previous handlers blocked dispatching of subsequent events. Problems in new observers would only be logged and execution would continue normally.&lt;br /&gt;
* Previously, execution of external handlers during DB transactions blocked other handlers. This would be eliminated by in-memory buffer for external events.&lt;br /&gt;
* Previously there was no observer priority.&lt;br /&gt;
* Previously, nested events were not dispatched sequentially, which would change the order of events were received by lower priority handlers.&lt;br /&gt;
&lt;br /&gt;
= Performance =&lt;br /&gt;
Some basic profiling was conducted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;This next two paragraphs need to be replaced with results.&amp;lt;/p&amp;gt;&lt;br /&gt;
There is a general plan to complete pre- and post-implementation testing as development happens. The new system will be implemented in parallel with the old one, which should help with comparison of new and old logging and event triggering performance on each page.&lt;br /&gt;
&lt;br /&gt;
Our aim is to capture more information about actions in Moodle by triggering more events and logging more information about each event. This is going to impact negatively 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.&lt;br /&gt;
&lt;br /&gt;
More details will be added to this section soon.&lt;br /&gt;
&lt;br /&gt;
= Events API =&lt;br /&gt;
&lt;br /&gt;
Each plugin defines the events that it can report (trigger) by extending an abstract base class, once for each possible event. This approach has several benefits.&lt;br /&gt;
; Events are active objects&lt;br /&gt;
: When they are triggered and possibly after they are reinstantiated (say, when they are retrieved from a log), an event object is able to provide callback functions for various purposes (such as event description).&lt;br /&gt;
; Automatic inclusion&lt;br /&gt;
: Event class definitions are automatically included when needed, without having to maintain lists of known event types. During development new event definitions and observers can be added without the need to upgrade, only purging of the MUC cache is required.&lt;br /&gt;
; Maintainability&lt;br /&gt;
: It is relatively simple to add new events and migrate existing events. Code review is simplified because there is less duplication of code when triggering same events and all event related information/code is concentrated in one file in fixed locations.&lt;br /&gt;
; Self documenting&lt;br /&gt;
: The behaviour of events is combined with the definition of events in one place (file). It is easy for event observer writers to know what events a plugin can trigger. This includes support for autocompletion and code inspection in modern IDEs. A list of all events is now available as a report to administrators and researchers.&lt;br /&gt;
; Quick, self-validating data structure&lt;br /&gt;
: 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 unintentional typos.&lt;br /&gt;
&lt;br /&gt;
== Backwards compatibility and migration ==&lt;br /&gt;
&lt;br /&gt;
Events:&lt;br /&gt;
* All legacy events in the standard distribution have been replaced with the new events API and events_trigger() has been deprecated.&lt;br /&gt;
* For events that already exist in Moodle 2.5, the additional legacy information has been added to the event data (in properties &#039;legacyeventname&#039; and &#039;legacyeventdata&#039;)&lt;br /&gt;
* The legacy events handling code is maintained  separately and will continue being supported in Moodle 2.7.&lt;br /&gt;
* All legacy events-handlers have been migrated to new observers in standard distribution.&lt;br /&gt;
* In future, more subsystems may be migrated to events-observers, ex.: gradebook history, completion.&lt;br /&gt;
&lt;br /&gt;
Logging:&lt;br /&gt;
* The function add_to_log() has been deprecated with a notice as of Moodle 2.7.&lt;br /&gt;
* Existing add_to_log() parameters have been migrated to method get_legacy_log_data() in new events. Calls to core_event_base::trigger() will lead to entries being added to the legacy log table if the legacy log plugin is enabled.&lt;br /&gt;
&lt;br /&gt;
See https://docs.moodle.org/dev/Migrating_logging_calls_in_plugins&lt;br /&gt;
&lt;br /&gt;
== Event dispatching and observers ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Event observers ===&lt;br /&gt;
&lt;br /&gt;
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;&lt;br /&gt;
* eventname – fully qualified event class name or &amp;quot;*&amp;quot; indicating all events, ex.: &#039;&#039;\plugintype_pluginname\event\something_happened&#039;&#039;.&lt;br /&gt;
* callback - PHP callable type.&lt;br /&gt;
* includefile - optional. File to be included before calling the observer. Path relative to dirroot.&lt;br /&gt;
* priority - optional. Defaults to 0. Observers with higher priority are notified first.&lt;br /&gt;
* internal - optional. Defaults to true. Non-internal observers are not called during database transactions, but instead after a successful commit of the transaction.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$observers = array(&lt;br /&gt;
&lt;br /&gt;
    array(&lt;br /&gt;
        &#039;eventname&#039;   =&amp;gt; &#039;\core\event\sample_executed&#039;,&lt;br /&gt;
        &#039;callback&#039;    =&amp;gt; &#039;core_event_sample_observer::observe_one&#039;,&lt;br /&gt;
    ),&lt;br /&gt;
&lt;br /&gt;
    array(&lt;br /&gt;
        &#039;eventname&#039;   =&amp;gt; &#039;\core\event\sample_executed&#039;,&lt;br /&gt;
        &#039;callback&#039;    =&amp;gt; &#039;core_event_sample_observer::external_observer&#039;,&lt;br /&gt;
        &#039;priority&#039;    =&amp;gt; 200,&lt;br /&gt;
        &#039;internal&#039;    =&amp;gt; false,&lt;br /&gt;
    ),&lt;br /&gt;
&lt;br /&gt;
    array(&lt;br /&gt;
        &#039;eventname&#039;   =&amp;gt; &#039;*&#039;,&lt;br /&gt;
        &#039;callback&#039;    =&amp;gt; &#039;core_event_sample_observer::observe_all&#039;,&lt;br /&gt;
        &#039;includefile&#039; =&amp;gt; null,&lt;br /&gt;
        &#039;internal&#039;    =&amp;gt; true,&lt;br /&gt;
        &#039;priority&#039;    =&amp;gt; 9999,&lt;br /&gt;
    ),&lt;br /&gt;
&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE: Event observers are cached. If you add or change observers you need to purge the caches or they will not be recognised. Plugin developers need to bump up the version number to guarantee that the list of observers is reloaded during upgrade.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Event dispatching ===&lt;br /&gt;
&lt;br /&gt;
A list of available observers is constructed on the fly directly from all available &#039;events.php&#039; files from core and all plugins. Previously handlers were installed only during installation and upgrade. This has little 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.&lt;br /&gt;
&lt;br /&gt;
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. Original handlers could not throw any exceptions at any time.&lt;br /&gt;
&lt;br /&gt;
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 of the current event are notified.&lt;br /&gt;
&lt;br /&gt;
=== Differences from old event handling ===&lt;br /&gt;
&lt;br /&gt;
# New events contain a lot more structured information.&lt;br /&gt;
# There is a separate record snapshot cache that may be used when deleting data or for observer performance improvements.&lt;br /&gt;
# There is no database access in new event dispatching code.&lt;br /&gt;
# There is no support for delayed cron execution. This eliminates performance problems, simplifies events dispatching and prevents abuse of cron events.&lt;br /&gt;
# Events triggered in observers are processed in a different order.&lt;br /&gt;
# External events are buffered when a transaction is in progress, instead of being sent to the cron queue.&lt;br /&gt;
# It is possible to define multiple observers for one event in one events.php file.&lt;br /&gt;
# It is possible to subscribe an observer to all events.&lt;br /&gt;
# The new event manager is using frankenstyle autoloading, which leads to a smaller memory footprint when events are not used on the current page.&lt;br /&gt;
&lt;br /&gt;
== Triggering events ==&lt;br /&gt;
&lt;br /&gt;
* All event definitions are classes extending the \core\event\base class.&lt;br /&gt;
* Events are triggered by creating a new instance of the class event and executing $event-&amp;gt;trigger().&lt;br /&gt;
* Event class name is a unique identifier of each event.&lt;br /&gt;
* Class names and namespace follow the identifier scheme \&#039;&#039;&#039;frankenstyle_component&#039;&#039;&#039;\event\&#039;&#039;&#039;some_object_action&#039;&#039;&#039;. Core events are in namespace &#039;\core\event\&#039;.&lt;br /&gt;
* Each event class is defined in a separate file. File name and location must match the class name for the use of auto loading, for example: &#039;&#039;&#039;plugindir&#039;&#039;&#039;/classes/event/&#039;&#039;&#039;something_happened&#039;&#039;&#039;.php&lt;br /&gt;
* The event identifier suffix has the form &#039;&#039;some_object_action&#039;&#039;  (&#039;&#039;&#039;something_happened&#039;&#039;&#039; in the example above) and should follow the standard naming convention. The last word after the underscore is automatically parsed as its action, the rest of word is its object.&lt;br /&gt;
&lt;br /&gt;
Decision:[[#Verb_list| Recommended verb list]]&lt;br /&gt;
&lt;br /&gt;
Examples: \core\event\course_completed, \mod_assign\event\submission_commented, \mod_forum\event\post_shared, \mod_forum\event\post_responded, etc.&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
An example of triggering an event:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$event = \mod_myplugin\event\something_happened::create(array(&#039;context&#039; =&amp;gt; $context, &#039;objectid&#039; =&amp;gt; YYY, &#039;other&#039; =&amp;gt; ZZZ));&lt;br /&gt;
// ... code that may add some record snapshots&lt;br /&gt;
$event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Use of PHP autoloading ===&lt;br /&gt;
&lt;br /&gt;
All new OOP APIs in Moodle 2.6 onwards are going to use class auto loading - see [[Automatic class loading]]. New events use PHP within strictly defined namespaces, which concentrate all event classes in the classes/event/ subdirectory.&lt;br /&gt;
&lt;br /&gt;
=== Why separate classes? ===&lt;br /&gt;
Pros&lt;br /&gt;
* Maintainability - It is much easier to review, debug and integrate.&lt;br /&gt;
* Self documenting, behaviour is combined with definition.&lt;br /&gt;
* It is extremely flexible for plugin developers and core devs too.&lt;br /&gt;
* Automatic lists events can be generated without being installed - PHPDocs as events documentation.&lt;br /&gt;
* It is included only when needed using autoloading.&lt;br /&gt;
* Self-validating data structure (by PHP).&lt;br /&gt;
* Some developers will find it easier to copy whole class files as templates.&lt;br /&gt;
Cons&lt;br /&gt;
* 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).&lt;br /&gt;
* 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).&lt;br /&gt;
* This has increased the number lines of code in Moodle for events and logging.&lt;br /&gt;
&lt;br /&gt;
== Information contained in events ==&lt;br /&gt;
&lt;br /&gt;
Events have to contain as much information as they can, but this should not affect the performance. That&#039;s why part of the information is available in properties, and the rest via methods. This allows for delayed computation of data until the time it is really needed, if it ever is.&lt;br /&gt;
&lt;br /&gt;
=== Properties ===&lt;br /&gt;
&lt;br /&gt;
The following is a list of properties that the developer has to pass to the event upon creation, or ones that can be automatically generated where possible and cost free. Some of these properties are not mandatory.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Property name&lt;br /&gt;
! Title&lt;br /&gt;
! Type&lt;br /&gt;
! Required&lt;br /&gt;
! Comment&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;eventname&#039;&#039;&lt;br /&gt;
| Event name&lt;br /&gt;
| &#039;&#039;static, automatic from class name&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| Automatically computed by copying class name&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;component&#039;&#039;&lt;br /&gt;
| Component&lt;br /&gt;
| &#039;&#039;static, automatic from top namespace&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| Component declaring the event, automatically computed from class name.&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;action&#039;&#039;&lt;br /&gt;
| Action&lt;br /&gt;
| &#039;&#039;static, automatic from last word in class name&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| Can be automatically computed from class name.&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;target&#039;&#039;&lt;br /&gt;
| target of action&lt;br /&gt;
| &#039;&#039;static, automatic from class name&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| Target on which the action is taken, can be automatically computed from class name.&lt;br /&gt;
|-&lt;br /&gt;
| objecttable&lt;br /&gt;
| Database table name&lt;br /&gt;
| static&lt;br /&gt;
| optional (Must be set if objectid present)&lt;br /&gt;
| Database table name which represents the event object to the best. Never use a relationship table here.&lt;br /&gt;
|-&lt;br /&gt;
| objectid&lt;br /&gt;
| Object ID&lt;br /&gt;
| variable&lt;br /&gt;
| Optional (Must be set if objettable present)&lt;br /&gt;
| Id of the object record from objecttable.&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;&#039;&#039;crud&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
| Transaction type&lt;br /&gt;
| &#039;&#039;static&#039;&#039;&lt;br /&gt;
| optional&lt;br /&gt;
| One of [crud] letters - indicating &#039;c&#039;reate, &#039;r&#039;ead, &#039;u&#039;pdate or &#039;d&#039;elete operation. Statically declared in the event class method init().&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;&#039;&#039;level&#039;&#039;&#039;&#039;&#039; / &#039;&#039;&#039;&#039;&#039;edulevel&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
| Level&lt;br /&gt;
| &#039;&#039;static&#039;&#039;&lt;br /&gt;
| optional&lt;br /&gt;
| Level of educational value of the event. Statically declared in the event class method init(). Changed from &#039;&#039;&#039;&#039;&#039;level&#039;&#039;&#039;&#039;&#039; to &#039;&#039;&#039;&#039;&#039;edulevel&#039;&#039;&#039;&#039;&#039; in Moodle 2.7 (See below for more details)&lt;br /&gt;
|-&lt;br /&gt;
| contextid&lt;br /&gt;
| Context ID&lt;br /&gt;
| mandatory&lt;br /&gt;
| -&lt;br /&gt;
|-&lt;br /&gt;
| contextlevel&lt;br /&gt;
| Context level&lt;br /&gt;
| automatic from context&lt;br /&gt;
| -&lt;br /&gt;
| This tells you if it was a course, activity, course category, etc.&lt;br /&gt;
|-&lt;br /&gt;
| contextinstanceid&lt;br /&gt;
| Context instanceid&lt;br /&gt;
| automatic from context&lt;br /&gt;
| -&lt;br /&gt;
| Based on context level this may be course id , course module id, course category, etc. (event-&amp;gt;contextinstanceid)&lt;br /&gt;
|-&lt;br /&gt;
| userid&lt;br /&gt;
| User ID&lt;br /&gt;
| defaults to current user&lt;br /&gt;
| -&lt;br /&gt;
| User ID, or 0 when not logged in, or -1 when other (System, CLI, Cron, ...)&lt;br /&gt;
|-&lt;br /&gt;
| courseid&lt;br /&gt;
| Affected course&lt;br /&gt;
| defaults to course context from context&lt;br /&gt;
| -&lt;br /&gt;
| This is used only for contexts at and bellow course level - this can be used to filter events by course (includes all course activities)&lt;br /&gt;
|-&lt;br /&gt;
| relateduserid&lt;br /&gt;
| Affected user&lt;br /&gt;
| variable&lt;br /&gt;
| optional&lt;br /&gt;
| Is this action related to some user? This could be used for some personal timeline view.&lt;br /&gt;
|-&lt;br /&gt;
| anonymous&lt;br /&gt;
| Anonymous action&lt;br /&gt;
| variable&lt;br /&gt;
| optional (Defaults to 0)&lt;br /&gt;
| Is this action anonymous? Reports should ignore events with anonymous set to 1.&lt;br /&gt;
|-&lt;br /&gt;
| other&lt;br /&gt;
| All other data&lt;br /&gt;
| variable array&lt;br /&gt;
| optional&lt;br /&gt;
| Any other fields needed for event description - scalars or arrays, must be serialisable using json_encode(). Floating point numbers cannot be used (see MDL-46701).&lt;br /&gt;
|-&lt;br /&gt;
| timecreated&lt;br /&gt;
| Time of the event&lt;br /&gt;
| automatic&lt;br /&gt;
| -&lt;br /&gt;
| Time when the event was triggered.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* static: same for all event instances of this class&lt;br /&gt;
* variable: might not be same for all instances of this event class&lt;br /&gt;
* mandatory: required in order to trigger the event&lt;br /&gt;
* optional: not necessarily required to trigger the event&lt;br /&gt;
&lt;br /&gt;
==== Level property ====&lt;br /&gt;
&lt;br /&gt;
The edulevel property helps define the educational value of the event. It is intentional that the list is limited to only 3 different constants as 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.&lt;br /&gt;
&lt;br /&gt;
Remember that this is event based. If the user that has triggered the event is not really &amp;quot;participating&amp;quot; because he is an admin, or a manager, then it is the job of the report to filter those. In a few cases, the educational level of an event depends on the context (site, course...) and/or the role (admin, teacher...) in these cases the selected educational level should be the most usual use case of that event. The event itself has a static educational level. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Teaching&#039;&#039;&#039; (LEVEL_TEACHING: 1)&lt;br /&gt;
&lt;br /&gt;
Any event/action that is performed by someone (typically a teacher) and has a teaching value (anything that is effecting the learning experience/environment of the students). This should not be combined with &amp;quot;Participating&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
Valid events:&lt;br /&gt;
&lt;br /&gt;
* A teacher grading a student&lt;br /&gt;
* A teacher modifying the course settings&lt;br /&gt;
* A teacher adding a new section to the course page&lt;br /&gt;
* A teacher modifying a module settings&lt;br /&gt;
* A teacher adding a page to course&lt;br /&gt;
* A teacher leaving a feedback&lt;br /&gt;
&lt;br /&gt;
INVALID events:&lt;br /&gt;
&lt;br /&gt;
* A teacher posting in a forum (it might affect the learning experience, but not necessarily, so the teacher is just participating)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Participating&#039;&#039;&#039; (LEVEL_PARTICIPATING: 2)&lt;br /&gt;
&lt;br /&gt;
Any event/action that is performed by a user, that is related (or could be related) to his learning experience.&lt;br /&gt;
&lt;br /&gt;
Valid events:&lt;br /&gt;
&lt;br /&gt;
* A user posting to a forum&lt;br /&gt;
* A user submitting an assignment&lt;br /&gt;
* A user blogging&lt;br /&gt;
* A user reading someone&#039;s blog&lt;br /&gt;
* A user posting a comment&lt;br /&gt;
* A user chatting on a chat activity&lt;br /&gt;
* A user viewing the course page&lt;br /&gt;
* A user deletes a blog post&lt;br /&gt;
&lt;br /&gt;
INVALID events:&lt;br /&gt;
&lt;br /&gt;
* A user updating his profile&lt;br /&gt;
* A user visiting someone&#039;s profile&lt;br /&gt;
* A user viewing his /my/ page&lt;br /&gt;
* A user sending a message to another one&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Other&#039;&#039;&#039; (LEVEL_OTHER: 0)&lt;br /&gt;
&lt;br /&gt;
Any other action, whether they are related to the site administration, or are specific to user. They do not have any educational value.&lt;br /&gt;
&lt;br /&gt;
=== Methods ===&lt;br /&gt;
&lt;br /&gt;
The computation of this data is not required by default, but can be accessed by any event observer if need be.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Method&lt;br /&gt;
! Comment&lt;br /&gt;
|-&lt;br /&gt;
| get_name()&lt;br /&gt;
| Returns localised name of the event, it is the same for all instances.&lt;br /&gt;
|-&lt;br /&gt;
| get_description()&lt;br /&gt;
| Returns non-localised description of one particular event. There are plans to make this localised in future.&lt;br /&gt;
&lt;br /&gt;
It is recommended that the string begins with identifying the user who triggered the event by providing the user id in quotations. This applies to other variables used in the description as well. If an activity is also mentioned then the course module id should be used, not the id from the activity table. For example &amp;quot;The user with the id &#039;$this-&amp;gt;userid&#039; updated the activity &#039;youractivity&#039; with the course module id &#039;$this-&amp;gt;contextinstanceid&#039;.&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
| can_view($user)&lt;br /&gt;
| This method is deprecated, please do not use this.&lt;br /&gt;
|-&lt;br /&gt;
| get_url()&lt;br /&gt;
| Returns Moodle URL where the event can be observed afterwards. Can be null, if no valid location is present.&lt;br /&gt;
|-&lt;br /&gt;
| get_legacy_eventname()&lt;br /&gt;
| Information necessary for event backward compatibility.&lt;br /&gt;
|-&lt;br /&gt;
| get_legacy_eventdata()&lt;br /&gt;
| Information necessary for event backward compatibility.&lt;br /&gt;
|-&lt;br /&gt;
| get_legacy_logdata()&lt;br /&gt;
| Information necessary for logging backward compatibility.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Record caching===&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
* Snapshots are expected to be an exact snapshot of the database table at the instance when the event was triggered.&lt;br /&gt;
* Snapshot can be set and requested for any table. If not cached, it defaults to direct $DB-&amp;gt;get_record calls.&lt;br /&gt;
* Please be aware that the snapshots are not stored in the logging subsystem, and cannot be used later when logged event is displayed.&lt;br /&gt;
* Snapshot are supposed to be used only by observers. They should never be used by reports or by the event itself.&lt;br /&gt;
* When using a snapshot in an observer, please make sure you are taking proper care of handling exceptions as the record you are requesting could have been deleted in the time, in between your observer is notified about the event and the trigger.&lt;br /&gt;
* We recommend all delete event must add snapshot of the deleted record.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    $event = \core\event\role_assigned::create(&lt;br /&gt;
        array(&#039;context&#039;=&amp;gt;$context, &#039;objectid&#039;=&amp;gt;$ra-&amp;gt;roleid, &#039;relateduserid&#039;=&amp;gt;$ra-&amp;gt;userid,&lt;br /&gt;
            &#039;other&#039;=&amp;gt;array(&#039;id&#039;=&amp;gt;$ra-&amp;gt;id, &#039;component&#039;=&amp;gt;$ra-&amp;gt;component, &#039;itemid&#039;=&amp;gt;$ra-&amp;gt;itemid)));&lt;br /&gt;
    $event-&amp;gt;add_record_snapshot(&#039;role_assignments&#039;, $ra);&lt;br /&gt;
    $event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    $event = \core\event\role_unassigned::create(&lt;br /&gt;
        array(&#039;context&#039;=&amp;gt;$context, &#039;objectid&#039;=&amp;gt;$ra-&amp;gt;roleid, &#039;relateduserid&#039;=&amp;gt;$ra-&amp;gt;userid,&lt;br /&gt;
            &#039;other&#039;=&amp;gt;array(&#039;id&#039;=&amp;gt;$ra-&amp;gt;id, &#039;component&#039;=&amp;gt;$ra-&amp;gt;component, &#039;itemid&#039;=&amp;gt;$ra-&amp;gt;itemid)));&lt;br /&gt;
    $event-&amp;gt;add_record_snapshot(&#039;role_assignments&#039;, $ra);&lt;br /&gt;
    $event-&amp;gt;trigger();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The related methods are:&lt;br /&gt;
* public function add_record_snapshot($tablename, $record)&lt;br /&gt;
* public function get_record_snapshot($tablename, $id)&lt;br /&gt;
&lt;br /&gt;
== Events naming convention ==&lt;br /&gt;
&lt;br /&gt;
Clear event names help developers when reading what events are triggered, and defining the events properties when defining the event class.&lt;br /&gt;
&lt;br /&gt;
 Decision: \&amp;lt;component&amp;gt;\event\&amp;lt;some_object&amp;gt;_&amp;lt;verb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Existing events ===&lt;br /&gt;
&lt;br /&gt;
List of existing events in Moodle code base, along with their 2.5 couterparts.&lt;br /&gt;
&lt;br /&gt;
This list is out of date. For a full list of events check out the [https://docs.moodle.org/en/Event_list_report Event list report] which is located in &amp;quot;Site administration &amp;gt; Reports &amp;gt; Event list&amp;quot; of your Moodle installation (2.7+).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Fully qualified event name&lt;br /&gt;
! 2.5 name&lt;br /&gt;
! Component&lt;br /&gt;
! Object&lt;br /&gt;
! Action (Verb)&lt;br /&gt;
! Comment&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_comments\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_comments&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_comments\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_comments&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_file\event\assessable_uploaded&#039;&#039;&#039;&lt;br /&gt;
| assessable_file_uploaded&lt;br /&gt;
| assignsubmission_file&lt;br /&gt;
| assessable&lt;br /&gt;
| uploaded&lt;br /&gt;
| To be deprecated MDL-35197&lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_file\event\submission_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_file&lt;br /&gt;
| submission&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_file\event\submission_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_file&lt;br /&gt;
| submission&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_onlinetext\event\assessable_uploaded&#039;&#039;&#039;&lt;br /&gt;
| assessable_content_uploaded&lt;br /&gt;
| assignsubmission_onlinetext&lt;br /&gt;
| assessable&lt;br /&gt;
| uploaded&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_onlinetext\event\submission_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_onlinetext&lt;br /&gt;
| submission&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;assignsubmission_onlinetext\event\submission_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| assignsubmission_onlinetext&lt;br /&gt;
| submission&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;block_comments\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| block_comments&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;block_comments\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| block_comments&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;booktool_exportimscp\event\book_exported&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| booktool_exportimscp&lt;br /&gt;
| book&lt;br /&gt;
| exported&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;booktool_print\event\book_printed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| booktool_print&lt;br /&gt;
| book&lt;br /&gt;
| printed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;booktool_print\event\chapter_printed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| booktool_print&lt;br /&gt;
| chapter&lt;br /&gt;
| printed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\assessable_submitted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| assessable&lt;br /&gt;
| submitted&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\assessable_uploaded&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| assessable&lt;br /&gt;
| uploaded&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\base&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| base&lt;br /&gt;
| -&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_association_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| blog_association&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| blog_comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| blog_comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_entries_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| blog_entries&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_entry_created&#039;&#039;&#039;&lt;br /&gt;
| blog_entry_added&lt;br /&gt;
| core&lt;br /&gt;
| blog_entry&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_entry_deleted&#039;&#039;&#039;&lt;br /&gt;
| blog_entry_deleted&lt;br /&gt;
| core&lt;br /&gt;
| blog_entry&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\blog_entry_updated&#039;&#039;&#039;&lt;br /&gt;
| blog_entry_edited&lt;br /&gt;
| core&lt;br /&gt;
| blog_entry&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\cohort_created&#039;&#039;&#039;&lt;br /&gt;
| cohort_added&lt;br /&gt;
| core&lt;br /&gt;
| cohort&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\cohort_deleted&#039;&#039;&#039;&lt;br /&gt;
| cohort_deleted&lt;br /&gt;
| core&lt;br /&gt;
| cohort&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\cohort_member_added&#039;&#039;&#039;&lt;br /&gt;
| cohort_member_added&lt;br /&gt;
| core&lt;br /&gt;
| cohort_member&lt;br /&gt;
| added&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\cohort_member_removed&#039;&#039;&#039;&lt;br /&gt;
| cohort_member_removed&lt;br /&gt;
| core&lt;br /&gt;
| cohort_member&lt;br /&gt;
| removed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\cohort_updated&#039;&#039;&#039;&lt;br /&gt;
| cohort_updated&lt;br /&gt;
| core&lt;br /&gt;
| cohort&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\comments_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| comments&lt;br /&gt;
| viewed&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_category_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_category&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_category_deleted&#039;&#039;&#039;&lt;br /&gt;
| course_category_deleted&lt;br /&gt;
| core&lt;br /&gt;
| course_category&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_category_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_category&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_completed&#039;&#039;&#039;&lt;br /&gt;
| course_completed&lt;br /&gt;
| core&lt;br /&gt;
| course&lt;br /&gt;
| completed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_completion_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_completion&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_content_deleted&#039;&#039;&#039;&lt;br /&gt;
| course_content_removed&lt;br /&gt;
| core&lt;br /&gt;
| course_content&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_created&#039;&#039;&#039;&lt;br /&gt;
| course_created&lt;br /&gt;
| core&lt;br /&gt;
| course&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_deleted&#039;&#039;&#039;&lt;br /&gt;
| course_deleted&lt;br /&gt;
| core&lt;br /&gt;
| course&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_completion_updated&#039;&#039;&#039;&lt;br /&gt;
| activity_completion_changed&lt;br /&gt;
| core&lt;br /&gt;
| course_module_completion&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_created&#039;&#039;&#039;&lt;br /&gt;
| mod_created&lt;br /&gt;
| core&lt;br /&gt;
| course_module&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_deleted&#039;&#039;&#039;&lt;br /&gt;
| mod_deleted&lt;br /&gt;
| core&lt;br /&gt;
| course_module&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_instances_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_module_instances_list&lt;br /&gt;
| viewed&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_updated&#039;&#039;&#039;&lt;br /&gt;
| mod_updated&lt;br /&gt;
| core&lt;br /&gt;
| course_module&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_reset_ended&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_reset&lt;br /&gt;
| ended&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_reset_started&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_reset&lt;br /&gt;
| started&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_restored&#039;&#039;&#039;&lt;br /&gt;
| course_restored&lt;br /&gt;
| core&lt;br /&gt;
| course&lt;br /&gt;
| restored&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_section_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| course_section&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\course_updated&#039;&#039;&#039;&lt;br /&gt;
| course_updated&lt;br /&gt;
| core&lt;br /&gt;
| course&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\email_failed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| email&lt;br /&gt;
| failed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\group_created&#039;&#039;&#039;&lt;br /&gt;
| groups_group_created&lt;br /&gt;
| core&lt;br /&gt;
| group&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\group_deleted&#039;&#039;&#039;&lt;br /&gt;
| groups_group_deleted&lt;br /&gt;
| core&lt;br /&gt;
| group&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\group_member_added&#039;&#039;&#039;&lt;br /&gt;
| groups_member_added&lt;br /&gt;
| core&lt;br /&gt;
| group_member&lt;br /&gt;
| added&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\group_member_removed&#039;&#039;&#039;&lt;br /&gt;
| groups_member_removed&lt;br /&gt;
| core&lt;br /&gt;
| group_member&lt;br /&gt;
| removed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\group_updated&#039;&#039;&#039;&lt;br /&gt;
| groups_group_updated&lt;br /&gt;
| core&lt;br /&gt;
| group&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\grouping_created&#039;&#039;&#039;&lt;br /&gt;
| groups_grouping_created&lt;br /&gt;
| core&lt;br /&gt;
| grouping&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\grouping_deleted&#039;&#039;&#039;&lt;br /&gt;
| groups_grouping_deleted&lt;br /&gt;
| core&lt;br /&gt;
| grouping&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\grouping_updated&#039;&#039;&#039;&lt;br /&gt;
| groups_grouping_updated&lt;br /&gt;
| core&lt;br /&gt;
| grouping&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\manager&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| manager&lt;br /&gt;
| anager&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\mnet_access_control_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| mnet_access_control&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\mnet_access_control_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| mnet_access_control&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\note_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| note&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\note_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| note&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\note_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| note&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\notes_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| notes&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_allow_assign_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| role_allow_assign&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_allow_override_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| role_allow_override&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_allow_switch_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| role_allow_switch&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_assigned&#039;&#039;&#039;&lt;br /&gt;
| role_assigned&lt;br /&gt;
| core&lt;br /&gt;
| role&lt;br /&gt;
| assigned&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_capabilities_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| role_capabilities&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| role&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\role_unassigned&#039;&#039;&#039;&lt;br /&gt;
| role_unassigned&lt;br /&gt;
| core&lt;br /&gt;
| role&lt;br /&gt;
| unassigned&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_created&#039;&#039;&#039;&lt;br /&gt;
| user_created&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_deleted&#039;&#039;&#039;&lt;br /&gt;
| user_deleted&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_password_updated.php&#039;&#039;&#039;&lt;br /&gt;
| user_password_updated&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_enrolment_created&#039;&#039;&#039;&lt;br /&gt;
| user_enrolled&lt;br /&gt;
| core&lt;br /&gt;
| user_enrolment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_enrolment_deleted&#039;&#039;&#039;&lt;br /&gt;
| user_unenrolled&lt;br /&gt;
| core&lt;br /&gt;
| user_enrolment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_enrolment_updated&#039;&#039;&#039;&lt;br /&gt;
| user_enrol_modified&lt;br /&gt;
| core&lt;br /&gt;
| user_enrolment&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| user_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_loggedin&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| loggedin&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_loggedinas&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| loggedinas&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_loggedout&#039;&#039;&#039;&lt;br /&gt;
| user_logout&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| loggedout&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_login_failed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| user_login&lt;br /&gt;
| failed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_profile_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| user_profile&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\user_updated&#039;&#039;&#039;&lt;br /&gt;
| user_updated&lt;br /&gt;
| core&lt;br /&gt;
| user&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_function_called&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_function&lt;br /&gt;
| called&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_login_failed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_login&lt;br /&gt;
| failed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_service_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_service&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_service_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_service&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_service_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_service&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_service_user_added&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_service_user&lt;br /&gt;
| added&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_service_user_removed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_service_user&lt;br /&gt;
| removed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_token_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_token&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;core\event\webservice_token_sent&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| core&lt;br /&gt;
| webservice_token&lt;br /&gt;
| sent&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;logstore_legacy\event\legacy_logged&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| logstore_legacy&lt;br /&gt;
| legacy&lt;br /&gt;
| logged&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\all_submissions_downloaded&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| all_submissions&lt;br /&gt;
| downloaded&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\assessable_submitted&#039;&#039;&#039;&lt;br /&gt;
| assessable_submitted&lt;br /&gt;
| mod_assign&lt;br /&gt;
| assessable&lt;br /&gt;
| submitted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\extension_granted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| extension&lt;br /&gt;
| granted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\identities_revealed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| identities&lt;br /&gt;
| revealed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\marker_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| marker&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\statement_accepted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| statement&lt;br /&gt;
| accepted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| created&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_duplicated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| duplicated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_graded&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| graded&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_locked&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| locked&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_status_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission_status&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_unlocked&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| unlocked&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\submission_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| submission&lt;br /&gt;
| updated&lt;br /&gt;
| Abstract class &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_assign\event\workflow_state_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_assign&lt;br /&gt;
| workflow_state&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\chapter_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| chapter&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\chapter_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| chapter&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\chapter_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| chapter&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\chapter_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| chapter&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_book\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_book&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_chat\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_chat&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_chat\event\message_sent&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_chat&lt;br /&gt;
| message&lt;br /&gt;
| sent&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_chat\event\sessions_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_chat&lt;br /&gt;
| sessions&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_choice\event\answer_submitted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_choice&lt;br /&gt;
| answer&lt;br /&gt;
| submitted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_choice\event\answer_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_choice&lt;br /&gt;
| answer&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_choice\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_choice&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_choice\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_choice&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_choice\event\report_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_choice&lt;br /&gt;
| report&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\field_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| field&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\field_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| field&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\field_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| field&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\record_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| record&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\record_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| record&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\record_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| record&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\template_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| template&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_data\event\template_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_data&lt;br /&gt;
| template&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_feedback\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_feedback&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_feedback\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_feedback&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_feedback\event\response_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_feedback&lt;br /&gt;
| response&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_feedback\event\response_submitted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_feedback&lt;br /&gt;
| response&lt;br /&gt;
| submitted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_folder\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_folder&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_folder\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_folder&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_folder\event\folder_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_folder&lt;br /&gt;
| folder&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\assessable_uploaded&#039;&#039;&#039;&lt;br /&gt;
| assessable_content_uploaded&lt;br /&gt;
| mod_forum&lt;br /&gt;
| assessable&lt;br /&gt;
| uploaded&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\course_searched&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| course&lt;br /&gt;
| searched&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\discussion_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| discussion&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\discussion_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| discussion&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\discussion_moved&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| discussion&lt;br /&gt;
| moved&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\discussion_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| discussion&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\discussion_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| discussion&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\forum_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| forum&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\post_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| post&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\post_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| post&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\post_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| post&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\readtracking_disabled&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| readtracking&lt;br /&gt;
| disabled&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\readtracking_enabled&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| readtracking&lt;br /&gt;
| enabled&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\subscribers_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| subscribers&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\subscription_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| subscription&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\subscription_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| subscription&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_forum\event\userreport_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_forum&lt;br /&gt;
| userreport&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_glossary\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_glossary&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_glossary\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_glossary&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\essay_assessed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| essay&lt;br /&gt;
| assessed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\essay_attempt_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| essay_attempt&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\highscore_added&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| highscore&lt;br /&gt;
| added&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\highscores_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| highscores&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\lesson_ended&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| lesson&lt;br /&gt;
| ended&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lesson\event\lesson_started&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lesson&lt;br /&gt;
| lesson&lt;br /&gt;
| started&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lti\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lti&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lti\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_lti&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_lti\event\unknown_service_api_called&#039;&#039;&#039;&lt;br /&gt;
| lti_unknown_service_api_call&lt;br /&gt;
| mod_lti&lt;br /&gt;
| unknown_service_api&lt;br /&gt;
| called&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_page\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_page&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_page\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_page&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_quiz\event\attempt_abandoned&#039;&#039;&#039;&lt;br /&gt;
| quiz_attempt_abandoned&lt;br /&gt;
| mod_quiz&lt;br /&gt;
| attempt&lt;br /&gt;
| abandoned&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_quiz\event\attempt_becameoverdue&#039;&#039;&#039;&lt;br /&gt;
| quiz_attempt_overdue&lt;br /&gt;
| mod_quiz&lt;br /&gt;
| attempt&lt;br /&gt;
| becameoverdue&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_quiz\event\attempt_started&#039;&#039;&#039;&lt;br /&gt;
| quiz_attempt_started&lt;br /&gt;
| mod_quiz&lt;br /&gt;
| attempt&lt;br /&gt;
| started&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_quiz\event\attempt_submitted&#039;&#039;&#039;&lt;br /&gt;
| quiz_attempt_submitted&lt;br /&gt;
| mod_quiz&lt;br /&gt;
| attempt&lt;br /&gt;
| submitted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_resource\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_resource&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_resource\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_resource&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\attempt_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| attempt&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\interactions_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| interactions&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\report_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| report&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\sco_launched&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| sco&lt;br /&gt;
| launched&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\tracks_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| tracks&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_scorm\event\user_report_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_scorm&lt;br /&gt;
| user_report&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_url\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_url&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_url\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_url&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\comment_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| comment&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\comment_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| comment&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\comments_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| comments&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\course_module_instance_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| course_module_instance_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_diff_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_diff&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_history_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_history&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_locks_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_locks&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_map_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_map&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_version_deleted&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_version&lt;br /&gt;
| deleted&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_version_restored&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_version&lt;br /&gt;
| restored&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_version_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page_version&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_wiki\event\page_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_wiki&lt;br /&gt;
| page&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\assessable_uploaded&#039;&#039;&#039;&lt;br /&gt;
| assessable_content_uploaded&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| assessable&lt;br /&gt;
| uploaded&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\assessment_evaluated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| assessment&lt;br /&gt;
| evaluated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\assessment_evaluations_reset&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| assessment_evaluations&lt;br /&gt;
| reset&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\assessment_reevaluated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| assessment&lt;br /&gt;
| reevaluated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\course_module_viewed&#039;&#039;&#039;&lt;br /&gt;
| workshop_viewed&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| course_module&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\instances_list_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| instances_list&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\phase_switched&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| phase&lt;br /&gt;
| switched&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\submission_assessed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| submission&lt;br /&gt;
| assessed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\submission_created&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| submission&lt;br /&gt;
| created&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\submission_reassessed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| submission&lt;br /&gt;
| reassessed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\submission_updated&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| submission&lt;br /&gt;
| updated&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;mod_workshop\event\submission_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| mod_workshop&lt;br /&gt;
| submission&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;report_log\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| report_log&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;report_loglive\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| report_loglive&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;report_outline\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| report_outline&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;report_participation\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| report_participation&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| &#039;&#039;&#039;report_stats\event\content_viewed&#039;&#039;&#039;&lt;br /&gt;
| -&lt;br /&gt;
| report_stats&lt;br /&gt;
| content&lt;br /&gt;
| viewed&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Verb list ===&lt;br /&gt;
All events must use a verb from this list. New verbs can be added to this list if required, but additions should only be made if there is no valid alternative (we want to keep this list as small as possible).&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!verb&lt;br /&gt;
!Explanation&lt;br /&gt;
!Source&lt;br /&gt;
|-&lt;br /&gt;
|abandoned&lt;br /&gt;
|When a attempt is abandoned by user (Quiz attempt)&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|accepted&lt;br /&gt;
|Example: Accepting a statement when submitting an assignment.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|added&lt;br /&gt;
|	Used to represent &amp;quot;something that already exists is now part of/bound to another entity&amp;quot;. Examples: &amp;quot;Admin added role to user X&amp;quot;, &amp;quot;Admin added user X to group A&amp;quot;. Wrong example: &amp;quot;User added course in category&amp;quot; because it is a &#039;move&#039; 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.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|answered&lt;br /&gt;
| Indicates the actor responded to a Question&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|assessed&lt;br /&gt;
| Some submitted material has been assessed&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|assigned&lt;br /&gt;
| Assign some privilege or role to user.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|attempted&lt;br /&gt;
| Trying to do an activity. Example: attempting a Math class.&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|awarded&lt;br /&gt;
| ex:-teacher awarded student a badge.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|backedup&lt;br /&gt;
| When a backup has been performed.&lt;br /&gt;
|Moodle	&lt;br /&gt;
|-&lt;br /&gt;
|becomeoverdue&lt;br /&gt;
| When an activity is overdue Example: Quiz attempt is overdue&lt;br /&gt;
|Moodle	&lt;br /&gt;
|-&lt;br /&gt;
|called&lt;br /&gt;
| When a call to something is made like an API @see unknown_service_api_called.php&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|commented&lt;br /&gt;
|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.&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|completed&lt;br /&gt;
|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 &#039;incomplete&#039; an activity, one would void the statement which completes the activity.&amp;quot;&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|created&lt;br /&gt;
|Used to represent &amp;quot;something new has been created&amp;quot;.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|deleted&lt;br /&gt;
|Used to indicate the object in context was deleted.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|disabled&lt;br /&gt;
|When an activity is disabled. Example: forum read tracking disabled.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|downloaded&lt;br /&gt;
|When a user download file from user. Example submission/report downloaded.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|duplicated&lt;br /&gt;
|For something that has been copied.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|enabled&lt;br /&gt;
|When some setting is enabled. Example: forum read tracking enabled.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|ended&lt;br /&gt;
|When a process ends. Example: Lesson ended or course reset ended.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|evaluated&lt;br /&gt;
|Material has been evaluated.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|exported&lt;br /&gt;
|When a report is exported in certain format.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|failed&lt;br /&gt;
|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 &#039;failed&#039;. The opposite of &#039;passed&#039;. This is also used in case when message sending is failed.&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|graded&lt;br /&gt;
|Used to represent an activity was graded.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|granted&lt;br /&gt;
|User is granted some extension or capability. Example: extension granted for submission.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|imported&lt;br /&gt;
|The act of moving an object into another location or system.&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|launched&lt;br /&gt;
|When an external object is launched. Try consider started if there is related stopped event.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|locked&lt;br /&gt;
|When an activity is locked. Should have a related unlocked event.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|loggedin/loggedout&lt;br /&gt;
|	For login and logout.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|loggedinas&lt;br /&gt;
|	 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.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|locked&lt;br /&gt;
|&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|moved&lt;br /&gt;
|	Used to indicate the object in context was moved.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|passed&lt;br /&gt;
|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 &#039;passed&#039;. The opposite of &#039;failed&#039;.&lt;br /&gt;
|Tincan&lt;br /&gt;
|-&lt;br /&gt;
|printed&lt;br /&gt;
|Something is printed.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|reassessed&lt;br /&gt;
|Submitted material has been assessed again.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|reevaluated&lt;br /&gt;
|Material has been evaluated again.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|removed&lt;br /&gt;
|By opposition to &amp;quot;Added&amp;quot;. This does not mean that the object has been deleted, but removed from the entity, or not bound to it any more.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|reset&lt;br /&gt;
|Sets one or more properties back to the default value.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|restored&lt;br /&gt;
|When restoring a backup. Rolling back to a previous state.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|revealed&lt;br /&gt;
|Some identity is revealed. Example: Identities revealed after blind marking.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|searched&lt;br /&gt;
|Something is searched. Example: searched in course.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|sent&lt;br /&gt;
|Message sent.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|started&lt;br /&gt;
|Some activity started&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|submitted&lt;br /&gt;
|This is very close to &amp;quot;Attempted&amp;quot;. Depends on context which one should be used. For example:- &amp;quot;Admin submitted a form. Student attempted a quiz.&amp;quot;  is correct, however some cases might not be as clear as the previous example. We can say both &amp;quot;Student submitted an assignment&amp;quot; or &amp;quot;student attempted an assignment&amp;quot;. We need to make the difference clear.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|suspended&lt;br /&gt;
| Suspend something. (example a user)&lt;br /&gt;
| Tincan (However the context is different)&lt;br /&gt;
|-&lt;br /&gt;
|switched&lt;br /&gt;
|Something has been switched. For example:- The workshop phase has been switched to assessment&amp;quot;&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|unassigned&lt;br /&gt;
|As opposed to assigned. When some role is unassigned.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|unlocked&lt;br /&gt;
|&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|upgraded&lt;br /&gt;
|Something was upgraded, some module probably&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|updated&lt;br /&gt;
|Used to indicate the object in context was updated. Simple example is &amp;quot;Admin updated course xyz&amp;quot;.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|uploaded&lt;br /&gt;
|When an assignment is uploaded.&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|viewed&lt;br /&gt;
|Something has been viewed. For example:- &amp;quot;Student viewed chapter 1 of book 1.&amp;quot;&lt;br /&gt;
|Moodle&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Rules ===&lt;br /&gt;
&lt;br /&gt;
==== Use singular ====&lt;br /&gt;
&lt;br /&gt;
Plurals must be used on objects when it&#039;s a &#039;&#039;One to Many&#039;&#039; relationship. Ex: bulk import, mass deletion, ... In any other case, use the singular.&lt;br /&gt;
&lt;br /&gt;
==== Ends with a verb ====&lt;br /&gt;
&lt;br /&gt;
The last word (after the last underscore) must be a verb.&lt;br /&gt;
&lt;br /&gt;
==== Consistency among child events ====&lt;br /&gt;
&lt;br /&gt;
If an event is extending a parent class, it should have the same exact name as the parent event.&lt;br /&gt;
&lt;br /&gt;
=== Deprecated events ===&lt;br /&gt;
Following are the events that were supported in 2.5, but deprecated in 2.6 or later&lt;br /&gt;
&lt;br /&gt;
groups_groupings_deleted (see MDL-41312)&lt;br /&gt;
&lt;br /&gt;
groups_groupings_groups_removed (see MDL-41312)&lt;br /&gt;
&lt;br /&gt;
groups_groups_deleted (see MDL-41312)&lt;br /&gt;
&lt;br /&gt;
groups_members_removed (see MDL-41312)&lt;br /&gt;
&lt;br /&gt;
== Shared events ==&lt;br /&gt;
&lt;br /&gt;
 Decision: Not supported at this stage.&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.5 we have a good example of a shared event: &#039;assessable_content_uploaded&#039; which is triggered in &#039;&#039;forum&#039;&#039;, &#039;assignment&#039;&#039; and &#039;&#039;workshop&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== One to many ==&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&lt;br /&gt;
In 2.5, some events are triggered when an action happens on multiple objects. We have to decide whether we want to keep supporting &#039;&#039;One to Many&#039;&#039; events or not.&lt;br /&gt;
&lt;br /&gt;
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...&lt;br /&gt;
&lt;br /&gt;
=== Accuracy ===&lt;br /&gt;
&lt;br /&gt;
It is important to trigger individual events for each and every action, bulk events are very strongly discouraged because existing observers would not receive important information. Changes from one to many would break backwards compatibility with observers and reporting.&lt;br /&gt;
&lt;br /&gt;
=== Performance ===&lt;br /&gt;
&lt;br /&gt;
Triggering one event is cheaper then repeating the same events x number of times...&lt;br /&gt;
&lt;br /&gt;
=== Information tracking ===&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;bulk&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
=== Double event ===&lt;br /&gt;
&lt;br /&gt;
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 &#039;user_bulk_upload_started&#039; when the action starts.&lt;br /&gt;
&lt;br /&gt;
== Unit Testing ==&lt;br /&gt;
&lt;br /&gt;
With unit testing for this system we want to assert the following:&lt;br /&gt;
&lt;br /&gt;
* That event strict validation and custom validation works.&lt;br /&gt;
* Missing event data is auto filled with accurate data.&lt;br /&gt;
* Typos in properties passed to ::create() are captured (if we decide to validate).&lt;br /&gt;
* The legacy methods return the expected values (use assertEventLegacyData() and assertEventLegacyData())&lt;br /&gt;
* The class properties are correctly overridden (crud, level, action, object, ...).&lt;br /&gt;
* The properties automatically generated (component, name, ...) are correct.&lt;br /&gt;
* Events are dispatched to the corresponding observers.&lt;br /&gt;
* Events are dispatched to the corresponding legacy handlers.&lt;br /&gt;
* Events are dispatched to the * observers.&lt;br /&gt;
* Events perform an add_to_log() if it has legacy log data.&lt;br /&gt;
* &#039;Events restore&#039; restored the whole event data, and does not miss any information.&lt;br /&gt;
* &#039;Events restore&#039; does not generate any extra information.&lt;br /&gt;
* Event methods should check context object or avoid using it, as context might not be valid at time of event restore (use assertEventContextNotUsed())&lt;br /&gt;
&lt;br /&gt;
= PHP docs =&lt;br /&gt;
* All events php docs must include @since parameter, indicating when the event was first included in standard Moodle distribution.&lt;br /&gt;
* All events must declare the $other properties using mark down in the php docs. This later might be converted to a self documenting structure. (See MDL-45108)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * blog_association_created&lt;br /&gt;
 *&lt;br /&gt;
 * Class for event to be triggered when a new blog entry is associated with a context.&lt;br /&gt;
 *&lt;br /&gt;
 * @property-read array $other {&lt;br /&gt;
 *      Extra information about event.&lt;br /&gt;
 *&lt;br /&gt;
 *      - string associatetype: type of blog association, course/coursemodule.&lt;br /&gt;
 *      - int blogid: id of blog.&lt;br /&gt;
 *      - int associateid: id of associate.&lt;br /&gt;
 *      - string subject: blog subject.&lt;br /&gt;
 * }&lt;br /&gt;
 *&lt;br /&gt;
 * @package    core&lt;br /&gt;
 * @since      Moodle 2.7&lt;br /&gt;
 * @copyright  2013 onwards Ankit Agarwal&lt;br /&gt;
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Example events =&lt;br /&gt;
&lt;br /&gt;
== Assignment ==&lt;br /&gt;
&lt;br /&gt;
Assumption: Course contains groups with students in each group.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1.&#039;&#039;&#039; Teacher creates an assignment with group mode set to &#039;Separate groups&#039; and Feedback type set to comments and files.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has created assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;2.&#039;&#039;&#039; A student views the assignment.&lt;br /&gt;
*Event: User &#039;Student&#039; has viewed assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;3.&#039;&#039;&#039; A member from one of the groups submits an assignment&lt;br /&gt;
*Event: User &#039;Student&#039; has added a submission for assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: Email sent to the teacher and all students in that group.&lt;br /&gt;
&#039;&#039;&#039;4.&#039;&#039;&#039; User &#039;Adrian&#039; adds some changes to the assignment and updates it.&lt;br /&gt;
*Event: User &#039;Adrian&#039; has updated the submission for assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: Email sent to the teacher and all students in that group.&lt;br /&gt;
&#039;&#039;&#039;5.&#039;&#039;&#039; Teacher views the assignment.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has viewed assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;6.&#039;&#039;&#039; Teacher clicks on &#039;View/grade all submissions&#039;&lt;br /&gt;
*Event: User &#039;Teacher&#039; has viewed the assignment &#039;B&#039; grade area in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;7.&#039;&#039;&#039; Teacher clicks to grade the student&#039;s submission.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has viewed the submission for user &#039;student&#039; for assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;8.&#039;&#039;&#039; Teacher marks the assignment with the setting &#039;Apply grades and feedback to entire group&#039; set to &#039;Yes&#039; leaving a comment and a file.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has marked assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has left a comment for assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has uploaded a feedback file for assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has uploaded a file to the course &#039;C101&#039;.&lt;br /&gt;
*Event: Email sent to user &#039;Student&#039; notifying them their submission for assignment &#039;B&#039; has been marked. - This is done for all users in the group.&lt;br /&gt;
&#039;&#039;&#039;9.&#039;&#039;&#039; User &#039;Adrian&#039; views the feedback.&lt;br /&gt;
*Event: User &#039;Adrian&#039; has viewed assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;10.&#039;&#039;&#039; User &#039;Adrian&#039; opens the feedback file.&lt;br /&gt;
*Event: User &#039;Adrian&#039; has viewed the file &#039;A&#039; for assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;11.&#039;&#039;&#039; User &#039;Adrian&#039; adds some changes to the assignment insulting the teachers marking and updates it.&lt;br /&gt;
*Event: User &#039;Adrian&#039; has updated the submission for assignment &#039;B&#039; for group &#039;C&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: Email sent to user &#039;Teacher&#039; notifying them that user &#039;Adrian&#039; has updated the submission for assignment &#039;B&#039;.&lt;br /&gt;
*Event: Email sent to user &#039;Student&#039; notifying them their submission for assignment &#039;B&#039; has been updated. - This is done for all users in the group.&lt;br /&gt;
&#039;&#039;&#039;12.&#039;&#039;&#039; The teacher clicks directly on the link in the email to be taken to the grading page.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has viewed the submission for user &#039;student&#039; for assignment &#039;B&#039; in course &#039;C101&#039;.&lt;br /&gt;
&#039;&#039;&#039;13.&#039;&#039;&#039; The teacher is upset due to the harsh comments and decides to mark Adrian down, but not the rest of the group by setting &#039;Apply grades and feedback to entire group&#039; set to &#039;No&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has marked assignment &#039;B&#039; for user &#039;Adrian&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has left a comment for assignment &#039;B&#039; for user &#039;Adrian&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: User &#039;Teacher&#039; has uploaded a feedback file for assignment &#039;B&#039; for user &#039;Adrian&#039; in course &#039;C101&#039;.&lt;br /&gt;
*Event: Email sent to user &#039;Adrian&#039; notifying them their submission for assignment &#039;B&#039; has been marked.&lt;br /&gt;
&lt;br /&gt;
= FAQs =&lt;br /&gt;
&lt;br /&gt;
; Why not 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.&lt;br /&gt;
&lt;br /&gt;
= Possible future work = &lt;br /&gt;
* MDL-45108 &amp;quot;Other&amp;quot; parameters should be defined in a method similar to webservices&lt;br /&gt;
* MDL-45217 Create traits for refactoring event triggers&lt;br /&gt;
* MDL-42897 Converting completion to use events&lt;br /&gt;
* MDL-42898 Develop a timeline page/block&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
[[Logging 2]]  &lt;br /&gt;
&lt;br /&gt;
[[Tin Can]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=47098</id>
		<title>version.php</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=47098"/>
		<updated>2015-01-06T15:52:58Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Plugin version properties */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an essential file expected to be located in the root directory of the plugin.&lt;br /&gt;
&lt;br /&gt;
It contains a number of properties, which are used during the plugin installation and upgrade process. It allows to make sure the plugin is compatible with the given Moodle site, as well as spotting whether an upgrade is needed.&lt;br /&gt;
&lt;br /&gt;
The file is a standard PHP file, starting with an opening &#039;&amp;amp;lt;?php&#039; tag and defining the following variables:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; There has been an exception for [[Activity modules]]. Before Moodle 2.6, &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; was used instead of &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; in the version.php file of this plugin type. Starting in Moodle 2.6, activity modules support the &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; notation and it is recommended to use it. Support for &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; will be dropped in Moodle 3.0. (MDL-43896)&lt;br /&gt;
&lt;br /&gt;
== Plugin version properties ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Property&lt;br /&gt;
! Type&lt;br /&gt;
! Status&lt;br /&gt;
! Description and example&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;version&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| required&lt;br /&gt;
| The version number of the plugin. The format is partially date based with the form YYYYMMDDXX where XX is an incremental counter for the given year (YYYY), month (MM) and date (DD) of the plugin version&#039;s release. Every new plugin version must have this number increased in this file, which is detected by Moodle core and the upgrade process is triggered.&lt;br /&gt;
&lt;br /&gt;
In case of linear development (i.e. there is just one branch of the plugin maintained), the XX counter should stay with the initial 00 value unless multiple versions are released on the same date.&lt;br /&gt;
&lt;br /&gt;
If multiple stable branches of the plugin are maintained, the date part YYYYMMDD should be frozen at the branch forking date and the XX is used for incrementing the value on the given stable branch (allowing up to 100 updates on the given stable branch). The key point is that the version number is always increased both horizontally (within the same stable branch, more recent updates have higher XX than any previous one) and vertically (between any two stable branches, the more recent branch has YYYYMMDD00 higher than the older stable branch). Pay attention to this. It&#039;s easy to mess up and hard to fix.&lt;br /&gt;
&lt;br /&gt;
See [https://moodle.org/mod/forum/discuss.php?d=275635#p1184986] for an example of versioning scheme on multiple branches.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;version = 2014052000; // Plugin released on 20th May 2014.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;requires&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| recommended&lt;br /&gt;
| Specifies the minimum version number of Moodle core that this plugin requires. It is not possible to install it to earlier Moodle version. Moodle core&#039;s version number is defined in the file version.php located in Moodle root directory, in the &amp;lt;tt&amp;gt;$version&amp;lt;/tt&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
Note that Moodle core extends the version numbering scheme and has inbuilt support for decimal versioning (such as &amp;lt;tt&amp;gt;$version = 2014051200.01;&amp;lt;/tt&amp;gt;). The decimal part is used for incremental (weekly) builds. Plugins should not rely on a particular weekly build so they use just the whole number part of the Moodle core&#039;s version in this field.&lt;br /&gt;
&lt;br /&gt;
See [[Releases]] for the full list of Moodle core version numbers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;requires = 2014051200; // Moodle 2.7.0 is required.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;cron&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| optional&lt;br /&gt;
| Allows to throttle the plugin&#039;s cron function calls. If set to 0, or not set, the cron function is disabled. The set value represents a minimal required gap in seconds between two calls of the plugin&#039;s cron function. Note that the cron function is not supported in all plugin types (also note that, while the cron function &#039;&#039;&#039;is&#039;&#039;&#039; supported for authentication plugins, it is run every time the cron script runs, regardless of this value in version.php). This value is stored in the database. After changing this value, the version number must be incremented.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Note:&#039;&#039; Starting with Moodle 2.7, plugin developers are encouraged to use the [[Task API|scheduled task API]] instead of the cron function feature.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;cron = 300; // Do not execute this plugin&#039;s cron more often than every five minutes.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;component&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| recommended&lt;br /&gt;
| The full [[Frankenstyle|frankenstyle]] compoment name in the form of plugintype_pluginname. It is used during the installation and upgrade process for diagnostics and validation purposes to make sure the plugin code has been deployed to the correct location within the Moodle code tree.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;component = &#039;block_foobar&#039;; // Declare the type and name of this plugin.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;maturity&#039;&#039;&#039;&lt;br /&gt;
| enum&lt;br /&gt;
| recommended&lt;br /&gt;
| Declares the maturity level of this plugin version, that is how stable it is. This affects the [[:en:Available update notifications|available update notifications]] feature in Moodle. Administrators can configure their site so that they are not notified about an available update unless it has certain maturity level declared.&lt;br /&gt;
&lt;br /&gt;
Supported value is any of the predefined constants &amp;lt;tt&amp;gt;MATURITY_ALPHA, MATURITY_BETA, MATURITY_RC&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MATURITY_STABLE&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;maturity = MATURITY_STABLE; // This is considered as ready for production sites.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;release&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| recommended&lt;br /&gt;
| Human readable version name that should help to identify each release of the plugin. It can be anything you like although it is recommended to choose a pattern and stick with it. Usually it is a simple version like &amp;lt;tt&amp;gt;2.1&amp;lt;/tt&amp;gt; but some plugin authors use more sophisticated schemes or follow the upstream release name if the plugin represents a wrapper for another program.&lt;br /&gt;
&lt;br /&gt;
It is a good practise to tag the code in the source code management system for each plugin release and then make the tag names matching this release name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;release = &#039;v2.7-r1&#039;; // This is our first revision for Moodle 2.7.x branch.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;dependencies&#039;&#039;&#039;&lt;br /&gt;
| array&lt;br /&gt;
| optional&lt;br /&gt;
| Allows to declare explicit dependency on other plugin(s) for this plugin to work. Moodle core checks these declared dependencies and will not allow the plugin installation and/or upgrade until all dependencies are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each element in the array represents an dependency on another plugin. The key (string) in the array is the component (frankenstyle) name of the other plugin. The value (integer) is the version of the other plugin we require. The predefined constant &amp;lt;tt&amp;gt;ANY_VERSION&amp;lt;/tt&amp;gt; can be used if we do not depend on a particular version (we just need the other plugin present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;dependencies = array(&lt;br /&gt;
    &#039;mod_foo&#039; =&amp;gt; ANY_VERSION,   // The Foo activity must be present (any version).&lt;br /&gt;
    &#039;enrol_bar&#039; =&amp;gt; 2014020300, // The Bar enrolment plugin version 2014020300 or higher must be present.&lt;br /&gt;
);&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File template ==&lt;br /&gt;
&lt;br /&gt;
Here is a template for the plugin&#039;s version.php file to copy and paste:&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;
 * @package   plugintype_pluginname&lt;br /&gt;
 * @copyright 2014, You Name &amp;lt;your@email.address&amp;gt;&lt;br /&gt;
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;version   = TODO;&lt;br /&gt;
$plugin-&amp;gt;requires  = TODO;&lt;br /&gt;
$plugin-&amp;gt;cron      = 0;&lt;br /&gt;
$plugin-&amp;gt;component = &#039;TODO_FRANKENSTYLE&#039;;&lt;br /&gt;
$plugin-&amp;gt;maturity  = MATURITY_STABLE;&lt;br /&gt;
$plugin-&amp;gt;release   = &#039;TODO&#039;;&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;dependencies = array(&lt;br /&gt;
    &#039;mod_forum&#039; =&amp;gt; ANY_VERSION,&lt;br /&gt;
    &#039;mod_data&#039;  =&amp;gt; TODO&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Moodle versions]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=47097</id>
		<title>version.php</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=version.php&amp;diff=47097"/>
		<updated>2015-01-06T15:52:07Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* Plugin version properties */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an essential file expected to be located in the root directory of the plugin.&lt;br /&gt;
&lt;br /&gt;
It contains a number of properties, which are used during the plugin installation and upgrade process. It allows to make sure the plugin is compatible with the given Moodle site, as well as spotting whether an upgrade is needed.&lt;br /&gt;
&lt;br /&gt;
The file is a standard PHP file, starting with an opening &#039;&amp;amp;lt;?php&#039; tag and defining the following variables:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; There has been an exception for [[Activity modules]]. Before Moodle 2.6, &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; was used instead of &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; in the version.php file of this plugin type. Starting in Moodle 2.6, activity modules support the &amp;lt;tt&amp;gt;$plugin-&amp;gt;&amp;lt;/tt&amp;gt; notation and it is recommended to use it. Support for &amp;lt;tt&amp;gt;$module-&amp;gt;&amp;lt;/tt&amp;gt; will be dropped in Moodle 3.0. (MDL-43896)&lt;br /&gt;
&lt;br /&gt;
== Plugin version properties ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;nicetable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Property&lt;br /&gt;
! Type&lt;br /&gt;
! Status&lt;br /&gt;
! Description and example&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;version&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| required&lt;br /&gt;
| The version number of the plugin. The format is partially date based with the form YYYYMMDDXX where XX is an incremental counter for the given year (YYYY), month (MM) and date (DD) of the plugin version&#039;s release. Every new plugin version must have this number increased in this file, which is detected by Moodle core and the upgrade process is triggered.&lt;br /&gt;
&lt;br /&gt;
In case of linear development (i.e. there is just one branch of the plugin maintained), the XX counter should stay with the initial 00 value unless multiple versions are released on the same date.&lt;br /&gt;
&lt;br /&gt;
If multiple stable branches of the plugin are maintained, the date part YYYYMMDD should be frozen at the branch forking date and the XX is used for incrementing the value on the given stable branch (allowing up to 100 updates on the given stable branch). The key point is that the version number is always increased both horizontally (within the same stable branch, more recent updates have higher XX than any previous one) and vertically (between any two stable branches, the more recent branch has YYYYMMDD00 higher than the older stable branch). Pay attention to this. It&#039;s easy to mess up and hard to fix.&lt;br /&gt;
&lt;br /&gt;
See [https://moodle.org/mod/forum/discuss.php?d=275635#p1184986] for an example of versioning scheme on multiple branches.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;version = 2014052000; // Plugin released on 20th May 2014.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;requires&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| recommended&lt;br /&gt;
| Specifies the minimum version number of Moodle core that this plugin requires. It is not possible to install it to earlier Moodle version. Moodle core&#039;s version number is defined in the file version.php located in Moodle root directory, in the &amp;lt;tt&amp;gt;$version&amp;lt;/tt&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
Note that Moodle core extends the version numbering scheme and has inbuilt support for decimal versioning (such as &amp;lt;tt&amp;gt;$version = 2014051200.01;&amp;lt;/tt&amp;gt;). The decimal part is used for incremental (weekly) builds. Plugins should not rely on a particular weekly build so they use just the whole number part of the Moodle core&#039;s version in this field.&lt;br /&gt;
&lt;br /&gt;
See [[Releases]] for the full list of Moodle core version numbers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;requires = 2014051200; // Moodle 2.7.0 is required.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;cron&#039;&#039;&#039;&lt;br /&gt;
| integer&lt;br /&gt;
| optional&lt;br /&gt;
| Allows to throttle the plugin&#039;s cron function calls. If set to 0, or not set, the cron function is disabled. The set value represents a minimal required gap in seconds between two calls of the plugin&#039;s cron function. Note that the cron function is not supported in all plugin types (also note that, while the cron function *is* supported for authentication plugins, it is run every time the cron script runs, regardless of this value in version.php). This value is stored in the database. After changing this value, the version number must be incremented.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Note:&#039;&#039; Starting with Moodle 2.7, plugin developers are encouraged to use the [[Task API|scheduled task API]] instead of the cron function feature.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;cron = 300; // Do not execute this plugin&#039;s cron more often than every five minutes.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;component&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| recommended&lt;br /&gt;
| The full [[Frankenstyle|frankenstyle]] compoment name in the form of plugintype_pluginname. It is used during the installation and upgrade process for diagnostics and validation purposes to make sure the plugin code has been deployed to the correct location within the Moodle code tree.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;component = &#039;block_foobar&#039;; // Declare the type and name of this plugin.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;maturity&#039;&#039;&#039;&lt;br /&gt;
| enum&lt;br /&gt;
| recommended&lt;br /&gt;
| Declares the maturity level of this plugin version, that is how stable it is. This affects the [[:en:Available update notifications|available update notifications]] feature in Moodle. Administrators can configure their site so that they are not notified about an available update unless it has certain maturity level declared.&lt;br /&gt;
&lt;br /&gt;
Supported value is any of the predefined constants &amp;lt;tt&amp;gt;MATURITY_ALPHA, MATURITY_BETA, MATURITY_RC&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MATURITY_STABLE&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;maturity = MATURITY_STABLE; // This is considered as ready for production sites.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;release&#039;&#039;&#039;&lt;br /&gt;
| string&lt;br /&gt;
| recommended&lt;br /&gt;
| Human readable version name that should help to identify each release of the plugin. It can be anything you like although it is recommended to choose a pattern and stick with it. Usually it is a simple version like &amp;lt;tt&amp;gt;2.1&amp;lt;/tt&amp;gt; but some plugin authors use more sophisticated schemes or follow the upstream release name if the plugin represents a wrapper for another program.&lt;br /&gt;
&lt;br /&gt;
It is a good practise to tag the code in the source code management system for each plugin release and then make the tag names matching this release name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;release = &#039;v2.7-r1&#039;; // This is our first revision for Moodle 2.7.x branch.&amp;lt;/code&amp;gt;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| &#039;&#039;&#039;$plugin-&amp;gt;dependencies&#039;&#039;&#039;&lt;br /&gt;
| array&lt;br /&gt;
| optional&lt;br /&gt;
| Allows to declare explicit dependency on other plugin(s) for this plugin to work. Moodle core checks these declared dependencies and will not allow the plugin installation and/or upgrade until all dependencies are satisfied.&lt;br /&gt;
&lt;br /&gt;
Each element in the array represents an dependency on another plugin. The key (string) in the array is the component (frankenstyle) name of the other plugin. The value (integer) is the version of the other plugin we require. The predefined constant &amp;lt;tt&amp;gt;ANY_VERSION&amp;lt;/tt&amp;gt; can be used if we do not depend on a particular version (we just need the other plugin present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$plugin-&amp;gt;dependencies = array(&lt;br /&gt;
    &#039;mod_foo&#039; =&amp;gt; ANY_VERSION,   // The Foo activity must be present (any version).&lt;br /&gt;
    &#039;enrol_bar&#039; =&amp;gt; 2014020300, // The Bar enrolment plugin version 2014020300 or higher must be present.&lt;br /&gt;
);&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File template ==&lt;br /&gt;
&lt;br /&gt;
Here is a template for the plugin&#039;s version.php file to copy and paste:&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;
 * @package   plugintype_pluginname&lt;br /&gt;
 * @copyright 2014, You Name &amp;lt;your@email.address&amp;gt;&lt;br /&gt;
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;version   = TODO;&lt;br /&gt;
$plugin-&amp;gt;requires  = TODO;&lt;br /&gt;
$plugin-&amp;gt;cron      = 0;&lt;br /&gt;
$plugin-&amp;gt;component = &#039;TODO_FRANKENSTYLE&#039;;&lt;br /&gt;
$plugin-&amp;gt;maturity  = MATURITY_STABLE;&lt;br /&gt;
$plugin-&amp;gt;release   = &#039;TODO&#039;;&lt;br /&gt;
&lt;br /&gt;
$plugin-&amp;gt;dependencies = array(&lt;br /&gt;
    &#039;mod_forum&#039; =&amp;gt; ANY_VERSION,&lt;br /&gt;
    &#039;mod_data&#039;  =&amp;gt; TODO&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Moodle versions]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Plugins]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=44828</id>
		<title>Assign submission plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Assign_submission_plugins&amp;diff=44828"/>
		<updated>2014-05-16T10:56:51Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* db/install.php */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This page gives an overview of assignment submission plugims within the assignment module.&lt;br /&gt;
&lt;br /&gt;
=== Overview of an assignment submission plugin ===&lt;br /&gt;
An assignment submission plugin is used to display custom form fields to a student when they are editing their assignment submission. It also has full control over the display the submitted assignment to graders and students. Plugins participate in all assignment features including backup/restore, upgrades from 2.2, offline grading, group assignments and blind marking.&lt;br /&gt;
&amp;lt;gallery&amp;gt;&lt;br /&gt;
File:assignsubmission_plugin_overview1.png|An assignment submission plugin can add settings to the module settings page.&lt;br /&gt;
File:assignsubmission_plugin_overview2.png|An assignment submission plugin can show a summary of the submission to students and graders.&lt;br /&gt;
File:assignsubmission_plugin_overview3.png|An assignment submission plugin can add form fields to the student submission page.&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
Assignment submission plugins were added with the assignment module rewrite for Moodle 2.3. &lt;br /&gt;
&lt;br /&gt;
== Template ==&lt;br /&gt;
A great example is the &amp;quot;onlinetext&amp;quot; submission plugin included with Moodle core. &lt;br /&gt;
&lt;br /&gt;
== File structure ==&lt;br /&gt;
The files for a custom submission plugin sit under &amp;quot;mod/assign/submission/&amp;lt;pluginname&amp;gt;&amp;quot;. A plugin should not include any custom files outside of it&#039;s own plugin folder. &lt;br /&gt;
&lt;br /&gt;
Note: The plugin name should be no longer than 11 characters - this is because the database tables for a submission plugin must be prefixed with &amp;quot;assignsubmission_&amp;quot; + pluginname (17 chars + X) and the table names can be no longer than 28 chars (thanks oracle). If a plugin requires multiple database tables, the plugin name will need to be shorter to allow different table names to fit under the 28 character limit.&lt;br /&gt;
&lt;br /&gt;
All examples in this document exclude the required copyright and license information from source files for brevity.&lt;br /&gt;
&lt;br /&gt;
=== version.php ===&lt;br /&gt;
&lt;br /&gt;
To start with we need to tell Moodle the version information for our new plugin so that it can be installed and upgraded correctly. This information is added to version.php as with any other type of Moodle plugin. The component name must begin with &amp;quot;assignsubmission_&amp;quot; to identify this as a submission plugin.&lt;br /&gt;
&lt;br /&gt;
See [[version.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
defined(&#039;MOODLE_INTERNAL&#039;) || die();                                                                                                &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;version   = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;requires  = 2012112900;                                                                                                    &lt;br /&gt;
$plugin-&amp;gt;component = &#039;assignsubmission_file&#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== settings.php ===&lt;br /&gt;
&lt;br /&gt;
The settings file allows us to add custom settings to the system wide configuration page for our plugin. &lt;br /&gt;
&lt;br /&gt;
All submission settings should be named &#039;assignsubmission_pluginname/settingname&#039; in order for the setting to be associated with the plugin.&lt;br /&gt;
&lt;br /&gt;
All submission plugins should include one setting named &#039;default&#039; to indicate if the plugin should be enabled by default when creating a new assignment. &lt;br /&gt;
&lt;br /&gt;
This example from the submission_file plugin also checks to see if there is a maxbytes setting for this moodle installation and if found, it adds a new admin setting to the settings page. The name of the setting should begin with the plugin component name (&amp;quot;assignsubmission_file&amp;quot;) in this case. The strings are specified in this plugins language file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Note: This is on by default.                                                                                                     &lt;br /&gt;
$settings-&amp;gt;add(new admin_setting_configcheckbox(&#039;assignsubmission_file/default&#039;,                                                    &lt;br /&gt;
                   new lang_string(&#039;default&#039;, &#039;assignsubmission_file&#039;),                                                             &lt;br /&gt;
                   new lang_string(&#039;default_help&#039;, &#039;assignsubmission_file&#039;), 1));                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
if (isset($CFG-&amp;gt;maxbytes)) {                                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $name = new lang_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
    $description = new lang_string(&#039;configmaxbytes&#039;, &#039;assignsubmission_file&#039;);                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $element = new admin_setting_configselect(&#039;assignsubmission_file/maxbytes&#039;,                                                     &lt;br /&gt;
                                              $name,                                                                                &lt;br /&gt;
                                              $description,                                                                         &lt;br /&gt;
                                              1048576,                                                                              &lt;br /&gt;
                                              get_max_upload_sizes($CFG-&amp;gt;maxbytes));                                                &lt;br /&gt;
    $settings-&amp;gt;add($element);                                                                                                       &lt;br /&gt;
}                                                                          &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== lang/en/submission_pluginname.php ===&lt;br /&gt;
&lt;br /&gt;
The language file for this plugin must have the same name as the component name (e.g. &amp;quot;submission_file.php&amp;quot;). It should at least define a string for &amp;quot;pluginname&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$string[&#039;pluginname&#039;] = &#039;Awesome submissions&#039;;      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/access.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any additional capabilities are defined if required. This file can be omitted if there are no capabilities added by the plugin.&lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#access.php]] for more information.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$capabilities = array(&lt;br /&gt;
    &#039;assignsubmission/dungeon:master&#039; =&amp;gt; array(&lt;br /&gt;
        &#039;riskbitmask&#039; =&amp;gt; RISK_XSS,&lt;br /&gt;
        &#039;captype&#039; =&amp;gt; &#039;write&#039;,&lt;br /&gt;
        &#039;contextlevel&#039; =&amp;gt; CONTEXT_COURSE,&lt;br /&gt;
        &#039;archetypes&#039; =&amp;gt; array(&lt;br /&gt;
            &#039;editingteacher&#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &#039;manager&#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        ),&lt;br /&gt;
        &#039;clonepermissionsfrom&#039; =&amp;gt; &#039;moodle/course:manageactivities&#039;&lt;br /&gt;
    ),&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/upgrade.php ===&lt;br /&gt;
&lt;br /&gt;
This is where any upgrade code is defined. &lt;br /&gt;
&lt;br /&gt;
See [[Activity_modules#upgrade.php]] for more infomation.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function xmldb_submission_file_upgrade($oldversion) {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    $dbman = $DB-&amp;gt;get_manager();&lt;br /&gt;
    if ($oldversion &amp;lt; 2012091800) {&lt;br /&gt;
        // Put upgrade code here&lt;br /&gt;
&lt;br /&gt;
        // Savepoint reached.&lt;br /&gt;
        upgrade_plugin_savepoint(true, 2012091800, &#039;assignsubmission&#039;, &#039;file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.xml ===&lt;br /&gt;
&lt;br /&gt;
This is where any database tables required to save this plugins data are defined. File submissions define a table that links to submission and contains a column to record the number of files. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;                                                                                             &lt;br /&gt;
&amp;lt;XMLDB PATH=&amp;quot;mod/assign/submission/file/db&amp;quot; VERSION=&amp;quot;20120423&amp;quot; COMMENT=&amp;quot;XMLDB file for Moodle mod/assign/submission/file&amp;quot;           &lt;br /&gt;
    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;                                                                           &lt;br /&gt;
    xsi:noNamespaceSchemaLocation=&amp;quot;../../../../../lib/xmldb/xmldb.xsd&amp;quot;                                                              &lt;br /&gt;
&amp;gt;                                                                                                                                   &lt;br /&gt;
  &amp;lt;TABLES&amp;gt;                                                                                                                          &lt;br /&gt;
    &amp;lt;TABLE NAME=&amp;quot;assignsubmission_file&amp;quot; COMMENT=&amp;quot;Info about file submissions for assignments&amp;quot;&amp;gt;                                      &lt;br /&gt;
      &amp;lt;FIELDS&amp;gt;                                                                                                                      &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;id&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; SEQUENCE=&amp;quot;true&amp;quot;/&amp;gt;                                                    &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot;/&amp;gt;                               &lt;br /&gt;
        &amp;lt;FIELD NAME=&amp;quot;numfiles&amp;quot; TYPE=&amp;quot;int&amp;quot; LENGTH=&amp;quot;10&amp;quot; NOTNULL=&amp;quot;true&amp;quot; DEFAULT=&amp;quot;0&amp;quot; SEQUENCE=&amp;quot;false&amp;quot; COMMENT=&amp;quot;The number of files the student submitted.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/FIELDS&amp;gt;                                                                                                                     &lt;br /&gt;
      &amp;lt;KEYS&amp;gt;                                                                                                                        &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;primary&amp;quot; TYPE=&amp;quot;primary&amp;quot; FIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The unique id for this submission info.&amp;quot;/&amp;gt;                          &lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;assignment&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;assignment&amp;quot; REFTABLE=&amp;quot;assign&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The assignment instance this submission relates to&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;KEY NAME=&amp;quot;submission&amp;quot; TYPE=&amp;quot;foreign&amp;quot; FIELDS=&amp;quot;submission&amp;quot; REFTABLE=&amp;quot;assign_submission&amp;quot; REFFIELDS=&amp;quot;id&amp;quot; COMMENT=&amp;quot;The submission this file submission relates to.&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/KEYS&amp;gt;                                                                                                                       &lt;br /&gt;
    &amp;lt;/TABLE&amp;gt;                                                                                                                        &lt;br /&gt;
  &amp;lt;/TABLES&amp;gt;                                                                                                                         &lt;br /&gt;
&amp;lt;/XMLDB&amp;gt;                           &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== db/install.php ===&lt;br /&gt;
&lt;br /&gt;
This example is from the submission_comments plugin. It shows how to run custom code on installation of the plugin. In this case it makes the comments plugin the last of the three submission plugins installed by default. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Code run after the plugin database tables have been created.&lt;br /&gt;
 */&lt;br /&gt;
function xmldb_assignsubmission_comments_install() {&lt;br /&gt;
    global $CFG, $DB, $OUTPUT;&lt;br /&gt;
&lt;br /&gt;
    // do the install&lt;br /&gt;
&lt;br /&gt;
    require_once($CFG-&amp;gt;dirroot . &#039;/mod/assign/locallib.php&#039;);&lt;br /&gt;
    // set the correct initial order for the plugins&lt;br /&gt;
    $assignment = new assignment();&lt;br /&gt;
    $plugin = $assignment-&amp;gt;get_submission_plugin_by_type(&#039;comments&#039;);&lt;br /&gt;
    if ($plugin) {&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
        $plugin-&amp;gt;move(&#039;down&#039;);&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== locallib.php ===&lt;br /&gt;
&lt;br /&gt;
This is where all the functionality for this plugin is defined. We will step through this file and describe each part as we go.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class assign_submission_file extends assign_submission_plugin {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All submission plugins MUST define a class with the component name of the plugin that extends assign_submission_plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_name()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function get_name() {&lt;br /&gt;
        return get_string(&#039;file&#039;, &#039;assignsubmission_file&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Get name is abstract in submission_plugin and must be defined in your new plugin. Use the language strings to make your plugin translatable. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_settings(MoodleQuickForm $mform) {&lt;br /&gt;
       global $CFG, $COURSE;                                                                                                       &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $defaultmaxfilesubmissions = $this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;);                                                       &lt;br /&gt;
        $defaultmaxsubmissionsizebytes = $this-&amp;gt;get_config(&#039;maxsubmissionsizebytes&#039;);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings = array();                                                                                                        &lt;br /&gt;
        $options = array();                                                                                                         &lt;br /&gt;
        for ($i = 1; $i &amp;lt;= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {                                                                  &lt;br /&gt;
            $options[$i] = $i;                                                                                                      &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maxfilessubmission&#039;, &#039;assignsubmission_file&#039;);                                                          &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxfiles&#039;, $name, $options);                                            &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxfiles&#039;,                                                                     &lt;br /&gt;
                              &#039;maxfilessubmission&#039;,                                                                                 &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxfiles&#039;, $defaultmaxfilesubmissions);                                           &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxfiles&#039;, &#039;assignsubmission_file_enabled&#039;, &#039;notchecked&#039;);                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $choices = get_max_upload_sizes($CFG-&amp;gt;maxbytes,                                                                             &lt;br /&gt;
                                        $COURSE-&amp;gt;maxbytes,                                                                          &lt;br /&gt;
                                        get_config(&#039;assignsubmission_file&#039;, &#039;maxbytes&#039;));                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $settings[] = array(&#039;type&#039; =&amp;gt; &#039;select&#039;,                                                                                     &lt;br /&gt;
                            &#039;name&#039; =&amp;gt; &#039;maxsubmissionsizebytes&#039;,                                                                     &lt;br /&gt;
                            &#039;description&#039; =&amp;gt; get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;),                          &lt;br /&gt;
                            &#039;options&#039;=&amp;gt; $choices,                                                                                   &lt;br /&gt;
                            &#039;default&#039;=&amp;gt; $defaultmaxsubmissionsizebytes);                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $name = get_string(&#039;maximumsubmissionsize&#039;, &#039;assignsubmission_file&#039;);                                                       &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;assignsubmission_file_maxsizebytes&#039;, $name, $choices);                                        &lt;br /&gt;
        $mform-&amp;gt;addHelpButton(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                 &lt;br /&gt;
                              &#039;maximumsubmissionsize&#039;,                                                                              &lt;br /&gt;
                              &#039;assignsubmission_file&#039;);                                                                             &lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;assignsubmission_file_maxsizebytes&#039;, $defaultmaxsubmissionsizebytes);                                   &lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;assignsubmission_file_maxsizebytes&#039;,                                                                    &lt;br /&gt;
                           &#039;assignsubmission_file_enabled&#039;,                                                                         &lt;br /&gt;
                           &#039;notchecked&#039;);                             &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;get_settings&amp;quot; function is called when building the settings page for the assignment. It allows this plugin to add a list of settings to the form. Notice that the settings are prefixed by the plugin name which is good practice to avoid conflicts with other plugins. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function save_settings(stdClass $data) {                                                                                 &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $data-&amp;gt;assignsubmission_file_maxfiles);                                             &lt;br /&gt;
        $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $data-&amp;gt;assignsubmission_file_maxsizebytes);                                     &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save_settings&amp;quot; function is called when the assignment settings page is submitted, either for a new assignment or when editing an existing one. For settings specific to a single instance of the assignment you can use the assign_plugin::set_config function shown here to save key/value pairs against this assignment instance for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_form_elements()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($this-&amp;gt;get_config(&#039;maxfilesubmissions&#039;) &amp;lt;= 0) {                                                                         &lt;br /&gt;
            return false;                                                                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
        $submissionid = $submission ? $submission-&amp;gt;id : 0;                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_prepare_standard_filemanager($data,                                                                            &lt;br /&gt;
                                                  &#039;files&#039;,                                                                          &lt;br /&gt;
                                                  $fileoptions,                                                                     &lt;br /&gt;
                                                  $this-&amp;gt;assignment-&amp;gt;get_context(),                                                 &lt;br /&gt;
                                                  &#039;assignsubmission_file&#039;,                                                          &lt;br /&gt;
                                                  ASSIGNSUBMISSION_FILE_FILEAREA,                                                   &lt;br /&gt;
                                                  $submissionid);                                                                   &lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;filemanager&#039;, &#039;files_filemanager&#039;, html_writer::tag(&#039;span&#039;, $this-&amp;gt;get_name(),                          &lt;br /&gt;
            array(&#039;class&#039; =&amp;gt; &#039;accesshide&#039;)), null, $fileoptions);                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                    &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The get_form_elements function is called when building the submission form. It functions identically to the get_settings function except that the submission object is available (if there is a submission) to associate the settings with a single submission. This example also shows how to use a filemanager within a submission plugin. The function must return true if it has modified the form otherwise the assignment will not include a header for this plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; save()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function save(stdClass $submission, stdClass $data) {                                                                    &lt;br /&gt;
        global $USER, $DB;                                                                                                          &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fileoptions = $this-&amp;gt;get_file_options();                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $data = file_postupdate_standard_filemanager($data,                                                                         &lt;br /&gt;
                                                     &#039;files&#039;,                                                                       &lt;br /&gt;
                                                     $fileoptions,                                                                  &lt;br /&gt;
                                                     $this-&amp;gt;assignment-&amp;gt;get_context(),                                              &lt;br /&gt;
                                                     &#039;assignsubmission_file&#039;,                                                       &lt;br /&gt;
                                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                &lt;br /&gt;
                                                     $submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $filesubmission = $this-&amp;gt;get_file_submission($submission-&amp;gt;id);                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Plagiarism code event trigger when files are uploaded.                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Send files to event system.                                                                                              &lt;br /&gt;
        // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).                                &lt;br /&gt;
        $eventdata = new stdClass();                                                                                                &lt;br /&gt;
        $eventdata-&amp;gt;modulename = &#039;assign&#039;;                                                                                          &lt;br /&gt;
        $eventdata-&amp;gt;cmid = $this-&amp;gt;assignment-&amp;gt;get_course_module()-&amp;gt;id;                                                              &lt;br /&gt;
        $eventdata-&amp;gt;itemid = $submission-&amp;gt;id;                                                                                       &lt;br /&gt;
        $eventdata-&amp;gt;courseid = $this-&amp;gt;assignment-&amp;gt;get_course()-&amp;gt;id;                                                                 &lt;br /&gt;
        $eventdata-&amp;gt;userid = $USER-&amp;gt;id;                                                                                             &lt;br /&gt;
        if ($count &amp;gt; 1) {                                                                                                           &lt;br /&gt;
            $eventdata-&amp;gt;files = $files;                                                                                             &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        $eventdata-&amp;gt;file = $files;                                    &lt;br /&gt;
        $eventdata-&amp;gt;pathnamehashes = array_keys($files);                                                                            &lt;br /&gt;
        events_trigger(&#039;assessable_file_uploaded&#039;, $eventdata);                                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($filesubmission) {                                                                                                      &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            return $DB-&amp;gt;update_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                    &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            $filesubmission = new stdClass();                                                                                       &lt;br /&gt;
            $filesubmission-&amp;gt;numfiles = $this-&amp;gt;count_files($submission-&amp;gt;id,                                                         &lt;br /&gt;
                                                           ASSIGNSUBMISSION_FILE_FILEAREA);                                         &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $submission-&amp;gt;id;                                                                          &lt;br /&gt;
            $filesubmission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;                                                    &lt;br /&gt;
            return $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission) &amp;gt; 0;                                                &lt;br /&gt;
        }                                &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;save&amp;quot; function is called to save a user submission. The parameters are the submission object and the data from the submission form. This example calls file_postupdate_standard_filemanager to copy the files from the draft file area to the filearea for this submission, it then uses the event api to trigger an assessable_file_uploaded event for the plagiarism api. It then records the number of files in the plugin specific &amp;quot;assignsubmission_file&amp;quot; table. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_files()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_files($submission) {&lt;br /&gt;
        $result = array();                                                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,                                                          &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $submission-&amp;gt;id,                                                                               &lt;br /&gt;
                                     &#039;timemodified&#039;,                                                                                &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $result[$file-&amp;gt;get_filename()] = $file;                                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return $result;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If this submission plugin produces one or more files, it should implement &amp;quot;get_files&amp;quot; so that the portfolio API can export a list of all the files from all of the plugins for this assignment submission. This is also used by the offline grading feature in the assignment.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view_summary()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view_summary(stdClass $submission, &amp;amp; $showviewlink) {                                                           &lt;br /&gt;
        $count = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Show we show a link to view all files for this plugin?                                                                   &lt;br /&gt;
        $showviewlink = $count &amp;gt; ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;                                                             &lt;br /&gt;
        if ($count &amp;lt;= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {                                                                      &lt;br /&gt;
            return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                    &lt;br /&gt;
                                                        ASSIGNSUBMISSION_FILE_FILEAREA,                                             &lt;br /&gt;
                                                        $submission-&amp;gt;id);                                                           &lt;br /&gt;
        } else {                                                                                                                    &lt;br /&gt;
            return get_string(&#039;countfiles&#039;, &#039;assignsubmission_file&#039;, $count);                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view_summary function is called to display a summary of the submission to both markers and students. It counts the number of files submitted and if it is more that a set number, it only displays a count of how many files are in the submission - otherwise it uses a helper function to write the entire list of files. This is because we want to keep the summaries really short so they can be displayed in a table. There will be a link to view the full submission on the submission status page. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; view()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function view($submission) {&lt;br /&gt;
        return $this-&amp;gt;assignment-&amp;gt;render_area_files(&#039;assignsubmission_file&#039;,                                                        &lt;br /&gt;
                                                    ASSIGNSUBMISSION_FILE_FILEAREA,                                                 &lt;br /&gt;
                                                    $submission-&amp;gt;id);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view function is called to display the entire submission to both markers and students. In this case it uses the helper function in the assignment class to write the list of files. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; can_upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function can_upgrade($type, $version) {&lt;br /&gt;
&lt;br /&gt;
        $uploadsingle_type =&#039;uploadsingle&#039;;&lt;br /&gt;
        $upload_type =&#039;upload&#039;;&lt;br /&gt;
&lt;br /&gt;
        if (($type == $uploadsingle_type || $type == $upload_type) &amp;amp;&amp;amp; $version &amp;gt;= 2011112900) {&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The can_upgrade function is used to identify old &amp;quot;Assignment 2.2&amp;quot; subtypes that can be upgraded by this plugin. This plugin supports upgrades from the old &amp;quot;upload&amp;quot; and &amp;quot;uploadsingle&amp;quot; assignment subtypes. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade_settings()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, &amp;amp; $log) {                                        &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        if ($oldassignment-&amp;gt;assignmenttype == &#039;uploadsingle&#039;) {                                                                     &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, 1);                                                                             &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        } else if ($oldassignment-&amp;gt;assignmenttype == &#039;upload&#039;) {                                                                    &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxfilesubmissions&#039;, $oldassignment-&amp;gt;var1);                                                          &lt;br /&gt;
            $this-&amp;gt;set_config(&#039;maxsubmissionsizebytes&#039;, $oldassignment-&amp;gt;maxbytes);                                                  &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Advanced file upload uses a different setting to do the same thing.                                                  &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;submissiondrafts&#039;,                                                                                      &lt;br /&gt;
                           $oldassignment-&amp;gt;var4,                                                                                    &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
            // Convert advanced file upload &amp;quot;hide description before due date&amp;quot; setting.                                             &lt;br /&gt;
            $alwaysshow = 0;                                                                                                        &lt;br /&gt;
            if (!$oldassignment-&amp;gt;var3) {                                                                                            &lt;br /&gt;
                $alwaysshow = 1;                                                                                                    &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
            $DB-&amp;gt;set_field(&#039;assign&#039;,                                                                                                &lt;br /&gt;
                           &#039;alwaysshowdescription&#039;,                                                                                 &lt;br /&gt;
                           $alwaysshow,                                                                                             &lt;br /&gt;
                           array(&#039;id&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                                                     &lt;br /&gt;
            return true;                                                                                                            &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
    }                     &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is called once per assignment instance to upgrade the settings from the old assignment to the new mod_assign. In this case it sets the maxbytes, maxfiles and alwaysshowdescription configuration settings. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; upgrade()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    public function upgrade($oldcontext,$oldassignment, $oldsubmission, $submission, &amp;amp; $log) {&lt;br /&gt;
        global $DB;&lt;br /&gt;
&lt;br /&gt;
        $file_submission = new stdClass();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        $file_submission-&amp;gt;numfiles = $oldsubmission-&amp;gt;numfiles;&lt;br /&gt;
        $file_submission-&amp;gt;submission = $submission-&amp;gt;id;&lt;br /&gt;
        $file_submission-&amp;gt;assignment = $this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id;&lt;br /&gt;
&lt;br /&gt;
        if (!$DB-&amp;gt;insert_record(&#039;assign_submission_file&#039;, $file_submission) &amp;gt; 0) {&lt;br /&gt;
            $log .= get_string(&#039;couldnotconvertsubmission&#039;, &#039;mod_assign&#039;, $submission-&amp;gt;userid);&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        // now copy the area files&lt;br /&gt;
        $this-&amp;gt;assignment-&amp;gt;copy_area_files_for_upgrade($oldcontext-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assignment&#039;,&lt;br /&gt;
                                                        &#039;submission&#039;,&lt;br /&gt;
                                                        $oldsubmission-&amp;gt;id,&lt;br /&gt;
                                                        // New file area&lt;br /&gt;
                                                        $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id,&lt;br /&gt;
                                                        &#039;mod_assign&#039;,&lt;br /&gt;
                                                        ASSIGN_FILEAREA_SUBMISSION_FILES,&lt;br /&gt;
                                                        $submission-&amp;gt;id);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;upgrade&amp;quot; function upgrades a single submission from the old assignment type to the new one. In this case it involves copying all the files from the old filearea to the new one. There is a helper function available in the assignment class for this (Note: the copy will be fast as it is just adding rows to the files table). If this function returns false, the upgrade will be aborted and rolled back.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_fields()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;    public function () {                                                                                           &lt;br /&gt;
        return array(&#039;onlinetext&#039; =&amp;gt; get_string(&#039;pluginname&#039;, &#039;assignsubmission_comments&#039;));                                        &lt;br /&gt;
    }       &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_fields&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. If a plugin supports multiple text areas it can return the name of each of them here.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_text()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_text($name, $submissionid) {                                                                         &lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {                                                                                                &lt;br /&gt;
            $onlinetextsubmission = $this-&amp;gt;get_onlinetext_submission($submissionid);                                                &lt;br /&gt;
            if ($onlinetextsubmission) {                                                                                            &lt;br /&gt;
                return $onlinetextsubmission-&amp;gt;onlinetext;                                                                           &lt;br /&gt;
            }                                                                                                                       &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return &#039;&#039;;                                                                                                                  &lt;br /&gt;
    }             &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext.  If the plugin uses a text-editor it is ideal if the plugin implements &amp;quot;get_editor_text&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_editor_format()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function get_editor_format($name, $submissionid) {&lt;br /&gt;
        if ($name == &#039;onlinetext&#039;) {&lt;br /&gt;
            $onlinetext_submission = $this-&amp;gt;get_onlinetext_submission($submissionid);&lt;br /&gt;
            if ($onlinetext_submission) {&lt;br /&gt;
                return $onlinetext_submission-&amp;gt;onlineformat;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is from assignsubmission_onlinetext. For the same reason as the previous function, if the plugin uses a text editor, it is ideal if the plugin implements &amp;quot;get_editor_format&amp;quot;. This allows the portfolio to retrieve the text from the plugin when exporting the list of files for a submission. This is required because the text is stored in the plugin specific table that is only known to the plugin itself. The name is used to distinguish between multiple text areas in the one plugin.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; is_empty()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function is_empty(stdClass $submission) {                                                                                &lt;br /&gt;
        return $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;                                            &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a plugin has no submission data to show - it can return true from the is_empty function. This prevents a table row being added to the submission summary for this plugin. It is also used to check if a student has tried to save an assignment with no data.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; get_file_areas()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function get_file_areas() {                                                                                              &lt;br /&gt;
        return array(ASSIGNSUBMISSION_FILE_FILEAREA=&amp;gt;$this-&amp;gt;get_name());                                                            &lt;br /&gt;
    }    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A plugin should implement get_file_areas if it supports saving of any files to moodle - this allows the file areas to be browsed by the moodle file manager.&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; copy_submission()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {                                         &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the files across.                                                                                                   &lt;br /&gt;
        $contextid = $this-&amp;gt;assignment-&amp;gt;get_context()-&amp;gt;id;                                                                          &lt;br /&gt;
        $fs = get_file_storage();                                                                                                   &lt;br /&gt;
        $files = $fs-&amp;gt;get_area_files($contextid,                                                                                    &lt;br /&gt;
                                     &#039;assignsubmission_file&#039;,                                                                       &lt;br /&gt;
                                     ASSIGNSUBMISSION_FILE_FILEAREA,                                                                &lt;br /&gt;
                                     $sourcesubmission-&amp;gt;id,                                                                         &lt;br /&gt;
                                     &#039;id&#039;,                                                                                          &lt;br /&gt;
                                     false);                                                                                        &lt;br /&gt;
        foreach ($files as $file) {                                                                                                 &lt;br /&gt;
            $fieldupdates = array(&#039;itemid&#039; =&amp;gt; $destsubmission-&amp;gt;id);                                                                 &lt;br /&gt;
            $fs-&amp;gt;create_file_from_storedfile($fieldupdates, $file);                                                                 &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        // Copy the assignsubmission_file record.                                                                                   &lt;br /&gt;
        if ($filesubmission = $this-&amp;gt;get_file_submission($sourcesubmission-&amp;gt;id)) {                                                  &lt;br /&gt;
            unset($filesubmission-&amp;gt;id);                                                                                             &lt;br /&gt;
            $filesubmission-&amp;gt;submission = $destsubmission-&amp;gt;id;                                                                      &lt;br /&gt;
            $DB-&amp;gt;insert_record(&#039;assignsubmission_file&#039;, $filesubmission);                                                           &lt;br /&gt;
        }                                                                                                                           &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }                                 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Moodle 2.5 - a students submission can be copied to create a new submission attempt. Plugins should implement this function if they store data associated with the submission (most plugins).&lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; format_for_log()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   public function (stdClass $submission) {                                                                          &lt;br /&gt;
        // format the info for each submission plugin add_to_log                                                                    &lt;br /&gt;
        $filecount = $this-&amp;gt;count_files($submission-&amp;gt;id, ASSIGNSUBMISSION_FILE_FILEAREA);                                           &lt;br /&gt;
        $fileloginfo = &#039;&#039;;                                                                                                          &lt;br /&gt;
        $fileloginfo .= &#039; the number of file(s) : &#039; . $filecount . &amp;quot; file(s).&amp;lt;br&amp;gt;&amp;quot;;                                                 &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return $fileloginfo;                                                                                                        &lt;br /&gt;
    }                      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The format_for_log function lets a plugin produce a really short summary of a submission suitable for adding to a log message. &lt;br /&gt;
&lt;br /&gt;
==== &#039;&#039;&#039; delete_instance()&#039;&#039;&#039; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public function delete_instance() {                                                                                             &lt;br /&gt;
        global $DB;                                                                                                                 &lt;br /&gt;
        // will throw exception on failure                                                                                          &lt;br /&gt;
        $DB-&amp;gt;delete_records(&#039;assignsubmission_file&#039;, array(&#039;assignment&#039;=&amp;gt;$this-&amp;gt;assignment-&amp;gt;get_instance()-&amp;gt;id));                   &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
        return true;                                                                                                                &lt;br /&gt;
    }   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The delete_instance function is called when a plugin is deleted. Note only database records need to be cleaned up - files belonging to fileareas for this assignment will be automatically cleaned up.&lt;br /&gt;
&lt;br /&gt;
=== lib.php ===&lt;br /&gt;
&lt;br /&gt;
This file is the entry point to many standard Moodle APIs for plugins. An example is that in order for a plugin to allow users to download files contained within a filearea belonging to the plugin, they must implement the componentname_pluginfile function in order to perform their own security checks.&lt;br /&gt;
&lt;br /&gt;
See [[File_API]] for more information.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function assignfeedback_file_pluginfile($course,                                                                                    &lt;br /&gt;
                                        $cm,                                                                                        &lt;br /&gt;
                                        context $context,                                                                           &lt;br /&gt;
                                        $filearea,                                                                                  &lt;br /&gt;
                                        $args,                                                                                      &lt;br /&gt;
                                        $forcedownload) {                                                                           &lt;br /&gt;
    global $USER, $DB;                                                                                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($context-&amp;gt;contextlevel != CONTEXT_MODULE) {                                                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    require_login($course, false, $cm);                                                                                             &lt;br /&gt;
    $itemid = (int)array_shift($args);                                                                                              &lt;br /&gt;
    $record = $DB-&amp;gt;get_record(&#039;assign_grades&#039;, array(&#039;id&#039;=&amp;gt;$itemid), &#039;userid,assignment&#039;, MUST_EXIST);                              &lt;br /&gt;
    $userid = $record-&amp;gt;userid;                                                                                                      &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if (!$assign = $DB-&amp;gt;get_record(&#039;assign&#039;, array(&#039;id&#039;=&amp;gt;$cm-&amp;gt;instance))) {                                                         &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    if ($assign-&amp;gt;id != $record-&amp;gt;assignment) {                                                                                       &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    // Check is users feedback or has grading permission.                                                                           &lt;br /&gt;
    if ($USER-&amp;gt;id != $userid and !has_capability(&#039;mod/assign:grade&#039;, $context)) {                                                   &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $relativepath = implode(&#039;/&#039;, $args);                                                                                            &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fullpath = &amp;quot;/{$context-&amp;gt;id}/assignfeedback_file/$filearea/$itemid/$relativepath&amp;quot;;                                              &lt;br /&gt;
                                                                                                                                    &lt;br /&gt;
    $fs = get_file_storage();                                                                                                       &lt;br /&gt;
    if (!$file = $fs-&amp;gt;get_file_by_hash(sha1($fullpath)) or $file-&amp;gt;is_directory()) {                                                 &lt;br /&gt;
        return false;                                                                                                               &lt;br /&gt;
    }                                                                                                                               &lt;br /&gt;
    // Download MUST be forced - security!                                                                                          &lt;br /&gt;
    send_stored_file($file, 0, 0, true);                                                                                            &lt;br /&gt;
}                                            &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Useful classes ==&lt;br /&gt;
A submission plugin has access to a number of useful classes in the assignment module. See the phpdocs (or the code) for more information on these classes.&lt;br /&gt;
&lt;br /&gt;
=== assign_plugin ===&lt;br /&gt;
This abstract class is the base class for all assignment plugins (feedback or submission plugins).&lt;br /&gt;
&lt;br /&gt;
It provides access to the assign class which represents the current assignment instance through &amp;quot;$this-&amp;gt;assignment&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== assign_submission_plugin ===&lt;br /&gt;
This is the base class all assignment submission plugins must extend. It contains a small number of additional function that only apply to submission plugins. &lt;br /&gt;
&lt;br /&gt;
=== assign ===&lt;br /&gt;
This is the main class for interacting with the assignment module. &lt;br /&gt;
&lt;br /&gt;
It contains public functions that are useful for listing users, loading and updating submissions, loading and updating grades, displaying users etc.&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44709</id>
		<title>YUI/Shifter</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44709"/>
		<updated>2014-05-08T14:17:53Z</updated>

		<summary type="html">&lt;p&gt;Maherne: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.5}}&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
{{Moodle 2.7}}&lt;br /&gt;
&lt;br /&gt;
== What is Shifter and what does it do? ==&lt;br /&gt;
&lt;br /&gt;
[http://yui.github.com/shifter Shifter] is a tool written by the YUI team to help &#039;&#039;build&#039;&#039; YUI modules.&lt;br /&gt;
It has been supported since Moodle 2.5 and we encourage its use.&lt;br /&gt;
&lt;br /&gt;
It is written in JavaScript itself, and written as a [http://nodejs.org Node.JS] module which can be installed from [https://npmjs.org/package/shifter NPM].&lt;br /&gt;
&lt;br /&gt;
Shifter takes your source JavaScript file(s), along with some meta-data, and wraps it all together into a built file. This file is known in YUI parlance as a &#039;&#039;&#039;module&#039;&#039;&#039;.&lt;br /&gt;
After building this file, Shifter then produces three versions:&lt;br /&gt;
* the file in its original form (DEBUG - &#039;-debug&#039; filename postfix);&lt;br /&gt;
* the DEBUG file with all debugging calls to Y.Log removed (RAW - no filename postfix); and&lt;br /&gt;
* the RAW file minified using [https://github.com/mishoo/UglifyJS UglifyJS] (&#039;-min&#039; filename postfix).&lt;br /&gt;
&lt;br /&gt;
The meta-data file includes information on the module name, and its dependencies on other modules.&lt;br /&gt;
&lt;br /&gt;
By separating out the actual JS code from its dependencies, we are able to take those dependencies and use them elsewhere in Moodle.&lt;br /&gt;
As an example, this dependency data can be used to inform the Loading system to reduce the number of HTTP requests with Combo Loading. This has a benefical effect upon performance.&lt;br /&gt;
&lt;br /&gt;
Another feature of Shifter is its ability to handle &#039;&#039;rollups&#039;&#039;. Rollups describe the inclusion of multiple YUI classes in the same module. This means that you can have different but related components all in the same module, but without having a single large and less-managable source file.&lt;br /&gt;
&lt;br /&gt;
=== Why do I need build my modules? ===&lt;br /&gt;
&lt;br /&gt;
Technically you don&#039;t, and you can continue to use the old data structure, but by doing so you can benefit from:&lt;br /&gt;
* reduced data sent over the wire due to minification - great for slower connections and mobile devices;&lt;br /&gt;
* reduced number of http queries - the separation of module meta-data means that it can be consumed and fed into the Loader allowing your module to use combo-loading;&lt;br /&gt;
* a slightly simplified JS syntax for your source files.&lt;br /&gt;
&lt;br /&gt;
== Installing Shifter ==&lt;br /&gt;
&lt;br /&gt;
Shifter is written in JavaScript and the [http://nodejs.org Node.JS] environment. You will need to install Node.JS first. This is usually fairly easy as there are pre-built binary packages for &#039;&#039;&#039;Windows and Mac OS&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After installing Node.JS you then use the Node NPM tool to install Shifter:&lt;br /&gt;
   npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
Note: At present, the supported version of Shifter for use in Moodle is version &#039;&#039;0.4.6&#039;&#039;. You are not &#039;&#039;required&#039;&#039; to run a specific version of Shifter, but this is the version that Moodle YUI modules will be built with and you may find it beneficial to do so too.&lt;br /&gt;
&lt;br /&gt;
For &#039;&#039;&#039;Ubuntu&#039;&#039;&#039; run:&lt;br /&gt;
&lt;br /&gt;
   sudo apt-get install npm&lt;br /&gt;
   sudo apt-get install python-software-properties python g++ make&lt;br /&gt;
   sudo apt-get update&lt;br /&gt;
   sudo apt-get install nodejs&lt;br /&gt;
   sudo ln -s /usr/bin/nodejs /usr/local/bin/node&lt;br /&gt;
   sudo npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
== How do I write a YUI module which uses Shifter? ==&lt;br /&gt;
&lt;br /&gt;
Easy - it&#039;s mostly just a case of building the right directory structure.&lt;br /&gt;
&lt;br /&gt;
Assuming that you are creating a new YUI module in your block named fruit, and that your new YUI module will be called fruitbowl, we know the following:&lt;br /&gt;
{|&lt;br /&gt;
|Plugin type&lt;br /&gt;
|block&lt;br /&gt;
|-&lt;br /&gt;
|Plugin name&lt;br /&gt;
|fruit&lt;br /&gt;
|-&lt;br /&gt;
|Plugin frankenstyle name&lt;br /&gt;
|block_fruit&lt;br /&gt;
|-&lt;br /&gt;
|YUI Module name&lt;br /&gt;
|moodle-block_fruit-fruitbowl&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This means that your Moodle plugin directory is:&lt;br /&gt;
  /blocks/fruit&lt;br /&gt;
&lt;br /&gt;
And your &#039;&#039;fruitbowl&#039;&#039; YUI module source will therefore be in:&lt;br /&gt;
  /blocks/fruit/yui/src/fruitbowl&lt;br /&gt;
&lt;br /&gt;
The overall module directory structure looks like this:&lt;br /&gt;
&amp;lt;code bash&amp;gt;&lt;br /&gt;
yui/&lt;br /&gt;
|-- src&lt;br /&gt;
    |-- fruitbowl&lt;br /&gt;
        |-- build.json&lt;br /&gt;
        |-- js&lt;br /&gt;
        |   |-- fruitbowl.js&lt;br /&gt;
        |-- meta&lt;br /&gt;
            |-- fruitbowl.json&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== build.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;build.json&#039;&#039; file defines the eventual modules which will be built, and the source files that you&#039;ve written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;name&amp;quot;: &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;,&lt;br /&gt;
  &amp;quot;builds&amp;quot;: {&lt;br /&gt;
    &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
      &amp;quot;jsfiles&amp;quot;: [&lt;br /&gt;
        &amp;quot;fruitbowl.js&amp;quot;&lt;br /&gt;
      ]&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== meta/MODNAME.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;meta/MODNAME.json&#039;&#039; file describes the list of dependencies for each module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
    &amp;quot;requires&amp;quot;: [&lt;br /&gt;
        &amp;quot;base&amp;quot;,&lt;br /&gt;
        &amp;quot;node&amp;quot;&lt;br /&gt;
    ]&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== js/MODNAME.js ===&lt;br /&gt;
&lt;br /&gt;
The files in the js directory are the contents of your YUI module. This should include setting it up in the &#039;&#039;&#039;&#039;&#039;M&#039;&#039;&#039;&#039;&#039; namespace. As an example of a very basic structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
M.block_fruit = M.block_fruit || {};&lt;br /&gt;
M.block_fruit.fruitbowl = {&lt;br /&gt;
  init: function() {&lt;br /&gt;
    Y.one(&#039;#example&#039;).set(&#039;innerHTML&#039;, &#039;Example content&#039;);&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Can I put two modules in one directory tree? ===&lt;br /&gt;
&lt;br /&gt;
If you are still using Moodle 2.5, this does not work due to bug MDL-40478 (the non-default files cannot be loaded).&lt;br /&gt;
&lt;br /&gt;
If you are using Moodle 2.6+ this is possible. It may sometimes be sensible if the modules are not independent, e.g. if one module will only ever be used by the other one rather than directly called from PHP, or another module.&lt;br /&gt;
&lt;br /&gt;
If the modules are independent or might well be used separately from each other, then it is probably best to use a separate directory tree for each.&lt;br /&gt;
&lt;br /&gt;
See examples in mod/scorm/yui/src and lib/yui/src/notification.&lt;br /&gt;
&lt;br /&gt;
=== How do I load one of these modules from PHP code? ===&lt;br /&gt;
&lt;br /&gt;
You need to run Shifter so that it generates the compiled code. See below.&lt;br /&gt;
&lt;br /&gt;
Once you&#039;ve done that, you can use PHP code like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;yui_module(&lt;br /&gt;
        &#039;moodle-block_fruit-fruitbowl&#039;, &#039;M.block_fruit.fruitbowl.init&#039;, array());&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(The last empty array contains parameters for the init function, if required.)&lt;br /&gt;
&lt;br /&gt;
== How do I use shifter? ==&lt;br /&gt;
&lt;br /&gt;
So after writing out your structure, you now need to build it into a module. After installing Shifter, you have several options.&lt;br /&gt;
&lt;br /&gt;
=== During development ===&lt;br /&gt;
&lt;br /&gt;
A really nice way of running shifter during development is to have it detect changes to your files as you save them:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --watch&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;--watch&#039;&#039;&#039; option will watch for any changes to JS, JSON, CSS, or images within any module in the current directory and build that module when it detects changes.&lt;br /&gt;
&#039;&#039;Note: There can be a short delay of a few seconds while the changes are discovered and the file is built&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This is great for development because you don&#039;t have to continously run shifter manually.&lt;br /&gt;
&lt;br /&gt;
=== All YUI modules for the Moodle plugin you&#039;re currently working on ===&lt;br /&gt;
&lt;br /&gt;
If you are working on several YUI modules at once, then you can have shifter build all of them at the same time:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --walk&lt;br /&gt;
&lt;br /&gt;
=== Just the module you&#039;re working on now ===&lt;br /&gt;
If you want to build just the module you&#039;re working on at the moment:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --walk fruitbowl&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src/fruitbowl&lt;br /&gt;
  shifter&lt;br /&gt;
&lt;br /&gt;
=== Across the whole of Moodle ===&lt;br /&gt;
If you want to build every YUI module in Moodle at once:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle&lt;br /&gt;
  shifter --walk --recursive&lt;br /&gt;
&lt;br /&gt;
== Submitting my JavaScript back to Moodle core ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re working on some JavaScript for Moodle core, either in the form of a bug fix or a new feature, then you probably want to know what to do with your built module.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;We ask that you build your module and submit both the changes to tbe built module and the source directory.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In addition, the Continuous Integration server (Jenkins) will run &#039;&#039;shifter --walk --recursive&#039;&#039; on every run to ensure that all built files are up-to-date and correct.&lt;br /&gt;
&lt;br /&gt;
If there are several commits to the same YUI module within a single period of Moodle integration, then the built files will conflict. In this case the integration team will manually build the module and resolve the conflict. If you are aware that there may be multiple changes to the same file, please indicate this when submitting your change for Integration Review. If you are familiar with the changes, you may wish to consider building the issues upon one another too.&lt;br /&gt;
&lt;br /&gt;
== How do I convert my existing module? ==&lt;br /&gt;
&lt;br /&gt;
If you already have an existing moodle YUI module and want to convert it to the new Shifter format, then it&#039;s pretty easy on the whole:&lt;br /&gt;
&lt;br /&gt;
# create a new src/MODNAME directory;&lt;br /&gt;
# create js and meta directories within this;&lt;br /&gt;
# copy over your existing js file into the js directory;&lt;br /&gt;
# extract the meta-data;&lt;br /&gt;
# write your build.json;&lt;br /&gt;
# remove the YUI.add() wrapper from the top and bottom of your js file; and&lt;br /&gt;
# remove the old file.&lt;br /&gt;
&lt;br /&gt;
To create the complete directory structure and copy your existing code into the right directory (steps 1 - 3):&lt;br /&gt;
&lt;br /&gt;
  mkdir -p src/MODNAME/{js,meta}&lt;br /&gt;
  cp MODNAME/MODNAME.js src/MODNAME/js/&lt;br /&gt;
&lt;br /&gt;
Refer to the section above on the correct contents of the build.json and meta/MODNAME.json files.&lt;br /&gt;
&lt;br /&gt;
== Can I run my shifted module in older versions of Moodle 2 (before 2.5)? ==&lt;br /&gt;
&lt;br /&gt;
We are aware that many developers wish to reduce the overhead of maintaining a plugin for multiple versions of Moodle and that using Shifter will make this much harder. Unfortunately you can&#039;t use the shifted version of your module directly, but you can run them in parallel by copying the built file into the old directory structure.&lt;br /&gt;
&lt;br /&gt;
Moodle will always &#039;&#039;prefer&#039;&#039; the built shifted module over the older style module if both are present.&lt;br /&gt;
&lt;br /&gt;
If doing this, we would recommend copying over the minified version of your file so that older versions of moodle benefit from your user of shifted code.&lt;br /&gt;
&lt;br /&gt;
On a Linux-like operating system, you can use the following (again with our fruit example):&lt;br /&gt;
  cp yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl-min.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
Whilst developing, you may want to use a symlink so that you don&#039;t have to keep copying your changes after building them:&lt;br /&gt;
  ln -s yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[JavaScript guidelines]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44708</id>
		<title>YUI/Shifter</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44708"/>
		<updated>2014-05-08T14:16:57Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* All YUI modules for the Moodle plugin you&amp;#039;re currently working on */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.5}}&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
{{Moodle 2.7}}&lt;br /&gt;
&lt;br /&gt;
== What is Shifter and what does it do? ==&lt;br /&gt;
&lt;br /&gt;
[http://yui.github.com/shifter Shifter] is a tool written by the YUI team to help &#039;&#039;build&#039;&#039; YUI modules.&lt;br /&gt;
It has been supported since Moodle 2.5 and we encourage its use.&lt;br /&gt;
&lt;br /&gt;
It is written in JavaScript itself, and written as a [http://nodejs.org Node.JS] module which can be installed from [https://npmjs.org/package/shifter NPM].&lt;br /&gt;
&lt;br /&gt;
Shifter takes your source JavaScript file(s), along with some meta-data, and wraps it all together into a built file. This file is known in YUI parlance as a &#039;&#039;&#039;module&#039;&#039;&#039;.&lt;br /&gt;
After building this file, Shifter then produces three versions:&lt;br /&gt;
* the file in its original form (DEBUG - &#039;-debug&#039; filename postfix);&lt;br /&gt;
* the DEBUG file with all debugging calls to Y.Log removed (RAW - no filename postfix); and&lt;br /&gt;
* the RAW file minified using [https://github.com/mishoo/UglifyJS UglifyJS] (&#039;-min&#039; filename postfix).&lt;br /&gt;
&lt;br /&gt;
The meta-data file includes information on the module name, and its dependencies on other modules.&lt;br /&gt;
&lt;br /&gt;
By separating out the actual JS code from its dependencies, we are able to take those dependencies and use them elsewhere in Moodle.&lt;br /&gt;
As an example, this dependency data can be used to inform the Loading system to reduce the number of HTTP requests with Combo Loading. This has a benefical effect upon performance.&lt;br /&gt;
&lt;br /&gt;
Another feature of Shifter is its ability to handle &#039;&#039;rollups&#039;&#039;. Rollups describe the inclusion of multiple YUI classes in the same module. This means that you can have different but related components all in the same module, but without having a single large and less-managable source file.&lt;br /&gt;
&lt;br /&gt;
=== Why do I need build my modules? ===&lt;br /&gt;
&lt;br /&gt;
Technically you don&#039;t, and you can continue to use the old data structure, but by doing so you can benefit from:&lt;br /&gt;
* reduced data sent over the wire due to minification - great for slower connections and mobile devices;&lt;br /&gt;
* reduced number of http queries - the separation of module meta-data means that it can be consumed and fed into the Loader allowing your module to use combo-loading;&lt;br /&gt;
* a slightly simplified JS syntax for your source files.&lt;br /&gt;
&lt;br /&gt;
== Installing Shifter ==&lt;br /&gt;
&lt;br /&gt;
Shifter is written in JavaScript and the [http://nodejs.org Node.JS] environment. You will need to install Node.JS first. This is usually fairly easy as there are pre-built binary packages for &#039;&#039;&#039;Windows and Mac OS&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After installing Node.JS you then use the Node NPM tool to install Shifter:&lt;br /&gt;
   npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
Note: At present, the supported version of Shifter for use in Moodle is version &#039;&#039;0.4.6&#039;&#039;. You are not &#039;&#039;required&#039;&#039; to run a specific version of Shifter, but this is the version that Moodle YUI modules will be built with and you may find it beneficial to do so too.&lt;br /&gt;
&lt;br /&gt;
For &#039;&#039;&#039;Ubuntu&#039;&#039;&#039; run:&lt;br /&gt;
&lt;br /&gt;
   sudo apt-get install npm&lt;br /&gt;
   sudo apt-get install python-software-properties python g++ make&lt;br /&gt;
   sudo apt-get update&lt;br /&gt;
   sudo apt-get install nodejs&lt;br /&gt;
   sudo ln -s /usr/bin/nodejs /usr/local/bin/node&lt;br /&gt;
   sudo npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
== How do I write a YUI module which uses Shifter? ==&lt;br /&gt;
&lt;br /&gt;
Easy - it&#039;s mostly just a case of building the right directory structure.&lt;br /&gt;
&lt;br /&gt;
Assuming that you are creating a new YUI module in your block named fruit, and that your new YUI module will be called fruitbowl, we know the following:&lt;br /&gt;
{|&lt;br /&gt;
|Plugin type&lt;br /&gt;
|block&lt;br /&gt;
|-&lt;br /&gt;
|Plugin name&lt;br /&gt;
|fruit&lt;br /&gt;
|-&lt;br /&gt;
|Plugin frankenstyle name&lt;br /&gt;
|block_fruit&lt;br /&gt;
|-&lt;br /&gt;
|YUI Module name&lt;br /&gt;
|moodle-block_fruit-fruitbowl&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This means that your Moodle plugin directory is:&lt;br /&gt;
  /blocks/fruit&lt;br /&gt;
&lt;br /&gt;
And your &#039;&#039;fruitbowl&#039;&#039; YUI module source will therefore be in:&lt;br /&gt;
  /blocks/fruit/yui/src/fruitbowl&lt;br /&gt;
&lt;br /&gt;
The overall module directory structure looks like this:&lt;br /&gt;
&amp;lt;code bash&amp;gt;&lt;br /&gt;
yui/&lt;br /&gt;
|-- src&lt;br /&gt;
    |-- fruitbowl&lt;br /&gt;
        |-- build.json&lt;br /&gt;
        |-- js&lt;br /&gt;
        |   |-- fruitbowl.js&lt;br /&gt;
        |-- meta&lt;br /&gt;
            |-- fruitbowl.json&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== build.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;build.json&#039;&#039; file defines the eventual modules which will be built, and the source files that you&#039;ve written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;name&amp;quot;: &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;,&lt;br /&gt;
  &amp;quot;builds&amp;quot;: {&lt;br /&gt;
    &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
      &amp;quot;jsfiles&amp;quot;: [&lt;br /&gt;
        &amp;quot;fruitbowl.js&amp;quot;&lt;br /&gt;
      ]&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== meta/MODNAME.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;meta/MODNAME.json&#039;&#039; file describes the list of dependencies for each module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
    &amp;quot;requires&amp;quot;: [&lt;br /&gt;
        &amp;quot;base&amp;quot;,&lt;br /&gt;
        &amp;quot;node&amp;quot;&lt;br /&gt;
    ]&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== js/MODNAME.js ===&lt;br /&gt;
&lt;br /&gt;
The files in the js directory are the contents of your YUI module. This should include setting it up in the &#039;&#039;&#039;&#039;&#039;M&#039;&#039;&#039;&#039;&#039; namespace. As an example of a very basic structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
M.block_fruit = M.block_fruit || {};&lt;br /&gt;
M.block_fruit.fruitbowl = {&lt;br /&gt;
  init: function() {&lt;br /&gt;
    Y.one(&#039;#example&#039;).set(&#039;innerHTML&#039;, &#039;Example content&#039;);&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Can I put two modules in one directory tree? ===&lt;br /&gt;
&lt;br /&gt;
If you are still using Moodle 2.5, this does not work due to bug MDL-40478 (the non-default files cannot be loaded).&lt;br /&gt;
&lt;br /&gt;
If you are using Moodle 2.6+ this is possible. It may sometimes be sensible if the modules are not independent, e.g. if one module will only ever be used by the other one rather than directly called from PHP, or another module.&lt;br /&gt;
&lt;br /&gt;
If the modules are independent or might well be used separately from each other, then it is probably best to use a separate directory tree for each.&lt;br /&gt;
&lt;br /&gt;
See examples in mod/scorm/yui/src and lib/yui/src/notification.&lt;br /&gt;
&lt;br /&gt;
=== How do I load one of these modules from PHP code? ===&lt;br /&gt;
&lt;br /&gt;
You need to run Shifter so that it generates the compiled code. See below.&lt;br /&gt;
&lt;br /&gt;
Once you&#039;ve done that, you can use PHP code like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;yui_module(&lt;br /&gt;
        &#039;moodle-block_fruit-fruitbowl&#039;, &#039;M.block_fruit.fruitbowl.init&#039;, array());&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(The last empty array contains parameters for the init function, if required.)&lt;br /&gt;
&lt;br /&gt;
== How do I use shifter? ==&lt;br /&gt;
&lt;br /&gt;
So after writing out your structure, you now need to build it into a module. After installing Shifter, you have several options.&lt;br /&gt;
&lt;br /&gt;
=== During development ===&lt;br /&gt;
&lt;br /&gt;
A really nice way of running shifter during development is to have it detect changes to your files as you save them:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --watch&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;--watch&#039;&#039;&#039; option will watch for any changes to JS, JSON, CSS, or images within any module in the current directory and build that module when it detects changes.&lt;br /&gt;
&#039;&#039;Note: There can be a short delay of a few seconds while the changes are discovered and the file is built&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This is great for development because you don&#039;t have to continously run shifter manually.&lt;br /&gt;
&lt;br /&gt;
=== All YUI modules for the Moodle plugin you&#039;re currently working on ===&lt;br /&gt;
&lt;br /&gt;
If you are working on several YUI modules at once, then you can have shifter build all of them at the same time:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --walk&lt;br /&gt;
&lt;br /&gt;
=== Just the module you&#039;re working on now ===&lt;br /&gt;
If you want to build just the module you&#039;re working on at the moment:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/src&lt;br /&gt;
  shifter --walk fruitbowl&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/src/fruitbowl&lt;br /&gt;
  shifter&lt;br /&gt;
&lt;br /&gt;
=== Across the whole of Moodle ===&lt;br /&gt;
If you want to build every YUI module in Moodle at once:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle&lt;br /&gt;
  shifter --walk --recursive&lt;br /&gt;
&lt;br /&gt;
== Submitting my JavaScript back to Moodle core ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re working on some JavaScript for Moodle core, either in the form of a bug fix or a new feature, then you probably want to know what to do with your built module.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;We ask that you build your module and submit both the changes to tbe built module and the source directory.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In addition, the Continuous Integration server (Jenkins) will run &#039;&#039;shifter --walk --recursive&#039;&#039; on every run to ensure that all built files are up-to-date and correct.&lt;br /&gt;
&lt;br /&gt;
If there are several commits to the same YUI module within a single period of Moodle integration, then the built files will conflict. In this case the integration team will manually build the module and resolve the conflict. If you are aware that there may be multiple changes to the same file, please indicate this when submitting your change for Integration Review. If you are familiar with the changes, you may wish to consider building the issues upon one another too.&lt;br /&gt;
&lt;br /&gt;
== How do I convert my existing module? ==&lt;br /&gt;
&lt;br /&gt;
If you already have an existing moodle YUI module and want to convert it to the new Shifter format, then it&#039;s pretty easy on the whole:&lt;br /&gt;
&lt;br /&gt;
# create a new src/MODNAME directory;&lt;br /&gt;
# create js and meta directories within this;&lt;br /&gt;
# copy over your existing js file into the js directory;&lt;br /&gt;
# extract the meta-data;&lt;br /&gt;
# write your build.json;&lt;br /&gt;
# remove the YUI.add() wrapper from the top and bottom of your js file; and&lt;br /&gt;
# remove the old file.&lt;br /&gt;
&lt;br /&gt;
To create the complete directory structure and copy your existing code into the right directory (steps 1 - 3):&lt;br /&gt;
&lt;br /&gt;
  mkdir -p src/MODNAME/{js,meta}&lt;br /&gt;
  cp MODNAME/MODNAME.js src/MODNAME/js/&lt;br /&gt;
&lt;br /&gt;
Refer to the section above on the correct contents of the build.json and meta/MODNAME.json files.&lt;br /&gt;
&lt;br /&gt;
== Can I run my shifted module in older versions of Moodle 2 (before 2.5)? ==&lt;br /&gt;
&lt;br /&gt;
We are aware that many developers wish to reduce the overhead of maintaining a plugin for multiple versions of Moodle and that using Shifter will make this much harder. Unfortunately you can&#039;t use the shifted version of your module directly, but you can run them in parallel by copying the built file into the old directory structure.&lt;br /&gt;
&lt;br /&gt;
Moodle will always &#039;&#039;prefer&#039;&#039; the built shifted module over the older style module if both are present.&lt;br /&gt;
&lt;br /&gt;
If doing this, we would recommend copying over the minified version of your file so that older versions of moodle benefit from your user of shifted code.&lt;br /&gt;
&lt;br /&gt;
On a Linux-like operating system, you can use the following (again with our fruit example):&lt;br /&gt;
  cp yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl-min.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
Whilst developing, you may want to use a symlink so that you don&#039;t have to keep copying your changes after building them:&lt;br /&gt;
  ln -s yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[JavaScript guidelines]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44707</id>
		<title>YUI/Shifter</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=YUI/Shifter&amp;diff=44707"/>
		<updated>2014-05-08T14:16:32Z</updated>

		<summary type="html">&lt;p&gt;Maherne: /* During development */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.5}}&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
{{Moodle 2.7}}&lt;br /&gt;
&lt;br /&gt;
== What is Shifter and what does it do? ==&lt;br /&gt;
&lt;br /&gt;
[http://yui.github.com/shifter Shifter] is a tool written by the YUI team to help &#039;&#039;build&#039;&#039; YUI modules.&lt;br /&gt;
It has been supported since Moodle 2.5 and we encourage its use.&lt;br /&gt;
&lt;br /&gt;
It is written in JavaScript itself, and written as a [http://nodejs.org Node.JS] module which can be installed from [https://npmjs.org/package/shifter NPM].&lt;br /&gt;
&lt;br /&gt;
Shifter takes your source JavaScript file(s), along with some meta-data, and wraps it all together into a built file. This file is known in YUI parlance as a &#039;&#039;&#039;module&#039;&#039;&#039;.&lt;br /&gt;
After building this file, Shifter then produces three versions:&lt;br /&gt;
* the file in its original form (DEBUG - &#039;-debug&#039; filename postfix);&lt;br /&gt;
* the DEBUG file with all debugging calls to Y.Log removed (RAW - no filename postfix); and&lt;br /&gt;
* the RAW file minified using [https://github.com/mishoo/UglifyJS UglifyJS] (&#039;-min&#039; filename postfix).&lt;br /&gt;
&lt;br /&gt;
The meta-data file includes information on the module name, and its dependencies on other modules.&lt;br /&gt;
&lt;br /&gt;
By separating out the actual JS code from its dependencies, we are able to take those dependencies and use them elsewhere in Moodle.&lt;br /&gt;
As an example, this dependency data can be used to inform the Loading system to reduce the number of HTTP requests with Combo Loading. This has a benefical effect upon performance.&lt;br /&gt;
&lt;br /&gt;
Another feature of Shifter is its ability to handle &#039;&#039;rollups&#039;&#039;. Rollups describe the inclusion of multiple YUI classes in the same module. This means that you can have different but related components all in the same module, but without having a single large and less-managable source file.&lt;br /&gt;
&lt;br /&gt;
=== Why do I need build my modules? ===&lt;br /&gt;
&lt;br /&gt;
Technically you don&#039;t, and you can continue to use the old data structure, but by doing so you can benefit from:&lt;br /&gt;
* reduced data sent over the wire due to minification - great for slower connections and mobile devices;&lt;br /&gt;
* reduced number of http queries - the separation of module meta-data means that it can be consumed and fed into the Loader allowing your module to use combo-loading;&lt;br /&gt;
* a slightly simplified JS syntax for your source files.&lt;br /&gt;
&lt;br /&gt;
== Installing Shifter ==&lt;br /&gt;
&lt;br /&gt;
Shifter is written in JavaScript and the [http://nodejs.org Node.JS] environment. You will need to install Node.JS first. This is usually fairly easy as there are pre-built binary packages for &#039;&#039;&#039;Windows and Mac OS&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
After installing Node.JS you then use the Node NPM tool to install Shifter:&lt;br /&gt;
   npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
Note: At present, the supported version of Shifter for use in Moodle is version &#039;&#039;0.4.6&#039;&#039;. You are not &#039;&#039;required&#039;&#039; to run a specific version of Shifter, but this is the version that Moodle YUI modules will be built with and you may find it beneficial to do so too.&lt;br /&gt;
&lt;br /&gt;
For &#039;&#039;&#039;Ubuntu&#039;&#039;&#039; run:&lt;br /&gt;
&lt;br /&gt;
   sudo apt-get install npm&lt;br /&gt;
   sudo apt-get install python-software-properties python g++ make&lt;br /&gt;
   sudo apt-get update&lt;br /&gt;
   sudo apt-get install nodejs&lt;br /&gt;
   sudo ln -s /usr/bin/nodejs /usr/local/bin/node&lt;br /&gt;
   sudo npm install shifter@0.4.6 -g&lt;br /&gt;
&lt;br /&gt;
== How do I write a YUI module which uses Shifter? ==&lt;br /&gt;
&lt;br /&gt;
Easy - it&#039;s mostly just a case of building the right directory structure.&lt;br /&gt;
&lt;br /&gt;
Assuming that you are creating a new YUI module in your block named fruit, and that your new YUI module will be called fruitbowl, we know the following:&lt;br /&gt;
{|&lt;br /&gt;
|Plugin type&lt;br /&gt;
|block&lt;br /&gt;
|-&lt;br /&gt;
|Plugin name&lt;br /&gt;
|fruit&lt;br /&gt;
|-&lt;br /&gt;
|Plugin frankenstyle name&lt;br /&gt;
|block_fruit&lt;br /&gt;
|-&lt;br /&gt;
|YUI Module name&lt;br /&gt;
|moodle-block_fruit-fruitbowl&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This means that your Moodle plugin directory is:&lt;br /&gt;
  /blocks/fruit&lt;br /&gt;
&lt;br /&gt;
And your &#039;&#039;fruitbowl&#039;&#039; YUI module source will therefore be in:&lt;br /&gt;
  /blocks/fruit/yui/src/fruitbowl&lt;br /&gt;
&lt;br /&gt;
The overall module directory structure looks like this:&lt;br /&gt;
&amp;lt;code bash&amp;gt;&lt;br /&gt;
yui/&lt;br /&gt;
|-- src&lt;br /&gt;
    |-- fruitbowl&lt;br /&gt;
        |-- build.json&lt;br /&gt;
        |-- js&lt;br /&gt;
        |   |-- fruitbowl.js&lt;br /&gt;
        |-- meta&lt;br /&gt;
            |-- fruitbowl.json&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== build.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;build.json&#039;&#039; file defines the eventual modules which will be built, and the source files that you&#039;ve written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;name&amp;quot;: &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;,&lt;br /&gt;
  &amp;quot;builds&amp;quot;: {&lt;br /&gt;
    &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
      &amp;quot;jsfiles&amp;quot;: [&lt;br /&gt;
        &amp;quot;fruitbowl.js&amp;quot;&lt;br /&gt;
      ]&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== meta/MODNAME.json ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;meta/MODNAME.json&#039;&#039; file describes the list of dependencies for each module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;moodle-block_fruit-fruitbowl&amp;quot;: {&lt;br /&gt;
    &amp;quot;requires&amp;quot;: [&lt;br /&gt;
        &amp;quot;base&amp;quot;,&lt;br /&gt;
        &amp;quot;node&amp;quot;&lt;br /&gt;
    ]&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== js/MODNAME.js ===&lt;br /&gt;
&lt;br /&gt;
The files in the js directory are the contents of your YUI module. This should include setting it up in the &#039;&#039;&#039;&#039;&#039;M&#039;&#039;&#039;&#039;&#039; namespace. As an example of a very basic structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
M.block_fruit = M.block_fruit || {};&lt;br /&gt;
M.block_fruit.fruitbowl = {&lt;br /&gt;
  init: function() {&lt;br /&gt;
    Y.one(&#039;#example&#039;).set(&#039;innerHTML&#039;, &#039;Example content&#039;);&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Can I put two modules in one directory tree? ===&lt;br /&gt;
&lt;br /&gt;
If you are still using Moodle 2.5, this does not work due to bug MDL-40478 (the non-default files cannot be loaded).&lt;br /&gt;
&lt;br /&gt;
If you are using Moodle 2.6+ this is possible. It may sometimes be sensible if the modules are not independent, e.g. if one module will only ever be used by the other one rather than directly called from PHP, or another module.&lt;br /&gt;
&lt;br /&gt;
If the modules are independent or might well be used separately from each other, then it is probably best to use a separate directory tree for each.&lt;br /&gt;
&lt;br /&gt;
See examples in mod/scorm/yui/src and lib/yui/src/notification.&lt;br /&gt;
&lt;br /&gt;
=== How do I load one of these modules from PHP code? ===&lt;br /&gt;
&lt;br /&gt;
You need to run Shifter so that it generates the compiled code. See below.&lt;br /&gt;
&lt;br /&gt;
Once you&#039;ve done that, you can use PHP code like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;yui_module(&lt;br /&gt;
        &#039;moodle-block_fruit-fruitbowl&#039;, &#039;M.block_fruit.fruitbowl.init&#039;, array());&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(The last empty array contains parameters for the init function, if required.)&lt;br /&gt;
&lt;br /&gt;
== How do I use shifter? ==&lt;br /&gt;
&lt;br /&gt;
So after writing out your structure, you now need to build it into a module. After installing Shifter, you have several options.&lt;br /&gt;
&lt;br /&gt;
=== During development ===&lt;br /&gt;
&lt;br /&gt;
A really nice way of running shifter during development is to have it detect changes to your files as you save them:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/yui/src&lt;br /&gt;
  shifter --watch&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;--watch&#039;&#039;&#039; option will watch for any changes to JS, JSON, CSS, or images within any module in the current directory and build that module when it detects changes.&lt;br /&gt;
&#039;&#039;Note: There can be a short delay of a few seconds while the changes are discovered and the file is built&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This is great for development because you don&#039;t have to continously run shifter manually.&lt;br /&gt;
&lt;br /&gt;
=== All YUI modules for the Moodle plugin you&#039;re currently working on ===&lt;br /&gt;
&lt;br /&gt;
If you are working on several YUI modules at once, then you can have shifter build all of them at the same time:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/src&lt;br /&gt;
  shifter --walk&lt;br /&gt;
&lt;br /&gt;
=== Just the module you&#039;re working on now ===&lt;br /&gt;
If you want to build just the module you&#039;re working on at the moment:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/src&lt;br /&gt;
  shifter --walk fruitbowl&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle/blocks/fruit/src/fruitbowl&lt;br /&gt;
  shifter&lt;br /&gt;
&lt;br /&gt;
=== Across the whole of Moodle ===&lt;br /&gt;
If you want to build every YUI module in Moodle at once:&lt;br /&gt;
&lt;br /&gt;
  cd ~/git/moodle&lt;br /&gt;
  shifter --walk --recursive&lt;br /&gt;
&lt;br /&gt;
== Submitting my JavaScript back to Moodle core ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re working on some JavaScript for Moodle core, either in the form of a bug fix or a new feature, then you probably want to know what to do with your built module.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;We ask that you build your module and submit both the changes to tbe built module and the source directory.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In addition, the Continuous Integration server (Jenkins) will run &#039;&#039;shifter --walk --recursive&#039;&#039; on every run to ensure that all built files are up-to-date and correct.&lt;br /&gt;
&lt;br /&gt;
If there are several commits to the same YUI module within a single period of Moodle integration, then the built files will conflict. In this case the integration team will manually build the module and resolve the conflict. If you are aware that there may be multiple changes to the same file, please indicate this when submitting your change for Integration Review. If you are familiar with the changes, you may wish to consider building the issues upon one another too.&lt;br /&gt;
&lt;br /&gt;
== How do I convert my existing module? ==&lt;br /&gt;
&lt;br /&gt;
If you already have an existing moodle YUI module and want to convert it to the new Shifter format, then it&#039;s pretty easy on the whole:&lt;br /&gt;
&lt;br /&gt;
# create a new src/MODNAME directory;&lt;br /&gt;
# create js and meta directories within this;&lt;br /&gt;
# copy over your existing js file into the js directory;&lt;br /&gt;
# extract the meta-data;&lt;br /&gt;
# write your build.json;&lt;br /&gt;
# remove the YUI.add() wrapper from the top and bottom of your js file; and&lt;br /&gt;
# remove the old file.&lt;br /&gt;
&lt;br /&gt;
To create the complete directory structure and copy your existing code into the right directory (steps 1 - 3):&lt;br /&gt;
&lt;br /&gt;
  mkdir -p src/MODNAME/{js,meta}&lt;br /&gt;
  cp MODNAME/MODNAME.js src/MODNAME/js/&lt;br /&gt;
&lt;br /&gt;
Refer to the section above on the correct contents of the build.json and meta/MODNAME.json files.&lt;br /&gt;
&lt;br /&gt;
== Can I run my shifted module in older versions of Moodle 2 (before 2.5)? ==&lt;br /&gt;
&lt;br /&gt;
We are aware that many developers wish to reduce the overhead of maintaining a plugin for multiple versions of Moodle and that using Shifter will make this much harder. Unfortunately you can&#039;t use the shifted version of your module directly, but you can run them in parallel by copying the built file into the old directory structure.&lt;br /&gt;
&lt;br /&gt;
Moodle will always &#039;&#039;prefer&#039;&#039; the built shifted module over the older style module if both are present.&lt;br /&gt;
&lt;br /&gt;
If doing this, we would recommend copying over the minified version of your file so that older versions of moodle benefit from your user of shifted code.&lt;br /&gt;
&lt;br /&gt;
On a Linux-like operating system, you can use the following (again with our fruit example):&lt;br /&gt;
  cp yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl-min.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
Whilst developing, you may want to use a symlink so that you don&#039;t have to keep copying your changes after building them:&lt;br /&gt;
  ln -s yui/build/moodle-mod_fruit-fruitbowl/moodle-mod_fruit-fruitbowl.js yui/fruitbowl/fruitbowl.js&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[JavaScript guidelines]]&lt;/div&gt;</summary>
		<author><name>Maherne</name></author>
	</entry>
</feed>