Note:

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

Question formats: Difference between revisions

From MoodleDocs
Line 30: Line 30:
==The format.php code flow for import==
==The format.php code flow for import==
When importing questions
When importing questions
* the import.php open the file
* the import.php let select the file and import format
* pass the control to '''format.php''' and  '''format/$format/format.php''' 
$format being the selected file format ( Blackboard, XML etc.)
        require("format.php");  // Parent class
        require("format/$format/format.php");
        $classname = "qformat_$format";
        $qformat = new $classname();
        if (! $qformat->importpreprocess($category,$course)) {  // Do anything before that we need to
            error( $txt->importerror ,
                      "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
        }
        if (! $qformat->importprocess($importfile, $params->matchgrades) ) {  // Process the uploaded file
            error( $txt->importerror ,
                  "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
        }
        if (! $qformat->importpostprocess()) {                  // In case anything needs to be done after


* the specific format.php
 
* '''format.php''' call '''format/$format/format.php''' (i.e. the the specific format.php)
to read the imput file and return the questions (if any) that are in the imported files
        if (! $questions = $this->readquestions($lines)) {  // Extract all the questions
            notify( get_string('noquestionsinfile','quiz') );
            return false;
        }
        notify( get_string('importingquestions','quiz',count($questions)) );
*
lines of
* the format.php clone in a certain way the '''default_questiontype->save_question()'''  
* the format.php clone in a certain way the '''default_questiontype->save_question()'''  
by saving the question common elements  
by saving the question common elements  

Revision as of 12:56, 14 June 2006

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. Similarlay 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.

  • typically
    • 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

The main difficulty in coding these specific format.php files is that the developpers should be aware of

  • the coding format of the import or export files (XML, QTI etc.)
  • the parameters needed for the various question types of Moodle whher new types could easily be introduced in the near future due to the reworking of the question moodle code.

Where to find the information to code for import/export format.php files.

As we are developping new questiontypes or new import functions from external files, we need to safeguard the default_questiontype->save_question() from importing bad question data. We must well document these question parameters and get stronger validation before saving a new question.

The most exhaustive developper documentations rely on the knowledge of the database tables, the tags definition and a good comprehension of the few comments inserted in the code.

Efforts have been made to document the database (i.e. Gustav Delius has work a lot on the question part) but as usual solving the bugs and developping new features took most of the energy.

For the description of the different question parameters you should look at the developper docs Quiz database structure and the quiz_questions table that have been renamed question when the question code has been replaced as independent from the quiz in moodle 1.6.

These docs illustrate well the difficulty to maintain an up-to-date, I had to correct it (at least I try...) for the modifications done by Gustav himself in the database .

The format.php code flow for import

When importing questions

  • the import.php let select the file and import format
  • pass the control to format.php and format/$format/format.php

$format being the selected file format ( Blackboard, XML etc.)

       require("format.php");  // Parent class
       require("format/$format/format.php");
       $classname = "qformat_$format";
       $qformat = new $classname();
       if (! $qformat->importpreprocess($category,$course)) {  // Do anything before that we need to
           error( $txt->importerror ,
                     "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
       }
       if (! $qformat->importprocess($importfile, $params->matchgrades) ) {  // Process the uploaded file
           error( $txt->importerror ,
                 "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
       }
       if (! $qformat->importpostprocess()) {                  // In case anything needs to be done after


  • format.php call format/$format/format.php (i.e. the the specific format.php)

to read the imput file and return the questions (if any) that are in the imported files

        if (! $questions = $this->readquestions($lines)) {   // Extract all the questions
           notify( get_string('noquestionsinfile','quiz') );
           return false;
       }
       notify( get_string('importingquestions','quiz',count($questions)) );

lines of

  • the format.php clone in a certain way the default_questiontype->save_question()

by saving the question common elements

  • afterwards it calls the specific qtype->save_question_options() function

which are the regular way to save these options that were retrieved from the imported file.


The 1.6 question table contains the id,category,parent,name,questiontext,questiontextformat,image,defaultgrade,penalty,qtype,length,stamp,version,hidden as the old quiz_questions table with the following modifications.

It is difficult to types that can be data structure of and each question type is expected to implement supply at least Importing or Importing and exporting have in common the data structure of questions in moodle but different requirements about the import or export modules unless the import or export is related to the same standard (ex.QTI). The moodle data structure should be avalable elsewhere.

See also

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