<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Agwells</id>
	<title>MoodleDocs - Contribucions de l&amp;#039;usuari [ca]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Agwells"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/Especial:Contribucions/Agwells"/>
	<updated>2026-04-18T11:28:57Z</updated>
	<subtitle>Contribucions de l&amp;#039;usuari</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Administration_via_command_line&amp;diff=83346</id>
		<title>Administration via command line</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Administration_via_command_line&amp;diff=83346"/>
		<updated>2011-05-09T07:54:30Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Upgrading via command line */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. All command line tools are located in &amp;lt;code&amp;gt;admin/cli/*&amp;lt;/code&amp;gt; directory. To avoid problems with access control, you should run them as the owner of the web server process. It is especially important for CLI installation and upgrade as they create new files in moodledata directory and the web server has to have write access to them. In Linux distributions, the user that runs the web server is usually apache or wwrun or httpd or something similar. As a root, you will probably want to execute Moodle CLI scripts like this:&lt;br /&gt;
&lt;br /&gt;
    $ cd /path/to/your/moodle/dir&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/somescript.php --params&lt;br /&gt;
&lt;br /&gt;
Most of the scripts accept common --help (or -h) parameter to display the full usage information, for example:&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/install.php --help&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Installation via command line ==&lt;br /&gt;
&lt;br /&gt;
Since version 2.0, Moodle can be installed from the command line. There are two modes of installation. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs&lt;br /&gt;
&lt;br /&gt;
== Maintenance mode ==&lt;br /&gt;
&lt;br /&gt;
To switch your site into the maintenance mode via CLI, you can use&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable&lt;br /&gt;
&lt;br /&gt;
To turn the maintenance mode off, just execute the same script with --disable parameter.&lt;br /&gt;
&lt;br /&gt;
== Offline mode ==&lt;br /&gt;
&lt;br /&gt;
In some situations, you may want to switch your Moodle site into offline mode so that it is not accessible via the web but you can not stop the web server completely (typically because there are other web pages and applications running there). If a file called &amp;lt;code&amp;gt;climaintenance.html&amp;lt;/code&amp;gt; exists in the root folder of moodledata directory, Moodle will automatically display the contents of that file instead of any other page.&lt;br /&gt;
&lt;br /&gt;
    $ cd /var/www/sites/moodle/moodledata/&lt;br /&gt;
    $ echo &#039;&amp;amp;lt;h1&amp;amp;gt;Sorry, maintenance in progress&amp;amp;lt;/h1&amp;amp;gt;&#039; &amp;amp;gt; climaintenance.html&lt;br /&gt;
&lt;br /&gt;
You can prepare a nice formatted HTML page to inform your users about the server being down and keep in the moodledata directory under a name like &amp;lt;code&amp;gt;climaintenance.off&amp;lt;/code&amp;gt; and rename it to the &amp;lt;code&amp;gt;climaintenance.html&amp;lt;/code&amp;gt; if needed.&lt;br /&gt;
&lt;br /&gt;
== Upgrading via command line ==&lt;br /&gt;
&lt;br /&gt;
Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/upgrade.php --non-interactive&lt;br /&gt;
&lt;br /&gt;
Upgrading via command line is a very comfortable way of Moodle upgrade if you use [[CVS]] or [[Git|git]] checkout of the Moodle source code. See the following procedure how to upgrade your site within several seconds to the most recent version while preserving your eventual local customizations tracked in git repository:&lt;br /&gt;
&lt;br /&gt;
    $ cd /var/www/sites/moodle/htdocs/&lt;br /&gt;
    $ git fetch&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable&lt;br /&gt;
    $ git merge origin/cvshead&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/upgrade.php&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable&lt;br /&gt;
&lt;br /&gt;
===Issues with Upgrading via Command Line===&lt;br /&gt;
&lt;br /&gt;
if your config.php contains information about several moodle instances (distinct moodle websites and databases sharing the same codebase), then this script will silently fail&lt;br /&gt;
&lt;br /&gt;
The solution is to temporarily eliminate from config.php all but the one instance you want to upgrade &lt;br /&gt;
&lt;br /&gt;
If this is a problem for the other instances (production sites), then modify the cli script to point to a copy of config.php  (which will be the one edited to contain only one moodle instance at a time)&lt;br /&gt;
&lt;br /&gt;
== Custom site defaults ==&lt;br /&gt;
&lt;br /&gt;
During the install and upgrade via CLI, Moodle sets the administration variables to the default values. You can use different defaults. See MDL-17850 for details. Shortly, all you need to do is to add a file &amp;lt;code&amp;gt;local/defaults.php&amp;lt;/code&amp;gt; into your Moodle installation. The format of the file is like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$defaults[&#039;pluginname&#039;][&#039;settingname&#039;] = &#039;settingvalue&#039;; // for plugins&lt;br /&gt;
$defaults[&#039;moodle&#039;][&#039;settingname&#039;] = &#039;settingvalue&#039;;     // for core settings&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These defaults are used during install, upgrade and are also displayed as defaults at the Site administration pages.&lt;br /&gt;
&lt;br /&gt;
== Reset user password ==&lt;br /&gt;
&lt;br /&gt;
If you happen to forget your admin password (or you want to set a password for any other user of your Moodle system), you can use reset_password.php script. The script sets the correctly salted password for the given user.&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/reset_password.php&lt;br /&gt;
&lt;br /&gt;
== MySQL storage engine conversion ==&lt;br /&gt;
&lt;br /&gt;
If you run your Moodle site with MySQL database backend and use the default MyISAM as the storage engine for your tables, you may want to convert them to use some more reliable engine like InnoDB (actually, you should want to switch to PostgreSQL ;-) anyway).&lt;br /&gt;
&lt;br /&gt;
    $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Running cron via command line ==&lt;br /&gt;
&lt;br /&gt;
In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.&lt;br /&gt;
&lt;br /&gt;
[[Category:Administrator]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Administration en ligne de commande]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=lib/formslib.php_Form_Definition&amp;diff=80102</id>
		<title>lib/formslib.php Form Definition</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=lib/formslib.php_Form_Definition&amp;diff=80102"/>
		<updated>2011-01-06T22:01:01Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* tags */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Formslib}}&lt;br /&gt;
== &#039;&#039;definition()&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The definition of the elements to be included in the form, their &#039;types&#039; (PARAM_*), helpbuttons included, etc. is all included in a function you must define in your class.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;definition()&#039;&#039; is used to define the elements in the form and &#039;&#039;&#039;this definition will be used for validating data submitted as well as for printing the form.&#039;&#039;&#039; For select and checkbox type elements only data that could have been selected will be allowed. And only data that corresponds to a form element in the definition will be accepted as submitted data.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;definition()&#039;&#039; should include all elements that are going to be used on form, some elements may be removed or tweaked later in &#039;&#039;definition_after_data()&#039;&#039;. Please do not create conditional elements in &#039;&#039;definition()&#039;&#039;, the definition() should not directly depend on the submitted data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&amp;quot;$CFG-&amp;gt;libdir/formslib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
class simplehtml_form extends moodleform {&lt;br /&gt;
&lt;br /&gt;
    function definition() {&lt;br /&gt;
        global $CFG;&lt;br /&gt;
       &lt;br /&gt;
        $mform =&amp;amp; $this-&amp;gt;_form; // Don&#039;t forget the underscore! &lt;br /&gt;
&lt;br /&gt;
        $mform-&amp;gt;addElement()... // Add elements to your form&lt;br /&gt;
            ...&lt;br /&gt;
    }                           // Close the function&lt;br /&gt;
}                               // Close the class&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use Fieldsets to group Form Elements==&lt;br /&gt;
&lt;br /&gt;
You use code like this to open a fieldset with a &#039;&#039;legend&#039;&#039;. &amp;lt;br /&amp;gt;&lt;br /&gt;
(&#039;&#039;&#039;Note&#039;&#039;&#039;: Some themes turn off legends on admin setting pages by using CSS: &amp;lt;nowiki&amp;gt;#adminsettings legend {display:none;}&amp;lt;/nowiki&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;header&#039;, &#039;nameforyourheaderelement&#039;, get_string(&#039;titleforlegened&#039;, &#039;modulename&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can&#039;t yet nest these visible fieldsets unfortunately. But in fact groups of elements are wrapped in invisible fieldsets.&lt;br /&gt;
&lt;br /&gt;
You close a fieldset with moodle_form&#039;s closeHeaderBefore method. You tell closeHeaderBefore the element before you wish to end the fieldset. A fieldset is automatically closed if you open a new one. You need to use this code only if you want to close a fieldset and the subsequent form elements are not to be enclosed by a visible fieldset (they are still enclosed with an invisibe one with no legend) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;closeHeaderBefore(&#039;buttonar&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==addElement==&lt;br /&gt;
&lt;br /&gt;
Use the addElement method to add an element to a form. The first few arguments are always the same. The first param is the type of the element to add. The second is the elementname to use which is normally the html name of the element in the form. The third is often the text for the label for the element.&lt;br /&gt;
&lt;br /&gt;
Some examples are below :&lt;br /&gt;
=== button ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;button&#039;, &#039;intro&#039;, get_string(&amp;quot;buttonlabel&amp;quot;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A button element. If you want a submit or cancel button see &#039;submit&#039; element.&lt;br /&gt;
&lt;br /&gt;
=== checkbox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;checkbox&#039;, &#039;ratingtime&#039;, get_string(&#039;ratingtime&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a simple checkbox. The third parameter for this element is the label to display on the left side of the form. You can also supply a string as a fourth parameter to specify a label that will appear on the right of the element. Checkboxes and radio buttons can be grouped and have individual labels on their right.&lt;br /&gt;
&lt;br /&gt;
You can have a 5th parameter $attributes, as on other elements.&lt;br /&gt;
&lt;br /&gt;
==== advcheckbox ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;advcheckbox&#039;, &#039;ratingtime&#039;, get_string(&#039;ratingtime&#039;, &#039;forum&#039;), &#039;Label displayed after checkbox&#039;, array(&#039;group&#039; =&amp;gt; 1), array(0, 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to the checkbox above, but with a couple of important improvements:&lt;br /&gt;
&lt;br /&gt;
# The 5th parameter is a normal $attributes array, normally used to set HTML attributes for the &amp;lt;input&amp;gt; element. However, a special value of &#039;group&#039; can be given, which will add a class name to the element, and enable its grouping for a [[Development:lib/formslib.php_add_checkbox_controller|checkbox controller]]&lt;br /&gt;
#The 6th parameter is an array of values that will be associated with the checked/unchecked state of the checkbox. With a normal checkbox you cannot choose that value, and in fact an unchecked checkbox will not even be sent with the form data.&lt;br /&gt;
&lt;br /&gt;
=== choosecoursefile ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;choosecoursefile&#039;, &#039;mediafile&#039;, get_string(&#039;mediafile&#039;, &#039;lesson&#039;), array(&#039;courseid&#039;=&amp;gt;$COURSE-&amp;gt;id));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose a file from the course files area. The fourth option is a list of options for the element. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&#039;courseid&#039; =&amp;gt;null,  //if it is null (default then use global $COURSE&lt;br /&gt;
      &#039;height&#039;   =&amp;gt;500,   // height of the popup window&lt;br /&gt;
      &#039;width&#039;    =&amp;gt;750,   // width of the popup window&lt;br /&gt;
      &#039;options&#039;  =&amp;gt;&#039;none&#039;); //options string for the pop up window &lt;br /&gt;
                          //eg. &#039;menubar=0,location=0,scrollbars,resizable&#039;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass an optional 5th parameter of attributes, as for other elements. The most useful way of using that is something like &lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;maxlength&#039; =&amp;gt; 255, &#039;size&#039; =&amp;gt; 48)&amp;lt;/code&amp;gt;&lt;br /&gt;
to control the maxlength / size of the text box (note size will default to 48 if not specified)&lt;br /&gt;
&lt;br /&gt;
Finally, as this element is a group containing two elements (button + value), you can add validation rules by using the &#039;&#039;&#039;addGroupRule()&#039;&#039;&#039; method in this (complex) way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$mform-&amp;gt;addGroupRule(&#039;elementname&#039;, array(&#039;value&#039; =&amp;gt; array(array(list, of, rule, params, but, fieldname))));&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where: &#039;&#039;&#039;&amp;quot;elementname&amp;quot;&#039;&#039;&#039; is the name of the choosecoursefile group element, &#039;&#039;&#039;&amp;quot;value&amp;quot;&#039;&#039;&#039; is the name of the text field within the group and the &#039;&#039;&#039;&amp;quot;list, of, addrule, params, but, fieldname&amp;quot;&#039;&#039;&#039; is exactly that, the list of fields in the normal addRule() function but ommiting the first one, the fieldname.&lt;br /&gt;
&lt;br /&gt;
For example, the [http://cvs.moodle.org/moodle/mod/resource/type/file/resource.class.php?view=markup file/url resource type], uses one &amp;quot;choosecoursefile&amp;quot; element, and it controls the maximum length of the field (255) with this use of addGroupRule():&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$mform-&amp;gt;addGroupRule(&#039;reference&#039;, array(&#039;value&#039; =&amp;gt; array(array(get_string(&#039;maximumchars&#039;, &#039;&#039;, 255), &#039;maxlength&#039;, 255, &#039;client&#039;))));&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== date_selector ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;date_selector&#039;, &#039;assesstimefinish&#039;, get_string(&#039;to&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a date selector. You can select a Day, Month and Year using a group of select boxes. The fourth param here is an array of options. The defaults for the options are :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&lt;br /&gt;
    &#039;startyear&#039; =&amp;gt; 1970, &lt;br /&gt;
    &#039;stopyear&#039;  =&amp;gt; 2020,&lt;br /&gt;
    &#039;timezone&#039;  =&amp;gt; 99, &lt;br /&gt;
    &#039;applydst&#039;  =&amp;gt; true, &lt;br /&gt;
    &#039;optional&#039;  =&amp;gt; false&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can override these defaults by supplying an array as fourth param with one or more keys with a value to override the default. You can supply a fifth param of attributes here as well.&lt;br /&gt;
&lt;br /&gt;
=== date_time_selector ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;date_time_selector&#039;, &#039;assesstimestart&#039;, get_string(&#039;from&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a group of select boxes to select a date (Day Month and Year) and time (Hour and Minute). When submitted, submitted data is processed and a timestamp is passed to $form-&amp;gt;get_data(); the fourth param here is an array of options. The defaults for the options are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&lt;br /&gt;
    &#039;startyear&#039; =&amp;gt; 1970, &lt;br /&gt;
    &#039;stopyear&#039;  =&amp;gt; 2020,&lt;br /&gt;
    &#039;timezone&#039;  =&amp;gt; 99, &lt;br /&gt;
    &#039;applydst&#039;  =&amp;gt; true, &lt;br /&gt;
    &#039;step&#039;      =&amp;gt; 5&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can override these defaults by supplying an array as fourth param with one or more keys with a value to override the default. You can supply a fifth param of attributes here as well.&lt;br /&gt;
&lt;br /&gt;
===duration===&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;duration&#039;, &#039;timelimit&#039;, get_string(&#039;timelimit&#039;, &#039;quiz&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This field type lets the user input an interval of time. It comprises a text field, where you can type a number, and a dropdown for selecting a unit (days, hours, minutes or seconds). When submitted the value is converted to a number of seconds.&lt;br /&gt;
&lt;br /&gt;
You can add a fourth parameter to give options. At the moment the only option supported is here is an array of options. The defaults for the options is:&lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;optional&#039; =&amp;gt; true)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass an optional 5th parameter of attributes, as for other elements. The most useful way of using that is something like &lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;size&#039; =&amp;gt; 5)&amp;lt;/code&amp;gt;&lt;br /&gt;
to control the size of the text box.&lt;br /&gt;
&lt;br /&gt;
=== file ===&lt;br /&gt;
&lt;br /&gt;
File upload input box with browse button. In the form definition type&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;file&#039;, &#039;attachment&#039;, get_string(&#039;attachment&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after form submission and validation use&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;
      ...&lt;br /&gt;
    $mform-&amp;gt;save_files($destination_directory);&lt;br /&gt;
      ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need advanced settings such as required file, different max upload size or name of uploaded file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$this-&amp;gt;set_upload_manager(new upload_manager(&#039;attachment&#039;, true, false, $COURSE, false, 0, 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;addRule(&#039;attachment&#039;, null, &#039;required&#039;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&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;
      ...&lt;br /&gt;
    $mform-&amp;gt;save_files($destination_directory);&lt;br /&gt;
    $newfilename = $mform-&amp;gt;get_new_filename();&lt;br /&gt;
      ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When porting old code it is also possible to use the upload manager directly for processing of uploaded files.&lt;br /&gt;
&lt;br /&gt;
Please note that if using set_upload_manager() it must be before addElement(&#039;file&#039;,..).&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
File uploading was rewritten in 2.0. Please see inline docs for now. This page will be updated when the new API stabilises.&lt;br /&gt;
&lt;br /&gt;
===filepicker===&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
General replacement of &#039;&#039;file&#039;&#039; 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, 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;
See also [[Development:Using the File API in Moodle forms]]&lt;br /&gt;
&lt;br /&gt;
=== hidden ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;hidden&#039;, &#039;reply&#039;, &#039;yes&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A hidden element. Set the element name (in this case &#039;&#039;&#039;reply&#039;&#039;&#039;) to the stated value (in this case &#039;&#039;&#039;yes&#039;&#039;&#039;).&lt;br /&gt;
&lt;br /&gt;
=== html ===&lt;br /&gt;
You can add arbitrary HTML to your Moodle form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;html&#039;, &#039;&amp;lt;div class=&amp;quot;qheader&amp;quot;&amp;gt;&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://moodle.org/mod/forum/discuss.php?d=126935 &amp;quot;Question: Can I put a moodleform inside a table td?&amp;quot;] for a concrete example.&lt;br /&gt;
&lt;br /&gt;
=== htmleditor &amp;amp; format ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;htmleditor&#039;, &#039;text&#039;, get_string(&#039;choicetext&#039;, &#039;choice&#039;));&lt;br /&gt;
$mform-&amp;gt;setType(&#039;text&#039;, PARAM_RAW);&lt;br /&gt;
$mform-&amp;gt;addRule(&#039;text&#039;, null, &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;format&#039;, &#039;format&#039;, get_string(&#039;format&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can supply a fourth param to htmleditor of an array of options :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
array(&lt;br /&gt;
    &#039;canUseHtmlEditor&#039;=&amp;gt;&#039;detect&#039;,&lt;br /&gt;
    &#039;rows&#039;  =&amp;gt; 10, &lt;br /&gt;
    &#039;cols&#039;  =&amp;gt; 65, &lt;br /&gt;
    &#039;width&#039; =&amp;gt; 0,&lt;br /&gt;
    &#039;height&#039;=&amp;gt; 0, &lt;br /&gt;
    &#039;course&#039;=&amp;gt; 0,&lt;br /&gt;
);&lt;br /&gt;
//options same as print_textarea params&lt;br /&gt;
//use rows and cols options to control htmleditor size.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===modgrade===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;modgrade&#039;, &#039;scale&#039;, get_string(&#039;grade&#039;), false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a custom element for selecting a grade for any activity module. The fourth argument is whether to include an option for no grade which has a value 0. This select box does include scales. The default is true, include no grade option.&lt;br /&gt;
&lt;br /&gt;
A helpbutton is automatically added.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===password===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;password&#039;, &#039;password&#039;, get_string(&#039;label&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A password element. Fourth param is an array or string of attributes.&lt;br /&gt;
&lt;br /&gt;
===passwordunmask===&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;passwordunmask&#039;, &#039;password&#039;, get_string(&#039;label&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A password element with option to show the password in plaintext. Fourth param is an array or string of attributes.&lt;br /&gt;
&lt;br /&gt;
=== radio ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$radioarray=array();&lt;br /&gt;
$radioarray[] = &amp;amp;MoodleQuickForm::createElement(&#039;radio&#039;, &#039;yesno&#039;, &#039;&#039;, get_string(&#039;yes&#039;), 1, $attributes);&lt;br /&gt;
$radioarray[] = &amp;amp;MoodleQuickForm::createElement(&#039;radio&#039;, &#039;yesno&#039;, &#039;&#039;, get_string(&#039;no&#039;), 0, $attributes);&lt;br /&gt;
$mform-&amp;gt;addGroup($radioarray, &#039;radioar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second param names the radio button and should be the same for each button in the group in order to toggle correctly. Third param would be the label for the form element but is generally ignored as this element will always be in a group which has it&#039;s own label. Fourth param is a string, a label to be displayed on the right of the element. The fifth is the value for this radio button. $attributes can be a string or an array of attributes.&lt;br /&gt;
&lt;br /&gt;
It is possible to add help to individual radio buttons but this requires a custom template to be defined for the group elements. See MDL-15571.&lt;br /&gt;
&lt;br /&gt;
==== setDefault ====&lt;br /&gt;
&lt;br /&gt;
To set the default for a radio button group as above use the following :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;setDefault(&#039;yesno&#039;, 0);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the default &#039;no&#039;.&lt;br /&gt;
&lt;br /&gt;
===select===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;type&#039;, get_string(&#039;forumtype&#039;, &#039;forum&#039;), $FORUM_TYPES, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The fourth param for this element is an array of options for the select box. The keys are the values for the option and the value of the array is the text for the option. The fifth param $attributes is optional, see text element for description of attributes param.&lt;br /&gt;
&lt;br /&gt;
It is also possible to create a select with certain options disabled, using [http://stackoverflow.com/questions/2138089/how-can-i-use-quickform-to-add-disabled-select-options/2150275#2150275 this technique].&lt;br /&gt;
&lt;br /&gt;
====multi-select====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $select = &amp;amp;$mform-&amp;gt;addElement(&#039;select&#039;, &#039;colors&#039;, get_string(&#039;colors&#039;), array(&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;), $attributes);&lt;br /&gt;
        $select-&amp;gt;setMultiple(true);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===selectyesno===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;selectyesno&#039;, &#039;maxbytes&#039;, get_string(&#039;maxattachmentsize&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want a yes / no select box this one automatically translates itself and has value 1 for yes and 0 for no.&lt;br /&gt;
&lt;br /&gt;
===static===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;static&#039;, &#039;description&#039;, get_string(&#039;description&#039;, &#039;exercise&#039;),&lt;br /&gt;
                  get_string(&#039;descriptionofexercise&#039;, &#039;exercise&#039;, $COURSE-&amp;gt;students));&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a static element. It should be used with care it is used to display a static piece of text with a label. The third param is the label and the fourth is the static text itself.&lt;br /&gt;
&lt;br /&gt;
===submit, reset and cancel===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        //normally you use add_action_buttons instead of this code&lt;br /&gt;
        $buttonarray=array();&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;submit&#039;, &#039;submitbutton&#039;, get_string(&#039;savechanges&#039;));&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;reset&#039;, &#039;resetbutton&#039;, get_string(&#039;revert&#039;));&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;cancel&#039;);&lt;br /&gt;
        $mform-&amp;gt;addGroup($buttonarray, &#039;buttonar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
        $mform-&amp;gt;closeHeaderBefore(&#039;buttonar&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A &#039;Submit&#039; type element is a submit type form element which will submit the form. A &#039;Reset&#039; will not submit the form but will reset any changes the user has made to form contents. A &#039;Cancel&#039; element cancels form submission. You need to have a branch in your code before you check for get_data() to check if submission has been cancelled with is_cancelled(); See the example on the usage page.&lt;br /&gt;
&lt;br /&gt;
You should name your submit and reset buttons &#039;submitbutton&#039; and &#039;resetbutton&#039; or something similar (not &#039;submit&#039; and &#039;reset&#039;). This avoids problems in JavaScript of collisions between form element names and names of JavaScript methods of the form object.&lt;br /&gt;
&lt;br /&gt;
====add_action_buttons($cancel = true, $submitlabel=null);====&lt;br /&gt;
&lt;br /&gt;
You will normally use this helper function which is a method of moodleform to add all the &#039;action&#039; buttons to the end of your form. A boolean parameter allow you to specify whether to include a cancel button and specify the label for your submit button (pass the result of get_string). Default for the submit button label is get_string(&#039;savechanges&#039;).&lt;br /&gt;
&lt;br /&gt;
===text===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;text&#039;, &#039;name&#039;, get_string(&#039;forumname&#039;, &#039;forum&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For a simple text element. Your fourth parameter here can be a string or array of attributes for the text element. The following are equivalent :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $attributes=&#039;size=&amp;quot;20&amp;quot;&#039;;&lt;br /&gt;
        $attributes=array(&#039;size&#039;=&amp;gt;&#039;20&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Generally you are encouraged to use CSS instead of using attributes for styling.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A format element can be used as a format select box. It will be non-selectable if you&#039;re using an html editor.&lt;br /&gt;
&lt;br /&gt;
The third param for this element is $useHtmlEditor and it defaults to null in which case an html editor is used if the browser and user profile support it.&lt;br /&gt;
&lt;br /&gt;
===textarea===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;textarea&#039;, &#039;introduction&#039;, get_string(&amp;quot;introtext&amp;quot;, &amp;quot;survey&amp;quot;), &#039;wrap=&amp;quot;virtual&amp;quot; rows=&amp;quot;20&amp;quot; cols=&amp;quot;50&amp;quot;&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A textarea element. If you want an htmleditor use htmleditor element. Fourth element here is a string or array of attributes.&lt;br /&gt;
&lt;br /&gt;
===recaptcha===&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;recaptcha&#039;, &#039;recaptcha_field_name&#039;, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this recaptcha element to reduce the spam risk in your forms. Third element here is a string or array of attributes. Take care to get an API key from http://recaptcha.net/api/getkey before using this element.&lt;br /&gt;
&lt;br /&gt;
To check whether recaptcha is enabled at site level use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if (!empty($CFG-&amp;gt;recaptchapublickey) &amp;amp;&amp;amp; !empty($CFG-&amp;gt;recaptchaprivatekey)) {&lt;br /&gt;
    //recaptcha is enabled&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===tags===&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;tags&#039;, &#039;field_name&#039;, $lable, $options, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Used for editing a list of tags, for example on a blog post.&lt;br /&gt;
&lt;br /&gt;
There is only one option available, &#039;display&#039;, which should be set to one of the contstants MoodleQuickForm_tags::ONLYOFFICIAL, NOOFFICIAL or DEFAULTUI. This controls whether the official tags are listed for easy selection, or a text area where arbitrary tags may be typed, or both. The default is both.&lt;br /&gt;
&lt;br /&gt;
The value should be set/returned as an array of tags.&lt;br /&gt;
&lt;br /&gt;
==addGroup==&lt;br /&gt;
&lt;br /&gt;
A &#039;group&#039; in formslib is just a group of elements that will have a label and will be included on one line. &lt;br /&gt;
&lt;br /&gt;
For example typical code to include a submit and cancel button on the same line : &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $buttonarray=array();&lt;br /&gt;
        $buttonarray[] =&amp;amp; $mform-&amp;gt;createElement(&#039;submit&#039;, &#039;submitbutton&#039;, get_string(&#039;savechanges&#039;));&lt;br /&gt;
        $buttonarray[] =&amp;amp; $mform-&amp;gt;createElement(&#039;submit&#039;, &#039;cancel&#039;, get_string(&#039;cancel&#039;));&lt;br /&gt;
        $mform-&amp;gt;addGroup($buttonarray, &#039;buttonar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You use the same arguments for createElement as you do for addElement. Any label for the element in the third argument is normally ignored (but not in the case of the submit buttons above where the third argument is not for a label but is the text for the button).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a bad example (don&#039;t do this for real, use the &#039;optional&#039; =&amp;gt; true option of the date element): putting a date_selector (which is itself a group of elements) and a checkbox on the same line, note that you can disable every element in the group using the group name &#039;availablefromgroup&#039; but it doesn&#039;t disable the controlling element the &#039;availablefromenabled&#039; checkbox:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $availablefromgroup=array();&lt;br /&gt;
	$availablefromgroup[] =&amp;amp; $mform-&amp;gt;createElement(&#039;date_selector&#039;, &#039;availablefrom&#039;, &#039;&#039;);&lt;br /&gt;
	$availablefromgroup[] =&amp;amp; $mform-&amp;gt;createElement(&#039;checkbox&#039;, &#039;availablefromenabled&#039;, &#039;&#039;, get_string(&#039;enable&#039;));&lt;br /&gt;
        $mform-&amp;gt;addGroup($availablefromgroup, &#039;availablefromgroup&#039;, get_string(&#039;availablefromdate&#039;, &#039;data&#039;), &#039;&amp;amp;nbsp;&#039;, false);&lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;availablefromgroup&#039;, &#039;availablefromenabled&#039;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==addRule==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addRule(&#039;elementname&#039;, get_string(&#039;error&#039;), &#039;rule type&#039;, &#039;extraruledata&#039;, &#039;server&#039;(default), false, false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first param(element) is an element name and second(message) is the error message that will be displayed to the user.&lt;br /&gt;
The third parameter(type) is the type of rule. The fourth param(format) is used for extra data needed with some rules such as minlength and regex. The fifth parameter(validation) validates input data on server or client side, if validation is done on client side then it will be checked on the server side as well.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 * @param    string     $element       Form element name&lt;br /&gt;
 * @param    string     $message       Message to display for invalid data&lt;br /&gt;
 * @param    string     $type          Rule type, use getRegisteredRules() to get types&lt;br /&gt;
 * @param    string     $format        (optional)Required for extra rule data&lt;br /&gt;
 * @param    string     $validation    (optional)Where to perform validation: &amp;quot;server&amp;quot;, &amp;quot;client&amp;quot;&lt;br /&gt;
 * @param    boolean    $reset         Client-side validation: reset the form element to its original value if there is an error?&lt;br /&gt;
 * @param    boolean    $force         Force the rule to be applied, even if the target form element does not exist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Rule Types&#039;&#039;&#039;&lt;br /&gt;
* required &lt;br /&gt;
* maxlength&lt;br /&gt;
* minlength&lt;br /&gt;
* rangelength&lt;br /&gt;
* email&lt;br /&gt;
* regex&lt;br /&gt;
* lettersonly&lt;br /&gt;
* alphanumeric&lt;br /&gt;
* numeric&lt;br /&gt;
* nopunctuation&lt;br /&gt;
* nonzero&lt;br /&gt;
* callback&lt;br /&gt;
* compare&lt;br /&gt;
&lt;br /&gt;
==setHelpButton==&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;setHelpButton(&#039;lessondefault&#039;, array(&#039;lessondefault&#039;, get_string(&#039;lessondefault&#039;, &#039;lesson&#039;), &#039;lesson&#039;));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First param is an elementname and the second param is an array of params that are passed to helpbutton in weblib.php. Params are :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 * @param string $page  The keyword that defines a help page&lt;br /&gt;
 * @param string $title The title of links, rollover tips, alt tags etc&lt;br /&gt;
 *           &#039;Help with&#039; (or the language equivalent) will be prefixed and &#039;...&#039; will be stripped.&lt;br /&gt;
 * @param string $module Which module is the page defined in&lt;br /&gt;
 * @param mixed $image Use a help image for the link?  (true/false/&amp;quot;both&amp;quot;)&lt;br /&gt;
 * @param boolean $linktext If true, display the title next to the help icon.&lt;br /&gt;
 * @param string $text If defined then this text is used in the page, and&lt;br /&gt;
 *           the $page variable is ignored.&lt;br /&gt;
 * @param boolean $return If true then the output is returned as a string, if false it is printed to the current page.&lt;br /&gt;
 * @param string $imagetext The full text for the helpbutton icon. If empty use default help.gif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Make sure you don&#039;t set boolean $return to false. &lt;br /&gt;
&lt;br /&gt;
You need to do use this method after addElement();&lt;br /&gt;
&lt;br /&gt;
==addHelpButton==&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addHelpButton(&#039;api_key_field&#039;, &#039;api_key&#039;, &#039;block_extsearch&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 the &amp;quot;setHelpButton&amp;quot; method has been deprecated in favor of the &amp;quot;addHelpButton&amp;quot; method, which has a simplified interface and uses $OUTPUT-&amp;gt;help_icon() on the back end. The following parameters are expected:&lt;br /&gt;
&lt;br /&gt;
 * @param $elementname The name of the form element to add the help button for&lt;br /&gt;
 * @param $identifier The identifier for the help string and its title (see below)&lt;br /&gt;
 * @param $component The component name to look for the help string in&lt;br /&gt;
&lt;br /&gt;
Unlike in Moodle 1.9, it is no longer necessary to put your help pages in separate HTML files. Instead, the function looks for two strings:&lt;br /&gt;
&lt;br /&gt;
 1. get_string($identifier, $component): The title of the help page&lt;br /&gt;
 2. get_string(&amp;quot;{$identifier}_help&amp;quot;, $component): The content of the help page&lt;br /&gt;
&lt;br /&gt;
So you will need to have &#039;&#039;&#039;$identifier&#039;&#039;&#039; and &#039;&#039;&#039;{$identifier}_help&#039;&#039;&#039; defined in order for the help button to be created properly.&lt;br /&gt;
&lt;br /&gt;
==setDefault==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;grade&#039;, get_string(&#039;gradeforsubmission&#039;, &#039;exercise&#039;), $grades);&lt;br /&gt;
        $mform-&amp;gt;setHelpButton(&#039;grade&#039;, array(&#039;grade&#039;, get_string(&#039;gradeforsubmission&#039;, &#039;exercise&#039;), &#039;exercise&#039;));&lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;grade&#039;, 100);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set the default of the form value with setDefault($elementname, $value); where elementname is the elementname whose default you want to set and $value is the default to set. We set the defaults for the form in definition(). This default is what is used if no data is loaded into the form with set_data(); eg. on display of the form for an &#039;add&#039; rather than &#039;update&#039; function.&lt;br /&gt;
&lt;br /&gt;
==disabledIf==&lt;br /&gt;
&lt;br /&gt;
For any element or groups of element in a form you can conditionally disable the group or individual element depending on conditions.&lt;br /&gt;
&lt;br /&gt;
You can use $mform-&amp;gt;disabledIf($elementName, $dependentOn, $condition = &#039;notchecked&#039;, $value=null)&lt;br /&gt;
&lt;br /&gt;
* elementname can be a group. If you specify a group all elements in the group will be disabled (if dependentOn is in elementname group that is ignored and not disabled). These are the element names you&#039;ve used as the second argument in addElement or addGroup.&lt;br /&gt;
* dependentOn is the actual name of the element as it will appear in html. This can be different to the name used in addGroup particularly but also addElement where you&#039;re adding a complex element like a date_selector. Check the html of your page. You typically make the depedentOn a checkbox or select box.&lt;br /&gt;
* $condition will be &#039;notchecked&#039;, &#039;checked&#039;, &#039;noitemselected&#039;, &#039;eq&#039; or, if it is anything else, we test for &#039;neq&#039;.&lt;br /&gt;
** If $condition is &#039;eq&#039; or &#039;neq&#039; then we check the value of the dependentOn field and check for equality (==) or nonequality (!=) in js&lt;br /&gt;
** If $condition is &#039;checked&#039; or &#039;notchecked&#039; then we check to see if a checkbox is checked or not.&lt;br /&gt;
** If $condition is &#039;noitemselected&#039; then we check to see whether nothing is selected in a dropdown list.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
 // Disable my control unless a checkbox is checked.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;somecheckbox&#039;);&lt;br /&gt;
 &lt;br /&gt;
 // Disable my control if a checkbox &#039;&#039;&#039;is&#039;&#039;&#039; checked.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;somecheckbox&#039;, &#039;checked&#039;);&lt;br /&gt;
 &lt;br /&gt;
 // Disable my control unless a dropdown has value 42.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;someselect&#039;, &#039;eq&#039;, 42);&lt;br /&gt;
&lt;br /&gt;
The possible choices here are in the function lockoptionsall in lib/javascript-static.js.&lt;br /&gt;
&lt;br /&gt;
==setType==&lt;br /&gt;
&lt;br /&gt;
PARAM_* types are used to specify how a submitted variable should be cleaned. These should be used for get parameters such as id, course etc. which are used to load a page and also with setType(); method. Every form element should have a type specified except select, radio box and checkbox elements, these elements do a good job of cleaning themselves (only specified options are allowed as user input).&lt;br /&gt;
&lt;br /&gt;
===Most Commonly Used PARAM_* Types===&lt;br /&gt;
&lt;br /&gt;
These are the most commonly used PARAM_* types and their proper uses. More types can be seen in moodlelib.php starting around line 100.&lt;br /&gt;
&lt;br /&gt;
* PARAM_CLEAN is deprecated and you should try to use a more specific type.&lt;br /&gt;
* PARAM_TEXT should be used for cleaning data that is expected to be plain text. It will strip all html tags. But will still let tags for multilang support through.&lt;br /&gt;
* PARAM_NOTAGS should be used for cleaning data that is expected to be plain text. It will strip *all* html type tags. It will still *not* let tags for multilang support through. This should be used for instance for email addresses where no multilang support is appropriate.&lt;br /&gt;
* PARAM_RAW means no cleaning whatsoever, it is used mostly for data from the html editor. Data from the editor is later cleaned before display using format_text() function. PARAM_RAW can also be used for data that is validated by some other way or printed by p() or s().&lt;br /&gt;
* PARAM_INT should be used for integers.&lt;br /&gt;
* PARAM_ACTION is an alias of PARAM_ALPHA and is used for hidden fields specifying form actions.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.midnighthax.com/quickform.php PEAR HTML QuickForm Getting Started Guide] by Keith Edmunds of Midnighthax.com&lt;br /&gt;
* [http://pear.php.net/manual/en/package.html.html-quickform.php PEAR::HTML_QuickForm manual]&lt;br /&gt;
&lt;br /&gt;
[[Category:Formslib]]&lt;br /&gt;
&lt;br /&gt;
If you have problems creating php forms you may get them with form builder http://phpforms.net/tutorial/html-basics/form-builder.html&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=lib/formslib.php_Form_Definition&amp;diff=80101</id>
		<title>lib/formslib.php Form Definition</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=lib/formslib.php_Form_Definition&amp;diff=80101"/>
		<updated>2011-01-06T22:00:09Z</updated>

		<summary type="html">&lt;p&gt;Agwells: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Formslib}}&lt;br /&gt;
== &#039;&#039;definition()&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The definition of the elements to be included in the form, their &#039;types&#039; (PARAM_*), helpbuttons included, etc. is all included in a function you must define in your class.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;definition()&#039;&#039; is used to define the elements in the form and &#039;&#039;&#039;this definition will be used for validating data submitted as well as for printing the form.&#039;&#039;&#039; For select and checkbox type elements only data that could have been selected will be allowed. And only data that corresponds to a form element in the definition will be accepted as submitted data.&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;definition()&#039;&#039; should include all elements that are going to be used on form, some elements may be removed or tweaked later in &#039;&#039;definition_after_data()&#039;&#039;. Please do not create conditional elements in &#039;&#039;definition()&#039;&#039;, the definition() should not directly depend on the submitted data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&amp;quot;$CFG-&amp;gt;libdir/formslib.php&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
class simplehtml_form extends moodleform {&lt;br /&gt;
&lt;br /&gt;
    function definition() {&lt;br /&gt;
        global $CFG;&lt;br /&gt;
       &lt;br /&gt;
        $mform =&amp;amp; $this-&amp;gt;_form; // Don&#039;t forget the underscore! &lt;br /&gt;
&lt;br /&gt;
        $mform-&amp;gt;addElement()... // Add elements to your form&lt;br /&gt;
            ...&lt;br /&gt;
    }                           // Close the function&lt;br /&gt;
}                               // Close the class&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use Fieldsets to group Form Elements==&lt;br /&gt;
&lt;br /&gt;
You use code like this to open a fieldset with a &#039;&#039;legend&#039;&#039;. &amp;lt;br /&amp;gt;&lt;br /&gt;
(&#039;&#039;&#039;Note&#039;&#039;&#039;: Some themes turn off legends on admin setting pages by using CSS: &amp;lt;nowiki&amp;gt;#adminsettings legend {display:none;}&amp;lt;/nowiki&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;header&#039;, &#039;nameforyourheaderelement&#039;, get_string(&#039;titleforlegened&#039;, &#039;modulename&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can&#039;t yet nest these visible fieldsets unfortunately. But in fact groups of elements are wrapped in invisible fieldsets.&lt;br /&gt;
&lt;br /&gt;
You close a fieldset with moodle_form&#039;s closeHeaderBefore method. You tell closeHeaderBefore the element before you wish to end the fieldset. A fieldset is automatically closed if you open a new one. You need to use this code only if you want to close a fieldset and the subsequent form elements are not to be enclosed by a visible fieldset (they are still enclosed with an invisibe one with no legend) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;closeHeaderBefore(&#039;buttonar&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==addElement==&lt;br /&gt;
&lt;br /&gt;
Use the addElement method to add an element to a form. The first few arguments are always the same. The first param is the type of the element to add. The second is the elementname to use which is normally the html name of the element in the form. The third is often the text for the label for the element.&lt;br /&gt;
&lt;br /&gt;
Some examples are below :&lt;br /&gt;
=== button ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;button&#039;, &#039;intro&#039;, get_string(&amp;quot;buttonlabel&amp;quot;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A button element. If you want a submit or cancel button see &#039;submit&#039; element.&lt;br /&gt;
&lt;br /&gt;
=== checkbox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;checkbox&#039;, &#039;ratingtime&#039;, get_string(&#039;ratingtime&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a simple checkbox. The third parameter for this element is the label to display on the left side of the form. You can also supply a string as a fourth parameter to specify a label that will appear on the right of the element. Checkboxes and radio buttons can be grouped and have individual labels on their right.&lt;br /&gt;
&lt;br /&gt;
You can have a 5th parameter $attributes, as on other elements.&lt;br /&gt;
&lt;br /&gt;
==== advcheckbox ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;advcheckbox&#039;, &#039;ratingtime&#039;, get_string(&#039;ratingtime&#039;, &#039;forum&#039;), &#039;Label displayed after checkbox&#039;, array(&#039;group&#039; =&amp;gt; 1), array(0, 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to the checkbox above, but with a couple of important improvements:&lt;br /&gt;
&lt;br /&gt;
# The 5th parameter is a normal $attributes array, normally used to set HTML attributes for the &amp;lt;input&amp;gt; element. However, a special value of &#039;group&#039; can be given, which will add a class name to the element, and enable its grouping for a [[Development:lib/formslib.php_add_checkbox_controller|checkbox controller]]&lt;br /&gt;
#The 6th parameter is an array of values that will be associated with the checked/unchecked state of the checkbox. With a normal checkbox you cannot choose that value, and in fact an unchecked checkbox will not even be sent with the form data.&lt;br /&gt;
&lt;br /&gt;
=== choosecoursefile ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;choosecoursefile&#039;, &#039;mediafile&#039;, get_string(&#039;mediafile&#039;, &#039;lesson&#039;), array(&#039;courseid&#039;=&amp;gt;$COURSE-&amp;gt;id));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choose a file from the course files area. The fourth option is a list of options for the element. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&#039;courseid&#039; =&amp;gt;null,  //if it is null (default then use global $COURSE&lt;br /&gt;
      &#039;height&#039;   =&amp;gt;500,   // height of the popup window&lt;br /&gt;
      &#039;width&#039;    =&amp;gt;750,   // width of the popup window&lt;br /&gt;
      &#039;options&#039;  =&amp;gt;&#039;none&#039;); //options string for the pop up window &lt;br /&gt;
                          //eg. &#039;menubar=0,location=0,scrollbars,resizable&#039;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass an optional 5th parameter of attributes, as for other elements. The most useful way of using that is something like &lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;maxlength&#039; =&amp;gt; 255, &#039;size&#039; =&amp;gt; 48)&amp;lt;/code&amp;gt;&lt;br /&gt;
to control the maxlength / size of the text box (note size will default to 48 if not specified)&lt;br /&gt;
&lt;br /&gt;
Finally, as this element is a group containing two elements (button + value), you can add validation rules by using the &#039;&#039;&#039;addGroupRule()&#039;&#039;&#039; method in this (complex) way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$mform-&amp;gt;addGroupRule(&#039;elementname&#039;, array(&#039;value&#039; =&amp;gt; array(array(list, of, rule, params, but, fieldname))));&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where: &#039;&#039;&#039;&amp;quot;elementname&amp;quot;&#039;&#039;&#039; is the name of the choosecoursefile group element, &#039;&#039;&#039;&amp;quot;value&amp;quot;&#039;&#039;&#039; is the name of the text field within the group and the &#039;&#039;&#039;&amp;quot;list, of, addrule, params, but, fieldname&amp;quot;&#039;&#039;&#039; is exactly that, the list of fields in the normal addRule() function but ommiting the first one, the fieldname.&lt;br /&gt;
&lt;br /&gt;
For example, the [http://cvs.moodle.org/moodle/mod/resource/type/file/resource.class.php?view=markup file/url resource type], uses one &amp;quot;choosecoursefile&amp;quot; element, and it controls the maximum length of the field (255) with this use of addGroupRule():&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;$mform-&amp;gt;addGroupRule(&#039;reference&#039;, array(&#039;value&#039; =&amp;gt; array(array(get_string(&#039;maximumchars&#039;, &#039;&#039;, 255), &#039;maxlength&#039;, 255, &#039;client&#039;))));&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== date_selector ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;date_selector&#039;, &#039;assesstimefinish&#039;, get_string(&#039;to&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a date selector. You can select a Day, Month and Year using a group of select boxes. The fourth param here is an array of options. The defaults for the options are :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&lt;br /&gt;
    &#039;startyear&#039; =&amp;gt; 1970, &lt;br /&gt;
    &#039;stopyear&#039;  =&amp;gt; 2020,&lt;br /&gt;
    &#039;timezone&#039;  =&amp;gt; 99, &lt;br /&gt;
    &#039;applydst&#039;  =&amp;gt; true, &lt;br /&gt;
    &#039;optional&#039;  =&amp;gt; false&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can override these defaults by supplying an array as fourth param with one or more keys with a value to override the default. You can supply a fifth param of attributes here as well.&lt;br /&gt;
&lt;br /&gt;
=== date_time_selector ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;date_time_selector&#039;, &#039;assesstimestart&#039;, get_string(&#039;from&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a group of select boxes to select a date (Day Month and Year) and time (Hour and Minute). When submitted, submitted data is processed and a timestamp is passed to $form-&amp;gt;get_data(); the fourth param here is an array of options. The defaults for the options are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
array(&lt;br /&gt;
    &#039;startyear&#039; =&amp;gt; 1970, &lt;br /&gt;
    &#039;stopyear&#039;  =&amp;gt; 2020,&lt;br /&gt;
    &#039;timezone&#039;  =&amp;gt; 99, &lt;br /&gt;
    &#039;applydst&#039;  =&amp;gt; true, &lt;br /&gt;
    &#039;step&#039;      =&amp;gt; 5&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can override these defaults by supplying an array as fourth param with one or more keys with a value to override the default. You can supply a fifth param of attributes here as well.&lt;br /&gt;
&lt;br /&gt;
===duration===&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;duration&#039;, &#039;timelimit&#039;, get_string(&#039;timelimit&#039;, &#039;quiz&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This field type lets the user input an interval of time. It comprises a text field, where you can type a number, and a dropdown for selecting a unit (days, hours, minutes or seconds). When submitted the value is converted to a number of seconds.&lt;br /&gt;
&lt;br /&gt;
You can add a fourth parameter to give options. At the moment the only option supported is here is an array of options. The defaults for the options is:&lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;optional&#039; =&amp;gt; true)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass an optional 5th parameter of attributes, as for other elements. The most useful way of using that is something like &lt;br /&gt;
&amp;lt;code php&amp;gt;array(&#039;size&#039; =&amp;gt; 5)&amp;lt;/code&amp;gt;&lt;br /&gt;
to control the size of the text box.&lt;br /&gt;
&lt;br /&gt;
=== file ===&lt;br /&gt;
&lt;br /&gt;
File upload input box with browse button. In the form definition type&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;file&#039;, &#039;attachment&#039;, get_string(&#039;attachment&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after form submission and validation use&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;
      ...&lt;br /&gt;
    $mform-&amp;gt;save_files($destination_directory);&lt;br /&gt;
      ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need advanced settings such as required file, different max upload size or name of uploaded file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$this-&amp;gt;set_upload_manager(new upload_manager(&#039;attachment&#039;, true, false, $COURSE, false, 0, 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;addRule(&#039;attachment&#039;, null, &#039;required&#039;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&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;
      ...&lt;br /&gt;
    $mform-&amp;gt;save_files($destination_directory);&lt;br /&gt;
    $newfilename = $mform-&amp;gt;get_new_filename();&lt;br /&gt;
      ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When porting old code it is also possible to use the upload manager directly for processing of uploaded files.&lt;br /&gt;
&lt;br /&gt;
Please note that if using set_upload_manager() it must be before addElement(&#039;file&#039;,..).&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
File uploading was rewritten in 2.0. Please see inline docs for now. This page will be updated when the new API stabilises.&lt;br /&gt;
&lt;br /&gt;
===filepicker===&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
General replacement of &#039;&#039;file&#039;&#039; 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, 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;
See also [[Development:Using the File API in Moodle forms]]&lt;br /&gt;
&lt;br /&gt;
=== hidden ===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;hidden&#039;, &#039;reply&#039;, &#039;yes&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A hidden element. Set the element name (in this case &#039;&#039;&#039;reply&#039;&#039;&#039;) to the stated value (in this case &#039;&#039;&#039;yes&#039;&#039;&#039;).&lt;br /&gt;
&lt;br /&gt;
=== html ===&lt;br /&gt;
You can add arbitrary HTML to your Moodle form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;html&#039;, &#039;&amp;lt;div class=&amp;quot;qheader&amp;quot;&amp;gt;&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [http://moodle.org/mod/forum/discuss.php?d=126935 &amp;quot;Question: Can I put a moodleform inside a table td?&amp;quot;] for a concrete example.&lt;br /&gt;
&lt;br /&gt;
=== htmleditor &amp;amp; format ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;htmleditor&#039;, &#039;text&#039;, get_string(&#039;choicetext&#039;, &#039;choice&#039;));&lt;br /&gt;
$mform-&amp;gt;setType(&#039;text&#039;, PARAM_RAW);&lt;br /&gt;
$mform-&amp;gt;addRule(&#039;text&#039;, null, &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;format&#039;, &#039;format&#039;, get_string(&#039;format&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can supply a fourth param to htmleditor of an array of options :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
array(&lt;br /&gt;
    &#039;canUseHtmlEditor&#039;=&amp;gt;&#039;detect&#039;,&lt;br /&gt;
    &#039;rows&#039;  =&amp;gt; 10, &lt;br /&gt;
    &#039;cols&#039;  =&amp;gt; 65, &lt;br /&gt;
    &#039;width&#039; =&amp;gt; 0,&lt;br /&gt;
    &#039;height&#039;=&amp;gt; 0, &lt;br /&gt;
    &#039;course&#039;=&amp;gt; 0,&lt;br /&gt;
);&lt;br /&gt;
//options same as print_textarea params&lt;br /&gt;
//use rows and cols options to control htmleditor size.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===modgrade===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;modgrade&#039;, &#039;scale&#039;, get_string(&#039;grade&#039;), false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is a custom element for selecting a grade for any activity module. The fourth argument is whether to include an option for no grade which has a value 0. This select box does include scales. The default is true, include no grade option.&lt;br /&gt;
&lt;br /&gt;
A helpbutton is automatically added.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===password===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;password&#039;, &#039;password&#039;, get_string(&#039;label&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A password element. Fourth param is an array or string of attributes.&lt;br /&gt;
&lt;br /&gt;
===passwordunmask===&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;passwordunmask&#039;, &#039;password&#039;, get_string(&#039;label&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A password element with option to show the password in plaintext. Fourth param is an array or string of attributes.&lt;br /&gt;
&lt;br /&gt;
=== radio ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$radioarray=array();&lt;br /&gt;
$radioarray[] = &amp;amp;MoodleQuickForm::createElement(&#039;radio&#039;, &#039;yesno&#039;, &#039;&#039;, get_string(&#039;yes&#039;), 1, $attributes);&lt;br /&gt;
$radioarray[] = &amp;amp;MoodleQuickForm::createElement(&#039;radio&#039;, &#039;yesno&#039;, &#039;&#039;, get_string(&#039;no&#039;), 0, $attributes);&lt;br /&gt;
$mform-&amp;gt;addGroup($radioarray, &#039;radioar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second param names the radio button and should be the same for each button in the group in order to toggle correctly. Third param would be the label for the form element but is generally ignored as this element will always be in a group which has it&#039;s own label. Fourth param is a string, a label to be displayed on the right of the element. The fifth is the value for this radio button. $attributes can be a string or an array of attributes.&lt;br /&gt;
&lt;br /&gt;
It is possible to add help to individual radio buttons but this requires a custom template to be defined for the group elements. See MDL-15571.&lt;br /&gt;
&lt;br /&gt;
==== setDefault ====&lt;br /&gt;
&lt;br /&gt;
To set the default for a radio button group as above use the following :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;setDefault(&#039;yesno&#039;, 0);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the default &#039;no&#039;.&lt;br /&gt;
&lt;br /&gt;
===select===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;type&#039;, get_string(&#039;forumtype&#039;, &#039;forum&#039;), $FORUM_TYPES, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The fourth param for this element is an array of options for the select box. The keys are the values for the option and the value of the array is the text for the option. The fifth param $attributes is optional, see text element for description of attributes param.&lt;br /&gt;
&lt;br /&gt;
It is also possible to create a select with certain options disabled, using [http://stackoverflow.com/questions/2138089/how-can-i-use-quickform-to-add-disabled-select-options/2150275#2150275 this technique].&lt;br /&gt;
&lt;br /&gt;
====multi-select====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $select = &amp;amp;$mform-&amp;gt;addElement(&#039;select&#039;, &#039;colors&#039;, get_string(&#039;colors&#039;), array(&#039;red&#039;, &#039;blue&#039;, &#039;green&#039;), $attributes);&lt;br /&gt;
        $select-&amp;gt;setMultiple(true);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===selectyesno===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;selectyesno&#039;, &#039;maxbytes&#039;, get_string(&#039;maxattachmentsize&#039;, &#039;forum&#039;));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want a yes / no select box this one automatically translates itself and has value 1 for yes and 0 for no.&lt;br /&gt;
&lt;br /&gt;
===static===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
         $mform-&amp;gt;addElement(&#039;static&#039;, &#039;description&#039;, get_string(&#039;description&#039;, &#039;exercise&#039;),&lt;br /&gt;
                  get_string(&#039;descriptionofexercise&#039;, &#039;exercise&#039;, $COURSE-&amp;gt;students));&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a static element. It should be used with care it is used to display a static piece of text with a label. The third param is the label and the fourth is the static text itself.&lt;br /&gt;
&lt;br /&gt;
===submit, reset and cancel===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        //normally you use add_action_buttons instead of this code&lt;br /&gt;
        $buttonarray=array();&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;submit&#039;, &#039;submitbutton&#039;, get_string(&#039;savechanges&#039;));&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;reset&#039;, &#039;resetbutton&#039;, get_string(&#039;revert&#039;));&lt;br /&gt;
        $buttonarray[] = &amp;amp;$mform-&amp;gt;createElement(&#039;cancel&#039;);&lt;br /&gt;
        $mform-&amp;gt;addGroup($buttonarray, &#039;buttonar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
        $mform-&amp;gt;closeHeaderBefore(&#039;buttonar&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A &#039;Submit&#039; type element is a submit type form element which will submit the form. A &#039;Reset&#039; will not submit the form but will reset any changes the user has made to form contents. A &#039;Cancel&#039; element cancels form submission. You need to have a branch in your code before you check for get_data() to check if submission has been cancelled with is_cancelled(); See the example on the usage page.&lt;br /&gt;
&lt;br /&gt;
You should name your submit and reset buttons &#039;submitbutton&#039; and &#039;resetbutton&#039; or something similar (not &#039;submit&#039; and &#039;reset&#039;). This avoids problems in JavaScript of collisions between form element names and names of JavaScript methods of the form object.&lt;br /&gt;
&lt;br /&gt;
====add_action_buttons($cancel = true, $submitlabel=null);====&lt;br /&gt;
&lt;br /&gt;
You will normally use this helper function which is a method of moodleform to add all the &#039;action&#039; buttons to the end of your form. A boolean parameter allow you to specify whether to include a cancel button and specify the label for your submit button (pass the result of get_string). Default for the submit button label is get_string(&#039;savechanges&#039;).&lt;br /&gt;
&lt;br /&gt;
===text===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;text&#039;, &#039;name&#039;, get_string(&#039;forumname&#039;, &#039;forum&#039;), $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For a simple text element. Your fourth parameter here can be a string or array of attributes for the text element. The following are equivalent :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $attributes=&#039;size=&amp;quot;20&amp;quot;&#039;;&lt;br /&gt;
        $attributes=array(&#039;size&#039;=&amp;gt;&#039;20&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Generally you are encouraged to use CSS instead of using attributes for styling.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A format element can be used as a format select box. It will be non-selectable if you&#039;re using an html editor.&lt;br /&gt;
&lt;br /&gt;
The third param for this element is $useHtmlEditor and it defaults to null in which case an html editor is used if the browser and user profile support it.&lt;br /&gt;
&lt;br /&gt;
===textarea===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;textarea&#039;, &#039;introduction&#039;, get_string(&amp;quot;introtext&amp;quot;, &amp;quot;survey&amp;quot;), &#039;wrap=&amp;quot;virtual&amp;quot; rows=&amp;quot;20&amp;quot; cols=&amp;quot;50&amp;quot;&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A textarea element. If you want an htmleditor use htmleditor element. Fourth element here is a string or array of attributes.&lt;br /&gt;
&lt;br /&gt;
===recaptcha===&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;recaptcha&#039;, &#039;recaptcha_field_name&#039;, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this recaptcha element to reduce the spam risk in your forms. Third element here is a string or array of attributes. Take care to get an API key from http://recaptcha.net/api/getkey before using this element.&lt;br /&gt;
&lt;br /&gt;
To check whether recaptcha is enabled at site level use:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if (!empty($CFG-&amp;gt;recaptchapublickey) &amp;amp;&amp;amp; !empty($CFG-&amp;gt;recaptchaprivatekey)) {&lt;br /&gt;
    //recaptcha is enabled&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===tags===&lt;br /&gt;
{{Moodle 2.1}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addElement(&#039;tags&#039;, &#039;field_name&#039;, $lable, $options, $attributes);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Used for editing a list of tags, for example on a blog post.&lt;br /&gt;
&lt;br /&gt;
There is only one option available, &#039;display&#039;, which should be set to one of the contstants MoodleQuickForm_tags::ONLYOFFICIAL, NOOFFICIAL or DEFAULTUI. This controls whether the official tags are listed for easy selection, or a text area where arbitrary tags may be typed, or both. The default is both.&lt;br /&gt;
&lt;br /&gt;
The value should be set/returned as an array of tags.&lt;br /&gt;
&lt;br /&gt;
==addGroup==&lt;br /&gt;
&lt;br /&gt;
A &#039;group&#039; in formslib is just a group of elements that will have a label and will be included on one line. &lt;br /&gt;
&lt;br /&gt;
For example typical code to include a submit and cancel button on the same line : &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $buttonarray=array();&lt;br /&gt;
        $buttonarray[] =&amp;amp; $mform-&amp;gt;createElement(&#039;submit&#039;, &#039;submitbutton&#039;, get_string(&#039;savechanges&#039;));&lt;br /&gt;
        $buttonarray[] =&amp;amp; $mform-&amp;gt;createElement(&#039;submit&#039;, &#039;cancel&#039;, get_string(&#039;cancel&#039;));&lt;br /&gt;
        $mform-&amp;gt;addGroup($buttonarray, &#039;buttonar&#039;, &#039;&#039;, array(&#039; &#039;), false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You use the same arguments for createElement as you do for addElement. Any label for the element in the third argument is normally ignored (but not in the case of the submit buttons above where the third argument is not for a label but is the text for the button).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a bad example (don&#039;t do this for real, use the &#039;optional&#039; =&amp;gt; true option of the date element): putting a date_selector (which is itself a group of elements) and a checkbox on the same line, note that you can disable every element in the group using the group name &#039;availablefromgroup&#039; but it doesn&#039;t disable the controlling element the &#039;availablefromenabled&#039; checkbox:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $availablefromgroup=array();&lt;br /&gt;
	$availablefromgroup[] =&amp;amp; $mform-&amp;gt;createElement(&#039;date_selector&#039;, &#039;availablefrom&#039;, &#039;&#039;);&lt;br /&gt;
	$availablefromgroup[] =&amp;amp; $mform-&amp;gt;createElement(&#039;checkbox&#039;, &#039;availablefromenabled&#039;, &#039;&#039;, get_string(&#039;enable&#039;));&lt;br /&gt;
        $mform-&amp;gt;addGroup($availablefromgroup, &#039;availablefromgroup&#039;, get_string(&#039;availablefromdate&#039;, &#039;data&#039;), &#039;&amp;amp;nbsp;&#039;, false);&lt;br /&gt;
        $mform-&amp;gt;disabledIf(&#039;availablefromgroup&#039;, &#039;availablefromenabled&#039;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==addRule==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addRule(&#039;elementname&#039;, get_string(&#039;error&#039;), &#039;rule type&#039;, &#039;extraruledata&#039;, &#039;server&#039;(default), false, false);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first param(element) is an element name and second(message) is the error message that will be displayed to the user.&lt;br /&gt;
The third parameter(type) is the type of rule. The fourth param(format) is used for extra data needed with some rules such as minlength and regex. The fifth parameter(validation) validates input data on server or client side, if validation is done on client side then it will be checked on the server side as well.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 * @param    string     $element       Form element name&lt;br /&gt;
 * @param    string     $message       Message to display for invalid data&lt;br /&gt;
 * @param    string     $type          Rule type, use getRegisteredRules() to get types&lt;br /&gt;
 * @param    string     $format        (optional)Required for extra rule data&lt;br /&gt;
 * @param    string     $validation    (optional)Where to perform validation: &amp;quot;server&amp;quot;, &amp;quot;client&amp;quot;&lt;br /&gt;
 * @param    boolean    $reset         Client-side validation: reset the form element to its original value if there is an error?&lt;br /&gt;
 * @param    boolean    $force         Force the rule to be applied, even if the target form element does not exist&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Rule Types&#039;&#039;&#039;&lt;br /&gt;
* required &lt;br /&gt;
* maxlength&lt;br /&gt;
* minlength&lt;br /&gt;
* rangelength&lt;br /&gt;
* email&lt;br /&gt;
* regex&lt;br /&gt;
* lettersonly&lt;br /&gt;
* alphanumeric&lt;br /&gt;
* numeric&lt;br /&gt;
* nopunctuation&lt;br /&gt;
* nonzero&lt;br /&gt;
* callback&lt;br /&gt;
* compare&lt;br /&gt;
&lt;br /&gt;
==setHelpButton==&lt;br /&gt;
{{Moodle 1.9}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;setHelpButton(&#039;lessondefault&#039;, array(&#039;lessondefault&#039;, get_string(&#039;lessondefault&#039;, &#039;lesson&#039;), &#039;lesson&#039;));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First param is an elementname and the second param is an array of params that are passed to helpbutton in weblib.php. Params are :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 * @param string $page  The keyword that defines a help page&lt;br /&gt;
 * @param string $title The title of links, rollover tips, alt tags etc&lt;br /&gt;
 *           &#039;Help with&#039; (or the language equivalent) will be prefixed and &#039;...&#039; will be stripped.&lt;br /&gt;
 * @param string $module Which module is the page defined in&lt;br /&gt;
 * @param mixed $image Use a help image for the link?  (true/false/&amp;quot;both&amp;quot;)&lt;br /&gt;
 * @param boolean $linktext If true, display the title next to the help icon.&lt;br /&gt;
 * @param string $text If defined then this text is used in the page, and&lt;br /&gt;
 *           the $page variable is ignored.&lt;br /&gt;
 * @param boolean $return If true then the output is returned as a string, if false it is printed to the current page.&lt;br /&gt;
 * @param string $imagetext The full text for the helpbutton icon. If empty use default help.gif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Make sure you don&#039;t set boolean $return to false. &lt;br /&gt;
&lt;br /&gt;
You need to do use this method after addElement();&lt;br /&gt;
&lt;br /&gt;
==addHelpButton==&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
            $mform-&amp;gt;addHelpButton(&#039;api_key_field&#039;, &#039;api_key&#039;, &#039;block_extsearch&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 the &amp;quot;setHelpButton&amp;quot; method has been deprecated in favor of the &amp;quot;addHelpButton&amp;quot; method, which has a simplified interface and uses $OUTPUT-&amp;gt;help_icon() on the back end. The following parameters are expected:&lt;br /&gt;
&lt;br /&gt;
 * @param $elementname The name of the form element to add the help button for&lt;br /&gt;
 * @param $identifier The identifier for the help string and its title (see below)&lt;br /&gt;
 * @param $component The component name to look for the help string in&lt;br /&gt;
&lt;br /&gt;
Unlike in Moodle 1.9, it is no longer necessary to put your help pages in separate HTML files. Instead, the function looks for two strings:&lt;br /&gt;
&lt;br /&gt;
 1. get_string($identifier, $component): The title of the help page&lt;br /&gt;
 2. get_string(&amp;quot;{$identifier}_help&amp;quot;, $component): The content of the help page&lt;br /&gt;
&lt;br /&gt;
So you will need to have &#039;&#039;&#039;$identifier&#039;&#039;&#039; and &#039;&#039;&#039;{$identifier}_help&#039;&#039;&#039; defined in order for the help button to be created properly.&lt;br /&gt;
&lt;br /&gt;
==setDefault==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;select&#039;, &#039;grade&#039;, get_string(&#039;gradeforsubmission&#039;, &#039;exercise&#039;), $grades);&lt;br /&gt;
        $mform-&amp;gt;setHelpButton(&#039;grade&#039;, array(&#039;grade&#039;, get_string(&#039;gradeforsubmission&#039;, &#039;exercise&#039;), &#039;exercise&#039;));&lt;br /&gt;
        $mform-&amp;gt;setDefault(&#039;grade&#039;, 100);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set the default of the form value with setDefault($elementname, $value); where elementname is the elementname whose default you want to set and $value is the default to set. We set the defaults for the form in definition(). This default is what is used if no data is loaded into the form with set_data(); eg. on display of the form for an &#039;add&#039; rather than &#039;update&#039; function.&lt;br /&gt;
&lt;br /&gt;
==disabledIf==&lt;br /&gt;
&lt;br /&gt;
For any element or groups of element in a form you can conditionally disable the group or individual element depending on conditions.&lt;br /&gt;
&lt;br /&gt;
You can use $mform-&amp;gt;disabledIf($elementName, $dependentOn, $condition = &#039;notchecked&#039;, $value=null)&lt;br /&gt;
&lt;br /&gt;
* elementname can be a group. If you specify a group all elements in the group will be disabled (if dependentOn is in elementname group that is ignored and not disabled). These are the element names you&#039;ve used as the second argument in addElement or addGroup.&lt;br /&gt;
* dependentOn is the actual name of the element as it will appear in html. This can be different to the name used in addGroup particularly but also addElement where you&#039;re adding a complex element like a date_selector. Check the html of your page. You typically make the depedentOn a checkbox or select box.&lt;br /&gt;
* $condition will be &#039;notchecked&#039;, &#039;checked&#039;, &#039;noitemselected&#039;, &#039;eq&#039; or, if it is anything else, we test for &#039;neq&#039;.&lt;br /&gt;
** If $condition is &#039;eq&#039; or &#039;neq&#039; then we check the value of the dependentOn field and check for equality (==) or nonequality (!=) in js&lt;br /&gt;
** If $condition is &#039;checked&#039; or &#039;notchecked&#039; then we check to see if a checkbox is checked or not.&lt;br /&gt;
** If $condition is &#039;noitemselected&#039; then we check to see whether nothing is selected in a dropdown list.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
 // Disable my control unless a checkbox is checked.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;somecheckbox&#039;);&lt;br /&gt;
 &lt;br /&gt;
 // Disable my control if a checkbox &#039;&#039;&#039;is&#039;&#039;&#039; checked.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;somecheckbox&#039;, &#039;checked&#039;);&lt;br /&gt;
 &lt;br /&gt;
 // Disable my control unless a dropdown has value 42.&lt;br /&gt;
 $mform-&amp;gt;disabledIf(&#039;mycontrol&#039;, &#039;someselect&#039;, &#039;eq&#039;, 42);&lt;br /&gt;
&lt;br /&gt;
The possible choices here are in the function lockoptionsall in lib/javascript-static.js.&lt;br /&gt;
&lt;br /&gt;
==setType==&lt;br /&gt;
&lt;br /&gt;
PARAM_* types are used to specify how a submitted variable should be cleaned. These should be used for get parameters such as id, course etc. which are used to load a page and also with setType(); method. Every form element should have a type specified except select, radio box and checkbox elements, these elements do a good job of cleaning themselves (only specified options are allowed as user input).&lt;br /&gt;
&lt;br /&gt;
===Most Commonly Used PARAM_* Types===&lt;br /&gt;
&lt;br /&gt;
These are the most commonly used PARAM_* types and their proper uses. More types can be seen in moodlelib.php starting around line 100.&lt;br /&gt;
&lt;br /&gt;
* PARAM_CLEAN is deprecated and you should try to use a more specific type.&lt;br /&gt;
* PARAM_TEXT should be used for cleaning data that is expected to be plain text. It will strip all html tags. But will still let tags for multilang support through.&lt;br /&gt;
* PARAM_NOTAGS should be used for cleaning data that is expected to be plain text. It will strip *all* html type tags. It will still *not* let tags for multilang support through. This should be used for instance for email addresses where no multilang support is appropriate.&lt;br /&gt;
* PARAM_RAW means no cleaning whatsoever, it is used mostly for data from the html editor. Data from the editor is later cleaned before display using format_text() function. PARAM_RAW can also be used for data that is validated by some other way or printed by p() or s().&lt;br /&gt;
* PARAM_INT should be used for integers.&lt;br /&gt;
* PARAM_ACTION is an alias of PARAM_ALPHA and is used for hidden fields specifying form actions.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.midnighthax.com/quickform.php PEAR HTML QuickForm Getting Started Guide] by Keith Edmunds of Midnighthax.com&lt;br /&gt;
* [http://pear.php.net/manual/en/package.html.html-quickform.php PEAR::HTML_QuickForm manual]&lt;br /&gt;
&lt;br /&gt;
[[Category:Formslib]]&lt;br /&gt;
&lt;br /&gt;
If you have problems creating php forms you may get them with form builder http://phpforms.net/tutorial/html-basics/form-builder.html&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Repository_plugins&amp;diff=80091</id>
		<title>Repository plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Repository_plugins&amp;diff=80091"/>
		<updated>2011-01-06T04:31:48Z</updated>

		<summary type="html">&lt;p&gt;Agwells: All the lang directories use &amp;quot;en&amp;quot; rather than &amp;quot;en_utf8&amp;quot; now&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A guide for developers on how to create a repository plugin.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
Before starting coding, it is necessary to know how to use repository administration pages and how to use the file picker.&lt;br /&gt;
&lt;br /&gt;
There is a template attached to MDL-16543 which might be useful to help get you started.&lt;br /&gt;
&lt;br /&gt;
===First steps===&lt;br /&gt;
# Create a folder for your plugin in &#039;&#039;moodle/repository/&#039;&#039; e.g. &#039;&#039;moodle/repository/myplugin&#039;&#039;&lt;br /&gt;
# Create the following files and add them to the plugin folder:&lt;br /&gt;
#* &#039;&#039;lib.php&#039;&#039;&lt;br /&gt;
#* &#039;&#039;pix/icon.png&#039;&#039; (the icon displayed in the file picker)&lt;br /&gt;
#* &amp;quot;version.php&amp;quot;&lt;br /&gt;
# Create the language file &#039;&#039;repository_myplugin.php&#039;&#039; and add it to the plugin folder, keeping the following folder structure:&lt;br /&gt;
#*&#039;&#039;moodle/repository/myplugin/lang/en/repository_myplugin.php&#039;&#039; or &#039;&#039;moodle/lang/en/repository_myplugin.php&#039;&#039;&lt;br /&gt;
# Create access.php and upgrade scripts&lt;br /&gt;
&lt;br /&gt;
===Overview===&lt;br /&gt;
&lt;br /&gt;
The 3 different parts to write&lt;br /&gt;
# Administration - You can customise the way administrators and users can configure their repositories. &lt;br /&gt;
# File picker integration - The core of your plugin, it will manage communication between Moodle and the repository service, and also the file picker display.&lt;br /&gt;
# I18n - Internationalization should be done at the same time as you&#039;re writing the other parts.&lt;br /&gt;
&lt;br /&gt;
==Administration APIs==&lt;br /&gt;
&lt;br /&gt;
As an example, let&#039;s create a Flickr plugin for accessing a public flickr account. The plugin will be called &amp;quot;Flickr Public&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Firstly the skeleton:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    /**&lt;br /&gt;
     * repository_flickr_public class&lt;br /&gt;
     * Moodle user can access public flickr account&lt;br /&gt;
     *&lt;br /&gt;
     * @license http://www.gnu.org/copyleft/gpl.html GNU Public License&lt;br /&gt;
    */&lt;br /&gt;
    class repository_flickr_public extends repository {&lt;br /&gt;
    }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then consider the question &amp;quot;What does my plugin do?&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In the Moodle file picker, we want to display some flickr public repositories directly linked to a flickr public account. For example &#039;&#039;My Public Flickr Pictures&#039;&#039;, and also &#039;&#039;My Friend&#039;s Flickr Pictures&#039;&#039;. When the user clicks on one of these repositories, the public pictures are displayed in the file picker.&lt;br /&gt;
&lt;br /&gt;
In order to access to a flickr public account, the plugin needs to know the email address of the Flickr public account owner. So the administrator will need to set an email address for every repository. Let&#039;s add an &amp;quot;email address&amp;quot; setting to every repository.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
//We tell the API that the repositories have specific settings: &amp;quot;email address&amp;quot;&lt;br /&gt;
    public static function get_instance_option_names() {&lt;br /&gt;
        return array(&#039;email_address&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
//We add an &amp;quot;email address&amp;quot; text box to the create/edit repository instance Moodle form&lt;br /&gt;
    public function instance_config_form(&amp;amp;$mform) {&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;text&#039;, &#039;email_address&#039;, get_string(&#039;emailaddress&#039;, &#039;repository_flickr_public&#039;));&lt;br /&gt;
        $mform-&amp;gt;addRule(&#039;email_address&#039;, get_string(&#039;required&#039;), &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So at this moment all our Flickr Public Repositories will have a specific email address. However this is not enough. In order to communicate with Flickr, Moodle needs to know a Flickr API key (http://www.flickr.com/services/api/). This API key is the same for any repository. We could add it with the email address setting but the administrator would have to enter the same API key for every repository. Hopefully the administrator can add settings to the plugin level, impacting all repositories. The code is similar the repository instance settings:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
//We tell the API that the repositories have general settings: &amp;quot;api_key&amp;quot;&lt;br /&gt;
    public static function get_type_option_names() {&lt;br /&gt;
        return array(&#039;api_key&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
//We add an &amp;quot;api key&amp;quot; text box to the create/edit repository plugin Moodle form (also called a Repository type Moodle form)&lt;br /&gt;
    public function type_config_form(&amp;amp;$mform) {&lt;br /&gt;
        //the following line is needed in order to retrieve the API key value from the database when Moodle displays the edit form&lt;br /&gt;
        $api_key = get_config(&#039;flickr_public&#039;, &#039;api_key&#039;);&lt;br /&gt;
        $mform-&amp;gt;addElement(&#039;text&#039;, &#039;api_key&#039;, get_string(&#039;apikey&#039;, &#039;repository_flickr_public&#039;), &lt;br /&gt;
                           array(&#039;value&#039;=&amp;gt;$api_key,&#039;size&#039; =&amp;gt; &#039;40&#039;));&lt;br /&gt;
        $mform-&amp;gt;addRule(&#039;api_key&#039;, get_string(&#039;required&#039;), &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Have we finished yet?&lt;br /&gt;
&lt;br /&gt;
Yes! We have created everything necessary for the administration pages. But let&#039;s go further. It would be good if the user can enter any &amp;quot;Flickr public account email address&amp;quot; in the file picker. In fact we want to display in the file picker a Flickr Public repository that the Moodle administrator can never delete. Let&#039;s add:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
     //this function is only called one time, when the Moodle administrator add the Flickr Public Plugin into the Moodle site.&lt;br /&gt;
     public static function plugin_init() {&lt;br /&gt;
        //here we create a default repository instance. The last parameter is 1 in order to set the instance as readonly.&lt;br /&gt;
        repository_static_function(&#039;flickr_public&#039;,&#039;create&#039;, &#039;flickr_public&#039;, 0, get_system_context(), &lt;br /&gt;
                                    array(&#039;name&#039; =&amp;gt; &#039;default instance&#039;,&#039;email_address&#039; =&amp;gt; null),1);&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That&#039;s all - the administration part of our Flickr Public plugin is done. For your information, Box.net, Flickr, and Flickr Public all have similar administration APIs.&lt;br /&gt;
&lt;br /&gt;
===Functions===&lt;br /&gt;
All of the following functions are optional. If they&#039;re not implemented, your plugin will not have manual settings and will have only one instance displayed in the File Picker (The repository API creates this unique instance when the administrator add the plugin).&lt;br /&gt;
&lt;br /&gt;
==== get_instance_option_names====&lt;br /&gt;
&#039;&#039;This function must be declared static&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
Return an array of strings. These strings are setting names. These settings are specific to an instance.&lt;br /&gt;
If the function returns an empty array, the API will consider that the plugin displays only one repository in the file picker.&lt;br /&gt;
Parent function returns an empty array.&lt;br /&gt;
&lt;br /&gt;
====instance_config_form(&amp;amp;$mform)====&lt;br /&gt;
This is for modifying the Moodle form displaying the settings specific to an instance.&lt;br /&gt;
&lt;br /&gt;
For example, to add a required text box called email_address:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mform-&amp;gt;addElement(&#039;text&#039;, &#039;email_address&#039;, get_string(&#039;emailaddress&#039;, &#039;repository_flickr_public&#039;));&lt;br /&gt;
$mform-&amp;gt;addRule(&#039;email_address&#039;, $strrequired, &#039;required&#039;, null, &#039;client&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&#039;&#039;Note: &#039;&#039;mform&#039;&#039; has by default a name text box (cannot be removed).&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Parent function does nothing.&lt;br /&gt;
&lt;br /&gt;
====get_type_option_names====&lt;br /&gt;
&#039;&#039;This function must be declared static&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
Return an array of string. These strings are setting names. These settings are shared by all instances.&lt;br /&gt;
Parent function return an empty array.&lt;br /&gt;
&lt;br /&gt;
====type_config_form(&amp;amp;$mform)====&lt;br /&gt;
This is for modifying the Moodle form displaying the plugin settings.&lt;br /&gt;
Similar to &#039;&#039;instance_config_form(&amp;amp;$mform)&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
====plugin_init()====&lt;br /&gt;
&#039;&#039;This function must be declared static&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
This function is called when the administrator adds the plugin. So unless the administrator deletes the plugin and re-adds it, it should be called only once.&lt;br /&gt;
Parent function does nothing.&lt;br /&gt;
&lt;br /&gt;
==File picker APIs==&lt;br /&gt;
=== Quick Start ===&lt;br /&gt;
&#039;&#039;&#039;To be completed&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
First of all, the File Picker using intensively Ajax you will need a easy way to debug. Install FirePHP (MDL-16371) and make it works. It will save you a lot of time. (You might give the [http://moodle.org/mod/forum/discuss.php?d=119961 FirePHP plugin for Moodle] a try, it&#039;s still work in progress, though.)&lt;br /&gt;
&lt;br /&gt;
Your first question when you write your plugin specification is &#039;Does the user need to log-in&#039;?&amp;lt;br&amp;gt;&lt;br /&gt;
.....&lt;br /&gt;
&lt;br /&gt;
For most of plugins, you need to establish a connection with the remote repository. This connection can be done into the get_listing(), constructor() function...&amp;lt;br&amp;gt;&lt;br /&gt;
.....&lt;br /&gt;
&lt;br /&gt;
You wanna display a list of files once the user is logged&amp;lt;br&amp;gt;&lt;br /&gt;
.... get_listing() .....&lt;br /&gt;
&lt;br /&gt;
You wanna retrieve the file that the user selected&amp;lt;br&amp;gt;&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
Optional question that you should ask yourself is &#039;Does the user can execute a search&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
.... search() ....&lt;br /&gt;
&lt;br /&gt;
===Functions you *MUST* override===&lt;br /&gt;
====__construct====&lt;br /&gt;
You may initialize your plugin here, such as:&lt;br /&gt;
# Get options from database&lt;br /&gt;
# Get user name and password from HTTP POST&lt;br /&gt;
&lt;br /&gt;
====get_listing($path=&amp;quot;&amp;quot;, $page=&amp;quot;&amp;quot;)====&lt;br /&gt;
This function will return a list of files, the list must be a array like this:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$list = array(&lt;br /&gt;
 //this will be used to build navigation bar&lt;br /&gt;
&#039;path&#039;=&amp;gt;array(array(&#039;name&#039;=&amp;gt;&#039;root&#039;,&#039;path&#039;=&amp;gt;&#039;/&#039;), array(&#039;name&#039;=&amp;gt;&#039;subfolder&#039;, &#039;path&#039;=&amp;gt;&#039;/subfolder&#039;)),&lt;br /&gt;
&#039;manage&#039;=&amp;gt;&#039;http://webmgr.moodle.com&#039;,&lt;br /&gt;
&#039;list&#039;=&amp;gt; array(&lt;br /&gt;
    array(&#039;title&#039;=&amp;gt;&#039;filename1&#039;, &#039;date&#039;=&amp;gt;&#039;01/01/2009&#039;, &#039;size&#039;=&amp;gt;&#039;10MB&#039;, &#039;source&#039;=&amp;gt;&#039;http://www.moodle.com/dl.rar&#039;),&lt;br /&gt;
    array(&#039;title&#039;=&amp;gt;&#039;folder&#039;, &#039;date&#039;=&amp;gt;&#039;01/01/2009&#039;, &#039;size&#039;=&amp;gt;&#039;0&#039;, &#039;children&#039;=&amp;gt;array())&lt;br /&gt;
)&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;The full specification of list element:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 array(&lt;br /&gt;
   // Used to build navegation bar, so you need to include all parents folders&lt;br /&gt;
   // array(array(&#039;name&#039;=&amp;gt;&#039;root&#039;,&#039;path&#039;=&amp;gt;&#039;/&#039;), array(&#039;name&#039;=&amp;gt;&#039;subfolder&#039;, &#039;path&#039;=&amp;gt;&#039;/subfolder&#039;))&lt;br /&gt;
   &#039;path&#039; =&amp;gt; (array) this will be used to build navigation bar&lt;br /&gt;
   // dynload tells file picker to fetch list dynamically, when user click&lt;br /&gt;
   // the folder, it will send a ajax request to server side.&lt;br /&gt;
   // if you are using pagination, &#039;page&#039; and &#039;pages&#039; parameters should be used&lt;br /&gt;
   // and note, you&#039;d better don&#039;t use pagination and page at the same time&lt;br /&gt;
   &#039;page&#039; =&amp;gt; (int) which page is this list&lt;br /&gt;
   &#039;pages&#039; =&amp;gt; (pages) how many pages&lt;br /&gt;
   &#039;dynload&#039; =&amp;gt; (bool) use dynamic loading,&lt;br /&gt;
   // will display a link in file picker&lt;br /&gt;
   &#039;manage&#039; =&amp;gt; (string) url of the file manager,&lt;br /&gt;
   // set to true, the login link will be removed from file picker&lt;br /&gt;
   &#039;nologin&#039; =&amp;gt; (bool) requires login,&lt;br /&gt;
   // set to true, the search link will be removed from file picker&lt;br /&gt;
   &#039;nosearch&#039; =&amp;gt; (bool) no search link,&lt;br /&gt;
   // set this option will display a upload form in file picker&lt;br /&gt;
   // only used in upload plugin currently&lt;br /&gt;
   &#039;upload&#039; =&amp;gt; array( // upload manager&lt;br /&gt;
     &#039;label&#039; =&amp;gt; (string) label of the form element,&lt;br /&gt;
     &#039;id&#039; =&amp;gt; (string) id of the form element&lt;br /&gt;
   ),&lt;br /&gt;
   // file picker will build a file tree according this &lt;br /&gt;
   // list&lt;br /&gt;
   &#039;list&#039; =&amp;gt; array(&lt;br /&gt;
     array( // file&lt;br /&gt;
       &#039;title&#039; =&amp;gt; (string) file name,&lt;br /&gt;
       &#039;shorttitle&#039; =&amp;gt; (string) optional, if you prefer to display a short title&lt;br /&gt;
       &#039;date&#039; =&amp;gt; (string) file last modification time, usually userdate(...),&lt;br /&gt;
       &#039;size&#039; =&amp;gt; (int) file size,&lt;br /&gt;
       &#039;thumbnail&#039; =&amp;gt; (string) url to thumbnail for the file,&lt;br /&gt;
       &#039;thumbnail_width&#039; =&amp;gt; (int) the width of the thumbnail image,&lt;br /&gt;
       &#039;source&#039; =&amp;gt; plugin-dependent unique path to the file (id, url, path, etc.),&lt;br /&gt;
       &#039;url&#039;=&amp;gt; the accessible url of file&lt;br /&gt;
     ),&lt;br /&gt;
     array( // folder - same as file, but no &#039;source&#039;.&lt;br /&gt;
       &#039;title&#039; =&amp;gt; (string) folder name,&lt;br /&gt;
       &#039;shorttitle&#039; =&amp;gt; (string) optional, if you prefer to display a short title&lt;br /&gt;
       &#039;path&#039; =&amp;gt; (string) path to this folder&lt;br /&gt;
       &#039;date&#039; =&amp;gt; (string) folder last modification time, usually userdate(...),&lt;br /&gt;
       &#039;size&#039; =&amp;gt; 0,&lt;br /&gt;
       &#039;thumbnail&#039; =&amp;gt; (string) url to thumbnail for the folder,&lt;br /&gt;
       &#039;children&#039; =&amp;gt; array( // an empty folder needs to have &#039;children&#039; defined, but empty.&lt;br /&gt;
         // content (files and folders)&lt;br /&gt;
       )&lt;br /&gt;
     ),&lt;br /&gt;
   )&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Dynamically loading&lt;br /&gt;
Some repositories contain many files which cannot load in one time, in this case, we need dynamically loading to fetch them step by step, files in subfolder won&#039;t be listed until user click the folder in file picker treeview.&lt;br /&gt;
&lt;br /&gt;
As a plug-in developer, if you set dynload flag as &#039;&#039;&#039;true&#039;&#039;&#039;, you should return files and folders (set children as a null array) in current path instead of building the whole file tree.&lt;br /&gt;
&lt;br /&gt;
Example of dynamically loading&lt;br /&gt;
See [http://cvs.moodle.org/moodle/repository/alfresco/lib.php?view=log Alfresco] plug-in&lt;br /&gt;
&lt;br /&gt;
===Functions you can override===&lt;br /&gt;
====print_login====&lt;br /&gt;
This function will help to print a login form, for the Ajax file picker, this function will return a&lt;br /&gt;
PHP array to define this form.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    public function print_login(){&lt;br /&gt;
        if ($this-&amp;gt;options[&#039;ajax&#039;]) {&lt;br /&gt;
            $user_field-&amp;gt;label = get_string(&#039;username&#039;, &#039;repository_boxnet&#039;).&#039;: &#039;;&lt;br /&gt;
            $user_field-&amp;gt;id    = &#039;box_username&#039;;&lt;br /&gt;
            $user_field-&amp;gt;type  = &#039;text&#039;;&lt;br /&gt;
            $user_field-&amp;gt;name  = &#039;boxusername&#039;;&lt;br /&gt;
            $user_field-&amp;gt;value = $ret-&amp;gt;username;&lt;br /&gt;
            &lt;br /&gt;
            $passwd_field-&amp;gt;label = get_string(&#039;password&#039;, &#039;repository_boxnet&#039;).&#039;: &#039;;&lt;br /&gt;
            $passwd_field-&amp;gt;id    = &#039;box_password&#039;;&lt;br /&gt;
            $passwd_field-&amp;gt;type  = &#039;password&#039;;&lt;br /&gt;
            $passwd_field-&amp;gt;name  = &#039;boxpassword&#039;;&lt;br /&gt;
&lt;br /&gt;
            $ret = array();&lt;br /&gt;
            $ret[&#039;login&#039;] = array($user_field, $passwd_field);&lt;br /&gt;
            // if you are going to rename submit button label, use this option&lt;br /&gt;
            //$ret[&#039;login_btn_label&#039;] = get_string(&#039;submit_btn_label&#039;);&lt;br /&gt;
            // if you are going to display a search form instead of login form&lt;br /&gt;
            // set this option to true&lt;br /&gt;
            //$ret[&#039;login_search_form&#039;] = true;&lt;br /&gt;
            return $ret;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
This will help to generate a form by file picker which contains user name and password input elements.&lt;br /&gt;
&lt;br /&gt;
If your plugin don&#039;t require logging in, you don&#039;t need to override it, it will call get_listing to list files automatically by default.&lt;br /&gt;
====check_login====&lt;br /&gt;
This function will return a boolean value to tell Moodle whether the user has logged in.&lt;br /&gt;
By default, this function will return true.&lt;br /&gt;
====logout====&lt;br /&gt;
When a user clicks the logout button in file picker, this function will be called. You may clean up the session or disconnect the connection with remote server here.&lt;br /&gt;
====print_search====&lt;br /&gt;
When a user clicks the search button on file picker, this function will be called to return a search form. By default, it will create a form with single search bar - you can override it to create a advanced search form.&lt;br /&gt;
====search====&lt;br /&gt;
This function will do the searching job. You can obtain the POST parameters from the from the form you created in print_search function&lt;br /&gt;
This function will return a file list exactly like the one from get_listing.&lt;br /&gt;
====get_file====&lt;br /&gt;
When a user clicks the &amp;quot;Get&amp;quot; button to transfer the file, this function will be called. Basically, it will download a file to Moodle - you can override it to modify the file then move it to a better location.&lt;br /&gt;
====get_name====&lt;br /&gt;
This function will return the name of the repository instance.&lt;br /&gt;
&lt;br /&gt;
== I18n - Internationalization ==&lt;br /&gt;
These following strings are required in &#039;&#039;moodle/repository/myplugin/lang/en_utf8/repository_myplugin.php&#039;&#039; or &#039;&#039;moodle/lang/en_utf8/repository_myplugin.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$string[&#039;configplugin&#039;] = &#039;Flickr Public configuration&#039;;&lt;br /&gt;
$string[&#039;repositorydesc&#039;] = &#039;A Flickr public repository&#039;;&lt;br /&gt;
$string[&#039;repositoryname&#039;] = &#039;Flickr Public&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Standard repository plugins ==&lt;br /&gt;
This is the functional specification list of the officially supported repository plugins.&lt;br /&gt;
For each plugins, the two mains part we are interested in are:&lt;br /&gt;
* How do I administrate the plugin? See [[Development:Repository_Administration_Specification| Repository Administration Specification - UC001-3]]&lt;br /&gt;
* How do I set up an account for this repository? See [[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]]&lt;br /&gt;
&lt;br /&gt;
=== Functional specifications ===&lt;br /&gt;
*[[Development:Box.net Repository Plugin|Box.net Repository Plugin]]&lt;br /&gt;
*[[Development:Flickr Repository Plugin|Flickr Repository Plugin]]&lt;br /&gt;
*[[Development:Moodle Repository Plugin|Remote Moodle Repository Plugin]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
*[[Development:Repository API| Repository API]]&lt;br /&gt;
*[[Development:Repository_Interface_for_Moodle/Course/User| Repository Interface for Moodle/Course/User]]&lt;br /&gt;
*[[QA:Use Case Number Attribution| Use Case Number Attribution]]&lt;br /&gt;
* MDL-16543 - A list of officially supported repository plugins&lt;br /&gt;
* MDL-16543 - Template plugin for developers&lt;br /&gt;
[[Category:Repositories]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/How_Moodle_outputs_HTML&amp;diff=80090</id>
		<title>Broken/How Moodle outputs HTML</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/How_Moodle_outputs_HTML&amp;diff=80090"/>
		<updated>2011-01-06T02:17:54Z</updated>

		<summary type="html">&lt;p&gt;Agwells: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;THIS PAGE IS OUTDATED, DO NOT USE!&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
(What should you use instead?)&lt;br /&gt;
&lt;br /&gt;
If you think about the HTML that is output by Moodle, then you can split it into two parts:&lt;br /&gt;
# the parts like the header and the user-interface, that are generated automatically by the Moodle code and the themes; and&lt;br /&gt;
# the bits like the contents of forum posts and labels that are input by the users.&lt;br /&gt;
This page is about the bits of output that are generated automatically. It explains how that happens.&lt;br /&gt;
&lt;br /&gt;
==History==&lt;br /&gt;
&lt;br /&gt;
Most of what is described here is new in Moodle 2.0. The work that lead to this new system is described in [[Development:Theme engines for Moodle?]] and [[Development:Navigation 2.0 implementation plan]]. Those documents were both written before the new system was developed and shows how the thinking evolved and the rationale behind it.&lt;br /&gt;
&lt;br /&gt;
If you are familiar with Moodle 1.9 or before, and just wish to update your existing code, you may wish to read [[Development:Migrating your code to the 2.0 rendering API]].&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
I said above that this page is about the parts of the output that are automatically generated by Moodle code. We can break those bits of output down further:&lt;br /&gt;
# The overall layout of the page including the header and the footer. This is generated from the layout template provided by the theme (for example theme/standard/layout.php).&lt;br /&gt;
# The blocks that can appear in various places on the page. Where the blocks can appear on the page is determined by the theme layout. Which blocks appear where on which pages is configured by the user. The code that manages this is in blocklib.php. Finally, the contents of the blocks is a mixture of user-entered content (for example, the HTML block) and automatically generated content (for example, the Administration block).&lt;br /&gt;
# The main content in the middle of the page. This is generated by the particular script, for example mod/forum/view.php using the output libraries. (Moodle mostly uses a transaction script architecture.) This area frequently contains user-entered content too.&lt;br /&gt;
&lt;br /&gt;
==Basic HTML output and $OUTPUT==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Themes==&lt;br /&gt;
&lt;br /&gt;
Themes (along with some of the other the configuration choices made by the administrator) control the overall look of a Moodle site.&lt;br /&gt;
&lt;br /&gt;
A Moodle theme is a folder of code like theme/mytheme. It contains a number of files:&lt;br /&gt;
; config.php&lt;br /&gt;
: tells Moodle the settings that this theme wants.&lt;br /&gt;
; layout.php, layout-head.php, ...&lt;br /&gt;
: defines the overall layout of the page. Different types of page can have different templates. This is controlled by config.php.&lt;br /&gt;
; styles.css, styles_colours.css, ...&lt;br /&gt;
: the stylesheets that determine the layout. It is up to the theme whether to have one or many.&lt;br /&gt;
; rtl.css&lt;br /&gt;
: A special stylesheet that is only included if the current language is right-to-left.&lt;br /&gt;
; readme.html, screenshot.jpg &lt;br /&gt;
: Information about the theme. Used in the theme chooser user interface.&lt;br /&gt;
; favicon.ico, pix/*&lt;br /&gt;
: Images that the theme wants to use. These can replace the default Moodle icons.&lt;br /&gt;
; meta.php&lt;br /&gt;
: A way to add information to the page header. Somewhat an implementation detail.&lt;br /&gt;
; styles.php&lt;br /&gt;
: Implementation detail. Responsible for serving the CSS.&lt;br /&gt;
&lt;br /&gt;
===Theme config.php===&lt;br /&gt;
&lt;br /&gt;
===Layout templates===&lt;br /&gt;
&lt;br /&gt;
===Ways themes can change other parts of the HTML===&lt;br /&gt;
&lt;br /&gt;
====Basic themes====&lt;br /&gt;
&lt;br /&gt;
====Minor changes to the HTML====&lt;br /&gt;
&lt;br /&gt;
====Templated themes====&lt;br /&gt;
(Experimental)&lt;br /&gt;
&lt;br /&gt;
$THEME gets initialised the frist time you use $OUTPUT, or the first time you refer to $PAGE-&amp;gt;theme&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Blocks==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Plugin-specific output==&lt;br /&gt;
&lt;br /&gt;
$OUTPUT lets themes customise the HTML used for standard things like boxes, but what about places where code echoes HTML directly? Well, in addition to moodle_core_renderer, every plugin should also define its own renderer, like moodle_mod_workshop_renderer, and all  module-specific HTML output should done in methods of that class. That class should be defined in a files called renderers.php inside your plugin. &lt;br /&gt;
&lt;br /&gt;
When you actually want to do some output, you need to get an instance of your class (or whatever subclass the theme wants to substitute). You do that like this:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
global $PAGE; // If necessary.&lt;br /&gt;
$wsoutput = $PAGE-&amp;gt;theme-&amp;gt;get_renderer(&#039;mod_workshop&#039;, $PAGE);&lt;br /&gt;
echo $wsoutput-&amp;gt;manual_allocation_interface($workshop, $allocationdata);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The moodle_mod_workshop_renderer object will have access to a moodle_core_renderer available as $this-&amp;gt;output. You should use this instead of global $OUTPUT, and you should use it. That is, boxes in the workshop should look like boxes elsewhere, so boxes in the workshop should be output with $this-&amp;gt;output-&amp;gt;box().&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How to learn more==&lt;br /&gt;
&lt;br /&gt;
Software normally needs documentation on three levels:&lt;br /&gt;
# An general overview of how all the parts fit together. You&#039;ve just read that!&lt;br /&gt;
# Detailed documentation of the API and how to use it. What functions exist. What arguments they take and what they mean. What is returned. You can find that at http://phpdocs.moodle.org/. All the output code is in the moodlecore package.&lt;br /&gt;
# All the details of how the code actually works. In the open source world, the best way to learn that (after you have read the overview) is to read the code itself. It is the only sort of documentation that never goes out of date!&lt;br /&gt;
&lt;br /&gt;
You should also remember Moodle&#039;s [[Pedagogy|social constructionist pedagogy]]. We learn best by doing, and then showing what we have done to others; we learn by looking at what other people have done; and we learn by asking each other questions. Don&#039;t be afraid to use the [http://moodle.org/course/view.php?id=5 forums] to share what you have done, and to ask for further explanation. This document has already benefited greatly from the questions that Sam Hemelryk and David Mudrak asked when they started working with this code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Development:Migrating your code to the 2.0 rendering API]]&lt;br /&gt;
* [[Development:Theme_engines_for_Moodle%3F]]&lt;br /&gt;
* [[Development:Navigation_2.0_implementation_plan]]&lt;br /&gt;
* [[Development:Navigation_2.0]]&lt;br /&gt;
&lt;br /&gt;
{{CategoryDeveloper}}&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Setting_up_Netbeans&amp;diff=80064</id>
		<title>Setting up Netbeans</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Setting_up_Netbeans&amp;diff=80064"/>
		<updated>2011-01-05T01:59:28Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.netbeans.org/features/php/index.html NetBeans] has got a good PHP support. You find a host of information on the website (tutorials, developer blog, screen casts, etc.).&lt;br /&gt;
&lt;br /&gt;
== Features ==&lt;br /&gt;
* CVS integration: see all changes, lines deletion, diff in real time, show annotations, diff history...&lt;br /&gt;
* Ctrl+Click: Go to declaration&lt;br /&gt;
* Export/Import Diff Patch&lt;br /&gt;
* Easy navigation&lt;br /&gt;
* List of functions&lt;br /&gt;
* Code completion&lt;br /&gt;
* Instant rename&lt;br /&gt;
* HTML, CSS, JavaScript support&lt;br /&gt;
* MySQL manager&lt;br /&gt;
* Quick Search&lt;br /&gt;
* Very few bugs&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
* Download the latest stable version from [http://netbeans.org http://netbeans.org]. Get the bundle that contains only PHP support.&lt;br /&gt;
* Install and run it.&lt;br /&gt;
&lt;br /&gt;
== Set up for Moodle development ==&lt;br /&gt;
&lt;br /&gt;
* Checkout your Moodle project with a &#039;&#039;&#039;CVS client&#039;&#039;&#039; - see [[CVS for Administrators]] or [[Development:CVS for developers|CVS for Developers]].&lt;br /&gt;
&lt;br /&gt;
* Open File &amp;gt; New Project &amp;gt; PHP &amp;gt; PHP Application &amp;gt; Next &lt;br /&gt;
: You&#039;re going to set the project now. &#039;&#039;Name&#039;&#039;, &#039;&#039;Location&#039;&#039; and &#039;&#039;Folder&#039;&#039; are used by NetBeans and are not related to the source code. So you can choose whatever you like, except your source folder. &#039;&#039;Sources&#039;&#039; has to be your checked out Moodle branch/head folder. The rest is clear enough. Don&#039;t forget to choose UTF-8 for &#039;&#039;Default Encoding&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
: Example:&lt;br /&gt;
&lt;br /&gt;
 Project Name:        Moodle 1.9 Stable&lt;br /&gt;
 Project Location:    C:\Users\jerome\Documents\NetBeansProjects&lt;br /&gt;
 Project Folder:      C:\Users\jerome\Documents\NetBeansProjects\Moodle 1.9 Stable&lt;br /&gt;
 Project Sources:     C:\Users\jerome\Projects\branch19_STABLE\moodle&lt;br /&gt;
 Project URL:         http://localhost/moodle19/&lt;br /&gt;
 Index File:          index.php&lt;br /&gt;
 Create:              unchecked&lt;br /&gt;
 Default Encoding:    UTF-8&lt;br /&gt;
 Set as Main Project: unchecked&lt;br /&gt;
&lt;br /&gt;
* Click on Finish.&lt;br /&gt;
&lt;br /&gt;
* Start coding!&lt;br /&gt;
&lt;br /&gt;
== CVS with NetBeans ==&lt;br /&gt;
&lt;br /&gt;
NetBeans comes with &#039;&#039;&#039;integrated CVS support&#039;&#039;&#039; which might be the easiest way to check out Moodle.&lt;br /&gt;
&lt;br /&gt;
=== Anonymous checkout ===&lt;br /&gt;
&lt;br /&gt;
# In NetBeans, select Window-&amp;gt;Versioning-&amp;gt;CVS-&amp;gt;Checkout&lt;br /&gt;
# Select Team-&amp;gt;CVS-&amp;gt;Checkout&lt;br /&gt;
# Enter into CVS Root: &#039;&#039;:pserver:anonymous@us.cvs.moodle.org:/cvsroot/moodle&#039;&#039;&lt;br /&gt;
: (Non-US-residents might use one of the other [https://docs.moodle.org/en/CVS_for_Administrators#CVS_Servers Moodle CVS servers] nearer to them.)&lt;br /&gt;
# Click &#039;&#039;Next&#039;&#039;&lt;br /&gt;
# Browse or enter into &#039;&#039;Module:&#039;&#039; moodle&lt;br /&gt;
# Browse or enter into &#039;&#039;Branch:&#039;&#039; MOODLE_19_STABLE&lt;br /&gt;
# Browse or enter into &#039;&#039;Local Folder:&#039;&#039; C:\xampp\htdocs&lt;br /&gt;
# Click &#039;&#039;Finish&#039;&#039; (and wait a few minutes for Moodle to be checked out)&lt;br /&gt;
# When you get the dialog box &amp;quot;Do you want to create an IDE project from the checked-out sources?&amp;quot;, Click &amp;quot;Create Project...&amp;quot;&lt;br /&gt;
# Select PHP Application with Existing Sources, and click Next&lt;br /&gt;
# Browse or enter into &#039;&#039;Sources Folder&#039;&#039; C:\xampp\htdocs\moodle&lt;br /&gt;
# Enter into &#039;&#039;Project Name:&#039;&#039; moodle&lt;br /&gt;
# Keep the other defaults and click next&lt;br /&gt;
# &#039;&#039;Run As:&#039;&#039; should have selected &#039;&#039;Local Web Site (running on local web server)&#039;&#039;&lt;br /&gt;
# Enter into Project URL http://localhost/moodle/&lt;br /&gt;
# Browse or enter into &#039;&#039;Index File:&#039;&#039; index.php&lt;br /&gt;
# Click &#039;&#039;Finish&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Checkout for developers with write access ===&lt;br /&gt;
&lt;br /&gt;
As far as I can tell, there is no way to get NetBeans to work with CVS keys on Mac/Linux via the system SSH binary, so you will need to use the internal SSH and your password (which you can choose to save).&lt;br /&gt;
&lt;br /&gt;
If you wish to use NetBeans CVS and have CVS write access, the procedure is as follows:&lt;br /&gt;
&lt;br /&gt;
====Checkout of main Moodle codebase====&lt;br /&gt;
&lt;br /&gt;
# Follow the above instructions except for point 3&lt;br /&gt;
# At part 3, substitute &#039;&#039;:ext:yourusername&#039;&#039; for the part before the @ and remove the country code from after it, leaving it like this  &#039;&#039;&#039;&#039;&#039;:ext:yourusername&#039;&#039;&#039;@cvs.moodle.org:/cvsroot/moodle&#039;&#039;&lt;br /&gt;
# Follow the rest of the instructions as above&lt;br /&gt;
&lt;br /&gt;
If you wish to work with a plugin, you will need to check this out separately and add it to the Moodle code project you have just made. This because for some reason, the &#039;Do you want to create a project&#039; option fails to show any of the files once you open it if you try to check out the plugin on its own (NetBeans 6.7.1 on a Mac).&lt;br /&gt;
&lt;br /&gt;
====Checkout of contrib code====&lt;br /&gt;
&lt;br /&gt;
# Follow the above steps up to point 3, and again substitute &#039;&#039;:ext:yourusername&#039;&#039; and click &#039;next&#039;. Note that you should not try to specify the full contrib repository path yet.&lt;br /&gt;
# Click &#039;Browse...&#039; next to the &#039;Module&#039; dialogue&lt;br /&gt;
# Find the plugin you wish to work with in CONTRIB&lt;br /&gt;
# Click &#039;OK&#039;&lt;br /&gt;
# Proceed as before up to point 6, then press &#039;Close&#039; when asked if you want to create a new project.&lt;br /&gt;
# Now navigate to the folder you earlier specified in the &#039;local folder&#039; dialogue and you will find a folder marked &#039;contrib&#039;.&lt;br /&gt;
# navigate to the plugin folder, copy that folder, and then navigate to your main Moodle project folder and paste it where it belongs.&lt;br /&gt;
&lt;br /&gt;
Note that the last three points may cause you some grief when attempting to commit changes. CVS doesn&#039;t seem to like having subfolders with different origins. A workaround that operates well for me is to check out the contrib code manually into a folder using the command line as specified in the CVS for developers instructions, then import that code as a new project with existing sources. This allows easy commits and updates, whilst keeping it separate. You can then make a symlink from you main Moodle project to your contrib directory, restart NetBeans (the CVS info and symlink stuff is only refreshed on restart) and then right click the linked directory and choose &#039;Ignore&#039;, then do the same and choose &#039;Exclude from commit&#039;. You can now use the code as if it were part of Moodle, still getting all the code completion stuff, and also do clean updates and commits from the secondary project. [[User:Matt Gibson|Matt Gibson]] 19:53, 6 June 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
===Adding a branch to your plugin===&lt;br /&gt;
Your plugin will likely start with just a HEAD tag and at some point, you will want to branch it so that you can have versions for different Moodles. To add a MOODLE_20_STABLE branch, for example, do the following.&lt;br /&gt;
&lt;br /&gt;
# Checkout as above, but use the MOODLE_20_STABLE branch instead of MOODLE_19_STABLE&lt;br /&gt;
# You will end up with an empty folder, which you copy into place as above.&lt;br /&gt;
# Find the folder in NetBeans, then right click and choose CVS-&amp;gt;Merge changes from branch...&lt;br /&gt;
# Choose to merge from whatever branch has all the current code, using the help link if not sure what to do.&lt;br /&gt;
&lt;br /&gt;
There is a small chance that the Merge link will not be there, in which case, you will have to copy the files into the directory by hand outside netbeans and then check them in. If you do this, make sure you don&#039;t copy over the &#039;CVS&#039; folders that will have been made when you checked out the MOODLE_19_STABLE code.&lt;br /&gt;
&lt;br /&gt;
=== A few warnings ===&lt;br /&gt;
&lt;br /&gt;
Some of these warnings could also apply to other IDEs:&lt;br /&gt;
&lt;br /&gt;
* If you want to delete a file from your computer but not from CVS, delete it from Windows Explorer/Nautilus/Finder. Otherwise your next commit could delete the file from CVS. &amp;lt;br/&amp;gt;In case you have already deleted a wrong file from NetBeans:  with Windows Explorer/Nautilus/Finder, delete all the folder content of this deleted file (including the CVS folder) and update the folder.&lt;br /&gt;
* If you rename files and that other people are working on them, NetBeans could end up to mess up your CVS folder (even though that is quite rare). Then NetBeans CVS will refuse to update your code displaying a no explicit error as &#039;&#039;&#039;Update Failed&#039;&#039;&#039;. In this case, delete the content of the damaged folder with Windows Explorer/Nautilus/Finder. Then update this folder.&lt;br /&gt;
* If you create a patch or a new PHP file with NetBeans on Microsoft Windows, please check that it&#039;s a Unix format file. You may want to use another software to create Unix format patch/php files.&lt;br /&gt;
&lt;br /&gt;
== Git with NetBeans ==&lt;br /&gt;
&lt;br /&gt;
=== NBGit | Git Support for NetBeans ===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;NBGit is a module for the NetBeans IDE that adds support for working with the Git version control system. It uses the JGit library created as part of EGit to interact with Git repositories. Because the module is Java code all the way, it should work better cross-platform modulo platform specific differences, such as file system behavior. It is based on the NetBeans Mercurial module.&amp;quot; &lt;br /&gt;
(http://nbgit.org)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note orange&amp;quot;&amp;gt;&lt;br /&gt;
Unfortunately, the key word in that quote is &#039;&#039;&#039;should&#039;&#039;&#039;. In reality, because JGit is a rewrite from scratch of the tried-and-tested git core C code, it is still buggy, incomplete and unreliable. Therefore, the NetBeans and Eclipse git plugins are still only beta quality, and pretty sucky. Well, they mostly work for browsing the contents of the repository, but there is a small chance that if you use them for update operations, they will corrupt your repository. Hence, I am still doing git from the command-line.--[[User:Tim Hunt|Tim Hunt]] 11:11, 19 January 2010 (UTC)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optimization ==&lt;br /&gt;
&lt;br /&gt;
1. You may want to run NetBeans with the Sun JDK. NetBeans seems to work a bit better with the [http://java.sun.com/javase/downloads/index.jsp Sun JDK]. You&#039;ll have to edit NetBeans config file. Open netbeans/etc/netbeans.conf. Then uncomment and edit: &lt;br /&gt;
&lt;br /&gt;
 netbeans_jdkhome=&amp;quot;your_JDK_path&amp;quot;&lt;br /&gt;
&lt;br /&gt;
2. To change the NetBeans look and feel run netbeans in the command line with this parameter:&lt;br /&gt;
 &amp;quot;netbeans&amp;quot;  --laf javax.swing.plaf.metal.MetalLookAndFeel &lt;br /&gt;
&lt;br /&gt;
3. If NetBeans starts to slow down, give it more memory&lt;br /&gt;
 &amp;quot;netbeans&amp;quot; -J-Xmx600m&lt;br /&gt;
See FAQ for more [http://wiki.netbeans.org/FaqSettingHeapSize details on memory optimisation].&lt;br /&gt;
&lt;br /&gt;
== Coding faster ==&lt;br /&gt;
&lt;br /&gt;
=== Keyboard shortcuts ===&lt;br /&gt;
(Note: Some shortcuts might not work for PHP development.)&lt;br /&gt;
* [http://www.phpmag.ru/2009/01/23/extremely-usefull-netbeans-shortcuts/ Extremely Useful NetBeans Shortcuts] &lt;br /&gt;
* [http://netbeanside61.blogspot.com/2008/04/top-10-netbeans-ide-keyboard-shortcuts.html Top 10 NetBeans IDE Keyboard Shortcuts I use the most]&lt;br /&gt;
* [http://wiki.netbeans.org/KeymapProfileFor60 NetBeans IDE 6.x Keyboard Shortcuts Specification]&lt;br /&gt;
&lt;br /&gt;
=== PHPUnit support ===&lt;br /&gt;
See [http://blogs.sun.com/netbeansphp/entry/recent_improvements_in_phpunit_support &amp;quot;Recent improvements in PHPUnit-support&amp;quot;] on how to use PHPUnit with NetBeans.&lt;br /&gt;
&lt;br /&gt;
===JIRA support===&lt;br /&gt;
You can install the optional JIRA module in order to be able to interact with the Moodle Tracker from within NetBeans. This has the advantage of avoiding constantly switching back and forth from the browser and works quite well. Go to &#039;&#039;Tools-&amp;gt;plugins-&amp;gt;available plugins&#039;&#039; and search for JIRA. Once installed, right click &#039;issue trackers&#039; in the &#039;services&#039; pane to make a new tracker instance, then enter your details.&lt;br /&gt;
&lt;br /&gt;
== See also: ==&lt;br /&gt;
Moodle forums &lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=112972 NetBeans 6.5 for moodle/PHP/debugging is a better experience v.s Eclipse]&lt;br /&gt;
&lt;br /&gt;
Online resources&lt;br /&gt;
* [http://www.netbeans.org/kb/trails/php.html NetBeans PHP Learning Trail]&lt;br /&gt;
* [http://wiki.netbeans.org/PHP NetBeans PHP Wiki]&lt;br /&gt;
* Sun&#039;s [http://blogs.sun.com/netbeansphp/ NetBeans PHP Team Blog]&lt;br /&gt;
&lt;br /&gt;
Book&lt;br /&gt;
* [http://www.packtpub.com/netbeans-platform-6-8-developers-guide/book NetBeans Platform 6.8 Developer&#039;s Guide] by Jürgen Petri (March 2010), focus on Java and Swing &lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|NetBeans]]&lt;br /&gt;
[[Category:Developer tools|NetBeans]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Setting_up_Netbeans&amp;diff=80063</id>
		<title>Setting up Netbeans</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Setting_up_Netbeans&amp;diff=80063"/>
		<updated>2011-01-05T01:59:07Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.netbeans.org/features/php/index.html NetBeans] has got a good PHP support. You find a host of information on the website (tutorials, developer blog, screen casts, etc.).&lt;br /&gt;
&lt;br /&gt;
== Features ==&lt;br /&gt;
* CVS integration: see all changes, lines deletion, diff in real time, show annotations, diff history...&lt;br /&gt;
* Ctrl+Click: Go to declaration&lt;br /&gt;
* Export/Import Diff Patch&lt;br /&gt;
* Easy navigation&lt;br /&gt;
* List of functions&lt;br /&gt;
* Code completion&lt;br /&gt;
* Instant rename&lt;br /&gt;
* HTML, CSS, JavaScript support&lt;br /&gt;
* MySQL manager&lt;br /&gt;
* Quick Search&lt;br /&gt;
* Very few bugs&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
* Download the latest stable version from [http://netbeans.org]. Get the bundle that contains only PHP support.&lt;br /&gt;
* Install and run it.&lt;br /&gt;
&lt;br /&gt;
== Set up for Moodle development ==&lt;br /&gt;
&lt;br /&gt;
* Checkout your Moodle project with a &#039;&#039;&#039;CVS client&#039;&#039;&#039; - see [[CVS for Administrators]] or [[Development:CVS for developers|CVS for Developers]].&lt;br /&gt;
&lt;br /&gt;
* Open File &amp;gt; New Project &amp;gt; PHP &amp;gt; PHP Application &amp;gt; Next &lt;br /&gt;
: You&#039;re going to set the project now. &#039;&#039;Name&#039;&#039;, &#039;&#039;Location&#039;&#039; and &#039;&#039;Folder&#039;&#039; are used by NetBeans and are not related to the source code. So you can choose whatever you like, except your source folder. &#039;&#039;Sources&#039;&#039; has to be your checked out Moodle branch/head folder. The rest is clear enough. Don&#039;t forget to choose UTF-8 for &#039;&#039;Default Encoding&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
: Example:&lt;br /&gt;
&lt;br /&gt;
 Project Name:        Moodle 1.9 Stable&lt;br /&gt;
 Project Location:    C:\Users\jerome\Documents\NetBeansProjects&lt;br /&gt;
 Project Folder:      C:\Users\jerome\Documents\NetBeansProjects\Moodle 1.9 Stable&lt;br /&gt;
 Project Sources:     C:\Users\jerome\Projects\branch19_STABLE\moodle&lt;br /&gt;
 Project URL:         http://localhost/moodle19/&lt;br /&gt;
 Index File:          index.php&lt;br /&gt;
 Create:              unchecked&lt;br /&gt;
 Default Encoding:    UTF-8&lt;br /&gt;
 Set as Main Project: unchecked&lt;br /&gt;
&lt;br /&gt;
* Click on Finish.&lt;br /&gt;
&lt;br /&gt;
* Start coding!&lt;br /&gt;
&lt;br /&gt;
== CVS with NetBeans ==&lt;br /&gt;
&lt;br /&gt;
NetBeans comes with &#039;&#039;&#039;integrated CVS support&#039;&#039;&#039; which might be the easiest way to check out Moodle.&lt;br /&gt;
&lt;br /&gt;
=== Anonymous checkout ===&lt;br /&gt;
&lt;br /&gt;
# In NetBeans, select Window-&amp;gt;Versioning-&amp;gt;CVS-&amp;gt;Checkout&lt;br /&gt;
# Select Team-&amp;gt;CVS-&amp;gt;Checkout&lt;br /&gt;
# Enter into CVS Root: &#039;&#039;:pserver:anonymous@us.cvs.moodle.org:/cvsroot/moodle&#039;&#039;&lt;br /&gt;
: (Non-US-residents might use one of the other [https://docs.moodle.org/en/CVS_for_Administrators#CVS_Servers Moodle CVS servers] nearer to them.)&lt;br /&gt;
# Click &#039;&#039;Next&#039;&#039;&lt;br /&gt;
# Browse or enter into &#039;&#039;Module:&#039;&#039; moodle&lt;br /&gt;
# Browse or enter into &#039;&#039;Branch:&#039;&#039; MOODLE_19_STABLE&lt;br /&gt;
# Browse or enter into &#039;&#039;Local Folder:&#039;&#039; C:\xampp\htdocs&lt;br /&gt;
# Click &#039;&#039;Finish&#039;&#039; (and wait a few minutes for Moodle to be checked out)&lt;br /&gt;
# When you get the dialog box &amp;quot;Do you want to create an IDE project from the checked-out sources?&amp;quot;, Click &amp;quot;Create Project...&amp;quot;&lt;br /&gt;
# Select PHP Application with Existing Sources, and click Next&lt;br /&gt;
# Browse or enter into &#039;&#039;Sources Folder&#039;&#039; C:\xampp\htdocs\moodle&lt;br /&gt;
# Enter into &#039;&#039;Project Name:&#039;&#039; moodle&lt;br /&gt;
# Keep the other defaults and click next&lt;br /&gt;
# &#039;&#039;Run As:&#039;&#039; should have selected &#039;&#039;Local Web Site (running on local web server)&#039;&#039;&lt;br /&gt;
# Enter into Project URL http://localhost/moodle/&lt;br /&gt;
# Browse or enter into &#039;&#039;Index File:&#039;&#039; index.php&lt;br /&gt;
# Click &#039;&#039;Finish&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Checkout for developers with write access ===&lt;br /&gt;
&lt;br /&gt;
As far as I can tell, there is no way to get NetBeans to work with CVS keys on Mac/Linux via the system SSH binary, so you will need to use the internal SSH and your password (which you can choose to save).&lt;br /&gt;
&lt;br /&gt;
If you wish to use NetBeans CVS and have CVS write access, the procedure is as follows:&lt;br /&gt;
&lt;br /&gt;
====Checkout of main Moodle codebase====&lt;br /&gt;
&lt;br /&gt;
# Follow the above instructions except for point 3&lt;br /&gt;
# At part 3, substitute &#039;&#039;:ext:yourusername&#039;&#039; for the part before the @ and remove the country code from after it, leaving it like this  &#039;&#039;&#039;&#039;&#039;:ext:yourusername&#039;&#039;&#039;@cvs.moodle.org:/cvsroot/moodle&#039;&#039;&lt;br /&gt;
# Follow the rest of the instructions as above&lt;br /&gt;
&lt;br /&gt;
If you wish to work with a plugin, you will need to check this out separately and add it to the Moodle code project you have just made. This because for some reason, the &#039;Do you want to create a project&#039; option fails to show any of the files once you open it if you try to check out the plugin on its own (NetBeans 6.7.1 on a Mac).&lt;br /&gt;
&lt;br /&gt;
====Checkout of contrib code====&lt;br /&gt;
&lt;br /&gt;
# Follow the above steps up to point 3, and again substitute &#039;&#039;:ext:yourusername&#039;&#039; and click &#039;next&#039;. Note that you should not try to specify the full contrib repository path yet.&lt;br /&gt;
# Click &#039;Browse...&#039; next to the &#039;Module&#039; dialogue&lt;br /&gt;
# Find the plugin you wish to work with in CONTRIB&lt;br /&gt;
# Click &#039;OK&#039;&lt;br /&gt;
# Proceed as before up to point 6, then press &#039;Close&#039; when asked if you want to create a new project.&lt;br /&gt;
# Now navigate to the folder you earlier specified in the &#039;local folder&#039; dialogue and you will find a folder marked &#039;contrib&#039;.&lt;br /&gt;
# navigate to the plugin folder, copy that folder, and then navigate to your main Moodle project folder and paste it where it belongs.&lt;br /&gt;
&lt;br /&gt;
Note that the last three points may cause you some grief when attempting to commit changes. CVS doesn&#039;t seem to like having subfolders with different origins. A workaround that operates well for me is to check out the contrib code manually into a folder using the command line as specified in the CVS for developers instructions, then import that code as a new project with existing sources. This allows easy commits and updates, whilst keeping it separate. You can then make a symlink from you main Moodle project to your contrib directory, restart NetBeans (the CVS info and symlink stuff is only refreshed on restart) and then right click the linked directory and choose &#039;Ignore&#039;, then do the same and choose &#039;Exclude from commit&#039;. You can now use the code as if it were part of Moodle, still getting all the code completion stuff, and also do clean updates and commits from the secondary project. [[User:Matt Gibson|Matt Gibson]] 19:53, 6 June 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
===Adding a branch to your plugin===&lt;br /&gt;
Your plugin will likely start with just a HEAD tag and at some point, you will want to branch it so that you can have versions for different Moodles. To add a MOODLE_20_STABLE branch, for example, do the following.&lt;br /&gt;
&lt;br /&gt;
# Checkout as above, but use the MOODLE_20_STABLE branch instead of MOODLE_19_STABLE&lt;br /&gt;
# You will end up with an empty folder, which you copy into place as above.&lt;br /&gt;
# Find the folder in NetBeans, then right click and choose CVS-&amp;gt;Merge changes from branch...&lt;br /&gt;
# Choose to merge from whatever branch has all the current code, using the help link if not sure what to do.&lt;br /&gt;
&lt;br /&gt;
There is a small chance that the Merge link will not be there, in which case, you will have to copy the files into the directory by hand outside netbeans and then check them in. If you do this, make sure you don&#039;t copy over the &#039;CVS&#039; folders that will have been made when you checked out the MOODLE_19_STABLE code.&lt;br /&gt;
&lt;br /&gt;
=== A few warnings ===&lt;br /&gt;
&lt;br /&gt;
Some of these warnings could also apply to other IDEs:&lt;br /&gt;
&lt;br /&gt;
* If you want to delete a file from your computer but not from CVS, delete it from Windows Explorer/Nautilus/Finder. Otherwise your next commit could delete the file from CVS. &amp;lt;br/&amp;gt;In case you have already deleted a wrong file from NetBeans:  with Windows Explorer/Nautilus/Finder, delete all the folder content of this deleted file (including the CVS folder) and update the folder.&lt;br /&gt;
* If you rename files and that other people are working on them, NetBeans could end up to mess up your CVS folder (even though that is quite rare). Then NetBeans CVS will refuse to update your code displaying a no explicit error as &#039;&#039;&#039;Update Failed&#039;&#039;&#039;. In this case, delete the content of the damaged folder with Windows Explorer/Nautilus/Finder. Then update this folder.&lt;br /&gt;
* If you create a patch or a new PHP file with NetBeans on Microsoft Windows, please check that it&#039;s a Unix format file. You may want to use another software to create Unix format patch/php files.&lt;br /&gt;
&lt;br /&gt;
== Git with NetBeans ==&lt;br /&gt;
&lt;br /&gt;
=== NBGit | Git Support for NetBeans ===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;NBGit is a module for the NetBeans IDE that adds support for working with the Git version control system. It uses the JGit library created as part of EGit to interact with Git repositories. Because the module is Java code all the way, it should work better cross-platform modulo platform specific differences, such as file system behavior. It is based on the NetBeans Mercurial module.&amp;quot; &lt;br /&gt;
(http://nbgit.org)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note orange&amp;quot;&amp;gt;&lt;br /&gt;
Unfortunately, the key word in that quote is &#039;&#039;&#039;should&#039;&#039;&#039;. In reality, because JGit is a rewrite from scratch of the tried-and-tested git core C code, it is still buggy, incomplete and unreliable. Therefore, the NetBeans and Eclipse git plugins are still only beta quality, and pretty sucky. Well, they mostly work for browsing the contents of the repository, but there is a small chance that if you use them for update operations, they will corrupt your repository. Hence, I am still doing git from the command-line.--[[User:Tim Hunt|Tim Hunt]] 11:11, 19 January 2010 (UTC)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Optimization ==&lt;br /&gt;
&lt;br /&gt;
1. You may want to run NetBeans with the Sun JDK. NetBeans seems to work a bit better with the [http://java.sun.com/javase/downloads/index.jsp Sun JDK]. You&#039;ll have to edit NetBeans config file. Open netbeans/etc/netbeans.conf. Then uncomment and edit: &lt;br /&gt;
&lt;br /&gt;
 netbeans_jdkhome=&amp;quot;your_JDK_path&amp;quot;&lt;br /&gt;
&lt;br /&gt;
2. To change the NetBeans look and feel run netbeans in the command line with this parameter:&lt;br /&gt;
 &amp;quot;netbeans&amp;quot;  --laf javax.swing.plaf.metal.MetalLookAndFeel &lt;br /&gt;
&lt;br /&gt;
3. If NetBeans starts to slow down, give it more memory&lt;br /&gt;
 &amp;quot;netbeans&amp;quot; -J-Xmx600m&lt;br /&gt;
See FAQ for more [http://wiki.netbeans.org/FaqSettingHeapSize details on memory optimisation].&lt;br /&gt;
&lt;br /&gt;
== Coding faster ==&lt;br /&gt;
&lt;br /&gt;
=== Keyboard shortcuts ===&lt;br /&gt;
(Note: Some shortcuts might not work for PHP development.)&lt;br /&gt;
* [http://www.phpmag.ru/2009/01/23/extremely-usefull-netbeans-shortcuts/ Extremely Useful NetBeans Shortcuts] &lt;br /&gt;
* [http://netbeanside61.blogspot.com/2008/04/top-10-netbeans-ide-keyboard-shortcuts.html Top 10 NetBeans IDE Keyboard Shortcuts I use the most]&lt;br /&gt;
* [http://wiki.netbeans.org/KeymapProfileFor60 NetBeans IDE 6.x Keyboard Shortcuts Specification]&lt;br /&gt;
&lt;br /&gt;
=== PHPUnit support ===&lt;br /&gt;
See [http://blogs.sun.com/netbeansphp/entry/recent_improvements_in_phpunit_support &amp;quot;Recent improvements in PHPUnit-support&amp;quot;] on how to use PHPUnit with NetBeans.&lt;br /&gt;
&lt;br /&gt;
===JIRA support===&lt;br /&gt;
You can install the optional JIRA module in order to be able to interact with the Moodle Tracker from within NetBeans. This has the advantage of avoiding constantly switching back and forth from the browser and works quite well. Go to &#039;&#039;Tools-&amp;gt;plugins-&amp;gt;available plugins&#039;&#039; and search for JIRA. Once installed, right click &#039;issue trackers&#039; in the &#039;services&#039; pane to make a new tracker instance, then enter your details.&lt;br /&gt;
&lt;br /&gt;
== See also: ==&lt;br /&gt;
Moodle forums &lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=112972 NetBeans 6.5 for moodle/PHP/debugging is a better experience v.s Eclipse]&lt;br /&gt;
&lt;br /&gt;
Online resources&lt;br /&gt;
* [http://www.netbeans.org/kb/trails/php.html NetBeans PHP Learning Trail]&lt;br /&gt;
* [http://wiki.netbeans.org/PHP NetBeans PHP Wiki]&lt;br /&gt;
* Sun&#039;s [http://blogs.sun.com/netbeansphp/ NetBeans PHP Team Blog]&lt;br /&gt;
&lt;br /&gt;
Book&lt;br /&gt;
* [http://www.packtpub.com/netbeans-platform-6-8-developers-guide/book NetBeans Platform 6.8 Developer&#039;s Guide] by Jürgen Petri (March 2010), focus on Java and Swing &lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|NetBeans]]&lt;br /&gt;
[[Category:Developer tools|NetBeans]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Podcasting&amp;diff=79136</id>
		<title>Podcasting</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Podcasting&amp;diff=79136"/>
		<updated>2010-12-14T04:17:18Z</updated>

		<summary type="html">&lt;p&gt;Agwells: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Podcasting&#039;&#039;&#039; is an easy way to deliver a series of audio files in such a way that people can subscribe and have all current and future &#039;episodes&#039; downloaded automatically to their computer or media player. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Podcasting in Moodle ==&lt;br /&gt;
&lt;br /&gt;
There are two possibilities for podcasting using Moodle:&lt;br /&gt;
&lt;br /&gt;
* Starting from Moodle 1.6, simply use the discussion forums tool! If you create a discussion forum and activate RSS feeds for the forum, you can simply post messages with media files as attachments. These will be delivered as podcasts in the [[RSS in forums|RSS feed]]. (&#039;&#039;Note:&#039;&#039; This works in some situations and not in others, possibly based on whether or not guest users are able to view the forum. Otherwise the media files may not be accessible by the podcast-receiving software.)&lt;br /&gt;
* Use the optional [[Ipodcast module]] which creates a specific podcasting activity type in Moodle. The advantage of this method is that it includes extra &#039;&#039;metadata&#039;&#039; designed to work well with Apple&#039;s &#039;&#039;iTunes&#039;&#039; software (such as keywords and category labels).&lt;br /&gt;
* Or the &amp;quot;Podcaster&amp;quot; module: http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=2160&lt;br /&gt;
&lt;br /&gt;
== Podcasting about Moodle ==&lt;br /&gt;
&lt;br /&gt;
You may also like to know that a podcast &#039;&#039;about&#039;&#039; Moodle is in preparation, tentatively called &amp;quot;[[Portal:mCast|mCast]]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Software for receiving podcasts ==&lt;br /&gt;
&lt;br /&gt;
* Apple&#039;s [http://www.apple.com/itunes/ iTunes] is currently one of the more popular pieces of software for subscribing to podcasts &lt;br /&gt;
* [http://juicereceiver.sourceforge.net/ Juice] (previously known as &#039;&#039;iPodder&#039;&#039;) which is open-source and available on Windows and Macintosh.&lt;br /&gt;
* [http://getsongbird.com/ Songbird], a truly cross-platform, Open Source media player.&lt;br /&gt;
* [http://www.gnome.org/projects/rhythmbox/ Rhythmbox] for the Gnome Desktop&lt;br /&gt;
* [http://amarok.kde.org/ Amarok] for KDE&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=7230 Podcasting module forum]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Podcasting Wikipedia article on podcasting]&lt;br /&gt;
* [http://www.apple.com/podcasting Apple webpage on podcasting]&lt;br /&gt;
*[[Audio in Moodle]]&lt;br /&gt;
*[[iTunes university block]] contributed code that links to iTunes U&lt;br /&gt;
&lt;br /&gt;
[[Category:RSS]]&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Audio]]&lt;br /&gt;
&lt;br /&gt;
[[pl:Podcasting]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Moodle_myths&amp;diff=76826</id>
		<title>Moodle myths</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Moodle_myths&amp;diff=76826"/>
		<updated>2010-10-18T04:02:40Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* The Total Cost of Ownership is actually higher for Moodle than it would be with a wholly commercial platform */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{About Moodle}}&lt;br /&gt;
Our top 10 has expanded.  Sometimes we hear these myths:&lt;br /&gt;
&lt;br /&gt;
==Once Moodle is stable, it will be put under licence. If it were any good, they’d already be charging for it==&lt;br /&gt;
Martin Dougiamas is [http://moodle.org/mod/forum/discuss.php?d=41253 on record] that Moodle will always be free and under the GPL. Even if it weren&#039;t, the community could take the latest GPL code and continue development from there. One of the reasons why Moodle&#039;s so good is that it&#039;s open source code, and so the world wide educational community can contribute to making it better still. &lt;br /&gt;
&lt;br /&gt;
In other cases where such things have happened, the community quickly &amp;quot;forked&amp;quot; the tool and continued it, with ongoing improvements, as an open-source project. What is out there up to this point will stay out there - legally - even if something in the future did not. Nobody can &amp;quot;buy&amp;quot; Moodle, and any coopting without the consent of the global community wouldn&#039;t get very far.&lt;br /&gt;
&lt;br /&gt;
==Moodle needs a full time, php developer on your staff- or at least a lot of technical support to run it in house==&lt;br /&gt;
There are &#039;&#039;plenty&#039;&#039; of institutions running Moodle as is, without any php developers in sight. You don&#039;t need to know any programming if you just want to run an out of the box, as a full featured, rich Moodle site. &lt;br /&gt;
&lt;br /&gt;
That said, PHP is actually a fairly easy language to pick up, and the Moodle code is well documented, so if you did want to help with [[Developer|development]], it&#039;s a fairly gentle learning curve. We have [[Development:Developer documentation|documentation]] and a process to help you.&lt;br /&gt;
&lt;br /&gt;
It is also fair to say you need a certain amount of technical know-how to run any program on the web securely.  But this has more to do with getting a web-server, SQL database and scripting language up and integrated than running a Moodle instance itself. If you can run your own webserver, you should be OK to run Moodle on it. &lt;br /&gt;
&lt;br /&gt;
You don&#039;t actually have to run Moodle in house. There are well respected [[Moodle Partners]] who&#039;ll run Moodle for you. Some of the more enlightened educational consortia (Regional Broadband Consortia in the UK), or some local non-profit authorities may provide Moodle hosting. Moodle will work on plenty of commercially [[Web Hosts|hosted]] webspaces too.&lt;br /&gt;
&lt;br /&gt;
==Moodle won’t be compatible with our other systems/software==&lt;br /&gt;
Moodle will run on FreeBSD, Linux, Mac OS X, Solaris, Windows and many others. It&#039;s compatible with a huge range of databases through ADODB integration. There&#039;s a whole host of authentication and enrollment mechanisms, including LDAP and arbitrary external databases. Moodle will allow teachers to integrate content in a wide range of different formats, including SCORM, Flash, MP3s and RSS feeds. On the [[Roadmap]] for future releases is a Web API which will allow easy integration with other web-based applications. &lt;br /&gt;
&lt;br /&gt;
Finally, remember that this is open source software, with a well documented data and file structure. If Moodle&#039;s not compatible with a particular application at the moment, then you can pay a developer to code up that integration, or develop it in-house.&lt;br /&gt;
&lt;br /&gt;
==Moodle just doesn’t have the commercial experience we’re looking for==&lt;br /&gt;
Check out the [http://moodle.com partners]. Moodle is in use throughout the world by corporate clients for in-house training, including flight schools, pilot and mechanic certification, health professionals and all other varieties of professional development. Remember, Moodle is a tool (an application). The PEOPLE that make up the Moodle world-wide community have experience across the board in every industry and every sort of education setting. In fact, you&#039;ll be hard-pressed to find a more committed group of educators and [[Trainer|trainers]] in one place on the web than on moodle.org. Further evidence of the commercial applications of Moodle are supported by the fact that Microsoft Corporation funded the modification of Moodle to work on their SQL Server platform (if you choose to use that instead of mySQL) and that the support for features ranging from clustering to built-in payment mechanisms is growing with each version.&lt;br /&gt;
&lt;br /&gt;
==You can’t just use Moodle out of the box – the basic Moodle install just isn’t that sophisticated==&lt;br /&gt;
Have a look at the [[Features|feature list]], all of which comes as standard with every Moodle download. Additional themes, blocks and activities are easy to integrate and the vast majority are free, open source code too.  In fact, one of your problems will be to determine which combination of sophisticated features are best going to meet your needs right out of the box.  &lt;br /&gt;
&lt;br /&gt;
You can do a full install on a Windows or Mac OS based personal computer in the time it takes to download a 50MB file, run an install program (less than 10 minutes), and type [[Localhost]]. This install includes a webserver, the database, and the Moodle installation. While this basic install is not appropriate for an enterprise installation, the simplicity of the install along with all its features is a testament to the robustness of the platform.&lt;br /&gt;
&lt;br /&gt;
==There’s no documentation, training or technical support available – you’re on your own==&lt;br /&gt;
There&#039;s excellent documentation [https://docs.moodle.org/en/Main_Page online], provided by the user and developer community. Being online and digital, this resource is updated daily and keeps abreast of Moodle developments as they happen - with far more details than any book could provide, and certainly more than any commercial vendor offers for their product.&lt;br /&gt;
&lt;br /&gt;
There are lots of [[Moodle manuals|books on Moodle]]. A few examples: Jason Cole and Helen Foster have written an excellent introduction to Moodle for teachers, available as a [http://www.amazon.com/gp/product/0596008635/ proper book] from O&#039;Reilly. Additionally, [http://www.packtpub.com/ Packt Publishing] has several other books on Moodle teaching and administration available.&lt;br /&gt;
&lt;br /&gt;
Most users find the Moodle interface intuitive which helps reduce the training requirements.   Many organizations offer Moodle training to their members on line and in face to face settings.  Some Moodle Partners [http://moodle.com/training/ moodle.com] also specialize in training.&lt;br /&gt;
&lt;br /&gt;
High quality, timely technical support is available from the user and developer community in the Using Moodle course on [http://moodle.org moodle.org]. Some LAs and RBCs (Local Authorities and Regional Broadband Consortia in the UK) support Moodle in their areas. Commercial support contracts are available from authorized Moodle Partners [http://moodle.com/support/ moodle.com].&lt;br /&gt;
&lt;br /&gt;
==The Total Cost of Ownership is actually higher for Moodle than it would be with a wholly commercial platform==&lt;br /&gt;
Stop and think for a moment about [[Wikipedia:Total cost of ownership|TCO]]. With both Moodle and commercial platforms, you&#039;ll still need to pay for hosting, support, training and content, one way or another: with Moodle, more of these costs &#039;&#039;can&#039;&#039; be brought in-house, because the code&#039;s open source and Moodle&#039;s great at providing the tools teachers need to write online activities themselves, but that doesn&#039;t mean you have to.&lt;br /&gt;
&lt;br /&gt;
The difference is that with Moodle, there are &#039;&#039;&#039;no&#039;&#039;&#039; licence fees to pay. None, nada, zero. If you must spend money, please help us by making the software better, to improve Moodle for the common good. None of your money needs to go to meet shareholder dividends or pay back the venture capitalists. Furthermore, you&#039;re not exposed to the risks of commercial suppliers unilaterally increasing their licence fees, or going out of business. You are also not &#039;&#039;&#039;restricted&#039;&#039;&#039; by license agreements - you can use it however you like. There isn&#039;t an &amp;quot;Enterprise&amp;quot; version that costs many times more than the basic (but has the features you actually) need - Moodle comes with &#039;&#039;&#039;everything&#039;&#039;&#039; you need.&lt;br /&gt;
&lt;br /&gt;
It&#039;s that Moodle offers significant savings over other applications. For example when the UK government agency [http://www.becta.org.uk Becta] examined the [http://publications.becta.org.uk/display.cfm?resID=25907 Total Cost of Ownership of open-source software] on desktops in UK schools, they found significant savings compared to commercial alternatives. The savings on support costs were particularly impressive. It&#039;s likely that these savings would have been greater still had they examined web-based applications like Moodle.&lt;br /&gt;
&lt;br /&gt;
==Moodle is just no good for an institution as large as mine==&lt;br /&gt;
Does that mean you have more than 30,000 on line students? [http://aprender.unb.br Universidade de Brasília] has 34,000 users, [http://www.sfsu.edu San Francisco State University (SFSU)] has 34,000 active users,  The Austrian Federal Ministry of Education has over 110,000 on [http://www.edumoodle.at/moodle/ their site] and the UK&#039;s Open University has well over 180,000 users. There are plenty of other less than 30,000 user [[Large_installations|institution systems]] officially using Moodle.  &lt;br /&gt;
&lt;br /&gt;
Moodle works for large institutions who also have a large numbers of users.&lt;br /&gt;
&lt;br /&gt;
==Moodle is just not designed to cope with my specific group of learners or customers==&lt;br /&gt;
Moodle&#039;s being used successfully from elementary education, including early years provision, up to higher education, in all subject areas including art, languages, the humanities and mathematics. It&#039;s also established itself in the world of life-long learning, teachers&#039; CPD, corporate and government training environments.&lt;br /&gt;
&lt;br /&gt;
==We have all our stuff on *******, it’s just not worth the hassle of switching to Moodle==&lt;br /&gt;
The switch may not be that much of a hassle, as Moodle will happily import content in a wide range of standard formats, including SCORM, Blackboard and WebCT questions. There are an increasing number of Further and Higher Education institutions that are making the move.&lt;br /&gt;
&lt;br /&gt;
Pedagogically, there&#039;s much to be gained from moving to a VLE which puts social, collaborative learning at the centre, and acknowledges the vital role that learners have to play, as well as providing teachers with the tools that they need to build effective on-line learning communities, rather than just presenting resources and activities. &lt;br /&gt;
&lt;br /&gt;
From a financial perspective, the costs involved in switching to Moodle should be quickly recouped through savings in licence fees.&lt;br /&gt;
&lt;br /&gt;
==Moodle is free and therefore can&#039;t really be as good as something produced by a large company which earns millions in Licence fees every year==&lt;br /&gt;
The fact that Moodle is both free and Free (as in Free Speech) means you are certainly not buying something on blind faith that it will fit your needs, as with most commercial products.&lt;br /&gt;
&lt;br /&gt;
For example, the efforts of the Moodle core team are entirely public. Anyone can watch progress in our issue [[Tracker]], download recently written code and take part in their conversations in the forums. This means that anyone who wants to (and there are literally hundreds that do) can assist in developing either the core code, custom plugins and modules, integrations and themes, or by reporting bugs that appear. A licence fee to a commercial product will not give you access to over 150 contributed code extensions that are in Moodle&#039;s [http://moodle.org/mod/data/view.php?id=6009 modules and plugins database].  Nor will the fee have such a public and transparent issue reporting system as [http://tracker.moodle.org/secure/Dashboard.jspa MoodleTracker].  A License fee will not buy you these outstanding features.&lt;br /&gt;
&lt;br /&gt;
On top of that, many institutions that use Moodle decide to devote some of their own in-house expertise to maintaining parts of the Moodle code, or developing new features. Because Moodle is free, this makes sense. With a commercial product, customization is only made by the company and their price includes all their overheads and profit. Moodle&#039;s community (or customer) direct input gets more bang for the resources spent. And there is no yearly licence fee. &lt;br /&gt;
&lt;br /&gt;
All this open activity and discussion with the entire software application community is a big advantage for the Moodle user. The secretiveness inherent in a proprietary application also stifles customer based innovation and change. In a proprietary for profit company everything is controlled and developed in house. with the goal to maximize profit. As with most Open Source software, Moodle develops much faster for a given amount of cash input than commercial software. &lt;br /&gt;
&lt;br /&gt;
Additionally, a commercial company has to devote significant resources to selling a virtual learning environment product to potential customers (and others).  This translates into less money and focus in meaningful product development.  Moodle has no such overhead, leaving more resources for customer drive development.&lt;br /&gt;
&lt;br /&gt;
Put together, the stable open source core and dozens of custom plugins means that Moodle can be tailored to fit your institutional needs much better than a secretive, one-size-fits-all offering.  These are some of the reasons Moodle has such a large  install base of satisfied users.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
A top 10 list in Moodle Docs  started life in [http://moodle.org/mod/forum/discuss.php?d=33044 a post by Josie Fraser], as part of the 2005-6 [http://helpusgettobett.com HUGToB campaign].  Thanks to Josie and others for their contributions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[es:Los 10 mitos de Moodle]]&lt;br /&gt;
[[fr:Mythes sur Moodle]]&lt;br /&gt;
[[zh:Moodle十大流言]]&lt;br /&gt;
[[ja:Moodle伝説トップ10]]&lt;br /&gt;
[[de:Moodle-Mythen]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76468</id>
		<title>DB layer 2.0 migration docs</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76468"/>
		<updated>2010-10-04T04:55:20Z</updated>

		<summary type="html">&lt;p&gt;Agwells: Noting the upgrade_mod_savepoint() change to upgrade.php&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Development:dmllib 2.0}}{{Moodle_2.0}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Much of the following documentation will not make much sense unless you first read [[Development:XMLDB_Documentation|the XMLDB documentation]]. Please read it first if you would like to join the effort to convert Moodle&#039;s code to the new dmllib.&lt;br /&gt;
&lt;br /&gt;
Also, it&#039;s recommended to read the whole [[wikipedia:Data_Definition_Language|DDL]] and [[wikipedia:Data_Manipulation_Language|DML]] documentation before start with the migration. That will allow you to have some initial knowledge about the new architecture.&lt;br /&gt;
&lt;br /&gt;
This article defines all the changes that need to be performed in Moodle 1.9 code in order to make it run properly under new Moodle 2.0 DB layer. Changes below are grouped into 2 main blocks ([[Development:XMLDB_Documentation|xmldb]] and [[wikipedia:Data_Manipulation_Language|dml]]) to have them organised. Additional minor changes may be required in the [[wikipedia:Data_Definition_Language|ddl]] code, but won&#039;t be documented here.&lt;br /&gt;
&lt;br /&gt;
Although the order of changes showed in this page isn&#039;t mandatory at all, it can be interesting to follow it in the migration progress to be able to understand and learn a bit more about all them. That way, you&#039;ll end up knowing not only what to change but how and why those changes are required.&lt;br /&gt;
&lt;br /&gt;
Each change will be as simple as possible, representing one easy rule to follow to adapt the code. When anything become too complex to be explained as one simple rule, it will contain one link to the [[Development:dmllib_2.0_examples|examples page]].&lt;br /&gt;
&lt;br /&gt;
For any problem in the migration of code, it&#039;s recommended to use the [http://moodle.org/mod/forum/view.php?id=45 Databases forum] at [http://moodle.org moodle.org]. Also if you find any bug in the process, please report it in the [http://tracker.moodle.org/browse/MDL-14679 Moodle Tracker], that way developers will be able to fix it ASAP.&lt;br /&gt;
&lt;br /&gt;
The Glossary module was used as the basis for many of the examples below.&lt;br /&gt;
&lt;br /&gt;
And finally, feel free to complete/fix the list below with the changes you find in the progress of migration, that will certainly help many developers. Thanks!&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== check_db_syntax: One helper script ==&lt;br /&gt;
&lt;br /&gt;
Before start migrating your code to Moodle 2.0, it&#039;s recommended to install and run the [http://cvs.moodle.org/contrib/tools/check_db_syntax/ check_db_syntax.php] script. Simply copy it to the main folder of your plugin and execute it (from command line or via web). I&#039;ll show you the list of old DB usages that need to be transformed following the information below in this article.&lt;br /&gt;
&lt;br /&gt;
If you think that something is missing in the script or have any idea to improve it, feel free to do that yourself, commenting about it in MDL-15237. Thanks!&lt;br /&gt;
&lt;br /&gt;
Also, this (perl-compatible - works in Eclipse, hopefully elsewhere) regex:&lt;br /&gt;
&lt;br /&gt;
 (?&amp;lt;!-&amp;gt;)(?&amp;lt;!function )(?&amp;lt;!\$)\b(?:(?:count|delete|get|insert|update)_record(?!s_csv)|[gs]et_field|record_exists|execute_sql)|\$CFG-&amp;gt;prefix|rs_(?:fetch|close)|(add|strip)slashes(?!_js)&lt;br /&gt;
&lt;br /&gt;
Will find most of the things in your code that need attention, with few false positives.&lt;br /&gt;
&lt;br /&gt;
== XMLDB/DDL changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
&lt;br /&gt;
* When changing DB structures it&#039;s highly recommended to use the [[XMLDB editor|XMLDB Editor]] (Admin-&amp;gt;Development-&amp;gt;XMLDB Editor). It is a safe way to edit install.xml files and to get correct PHP code to be used in upgrade.php scripts.&lt;br /&gt;
* If you have some doubts about the list of changes below, it&#039;s highly recommended to take a look at some code in core modules, blocks... whatever you need. They are a good reference.&lt;br /&gt;
* The most noticeable change (from a migration perspective) in the DDL stuff is that, starting with Moodle 2.0, we have decided to &#039;&#039;&#039;drop support for enum (check constraint) fields completely&#039;&#039;&#039;. See MDL-18577 and [http://moodle.org/mod/forum/discuss.php?d=118852 this discussion]. That implies changes in different parts of the DB stuff (xml files, function parameters, dropping existing enums...) and are commented with more detail below.&lt;br /&gt;
&lt;br /&gt;
=== The changes ===&lt;br /&gt;
* STATEMENTS section was replaced by db/install.php code and db/log.php&lt;br /&gt;
* Few other changes are required in install.xml files (that&#039;s good news!). Although you must know these:&lt;br /&gt;
** The &#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039; attributes present in your install.xml files will be completely ignored both by the DLL generation stuff and by the XMLDB Editor.&lt;br /&gt;
** In Moodle 2.1, those attributes (&#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039;) will be 100% forbidden, so it&#039;s highly recommended to erase them since now (Moodle 2.0).&lt;br /&gt;
** The XMLDB Editor will detect them when loading any install.xml file and will suggest you to fix that easily with one 1-click® option.&lt;br /&gt;
** No matter if you have fixed that with the 1-click® option when loading the files... the [[XMLDB editor|XMLDB Editor]] will save any edited install.xml files without those attributes.&lt;br /&gt;
** If your Moodle 1.9.x code &#039;&#039;&#039;has some enum defined in the database&#039;&#039;&#039;, you will need to create one upgrade block (in db/upgrade.php) to drop it (the enum, not the field! ;-) as part of the migration from Moodle 1.9 to 2.0 by using the new &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; method.&lt;br /&gt;
** Of course, if you don&#039;t use/like the XMLDB Editor, you can erase them manually with something like this:&lt;br /&gt;
 perl -p -e &#039;s/ENUM(VALUES)?=&amp;quot;.*?&amp;quot; //g&#039; &amp;lt; install.xml &amp;gt; install.xml.new&lt;br /&gt;
&lt;br /&gt;
* All upgrade.php scripts, within the main xxxx_upgrade function must have &#039;&#039;&#039;$DB&#039;&#039;&#039; (uppercase) in the globals declaration (along with others if needed). Example (from glossary module):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function xmldb_glossary_upgrade($oldversion=0) {&lt;br /&gt;
    &lt;br /&gt;
    global $CFG, $THEME, $DB;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All upgrade.php scripts, must NOT have &#039;&#039;&#039;$db&#039;&#039;&#039; (lowercase) in the globals declaration. Delete it if present.&lt;br /&gt;
* After the global declaration in the points above, this line must be present (we&#039;ll need it later):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$dbman = $DB-&amp;gt;get_manager(); /// loads ddl manager and xmldb classes&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBTable&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_table&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBField&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_field&#039;&#039;&#039; (with change in params, both $enum and $enumvalues are out from function declaration!)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBIndex&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_index&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBKey&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_key&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addFieldInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_field()&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 7th and 8th parameters) are out from function declaration!)&lt;br /&gt;
* All the &#039;&#039;&#039;addIndexInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_index()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addKeyInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_key()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;setAttributes()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;set_attributes()&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 6th and 7th parameters) are out from function declaration!)&lt;br /&gt;
* All the DDL functions used in upgrade code, must be transformed as detailed below (it&#039;s only about to add &#039;&#039;&#039;&amp;quot;$dbman-&amp;gt;&amp;quot;&#039;&#039;&#039; - without the quotes - before each function call). No changes in parameters are required:&lt;br /&gt;
** table_exists ==&amp;gt; $dbman-&amp;gt;table_exists&lt;br /&gt;
** field_exists ==&amp;gt; $dbman-&amp;gt;field_exists&lt;br /&gt;
** index_exists ==&amp;gt; $dbman-&amp;gt;index_exists&lt;br /&gt;
** find_index_name ==&amp;gt; $dbman-&amp;gt;find_index_name&lt;br /&gt;
** find_check_constraint_name ==&amp;gt; $dbman-&amp;gt;find_check_constraint_name (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** check_constraint_exists ==&amp;gt; $dbman-&amp;gt;check_constraint_exists (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** find_sequence_name ==&amp;gt; &#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see MDL-20349&lt;br /&gt;
** create_table ==&amp;gt; $dbman-&amp;gt;create_table&lt;br /&gt;
** drop_table ==&amp;gt; $dbman-&amp;gt;drop_table&lt;br /&gt;
** rename_table ==&amp;gt; $dbman-&amp;gt;rename_table&lt;br /&gt;
** add_field ==&amp;gt; $dbman-&amp;gt;add_field&lt;br /&gt;
** drop_field ==&amp;gt; $dbman-&amp;gt;drop field&lt;br /&gt;
** rename_field ==&amp;gt; $dbman-&amp;gt;rename_field&lt;br /&gt;
** change_field_type ==&amp;gt; $dbman-&amp;gt;change_field_type&lt;br /&gt;
** change_field_precision =&amp;gt; $dbman-&amp;gt;change_field_precision&lt;br /&gt;
** change_field_unsigned ==&amp;gt; $dbman-&amp;gt;change_field_unsigned&lt;br /&gt;
** change_field_notnull ==&amp;gt; $dbman-&amp;gt;change_field_notnull&lt;br /&gt;
** change_field_enum ==&amp;gt; $dbman-&amp;gt;change_field_enum (&#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; to get rid of remaining ENUMs in code).&lt;br /&gt;
** change_field_default ==&amp;gt; $dbman-&amp;gt;change_field_default&lt;br /&gt;
** add_key ==&amp;gt; $dbman-&amp;gt;add_key&lt;br /&gt;
** drop_key ==&amp;gt; $dbman-&amp;gt;drop_key&lt;br /&gt;
** add_index ==&amp;gt; $dbman-&amp;gt;add_index&lt;br /&gt;
** drop_index ==&amp;gt; $dbman-&amp;gt;drop_index&lt;br /&gt;
* The DDL functions that change things (create/drop/rename/add)_(table/field/index) no longer return boolean. Instead, they throw an exception if they fail. So your upgrade.php no longer needs to use a &#039;&#039;&#039;$result&#039;&#039;&#039; variable, and instead should use the &#039;&#039;&#039;upgrade_mod_savepoint()&#039;&#039;&#039; function:&lt;br /&gt;
&amp;lt;code php&amp;gt;/** old way **/&lt;br /&gt;
if ($result &amp;amp;&amp;amp; $oldversion &amp;lt; 2008061000) {&lt;br /&gt;
    $table = new xmldb_table(&#039;facetoface_submissions&#039;);&lt;br /&gt;
    $field = new xmldb_field(&#039;notificationtype&#039;);&lt;br /&gt;
    $field-&amp;gt;set_attributes(XMLDB_TYPE_INTEGER, &#039;10&#039;, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, &#039;0&#039;, &#039;timemodified&#039;);&lt;br /&gt;
&lt;br /&gt;
    $result = $result &amp;amp;&amp;amp; $dbman-&amp;gt;add_field($table, $field);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/** new way **/&lt;br /&gt;
if ($oldversion &amp;lt; 2008061000) {&lt;br /&gt;
    $table = new xmldb_table(&#039;facetoface_submissions&#039;);&lt;br /&gt;
    $field = new xmldb_field(&#039;notificationtype&#039;);&lt;br /&gt;
    $field-&amp;gt;set_attributes(XMLDB_TYPE_INTEGER, &#039;10&#039;, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, &#039;0&#039;, &#039;timemodified&#039;);&lt;br /&gt;
&lt;br /&gt;
    $dbman-&amp;gt;add_field($table, $field);&lt;br /&gt;
    upgrade_mod_savepoint(true, 2008061000, &#039;facetoface&#039;);&lt;br /&gt;
}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Finally, and not less important, your code (module, block... plugin) must guarantee that it can be upgraded &#039;&#039;&#039;ONLY&#039;&#039;&#039; from Moodle 1.9, so any previous upgrade code can be safely deleted. &#039;&#039;&#039;Moodle 2.0 requires Moodle 1.9&#039;&#039;&#039; to be upgraded, so everybody will run the 1.9 =&amp;gt; 2.0 upgrade (with other paths like 1.8 =&amp;gt; 2.0 not being possible). One good time to clean-up a bit your upgrade code ;-). Don&#039;t forget to take a look to the [[XMLDB editor|XMLDB Editor]] and to core modules to see how [http://cvs.moodle.org/moodle/lib/db/upgrade.php?view=markup upgrade.php] files look like in Moodle 2.0.&lt;br /&gt;
&lt;br /&gt;
== DML changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
* The ENTIRE CODEBASE requires an update of ALL database query function calls. Expect most moodle files to be affected by this change.&lt;br /&gt;
&lt;br /&gt;
* This is the more complex part to migrate to have the code working under Moodle 2.0, not because of the complexity of the changes themselves (90% of them will be really easy), but because you&#039;ll need to triple-check everything works as expected after changes.&lt;br /&gt;
&lt;br /&gt;
* Along the changes below, you&#039;ll find links to some examples that will try to make things easier. Anyway, if you are blocked at any point, please ask in forums or tracker (see links at the beginning of the page). Sure it has a good enough solution.&lt;br /&gt;
&lt;br /&gt;
* Once more it&#039;s highly recommended to take a look to Moodle core code, searching for similar examples. Of course, new meaningful examples are welcome, and also any clarification in the list of changes below. Feel free to do it, this is a wiki!&lt;br /&gt;
&lt;br /&gt;
* Finally, one more explanation: The changes below have been split into three sections. First one, (called &#039;&#039;&#039;&amp;quot;The golden changes&amp;quot;&#039;&#039;&#039;) are modifications that must be applied to ALL the transformations defined in the second section (&#039;&#039;&#039;&amp;quot;The iron changes&amp;quot;&#039;&#039;&#039;). Sure you&#039;ll understand that after reading them (it&#039;s basically a matter of not repeating the golden ones within each iron one, just imagine they are everywhere). Finally other changes details can be found in the &#039;&#039;&#039;&amp;quot;The tin changes&amp;quot;&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
=== The golden changes ===&lt;br /&gt;
&#039;&#039;PLEASE read the API before going crazy with search &amp;amp; replace&#039;&#039;! You can find all the new methods in lib/dml/moodle_database.php. This is essential because some method signatures have changed (params are different), and some method names have even changed (execute_sql() is now execute()).&lt;br /&gt;
&lt;br /&gt;
Each of the golden changes below is given one short name (G1, G2, G3...) for further reference in the rest of the documentation. Let&#039;s go:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Wherever old functions are used (get_record*, get_field*, set_field, insert_record, update_record, count_records*, delete_records, record_exists), the global $DB must be used as the object on which these functions are called (e.g. get_record_select() becomes $DB-&amp;gt;get_record_select()).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
get_record_select($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
$DB-&amp;gt;get_record_select($sql);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: for some functions, the $params array is not the second function parameter. For example, set_field:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Old syntax&lt;br /&gt;
set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, &#039;id&#039;, 1);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$DB-&amp;gt;set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, array(&#039;id&#039; =&amp;gt; 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g2&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All uses of addslashes() &#039;&#039;&#039;must be removed&#039;&#039;&#039;. They are no longer needed&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g3&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G3&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All the functions that used to accept a list of string params in the form &amp;quot;param1, value1, param2, value2&amp;quot; now need to be given an array of key=&amp;gt;value pairs as a replacement for these params. Other params remain as before. Check the new API for any exceptions.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$user = get_record(&amp;quot;user&amp;quot;, &amp;quot;firstname&amp;quot;, &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot;, &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// New syntax:&lt;br /&gt;
global $DB;&lt;br /&gt;
$conditions = array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, $conditions);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: The example above has been written out in full for clarity. You can use the array() directly within the function call, without using a temporary variable, if you prefer:&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
global $DB;&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;) );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g4&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G4&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: rs_fetch_next_record($rs) is deprecated, in favour of the simple foreach($rs as $var). Calls to rs_close() must be replaced by $rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
while($result = rs_fetch_next_record($rs)) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
rs_close();&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
foreach ($rs as $result) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
$rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g5&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G5&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Placeholders must be used for table names. Instead of {$CFG-&amp;gt;prefix}tablename, use {tablename}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {$CFG-&amp;gt;prefix}user&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {user}&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g6&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G6&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: When PHP variables are used in SQL queries, they must be replaced by parameters. You have the choice between two approaches: ordered parameters, or named parameters.&lt;br /&gt;
** Ordered parameters use a simple array of values, which are given to the DML function as $params. The values in the SQL code are simply question marks (?) replacing the values. They are replaced by the DML code one by one, substituting each question mark (?) with the next value in the $params array.&lt;br /&gt;
** Named parameters use an associative array of name =&amp;gt; value pairs as the $params array. The values in the SQL code are replaced with a colon (:) followed by the key associated with the value, in the $params array. Note that named params &#039;&#039;&#039;must be unique&#039;&#039;&#039;, no matter if the value passed is the same.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {$CFG-&amp;gt;prefix}user WHERE firstname = &#039;Peter&#039; AND lastname = &#039;Cantrophus&#039;&amp;quot;;&lt;br /&gt;
$user = get_record_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax: ordered params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;Peter&#039;, &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = ? AND lastname = ?&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
// New syntax: named params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;firstname&#039; =&amp;gt; &#039;Peter&#039;, &#039;lastname&#039; =&amp;gt; &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = :firstname AND lastname = :lastname&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To obtain a value for the LIKE operator, include any % signs into the parameter string. For example, code that was previously &amp;lt;tt&amp;gt;&amp;quot;LIKE &#039;$value%&#039;&amp;quot;&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;&amp;quot;LIKE ?&amp;quot;&amp;lt;/tt&amp;gt; with the parameter &amp;lt;tt&amp;gt;$value.&#039;%&#039;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g7&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G7&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Replacement of the IN(...) syntax: We no longer hard-code this in our SQL queries, we use a function which determines whether the IN() syntax is needed, or, if there is only one value to compare, the equal (=) sign can be used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
$gis = implode(&#039;,&#039;, $depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {$CFG-&amp;gt;prefix}grade_items&lt;br /&gt;
         WHERE id IN ($gis)&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// new syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
list($usql, $params) = $DB-&amp;gt;get_in_or_equal($depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {grade_items}&lt;br /&gt;
         WHERE id $usql&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; to combine the last two, do the G7 IN SQL stuff first to generate the params array, then do something like&lt;br /&gt;
  $param[&#039;firstname&#039;] = &#039;peter&#039;;&lt;br /&gt;
to add the stuff for G6&lt;br /&gt;
&lt;br /&gt;
=== The iron changes ===&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;span id=&amp;quot;I1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;I1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally the &#039;&#039;&#039;sql_substr()&#039;&#039;&#039; function was used without parameters and it returned only the name of the &amp;quot;substring&amp;quot; function to be used under each DB. In Moodle 2.0 and upwards, it has 3 parameters (2 being mandatory) and it returns the complete SQL text to be used when handling substrings. Note that positions in this function are 1-based (first char has index 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$records = get_records_sql(&amp;quot;SELECT &amp;quot; . sql_substr() . &amp;quot;(firstname, 1, 20)&amp;quot; . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$records = $DB-&amp;gt;get_records_sql(&amp;quot;SELECT &amp;quot; . $DB-&amp;gt;sql_substr(&#039;firstname&#039;, 1, 20) . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The tin changes ===&lt;br /&gt;
List of minor changes&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally get_records() and similar functions were returning false if no records found. All these methods are now always returning arrays, empty array in case of no records found. Please note that get_record() still returns false if specified record not found.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
if (!$posts = get_records(&#039;forum_posts&#039;, &#039;parent&#039;, 666)) {&lt;br /&gt;
    $posts = array()&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$posts = $DB-&amp;gt;get_records(&#039;forum_posts&#039;, array(&#039;parent&#039;=&amp;gt;666));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally DML functions were returning &#039;&#039;false&#039;&#039; if error occurred - &#039;&#039;dml_exception&#039;&#039; is thrown now instead.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
if (!$id = insert_record(&#039;sometable&#039;, $record)) {&lt;br /&gt;
   error(&#039;can not insert new record&#039;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
$id = $DB-&amp;gt;insert_record(&#039;sometable&#039;, $record);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB Documentation|XMLDB Documentation]]: where both xmldb and ddl stuff is explained.&lt;br /&gt;
* [[Development:DDL functions|DDL functions]] - Documentation for all the Data Definition Language (DDL) functions available inside Moodle.&lt;br /&gt;
* [[Development:DML functions|DML functions]] - Documentation for all the Data Manipulation Language (DML) functions available inside Moodle.&lt;br /&gt;
* [[Development:DDL exceptions|DDL exceptions]] - DDL exceptions information.&lt;br /&gt;
* [[Development:DML exceptions|DML exceptions]] - DML exceptions information.&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76467</id>
		<title>DB layer 2.0 migration docs</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76467"/>
		<updated>2010-10-04T03:39:49Z</updated>

		<summary type="html">&lt;p&gt;Agwells: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Development:dmllib 2.0}}{{Moodle_2.0}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Much of the following documentation will not make much sense unless you first read [[Development:XMLDB_Documentation|the XMLDB documentation]]. Please read it first if you would like to join the effort to convert Moodle&#039;s code to the new dmllib.&lt;br /&gt;
&lt;br /&gt;
Also, it&#039;s recommended to read the whole [[wikipedia:Data_Definition_Language|DDL]] and [[wikipedia:Data_Manipulation_Language|DML]] documentation before start with the migration. That will allow you to have some initial knowledge about the new architecture.&lt;br /&gt;
&lt;br /&gt;
This article defines all the changes that need to be performed in Moodle 1.9 code in order to make it run properly under new Moodle 2.0 DB layer. Changes below are grouped into 2 main blocks ([[Development:XMLDB_Documentation|xmldb]] and [[wikipedia:Data_Manipulation_Language|dml]]) to have them organised. Additional minor changes may be required in the [[wikipedia:Data_Definition_Language|ddl]] code, but won&#039;t be documented here.&lt;br /&gt;
&lt;br /&gt;
Although the order of changes showed in this page isn&#039;t mandatory at all, it can be interesting to follow it in the migration progress to be able to understand and learn a bit more about all them. That way, you&#039;ll end up knowing not only what to change but how and why those changes are required.&lt;br /&gt;
&lt;br /&gt;
Each change will be as simple as possible, representing one easy rule to follow to adapt the code. When anything become too complex to be explained as one simple rule, it will contain one link to the [[Development:dmllib_2.0_examples|examples page]].&lt;br /&gt;
&lt;br /&gt;
For any problem in the migration of code, it&#039;s recommended to use the [http://moodle.org/mod/forum/view.php?id=45 Databases forum] at [http://moodle.org moodle.org]. Also if you find any bug in the process, please report it in the [http://tracker.moodle.org/browse/MDL-14679 Moodle Tracker], that way developers will be able to fix it ASAP.&lt;br /&gt;
&lt;br /&gt;
The Glossary module was used as the basis for many of the examples below.&lt;br /&gt;
&lt;br /&gt;
And finally, feel free to complete/fix the list below with the changes you find in the progress of migration, that will certainly help many developers. Thanks!&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== check_db_syntax: One helper script ==&lt;br /&gt;
&lt;br /&gt;
Before start migrating your code to Moodle 2.0, it&#039;s recommended to install and run the [http://cvs.moodle.org/contrib/tools/check_db_syntax/ check_db_syntax.php] script. Simply copy it to the main folder of your plugin and execute it (from command line or via web). I&#039;ll show you the list of old DB usages that need to be transformed following the information below in this article.&lt;br /&gt;
&lt;br /&gt;
If you think that something is missing in the script or have any idea to improve it, feel free to do that yourself, commenting about it in MDL-15237. Thanks!&lt;br /&gt;
&lt;br /&gt;
Also, this (perl-compatible - works in Eclipse, hopefully elsewhere) regex:&lt;br /&gt;
&lt;br /&gt;
 (?&amp;lt;!-&amp;gt;)(?&amp;lt;!function )(?&amp;lt;!\$)\b(?:(?:count|delete|get|insert|update)_record(?!s_csv)|[gs]et_field|record_exists|execute_sql)|\$CFG-&amp;gt;prefix|rs_(?:fetch|close)|(add|strip)slashes(?!_js)&lt;br /&gt;
&lt;br /&gt;
Will find most of the things in your code that need attention, with few false positives.&lt;br /&gt;
&lt;br /&gt;
== XMLDB/DDL changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
&lt;br /&gt;
* When changing DB structures it&#039;s highly recommended to use the [[XMLDB editor|XMLDB Editor]] (Admin-&amp;gt;Development-&amp;gt;XMLDB Editor). It is a safe way to edit install.xml files and to get correct PHP code to be used in upgrade.php scripts.&lt;br /&gt;
* If you have some doubts about the list of changes below, it&#039;s highly recommended to take a look at some code in core modules, blocks... whatever you need. They are a good reference.&lt;br /&gt;
* The most noticeable change (from a migration perspective) in the DDL stuff is that, starting with Moodle 2.0, we have decided to &#039;&#039;&#039;drop support for enum (check constraint) fields completely&#039;&#039;&#039;. See MDL-18577 and [http://moodle.org/mod/forum/discuss.php?d=118852 this discussion]. That implies changes in different parts of the DB stuff (xml files, function parameters, dropping existing enums...) and are commented with more detail below.&lt;br /&gt;
&lt;br /&gt;
=== The changes ===&lt;br /&gt;
* STATEMENTS section was replaced by db/install.php code and db/log.php&lt;br /&gt;
* Few other changes are required in install.xml files (that&#039;s good news!). Although you must know these:&lt;br /&gt;
** The &#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039; attributes present in your install.xml files will be completely ignored both by the DLL generation stuff and by the XMLDB Editor.&lt;br /&gt;
** In Moodle 2.1, those attributes (&#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039;) will be 100% forbidden, so it&#039;s highly recommended to erase them since now (Moodle 2.0).&lt;br /&gt;
** The XMLDB Editor will detect them when loading any install.xml file and will suggest you to fix that easily with one 1-click® option.&lt;br /&gt;
** No matter if you have fixed that with the 1-click® option when loading the files... the [[XMLDB editor|XMLDB Editor]] will save any edited install.xml files without those attributes.&lt;br /&gt;
** If your Moodle 1.9.x code &#039;&#039;&#039;has some enum defined in the database&#039;&#039;&#039;, you will need to create one upgrade block (in db/upgrade.php) to drop it (the enum, not the field! ;-) as part of the migration from Moodle 1.9 to 2.0 by using the new &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; method.&lt;br /&gt;
** Of course, if you don&#039;t use/like the XMLDB Editor, you can erase them manually with something like this:&lt;br /&gt;
 perl -p -e &#039;s/ENUM(VALUES)?=&amp;quot;.*?&amp;quot; //g&#039; &amp;lt; install.xml &amp;gt; install.xml.new&lt;br /&gt;
&lt;br /&gt;
* All upgrade.php scripts, within the main xxxx_upgrade function must have &#039;&#039;&#039;$DB&#039;&#039;&#039; (uppercase) in the globals declaration (along with others if needed). Example (from glossary module):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function xmldb_glossary_upgrade($oldversion=0) {&lt;br /&gt;
    &lt;br /&gt;
    global $CFG, $THEME, $DB;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All upgrade.php scripts, must NOT have &#039;&#039;&#039;$db&#039;&#039;&#039; (lowercase) in the globals declaration. Delete it if present.&lt;br /&gt;
* After the global declaration in the points above, this line must be present (we&#039;ll need it later):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$dbman = $DB-&amp;gt;get_manager(); /// loads ddl manager and xmldb classes&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBTable&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_table&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBField&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_field&#039;&#039;&#039; (with change in params, both $enum and $enumvalues are out from function declaration!)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBIndex&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_index&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBKey&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_key&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addFieldInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_field()&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 7th and 8th parameters) are out from function declaration!)&lt;br /&gt;
* All the &#039;&#039;&#039;addIndexInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_index()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addKeyInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_key()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;setAttributes()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;set_attributes()&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 6th and 7th parameters) are out from function declaration!)&lt;br /&gt;
* All the DDL functions used in upgrade code, must be transformed as detailed below (it&#039;s only about to add &#039;&#039;&#039;&amp;quot;$dbman-&amp;gt;&amp;quot;&#039;&#039;&#039; - without the quotes - before each function call). No changes in parameters are required:&lt;br /&gt;
** table_exists ==&amp;gt; $dbman-&amp;gt;table_exists&lt;br /&gt;
** field_exists ==&amp;gt; $dbman-&amp;gt;field_exists&lt;br /&gt;
** index_exists ==&amp;gt; $dbman-&amp;gt;index_exists&lt;br /&gt;
** find_index_name ==&amp;gt; $dbman-&amp;gt;find_index_name&lt;br /&gt;
** find_check_constraint_name ==&amp;gt; $dbman-&amp;gt;find_check_constraint_name (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** check_constraint_exists ==&amp;gt; $dbman-&amp;gt;check_constraint_exists (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** find_sequence_name ==&amp;gt; &#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see MDL-20349&lt;br /&gt;
** create_table ==&amp;gt; $dbman-&amp;gt;create_table&lt;br /&gt;
** drop_table ==&amp;gt; $dbman-&amp;gt;drop_table&lt;br /&gt;
** rename_table ==&amp;gt; $dbman-&amp;gt;rename_table&lt;br /&gt;
** add_field ==&amp;gt; $dbman-&amp;gt;add_field&lt;br /&gt;
** drop_field ==&amp;gt; $dbman-&amp;gt;drop field&lt;br /&gt;
** rename_field ==&amp;gt; $dbman-&amp;gt;rename_field&lt;br /&gt;
** change_field_type ==&amp;gt; $dbman-&amp;gt;change_field_type&lt;br /&gt;
** change_field_precision =&amp;gt; $dbman-&amp;gt;change_field_precision&lt;br /&gt;
** change_field_unsigned ==&amp;gt; $dbman-&amp;gt;change_field_unsigned&lt;br /&gt;
** change_field_notnull ==&amp;gt; $dbman-&amp;gt;change_field_notnull&lt;br /&gt;
** change_field_enum ==&amp;gt; $dbman-&amp;gt;change_field_enum (&#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; to get rid of remaining ENUMs in code).&lt;br /&gt;
** change_field_default ==&amp;gt; $dbman-&amp;gt;change_field_default&lt;br /&gt;
** add_key ==&amp;gt; $dbman-&amp;gt;add_key&lt;br /&gt;
** drop_key ==&amp;gt; $dbman-&amp;gt;drop_key&lt;br /&gt;
** add_index ==&amp;gt; $dbman-&amp;gt;add_index&lt;br /&gt;
** drop_index ==&amp;gt; $dbman-&amp;gt;drop_index&lt;br /&gt;
* Finally, and not less important, your code (module, block... plugin) must guarantee that it can be upgraded &#039;&#039;&#039;ONLY&#039;&#039;&#039; from Moodle 1.9, so any previous upgrade code can be safely deleted. &#039;&#039;&#039;Moodle 2.0 requires Moodle 1.9&#039;&#039;&#039; to be upgraded, so everybody will run the 1.9 =&amp;gt; 2.0 upgrade (with other paths like 1.8 =&amp;gt; 2.0 not being possible). One good time to clean-up a bit your upgrade code ;-). Don&#039;t forget to take a look to the [[XMLDB editor|XMLDB Editor]] and to core modules to see how [http://cvs.moodle.org/moodle/lib/db/upgrade.php?view=markup upgrade.php] files look like in Moodle 2.0.&lt;br /&gt;
&lt;br /&gt;
== DML changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
* The ENTIRE CODEBASE requires an update of ALL database query function calls. Expect most moodle files to be affected by this change.&lt;br /&gt;
&lt;br /&gt;
* This is the more complex part to migrate to have the code working under Moodle 2.0, not because of the complexity of the changes themselves (90% of them will be really easy), but because you&#039;ll need to triple-check everything works as expected after changes.&lt;br /&gt;
&lt;br /&gt;
* Along the changes below, you&#039;ll find links to some examples that will try to make things easier. Anyway, if you are blocked at any point, please ask in forums or tracker (see links at the beginning of the page). Sure it has a good enough solution.&lt;br /&gt;
&lt;br /&gt;
* Once more it&#039;s highly recommended to take a look to Moodle core code, searching for similar examples. Of course, new meaningful examples are welcome, and also any clarification in the list of changes below. Feel free to do it, this is a wiki!&lt;br /&gt;
&lt;br /&gt;
* Finally, one more explanation: The changes below have been split into three sections. First one, (called &#039;&#039;&#039;&amp;quot;The golden changes&amp;quot;&#039;&#039;&#039;) are modifications that must be applied to ALL the transformations defined in the second section (&#039;&#039;&#039;&amp;quot;The iron changes&amp;quot;&#039;&#039;&#039;). Sure you&#039;ll understand that after reading them (it&#039;s basically a matter of not repeating the golden ones within each iron one, just imagine they are everywhere). Finally other changes details can be found in the &#039;&#039;&#039;&amp;quot;The tin changes&amp;quot;&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
=== The golden changes ===&lt;br /&gt;
&#039;&#039;PLEASE read the API before going crazy with search &amp;amp; replace&#039;&#039;! You can find all the new methods in lib/dml/moodle_database.php. This is essential because some method signatures have changed (params are different), and some method names have even changed (execute_sql() is now execute()).&lt;br /&gt;
&lt;br /&gt;
Each of the golden changes below is given one short name (G1, G2, G3...) for further reference in the rest of the documentation. Let&#039;s go:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Wherever old functions are used (get_record*, get_field*, set_field, insert_record, update_record, count_records*, delete_records, record_exists), the global $DB must be used as the object on which these functions are called (e.g. get_record_select() becomes $DB-&amp;gt;get_record_select()).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
get_record_select($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
$DB-&amp;gt;get_record_select($sql);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: for some functions, the $params array is not the second function parameter. For example, set_field:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Old syntax&lt;br /&gt;
set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, &#039;id&#039;, 1);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$DB-&amp;gt;set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, array(&#039;id&#039; =&amp;gt; 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g2&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All uses of addslashes() &#039;&#039;&#039;must be removed&#039;&#039;&#039;. They are no longer needed&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g3&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G3&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All the functions that used to accept a list of string params in the form &amp;quot;param1, value1, param2, value2&amp;quot; now need to be given an array of key=&amp;gt;value pairs as a replacement for these params. Other params remain as before. Check the new API for any exceptions.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$user = get_record(&amp;quot;user&amp;quot;, &amp;quot;firstname&amp;quot;, &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot;, &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// New syntax:&lt;br /&gt;
global $DB;&lt;br /&gt;
$conditions = array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, $conditions);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: The example above has been written out in full for clarity. You can use the array() directly within the function call, without using a temporary variable, if you prefer:&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
global $DB;&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;) );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g4&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G4&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: rs_fetch_next_record($rs) is deprecated, in favour of the simple foreach($rs as $var). Calls to rs_close() must be replaced by $rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
while($result = rs_fetch_next_record($rs)) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
rs_close();&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
foreach ($rs as $result) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
$rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g5&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G5&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Placeholders must be used for table names. Instead of {$CFG-&amp;gt;prefix}tablename, use {tablename}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {$CFG-&amp;gt;prefix}user&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {user}&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g6&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G6&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: When PHP variables are used in SQL queries, they must be replaced by parameters. You have the choice between two approaches: ordered parameters, or named parameters.&lt;br /&gt;
** Ordered parameters use a simple array of values, which are given to the DML function as $params. The values in the SQL code are simply question marks (?) replacing the values. They are replaced by the DML code one by one, substituting each question mark (?) with the next value in the $params array.&lt;br /&gt;
** Named parameters use an associative array of name =&amp;gt; value pairs as the $params array. The values in the SQL code are replaced with a colon (:) followed by the key associated with the value, in the $params array. Note that named params &#039;&#039;&#039;must be unique&#039;&#039;&#039;, no matter if the value passed is the same.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {$CFG-&amp;gt;prefix}user WHERE firstname = &#039;Peter&#039; AND lastname = &#039;Cantrophus&#039;&amp;quot;;&lt;br /&gt;
$user = get_record_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax: ordered params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;Peter&#039;, &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = ? AND lastname = ?&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
// New syntax: named params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;firstname&#039; =&amp;gt; &#039;Peter&#039;, &#039;lastname&#039; =&amp;gt; &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = :firstname AND lastname = :lastname&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To obtain a value for the LIKE operator, include any % signs into the parameter string. For example, code that was previously &amp;lt;tt&amp;gt;&amp;quot;LIKE &#039;$value%&#039;&amp;quot;&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;&amp;quot;LIKE ?&amp;quot;&amp;lt;/tt&amp;gt; with the parameter &amp;lt;tt&amp;gt;$value.&#039;%&#039;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g7&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G7&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Replacement of the IN(...) syntax: We no longer hard-code this in our SQL queries, we use a function which determines whether the IN() syntax is needed, or, if there is only one value to compare, the equal (=) sign can be used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
$gis = implode(&#039;,&#039;, $depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {$CFG-&amp;gt;prefix}grade_items&lt;br /&gt;
         WHERE id IN ($gis)&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// new syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
list($usql, $params) = $DB-&amp;gt;get_in_or_equal($depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {grade_items}&lt;br /&gt;
         WHERE id $usql&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; to combine the last two, do the G7 IN SQL stuff first to generate the params array, then do something like&lt;br /&gt;
  $param[&#039;firstname&#039;] = &#039;peter&#039;;&lt;br /&gt;
to add the stuff for G6&lt;br /&gt;
&lt;br /&gt;
=== The iron changes ===&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;span id=&amp;quot;I1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;I1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally the &#039;&#039;&#039;sql_substr()&#039;&#039;&#039; function was used without parameters and it returned only the name of the &amp;quot;substring&amp;quot; function to be used under each DB. In Moodle 2.0 and upwards, it has 3 parameters (2 being mandatory) and it returns the complete SQL text to be used when handling substrings. Note that positions in this function are 1-based (first char has index 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$records = get_records_sql(&amp;quot;SELECT &amp;quot; . sql_substr() . &amp;quot;(firstname, 1, 20)&amp;quot; . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$records = $DB-&amp;gt;get_records_sql(&amp;quot;SELECT &amp;quot; . $DB-&amp;gt;sql_substr(&#039;firstname&#039;, 1, 20) . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The tin changes ===&lt;br /&gt;
List of minor changes&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally get_records() and similar functions were returning false if no records found. All these methods are now always returning arrays, empty array in case of no records found. Please note that get_record() still returns false if specified record not found.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
if (!$posts = get_records(&#039;forum_posts&#039;, &#039;parent&#039;, 666)) {&lt;br /&gt;
    $posts = array()&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$posts = $DB-&amp;gt;get_records(&#039;forum_posts&#039;, array(&#039;parent&#039;=&amp;gt;666));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally DML functions were returning &#039;&#039;false&#039;&#039; if error occurred - &#039;&#039;dml_exception&#039;&#039; is thrown now instead.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
if (!$id = insert_record(&#039;sometable&#039;, $record)) {&lt;br /&gt;
   error(&#039;can not insert new record&#039;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
$id = $DB-&amp;gt;insert_record(&#039;sometable&#039;, $record);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB Documentation|XMLDB Documentation]]: where both xmldb and ddl stuff is explained.&lt;br /&gt;
* [[Development:DDL functions|DDL functions]] - Documentation for all the Data Definition Language (DDL) functions available inside Moodle.&lt;br /&gt;
* [[Development:DML functions|DML functions]] - Documentation for all the Data Manipulation Language (DML) functions available inside Moodle.&lt;br /&gt;
* [[Development:DDL exceptions|DDL exceptions]] - DDL exceptions information.&lt;br /&gt;
* [[Development:DML exceptions|DML exceptions]] - DML exceptions information.&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76465</id>
		<title>DB layer 2.0 migration docs</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76465"/>
		<updated>2010-10-04T03:13:59Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* The changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Development:dmllib 2.0}}{{Moodle_2.0}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Much of the following documentation will not make much sense unless you first read [[Development:XMLDB_Documentation|the XMLDB documentation]]. Please read it first if you would like to join the effort to convert Moodle&#039;s code to the new dmllib.&lt;br /&gt;
&lt;br /&gt;
Also, it&#039;s recommended to read the whole [[wikipedia:Data_Definition_Language|DDL]] and [[wikipedia:Data_Manipulation_Language|DML]] documentation before start with the migration. That will allow you to have some initial knowledge about the new architecture.&lt;br /&gt;
&lt;br /&gt;
This article defines all the changes that need to be performed in Moodle 1.9 code in order to make it run properly under new Moodle 2.0 DB layer. Changes below are grouped into 2 main blocks ([[Development:XMLDB_Documentation|xmldb]] and [[wikipedia:Data_Manipulation_Language|dml]]) to have them organised. Additional minor changes may be required in the [[wikipedia:Data_Definition_Language|ddl]] code, but won&#039;t be documented here.&lt;br /&gt;
&lt;br /&gt;
Although the order of changes showed in this page isn&#039;t mandatory at all, it can be interesting to follow it in the migration progress to be able to understand and learn a bit more about all them. That way, you&#039;ll end up knowing not only what to change but how and why those changes are required.&lt;br /&gt;
&lt;br /&gt;
Each change will be as simple as possible, representing one easy rule to follow to adapt the code. When anything become too complex to be explained as one simple rule, it will contain one link to the [[Development:dmllib_2.0_examples|examples page]].&lt;br /&gt;
&lt;br /&gt;
For any problem in the migration of code, it&#039;s recommended to use the [http://moodle.org/mod/forum/view.php?id=45 Databases forum] at [http://moodle.org moodle.org]. Also if you find any bug in the process, please report it in the [http://tracker.moodle.org/browse/MDL-14679 Moodle Tracker], that way developers will be able to fix it ASAP.&lt;br /&gt;
&lt;br /&gt;
The Glossary module was used as the basis for many of the examples below.&lt;br /&gt;
&lt;br /&gt;
And finally, feel free to complete/fix the list below with the changes you find in the progress of migration, that will certainly help many developers. Thanks!&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== check_db_syntax: One helper script ==&lt;br /&gt;
&lt;br /&gt;
Before start migrating your code to Moodle 2.0, it&#039;s recommended to install and run the [http://cvs.moodle.org/contrib/tools/check_db_syntax/ check_db_syntax.php] script. Simply copy it to the main folder of your plugin and execute it (from command line or via web). I&#039;ll show you the list of old DB usages that need to be transformed following the information below in this article.&lt;br /&gt;
&lt;br /&gt;
If you think that something is missing in the script or have any idea to improve it, feel free to do that yourself, commenting about it in MDL-15237. Thanks!&lt;br /&gt;
&lt;br /&gt;
Also, this (perl-compatible - works in Eclipse, hopefully elsewhere) regex:&lt;br /&gt;
&lt;br /&gt;
 (?&amp;lt;!-&amp;gt;)(?&amp;lt;!function )(?&amp;lt;!\$)\b(?:(?:count|delete|get|insert|update)_record(?!s_csv)|[gs]et_field|record_exists|execute_sql)|\$CFG-&amp;gt;prefix|rs_(?:fetch|close)|(add|strip)slashes(?!_js)&lt;br /&gt;
&lt;br /&gt;
Will find most of the things in your code that need attention, with few false positives.&lt;br /&gt;
&lt;br /&gt;
== XMLDB/DDL changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
&lt;br /&gt;
* When changing DB structures it&#039;s highly recommended to use the [[XMLDB editor|XMLDB Editor]] (Admin-&amp;gt;Development-&amp;gt;XMLDB Editor). It is a safe way to edit install.xml files and to get correct PHP code to be used in upgrade.php scripts.&lt;br /&gt;
* If you have some doubts about the list of changes below, it&#039;s highly recommended to take a look at some code in core modules, blocks... whatever you need. They are a good reference.&lt;br /&gt;
* The most noticeable change (from a migration perspective) in the DDL stuff is that, starting with Moodle 2.0, we have decided to &#039;&#039;&#039;drop support for enum (check constraint) fields completely&#039;&#039;&#039;. See MDL-18577 and [http://moodle.org/mod/forum/discuss.php?d=118852 this discussion]. That implies changes in different parts of the DB stuff (xml files, function parameters, dropping existing enums...) and are commented with more detail below.&lt;br /&gt;
&lt;br /&gt;
=== The changes ===&lt;br /&gt;
* STATEMENTS section was replaced by db/install.php code and db/log.php&lt;br /&gt;
* Few other changes are required in install.xml files (that&#039;s good news!). Although you must know these:&lt;br /&gt;
** The &#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039; attributes present in your install.xml files will be completely ignored both by the DLL generation stuff and by the XMLDB Editor.&lt;br /&gt;
** In Moodle 2.1, those attributes (&#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039;) will be 100% forbidden, so it&#039;s highly recommended to erase them since now (Moodle 2.0).&lt;br /&gt;
** The XMLDB Editor will detect them when loading any install.xml file and will suggest you to fix that easily with one 1-click® option.&lt;br /&gt;
** No matter if you have fixed that with the 1-click® option when loading the files... the [[XMLDB editor|XMLDB Editor]] will save any edited install.xml files without those attributes.&lt;br /&gt;
** If your Moodle 1.9.x code &#039;&#039;&#039;has some enum defined in the database&#039;&#039;&#039;, you will need to create one upgrade block (in db/upgrade.php) to drop it (the enum, not the field! ;-) as part of the migration from Moodle 1.9 to 2.0 by using the new &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; method.&lt;br /&gt;
** Of course, if you don&#039;t use/like the XMLDB Editor, you can erase them manually with something like this:&lt;br /&gt;
 perl -p -e &#039;s/ENUM(VALUES)?=&amp;quot;.*?&amp;quot; //g&#039; &amp;lt; install.xml &amp;gt; install.xml.new&lt;br /&gt;
&lt;br /&gt;
* All upgrade.php scripts, within the main xxxx_upgrade function must have &#039;&#039;&#039;$DB&#039;&#039;&#039; (uppercase) in the globals declaration (along with others if needed). Example (from glossary module):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function xmldb_glossary_upgrade($oldversion=0) {&lt;br /&gt;
    &lt;br /&gt;
    global $CFG, $THEME, $DB;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All upgrade.php scripts, must NOT have &#039;&#039;&#039;$db&#039;&#039;&#039; (lowercase) in the globals declaration. Delete it if present.&lt;br /&gt;
* After the global declaration in the points above, this line must be present (we&#039;ll need it later):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$dbman = $DB-&amp;gt;get_manager(); /// loads ddl manager and xmldb classes&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBTable&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_table&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBField&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_field&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 7th and 8th parameters) are out from function declaration!)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBIndex&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_index&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBKey&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_key&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addFieldInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_field()&#039;&#039;&#039; (with change in params, both enum and enumvalues are out from function declaration!)&lt;br /&gt;
* All the &#039;&#039;&#039;addIndexInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_index()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addKeyInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_key()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;setAttributes()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;set_attributes()&#039;&#039;&#039; (with change in params, both $enum and $enumvalues (the 6th and 7th parameters) are out from function declaration!)&lt;br /&gt;
* All the DDL functions used in upgrade code, must be transformed as detailed below (it&#039;s only about to add &#039;&#039;&#039;&amp;quot;$dbman-&amp;gt;&amp;quot;&#039;&#039;&#039; - without the quotes - before each function call). No changes in parameters are required:&lt;br /&gt;
** table_exists ==&amp;gt; $dbman-&amp;gt;table_exists&lt;br /&gt;
** field_exists ==&amp;gt; $dbman-&amp;gt;field_exists&lt;br /&gt;
** index_exists ==&amp;gt; $dbman-&amp;gt;index_exists&lt;br /&gt;
** find_index_name ==&amp;gt; $dbman-&amp;gt;find_index_name&lt;br /&gt;
** find_check_constraint_name ==&amp;gt; $dbman-&amp;gt;find_check_constraint_name (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** check_constraint_exists ==&amp;gt; $dbman-&amp;gt;check_constraint_exists (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** find_sequence_name ==&amp;gt; &#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see MDL-20349&lt;br /&gt;
** create_table ==&amp;gt; $dbman-&amp;gt;create_table&lt;br /&gt;
** drop_table ==&amp;gt; $dbman-&amp;gt;drop_table&lt;br /&gt;
** rename_table ==&amp;gt; $dbman-&amp;gt;rename_table&lt;br /&gt;
** add_field ==&amp;gt; $dbman-&amp;gt;add_field&lt;br /&gt;
** drop_field ==&amp;gt; $dbman-&amp;gt;drop field&lt;br /&gt;
** rename_field ==&amp;gt; $dbman-&amp;gt;rename_field&lt;br /&gt;
** change_field_type ==&amp;gt; $dbman-&amp;gt;change_field_type&lt;br /&gt;
** change_field_precision =&amp;gt; $dbman-&amp;gt;change_field_precision&lt;br /&gt;
** change_field_unsigned ==&amp;gt; $dbman-&amp;gt;change_field_unsigned&lt;br /&gt;
** change_field_notnull ==&amp;gt; $dbman-&amp;gt;change_field_notnull&lt;br /&gt;
** change_field_enum ==&amp;gt; $dbman-&amp;gt;change_field_enum (&#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; to get rid of remaining ENUMs in code).&lt;br /&gt;
** change_field_default ==&amp;gt; $dbman-&amp;gt;change_field_default&lt;br /&gt;
** add_key ==&amp;gt; $dbman-&amp;gt;add_key&lt;br /&gt;
** drop_key ==&amp;gt; $dbman-&amp;gt;drop_key&lt;br /&gt;
** add_index ==&amp;gt; $dbman-&amp;gt;add_index&lt;br /&gt;
** drop_index ==&amp;gt; $dbman-&amp;gt;drop_index&lt;br /&gt;
* Finally, and not less important, your code (module, block... plugin) must guarantee that it can be upgraded &#039;&#039;&#039;ONLY&#039;&#039;&#039; from Moodle 1.9, so any previous upgrade code can be safely deleted. &#039;&#039;&#039;Moodle 2.0 requires Moodle 1.9&#039;&#039;&#039; to be upgraded, so everybody will run the 1.9 =&amp;gt; 2.0 upgrade (with other paths like 1.8 =&amp;gt; 2.0 not being possible). One good time to clean-up a bit your upgrade code ;-). Don&#039;t forget to take a look to the [[XMLDB editor|XMLDB Editor]] and to core modules to see how [http://cvs.moodle.org/moodle/lib/db/upgrade.php?view=markup upgrade.php] files look like in Moodle 2.0.&lt;br /&gt;
&lt;br /&gt;
== DML changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
* The ENTIRE CODEBASE requires an update of ALL database query function calls. Expect most moodle files to be affected by this change.&lt;br /&gt;
&lt;br /&gt;
* This is the more complex part to migrate to have the code working under Moodle 2.0, not because of the complexity of the changes themselves (90% of them will be really easy), but because you&#039;ll need to triple-check everything works as expected after changes.&lt;br /&gt;
&lt;br /&gt;
* Along the changes below, you&#039;ll find links to some examples that will try to make things easier. Anyway, if you are blocked at any point, please ask in forums or tracker (see links at the beginning of the page). Sure it has a good enough solution.&lt;br /&gt;
&lt;br /&gt;
* Once more it&#039;s highly recommended to take a look to Moodle core code, searching for similar examples. Of course, new meaningful examples are welcome, and also any clarification in the list of changes below. Feel free to do it, this is a wiki!&lt;br /&gt;
&lt;br /&gt;
* Finally, one more explanation: The changes below have been split into three sections. First one, (called &#039;&#039;&#039;&amp;quot;The golden changes&amp;quot;&#039;&#039;&#039;) are modifications that must be applied to ALL the transformations defined in the second section (&#039;&#039;&#039;&amp;quot;The iron changes&amp;quot;&#039;&#039;&#039;). Sure you&#039;ll understand that after reading them (it&#039;s basically a matter of not repeating the golden ones within each iron one, just imagine they are everywhere). Finally other changes details can be found in the &#039;&#039;&#039;&amp;quot;The tin changes&amp;quot;&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
=== The golden changes ===&lt;br /&gt;
&#039;&#039;PLEASE read the API before going crazy with search &amp;amp; replace&#039;&#039;! You can find all the new methods in lib/dml/moodle_database.php. This is essential because some method signatures have changed (params are different), and some method names have even changed (execute_sql() is now execute()).&lt;br /&gt;
&lt;br /&gt;
Each of the golden changes below is given one short name (G1, G2, G3...) for further reference in the rest of the documentation. Let&#039;s go:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Wherever old functions are used (get_record*, get_field*, set_field, insert_record, update_record, count_records*, delete_records, record_exists), the global $DB must be used as the object on which these functions are called (e.g. get_record_select() becomes $DB-&amp;gt;get_record_select()).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
get_record_select($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
$DB-&amp;gt;get_record_select($sql);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: for some functions, the $params array is not the second function parameter. For example, set_field:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Old syntax&lt;br /&gt;
set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, &#039;id&#039;, 1);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$DB-&amp;gt;set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, array(&#039;id&#039; =&amp;gt; 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g2&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All uses of addslashes() &#039;&#039;&#039;must be removed&#039;&#039;&#039;. They are no longer needed&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g3&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G3&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All the functions that used to accept a list of string params in the form &amp;quot;param1, value1, param2, value2&amp;quot; now need to be given an array of key=&amp;gt;value pairs as a replacement for these params. Other params remain as before. Check the new API for any exceptions.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$user = get_record(&amp;quot;user&amp;quot;, &amp;quot;firstname&amp;quot;, &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot;, &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// New syntax:&lt;br /&gt;
global $DB;&lt;br /&gt;
$conditions = array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, $conditions);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: The example above has been written out in full for clarity. You can use the array() directly within the function call, without using a temporary variable, if you prefer:&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
global $DB;&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;) );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g4&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G4&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: rs_fetch_next_record($rs) is deprecated, in favour of the simple foreach($rs as $var). Calls to rs_close() must be replaced by $rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
while($result = rs_fetch_next_record($rs)) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
rs_close();&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
foreach ($rs as $result) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
$rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g5&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G5&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Placeholders must be used for table names. Instead of {$CFG-&amp;gt;prefix}tablename, use {tablename}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {$CFG-&amp;gt;prefix}user&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {user}&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g6&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G6&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: When PHP variables are used in SQL queries, they must be replaced by parameters. You have the choice between two approaches: ordered parameters, or named parameters.&lt;br /&gt;
** Ordered parameters use a simple array of values, which are given to the DML function as $params. The values in the SQL code are simply question marks (?) replacing the values. They are replaced by the DML code one by one, substituting each question mark (?) with the next value in the $params array.&lt;br /&gt;
** Named parameters use an associative array of name =&amp;gt; value pairs as the $params array. The values in the SQL code are replaced with a colon (:) followed by the key associated with the value, in the $params array. Note that named params &#039;&#039;&#039;must be unique&#039;&#039;&#039;, no matter if the value passed is the same.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {$CFG-&amp;gt;prefix}user WHERE firstname = &#039;Peter&#039; AND lastname = &#039;Cantrophus&#039;&amp;quot;;&lt;br /&gt;
$user = get_record_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax: ordered params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;Peter&#039;, &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = ? AND lastname = ?&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
// New syntax: named params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;firstname&#039; =&amp;gt; &#039;Peter&#039;, &#039;lastname&#039; =&amp;gt; &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = :firstname AND lastname = :lastname&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To obtain a value for the LIKE operator, include any % signs into the parameter string. For example, code that was previously &amp;lt;tt&amp;gt;&amp;quot;LIKE &#039;$value%&#039;&amp;quot;&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;&amp;quot;LIKE ?&amp;quot;&amp;lt;/tt&amp;gt; with the parameter &amp;lt;tt&amp;gt;$value.&#039;%&#039;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g7&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G7&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Replacement of the IN(...) syntax: We no longer hard-code this in our SQL queries, we use a function which determines whether the IN() syntax is needed, or, if there is only one value to compare, the equal (=) sign can be used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
$gis = implode(&#039;,&#039;, $depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {$CFG-&amp;gt;prefix}grade_items&lt;br /&gt;
         WHERE id IN ($gis)&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// new syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
list($usql, $params) = $DB-&amp;gt;get_in_or_equal($depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {grade_items}&lt;br /&gt;
         WHERE id $usql&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; to combine the last two, do the G7 IN SQL stuff first to generate the params array, then do something like&lt;br /&gt;
  $param[&#039;firstname&#039;] = &#039;peter&#039;;&lt;br /&gt;
to add the stuff for G6&lt;br /&gt;
&lt;br /&gt;
=== The iron changes ===&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;span id=&amp;quot;I1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;I1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally the &#039;&#039;&#039;sql_substr()&#039;&#039;&#039; function was used without parameters and it returned only the name of the &amp;quot;substring&amp;quot; function to be used under each DB. In Moodle 2.0 and upwards, it has 3 parameters (2 being mandatory) and it returns the complete SQL text to be used when handling substrings. Note that positions in this function are 1-based (first char has index 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$records = get_records_sql(&amp;quot;SELECT &amp;quot; . sql_substr() . &amp;quot;(firstname, 1, 20)&amp;quot; . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$records = $DB-&amp;gt;get_records_sql(&amp;quot;SELECT &amp;quot; . $DB-&amp;gt;sql_substr(&#039;firstname&#039;, 1, 20) . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The tin changes ===&lt;br /&gt;
List of minor changes&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally get_records() and similar functions were returning false if no records found. All these methods are now always returning arrays, empty array in case of no records found. Please note that get_record() still returns false if specified record not found.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
if (!$posts = get_records(&#039;forum_posts&#039;, &#039;parent&#039;, 666)) {&lt;br /&gt;
    $posts = array()&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$posts = $DB-&amp;gt;get_records(&#039;forum_posts&#039;, array(&#039;parent&#039;=&amp;gt;666));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally DML functions were returning &#039;&#039;false&#039;&#039; if error occurred - &#039;&#039;dml_exception&#039;&#039; is thrown now instead.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
if (!$id = insert_record(&#039;sometable&#039;, $record)) {&lt;br /&gt;
   error(&#039;can not insert new record&#039;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
$id = $DB-&amp;gt;insert_record(&#039;sometable&#039;, $record);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB Documentation|XMLDB Documentation]]: where both xmldb and ddl stuff is explained.&lt;br /&gt;
* [[Development:DDL functions|DDL functions]] - Documentation for all the Data Definition Language (DDL) functions available inside Moodle.&lt;br /&gt;
* [[Development:DML functions|DML functions]] - Documentation for all the Data Manipulation Language (DML) functions available inside Moodle.&lt;br /&gt;
* [[Development:DDL exceptions|DDL exceptions]] - DDL exceptions information.&lt;br /&gt;
* [[Development:DML exceptions|DML exceptions]] - DML exceptions information.&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76464</id>
		<title>DB layer 2.0 migration docs</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=DB_layer_2.0_migration_docs&amp;diff=76464"/>
		<updated>2010-10-04T03:07:16Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* The changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Development:dmllib 2.0}}{{Moodle_2.0}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Much of the following documentation will not make much sense unless you first read [[Development:XMLDB_Documentation|the XMLDB documentation]]. Please read it first if you would like to join the effort to convert Moodle&#039;s code to the new dmllib.&lt;br /&gt;
&lt;br /&gt;
Also, it&#039;s recommended to read the whole [[wikipedia:Data_Definition_Language|DDL]] and [[wikipedia:Data_Manipulation_Language|DML]] documentation before start with the migration. That will allow you to have some initial knowledge about the new architecture.&lt;br /&gt;
&lt;br /&gt;
This article defines all the changes that need to be performed in Moodle 1.9 code in order to make it run properly under new Moodle 2.0 DB layer. Changes below are grouped into 2 main blocks ([[Development:XMLDB_Documentation|xmldb]] and [[wikipedia:Data_Manipulation_Language|dml]]) to have them organised. Additional minor changes may be required in the [[wikipedia:Data_Definition_Language|ddl]] code, but won&#039;t be documented here.&lt;br /&gt;
&lt;br /&gt;
Although the order of changes showed in this page isn&#039;t mandatory at all, it can be interesting to follow it in the migration progress to be able to understand and learn a bit more about all them. That way, you&#039;ll end up knowing not only what to change but how and why those changes are required.&lt;br /&gt;
&lt;br /&gt;
Each change will be as simple as possible, representing one easy rule to follow to adapt the code. When anything become too complex to be explained as one simple rule, it will contain one link to the [[Development:dmllib_2.0_examples|examples page]].&lt;br /&gt;
&lt;br /&gt;
For any problem in the migration of code, it&#039;s recommended to use the [http://moodle.org/mod/forum/view.php?id=45 Databases forum] at [http://moodle.org moodle.org]. Also if you find any bug in the process, please report it in the [http://tracker.moodle.org/browse/MDL-14679 Moodle Tracker], that way developers will be able to fix it ASAP.&lt;br /&gt;
&lt;br /&gt;
The Glossary module was used as the basis for many of the examples below.&lt;br /&gt;
&lt;br /&gt;
And finally, feel free to complete/fix the list below with the changes you find in the progress of migration, that will certainly help many developers. Thanks!&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== check_db_syntax: One helper script ==&lt;br /&gt;
&lt;br /&gt;
Before start migrating your code to Moodle 2.0, it&#039;s recommended to install and run the [http://cvs.moodle.org/contrib/tools/check_db_syntax/ check_db_syntax.php] script. Simply copy it to the main folder of your plugin and execute it (from command line or via web). I&#039;ll show you the list of old DB usages that need to be transformed following the information below in this article.&lt;br /&gt;
&lt;br /&gt;
If you think that something is missing in the script or have any idea to improve it, feel free to do that yourself, commenting about it in MDL-15237. Thanks!&lt;br /&gt;
&lt;br /&gt;
Also, this (perl-compatible - works in Eclipse, hopefully elsewhere) regex:&lt;br /&gt;
&lt;br /&gt;
 (?&amp;lt;!-&amp;gt;)(?&amp;lt;!function )(?&amp;lt;!\$)\b(?:(?:count|delete|get|insert|update)_record(?!s_csv)|[gs]et_field|record_exists|execute_sql)|\$CFG-&amp;gt;prefix|rs_(?:fetch|close)|(add|strip)slashes(?!_js)&lt;br /&gt;
&lt;br /&gt;
Will find most of the things in your code that need attention, with few false positives.&lt;br /&gt;
&lt;br /&gt;
== XMLDB/DDL changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
&lt;br /&gt;
* When changing DB structures it&#039;s highly recommended to use the [[XMLDB editor|XMLDB Editor]] (Admin-&amp;gt;Development-&amp;gt;XMLDB Editor). It is a safe way to edit install.xml files and to get correct PHP code to be used in upgrade.php scripts.&lt;br /&gt;
* If you have some doubts about the list of changes below, it&#039;s highly recommended to take a look at some code in core modules, blocks... whatever you need. They are a good reference.&lt;br /&gt;
* The most noticeable change (from a migration perspective) in the DDL stuff is that, starting with Moodle 2.0, we have decided to &#039;&#039;&#039;drop support for enum (check constraint) fields completely&#039;&#039;&#039;. See MDL-18577 and [http://moodle.org/mod/forum/discuss.php?d=118852 this discussion]. That implies changes in different parts of the DB stuff (xml files, function parameters, dropping existing enums...) and are commented with more detail below.&lt;br /&gt;
&lt;br /&gt;
=== The changes ===&lt;br /&gt;
* STATEMENTS section was replaced by db/install.php code and db/log.php&lt;br /&gt;
* Few other changes are required in install.xml files (that&#039;s good news!). Although you must know these:&lt;br /&gt;
** The &#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039; attributes present in your install.xml files will be completely ignored both by the DLL generation stuff and by the XMLDB Editor.&lt;br /&gt;
** In Moodle 2.1, those attributes (&#039;&#039;&#039;ENUM&#039;&#039;&#039; and &#039;&#039;&#039;ENUMVALUES&#039;&#039;&#039;) will be 100% forbidden, so it&#039;s highly recommended to erase them since now (Moodle 2.0).&lt;br /&gt;
** The XMLDB Editor will detect them when loading any install.xml file and will suggest you to fix that easily with one 1-click® option.&lt;br /&gt;
** No matter if you have fixed that with the 1-click® option when loading the files... the [[XMLDB editor|XMLDB Editor]] will save any edited install.xml files without those attributes.&lt;br /&gt;
** If your Moodle 1.9.x code &#039;&#039;&#039;has some enum defined in the database&#039;&#039;&#039;, you will need to create one upgrade block (in db/upgrade.php) to drop it (the enum, not the field! ;-) as part of the migration from Moodle 1.9 to 2.0 by using the new &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; method.&lt;br /&gt;
** Of course, if you don&#039;t use/like the XMLDB Editor, you can erase them manually with something like this:&lt;br /&gt;
 perl -p -e &#039;s/ENUM(VALUES)?=&amp;quot;.*?&amp;quot; //g&#039; &amp;lt; install.xml &amp;gt; install.xml.new&lt;br /&gt;
&lt;br /&gt;
* All upgrade.php scripts, within the main xxxx_upgrade function must have &#039;&#039;&#039;$DB&#039;&#039;&#039; (uppercase) in the globals declaration (along with others if needed). Example (from glossary module):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function xmldb_glossary_upgrade($oldversion=0) {&lt;br /&gt;
    &lt;br /&gt;
    global $CFG, $THEME, $DB;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All upgrade.php scripts, must NOT have &#039;&#039;&#039;$db&#039;&#039;&#039; (lowercase) in the globals declaration. Delete it if present.&lt;br /&gt;
* After the global declaration in the points above, this line must be present (we&#039;ll need it later):&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$dbman = $DB-&amp;gt;get_manager(); /// loads ddl manager and xmldb classes&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBTable&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_table&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBField&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_field&#039;&#039;&#039; (with change in params, both enum and enumvalues (the 7th and 8th parameters) are out from function declaration!)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBIndex&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_index&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All &#039;&#039;&#039;XMLDBKey&#039;&#039;&#039; instances in your upgrade code must be replaced by &#039;&#039;&#039;xmldb_key&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addFieldInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_field()&#039;&#039;&#039; (with change in params, both enum and enumvalues are out from function declaration!)&lt;br /&gt;
* All the &#039;&#039;&#039;addIndexInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_index()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;addKeyInfo()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;add_key()&#039;&#039;&#039; (no change in parameters)&lt;br /&gt;
* All the &#039;&#039;&#039;setAttributes()&#039;&#039;&#039; methods must be replaced by &#039;&#039;&#039;set_attributes()&#039;&#039;&#039; (with change in params, both enum and enumvalues are out from function declaration!)&lt;br /&gt;
* All the DDL functions used in upgrade code, must be transformed as detailed below (it&#039;s only about to add &#039;&#039;&#039;&amp;quot;$dbman-&amp;gt;&amp;quot;&#039;&#039;&#039; - without the quotes - before each function call). No changes in parameters are required:&lt;br /&gt;
** table_exists ==&amp;gt; $dbman-&amp;gt;table_exists&lt;br /&gt;
** field_exists ==&amp;gt; $dbman-&amp;gt;field_exists&lt;br /&gt;
** index_exists ==&amp;gt; $dbman-&amp;gt;index_exists&lt;br /&gt;
** find_index_name ==&amp;gt; $dbman-&amp;gt;find_index_name&lt;br /&gt;
** find_check_constraint_name ==&amp;gt; $dbman-&amp;gt;find_check_constraint_name (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** check_constraint_exists ==&amp;gt; $dbman-&amp;gt;check_constraint_exists (&#039;&#039;&#039;DEPRECATED in 2.0. OUT in 2.1&#039;&#039;&#039;)&lt;br /&gt;
** find_sequence_name ==&amp;gt; &#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see MDL-20349&lt;br /&gt;
** create_table ==&amp;gt; $dbman-&amp;gt;create_table&lt;br /&gt;
** drop_table ==&amp;gt; $dbman-&amp;gt;drop_table&lt;br /&gt;
** rename_table ==&amp;gt; $dbman-&amp;gt;rename_table&lt;br /&gt;
** add_field ==&amp;gt; $dbman-&amp;gt;add_field&lt;br /&gt;
** drop_field ==&amp;gt; $dbman-&amp;gt;drop field&lt;br /&gt;
** rename_field ==&amp;gt; $dbman-&amp;gt;rename_field&lt;br /&gt;
** change_field_type ==&amp;gt; $dbman-&amp;gt;change_field_type&lt;br /&gt;
** change_field_precision =&amp;gt; $dbman-&amp;gt;change_field_precision&lt;br /&gt;
** change_field_unsigned ==&amp;gt; $dbman-&amp;gt;change_field_unsigned&lt;br /&gt;
** change_field_notnull ==&amp;gt; $dbman-&amp;gt;change_field_notnull&lt;br /&gt;
** change_field_enum ==&amp;gt; $dbman-&amp;gt;change_field_enum (&#039;&#039;&#039;OUT in 2.0&#039;&#039;&#039;, see &#039;&#039;&#039;[[Development:DB layer 2.0 examples#Dropping one enum from one field|drop_enum_from_field()]]&#039;&#039;&#039; to get rid of remaining ENUMs in code).&lt;br /&gt;
** change_field_default ==&amp;gt; $dbman-&amp;gt;change_field_default&lt;br /&gt;
** add_key ==&amp;gt; $dbman-&amp;gt;add_key&lt;br /&gt;
** drop_key ==&amp;gt; $dbman-&amp;gt;drop_key&lt;br /&gt;
** add_index ==&amp;gt; $dbman-&amp;gt;add_index&lt;br /&gt;
** drop_index ==&amp;gt; $dbman-&amp;gt;drop_index&lt;br /&gt;
* Finally, and not less important, your code (module, block... plugin) must guarantee that it can be upgraded &#039;&#039;&#039;ONLY&#039;&#039;&#039; from Moodle 1.9, so any previous upgrade code can be safely deleted. &#039;&#039;&#039;Moodle 2.0 requires Moodle 1.9&#039;&#039;&#039; to be upgraded, so everybody will run the 1.9 =&amp;gt; 2.0 upgrade (with other paths like 1.8 =&amp;gt; 2.0 not being possible). One good time to clean-up a bit your upgrade code ;-). Don&#039;t forget to take a look to the [[XMLDB editor|XMLDB Editor]] and to core modules to see how [http://cvs.moodle.org/moodle/lib/db/upgrade.php?view=markup upgrade.php] files look like in Moodle 2.0.&lt;br /&gt;
&lt;br /&gt;
== DML changes ==&lt;br /&gt;
&lt;br /&gt;
=== Some comments ===&lt;br /&gt;
* The ENTIRE CODEBASE requires an update of ALL database query function calls. Expect most moodle files to be affected by this change.&lt;br /&gt;
&lt;br /&gt;
* This is the more complex part to migrate to have the code working under Moodle 2.0, not because of the complexity of the changes themselves (90% of them will be really easy), but because you&#039;ll need to triple-check everything works as expected after changes.&lt;br /&gt;
&lt;br /&gt;
* Along the changes below, you&#039;ll find links to some examples that will try to make things easier. Anyway, if you are blocked at any point, please ask in forums or tracker (see links at the beginning of the page). Sure it has a good enough solution.&lt;br /&gt;
&lt;br /&gt;
* Once more it&#039;s highly recommended to take a look to Moodle core code, searching for similar examples. Of course, new meaningful examples are welcome, and also any clarification in the list of changes below. Feel free to do it, this is a wiki!&lt;br /&gt;
&lt;br /&gt;
* Finally, one more explanation: The changes below have been split into three sections. First one, (called &#039;&#039;&#039;&amp;quot;The golden changes&amp;quot;&#039;&#039;&#039;) are modifications that must be applied to ALL the transformations defined in the second section (&#039;&#039;&#039;&amp;quot;The iron changes&amp;quot;&#039;&#039;&#039;). Sure you&#039;ll understand that after reading them (it&#039;s basically a matter of not repeating the golden ones within each iron one, just imagine they are everywhere). Finally other changes details can be found in the &#039;&#039;&#039;&amp;quot;The tin changes&amp;quot;&#039;&#039;&#039; section.&lt;br /&gt;
&lt;br /&gt;
=== The golden changes ===&lt;br /&gt;
&#039;&#039;PLEASE read the API before going crazy with search &amp;amp; replace&#039;&#039;! You can find all the new methods in lib/dml/moodle_database.php. This is essential because some method signatures have changed (params are different), and some method names have even changed (execute_sql() is now execute()).&lt;br /&gt;
&lt;br /&gt;
Each of the golden changes below is given one short name (G1, G2, G3...) for further reference in the rest of the documentation. Let&#039;s go:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Wherever old functions are used (get_record*, get_field*, set_field, insert_record, update_record, count_records*, delete_records, record_exists), the global $DB must be used as the object on which these functions are called (e.g. get_record_select() becomes $DB-&amp;gt;get_record_select()).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
get_record_select($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$sql = &amp;quot;WHERE id = 1&amp;quot;;&lt;br /&gt;
$DB-&amp;gt;get_record_select($sql);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: for some functions, the $params array is not the second function parameter. For example, set_field:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Old syntax&lt;br /&gt;
set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, &#039;id&#039;, 1);&lt;br /&gt;
  &lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$DB-&amp;gt;set_field(&#039;user&#039;, &#039;firstname&#039;, &#039;Peter&#039;, array(&#039;id&#039; =&amp;gt; 1));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g2&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All uses of addslashes() &#039;&#039;&#039;must be removed&#039;&#039;&#039;. They are no longer needed&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g3&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G3&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: All the functions that used to accept a list of string params in the form &amp;quot;param1, value1, param2, value2&amp;quot; now need to be given an array of key=&amp;gt;value pairs as a replacement for these params. Other params remain as before. Check the new API for any exceptions.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$user = get_record(&amp;quot;user&amp;quot;, &amp;quot;firstname&amp;quot;, &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot;, &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// New syntax:&lt;br /&gt;
global $DB;&lt;br /&gt;
$conditions = array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;);&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, $conditions);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
::Note: The example above has been written out in full for clarity. You can use the array() directly within the function call, without using a temporary variable, if you prefer:&#039;&#039;&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
global $DB;&lt;br /&gt;
$user = $DB-&amp;gt;get_record(&amp;quot;user&amp;quot;, array(&amp;quot;firstname&amp;quot; =&amp;gt; &amp;quot;Peter&amp;quot;, &amp;quot;lastname&amp;quot; =&amp;gt; &amp;quot;Cantrophus&amp;quot;) );&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g4&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G4&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: rs_fetch_next_record($rs) is deprecated, in favour of the simple foreach($rs as $var). Calls to rs_close() must be replaced by $rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
while($result = rs_fetch_next_record($rs)) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
rs_close();&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
foreach ($rs as $result) {&lt;br /&gt;
    ...&lt;br /&gt;
}&lt;br /&gt;
$rs-&amp;gt;close();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g5&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G5&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Placeholders must be used for table names. Instead of {$CFG-&amp;gt;prefix}tablename, use {tablename}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {$CFG-&amp;gt;prefix}user&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$sql = &amp;quot;SELECT * FROM {user}&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g6&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G6&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: When PHP variables are used in SQL queries, they must be replaced by parameters. You have the choice between two approaches: ordered parameters, or named parameters.&lt;br /&gt;
** Ordered parameters use a simple array of values, which are given to the DML function as $params. The values in the SQL code are simply question marks (?) replacing the values. They are replaced by the DML code one by one, substituting each question mark (?) with the next value in the $params array.&lt;br /&gt;
** Named parameters use an associative array of name =&amp;gt; value pairs as the $params array. The values in the SQL code are replaced with a colon (:) followed by the key associated with the value, in the $params array. Note that named params &#039;&#039;&#039;must be unique&#039;&#039;&#039;, no matter if the value passed is the same.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {$CFG-&amp;gt;prefix}user WHERE firstname = &#039;Peter&#039; AND lastname = &#039;Cantrophus&#039;&amp;quot;;&lt;br /&gt;
$user = get_record_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// New syntax: ordered params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;Peter&#039;, &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = ? AND lastname = ?&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&lt;br /&gt;
// New syntax: named params&lt;br /&gt;
global $DB;&lt;br /&gt;
$params = array(&#039;firstname&#039; =&amp;gt; &#039;Peter&#039;, &#039;lastname&#039; =&amp;gt; &#039;Cantrophus&#039;);&lt;br /&gt;
$sql = &amp;quot;SELECT id, firstname FROM {user} WHERE firstname = :firstname AND lastname = :lastname&amp;quot;;&lt;br /&gt;
$user = $DB-&amp;gt;get_record_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To obtain a value for the LIKE operator, include any % signs into the parameter string. For example, code that was previously &amp;lt;tt&amp;gt;&amp;quot;LIKE &#039;$value%&#039;&amp;quot;&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;&amp;quot;LIKE ?&amp;quot;&amp;lt;/tt&amp;gt; with the parameter &amp;lt;tt&amp;gt;$value.&#039;%&#039;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g7&amp;quot;&amp;gt;&amp;lt;b&amp;gt;G7&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Replacement of the IN(...) syntax: We no longer hard-code this in our SQL queries, we use a function which determines whether the IN() syntax is needed, or, if there is only one value to compare, the equal (=) sign can be used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax:&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
$gis = implode(&#039;,&#039;, $depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {$CFG-&amp;gt;prefix}grade_items&lt;br /&gt;
         WHERE id IN ($gis)&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql);&lt;br /&gt;
  &lt;br /&gt;
// new syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$depends_on = array(1, 43, 553);&lt;br /&gt;
list($usql, $params) = $DB-&amp;gt;get_in_or_equal($depends_on);&lt;br /&gt;
$sql = &amp;quot;SELECT *&lt;br /&gt;
          FROM {grade_items}&lt;br /&gt;
         WHERE id $usql&amp;quot;;&lt;br /&gt;
$items = $DB-&amp;gt;get_records_sql($sql, $params);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; to combine the last two, do the G7 IN SQL stuff first to generate the params array, then do something like&lt;br /&gt;
  $param[&#039;firstname&#039;] = &#039;peter&#039;;&lt;br /&gt;
to add the stuff for G6&lt;br /&gt;
&lt;br /&gt;
=== The iron changes ===&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;span id=&amp;quot;I1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;I1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally the &#039;&#039;&#039;sql_substr()&#039;&#039;&#039; function was used without parameters and it returned only the name of the &amp;quot;substring&amp;quot; function to be used under each DB. In Moodle 2.0 and upwards, it has 3 parameters (2 being mandatory) and it returns the complete SQL text to be used when handling substrings. Note that positions in this function are 1-based (first char has index 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$records = get_records_sql(&amp;quot;SELECT &amp;quot; . sql_substr() . &amp;quot;(firstname, 1, 20)&amp;quot; . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
$records = $DB-&amp;gt;get_records_sql(&amp;quot;SELECT &amp;quot; . $DB-&amp;gt;sql_substr(&#039;firstname&#039;, 1, 20) . &amp;quot; FROM ... ...&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The tin changes ===&lt;br /&gt;
List of minor changes&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T1&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally get_records() and similar functions were returning false if no records found. All these methods are now always returning arrays, empty array in case of no records found. Please note that get_record() still returns false if specified record not found.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
if (!$posts = get_records(&#039;forum_posts&#039;, &#039;parent&#039;, 666)) {&lt;br /&gt;
    $posts = array()&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$posts = $DB-&amp;gt;get_records(&#039;forum_posts&#039;, array(&#039;parent&#039;=&amp;gt;666));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span id=&amp;quot;g1&amp;quot;&amp;gt;&amp;lt;b&amp;gt;T2&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;: Originally DML functions were returning &#039;&#039;false&#039;&#039; if error occurred - &#039;&#039;dml_exception&#039;&#039; is thrown now instead.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
// Old syntax&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
if (!$id = insert_record(&#039;sometable&#039;, $record)) {&lt;br /&gt;
   error(&#039;can not insert new record&#039;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// New syntax&lt;br /&gt;
global $DB;&lt;br /&gt;
$record = new object();&lt;br /&gt;
$record-&amp;gt;course = 5;&lt;br /&gt;
$id = $DB-&amp;gt;insert_record(&#039;sometable&#039;, $record);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB Documentation|XMLDB Documentation]]: where both xmldb and ddl stuff is explained.&lt;br /&gt;
* [[Development:DDL functions|DDL functions]] - Documentation for all the Data Definition Language (DDL) functions available inside Moodle.&lt;br /&gt;
* [[Development:DML functions|DML functions]] - Documentation for all the Data Manipulation Language (DML) functions available inside Moodle.&lt;br /&gt;
* [[Development:DDL exceptions|DDL exceptions]] - DDL exceptions information.&lt;br /&gt;
* [[Development:DML exceptions|DML exceptions]] - DML exceptions information.&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=76073</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=76073"/>
		<updated>2010-09-22T23:56:10Z</updated>

		<summary type="html">&lt;p&gt;Agwells: Undo revision 76072 by Agwells (Talk)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou ([mailto:pj@moodle.org pj@moodle.org])&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a &#039;&#039;&#039;reference&#039;&#039;&#039; text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behaviour is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if (!empty($this-&amp;gt;config-&amp;gt;title)) {&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  } else {&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if (empty($this-&amp;gt;config-&amp;gt;text)) {&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&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;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&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;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
===String me up ===&lt;br /&gt;
&lt;br /&gt;
Now that we have a working config_global with saved values we want to be able to read the values from it. The simplest way to do this is to assign the config setting to a string and then use the string as we would any other. IE;&lt;br /&gt;
&lt;br /&gt;
$string = $CFG-&amp;gt;configsetting;&lt;br /&gt;
&lt;br /&gt;
You can check all the configuration settings available to a module by displaying them all with;&lt;br /&gt;
&lt;br /&gt;
print_object($CFG);  &lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;div&amp;amp;gt; or &amp;amp;lt;table&amp;amp;gt; elements, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we&#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
===and some other useful examples too:===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
    // Default case: an id with the instance and a class with our name in it&lt;br /&gt;
    return array(&#039;id&#039; =&amp;gt; &#039;inst&#039;.$this-&amp;gt;instance-&amp;gt;id, &#039;class&#039; =&amp;gt; &#039;block_&#039;. $this-&amp;gt;name());&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method should return an associative array of HTML attributes that will be given to your block&#039;s container element when Moodle constructs the output HTML. No sanitization will be performed in these elements at all.&lt;br /&gt;
&lt;br /&gt;
If you intend to override this method, you should return the default attributes as well as those you add yourself. The recommended way to do this is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
    $attrs = parent::html_attributes();&lt;br /&gt;
    // Add your own attributes here, e.g.&lt;br /&gt;
    // $attrs[&#039;width&#039;] = &#039;50%&#039;;&lt;br /&gt;
    return $attrs;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; true);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; false);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; true,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; false,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; true, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; false&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Responding to Cron ==&lt;br /&gt;
&lt;br /&gt;
It is possible to have your block respond to the cron process. That is have a method that is run at regular intervals regardless of user interaction. There are two parts to this. Firstly you need to define a new function within your block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function cron() {&lt;br /&gt;
    mtrace( &amp;quot;Hey, my cron script is running&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
    // do something&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;
Then within your init function you will need to set the (minimum) execution interval for your cron function. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    $this-&amp;gt;cron = 300;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Will set the minimum interval to 5 minutes. However, the function can only be called as frequently as cron has been set to run in the Moodle installation. Remember that if you change any values in the init function you &#039;&#039;&#039;must&#039;&#039;&#039; bump the version number and visit the Notifications page otherwise they will be ignored.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039;&lt;br /&gt;
The block cron is designed to call the cron script for that block &#039;&#039;&#039;type&#039;&#039;&#039; only. That is, cron does not care about individual instances of blocks. Inside your cron function although $this is defined it has almost nothing in it (only title, content type, version and cron fields are populated). If you need to execute cron for individual instances it is your own responsibility to iterate over them in the block&#039;s cron function. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function cron() {&lt;br /&gt;
&lt;br /&gt;
    // get the block type from the name&lt;br /&gt;
    $blocktype = get_record( &#039;block&#039;, &#039;name&#039;, &#039;my_block_name&#039; );&lt;br /&gt;
&lt;br /&gt;
    // get the instances of the block&lt;br /&gt;
    $instances = get_records( &#039;block_instance&#039;,&#039;blockid&#039;,$blocktype-&amp;gt;id );&lt;br /&gt;
&lt;br /&gt;
    // iterate over the instances&lt;br /&gt;
    foreach ($instances as $instance) {&lt;br /&gt;
&lt;br /&gt;
        // recreate block object&lt;br /&gt;
        $block = block_instance( &#039;my_block_name&#039;, $instance );&lt;br /&gt;
&lt;br /&gt;
        // $block is now the equivalent of $this in &#039;normal&#039; block&lt;br /&gt;
        // usage, e.g.&lt;br /&gt;
        $someconfigitem = $block-&amp;gt;config-&amp;gt;item2;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;my_block_name&#039; will coincide with the name of the directory the block is stored in.&lt;br /&gt;
&lt;br /&gt;
TIP: This also means that creating a block is a possible way to create code that can respond to cron with a reasonably low overhead. No actual instances of the block are required.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== null) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== Database support ==&lt;br /&gt;
In case you need to have a database table that holds some specific information that is used with your block. you will need to create a sub folder &#039;&#039;&#039;db&#039;&#039;&#039; and have an &#039;&#039;&#039;install.xml&#039;&#039;&#039; file with the table schema setup.&lt;br /&gt;
&lt;br /&gt;
To create the install.xml file, use the [[XMLDB editor]]. See [[Database_FAQ#XMLDB|Database FAQ &amp;gt; XMLDB]] for further details.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [http://dev.moodle.org/mod/resource/view.php?id=48 Unit 7 of the Introduction to Moodle Programming course] is a follow up to this course. (But you should follow the forum discussions of that course closely as there are still some bugs and inconsistencies.)&lt;br /&gt;
* A [http://cvs.moodle.org/contrib/plugins/blocks/NEWBLOCK/ NEWBLOCK template] you can all use to start you own block.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;br /&gt;
[[ja:開発:ブロック]]&lt;br /&gt;
[[ru:Development:Blocks]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=76072</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=76072"/>
		<updated>2010-09-22T23:54:38Z</updated>

		<summary type="html">&lt;p&gt;Agwells: Should use isset() before checking for a variable that may not be defined, in order to avoid a PHP warning&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou ([mailto:pj@moodle.org pj@moodle.org])&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a &#039;&#039;&#039;reference&#039;&#039;&#039; text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if (isset($this-&amp;gt;content) &amp;amp;&amp;amp; $this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behaviour is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if (!empty($this-&amp;gt;config-&amp;gt;title)) {&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  } else {&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if (empty($this-&amp;gt;config-&amp;gt;text)) {&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&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;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&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;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
===String me up ===&lt;br /&gt;
&lt;br /&gt;
Now that we have a working config_global with saved values we want to be able to read the values from it. The simplest way to do this is to assign the config setting to a string and then use the string as we would any other. IE;&lt;br /&gt;
&lt;br /&gt;
$string = $CFG-&amp;gt;configsetting;&lt;br /&gt;
&lt;br /&gt;
You can check all the configuration settings available to a module by displaying them all with;&lt;br /&gt;
&lt;br /&gt;
print_object($CFG);  &lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;div&amp;amp;gt; or &amp;amp;lt;table&amp;amp;gt; elements, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we&#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
===and some other useful examples too:===&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
    // Default case: an id with the instance and a class with our name in it&lt;br /&gt;
    return array(&#039;id&#039; =&amp;gt; &#039;inst&#039;.$this-&amp;gt;instance-&amp;gt;id, &#039;class&#039; =&amp;gt; &#039;block_&#039;. $this-&amp;gt;name());&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method should return an associative array of HTML attributes that will be given to your block&#039;s container element when Moodle constructs the output HTML. No sanitization will be performed in these elements at all.&lt;br /&gt;
&lt;br /&gt;
If you intend to override this method, you should return the default attributes as well as those you add yourself. The recommended way to do this is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
    $attrs = parent::html_attributes();&lt;br /&gt;
    // Add your own attributes here, e.g.&lt;br /&gt;
    // $attrs[&#039;width&#039;] = &#039;50%&#039;;&lt;br /&gt;
    return $attrs;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; true);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; false);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; true,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; false,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; true, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; false&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Responding to Cron ==&lt;br /&gt;
&lt;br /&gt;
It is possible to have your block respond to the cron process. That is have a method that is run at regular intervals regardless of user interaction. There are two parts to this. Firstly you need to define a new function within your block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function cron() {&lt;br /&gt;
    mtrace( &amp;quot;Hey, my cron script is running&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
    // do something&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;
Then within your init function you will need to set the (minimum) execution interval for your cron function. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    $this-&amp;gt;cron = 300;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Will set the minimum interval to 5 minutes. However, the function can only be called as frequently as cron has been set to run in the Moodle installation. Remember that if you change any values in the init function you &#039;&#039;&#039;must&#039;&#039;&#039; bump the version number and visit the Notifications page otherwise they will be ignored.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039;&lt;br /&gt;
The block cron is designed to call the cron script for that block &#039;&#039;&#039;type&#039;&#039;&#039; only. That is, cron does not care about individual instances of blocks. Inside your cron function although $this is defined it has almost nothing in it (only title, content type, version and cron fields are populated). If you need to execute cron for individual instances it is your own responsibility to iterate over them in the block&#039;s cron function. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function cron() {&lt;br /&gt;
&lt;br /&gt;
    // get the block type from the name&lt;br /&gt;
    $blocktype = get_record( &#039;block&#039;, &#039;name&#039;, &#039;my_block_name&#039; );&lt;br /&gt;
&lt;br /&gt;
    // get the instances of the block&lt;br /&gt;
    $instances = get_records( &#039;block_instance&#039;,&#039;blockid&#039;,$blocktype-&amp;gt;id );&lt;br /&gt;
&lt;br /&gt;
    // iterate over the instances&lt;br /&gt;
    foreach ($instances as $instance) {&lt;br /&gt;
&lt;br /&gt;
        // recreate block object&lt;br /&gt;
        $block = block_instance( &#039;my_block_name&#039;, $instance );&lt;br /&gt;
&lt;br /&gt;
        // $block is now the equivalent of $this in &#039;normal&#039; block&lt;br /&gt;
        // usage, e.g.&lt;br /&gt;
        $someconfigitem = $block-&amp;gt;config-&amp;gt;item2;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;my_block_name&#039; will coincide with the name of the directory the block is stored in.&lt;br /&gt;
&lt;br /&gt;
TIP: This also means that creating a block is a possible way to create code that can respond to cron with a reasonably low overhead. No actual instances of the block are required.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== null) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
{{Top}}&lt;br /&gt;
&lt;br /&gt;
== Database support ==&lt;br /&gt;
In case you need to have a database table that holds some specific information that is used with your block. you will need to create a sub folder &#039;&#039;&#039;db&#039;&#039;&#039; and have an &#039;&#039;&#039;install.xml&#039;&#039;&#039; file with the table schema setup.&lt;br /&gt;
&lt;br /&gt;
To create the install.xml file, use the [[XMLDB editor]]. See [[Database_FAQ#XMLDB|Database FAQ &amp;gt; XMLDB]] for further details.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [http://dev.moodle.org/mod/resource/view.php?id=48 Unit 7 of the Introduction to Moodle Programming course] is a follow up to this course. (But you should follow the forum discussions of that course closely as there are still some bugs and inconsistencies.)&lt;br /&gt;
* A [http://cvs.moodle.org/contrib/plugins/blocks/NEWBLOCK/ NEWBLOCK template] you can all use to start you own block.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;br /&gt;
[[ja:開発:ブロック]]&lt;br /&gt;
[[ru:Development:Blocks]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Aaron_Wells&amp;diff=76071</id>
		<title>Usuari:Aaron Wells</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Aaron_Wells&amp;diff=76071"/>
		<updated>2010-09-22T23:21:39Z</updated>

		<summary type="html">&lt;p&gt;Agwells: New page: http://moodle.org/user/view.php?id=1070092&amp;amp;course=1&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;http://moodle.org/user/view.php?id=1070092&amp;amp;course=1&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=76034</id>
		<title>Broken/Enrolment plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=76034"/>
		<updated>2010-09-22T06:10:04Z</updated>

		<summary type="html">&lt;p&gt;Agwells: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to write an Enrolment plugin in Moodle 1.9==&lt;br /&gt;
&lt;br /&gt;
Create a new directory called &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Within this directory, create a file &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;/enrol.php&amp;lt;/code&amp;gt; which contains the declaration for a class called &amp;lt;code&amp;gt;enrolment_plugin_&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class enrolment_plugin_PLUGINNAME {&lt;br /&gt;
    // Functions go here&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mandatory functions ===&lt;br /&gt;
&lt;br /&gt;
Your enrolment plugin class is required to define these two functions, which print and process a configuration form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints out the configuration form for this plugin. All we need&lt;br /&gt;
     * to provide is the form fields. The &amp;lt;form&amp;gt; tags and submit button will&lt;br /&gt;
     * be provided for us by Moodle.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata Equal to the global $CFG variable, or if&lt;br /&gt;
     *      process_config() returned false, the form contents&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function config_form( $formdata ){&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Process the data from the configuration form.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata&lt;br /&gt;
     * @return boolean True if configuration was successful, False if the user&lt;br /&gt;
     *      should be kicked back to config_form() again.&lt;br /&gt;
     */&lt;br /&gt;
    public function process_config( $formdata ){&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Optional functions ===&lt;br /&gt;
&lt;br /&gt;
==== Automated enrolment ====&lt;br /&gt;
&lt;br /&gt;
To handle automated enrolment, your function may contain a &amp;lt;code&amp;gt;setup_enrolments($user)&amp;lt;/code&amp;gt; function. This will be called each time a user logs in, to allow the plugin to make any changes to their enrolments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Set up non-interactive enrolments for user. This is the most&lt;br /&gt;
     * important function for non-interactive enrolment plugins (such as LDAP&lt;br /&gt;
     * and external database). It is called each time a user logs in.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $user&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function setup_enrolments( $user ){&lt;br /&gt;
        // Note that when you&#039;re setting up role assignments, you should use the&lt;br /&gt;
        // mdl_role_assignment.enrol column to indicate which role assignments&lt;br /&gt;
        // are from this plugin.&lt;br /&gt;
        role_assign($someroleid, $user-&amp;gt;id, null, $somecontextid, 0, 0, 0, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        role_unassign($someroleid, $user-&amp;gt;id, null, $somecontextid, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        &lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Interactive enrolment ====&lt;br /&gt;
&lt;br /&gt;
Your plugin can handle interactive enrolment (i.e. self-enrolment) by implementing these four functions. (See the manual enrolment plugin &amp;lt;code&amp;gt;enrol/manual/enrol.php&amp;lt;/code&amp;gt; for an example that uses all of these.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Prints the entry form/page for interactive enrolment into a course.&lt;br /&gt;
     * &lt;br /&gt;
     * This is only called from course/enrol.php. Most plugins will probably&lt;br /&gt;
     * override this to print payment forms, etc, or even just a notice to say&lt;br /&gt;
     * that manual enrollment is disabled.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function print_entry(){&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: The other half to print_entry(), this checks the form data.&lt;br /&gt;
     * &lt;br /&gt;
     * This function checks that the user has completed the task on the enrollment&lt;br /&gt;
     * entry page and enrolls them.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $form&lt;br /&gt;
     * @param object $course &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function check_entry( $form, $course ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        // some role_assign();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Check if the given enrolment key matches a group enrolment key for&lt;br /&gt;
     * the given course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param int $courseid&lt;br /&gt;
     * @param string $enrolmentkey&lt;br /&gt;
     * @return mixed The group id of the group which the key matches, or false&lt;br /&gt;
     *       if it matches none&lt;br /&gt;
     */&lt;br /&gt;
    public function check_group_entry( $courseid, $password ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        if ( $itlooksgood ){&lt;br /&gt;
            return $groupid;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Return a string with icons that give enrolment information&lt;br /&gt;
     * for this course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_access_icons( $course ){&lt;br /&gt;
        return &#039;put icons here&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cron ====&lt;br /&gt;
&lt;br /&gt;
Your plugin can perform cron tasks if it contains a &amp;lt;code&amp;gt;cron()&amp;lt;/code&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: If this function exists, it will be called each time&lt;br /&gt;
     * admin/cron.php runs. It takes no arguments and returns no values,&lt;br /&gt;
     * but anything written to $this-&amp;gt;log will be printed to the cron output.&lt;br /&gt;
     * &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function cron(){&lt;br /&gt;
        $this-&amp;gt;log = &#039;Print this in the cron log!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Language Strings ===&lt;br /&gt;
&lt;br /&gt;
Create a lang directory and file under your plugin&#039;s directory, like this &#039;&#039;&#039;&amp;lt;code&amp;gt;enrol/PLUGINNAME/lang/en_utf8/enrol_PLUGINNAME.php&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Plugins are expected to provide at least these two strings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// The name of your plugin. Displayed on admin menus.&lt;br /&gt;
$string[&#039;enrolname&#039;] = &#039;My plugin&#039;;&lt;br /&gt;
&lt;br /&gt;
// Description of your plugin. Shown on the plugin&#039;s configuration screen.&lt;br /&gt;
$string[&#039;description&#039;] = &#039;This is what my plugin does.&#039;;&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those two strings will be accessed automatically by Moodle in various situations. You can (and should) of course include other [[Development:Places to search for lang strings|language strings]] in your plugin&#039;s lang file(s). These can be accessed like normal language strings, using &amp;quot;enrol_PLUGINNAME&amp;quot; as the module name, e.g. &amp;lt;code&amp;gt;get_string(&#039;anotherstring&#039;, &#039;enrol_PLUGINNAME&#039;)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Database ===&lt;br /&gt;
&lt;br /&gt;
If your plugin needs to have its own Moodle database tables, create a file called &amp;lt;code&amp;gt;enrol/PLUGINNAME/version.php&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
$plugin-&amp;gt;version = 2010091600; // Version of your plugin&lt;br /&gt;
$plugin-&amp;gt;requires = 2007101000; // Required Moodle version (from /version.php)&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then create a directory &amp;lt;code&amp;gt;enrol/PLUGINNAME/db&amp;lt;/code&amp;gt;, and use the [[XMLDB editor]] to set up your tables.&lt;br /&gt;
&lt;br /&gt;
==Testing paypal using the paypal developer sandbox==&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.4 - 1.8 ===&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* change the address being used to send data to paypal to use the sandbox. in enrol/paypal/enrol.html:&lt;br /&gt;
** change the form post action to be https://www.sandbox.paypal.com/cgi-bin/webscr instead of https://www.paypal.com/cgi-bin/webscr&lt;br /&gt;
* change the address that is used to check the acknowledgment from paypal.  in enrol/paypal/ipn.php, change:&lt;br /&gt;
** $fp = fsockopen (&#039;www.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* to&lt;br /&gt;
** $fp = fsockopen (&#039;www.sandbox.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.8.2 - 1.9.x ===&lt;br /&gt;
&lt;br /&gt;
The above changes to the code were made part of the release starting with version 1.8.2&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
* add the following line to config.php located in the root directory of your installation (where you installed Moodle). &lt;br /&gt;
** $CFG-&amp;gt;usepaypalsandbox = &#039;TRUE&#039;;&lt;br /&gt;
* Add the business account email address from &amp;quot;business&amp;quot; account you created to the paypal module settings (in 1.9.x - Site Administration&amp;gt;Courses&amp;gt;Enrolments&amp;gt;Paypal&amp;gt;Edit)&lt;br /&gt;
* You should be ready to test&lt;br /&gt;
* When finished be sure to remove the line that was added to config.php in your root directory.&lt;br /&gt;
&lt;br /&gt;
=== Note about Paypal Sandbox ===&lt;br /&gt;
&lt;br /&gt;
Only implement these changes if the above is not working and Moodle is returning the &amp;quot;...Unfortunately your payment...&amp;quot; message after returning from Paypal Sandbox.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing (14 June 2008) some changes are necessary to test successfully in the Paypal Sandbox. Details can be found [http://www.pdncommunity.com/pdn/board/message?board.id=sandbox&amp;amp;view=by_date_ascending&amp;amp;message.id=9802#M9802 here]. This could change at any time at which point this may become unnecessary. For now do the following (only tested in 1.9.x so far):&lt;br /&gt;
&lt;br /&gt;
* Locate ipn.php in your installation on your server by migrating to to ../enrol/paypal/ipn.php&lt;br /&gt;
* Create a backup copy of ipn.php and give it a name something like ipn.php-backup &lt;br /&gt;
* Return to ipn.php and locate the following code :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);&lt;br /&gt;
* Change that code to :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;ssl://www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 443, $errno, $errstr, 30);&amp;lt;br /&amp;gt;&lt;br /&gt;
* Try the course purchase again. &lt;br /&gt;
&lt;br /&gt;
If the above still is not working you will need to seek help in the [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] Forum.&lt;br /&gt;
&lt;br /&gt;
==Enrolment plugins 1.7==&lt;br /&gt;
&lt;br /&gt;
===Background===&lt;br /&gt;
# isteacher(), isstudent(), isadmin(), iscoursecreator() are all deprecated and should be replaced with checks on has_capability instead.&lt;br /&gt;
# enrol_student() and add_teacher() are obsolete, so use the generic role_assign() or the convenience function for &amp;quot;students&amp;quot; called enrol_into_course() instead.&lt;br /&gt;
# unenrol_student() and remove_teacher() are obsolete, so use the generic role_unassign() instead.&lt;br /&gt;
# All the roles have a &amp;quot;shortname&amp;quot;, so by default we can refer to them using &amp;quot;teacher&amp;quot;, &amp;quot;student&amp;quot;, &amp;quot;editingteacher&amp;quot; etc.  Note that new roles may have different names.  Enrolment plugins can use these to map outside data onto the inside roles without needing to know internal role id numbers.   For example, it would be really handy for the flatfile plugin to refer to roles directly like this.&lt;br /&gt;
# Each plugin used to implement get_student_courses() and get_teacher_courses() for use at login time.  These now need to be converted to one new function called $enrol-&amp;gt;setup_enrolments($user) which looks up sets all enrolments for a given user (using role_assign and role_unassign).&lt;br /&gt;
# All the session arrays like $USER-&amp;gt;student[] and $USER-&amp;gt;teacher[] are gone and should not be relied on.  You can check what roles a user already has by calling get_user_roles() on that course context.&lt;br /&gt;
&lt;br /&gt;
===What&#039;s been done===&lt;br /&gt;
&lt;br /&gt;
#enrol/manual has been converted to use the new functions&lt;br /&gt;
#enrol/imsenterprise has been converted - testing needed though&lt;br /&gt;
&lt;br /&gt;
===What still needs to be done===&lt;br /&gt;
&lt;br /&gt;
#enrol/authorize&lt;br /&gt;
#enrol/database&lt;br /&gt;
#enrol/flatfile&lt;br /&gt;
#enrol/ldap&lt;br /&gt;
#enrol/paypal&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Enrolment plugins|Enrolment plugins (administrator)]]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] forum&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Enrolment plugins]]&lt;br /&gt;
[[Category:Enrolment]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75898</id>
		<title>Broken/Enrolment plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75898"/>
		<updated>2010-09-16T04:42:03Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Database */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to write an Enrolment plugin in Moodle 1.9==&lt;br /&gt;
&lt;br /&gt;
Create a new directory called &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Within this directory, create a file &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;/enrol.php&amp;lt;/code&amp;gt; which contains the declaration for a class called &amp;lt;code&amp;gt;enrolment_plugin_&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class enrolment_plugin_PLUGINNAME {&lt;br /&gt;
    // Functions go here&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mandatory functions ===&lt;br /&gt;
&lt;br /&gt;
Your enrolment plugin class is required to define these two functions, which print and process a configuration form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints out the configuration form for this plugin. All we need&lt;br /&gt;
     * to provide is the form fields. The &amp;lt;form&amp;gt; tags and submit button will&lt;br /&gt;
     * be provided for us by Moodle.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata Equal to the global $CFG variable, or if&lt;br /&gt;
     *      process_config() returned false, the form contents&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function config_form( $formdata ){&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Process the data from the configuration form.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata&lt;br /&gt;
     * @return boolean True if configuration was successful, False if the user&lt;br /&gt;
     *      should be kicked back to config_form() again.&lt;br /&gt;
     */&lt;br /&gt;
    public function process_config( $formdata ){&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automated enrolment ===&lt;br /&gt;
&lt;br /&gt;
To handle automated enrolment, your function may contain a &amp;lt;code&amp;gt;setup_enrolments($user)&amp;lt;/code&amp;gt; function. This will be called each time a user logs in, to allow the plugin to make any changes to their enrolments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Set up non-interactive enrolments for user. This is the most&lt;br /&gt;
     * important function for non-interactive enrolment plugins (such as LDAP&lt;br /&gt;
     * and external database). It is called each time a user logs in.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $user&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function setup_enrolments( $user ){&lt;br /&gt;
        // Note that when you&#039;re setting up role assignments, you should use the&lt;br /&gt;
        // mdl_role_assignment.enrol column to indicate which role assignments&lt;br /&gt;
        // are from this plugin.&lt;br /&gt;
        role_assign($someroleid, $user-&amp;gt;id, null, $somecontextid, 0, 0, 0, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        role_unassign($someroleid, $user-&amp;gt;id, null, $somecontextid, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        &lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Interactive enrolment ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can handle interactive enrolment (i.e. self-enrolment) by implementing these four functions. (See the manual enrolment plugin &amp;lt;code&amp;gt;enrol/manual/enrol.php&amp;lt;/code&amp;gt; for an example that uses all of these.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Prints the entry form/page for interactive enrolment into a course.&lt;br /&gt;
     * &lt;br /&gt;
     * This is only called from course/enrol.php. Most plugins will probably&lt;br /&gt;
     * override this to print payment forms, etc, or even just a notice to say&lt;br /&gt;
     * that manual enrollment is disabled.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function print_entry(){&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: The other half to print_entry(), this checks the form data.&lt;br /&gt;
     * &lt;br /&gt;
     * This function checks that the user has completed the task on the enrollment&lt;br /&gt;
     * entry page and enrolls them.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $form&lt;br /&gt;
     * @param object $course &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function check_entry( $form, $course ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        // some role_assign();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Check if the given enrolment key matches a group enrolment key for&lt;br /&gt;
     * the given course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param int $courseid&lt;br /&gt;
     * @param string $enrolmentkey&lt;br /&gt;
     * @return mixed The group id of the group which the key matches, or false&lt;br /&gt;
     *       if it matches none&lt;br /&gt;
     */&lt;br /&gt;
    public function check_group_entry( $courseid, $password ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        if ( $itlooksgood ){&lt;br /&gt;
            return $groupid;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Return a string with icons that give enrolment information&lt;br /&gt;
     * for this course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_access_icons( $course ){&lt;br /&gt;
        return &#039;put icons here&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cron ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can perform cron tasks if it contains a &amp;lt;code&amp;gt;cron()&amp;lt;/code&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: If this function exists, it will be called each time&lt;br /&gt;
     * admin/cron.php runs. It takes no arguments and returns no values,&lt;br /&gt;
     * but anything written to $this-&amp;gt;log will be printed to the cron output.&lt;br /&gt;
     * &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function cron(){&lt;br /&gt;
        $this-&amp;gt;log = &#039;Print this in the cron log!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Database ===&lt;br /&gt;
&lt;br /&gt;
If your plugin needs to have its own Moodle database tables, create a file called &amp;lt;code&amp;gt;enrol/PLUGINNAME/version.php&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
$plugin-&amp;gt;version = 2010091600; // Version of your plugin&lt;br /&gt;
$plugin-&amp;gt;requires = 2007101000; // Required Moodle version (from /version.php)&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then create a directory &amp;lt;code&amp;gt;enrol/PLUGINNAME/db&amp;lt;/code&amp;gt;, and use the [[XMLDB editor]] to set up your tables.&lt;br /&gt;
&lt;br /&gt;
==Testing paypal using the paypal developer sandbox==&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.4 - 1.8 ===&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* change the address being used to send data to paypal to use the sandbox. in enrol/paypal/enrol.html:&lt;br /&gt;
** change the form post action to be https://www.sandbox.paypal.com/cgi-bin/webscr instead of https://www.paypal.com/cgi-bin/webscr&lt;br /&gt;
* change the address that is used to check the acknowledgment from paypal.  in enrol/paypal/ipn.php, change:&lt;br /&gt;
** $fp = fsockopen (&#039;www.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* to&lt;br /&gt;
** $fp = fsockopen (&#039;www.sandbox.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.8.2 - 1.9.x ===&lt;br /&gt;
&lt;br /&gt;
The above changes to the code were made part of the release starting with version 1.8.2&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
* add the following line to config.php located in the root directory of your installation (where you installed Moodle). &lt;br /&gt;
** $CFG-&amp;gt;usepaypalsandbox = &#039;TRUE&#039;;&lt;br /&gt;
* Add the business account email address from &amp;quot;business&amp;quot; account you created to the paypal module settings (in 1.9.x - Site Administration&amp;gt;Courses&amp;gt;Enrolments&amp;gt;Paypal&amp;gt;Edit)&lt;br /&gt;
* You should be ready to test&lt;br /&gt;
* When finished be sure to remove the line that was added to config.php in your root directory.&lt;br /&gt;
&lt;br /&gt;
=== Note about Paypal Sandbox ===&lt;br /&gt;
&lt;br /&gt;
Only implement these changes if the above is not working and Moodle is returning the &amp;quot;...Unfortunately your payment...&amp;quot; message after returning from Paypal Sandbox.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing (14 June 2008) some changes are necessary to test successfully in the Paypal Sandbox. Details can be found [http://www.pdncommunity.com/pdn/board/message?board.id=sandbox&amp;amp;view=by_date_ascending&amp;amp;message.id=9802#M9802 here]. This could change at any time at which point this may become unnecessary. For now do the following (only tested in 1.9.x so far):&lt;br /&gt;
&lt;br /&gt;
* Locate ipn.php in your installation on your server by migrating to to ../enrol/paypal/ipn.php&lt;br /&gt;
* Create a backup copy of ipn.php and give it a name something like ipn.php-backup &lt;br /&gt;
* Return to ipn.php and locate the following code :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);&lt;br /&gt;
* Change that code to :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;ssl://www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 443, $errno, $errstr, 30);&amp;lt;br /&amp;gt;&lt;br /&gt;
* Try the course purchase again. &lt;br /&gt;
&lt;br /&gt;
If the above still is not working you will need to seek help in the [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] Forum.&lt;br /&gt;
&lt;br /&gt;
==Enrolment plugins 1.7==&lt;br /&gt;
&lt;br /&gt;
===Background===&lt;br /&gt;
# isteacher(), isstudent(), isadmin(), iscoursecreator() are all deprecated and should be replaced with checks on has_capability instead.&lt;br /&gt;
# enrol_student() and add_teacher() are obsolete, so use the generic role_assign() or the convenience function for &amp;quot;students&amp;quot; called enrol_into_course() instead.&lt;br /&gt;
# unenrol_student() and remove_teacher() are obsolete, so use the generic role_unassign() instead.&lt;br /&gt;
# All the roles have a &amp;quot;shortname&amp;quot;, so by default we can refer to them using &amp;quot;teacher&amp;quot;, &amp;quot;student&amp;quot;, &amp;quot;editingteacher&amp;quot; etc.  Note that new roles may have different names.  Enrolment plugins can use these to map outside data onto the inside roles without needing to know internal role id numbers.   For example, it would be really handy for the flatfile plugin to refer to roles directly like this.&lt;br /&gt;
# Each plugin used to implement get_student_courses() and get_teacher_courses() for use at login time.  These now need to be converted to one new function called $enrol-&amp;gt;setup_enrolments($user) which looks up sets all enrolments for a given user (using role_assign and role_unassign).&lt;br /&gt;
# All the session arrays like $USER-&amp;gt;student[] and $USER-&amp;gt;teacher[] are gone and should not be relied on.  You can check what roles a user already has by calling get_user_roles() on that course context.&lt;br /&gt;
&lt;br /&gt;
===What&#039;s been done===&lt;br /&gt;
&lt;br /&gt;
#enrol/manual has been converted to use the new functions&lt;br /&gt;
#enrol/imsenterprise has been converted - testing needed though&lt;br /&gt;
&lt;br /&gt;
===What still needs to be done===&lt;br /&gt;
&lt;br /&gt;
#enrol/authorize&lt;br /&gt;
#enrol/database&lt;br /&gt;
#enrol/flatfile&lt;br /&gt;
#enrol/ldap&lt;br /&gt;
#enrol/paypal&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Enrolment plugins|Enrolment plugins (administrator)]]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] forum&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Enrolment plugins]]&lt;br /&gt;
[[Category:Enrolment]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75897</id>
		<title>Broken/Enrolment plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75897"/>
		<updated>2010-09-16T04:41:05Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Automated enrolment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to write an Enrolment plugin in Moodle 1.9==&lt;br /&gt;
&lt;br /&gt;
Create a new directory called &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Within this directory, create a file &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;/enrol.php&amp;lt;/code&amp;gt; which contains the declaration for a class called &amp;lt;code&amp;gt;enrolment_plugin_&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class enrolment_plugin_PLUGINNAME {&lt;br /&gt;
    // Functions go here&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mandatory functions ===&lt;br /&gt;
&lt;br /&gt;
Your enrolment plugin class is required to define these two functions, which print and process a configuration form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints out the configuration form for this plugin. All we need&lt;br /&gt;
     * to provide is the form fields. The &amp;lt;form&amp;gt; tags and submit button will&lt;br /&gt;
     * be provided for us by Moodle.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata Equal to the global $CFG variable, or if&lt;br /&gt;
     *      process_config() returned false, the form contents&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function config_form( $formdata ){&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Process the data from the configuration form.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata&lt;br /&gt;
     * @return boolean True if configuration was successful, False if the user&lt;br /&gt;
     *      should be kicked back to config_form() again.&lt;br /&gt;
     */&lt;br /&gt;
    public function process_config( $formdata ){&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automated enrolment ===&lt;br /&gt;
&lt;br /&gt;
To handle automated enrolment, your function may contain a &amp;lt;code&amp;gt;setup_enrolments($user)&amp;lt;/code&amp;gt; function. This will be called each time a user logs in, to allow the plugin to make any changes to their enrolments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Set up non-interactive enrolments for user. This is the most&lt;br /&gt;
     * important function for non-interactive enrolment plugins (such as LDAP&lt;br /&gt;
     * and external database). It is called each time a user logs in.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $user&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function setup_enrolments( $user ){&lt;br /&gt;
        // Note that when you&#039;re setting up role assignments, you should use the&lt;br /&gt;
        // mdl_role_assignment.enrol column to indicate which role assignments&lt;br /&gt;
        // are from this plugin.&lt;br /&gt;
        role_assign($someroleid, $user-&amp;gt;id, null, $somecontextid, 0, 0, 0, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        role_unassign($someroleid, $user-&amp;gt;id, null, $somecontextid, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        &lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Interactive enrolment ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can handle interactive enrolment (i.e. self-enrolment) by implementing these four functions. (See the manual enrolment plugin &amp;lt;code&amp;gt;enrol/manual/enrol.php&amp;lt;/code&amp;gt; for an example that uses all of these.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Prints the entry form/page for interactive enrolment into a course.&lt;br /&gt;
     * &lt;br /&gt;
     * This is only called from course/enrol.php. Most plugins will probably&lt;br /&gt;
     * override this to print payment forms, etc, or even just a notice to say&lt;br /&gt;
     * that manual enrollment is disabled.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function print_entry(){&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: The other half to print_entry(), this checks the form data.&lt;br /&gt;
     * &lt;br /&gt;
     * This function checks that the user has completed the task on the enrollment&lt;br /&gt;
     * entry page and enrolls them.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $form&lt;br /&gt;
     * @param object $course &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function check_entry( $form, $course ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        // some role_assign();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Check if the given enrolment key matches a group enrolment key for&lt;br /&gt;
     * the given course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param int $courseid&lt;br /&gt;
     * @param string $enrolmentkey&lt;br /&gt;
     * @return mixed The group id of the group which the key matches, or false&lt;br /&gt;
     *       if it matches none&lt;br /&gt;
     */&lt;br /&gt;
    public function check_group_entry( $courseid, $password ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        if ( $itlooksgood ){&lt;br /&gt;
            return $groupid;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Return a string with icons that give enrolment information&lt;br /&gt;
     * for this course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_access_icons( $course ){&lt;br /&gt;
        return &#039;put icons here&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cron ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can perform cron tasks if it contains a &amp;lt;code&amp;gt;cron()&amp;lt;/code&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: If this function exists, it will be called each time&lt;br /&gt;
     * admin/cron.php runs. It takes no arguments and returns no values,&lt;br /&gt;
     * but anything written to $this-&amp;gt;log will be printed to the cron output.&lt;br /&gt;
     * &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function cron(){&lt;br /&gt;
        $this-&amp;gt;log = &#039;Print this in the cron log!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Database ===&lt;br /&gt;
&lt;br /&gt;
If your plugin needs to have its own Moodle database tables, create a file called &amp;lt;code&amp;gt;enrol/PLUGINNAME/version.php&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
$plugin-&amp;gt;version = 2010091600; // Version of your plugin&lt;br /&gt;
$plugin-&amp;gt;requires = 2007101000; // Required Moodle version (from /version.php)&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then create a directory &amp;lt;code&amp;gt;enrol/PLUGINNAME/db&amp;lt;/code&amp;gt;, and use the [[XMLDB Editor]] to set up your tables.&lt;br /&gt;
&lt;br /&gt;
==Testing paypal using the paypal developer sandbox==&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.4 - 1.8 ===&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* change the address being used to send data to paypal to use the sandbox. in enrol/paypal/enrol.html:&lt;br /&gt;
** change the form post action to be https://www.sandbox.paypal.com/cgi-bin/webscr instead of https://www.paypal.com/cgi-bin/webscr&lt;br /&gt;
* change the address that is used to check the acknowledgment from paypal.  in enrol/paypal/ipn.php, change:&lt;br /&gt;
** $fp = fsockopen (&#039;www.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* to&lt;br /&gt;
** $fp = fsockopen (&#039;www.sandbox.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.8.2 - 1.9.x ===&lt;br /&gt;
&lt;br /&gt;
The above changes to the code were made part of the release starting with version 1.8.2&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
* add the following line to config.php located in the root directory of your installation (where you installed Moodle). &lt;br /&gt;
** $CFG-&amp;gt;usepaypalsandbox = &#039;TRUE&#039;;&lt;br /&gt;
* Add the business account email address from &amp;quot;business&amp;quot; account you created to the paypal module settings (in 1.9.x - Site Administration&amp;gt;Courses&amp;gt;Enrolments&amp;gt;Paypal&amp;gt;Edit)&lt;br /&gt;
* You should be ready to test&lt;br /&gt;
* When finished be sure to remove the line that was added to config.php in your root directory.&lt;br /&gt;
&lt;br /&gt;
=== Note about Paypal Sandbox ===&lt;br /&gt;
&lt;br /&gt;
Only implement these changes if the above is not working and Moodle is returning the &amp;quot;...Unfortunately your payment...&amp;quot; message after returning from Paypal Sandbox.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing (14 June 2008) some changes are necessary to test successfully in the Paypal Sandbox. Details can be found [http://www.pdncommunity.com/pdn/board/message?board.id=sandbox&amp;amp;view=by_date_ascending&amp;amp;message.id=9802#M9802 here]. This could change at any time at which point this may become unnecessary. For now do the following (only tested in 1.9.x so far):&lt;br /&gt;
&lt;br /&gt;
* Locate ipn.php in your installation on your server by migrating to to ../enrol/paypal/ipn.php&lt;br /&gt;
* Create a backup copy of ipn.php and give it a name something like ipn.php-backup &lt;br /&gt;
* Return to ipn.php and locate the following code :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);&lt;br /&gt;
* Change that code to :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;ssl://www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 443, $errno, $errstr, 30);&amp;lt;br /&amp;gt;&lt;br /&gt;
* Try the course purchase again. &lt;br /&gt;
&lt;br /&gt;
If the above still is not working you will need to seek help in the [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] Forum.&lt;br /&gt;
&lt;br /&gt;
==Enrolment plugins 1.7==&lt;br /&gt;
&lt;br /&gt;
===Background===&lt;br /&gt;
# isteacher(), isstudent(), isadmin(), iscoursecreator() are all deprecated and should be replaced with checks on has_capability instead.&lt;br /&gt;
# enrol_student() and add_teacher() are obsolete, so use the generic role_assign() or the convenience function for &amp;quot;students&amp;quot; called enrol_into_course() instead.&lt;br /&gt;
# unenrol_student() and remove_teacher() are obsolete, so use the generic role_unassign() instead.&lt;br /&gt;
# All the roles have a &amp;quot;shortname&amp;quot;, so by default we can refer to them using &amp;quot;teacher&amp;quot;, &amp;quot;student&amp;quot;, &amp;quot;editingteacher&amp;quot; etc.  Note that new roles may have different names.  Enrolment plugins can use these to map outside data onto the inside roles without needing to know internal role id numbers.   For example, it would be really handy for the flatfile plugin to refer to roles directly like this.&lt;br /&gt;
# Each plugin used to implement get_student_courses() and get_teacher_courses() for use at login time.  These now need to be converted to one new function called $enrol-&amp;gt;setup_enrolments($user) which looks up sets all enrolments for a given user (using role_assign and role_unassign).&lt;br /&gt;
# All the session arrays like $USER-&amp;gt;student[] and $USER-&amp;gt;teacher[] are gone and should not be relied on.  You can check what roles a user already has by calling get_user_roles() on that course context.&lt;br /&gt;
&lt;br /&gt;
===What&#039;s been done===&lt;br /&gt;
&lt;br /&gt;
#enrol/manual has been converted to use the new functions&lt;br /&gt;
#enrol/imsenterprise has been converted - testing needed though&lt;br /&gt;
&lt;br /&gt;
===What still needs to be done===&lt;br /&gt;
&lt;br /&gt;
#enrol/authorize&lt;br /&gt;
#enrol/database&lt;br /&gt;
#enrol/flatfile&lt;br /&gt;
#enrol/ldap&lt;br /&gt;
#enrol/paypal&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Enrolment plugins|Enrolment plugins (administrator)]]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] forum&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Enrolment plugins]]&lt;br /&gt;
[[Category:Enrolment]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75896</id>
		<title>Broken/Enrolment plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75896"/>
		<updated>2010-09-16T04:40:09Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* How to write an Enrolment plugin in Moodle 1.9 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to write an Enrolment plugin in Moodle 1.9==&lt;br /&gt;
&lt;br /&gt;
Create a new directory called &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Within this directory, create a file &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;/enrol.php&amp;lt;/code&amp;gt; which contains the declaration for a class called &amp;lt;code&amp;gt;enrolment_plugin_&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class enrolment_plugin_PLUGINNAME {&lt;br /&gt;
    // Functions go here&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mandatory functions ===&lt;br /&gt;
&lt;br /&gt;
Your enrolment plugin class is required to define these two functions, which print and process a configuration form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints out the configuration form for this plugin. All we need&lt;br /&gt;
     * to provide is the form fields. The &amp;lt;form&amp;gt; tags and submit button will&lt;br /&gt;
     * be provided for us by Moodle.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata Equal to the global $CFG variable, or if&lt;br /&gt;
     *      process_config() returned false, the form contents&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function config_form( $formdata ){&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Process the data from the configuration form.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata&lt;br /&gt;
     * @return boolean True if configuration was successful, False if the user&lt;br /&gt;
     *      should be kicked back to config_form() again.&lt;br /&gt;
     */&lt;br /&gt;
    public function process_config( $formdata ){&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automated enrolment ===&lt;br /&gt;
&lt;br /&gt;
To handle automated enrolment, your function may contain a &amp;lt;code&amp;gt;setup_enrolments($user)&amp;lt;/code&amp;gt; function. This will be called each time a user logs in, to allow the plugin to make any changes to their enrolments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Set up non-interactive enrolments for user. This is the most&lt;br /&gt;
     * important function for non-interactive enrolment plugins (such as LDAP&lt;br /&gt;
     * and external database). It is called each time a user logs in.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $user&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function setup_enrolments( $user ){&lt;br /&gt;
        // Note that when you&#039;re setting up role assignments, you should use the&lt;br /&gt;
        // mdl_role_assignment.auth column to indicate which role assignments&lt;br /&gt;
        // are from this plugin.&lt;br /&gt;
        role_assign($someroleid, $user-&amp;gt;id, null, $somecontextid, 0, 0, 0, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        role_unassign($someroleid, $user-&amp;gt;id, null, $somecontextid, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        &lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Interactive enrolment ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can handle interactive enrolment (i.e. self-enrolment) by implementing these four functions. (See the manual enrolment plugin &amp;lt;code&amp;gt;enrol/manual/enrol.php&amp;lt;/code&amp;gt; for an example that uses all of these.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Prints the entry form/page for interactive enrolment into a course.&lt;br /&gt;
     * &lt;br /&gt;
     * This is only called from course/enrol.php. Most plugins will probably&lt;br /&gt;
     * override this to print payment forms, etc, or even just a notice to say&lt;br /&gt;
     * that manual enrollment is disabled.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function print_entry(){&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: The other half to print_entry(), this checks the form data.&lt;br /&gt;
     * &lt;br /&gt;
     * This function checks that the user has completed the task on the enrollment&lt;br /&gt;
     * entry page and enrolls them.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $form&lt;br /&gt;
     * @param object $course &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function check_entry( $form, $course ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        // some role_assign();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Check if the given enrolment key matches a group enrolment key for&lt;br /&gt;
     * the given course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param int $courseid&lt;br /&gt;
     * @param string $enrolmentkey&lt;br /&gt;
     * @return mixed The group id of the group which the key matches, or false&lt;br /&gt;
     *       if it matches none&lt;br /&gt;
     */&lt;br /&gt;
    public function check_group_entry( $courseid, $password ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        if ( $itlooksgood ){&lt;br /&gt;
            return $groupid;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Return a string with icons that give enrolment information&lt;br /&gt;
     * for this course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_access_icons( $course ){&lt;br /&gt;
        return &#039;put icons here&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cron ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can perform cron tasks if it contains a &amp;lt;code&amp;gt;cron()&amp;lt;/code&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: If this function exists, it will be called each time&lt;br /&gt;
     * admin/cron.php runs. It takes no arguments and returns no values,&lt;br /&gt;
     * but anything written to $this-&amp;gt;log will be printed to the cron output.&lt;br /&gt;
     * &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function cron(){&lt;br /&gt;
        $this-&amp;gt;log = &#039;Print this in the cron log!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Database ===&lt;br /&gt;
&lt;br /&gt;
If your plugin needs to have its own Moodle database tables, create a file called &amp;lt;code&amp;gt;enrol/PLUGINNAME/version.php&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
$plugin-&amp;gt;version = 2010091600; // Version of your plugin&lt;br /&gt;
$plugin-&amp;gt;requires = 2007101000; // Required Moodle version (from /version.php)&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then create a directory &amp;lt;code&amp;gt;enrol/PLUGINNAME/db&amp;lt;/code&amp;gt;, and use the [[XMLDB Editor]] to set up your tables.&lt;br /&gt;
&lt;br /&gt;
==Testing paypal using the paypal developer sandbox==&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.4 - 1.8 ===&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* change the address being used to send data to paypal to use the sandbox. in enrol/paypal/enrol.html:&lt;br /&gt;
** change the form post action to be https://www.sandbox.paypal.com/cgi-bin/webscr instead of https://www.paypal.com/cgi-bin/webscr&lt;br /&gt;
* change the address that is used to check the acknowledgment from paypal.  in enrol/paypal/ipn.php, change:&lt;br /&gt;
** $fp = fsockopen (&#039;www.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* to&lt;br /&gt;
** $fp = fsockopen (&#039;www.sandbox.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.8.2 - 1.9.x ===&lt;br /&gt;
&lt;br /&gt;
The above changes to the code were made part of the release starting with version 1.8.2&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
* add the following line to config.php located in the root directory of your installation (where you installed Moodle). &lt;br /&gt;
** $CFG-&amp;gt;usepaypalsandbox = &#039;TRUE&#039;;&lt;br /&gt;
* Add the business account email address from &amp;quot;business&amp;quot; account you created to the paypal module settings (in 1.9.x - Site Administration&amp;gt;Courses&amp;gt;Enrolments&amp;gt;Paypal&amp;gt;Edit)&lt;br /&gt;
* You should be ready to test&lt;br /&gt;
* When finished be sure to remove the line that was added to config.php in your root directory.&lt;br /&gt;
&lt;br /&gt;
=== Note about Paypal Sandbox ===&lt;br /&gt;
&lt;br /&gt;
Only implement these changes if the above is not working and Moodle is returning the &amp;quot;...Unfortunately your payment...&amp;quot; message after returning from Paypal Sandbox.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing (14 June 2008) some changes are necessary to test successfully in the Paypal Sandbox. Details can be found [http://www.pdncommunity.com/pdn/board/message?board.id=sandbox&amp;amp;view=by_date_ascending&amp;amp;message.id=9802#M9802 here]. This could change at any time at which point this may become unnecessary. For now do the following (only tested in 1.9.x so far):&lt;br /&gt;
&lt;br /&gt;
* Locate ipn.php in your installation on your server by migrating to to ../enrol/paypal/ipn.php&lt;br /&gt;
* Create a backup copy of ipn.php and give it a name something like ipn.php-backup &lt;br /&gt;
* Return to ipn.php and locate the following code :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);&lt;br /&gt;
* Change that code to :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;ssl://www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 443, $errno, $errstr, 30);&amp;lt;br /&amp;gt;&lt;br /&gt;
* Try the course purchase again. &lt;br /&gt;
&lt;br /&gt;
If the above still is not working you will need to seek help in the [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] Forum.&lt;br /&gt;
&lt;br /&gt;
==Enrolment plugins 1.7==&lt;br /&gt;
&lt;br /&gt;
===Background===&lt;br /&gt;
# isteacher(), isstudent(), isadmin(), iscoursecreator() are all deprecated and should be replaced with checks on has_capability instead.&lt;br /&gt;
# enrol_student() and add_teacher() are obsolete, so use the generic role_assign() or the convenience function for &amp;quot;students&amp;quot; called enrol_into_course() instead.&lt;br /&gt;
# unenrol_student() and remove_teacher() are obsolete, so use the generic role_unassign() instead.&lt;br /&gt;
# All the roles have a &amp;quot;shortname&amp;quot;, so by default we can refer to them using &amp;quot;teacher&amp;quot;, &amp;quot;student&amp;quot;, &amp;quot;editingteacher&amp;quot; etc.  Note that new roles may have different names.  Enrolment plugins can use these to map outside data onto the inside roles without needing to know internal role id numbers.   For example, it would be really handy for the flatfile plugin to refer to roles directly like this.&lt;br /&gt;
# Each plugin used to implement get_student_courses() and get_teacher_courses() for use at login time.  These now need to be converted to one new function called $enrol-&amp;gt;setup_enrolments($user) which looks up sets all enrolments for a given user (using role_assign and role_unassign).&lt;br /&gt;
# All the session arrays like $USER-&amp;gt;student[] and $USER-&amp;gt;teacher[] are gone and should not be relied on.  You can check what roles a user already has by calling get_user_roles() on that course context.&lt;br /&gt;
&lt;br /&gt;
===What&#039;s been done===&lt;br /&gt;
&lt;br /&gt;
#enrol/manual has been converted to use the new functions&lt;br /&gt;
#enrol/imsenterprise has been converted - testing needed though&lt;br /&gt;
&lt;br /&gt;
===What still needs to be done===&lt;br /&gt;
&lt;br /&gt;
#enrol/authorize&lt;br /&gt;
#enrol/database&lt;br /&gt;
#enrol/flatfile&lt;br /&gt;
#enrol/ldap&lt;br /&gt;
#enrol/paypal&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Enrolment plugins|Enrolment plugins (administrator)]]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] forum&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Enrolment plugins]]&lt;br /&gt;
[[Category:Enrolment]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75895</id>
		<title>Broken/Enrolment plugins</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Enrolment_plugins&amp;diff=75895"/>
		<updated>2010-09-16T04:39:41Z</updated>

		<summary type="html">&lt;p&gt;Agwells: Adding some documentation about how to write an enrolment plugin&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==How to write an Enrolment plugin in Moodle 1.9==&lt;br /&gt;
&lt;br /&gt;
Create a new directory called &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;. Within this directory, create a file &amp;lt;code&amp;gt;/enrol/&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;/enrol.php&amp;lt;/code&amp;gt; which contains the declaration for a class called &amp;lt;code&amp;gt;enrolment_plugin_&amp;lt;/code&amp;gt;&#039;&#039;&#039;&amp;lt;code&amp;gt;PLUGINNAME&amp;lt;/code&amp;gt;&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class enrolment_plugin_NAME {&lt;br /&gt;
    // Functions go here&lt;br /&gt;
    // ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mandatory functions ===&lt;br /&gt;
&lt;br /&gt;
Your enrolment plugin class is required to define these two functions, which print and process a configuration form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * Prints out the configuration form for this plugin. All we need&lt;br /&gt;
     * to provide is the form fields. The &amp;lt;form&amp;gt; tags and submit button will&lt;br /&gt;
     * be provided for us by Moodle.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata Equal to the global $CFG variable, or if&lt;br /&gt;
     *      process_config() returned false, the form contents&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function config_form( $formdata ){&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Process the data from the configuration form.&lt;br /&gt;
     *&lt;br /&gt;
     * @param object $formdata&lt;br /&gt;
     * @return boolean True if configuration was successful, False if the user&lt;br /&gt;
     *      should be kicked back to config_form() again.&lt;br /&gt;
     */&lt;br /&gt;
    public function process_config( $formdata ){&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automated enrolment ===&lt;br /&gt;
&lt;br /&gt;
To handle automated enrolment, your function may contain a &amp;lt;code&amp;gt;setup_enrolments($user)&amp;lt;/code&amp;gt; function. This will be called each time a user logs in, to allow the plugin to make any changes to their enrolments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Set up non-interactive enrolments for user. This is the most&lt;br /&gt;
     * important function for non-interactive enrolment plugins (such as LDAP&lt;br /&gt;
     * and external database). It is called each time a user logs in.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $user&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function setup_enrolments( $user ){&lt;br /&gt;
        // Note that when you&#039;re setting up role assignments, you should use the&lt;br /&gt;
        // mdl_role_assignment.auth column to indicate which role assignments&lt;br /&gt;
        // are from this plugin.&lt;br /&gt;
        role_assign($someroleid, $user-&amp;gt;id, null, $somecontextid, 0, 0, 0, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        role_unassign($someroleid, $user-&amp;gt;id, null, $somecontextid, &#039;PLUGINNAME&#039;);&lt;br /&gt;
        &lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Interactive enrolment ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can handle interactive enrolment (i.e. self-enrolment) by implementing these four functions. (See the manual enrolment plugin &amp;lt;code&amp;gt;enrol/manual/enrol.php&amp;lt;/code&amp;gt; for an example that uses all of these.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Prints the entry form/page for interactive enrolment into a course.&lt;br /&gt;
     * &lt;br /&gt;
     * This is only called from course/enrol.php. Most plugins will probably&lt;br /&gt;
     * override this to print payment forms, etc, or even just a notice to say&lt;br /&gt;
     * that manual enrollment is disabled.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function print_entry(){&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: The other half to print_entry(), this checks the form data.&lt;br /&gt;
     * &lt;br /&gt;
     * This function checks that the user has completed the task on the enrollment&lt;br /&gt;
     * entry page and enrolls them.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $form&lt;br /&gt;
     * @param object $course &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function check_entry( $form, $course ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        // some role_assign();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Check if the given enrolment key matches a group enrolment key for&lt;br /&gt;
     * the given course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param int $courseid&lt;br /&gt;
     * @param string $enrolmentkey&lt;br /&gt;
     * @return mixed The group id of the group which the key matches, or false&lt;br /&gt;
     *       if it matches none&lt;br /&gt;
     */&lt;br /&gt;
    public function check_group_entry( $courseid, $password ){&lt;br /&gt;
        // some logic&lt;br /&gt;
        if ( $itlooksgood ){&lt;br /&gt;
            return $groupid;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: Return a string with icons that give enrolment information&lt;br /&gt;
     * for this course.&lt;br /&gt;
     * &lt;br /&gt;
     * @param object $course&lt;br /&gt;
     * @return string&lt;br /&gt;
     */&lt;br /&gt;
    public function get_access_icons( $course ){&lt;br /&gt;
        return &#039;put icons here&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cron ===&lt;br /&gt;
&lt;br /&gt;
Your plugin can perform cron tasks if it contains a &amp;lt;code&amp;gt;cron()&amp;lt;/code&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    /**&lt;br /&gt;
     * OPTIONAL: If this function exists, it will be called each time&lt;br /&gt;
     * admin/cron.php runs. It takes no arguments and returns no values,&lt;br /&gt;
     * but anything written to $this-&amp;gt;log will be printed to the cron output.&lt;br /&gt;
     * &lt;br /&gt;
     * @return void&lt;br /&gt;
     */&lt;br /&gt;
    public function cron(){&lt;br /&gt;
        $this-&amp;gt;log = &#039;Print this in the cron log!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Database ===&lt;br /&gt;
&lt;br /&gt;
If your plugin needs to have its own Moodle database tables, create a file called &amp;lt;code&amp;gt;enrol/PLUGINNAME/version.php&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&amp;lt;?php&lt;br /&gt;
$plugin-&amp;gt;version = 2010091600; // Version of your plugin&lt;br /&gt;
$plugin-&amp;gt;requires = 2007101000; // Required Moodle version (from /version.php)&lt;br /&gt;
?&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then create a directory &amp;lt;code&amp;gt;enrol/PLUGINNAME/db&amp;lt;/code&amp;gt;, and use the [[XMLDB Editor]] to set up your tables.&lt;br /&gt;
&lt;br /&gt;
==Testing paypal using the paypal developer sandbox==&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.4 - 1.8 ===&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* change the address being used to send data to paypal to use the sandbox. in enrol/paypal/enrol.html:&lt;br /&gt;
** change the form post action to be https://www.sandbox.paypal.com/cgi-bin/webscr instead of https://www.paypal.com/cgi-bin/webscr&lt;br /&gt;
* change the address that is used to check the acknowledgment from paypal.  in enrol/paypal/ipn.php, change:&lt;br /&gt;
** $fp = fsockopen (&#039;www.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* to&lt;br /&gt;
** $fp = fsockopen (&#039;www.sandbox.paypal.com&#039;, 80, $errno, $errstr, 30);&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
&lt;br /&gt;
=== For moodle 1.8.2 - 1.9.x ===&lt;br /&gt;
&lt;br /&gt;
The above changes to the code were made part of the release starting with version 1.8.2&lt;br /&gt;
&lt;br /&gt;
If you follow these steps, you can test paypal payments using the paypal developer sandbox instead of the real paypal site.  No money is actually charged to any account.  All other actions are the same as if you were using the real paypal site.&lt;br /&gt;
* create a paypal developer account at https://developer.paypal.com/cgi-bin/devscr?cmd=_home&lt;br /&gt;
* create a couple of user accounts in the paypal sandbox and test enrolling in a course that requires payment (note that you need to be logged into the paypal sandbox while doing this testing)&lt;br /&gt;
* under the &amp;quot;business&amp;quot; sandbox account you created, log in, go to &amp;quot;Profile&amp;quot;, &amp;quot;Instant Payment Notification&amp;quot;, and enter the full web-visible HTTPS URL to ../enrol/paypal/ipn.php. Failing to do this is a common mistake, and the cause of most headaches with this plugin.&lt;br /&gt;
* add the following line to config.php located in the root directory of your installation (where you installed Moodle). &lt;br /&gt;
** $CFG-&amp;gt;usepaypalsandbox = &#039;TRUE&#039;;&lt;br /&gt;
* Add the business account email address from &amp;quot;business&amp;quot; account you created to the paypal module settings (in 1.9.x - Site Administration&amp;gt;Courses&amp;gt;Enrolments&amp;gt;Paypal&amp;gt;Edit)&lt;br /&gt;
* You should be ready to test&lt;br /&gt;
* When finished be sure to remove the line that was added to config.php in your root directory.&lt;br /&gt;
&lt;br /&gt;
=== Note about Paypal Sandbox ===&lt;br /&gt;
&lt;br /&gt;
Only implement these changes if the above is not working and Moodle is returning the &amp;quot;...Unfortunately your payment...&amp;quot; message after returning from Paypal Sandbox.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing (14 June 2008) some changes are necessary to test successfully in the Paypal Sandbox. Details can be found [http://www.pdncommunity.com/pdn/board/message?board.id=sandbox&amp;amp;view=by_date_ascending&amp;amp;message.id=9802#M9802 here]. This could change at any time at which point this may become unnecessary. For now do the following (only tested in 1.9.x so far):&lt;br /&gt;
&lt;br /&gt;
* Locate ipn.php in your installation on your server by migrating to to ../enrol/paypal/ipn.php&lt;br /&gt;
* Create a backup copy of ipn.php and give it a name something like ipn.php-backup &lt;br /&gt;
* Return to ipn.php and locate the following code :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 80, $errno, $errstr, 30);&lt;br /&gt;
* Change that code to :&amp;lt;br /&amp;gt;&lt;br /&gt;
$header = &#039;&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;POST /cgi-bin/webscr HTTP/1.0\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Type: application/x-www-form-urlencoded\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$header .= &amp;quot;Content-Length: &amp;quot; . strlen($req) . &amp;quot;\r\n\r\n&amp;quot;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$paypaladdr = empty($CFG-&amp;gt;usepaypalsandbox) ? &#039;www.paypal.com&#039; : &#039;ssl://www.sandbox.paypal.com&#039;;&amp;lt;br /&amp;gt;&lt;br /&gt;
$fp = fsockopen ($paypaladdr, 443, $errno, $errstr, 30);&amp;lt;br /&amp;gt;&lt;br /&gt;
* Try the course purchase again. &lt;br /&gt;
&lt;br /&gt;
If the above still is not working you will need to seek help in the [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] Forum.&lt;br /&gt;
&lt;br /&gt;
==Enrolment plugins 1.7==&lt;br /&gt;
&lt;br /&gt;
===Background===&lt;br /&gt;
# isteacher(), isstudent(), isadmin(), iscoursecreator() are all deprecated and should be replaced with checks on has_capability instead.&lt;br /&gt;
# enrol_student() and add_teacher() are obsolete, so use the generic role_assign() or the convenience function for &amp;quot;students&amp;quot; called enrol_into_course() instead.&lt;br /&gt;
# unenrol_student() and remove_teacher() are obsolete, so use the generic role_unassign() instead.&lt;br /&gt;
# All the roles have a &amp;quot;shortname&amp;quot;, so by default we can refer to them using &amp;quot;teacher&amp;quot;, &amp;quot;student&amp;quot;, &amp;quot;editingteacher&amp;quot; etc.  Note that new roles may have different names.  Enrolment plugins can use these to map outside data onto the inside roles without needing to know internal role id numbers.   For example, it would be really handy for the flatfile plugin to refer to roles directly like this.&lt;br /&gt;
# Each plugin used to implement get_student_courses() and get_teacher_courses() for use at login time.  These now need to be converted to one new function called $enrol-&amp;gt;setup_enrolments($user) which looks up sets all enrolments for a given user (using role_assign and role_unassign).&lt;br /&gt;
# All the session arrays like $USER-&amp;gt;student[] and $USER-&amp;gt;teacher[] are gone and should not be relied on.  You can check what roles a user already has by calling get_user_roles() on that course context.&lt;br /&gt;
&lt;br /&gt;
===What&#039;s been done===&lt;br /&gt;
&lt;br /&gt;
#enrol/manual has been converted to use the new functions&lt;br /&gt;
#enrol/imsenterprise has been converted - testing needed though&lt;br /&gt;
&lt;br /&gt;
===What still needs to be done===&lt;br /&gt;
&lt;br /&gt;
#enrol/authorize&lt;br /&gt;
#enrol/database&lt;br /&gt;
#enrol/flatfile&lt;br /&gt;
#enrol/ldap&lt;br /&gt;
#enrol/paypal&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Enrolment plugins|Enrolment plugins (administrator)]]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/view.php?id=2981 Enrolment Plugins] forum&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Enrolment plugins]]&lt;br /&gt;
[[Category:Enrolment]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Installing_and_upgrading_plugin_database_tables&amp;diff=75841</id>
		<title>Installing and upgrading plugin database tables</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Installing_and_upgrading_plugin_database_tables&amp;diff=75841"/>
		<updated>2010-09-15T06:22:12Z</updated>

		<summary type="html">&lt;p&gt;Agwells: Explaining the version number format&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
If you have done the right thing, Moodle will automatically create the database tables for your plugin when you visit the Admin notifications page (.../admin/index.php). This process is controlled by three files within your plugin:&lt;br /&gt;
;version.php : This records the version of the plugin code (&#039;&#039;&#039;Please note:&#039;&#039;&#039; In Moodle installations below v2.0, this is not required for blocks, which use the version number returned by the block&#039;s init() method - see [[Development:Blocks#Ready.2C_Set.2C_Go.21|the blocks development page]])&lt;br /&gt;
;db/install.xml : This is used when someone installs your plugin for the first time.&lt;br /&gt;
;db/upgrade.php : This is used when someone who had an older version of your plugin installed upgrades to the latest version.&lt;br /&gt;
&lt;br /&gt;
In addition, Moodle also stores in the database the currently installed version of each plugin.&lt;br /&gt;
&lt;br /&gt;
In Moodle 1.9 and before, this is stored in the mdl_config table, in a row with the name &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version. For example qtype_myqtype_version. The exception to this rule are modules and blocks. Installed module version numbers are stored in the mdl_modules table. Block version numbers are in mdl_block.&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 an beyond, plugin version numbers are stored in the mdl_config_plugins table, with &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039; in the plugin column, and &#039;version&#039; in the name column - with the same exception for modules and blocks.&lt;br /&gt;
&lt;br /&gt;
==A specific example==&lt;br /&gt;
&lt;br /&gt;
For the rest of this document, I will use a particular example, because it should make the explanation easier. You should be able to see how to generalise it.&lt;br /&gt;
&lt;br /&gt;
We will suppose you that you are making a new question type myqtype. This is plugin type qtype, and the code will be in the question/type/myqtype folder. The currently installed version number will be stored in the qtype_myqtype_version row of the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
In addition, we will just consider the first two releases of the plugin. The first release will have version number 2008080100, and will just use one database table mdl_question_myqtype, with two columns col1 and col2. Then we will suppose that the second release is 2008080200, and that requires an extra column, newcol, to be added to the mdl_question_myqtype table.&lt;br /&gt;
&lt;br /&gt;
==The files you need for the first release==&lt;br /&gt;
&lt;br /&gt;
In what follows, the bits of code you need to replace are &#039;&#039;&#039;in bold&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
;version.php&lt;br /&gt;
 $plugin-&amp;gt;version  = 2008080100; // Today&#039;s date in YYYYMMDD format, plus two additional digits&lt;br /&gt;
 $plugin-&amp;gt;requires = &#039;&#039;&#039;XXXXXXXXXX; // Moodle version - copy from $version in the top-level version.php file.&#039;&#039;&#039;&lt;br /&gt;
:Version numbers normally consist of the day&#039;s date in YYYYMMDD format, followed by two additional digits to allow for multiple versions in the same day, e.g. Sept 15, 2010 could have versions 2010091500, 2010091501, 2010091502, etc.&lt;br /&gt;
&lt;br /&gt;
;db/install.xml&lt;br /&gt;
:This file, which you should [[XMLDB editor|create with the XMLDB editor]], should contain the definition for your mdl_question_myqtype table, with the two columns col1 and col2.&lt;br /&gt;
&lt;br /&gt;
At this stage, you do not need a db/upgrade.php file.&lt;br /&gt;
&lt;br /&gt;
==The files you need for the second release==&lt;br /&gt;
&lt;br /&gt;
;version.php&lt;br /&gt;
 $plugin-&amp;gt;version  = 2008080200;&lt;br /&gt;
 $plugin-&amp;gt;requires = &#039;&#039;&#039;XXXXXXXXXX; // Copy the current value from the top-level version.php file.&#039;&#039;&#039;&lt;br /&gt;
;db/install.xml : This file should now contain the updated definition for your mdl_question_myqtype table, with three columns col1, col2 and newcol. You modify this file using the XMLDB editor.&lt;br /&gt;
;db/upgrade.php&lt;br /&gt;
:This file should contain the code that people need to run to upgrade from version 2008080100 of your plugin. That is, the code to add a column newcol to the mdl_question_myqtype table. You don&#039;t have to write this code yourself as the XMLDB editor will generate it for you. The upgrade.php file should contain a single function xmldb_qtype_myqtype_upgrade that looks a bit like:&lt;br /&gt;
 function xmldb_qtype_myqtype_upgrade($oldversion = 0) {&lt;br /&gt;
     $result = true;&lt;br /&gt;
 &lt;br /&gt;
     /// Add a new column newcol to the mdl_question_myqtype&lt;br /&gt;
     if ($result &amp;amp;&amp;amp; $oldversion &amp;lt; 2008080200) {&lt;br /&gt;
         &#039;&#039;&#039;// Code to add the column, generated by the &#039;View PHP Code&#039; option of the XMLDB editor.&#039;&#039;&#039;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     return $result;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Hint: If you are modifying or adding a field/table, get the XMLDB editor to generate the PHP update code for you &#039;&#039;&#039;after&#039;&#039;&#039; making the changes in the editor. If you are deleting one, you need to generate the PHP code &#039;&#039;&#039;before&#039;&#039;&#039; making the change - or you won&#039;t be able to select the field/table to write the code for, because it no longer exists.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==What happens when a user installs or upgrades your plugin==&lt;br /&gt;
&lt;br /&gt;
The process is triggered when an administrator goes to the Admin notifications page (.../admin/index.php). The code that does the work is the upgrade_plugins function in lib/adminlib.php and reading this code is the best way to find out exactly what happens. In pseudo-code, what it does is:&lt;br /&gt;
&lt;br /&gt;
 For each plugin of this type (e.g. all qtype plugins) {&lt;br /&gt;
     // For the body of this loop, suppose the current plugin being processed is myqtype.&lt;br /&gt;
 &lt;br /&gt;
     Check that question/type/myqtype/version.php, .../db/upgrade.php and .../db/install.xml exist.&lt;br /&gt;
 &lt;br /&gt;
     if ($CFG-&amp;gt;qtype_myqtype_version exists, and is less than the number in version.php) {&lt;br /&gt;
         Call the upgrade function xmldb_qtype_myqtype_upgrade from&lt;br /&gt;
                 upgrade.php, passing the old version number ($CFG-&amp;gt;qtype_myqtype_version)&lt;br /&gt;
                 which says what is currently installed&lt;br /&gt;
         Update $CFG-&amp;gt;qtype_myqtype_version to the latest number from version.php&lt;br /&gt;
                 to record what is currently installed&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     else if ($CFG-&amp;gt;qtype_myqtype_version does not exist) {&lt;br /&gt;
         Create the tables from the definitions in install.xml&lt;br /&gt;
         Update $CFG-&amp;gt;qtype_myqtype_version to the latest number from version.php&lt;br /&gt;
                 to record what is currently installed&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Of course, it is a bit more complex that than. However, the code in the upgrade_plugins is quite clear, and I encourage you to go and have a look at it so you can see all the details of how it works.&lt;br /&gt;
&lt;br /&gt;
Let us now look at some worked examples:&lt;br /&gt;
&lt;br /&gt;
===User installs version 2008080100 of myqtype===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype does not exist, and there will not be a qtype_myqtype_version row in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
# The user will unzip myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for the qtype_myqtype plugin is now present, but there is no trace of it in the database, so it will install the plugin from the install.xml file.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will exist with the two columns col1 and col2; and the qtype_myqtype_version row in the mdl_config table will contain 2008080100.&lt;br /&gt;
&lt;br /&gt;
===User upgrades from version 2008080100 to version 2008080200 of myqtype===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype table will exist with the two columns col1 and col2; and the qtype_myqtype_version row in the mdl_config table will contain 2008080100.&lt;br /&gt;
&lt;br /&gt;
# The user will delete the old question/type/myqtype folder.&lt;br /&gt;
# The user will unzip the new myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for version 2008080200 the qtype_myqtype plugin is now present, but the installed version of the the database tables (qtype_myqtype_version) is 2008080100. Therefore, it will call xmldb_qtype_myqtype_upgrade from upgrade.php, passing 2008080100 as the $oldversion argument.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will now have three columns col1, col2 and newcol; and the qtype_myqtype_version row in the mdl_config table will contain 2008080200.&lt;br /&gt;
&lt;br /&gt;
===User installs version 2008080200 of myqtype into a clean Moodle install===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype does not exist, and there will not be a qtype_myqtype_version row in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
# The user will unzip the 2008080200 version of myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for the qtype_myqtype plugin is now present, but there is no trace of it in the database, so it will install the plugin from the install.xml file.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will exist with three columns col1, col2 and newcol; and the qtype_myqtype_version row in the mdl_config table will contain 2008080200.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
The first time a user installs any version of your plugin, the install.xml file will be used to create all the required database tables. Therefore install.xml should always contain the definition of the up-to-date database structure. Moodle recognises this situation because there is a version.php file on disc, but there is no &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version value in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
If the user already had a version of your plugin installed, and then upgrades to a newer version, Moodle will detect this because the version.php file will contain a newer version number than the &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version value in the mdl_config table. In this case, Moodle will run the code in the upgrade.php file, passing in the old version number, so that the correct bits of upgrade can be run, as controlled by the if ($oldversion &amp;lt; XXXXXXXXXX) blocks of code.&lt;br /&gt;
&lt;br /&gt;
The contents of the install.xml and upgrade.php files should be generated using the XMLDB editor.&lt;br /&gt;
&lt;br /&gt;
==Database upgrades and stable branches==&lt;br /&gt;
&lt;br /&gt;
The simple rule is, never make any database changes on a stable branch. You only need to read this section in the rare situations where a database change on the stable branch is unavoidable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Warning!!! advanced material follows.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Suppose, in order to fix a bug, you need to make a database change in Moodle 1.9.3+ (which must also be merged into HEAD). The root of the problem is that people may upgrade their Moodle in three different ways, which &lt;br /&gt;
&lt;br /&gt;
* Upgrade from &amp;lt;=1.9.3 to 1.9.4 - this executes the ugprade script on the 1.9 branch.&lt;br /&gt;
* Upgrade from &amp;lt;=1.9.3 directly to &amp;gt;=2.0 - this executes the upgrade script on the HEAD branch.&lt;br /&gt;
* Upgrade from 1.9.4 to &amp;gt;=2.0 - in this case, you must ensure that the upgrade on HEAD is not executed.&lt;br /&gt;
&lt;br /&gt;
The normal way to do this is ensure that your database upgrade is idempotent. That is, it does not matter if you do it twice. So for example, instead of doing&lt;br /&gt;
&lt;br /&gt;
        $dbman-&amp;gt;create_table($table);&lt;br /&gt;
&lt;br /&gt;
you should do&lt;br /&gt;
&lt;br /&gt;
        if (!$dbman-&amp;gt;table_exists($table)) {&lt;br /&gt;
            $dbman-&amp;gt;create_table($table);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
You should also think about what version numbers to put in your version.php file on each branch. Above all, test carefully.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB_Documentation|XMLDB_Documentation]]&lt;br /&gt;
* [[Development:Coding|Coding guidelines]]&lt;br /&gt;
* [[Development:DDL functions|DDL functions]]&lt;br /&gt;
* [[Development:XMLDB defining an XML structure|install.xml file documentation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:Installation]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Installing_and_upgrading_plugin_database_tables&amp;diff=75840</id>
		<title>Installing and upgrading plugin database tables</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Installing_and_upgrading_plugin_database_tables&amp;diff=75840"/>
		<updated>2010-09-15T06:17:40Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* The files you need for the first release */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
If you have done the right thing, Moodle will automatically create the database tables for your plugin when you visit the Admin notifications page (.../admin/index.php). This process is controlled by three files within your plugin:&lt;br /&gt;
;version.php : This records the version of the plugin code (&#039;&#039;&#039;Please note:&#039;&#039;&#039; In Moodle installations below v2.0, this is not required for blocks, which use the version number returned by the block&#039;s init() method - see [[Development:Blocks#Ready.2C_Set.2C_Go.21|the blocks development page]])&lt;br /&gt;
;db/install.xml : This is used when someone installs your plugin for the first time.&lt;br /&gt;
;db/upgrade.php : This is used when someone who had an older version of your plugin installed upgrades to the latest version.&lt;br /&gt;
&lt;br /&gt;
In addition, Moodle also stores in the database the currently installed version of each plugin.&lt;br /&gt;
&lt;br /&gt;
In Moodle 1.9 and before, this is stored in the mdl_config table, in a row with the name &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version. For example qtype_myqtype_version. The exception to this rule are modules and blocks. Installed module version numbers are stored in the mdl_modules table. Block version numbers are in mdl_block.&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 an beyond, plugin version numbers are stored in the mdl_config_plugins table, with &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039; in the plugin column, and &#039;version&#039; in the name column - with the same exception for modules and blocks.&lt;br /&gt;
&lt;br /&gt;
==A specific example==&lt;br /&gt;
&lt;br /&gt;
For the rest of this document, I will use a particular example, because it should make the explanation easier. You should be able to see how to generalise it.&lt;br /&gt;
&lt;br /&gt;
We will suppose you that you are making a new question type myqtype. This is plugin type qtype, and the code will be in the question/type/myqtype folder. The currently installed version number will be stored in the qtype_myqtype_version row of the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
In addition, we will just consider the first two releases of the plugin. The first release will have version number 2008080100, and will just use one database table mdl_question_myqtype, with two columns col1 and col2. Then we will suppose that the second release is 2008080200, and that requires an extra column, newcol, to be added to the mdl_question_myqtype table.&lt;br /&gt;
&lt;br /&gt;
==The files you need for the first release==&lt;br /&gt;
&lt;br /&gt;
In what follows, the bits of code you need to replace are &#039;&#039;&#039;in bold&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
;version.php&lt;br /&gt;
 $plugin-&amp;gt;version  = 2008080100;&lt;br /&gt;
 $plugin-&amp;gt;requires = &#039;&#039;&#039;XXXXXXXXXX; // Copy the current value from the top-level version.php file.&#039;&#039;&#039;&lt;br /&gt;
:Version numbers normally consist of the day&#039;s date in YYYYMMDD format, followed by two additional numbers to allow for multiple versions in the same day, e.g. Sept 15, 2010 could have versions 2010091500, 2010091501, 2010091502, etc.&lt;br /&gt;
&lt;br /&gt;
;db/install.xml&lt;br /&gt;
:This file, which you should [[XMLDB editor|create with the XMLDB editor]], should contain the definition for your mdl_question_myqtype table, with the two columns col1 and col2.&lt;br /&gt;
&lt;br /&gt;
At this stage, you do not need a db/upgrade.php file.&lt;br /&gt;
&lt;br /&gt;
==The files you need for the second release==&lt;br /&gt;
&lt;br /&gt;
;version.php&lt;br /&gt;
 $plugin-&amp;gt;version  = 2008080200;&lt;br /&gt;
 $plugin-&amp;gt;requires = &#039;&#039;&#039;XXXXXXXXXX; // Copy the current value from the top-level version.php file.&#039;&#039;&#039;&lt;br /&gt;
;db/install.xml : This file should now contain the updated definition for your mdl_question_myqtype table, with three columns col1, col2 and newcol. You modify this file using the XMLDB editor.&lt;br /&gt;
;db/upgrade.php&lt;br /&gt;
:This file should contain the code that people need to run to upgrade from version 2008080100 of your plugin. That is, the code to add a column newcol to the mdl_question_myqtype table. You don&#039;t have to write this code yourself as the XMLDB editor will generate it for you. The upgrade.php file should contain a single function xmldb_qtype_myqtype_upgrade that looks a bit like:&lt;br /&gt;
 function xmldb_qtype_myqtype_upgrade($oldversion = 0) {&lt;br /&gt;
     $result = true;&lt;br /&gt;
 &lt;br /&gt;
     /// Add a new column newcol to the mdl_question_myqtype&lt;br /&gt;
     if ($result &amp;amp;&amp;amp; $oldversion &amp;lt; 2008080200) {&lt;br /&gt;
         &#039;&#039;&#039;// Code to add the column, generated by the &#039;View PHP Code&#039; option of the XMLDB editor.&#039;&#039;&#039;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     return $result;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Hint: If you are modifying or adding a field/table, get the XMLDB editor to generate the PHP update code for you &#039;&#039;&#039;after&#039;&#039;&#039; making the changes in the editor. If you are deleting one, you need to generate the PHP code &#039;&#039;&#039;before&#039;&#039;&#039; making the change - or you won&#039;t be able to select the field/table to write the code for, because it no longer exists.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==What happens when a user installs or upgrades your plugin==&lt;br /&gt;
&lt;br /&gt;
The process is triggered when an administrator goes to the Admin notifications page (.../admin/index.php). The code that does the work is the upgrade_plugins function in lib/adminlib.php and reading this code is the best way to find out exactly what happens. In pseudo-code, what it does is:&lt;br /&gt;
&lt;br /&gt;
 For each plugin of this type (e.g. all qtype plugins) {&lt;br /&gt;
     // For the body of this loop, suppose the current plugin being processed is myqtype.&lt;br /&gt;
 &lt;br /&gt;
     Check that question/type/myqtype/version.php, .../db/upgrade.php and .../db/install.xml exist.&lt;br /&gt;
 &lt;br /&gt;
     if ($CFG-&amp;gt;qtype_myqtype_version exists, and is less than the number in version.php) {&lt;br /&gt;
         Call the upgrade function xmldb_qtype_myqtype_upgrade from&lt;br /&gt;
                 upgrade.php, passing the old version number ($CFG-&amp;gt;qtype_myqtype_version)&lt;br /&gt;
                 which says what is currently installed&lt;br /&gt;
         Update $CFG-&amp;gt;qtype_myqtype_version to the latest number from version.php&lt;br /&gt;
                 to record what is currently installed&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     else if ($CFG-&amp;gt;qtype_myqtype_version does not exist) {&lt;br /&gt;
         Create the tables from the definitions in install.xml&lt;br /&gt;
         Update $CFG-&amp;gt;qtype_myqtype_version to the latest number from version.php&lt;br /&gt;
                 to record what is currently installed&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Of course, it is a bit more complex that than. However, the code in the upgrade_plugins is quite clear, and I encourage you to go and have a look at it so you can see all the details of how it works.&lt;br /&gt;
&lt;br /&gt;
Let us now look at some worked examples:&lt;br /&gt;
&lt;br /&gt;
===User installs version 2008080100 of myqtype===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype does not exist, and there will not be a qtype_myqtype_version row in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
# The user will unzip myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for the qtype_myqtype plugin is now present, but there is no trace of it in the database, so it will install the plugin from the install.xml file.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will exist with the two columns col1 and col2; and the qtype_myqtype_version row in the mdl_config table will contain 2008080100.&lt;br /&gt;
&lt;br /&gt;
===User upgrades from version 2008080100 to version 2008080200 of myqtype===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype table will exist with the two columns col1 and col2; and the qtype_myqtype_version row in the mdl_config table will contain 2008080100.&lt;br /&gt;
&lt;br /&gt;
# The user will delete the old question/type/myqtype folder.&lt;br /&gt;
# The user will unzip the new myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for version 2008080200 the qtype_myqtype plugin is now present, but the installed version of the the database tables (qtype_myqtype_version) is 2008080100. Therefore, it will call xmldb_qtype_myqtype_upgrade from upgrade.php, passing 2008080100 as the $oldversion argument.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will now have three columns col1, col2 and newcol; and the qtype_myqtype_version row in the mdl_config table will contain 2008080200.&lt;br /&gt;
&lt;br /&gt;
===User installs version 2008080200 of myqtype into a clean Moodle install===&lt;br /&gt;
&lt;br /&gt;
To start with, the mdl_question_myqtype does not exist, and there will not be a qtype_myqtype_version row in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
# The user will unzip the 2008080200 version of myqtype.zip into the question/type folder.&lt;br /&gt;
# The user will visit the Admin notifications page.&lt;br /&gt;
# This will trigger Moodle to search for plugings to upgrade. It will find that the code for the qtype_myqtype plugin is now present, but there is no trace of it in the database, so it will install the plugin from the install.xml file.&lt;br /&gt;
&lt;br /&gt;
At the end of this process, the mdl_question_myqtype table will exist with three columns col1, col2 and newcol; and the qtype_myqtype_version row in the mdl_config table will contain 2008080200.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
The first time a user installs any version of your plugin, the install.xml file will be used to create all the required database tables. Therefore install.xml should always contain the definition of the up-to-date database structure. Moodle recognises this situation because there is a version.php file on disc, but there is no &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version value in the mdl_config table.&lt;br /&gt;
&lt;br /&gt;
If the user already had a version of your plugin installed, and then upgrades to a newer version, Moodle will detect this because the version.php file will contain a newer version number than the &#039;&#039;plugintype&#039;&#039;_&#039;&#039;pluginname&#039;&#039;_version value in the mdl_config table. In this case, Moodle will run the code in the upgrade.php file, passing in the old version number, so that the correct bits of upgrade can be run, as controlled by the if ($oldversion &amp;lt; XXXXXXXXXX) blocks of code.&lt;br /&gt;
&lt;br /&gt;
The contents of the install.xml and upgrade.php files should be generated using the XMLDB editor.&lt;br /&gt;
&lt;br /&gt;
==Database upgrades and stable branches==&lt;br /&gt;
&lt;br /&gt;
The simple rule is, never make any database changes on a stable branch. You only need to read this section in the rare situations where a database change on the stable branch is unavoidable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Warning!!! advanced material follows.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Suppose, in order to fix a bug, you need to make a database change in Moodle 1.9.3+ (which must also be merged into HEAD). The root of the problem is that people may upgrade their Moodle in three different ways, which &lt;br /&gt;
&lt;br /&gt;
* Upgrade from &amp;lt;=1.9.3 to 1.9.4 - this executes the ugprade script on the 1.9 branch.&lt;br /&gt;
* Upgrade from &amp;lt;=1.9.3 directly to &amp;gt;=2.0 - this executes the upgrade script on the HEAD branch.&lt;br /&gt;
* Upgrade from 1.9.4 to &amp;gt;=2.0 - in this case, you must ensure that the upgrade on HEAD is not executed.&lt;br /&gt;
&lt;br /&gt;
The normal way to do this is ensure that your database upgrade is idempotent. That is, it does not matter if you do it twice. So for example, instead of doing&lt;br /&gt;
&lt;br /&gt;
        $dbman-&amp;gt;create_table($table);&lt;br /&gt;
&lt;br /&gt;
you should do&lt;br /&gt;
&lt;br /&gt;
        if (!$dbman-&amp;gt;table_exists($table)) {&lt;br /&gt;
            $dbman-&amp;gt;create_table($table);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
You should also think about what version numbers to put in your version.php file on each branch. Above all, test carefully.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Development:XMLDB_Documentation|XMLDB_Documentation]]&lt;br /&gt;
* [[Development:Coding|Coding guidelines]]&lt;br /&gt;
* [[Development:DDL functions|DDL functions]]&lt;br /&gt;
* [[Development:XMLDB defining an XML structure|install.xml file documentation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:Installation]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Events_API&amp;diff=74952</id>
		<title>Events API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Events_API&amp;diff=74952"/>
		<updated>2010-08-19T22:15:50Z</updated>

		<summary type="html">&lt;p&gt;Agwells: /* Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Events API is a core system in Moodle to allow communication between modules.  &lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;event&#039;&#039;&#039; is when something &amp;quot;interesting&amp;quot; happens in Moodle that is worth alerting the system about.&lt;br /&gt;
&lt;br /&gt;
Any Moodle modules can &#039;&#039;&#039;trigger&#039;&#039;&#039; new events (with attached data), and other modules can elect to &#039;&#039;&#039;handle&#039;&#039;&#039; those events with custom functions that operate on the given data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at an example of how events are used to implement Messaging in Moodle 2.0.  In the messaging system, textual messages are generated for users by different modules, and the user can decide how certain types of messages are displayed.&lt;br /&gt;
&lt;br /&gt;
===Triggering an event===&lt;br /&gt;
&lt;br /&gt;
When a messaging event occurs, the module should trigger a &amp;quot;message_send&amp;quot; event.   In this example let&#039;s pretend someone just posted to a forum.&lt;br /&gt;
&lt;br /&gt;
The forum module needs to create an object with the data that this event needs.  This may vary completely for different types of events, it&#039;s just a data object.&lt;br /&gt;
&lt;br /&gt;
 $eventdata = new object();&lt;br /&gt;
 $eventdata-&amp;gt;component         = &#039;mod/forum&#039;;    // path in Moodle&lt;br /&gt;
 $eventdata-&amp;gt;name              = &#039;posts&#039;;        // type of message from that module (as module defines it)&lt;br /&gt;
 $eventdata-&amp;gt;userfrom          = $userfrom;      // user object&lt;br /&gt;
 $eventdata-&amp;gt;userto            = $userto;        // user object&lt;br /&gt;
 $eventdata-&amp;gt;subject           = &amp;quot;Hi there&amp;quot;;     // short one-line subject&lt;br /&gt;
 $eventdata-&amp;gt;fullmessage       = &amp;quot;Here is the full message&amp;quot;;      // raw text&lt;br /&gt;
 $eventdata-&amp;gt;fullmessageformat = FORMAT_PLAIN;   // text format&lt;br /&gt;
 $eventdata-&amp;gt;fullmessagehtml   = &amp;quot;Here is the &amp;amp;lt;b&amp;gt;full&amp;amp;lt;/b&amp;gt; message&amp;quot;;    // html rendered version   (optional)&lt;br /&gt;
 $eventdata-&amp;gt;smallmessage      = &amp;quot;Here is the truncated message&amp;quot;;      // useful for plugins like sms or twitter  (optional)&lt;br /&gt;
&lt;br /&gt;
Then we post the object as an event and forget about it:&lt;br /&gt;
&lt;br /&gt;
 events_trigger(&#039;message_send&#039;, $eventdata);&lt;br /&gt;
&lt;br /&gt;
===Handling an event===&lt;br /&gt;
&lt;br /&gt;
Modules or core code can define an events.php in the db directory which defines events they want to be notified about, and describes which of their functions or class methods should be notified.  For this case, there is this definition of a handler in lib/db/events.php&lt;br /&gt;
&lt;br /&gt;
 $handlers = array (&lt;br /&gt;
     &#039;message_send&#039; =&amp;gt; array (&lt;br /&gt;
          &#039;handlerfile&#039;      =&amp;gt; &#039;/lib/messagelib.php&#039;,&lt;br /&gt;
          &#039;handlerfunction&#039;  =&amp;gt; &#039;message_send_handler&#039;,&lt;br /&gt;
          &#039;schedule&#039;         =&amp;gt; &#039;instant&#039;&lt;br /&gt;
      )&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
These events.php files are parsed during install / upgrade and stored in a simple database table.&lt;br /&gt;
&lt;br /&gt;
Now, when a &#039;&#039;&#039;message_send&#039;&#039;&#039; event happens, all the registered handlers functions for that event will be called something like this (but with more error handling):&lt;br /&gt;
&lt;br /&gt;
          include_once($CFG-&amp;gt;dirroot.$handlers[&#039;message_send&#039;][&#039;handlerfile&#039;]);&lt;br /&gt;
          call_user_func($handlers[&#039;message_send&#039;][&#039;handlerfunction&#039;], $eventdata);&lt;br /&gt;
&lt;br /&gt;
Any code can hook into any events this way.&lt;br /&gt;
&lt;br /&gt;
The handler function accepts one parameter (the event data object) and should return a boolean.  Returning false indicates that there was an error and the event will be left in the event queue.&lt;br /&gt;
&lt;br /&gt;
    function message_send_handler($eventdata) {&lt;br /&gt;
        // handle event &lt;br /&gt;
        // ...&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Database structure==&lt;br /&gt;
&lt;br /&gt;
There are 3 core tables for events. Note that if a handler is queued, and yet to be processed or processing failed, then all subsequent calls on that handler must be queued.&lt;br /&gt;
&lt;br /&gt;
===events_handlers===&lt;br /&gt;
&lt;br /&gt;
This table is for storing which components requests what type of event, and the location of the responsible handler functions.&lt;br /&gt;
&lt;br /&gt;
These entries are created by parsing events.php files in all the modules, and can be rebuilt any time (during an upgrade, say).&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Field&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Type&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Info&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|id&lt;br /&gt;
|int(10)&lt;br /&gt;
|auto increment identifier&lt;br /&gt;
|-&lt;br /&gt;
|eventname&lt;br /&gt;
|varchar(255)&lt;br /&gt;
|name of the event, e.g. &#039;message_send&#039;&lt;br /&gt;
|-&lt;br /&gt;
|handlermodule&lt;br /&gt;
|varchar(255)&lt;br /&gt;
|e.g. moodle, mod/forum, block/rss_client&lt;br /&gt;
|-&lt;br /&gt;
|handlerfile&lt;br /&gt;
|varchar(255)&lt;br /&gt;
|path to the file of the function, eg /lib/messagelib.php&lt;br /&gt;
|-&lt;br /&gt;
|handlerfunction&lt;br /&gt;
|text&lt;br /&gt;
|serialized string or array describing function, suitable to be passed to &#039;&#039;&#039;call_user_func()&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|schedule 	&lt;br /&gt;
|varchar(255) 	&lt;br /&gt;
|&#039;cron&#039; or &#039;instant&#039;.&lt;br /&gt;
|-&lt;br /&gt;
|status&lt;br /&gt;
|int(10)&lt;br /&gt;
|number of failed attempts to process this handler&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===events_queue===&lt;br /&gt;
&lt;br /&gt;
This table is for storing queued events. It stores only one copy of the eventdata here, and entries from this table are being references by the events_queue_handlers table.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Field&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Type&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Info&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|id&lt;br /&gt;
|int(10)&lt;br /&gt;
|auto increment identifier&lt;br /&gt;
|-&lt;br /&gt;
|eventdata 	&lt;br /&gt;
|longtext 	&lt;br /&gt;
|serialized version of the data object passed to the event handler.&lt;br /&gt;
|-&lt;br /&gt;
|stackdump&lt;br /&gt;
|text&lt;br /&gt;
|serialized debug_backtrace showing where the event was fired from&lt;br /&gt;
|-&lt;br /&gt;
|userid&lt;br /&gt;
|int(10)&lt;br /&gt;
|$USER-&amp;gt;id when the event was fired&lt;br /&gt;
|-&lt;br /&gt;
|timecreated&lt;br /&gt;
|int(10) 	&lt;br /&gt;
|time stamp of the first time this was added&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===events_queue_handlers===&lt;br /&gt;
&lt;br /&gt;
This is the list of queued handlers for processing. The event object is retrieved from the events_queue table. When no further reference is made to the events_queue table, the corresponding entry in the events_queue table should be deleted. Entry should get deleted (?) after a successful event processing by the specified handler.  The status field keeps track of failures, after it gets to a certain number (eg 10?) it should trigger an &amp;quot;event failed&amp;quot; event (that could result in admin being emailed etc, or perhaps even the originating module taking care of it or rolling something back etc).&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Field&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Type&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Info&#039;&#039;&#039;&lt;br /&gt;
|- &lt;br /&gt;
|id&lt;br /&gt;
|int(10)&lt;br /&gt;
|auto increment identifier&lt;br /&gt;
|-&lt;br /&gt;
|queuedeventid&lt;br /&gt;
|int(10)&lt;br /&gt;
|foreign key id corresponding to the id of the event_queues table&lt;br /&gt;
|-&lt;br /&gt;
|handlerid&lt;br /&gt;
|int(10)&lt;br /&gt;
|foreign key id corresponding to the id of the event_handlers table&lt;br /&gt;
|-&lt;br /&gt;
|status&lt;br /&gt;
|int(10)&lt;br /&gt;
|number of failed attempts to process this handler&lt;br /&gt;
|-&lt;br /&gt;
|errormessage&lt;br /&gt;
|text&lt;br /&gt;
|if an error happened last time we tried to process this event, record it here.&lt;br /&gt;
|-&lt;br /&gt;
|timemodified&lt;br /&gt;
|int(10)&lt;br /&gt;
|time stamp of the last attempt to run this from the queue&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Standards for naming events==&lt;br /&gt;
&lt;br /&gt;
All event names should follow a consistent naming pattern, such as modulename_noun_verb&lt;br /&gt;
&lt;br /&gt;
If the event is being fired after the action has taken place (as in most cases) then use the past tense for the verb (created / deleted / updated / sent).&lt;br /&gt;
&lt;br /&gt;
If the event &#039;&#039;&#039;is&#039;&#039;&#039; the action, then use the present tense (create / delete / update / send).&lt;br /&gt;
&lt;br /&gt;
==Events which exist==&lt;br /&gt;
&lt;br /&gt;
When we add new events to core we should always add them here too.&lt;br /&gt;
&lt;br /&gt;
Under each event, list the data sent as part of the event.&lt;br /&gt;
&lt;br /&gt;
===Users===&lt;br /&gt;
* user_created&lt;br /&gt;
** full new record from &#039;user&#039; table&lt;br /&gt;
* user_deleted&lt;br /&gt;
** record from &#039;user&#039; table before marked as deleted&lt;br /&gt;
* user_updated&lt;br /&gt;
** full new record from &#039;user&#039; table&lt;br /&gt;
&lt;br /&gt;
===Roles===&lt;br /&gt;
* role_assigned (full new record from &#039;role_assignments&#039; table)&lt;br /&gt;
* role_unassigned (record from &#039;role_assignments&#039;, course context only)&lt;br /&gt;
&lt;br /&gt;
===Courses===&lt;br /&gt;
* course_created&lt;br /&gt;
** full course record&lt;br /&gt;
* course_updated&lt;br /&gt;
** full course record&lt;br /&gt;
* course_deleted&lt;br /&gt;
** full course record&lt;br /&gt;
* course_category_deleted&lt;br /&gt;
** full category record&lt;br /&gt;
&lt;br /&gt;
===Groups===&lt;br /&gt;
* groups_member_added&lt;br /&gt;
** groupid&lt;br /&gt;
** userid&lt;br /&gt;
* groups_member_removed&lt;br /&gt;
** groupid&lt;br /&gt;
** userid&lt;br /&gt;
* groups_group_created&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** description&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
** picture&lt;br /&gt;
* groups_group_updated&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** description&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
** picture&lt;br /&gt;
* groups_group_deleted&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** description&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
** picture&lt;br /&gt;
* groups_grouping_created&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
* groups_grouping_updated&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
* groups_grouping_deleted&lt;br /&gt;
** id&lt;br /&gt;
** courseid&lt;br /&gt;
** name&lt;br /&gt;
** timecreated&lt;br /&gt;
** timemodified&lt;br /&gt;
* groups_members_removed (user deleted from all groups in a course)&lt;br /&gt;
** courseid&lt;br /&gt;
** userid&lt;br /&gt;
* groups_groupings_groups_removed (remove all groups from all groupings in a course)&lt;br /&gt;
** courseid (as plain integer, not object)&lt;br /&gt;
* groups_groups_deleted (delete all groups in a course)&lt;br /&gt;
** courseid (as plain integer, not object)&lt;br /&gt;
* groups_groupings_deleted (delete all groupings in a course)&lt;br /&gt;
** courseid (as plain integer, not object)&lt;br /&gt;
&lt;br /&gt;
===Messaging===&lt;br /&gt;
* message_send&lt;br /&gt;
** component = &#039;mod/forum&#039;: path in Moodle&lt;br /&gt;
** name = &#039;posts&#039;: type of message from that module (as module defines it)&lt;br /&gt;
** userfrom = $userfrom: a user object to send from&lt;br /&gt;
** userto = $userto: a user object to send to&lt;br /&gt;
** subject = &#039;subject line&#039;: a short text line&lt;br /&gt;
** fullmessage = &#039;full plain text&#039;: raw text as entered by user&lt;br /&gt;
** fullmessageformat = FORMAT_PLAIN|FORMAT_HTML|FORMAT_MOODLE|FORMAT_MARKDOWN: the format of this text&lt;br /&gt;
** fullmessagehtml = &#039;long html text&#039;; html rendered version (optional)&lt;br /&gt;
** smallmessage = &#039;short text&#039;: useful for plugins like sms or twitter (optional)&lt;br /&gt;
&lt;br /&gt;
===Portfolio===&lt;br /&gt;
* portfolio_send&lt;br /&gt;
** id : recordid in portfolio_tempdata table, used for itemid in file storage&lt;br /&gt;
&lt;br /&gt;
==Events wishlist==&lt;br /&gt;
&lt;br /&gt;
List of events which it would be nice to have.  Please add to this list if what you want is not shown here.&lt;br /&gt;
&lt;br /&gt;
* mform_print_form -- this for all types of form e.g. admin settings, user profile, module updating, + some sort of standard way of discriminiating between them e.g. if ($form-&amp;gt;name == &#039;user_profile&#039;) {}. This would be better triggered at the end of the form generation process so that new bits can be inserted at any point, or existing bits could be removed.&lt;br /&gt;
* module_installed&lt;br /&gt;
* module_removed&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=69103 General Developer Forum thread for discussing this proposal]. &lt;br /&gt;
* [[Development:Messaging_2.0]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Coding guidelines|Events]]&lt;br /&gt;
[[Category:Grades]]&lt;/div&gt;</summary>
		<author><name>Agwells</name></author>
	</entry>
</feed>