Note:

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

Patching forms tutorial: Difference between revisions

From MoodleDocs
m (Continued update of draft)
Line 22: Line 22:
This creates a new input element on a page.
This creates a new input element on a page.


While forms can be produced in any scripting page which outputs code, you will often find code for printing out forms in a file like mod_form.php or in index.php of the affected component.
While forms can be produced in any scripting page which outputs code, you will often find code for printing out forms in a file like mod_form.php or in index.php of the affected component.  The examine the URL of the page being viewed as a starting point for finding the file to be changed.
 
Always comment your code so you can change or remove it later..  For single line modifications, considering putting in your new code, and then and it with //<yourusername> <date> <the old code>.  This allows for retention of eisting line numbers.


==Changing defaults==
==Changing defaults==

Revision as of 07:46, 10 June 2008

by Gary Anderson

One of the easiest local modifications that an institution can make to customize their Moodle site is to make simple changes to the form elements that appear on pages. This permits one to set different default values when creating new items, allows one to hide unused input elements to simplify the interface, and lets one identify certain more complicated elements as "advanced" so that features are still availale while simplifying the interface for most uses.

This tutorial will demonstrate how to do such modifications.

Warning

Making custom changes to the PHP scripting code in Moodle should be done carefully. You should always test your code before using it on your production site and should backup the original scripts. You should always comment your code so you know what has been changed. Care should also be taken during upgrades in the code to make sure things are not broken (like conflicts during a CVS update).

Having said that, these patches are among the simplest and the recovery from an error is easy: simply replace the file with original from the core distribution.

Moodle Forms Overview

Rather than placing HTML form elements directly on the page, most well-written Moodle code will use the Forms Library. This allows a standard way to create user input elements and also centralizes code making it easier to override certain behaviors creating a greater consistency.

An entire form is an "object" with methods and attributes. In many places in the Moodle code, the instance of the object is identified my the variable $mform, but you can use any variable.

Common methods of the form object look like:

  • $mform->addelement(<elementtype>,<elementidentifier>,<displayname>,<options>);

This creates a new input element on a page.

While forms can be produced in any scripting page which outputs code, you will often find code for printing out forms in a file like mod_form.php or in index.php of the affected component. The examine the URL of the page being viewed as a starting point for finding the file to be changed.

Always comment your code so you can change or remove it later.. For single line modifications, considering putting in your new code, and then and it with //<yourusername> <date> <the old code>. This allows for retention of eisting line numbers.

Changing defaults

WRITE THIS NEXT


Hiding fields

==Setting fields to "Advanced""