Any collaborative project needs consistency and stability to stay strong.
These guidelines are to provide a goal for all Moodle code to strive to. It's true that some of the older existing code falls short in a few areas, but it will all be fixed eventually. All new code definitely must adhere to these standards as closely as possible.
If you need to update a help file:
I know it can be a little annoying to change your style if you're used to something else, but balance that annoyance against the annoyance of all the people trying later on to make sense of Moodle code with mixed styles. There are obviously many good points for and against any style that people use, but the current style just is, so please stick to it.
GOOD: $quiz
GOOD: $errorstring
GOOD: $assignments (for an array of objects)
GOOD: $i (but only in little loops)
BAD: $Quiz
BAD: $aReallyLongVariableNameWithoutAGoodReason
BAD: $error_string
define("FORUM_MODE_FLATOLDEST", 1);
function forum_set_display_mode($mode=0)
{
global $USER,
$CFG;
if ($mode)
{
$USER->mode
= $mode;
} else if (empty($USER->mode))
{
$USER->mode
= $CFG->forum_displaymode;
}
}
if ($quiz->attempts)
{
if ($numattempts >
$quiz->attempts)
{
error($strtoomanyattempts,
"view.php?id=$cm->id");
}
}
$var = 'some text without any
variables';
$var = "with special characters like a new line \n";
$var = 'a very, very long string with a '.$single.' variable in it';
$var = "some $text with $many variables $within it";
/**
* The description should be first, with asterisks laid out exactly
* like this example. If you want to refer to a another function,
* do it like this: {@link clean_param()}. Then, add descriptions
* for each parameter as follows.
*
* @param int $postid The PHP type is followed by the variable name
* @param array $scale The PHP type is followed by the variable name
* @param array $ratings The PHP type is followed by the variable name
* @return mixed
*/
function forum_get_ratings_mean($postid,
$scale, $ratings=NULL)
{
if (!$ratings)
{
$ratings
= array(); //
Initialize the empty array
if ($rates
= get_records("forum_ratings",
"post", $postid))
{
//
Process each rating in turn
foreach
($rates as $rate)
{
....etc
foreach ($objects
as $key =>
$thing) {
process($thing);
}
if ($x ==
$y)
{
$a
= $b;
} else if ($x ==
$z) {
$a
= $c;
} else {
$a
= $d;
}
$a = array()
or $obj = new stdClass();
.optional_variable()
function. Use the optional_param()
function instead. Pick the correct PARAM_XXXX value for the data type you expect. To check and set an optional
value for a variable, use the set_default()
function.require_variable()
function. Use the required_param()
function instead. Pick the correct PARAM_XXXX value for the data type you expect.data_submitted()
, with care. Data must still be cleaned before use.$_GET
, $_POST
or $_REQUEST
. Use the
appropriate required_param()
or optional_param()
appropriate to your need.if (isset($_GET['something']))
.
Use, e.g., $something = optional_param( 'something',-1,PARAM_INT )
and then perform
proper test for it being in its expected range of values e.g., if ($something>=0) {...
.required_param()
, optional_param()
and other variables initialisation at the beginning of each file to make them easy to find.<input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
.
When you process the form check with if (!confirm_sesskey()) {error('Bad Session Key');}
.clean_filename()
function, if this
has not been done already by appropriate use of required_param()
or optional_param()
addslashes()
applied to it before it
can be written back. A whole object of data can be hit at once with addslashes_object()
.POST
data (from a form with method="POST"
) as opposed to GET
data (ie, data from the URL line).$_SERVER
if you can avoid it. This has portability
issues.clean_param()
function using the appropriate PARAM_XXXX for the datatype.Version: $Id: coding.html,v 1.2 2006/09/12 08:56:10 toyomoyo Exp $