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 11, 2010

01MOD: Moodle Module Setup - The install.xml file

TO START THE TUTORIAL FROM THE BEGINNING, CLICK HERE

We have just finished upacking our Moodle Module Template!

If we inspect the contents of our new directory, it's really not very assuming:




FIRST ORDER OF BUSINESS - MAKE THE PLUG-IN PLUGGABLE!


STEP 1 - the install.xml file
A plug-in is only a plug-in if we have "plugged it in". In Moodle, we accomplish this by registering the Module in the Moodle database. Towards this end, we focus on a single file in the package - the install.xml file.


It is located in the .\NEWMODULE\db\ directory.


So, I load it up in my trusty Notepad++ (here's a gratuitous plug for this great GPL editor: Notepad++ Home Page) and notice that install.xml is nothing but an XMLDB file. The file should something like this when you load it into your editor:


Click Image To Zoom


THE MODULE'S XMLDB FILE CAN BE EDITED USING MOODLE'S XMLDB EDITOR

This is the preferred method, and is covered in the post listed below. The editor is available after the module has been copied to the Moodle .\mod directory, so instructions for modifying the file manually are also provided below.


To learn a little about the XMLDB editor, VISIT THE XMLDB EDITOR ARTICLE.


TO EDIT THE XMLDB FILE MANUALLY:

I run a search & replace for the word "newmodule" and replace it with my module's name: inyourface. For version 1.9, it should result in 12 replacements.

We can also change the comments in the file, but we will save that for later. Before we can test our module, we still have a little more work to do!!! We will cover these steps in the next post.

CONTENTS OF install.xml after changes:
<?xml version="1.0" encoding="UTF-8" >
<XMLDB PATH="mod/inyourface/db" VERSION="20070401" COMMENT="XMLDB file for Moodle mod/inyourface"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="inyourface" COMMENT="Default comment for inyourface, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Course inyourface activity belongs to" PREVIOUS="id" NEXT="name"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="name field for moodle instances" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="General introduction of the inyourface activity" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Format of the intro field (MOODLE, HTML, MARKDOWN...)" PREVIOUS="intro" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="introformat" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
<INDEXES>
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>
</TABLES>
<STATEMENTS>
<STATEMENT NAME="insert log_display" TYPE="insert" TABLE="log_display" COMMENT="Initial insert of records on table log_display. Each record describes how data will be showed by log reports.">
<SENTENCES>
<SENTENCE TEXT="(module, action, mtable, field) VALUES ('inyourface', 'add', 'inyourface', 'name')" />
<SENTENCE TEXT="(module, action, mtable, field) VALUES ('inyourface', 'update', 'inyourface', 'name')" />
<SENTENCE TEXT="(module, action, mtable, field) VALUES ('inyourface', 'view', 'inyourface', 'name')" />
</SENTENCES>
</STATEMENT>
</STATEMENTS>
</XMLDB>


NEXT ARTICLE IN SERIES

3 comments:

  1. You might find it easier to use Moodle's built-in XMLDB editor to edit the database, rather than editing the XML file manually (where there's more scope for human error).

    ReplyDelete
  2. I agree. I wanted to illustrate the contents of the file, and introduce the XMLDB editor in a totally separate session - when the first upgrade of the module becomes necessary.

    Once that is available, I can place a link here, along with a reference to XMLDB editor documentation.

    Thanks - good catch.

    ReplyDelete
  3. Hi there.. what means the "SEQUENCE" attribute? Where can i find a good tutorial explain all attributes? i don't know how to use the xmldb editor .. thanks

    ReplyDelete