Note:

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

Question formats

From MoodleDocs
Revision as of 06:06, 30 January 2012 by Martin Dougiamas (talk | contribs) (moved Question import/export formats to Question formats)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Importing or exporting questions

Questions coming from other Moodle courses or from other e-learning systems (i.e. Blackboard, Webct etc.) can be imported in Moodle. Similarly, Moodle offers a process to export questions to other systems in different format ( XML, QTI etc.). The import and export code is mostly written in format.php files located in a directory specific to the type of format files from which the import or export is done.

  • examples:
    • question/format/aiken/format.php
    • question/format/blackboard/format.php
    • question/format/qti2/format.php
    • question/format/xml/format.php

there is a default class qformat_default defined in question/format.php and the process is controlled by

  • question/import.php
  • question/export.php

Import/Export formats are a pluggable resource and will automatically be "discovered" and made available to users simply by adding the plugin to question/format. Cross-support for optional questiontype plugins was added in 1.9 and support for language files (specifically a help file) has been implemented in Moodle 2.0.

Developing an import/export plugin

The simplest and most comprehensive import/export type is probably the Moodle XML format. There are also a few option plugins in contrib (plugins/question/format) that you can look at. It is worth studying these file (question/format/xml/format.php) along with the parent class (question/format.php) to get an understanding of what is going on. Some other custom formats circumvent the normal rules and structure and should be studied with caution.

Some things to note:

  • Study the question/format.php file to establish which methods must be overridden.
  • Each format.php file can support import, export or both indicated by the provide_import() and provide_export() methods which must be overridden to return true as required.
  • Do not read or write directly to the database, this is handled for you by the parent class
  • when importing, question objects need to be in a format suitable for use by the questiontype->save_options() method (see question/type/questiontype.php class). The easiest way to get this information is to do a print_r($question); at the start of the save_options() method for the appropriate question type when a question is saved. The imported object must be the same format.
  • when importing, always obtain 'empty' question objects from the defaultquestion() method, which sets all required properties to their default values.
  • for exporting the populated $question object is supplied to the format class. Again, print_r($question) can be used to learn the format.
  • You should add a help file for the plugin using the structure lang/en_utf8/help/myformat/myformat.html where myformat is the name of your import/export plugin (Moodle 2.0).

See also

Lesson question types - both Lesson and Quiz can import the basic Moodle question types.