Moodle/Totara | Creating a new Activity Plugin

| By Webner

Introduction: In Moodle/Totara we can add a different type of activities to a course by selecting from the list of standard activities available. As shown in the the list below:

But there might be some cases in which we need to add a custom activity to a course having its own features and completion criteria. In this case we can create a new activity plugin using the basic structure and conventions similar to that of a standard activity.

Basic structure of an activity plugin: We need to follow this basic structure and can add other files based on our requirements:
1.  DB: This folder of the plugin contains the database and access related files for plugin. If our plugin need to create tables and save records in database, we need to write necessary code for it in the following files:
access.php- for defining the capabilities and access permissions for the plugin.
The following code adds the capability “’mod/fourstep:addinstance’” to plugin so that it can be added as an instance on a course:

   $capabilities = array('mod/fourstep:addinstance' = & gt; array(
   'riskbitmask' = & gt; RISK_XSS,
   'captype' = & gt;
   'contextlevel' = & gt; CONTEXT_COURSE,
   'archetypes' = & gt; array(
   'editingteacher' = & gt; CAP_ALLOW,
   'manager' = & gt; CAP_ALLOW),
   'clonepermissionsfrom' = & gt;

install.php- for defining database functions performed while installing the plugin.
install.xml- for defining the tables associated with the plugin. It contains xml required for creating specific tables and their fields.

As in the following XML file, we are creating a table named “fourstep” for our activity plugin. The table has required fields such as :
course-to store the id of the course on which the activity is created
name-name of our activity instance.
intro- any description given to activity
timecreated,timemodified-for saving timestamps
quiz-id of the quiz that are activity is monitoring
userid-user for which quiz_attempts are monitored
comp_fourstep_passed- whether or not the fourstep activity is complete, based on conscutive three passed atempts in quiz.

XML code:

<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/fourstep/db" VERSION="20160225" COMMENT="XMLDB file for Moodle mod/fourstep"
<TABLE NAME="fourstep" COMMENT="Default comment for fourstep, please edit me">
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Course fourstep activity belongs to"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="name field for moodle instances"/>
<FIELD NAME="intro" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="General introduction of the fourstep activity"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Format of the intro field (MOODLE, HTML, MARKDOWN...)"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="grade" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="100" SEQUENCE="false" COMMENT="The maximum grade. Can be negative to indicate the use of a scale."/>
<FIELD NAME="quiz" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="quiz fourstep activity belongs to"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="quiz fourstep activity belongs to"/>
<FIELD NAME="comp_fourstep_passed" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="User Id"/>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>

uninstall.php- define code to be executed during the plugin uninstallation.

upgrade.php- upgrade function in this file will attempt to perform all the necessary actions to upgrade older installation to the current version.

2. lang: this folder is used to store language strings for the plugin. If our plugin need to support different languages then we need to create a separate folder under lang for each language. As most of the plugins support English only so they contain lang/en folder in it.

3. pix: this is used to store any images or icons that we need to use in our plugin.

4.  index.php: this file is used to list all the instances of our module/activity that are in use based on a particular course. This file uses the courseid parameter to determine the course currently being referenced.

5. lib.php: this file is the main important file required to make our activity plugin function. This is the library of interface functions and constants for module. All the core Moodle functions, needed to allow the module to work integrated in Moodle are placed here. Each of the function in this file must have a name that starts with modulename_. Following is the list of mandatory functions that must be included within lib.php:

function newmodule_add_instance($newmodule)
This function is executed after an admin creates an instance of activity plugin.
function newmodule_update_instance($newmodule)
This function is executed after an admin update an instance of activity plugin
function newmodule_delete_instance($id)
This function is executed after an adminr delete an instance of activity plugin.
function newmodule_user_outline()
function newmodule_user_complete($course, $user, $mod, $newmodule)
function newmodule_print_recent_activity($course, $isteacher, $timestart)
function newmodule_cron()

This function is used to write any actions related to our plugin that are to be executed as a part of moodle cron run.

6.  locallib.php: this is the internal library of functions for activity plugin. In case our plugin is supposed to perform a complex functionality then we can use this file to to implement some of the advanced functionality of plugin here. The functions written here are directly accessible in plugin lib.php file.

7. mod_form.php: this is the main plugin configuration from that we use for user input. We can modify the configuration form by adding any of the required form elements. The form created using this file will be used to create or edit an instance of our activity plugin. As for example:

defined('MOODLE_INTERNAL') || die();
require_once($CFG - & gt; dirroot.
class mod_fourstep_mod_form extends moodleform_mod {
     * Defines forms elements
    function definition() {
        global $CFG;

        $mform = $this - & gt;
        // Adding the "general" fieldset, where all the common settings are showed.
        $mform - & gt;
        addElement('header', 'general', get_string('general', 'form'));

        // Adding the standard "name" field.
        $mform - & gt;
        addElement('text', 'name', get_string('fourstepname', 'fourstep'), array('size' = & gt;
        //get quizzes available in the course so as to select the quiz that fourstep will be //monitoring
        $rec = $this - & gt;
        $res = $mform - & gt;
        addElement('select', 'quiz', get_string("select"), $rec);
        if (!empty($CFG - & gt; formatstringstriptags)) {
            $mform - & gt;
            setType('name', PARAM_TEXT);
        } else {
            $mform - & gt;
            setType('name', PARAM_CLEANHTML);
        $mform - & gt;
        addRule('name', null, 'required', null, 'client');
        $mform - & gt;
        addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
        $mform - & gt;
        addHelpButton('name', 'fourstepname', 'fourstep');

        // Adding the standard "intro" and "introformat" fields.
        if ($CFG - & gt; branch & gt; = 29) {
            $this - & gt;
        } else {
            $this - & gt;

        // Add standard grading elements.
        $this - & gt;

        // Add standard elements, common to all modules.
        $this - & gt;

        // Add standard buttons, common to all modules.
        $this - & gt;

8.  version .php: This file contains a number of properties, which are used during the plugin installation and upgrade process. It allows to make sure the plugin is compatible with the given Moodle site. Whenever we need to upgrade the moodle site after making changes in plugin code we’ll use this file to update the plugin version and it will force the moodle site to upgrade with latest plugin changes. As following:

defined('MOODLE_INTERNAL') || die();
//specifies the plugin type- “mod_” for activity plugin
$plugin - & gt;
component = 'mod_fourstep';
//specifies the plugin version
$plugin - & gt;
version = 2016030303;
$plugin - & gt;
release = 'v0.0';
//specifies the moodle version required to run this plugin
$plugin - & gt;
requires = 2014051200;
$plugin - & gt;
maturity = MATURITY_ALPHA;
//specifies the cron run time for the plugin
$plugin - & gt;
cron = 1;
$plugin - & gt;
dependencies = array();

9. view.php: this is the view page of our activity. The file contains necessary code that is required to render the plugin view when user clicks on an instance of the activity. The course module id is passed to this script and set as $id, which is then used to retrieve the course data and the information.

After adding the above required files we’ll able to see the activity in the “Add activity or resource” area of the course as below:


Leave a Reply

Your email address will not be published. Required fields are marked *