Note: You are currently viewing documentation for Moodle 4.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Assignment types.

Development:Assignment types

From MoodleDocs

This guide should get you started with creating a new Assignment Type plugin. There may be several mistakes or omissions as it is based on my own experience of writing an Assignment Type plugin. Everything in here should work for Moodle 1.8 and 1.9. It may also work with 1.7, but is untested. It is similarly not tested (yet) with Moodle 2.0.

Make sure you are familiar with all the Moodle Coding Guidelines before you get started.

Introduction

Throughout this guide, references to PLUGINNAME should be replaced with the (lowercase) name you have chosen for your assignment type.

All of the files for your assignment plugin will need to go in a folder called: <moodleroot>/mod/assignment/type/PLUGINNAME

It is a very good idea to read through the code for the built-in assignment types (all stored in <moodleroot>/mod/assignment/type/) to see how they work, before getting stuck in to writing your own.

The main code

You need to create a file, inside your plugin's folder, called 'assignment.class.php'. This will contain the main code for your plugin. The basic layout of this file should look like this: <?php require_once($CFG->libdir.'/formslib.php');

class assignment_PLUGINNAME extends assignment_base {

} ?> Read through the code in: <moodleroot>/mod/assignment/lib.php for a list of the functions that are available to override and look at examples in the other assignment types to see how to do this.

Editing the settings form

You may find that to add extra items to the settings form for your plugin, that you need to find out the courseid or the assignmentid of the assignment you are editing. This information is not provided directly to the 'setup_elements' function that is used for this purpose. However, the following code should retrieve that information for you: $update = optional_param('update', 0, PARAM_INT); $add = optional_param('add', 0, PARAM_ALPHA); if (!empty($update)) {

   if (! $cm = get_record("course_modules", "id", $update)) {
       error("This course module doesn't exist");
   }
   $courseid = $cm->course;
   $assignmentid = $cm->instance;

} elseif (!empty($add)) {

   $courseid = required_param('course', PARAM_INT);
   $assignmentid = null;

}

Updating the database

If your plugin needs to add some new tables to the database (or extend one of the existing tables, although this would generally be a very bad idea!), follow these steps:

1. Create a file called 'version.php' in: <moodleroot>/mod/assignment/type/PLUGINNAME

This file should contain the following: <?php //These 2 lines are needed for Moodle 1.9 and above $plugin->version = 2009040900; // YYYYMMDDnn for when the plugin was created $plugin->requires = 2007020200;; // Look in <moodleroot>/mod/assignment/version.php for the minimum version allowed here

// These 2 lines are needed for Moodle 1.8 and below $submodule->version = $plugin->version; $submodule->requires = $plugin->requires; ?>

2. Create a folder called <moodleroot>/mod/assignment/type/PLUGINNANE/db

3. If you are using Moodle 1.8 (or below), create a file in this folder called 'mysql.sql', which should contain the SQL instructions for creating your database (these can be exported from phpmyadmin, if you want to simplify this step). If you are using Moodle 1.9 (or above), create a file in this folder called 'install.xml' see XMLDB_editor for extensive help with this - but note that there does not seem to be any support for directly creating the 'install.xml' file directly in an assignment type folder, so you will have to create it in a different folder and then copy it across manually.

4. If you later need to upgrade your database tables (due to a newly released feature in later versions of your plugin), you need to create the following files:

  • For Moodle 1.8 (or below), 'mysql.php', containing the function 'assignment_PLUGINNAME_upgrade($oldversion)'
  • For Moodle 1.9 (or above), 'upgrade.php', containing the function 'xmldb_assignment_type_PLUGINNAME_upgrade($oldversion=0)'.

The contents of these two functions can be identical to each other. See Development:Installing_and_upgrading_plugin_database_tables for more details about how to fill these in.

Language support

Create a subfolder: <moodleroot>/mod/assignment/type/PLUGINNAME/lang/en_utf8/ English is the default language to use if the language selected in Moodle is not provided for, so you should always include that at the very least. You can put text for other languages in a similarly named folder under PLUGINNAME/lang

In the language subfolder, create a file named assignment_PLUGINNAME.php which should contain all the translatable text from your plugin in the following format: <?php

$string['typePLUGINNAME'] = 'My new plugin'; $string['textidentifier'] = 'Some text I want to display';

?> Note: the first item in the file should be the display name of your plugin. This should be used by Moodle anywhere that your plugin is referred to by name (e.g. in the 'Add new activity list' or in the 'settings' page for your assignment type). However, due to a bug (MDL-16796) this does not currently work and will display [[typePLUGINNANE]] instead.

To use the text stored in this way, make use of the functions get_string('textidentifier', 'assignment_PLUGINNAME') and print_string('textidentifier', 'assignment_PLUGINNAME'). See also point 13 in the Coding general rules.

Help files

These should go in a subfolder of the language files: <moodleroot>/mod/assignment/type/PLUGINNANE/lang/en_utf8/help/PLUGINNAME The files in here should end '.html' and links to them can be created within your plugin by either directly calling 'helpbutton' in 'weblib.php' or by calling '$mform->setHelpButton' inside your 'setup_elements' code (for adding items to the 'settings' page). Example of calling 'helpbutton' directly: helpbutton('helppagename', get_string('linktitle','assignment_PLUGINNAME'), 'assignment_PLUGINNAME', true, false, , true) This will link to the page 'helppagename.html', the title of the link will be the translatable text 'linktitle', the page will be looked for within your plugin's lang/.../help folder, the link will be a help icon, the title text will not be shown, the page text will not be overridden and the code to display will be returned, rather than printed. Example of calling $mform->setHelpButton: $mform->setHelpButton('nameofelement', array('helppagename', get_string('linktitle','assignment_PLUGINNAME'), 'assignment_PLUGINNAME')); This will also create a link to the page 'helppagename.html', the title of the link will be the translatable text 'linktitle' and the page will be looked for within your plugin's lang/.../help folder.

See also

Discussion in the General Developer forum on Adding a new assignment type.