Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

Code for exporting questions as Moodle XML.

Copyright: 1999 onwards Martin Dougiamas {@link http://moodle.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1681 lines (71 kb)
Included or required:0 times
Referenced: 3 times
Includes or requires: 0 files

Defines 1 class


Class: qformat_xml  - X-Ref

Importer for Moodle XML question format.

See http://docs.moodle.org/en/Moodle_XML_format for a description of the format.

provide_import()   X-Ref
No description

provide_export()   X-Ref
No description

mime_type()   X-Ref
No description

validate_file(stored_file $file)   X-Ref
Validate the given file.

For more expensive or detailed integrity checks.

return: string the error message that occurred while validating the given file
param: stored_file $file the file to check

trans_format($name)   X-Ref
Translate human readable format name
into internal Moodle code number
Note the reverse function is called get_format.

return: int Moodle format code
param: string name format name from xml file

trans_single($name)   X-Ref
Translate human readable single answer option
to internal code number

return: int internal code number
param: string name true/false

import_text($text)   X-Ref
process text string from xml file

return: string processed text.
param: array $text bit of xml tree after ['text']

getpath($xml, $path, $default, $istext=false, $error='')   X-Ref
return the value of a node, given a path to the node
if it doesn't exist return the default value

return: mixed value
param: array xml data to read
param: array path path to node expressed as array
param: mixed default
param: bool istext process as text
param: string error if set value must exist, return false and issue message if not

import_text_with_files($data, $path, $defaultvalue = '', $defaultformat = 'html')   X-Ref
No description

import_files_as_draft($xml)   X-Ref
No description

import_headers($question)   X-Ref
import parts of question common to all types

return: object question object
param: $question array question question array from xml tree

import_answer($answer, $withanswerfiles = false, $defaultformat = 'html')   X-Ref
Import the common parts of a single answer

return: object answer object
param: array answer xml tree for single answer
param: bool $withanswerfiles if true, the answers are HTML (or $defaultformat)
param: array Default text format for the feedback, and the answers if $withanswerfiles

import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false)   X-Ref
Import the common overall feedback fields.

param: object $question the part of the XML relating to this question.
param: object $qo the question data to add the fields to.
param: bool $withshownumpartscorrect include the shownumcorrect field.

import_hint($hintxml, $defaultformat)   X-Ref
Import a question hint

return: object hint for storing in the database.
param: array $hintxml hint xml fragment.
param: string $defaultformat the text format to assume for hints that do not specify.

import_hints($qo, $questionxml, $withparts = false,$withoptions = false, $defaultformat = 'html')   X-Ref
Import all the question hints

return: array of objects representing the hints in the file.
param: object $qo the question data that is being constructed.
param: array $questionxml The xml representing the question.
param: bool $withparts whether the extra fields relating to parts should be imported.
param: bool $withoptions whether the extra options field should be imported.
param: string $defaultformat the text format to assume for hints that do not specify.

import_question_tags($qo, $questionxml)   X-Ref
Import all the question tags

return: array of objects representing the tags in the file.
param: object $qo the question data that is being constructed.
param: array $questionxml The xml representing the question.

import_files($xml)   X-Ref
Import files from a node in the XML.

return: array of things representing files - in the form that save_question expects.
param: array $xml an array of <file> nodes from the the parsed XML.

import_multichoice($question)   X-Ref
import multiple choice question

return: object question object
param: array question question array from xml tree

import_multianswer($question)   X-Ref
Import cloze type question

return: object question object
param: array question question array from xml tree

import_truefalse($question)   X-Ref
Import true/false type question

return: object question object
param: array question question array from xml tree

import_shortanswer($question)   X-Ref
Import short answer type question

return: object question object
param: array question question array from xml tree

import_description($question)   X-Ref
Import description type question

return: object question object
param: array question question array from xml tree

import_numerical($question)   X-Ref
Import numerical type question

return: object question object
param: array question question array from xml tree

import_match($question)   X-Ref
Import matching type question

return: object question object
param: array question question array from xml tree

import_essay($question)   X-Ref
Import essay type question

return: object question object
param: array question question array from xml tree

import_calculated($question)   X-Ref
Import a calculated question

param: object $question the imported XML data.

import_category($question)   X-Ref
This is not a real question type. It's a dummy type used to specify the
import category. The format is:
<question type="category">
<category>tom/dick/harry</category>
<info format="moodle_auto_format"><text>Category description</text></info>
</question>


readquestions($lines)   X-Ref
Parse the array of lines into an array of questions
this *could* burn memory - but it won't happen that much
so fingers crossed!

return: array (of objects) question objects.
param: array of lines from the input file.
param: stdClass $context

import_questions($xml)   X-Ref

return: stdClass[] question objects to pass to question type save_question_options
param: array $xml the xmlized xml

import_question($questionxml)   X-Ref

return: null|stdClass an object with data to be fed to question type save_question_options
param: array $questionxml xml describing the question

export_file_extension()   X-Ref
No description

get_qtype($qtype)   X-Ref
Turn the internal question type name into a human readable form.
(In the past, the code used to use integers internally. Now, it uses
strings, so there is less need for this, but to maintain
backwards-compatibility we change two of the type names.)

return: string $qtype string to use in the file.
param: string $qtype question type plugin name.

get_format($id)   X-Ref
Convert internal Moodle text format code into
human readable form

return: string format text
param: int id internal code

get_single($id)   X-Ref
Convert internal single question code into
human readable form

return: string single question string
param: int id single question code

xml_escape($string)   X-Ref
Take a string, and wrap it in a CDATA secion, if that is required to make
the output XML valid.

return: string the string, wrapped in CDATA if necessary.
param: string $string a string

writetext($raw, $indent = 0, $short = true)   X-Ref
Generates <text></text> tags, processing raw text therein

return: string formatted text.
param: string $raw the content to output.
param: int $indent the current indent level.
param: bool $short stick it on one line.

write_files($files)   X-Ref
Generte the XML to represent some files.

return: string $string the XML.
param: array of store array of stored_file objects.

presave_process($content)   X-Ref
No description

writequestion($question)   X-Ref
Turns question into an xml segment

return: string xml segment
param: object $question the question data.

write_answers($answers)   X-Ref
No description

write_answer($answer, $extra = '')   X-Ref
No description

write_hints($question)   X-Ref
Write out the hints.

return: string XML to output.
param: object $question the question definition data.

format($format)   X-Ref

return: string the attribute to add to an XML tag.
param: int $format a FORMAT_... constant.

write_hint($hint, $contextid)   X-Ref
No description

write_combined_feedback($questionoptions, $questionid, $contextid)   X-Ref
Output the combined feedback fields.

return: string XML to output.
param: object $questionoptions the question definition data.
param: int $questionid the question id.
param: int $contextid the question context id.