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

Thursday, May 13, 2010

06MOD: Moodle Module Development - The module's form

TO START THE TUTORIAL FROM THE BEGINNING, CLICK HERE

Moodle follows a very specific pattern for activity modules:
  • User selects a course
  • User turns editing on
  • User selects a topic or week
  • User selects an activity to add
Once the activity has been selected from the dropdown box, the module's form is displayed.  In our case, selection of the "Facebook Integration" activity calls the form described in the .\mod\inyourface\mod_form.php script.

In this post we will go over the purpose of the form, how to customize the form, and how to handle the pre/post processing of data in the form.

NOTE: During this exercise, we will also have to modify our
module's database structure.  We will cover these steps in detail in a separate post.

We will start by examining our mod_form.php file. Bascially, it generates a new form that is based on the Moodle Form module (notice at the top of the file, it "extends moodleform_mod").

The form is divided into 3 basic sections:

1. GENERAL: Elements required for every activity type
2. CUSTOM: Elements required by our module
3. STANDARD: Elements that are common to all modules

These sections correlate to the following areas of the activity creation form (the form that comes up after the user chooses to add an activity to a course's section or topic):


Click Image to Zoom

For our module, we will create a single custom element. A simple drop down where the user can select the number of forum posts that the student can submit using our Module.

We will only concentrate on the custom section o four mod_form.php script.  Open the file for editing and find the following section:


EDITING .\mod\inyouface\mod_form.php

//-------------------------------------------------------------------------------
/// Adding the rest of inyourface settings, spreeading all them into this fieldset
/// or adding more fieldsets ('header' elements) if needed for better logic
$mform->addElement('static', 'label1', 'inyourfacesetting1', 'Your inyourface fields go here. Replace me!');

$mform->addElement('header', 'inyourfacefieldset', get_string('inyourfacefieldset', 'inyourface'));
$mform->addElement('static', 'label2', 'inyourfacesetting2', 'Your inyourface fields go here. Replace me!');

//-------------------------------------------------------------------------------


We actually start by modifying the .\mod\inyourface\lang\en_utf8\inyourface.php file!  We need to change the "inyourfacefieldset" variable.  Open the file in your editor and change that variable to "Feed Options".

We will also add one more variable that we'll use as a label for our dropdown list.

The completed inyourface.php file will look like this when you are done, with our changes in red:


EDITING \lang\en_utf8\inyourface.php

<?php
    $string['inyourface'] = 'Facebook Integration';
    $string['modulename'] = 'Facebook Integration';
    $string['modulenameplural'] = 'Facebook Integration';

    $string['inyourfacefieldset'] = 'Feed Options';
    $string['inyourfaceintro'] = 'Post to Facebook Intro';
    $string['inyourfacename'] = 'Post to Facebook';

    $string['postlimit'] = 'Maximum Posts';
?>

We can now concentrate on the form itself. We start by commenting out the annoying labels - label1 and label2 that the NEWMODULE template uses as a place holder.  Comment out these lines, so your mod_form.php custom block now looks like this:


EDITING .\mod\inyouface\mod_form.php

//-------------------------------------------------------------------------------
/// Adding the rest of inyourface settings, spreeading all them into this fieldset
/// or adding more fieldsets ('header' elements) if needed for better logic
/// $mform->addElement('static', 'label1', 'inyourfacesetting1', 'Your inyourface fields go here. Replace me!');

$mform->addElement('header', 'inyourfacefieldset', get_string('inyourfacefieldset', 'inyourface'));
/// $mform->addElement('static', 'label2', 'inyourfacesetting2', 'Your inyourface fields go here. Replace me!');

//-------------------------------------------------------------------------------


That leaves one active line of code in the block - the one that adds the header element to our block.  We want to add our drop down list immediately below the header.  When we are finished our custom block will look as follows:


EDITING .\mod\inyouface\mod_form.php

//-------------------------------------------------------------------------------
/// Adding the rest of inyourface settings, spreeading all them into this fieldset
/// or adding more fieldsets ('header' elements) if needed for better logic
/// $mform->addElement('static', 'label1', 'inyourfacesetting1', 'Your inyourface fields go here. Replace me!');

$mform->addElement('header', 'inyourfacefieldset', get_string('inyourfacefieldset', 'inyourface'));/// $mform->addElement('static', 'label2', 'inyourfacesetting2', 'Your inyourface fields go here. Replace me!');

$choices = array();
$choices[0] = 1;
$choices[1] = 2;
$choices[2] = 3;
$mform->addElement('select', 'postlimit', get_strin('postlimit','inyourface'), $choices);


WE CAN NOW CHECK THE EFFECT OF OUR CHANGES
TO THE MODULE'S FORM.

THAT'S COMING UP NEXT!

Next Article in Series

1 comment:

  1. The last code line: $mform->addElement('select', 'postlimit', get_strin('postlimit','inyourface'), $choices);

    "get_strin" should be "get_string".

    ReplyDelete