Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Question type plugin how to

From MoodleDocs

To follow this guide, you need to get hold of the qtype_NEW plugin template. You can get it from CVS in the contrib area or from the modules and plugins database. The rest of this guide is just a copy of the README.txt file from in there. (Or is the readme file a copy of this page?)

New question type template README file

Welcome to the new question type template.

This blank skeleton is a good place to start if you want to implement your own Moodle question type plugin.

Another good way to start is by looking at an existing question type that has good documentation in the code like TODO (if there is one, otherwise we'll hope that Tims will "light the way" :-) )

The latest version of the template can be found in CVS in the contrib/qtype_NEW module. Because you want your own copy do a cvs export, not a cvs checkout. The package can also been downloaded from the Modules and plugins database.

The latest version of this help can be read (nicely formatted) at https://docs.moodle.org/en/How_to_write_a_question_type_plugin

WARNING, THIS TEMPLATE IS NOT COMPLETE YET!

Getting started

Before you get to the interesting bit, you need to do a bit of file-renaming and search-and-replacing to turn this generic template into your own question type. You need to have chosen two things

The identifier for your question type
This is a string of lowercase letters and perhaps underscores that the Moodle code uses to refer to your question type. This needs to be unique, so perhaps start it with your initials. For example, all the quetions types I create while working at the OU will be referred to as ou_something. For the rest of these instructions, I will assume you have chosen 'myqtypeidentifier'.
The name of your question type
This is the name that people will see in the Moodle User-interface (in the English translation). For these instructions I will assume you have chosen 'My Question Type Name'.

Then you need to

  1. Rename the directory question/type/QTYPEID to question/type/myqtypeidentifier.
  2. Rename the language file lang/en_utf8/qtype_QTYPEID.html to lang/en_utf8/qtype_myqtypeidentifier.html
  3. Rename the help file lang/en_utf8/help/quiz/QTYPEID.html to lang/en_utf8/help/quiz/myqtypeidentifier.html
  4. Search and replace 'QTYPEID' with 'myqtypeidentifier' in all the files. You should find:
    • 2 occurrences in the language file
    • 1 occurrence in db/install.xml
    • 5 occurrences in simpletest/testquestiontype.php
    • 3 occurrences in editquestion.html
    • 1 occurrence in editquestion.php
    • 3 occurrences in questiontype.php
  5. Search and replace 'QTYPENAME' with 'My Question Type Name' in all the files. You should find:
    • 1 occurrence in the help file
    • 3 occurrences in the language file
    • 2 occurrences in questiontype.php
  6. Search and replace YOURNAME with your name. (This is only in the comments at the top of each file so it is not critical, but surely you want to take credit for your work.) You should find one occurrence in:
    • the language file.
    • simpletest/testquestiontype.php
    • editquestion.html
    • editquestion.php
    • questiontype.php
  7. Search and replace YOUREMAILADDRESS with your email address. (Again, this is only in the file header comments, but it is helpful if people can contact you if they have any questions about your code.) You should find:
    • There should be one occurrence of YOUREMAILADDRESS in each of the files listed under YOURNAME.
  8. Search and replace YOURPACKAGENAME with a package name for your code. This is used by PHPdoc when building the documentation for your classes. I suggest you make up one package name for all the question types you write. For example all the question types I write are in the package ou_questiontypes.
    • There should be one occurrence of YOURPACKAGENAME in each of the files listed under YOURNAME.
  9. Edit icon.gif to make an icon that represents your question type.

Finally, so you can test your code while working on it, you need to copy the lang and question folders on top of the folders in your Moodle test installation. This copy should not ovewrite any existing files. It should just add some new files and folders to that directory tree.

Now for the interesting bit

Now you need to write the code to

  1. Create any database tables you need. This works just like normal in Moodle with the files in the db directory and the version.php file. If you don't need to create any database tables, you can delete the db directory.
  2. Create the editing form for you question type, and the code to populate it. That means adding code to the files editquestion.html and editquestion.php.
  3. Create the template that will display the question to the student. This is in display.html. To make good flexible formating you should use CSS classes and use already existing ones when possible. An easy way to find which existing CSS classes exist is TODO
  4. Implement the rest of the question type class. This is in questiontype.php. You need to
    1. TODO finish writing this section.
  5. Make sure that the language file contains all the strings you refer to. Note that some of the strings you want may already be in the qtype_base.php and moodle.php language files in Moodle core. If so, use the existing strings. An easy way to find the existing strings is TODO
  6. Write the help file for your question type, and any other help files you need.
  7. Write a set of unit tests for you question type.

All the places in the code where you need to do things are marked TODO, and there are comments right there giving more detailed instructions.

Note that these comments are not complete or accurate yet. I have only just started implementing my own question type, and I will be filling in those comments and finishing this document as I go along.--Tim Hunt 16:29, 25 August 2006 (CDT)

When you have finished

Consider checking your question type into the contrib area of the Moodle CVS server (if you have access) so that other people can share it.

Add your new question type to the modules and plugins database.