Note:

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

Finding your way into the Moodle code: Difference between revisions

From MoodleDocs
(→‎See also: Secrets of Learning Moodle Development! link)
m (→‎See also: Remove the link to the outdated course at dev.moodle.org (which is going to be shut down))
(17 intermediate revisions by 9 users not shown)
Line 1: Line 1:
This article is aimed at people totally new to Moodle development, who may, or may not, have previous programming experience. It is a few tips to help you get started with the mass of Moodle code.
This article is aimed at people totally new to Moodle development and have some PHP experience, if you don't have that experience please visit [[PHP for novices|PHP for novices]]. It is a few tips to help you get started with the mass of Moodle code.


Please feel free to add more tips here, but I think it is also good if we can keep this article quite short.
Please feel free to add more tips here, but I think it is also good if we can keep this article quite short.
Line 6: Line 6:


If you have not already found it, the main source of developer documentation is [[Developer_documentation]].
If you have not already found it, the main source of developer documentation is [[Developer_documentation]].
==PHP for complete novices==
Moodle is written in [http://www.php.net PHP] - a relatively easy language to learn.
===How it works===
When your browser requests a webpage, you are asking for a file. The computer (server) you connect to at the other end of the internet normally sends back a file of [http://en.wikipedia.org/wiki/Html HTML] (which you need to get familiar with if you are not already) that your browser then converts into a webpage. HTML documents are static, meaning that they exist on the server in the same form as they are sent to you. PHP on the other hand is dynamic - the form it exists in on the server is different from what is ultimately sent to you, which may be different for different people. When a PHP file is requested, the server runs the instructions it contains and sends the output (HTML) to you in the normal way. PHP code can either be inserted into part of an HTML document, or can exist in files with nothing but PHP. You can recognise it by its start (<?php) and end (?>) tags
PHP almost always uses a database to store information. That way, the PHP forms a template, that gets filled with whatever information is appropriate to the user e.g. there is a space at the top right of this page for a username, but it is different for everyone.
In the following examples, the numbered line indicates what the intention of the developer is and the text in the box with the blue dashed outline is the code that would actually be written in the php file. Notice that the php lines always end with a ; symbol to indicate the end of a command.
The PHP code for the username box will say some thing like:
1. start a box by printing <div>
  echo "<div>";
2. set the font colour to blue by printing <font color="#002bb8">
  echo "<font color="#002bb8">";
3. get the username for user number x from the database
  $username=fullname(123);
4. print that username
  echo $username;
5. end font colour by printing </font>
  echo "</font>";
6. end div by printing </div>
  echo </div>
End result (HTML that will be sent to your browser):
  <div>
  <font color="#002bb8">
    Joe Bloggs
  </font>
  </div>
===Variables===
In the above example, $username is a variable. It is effectively algebra, where a word of some sort (starting with a dollar sign) represents a piece of data, in this case the username returned by the database.
===Functions===
In the above example, fullname() is a function. This means a collection of commands (which in this case accesses the user table of the database and returns the name) that have been defined elsewhere, which can be run using a shorthand reference. This saves writing them out hundreds of times in different places. Functions are often written down in large library files, so that they can be used whenever they are needed.
===Including other files===
Moodle is a very complex program. Instead of just one page with a list of commands, it uses a great many files linked together using the [http://uk.php.net/include/ include] or [http://uk.php.net/manual/en/function.require-once.php require_once] commands. This means that when you request e.g. the front page, there will be commands such as the following:
<code php>
require_once('config.php');
</code>
which means 'if its not been done already for this page request, go and get config.php and run all of its instructions before continuing'. In this way, a single Moodle webpage can sometimes include as many as 150 separate files on the server. This is how the library files are made available - you include the library and can then use any of the functions that the library contains e.g. fullname().
===Libraries and functions===
To make the username commands above into a function, we would write them like this:
<code php>
function print_username_box($useridnumber) {
    echo "&lt;div&gt;";
    echo "&lt;font color="#002bb8"&gt;";
    $username=fullname($useridnumber);
    echo $username;
    echo "&lt;/font&gt;";
    echo &lt;/div&gt;
}
</code>
Now, whenever the commands are needed, the developer simply writes
<code php>
print_username_box(123);
</code>


==Finding a way in==
==Finding a way in==
Line 90: Line 19:
==Understanding what you find==
==Understanding what you find==


As you look at the code, it is often a good idea, to insert statements like  
As you look at the code, it is often a good idea, to insert statements like
<code php>
  debugging('In function require_login');
  debugging('In function require_login');
</code>
which will print the message above if it gets far enough so you know the code has not stalled before there, or
which will print the message above if it gets far enough so you know the code has not stalled before there, or
  print_object($course);
  print_object($course);
which will print out a variable, showing you what it contains.  
which will print out a variable, showing you what it contains.  


Variable like $course are often objects with lots of fields. Seeing what they contain will help you understand what is going on. The first of these prints out whatever text you give it, and information about the sequence of function calls that the code took to get there. (It only works if you go to ''Administration > Server > [[Debugging]]'', and turn the debug level to ALL or DEVELOPER.)
Variable like $course are often objects with lots of fields. Seeing what they contain will help you understand what is going on. The first of these prints out whatever text you give it, and information about the sequence of function calls that the code took to get there. (It only works if you go to ''Administration > Server > [[:en:Debugging|Debugging]]'', and turn the debug level to ALL or DEVELOPER.)
 
==Tools that can help==


==Tools can help==
*'''Eclipse''', for instance helps finding function definitions. You can hold down CTRL and click on the name of a function, it immediately jumps you to where that function is defined. ([[Setting_up_Eclipse]])
*'''Netbeans'''
*'''Sublime 2'''
*'''Emacs'''
*'''Vim'''
*'''TextMate'''


Finally, it is helpful to have an editor that lets you jump around the code. For example, I like Eclipse, and if I hold down CTRL and click on the name of a function, it immediately jumps me to where that function is defined. Most good editors (including Emacs and vi/Vim) can do this. ([[Setting_up_Eclipse]])
See also [[Developer documentation#Tools]] and [[:Category:Developer tools]].


==See also==
==See also==


* First posted at http://moodle.org/mod/forum/discuss.php?d=82799#p366264
* [[Development FAQ]]
* [[Developer_documentation]]
* [[Developer_documentation]]
* [[Overview]]
* [[Overview]]
* [[ctags]]
* [[ctags]]
* [http://blog.danpoltawski.co.uk/archives/2009-05-Secrets-of-Learning-Moodle-Development!.html
* [http://blog.danpoltawski.co.uk/2009/05/secrets-of-learning-moodle-development "Secrets of Learning Moodle Development!"] by Moodle core developer [[User:Dan Poltawski| Dan Poltawski]]
Secrets of Learning Moodle Development!] by core developer Dan Poltawski
* [http://www.slideshare.net/tjh1000/a-basic-introduciton-to-the-moodle-architecture-5442122 A basic introduction to the Moodle architecture] by Moodle core developer Tim Hunt
* [http://www.aosabook.org/en/moodle.html The Architecture of Open Source Applications: Moodle] by Tim Hunt

Revision as of 09:02, 19 June 2017

This article is aimed at people totally new to Moodle development and have some PHP experience, if you don't have that experience please visit PHP for novices. It is a few tips to help you get started with the mass of Moodle code.

Please feel free to add more tips here, but I think it is also good if we can keep this article quite short.

Developer documentation

If you have not already found it, the main source of developer documentation is Developer_documentation.

Finding a way in

For finding where to start, you need to know that when you are looking at, for example, http://moodle.org/mod/forum/discuss.php?d=82799, then the code for that is in /mod/forum/discuss.php, and you just need to follow through what it does.

It calls functions in the main Moodle libraries, and the three most important are:

  • lib/moodlelib.php - general stuff.
  • lib/weblib.php - things to do with output of HTML
  • lib/dmllib.php - things to do with getting data in and out of the database.

Understanding what you find

As you look at the code, it is often a good idea, to insert statements like

debugging('In function require_login');

which will print the message above if it gets far enough so you know the code has not stalled before there, or

print_object($course);

which will print out a variable, showing you what it contains.

Variable like $course are often objects with lots of fields. Seeing what they contain will help you understand what is going on. The first of these prints out whatever text you give it, and information about the sequence of function calls that the code took to get there. (It only works if you go to Administration > Server > Debugging, and turn the debug level to ALL or DEVELOPER.)

Tools that can help

  • Eclipse, for instance helps finding function definitions. You can hold down CTRL and click on the name of a function, it immediately jumps you to where that function is defined. (Setting_up_Eclipse)
  • Netbeans
  • Sublime 2
  • Emacs
  • Vim
  • TextMate

See also Developer documentation#Tools and Category:Developer tools.

See also