Note:

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

Import/export for questiontype plugins: Difference between revisions

From MoodleDocs
Line 58: Line 58:
     *
     *
     */
     */
     format plugin_import( $formatname, $formatversion, $name, $data ) {
     format plugin_import( $formatname, $formatversion, $name, $data, $question ) {
         global $QTYPES;
         global $QTYPES;
         // poll the questiontypes to see if this can be handled
         // poll the questiontypes to see if this can be handled
         foreach ($QTYPES as $qtype) {
         foreach ($QTYPES as $qtype) {
             if ($question = $qtype->import( format?, $name, $data )) {
             if ($question = $qtype->import( $formatname, $formatversion, $name, $data, $question, $this )) {
                 return $question;
                 return $question;
             }
             }
         }
         }
        return false;
     }
     }



Revision as of 07:36, 2 August 2007

This is a proposal currently in development

Motivation

While it is easy enough to add import and export capability for core questiontypes there is no clear way to allow contributed questiontype authors to enhance their plugin with import and export code. This is an outline proposal for including hooks for that functionality. It is primarily aimed at the Moodle XML format (the most comprehensive import/export format) but with a definite goal not to exclude any other format that may wish to use it.

New Methods in Questiontype Class

The main mechanism for this process will be new classes for import and export to be added to the questiontype class. These should be overridden by question plugs that wish the support import and/or export. Note that these will not (and should not) be implemented for core question types.

Import

   /**
    * Override to provide import functionality for plugin questions (only)
    * @param formatname string the name of the import format making request
    * @param formatversion string any additional or version information or empty
    * @param name string the name of the question type (in the import file)
    * @param data mixed the segment of data containing the question
    * @param question object question object processed (so far) by standard import code
    * @param format object the format object so that helper methods can be used (in particular error() )
    * @return object question object suitable for save_options() call or false if cannot handle
    */
   function import( $formatname, $formatversion, $name, $data, $question, $format ) {
       return false;
   } 

Notes:

  • The method provides a single entry point for all formats
  • The name is supplied as it may differ from the questiontype name - more on this...
  • The version is a string indicating the version of the import file if required (Blackboard 5 or 6 for example), otherwise empty
  • If the plugin provides import functionality for multiple formats it is responsible for "switching" on the $format parameter as required.
  • The $question parameter will contain whatever the core routines have managed to work out so far
  • The $format object is the calling object in case there are any useful methods. In particular this method should call $format->error() to report errors properly

Export

   /**
    * Override to provide export functionality for plugin questions (only)
    * @param formatname string the name of the import format making request
    * @param formatversion string version information for the format or empty
    * @param question object the question object 
    * @return string the data to append to the output buffer
    */
   function export( $formatname, $formatversion, $question, $format ) {
       return false;
   }

Notes:

  • The method provides a single entry point for all formats

New methods is qformat_default class (format.php)

Two new methods (one for import and one for export) will be provided for the format plugin to call to handle situations that they cannot handle themselves.

Import

   /*
    *
    */
   format plugin_import( $formatname, $formatversion, $name, $data, $question ) {
       global $QTYPES;
       // poll the questiontypes to see if this can be handled
       foreach ($QTYPES as $qtype) {
           if ($question = $qtype->import( $formatname, $formatversion, $name, $data, $question, $this )) {
               return $question;
           }
       }
       return false;
   }

Question name resolution

One issue is that the name of the (type of a) question may differ, indeed is likely to differ from the name of the Moodle questiontype plugin. This makes it problematic knowing which plugin to call to potentially handle the question. My solution is to "poll" the questiontypes one-by-one to see if any will handle the question. The first that returns a response other than false will be accepted.

Error handling

The recommended way for import/export formats to report a syntax error is to call the error() method within the format class. This will no longer be straightforward from the questiontype class. Need to consider a way to do this.

See Also