After the Module is setup and configured correctly, it's time to add our own logic. This is what we had been waiting for!
The bulk of our logic goes into three scripts in our module's directory:
- view.php
- lib.php
- locallib.php
THE MODULE'S VIEW.PHP SCRIPT
This script handles the display of the activity once it is added to a course section. If we look at the code provided in the template, it already handles the following:
- Determining the instance of the module for the current course
- Entering the view as an the activity to the Moodle log
- Displaying the Moodle header and footer
/// Print the main part of the page
echo 'YOUR CODE GOES HERE';
Let's test it out by modifying it a little, and looking at the values of the Moodle $COURSE global. Modify the snippet above so it now looks like this:
/// Print the main part of the page
echo '<pre>';
var_dump( $COURSE );
echo '</pre>'
When we navigate to our course, and look at our activity, it now displays our variable dump:
Click Image to Zoom
THE MODULE'S LIB.PHP SCRIPT
The lib.php script serves two distinct functions:
1. It is where Moodle looks for a number of standard functions that SHOULD be implemented for every module:
- Add Instance
- Update Instance
- Delete Instance
- User Outline
- User Complete
- Recent Activity
- Cron handler
- Get Participants
2. A starting place for building our own custom variables and functions.
OUR OWN LOCALLIB.PHP SCRIPT
Although we could place our code inside the lib.php script, it is better to reserve lib.php for the Moodle handlers. It is a better practice to create our own locallib.php file, and place our functions within it.
Open up the editor, and create a new file called locallib.php in our module's root directory (,\mod\inyourface). Create the locallib.php file with the following contents:
<?php
/**
* Returns the current course id
*
* @return int The Course Id
*/
function inyourface_getcourseid()
{
global $COURSE;
return $COURSE->id;
}
?>
Notice that the function is commented in the phpDoc format. That is a subject all to itself, and one we should dedicate to a completely separate tutorial.
Once the locallib.php file has been created, we can now reference it within our view.php script. Open view.php and modify its references on the very top of the script so it looks like this:
/**
* InYourFace Module Instance
*
* @author Leo Cunha
* @version $Id: view.php,v 1.6.2.3 2009/04/17 22:06:25 skodak Exp $
* @package mod/inyourface
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
require_once(dirname(__FILE__).'/locallib.php');
Now we can call our new function from view.php. Replace our original snippet - the one that displayed the $COURSES variable with the following code:
/// Print the main part of the page
$courseid = inyourface_getcourseid();
echo 'We are editing course number '.$courseid.'<br />';
That's it - we now have added our first piece of custom code to the module. When we execute the module from the Moodle course, it now displays the course id:
Click Image to Zoom
is it only up to article 9? (--.)
ReplyDeletemoodle sensei where are you?
ReplyDeletecome back! i need to finish this stuff!
ReplyDeletePls moodle sensei answer us! pls :'( I don't like to cry a lot!
ReplyDeletemoodle sensei we need you!
ReplyDeleteThank you for this great tutorial! Looking forward to the next chapter!
ReplyDeleteVery usefull articles....
ReplyDeleteat the end of 2015 still it's helpfull.... even though moodle has huge change during this time period but with small modification on this article, it works....
I guess there is no chance to get next part of this tutorial.....
Thanks a lot...