List of rules for proper development in the Moodle environment

Naming of parameters (do not use “_”  or uppercase)

//BED $Userid, $course_id, $ModuleCourseid
//GOOD $userid, $courseid, $modulecourseid,  $param1, $parm2 ..

Do not use global parameters to write data

//BED
global $data;
$data['parameter']="";

Do not use the required function to include class, must use namespaces

//GOOD
namespace core\event;
class base {
} 

#--------#
namespace mod_forum\event;
class post_read extends \core\event\base {
}

#--------#
\mod_forum\event\post_read::create($post->id, ...)->trigger();

Name Space example in moodle

Always use get_string function for text or label text

$a = new stdClass();
$a->url = '';
get_string("languageidentifier',"plugin place",$a);

Use common Moodle function to generate URL path

$url = new \moodle_url('path_to_file',array("id"=>2));

Use common Moodle function to generates tags, links ,html

$attributes=array('class'=>'name of the classes');
html_writer::link($url, 'text',attributes)
html_writer::tag(....
html_writer::start_tag(....
html_writer::end_tag(....
....

Use the right syntax for access to MYSQL DB

example:

$sql = "SELECT *
        FROM {some_table}
        WHERE id > :above";
$records = $DB->get_records_sql($sql, array('above'=>111));

Source:

https://docs.moodle.org/dev/SQL_coding_style

https://docs.moodle.org/dev/Automatic_class_loading

https://docs.moodle.org/dev/Coding_style

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.