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
(Removing as it's out of date and (as a dump of some of the code) I think it's more likely to cause confusion than help.)
Line 23: Line 23:
* for exporting the $question object is supplied to the format class. Again, '''print_r($question)''' can be used to learn the format.
* for exporting the $question object is supplied to the format class. Again, '''print_r($question)''' can be used to learn the format.


==The '''question/format.php''' code flow for import==
When importing questions
*the '''import.php''' selects 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)) );
::*get list of valid answer grades
        $grades = get_grade_options();
        $gradeoptionsfull = $grades->gradeoptionsfull;
::*Process some data and store each question
:::*check for answer grades validity (must match fixed list of grades)
:::*store question general parameters '''DIRECTLY''' in the dadabase
            $question->category = $this->category->id;
            $question->stamp = make_unique_id_code();  // Set the unique code (not to be changed)
            if (!$question->id = '''insert_record'''("question", $question)) {
                error( get_string('cannotinsert','quiz') );
            }
            $this->questionids[] = $question->id;
:::*save the options using the '''qtype->save_question_options()''' function
::::$QTYPES being the different questiontypes (shortanwer,truefalse etc.)
            global $QTYPES;
            $result = '''$QTYPES[$question->qtype]'''
                    '''->save_question_options'''($question);
:::*diplay errors or notices
            if (!empty($result->error)) {
                notify($result->error);
                return false;
            }
            if (!empty($result->notice)) {
                notify($result->notice);
                return true;
            }
:::*if noerrors and no notices
::::Give the question a unique version stamp determined by question_hash()
            set_field('question', 'version', question_hash($question), 'id', $question->id);
::Process the next question until the last question in the file
:Return to the question or quiz editing.


== See also ==
== See also ==

Revision as of 08:22, 11 July 2008

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

The simplest and most comprehensive import/export type is probably the Moodle XML format. It is worth studying this 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
  • 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, obtain 'empty' question objects from the defaultquestion() method, which sets all required properties to their default values.
  • for exporting the $question object is supplied to the format class. Again, print_r($question) can be used to learn the format.


See also

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