GETTING STARTED



One of the challenges in Moodle development is finding a good starting point for creating a Moodle plug-in. I thought it would be cool to build a step-wise tutorial on the subject as I embark on a personal whim:

Building a Moodle Module that allows the Student to post their Moodle Forum posts to FaceBook, and elicits responses from their group of friends into a totally separate forum. Perhaps too ambitious for a first project, but it hits all of the major challenges.

I will start by coming up with a cool name for the plug-in. I am thinking of calling it InYourFace ?  This particular exercise will focus on building an activity module - a module that provides an activity that can be added to any Moodle course.

WHERE DO WE START?

NOTE: This tutorial assumes that you already have a working installation of Moodle 1.9.x available, and that you have at least one course installed in your Moodle instance.  It also assumes that you have some basic knowledge of PHP and MySQL.

Before we embark on writing our first Moodle module, let's download a template provided by the folks at Moodle.org. In our case, we will be writing our module for Moodle version 1.9.x:

Download Link for The Module Template for Moodle 1.9

We end up with a file called NEWMODULE.zip which we need to save to our local drive and unzip. In my case, I download to the Public folder, unzip the file and end up with .\Public\NEWMODULE.

NEXT ARTICLE IN SERIES

Tuesday, May 18, 2010

09MOD: Moodle Module Development - Adding Our Custom Code to the Module

TO START THE TUTORIAL FROM THE BEGINNING, CLICK HERE

 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
In essence, all we need to find is the marker where we can enter our own code. Let's open the view.php script and look for the following snippet:

/// 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
 Each of these sections will be linked to an article dedicated to implementing its specific functionality.  This article will be updated as these articles become available.

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

7 comments:

  1. come back! i need to finish this stuff!

    ReplyDelete
  2. Pls moodle sensei answer us! pls :'( I don't like to cry a lot!

    ReplyDelete
  3. Thank you for this great tutorial! Looking forward to the next chapter!

    ReplyDelete
  4. Very usefull articles....
    at 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...

    ReplyDelete