Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
qformat_default Class Reference

Base class for question import and export formats. More...

Inheritance diagram for qformat_default:
qformat_aiken qformat_based_on_xml qformat_based_on_xml qformat_gift qformat_missingword qformat_multianswer qformat_xml qformat_blackboard_six_base qformat_blackboard_six_base qformat_blackboard_six qformat_blackboard_six_pool qformat_blackboard_six_qti qformat_blackboard_six qformat_blackboard_six_pool qformat_blackboard_six_qti

Public Member Functions

 can_import_file ($file)
 Check if the given file is capable of being imported by this plugin.
 
 clean_question_name ($name)
 Ensure that a question name does not contain anything nasty, and will fit in the DB field.
 
 clean_question_name ($name)
 Ensure that a question name does not contain anything nasty, and will fit in the DB field.
 
 create_default_question_name ($questiontext, $default)
 Construct a reasonable default question name, based on the start of the question text.
 
 create_default_question_name ($questiontext, $default)
 Construct a reasonable default question name, based on the start of the question text.
 
 export_file_extension ()
 
 exportpreprocess ()
 Do any pre-processing that may be required.
 
 exportprocess ($checkcapabilities=true)
 Perform the export.
 
 importpostprocess ()
 
 importpostprocess ()
 Override if any post-processing is required.
 
 importpreprocess ()
 
 importpreprocess ()
 Perform any required pre-processing.
 
 importprocess ($filename, $lesson, $pageid)
 
 importprocess ()
 Process the file This method should not normally be overidden.
 
 mime_type ()
 The string mime-type of the files that this plugin reads or writes.
 
 provide_export ()
 
 provide_import ()
 
 provide_import ()
 
 readdata ($filename)
 
 set_can_access_backupdata ($canaccess)
 
 set_display_progress ($displayprogress)
 Change whether to display progress messages.
 
 set_importcontext ($context)
 
 setCategory ($category)
 set the category
 
 setCatfromfile ($catfromfile)
 set catfromfile
 
 setCattofile ($cattofile)
 set cattofile
 
 setContextfromfile ($contextfromfile)
 set contextfromfile
 
 setContexts ($contexts)
 set an array of contexts.
 
 setContexttofile ($contexttofile)
 set contexttofile
 
 setCourse ($course)
 set the course class variable
 
 setFilename ($filename)
 set the filename
 
 setMatchgrades ($matchgrades)
 set matchgrades
 
 setQuestions ($questions)
 Set the specific questions to export.
 
 setRealfilename ($realfilename)
 set the "real" filename (this is what the user typed, regardless of wha happened next)
 
 setStoponerror ($stoponerror)
 set stoponerror
 
 try_importing_using_qtypes ($data, $question=null, $extra=null, $qtypehint='')
 Import for questiontype plugins.
 
 try_importing_using_qtypes ($data, $question=null, $extra=null, $qtypehint='')
 Import for questiontype plugins Do not override.
 
 validate_file (stored_file $file)
 Validate the given file.
 

Public Attributes

 $canaccessbackupdata = true
 
 $category = null
 
 $catfromfile = 0
 
 $cattofile = 0
 
 $contextfromfile = 0
 
context[] $contexts
 
 $contexttofile = 0
 
 $course = null
 
 $displayerrors = true
 
bool $displayprogress = true
 $displayprogress Whether to display progress.
 
 $filename = ''
 
 $importerrors = 0
 
 $matchgrades = 'error'
 
 $qtypeconvert
 
 $questionids = array()
 
 $questions = array()
 
 $realfilename = ''
 
 $stoponerror = true
 
 $translator = null
 

Protected Member Functions

 add_blank_combined_feedback ($question)
 Since the lesson module tries to re-use the question bank import classes in a crazy way, this is necessary to stop things breaking.
 
 add_blank_combined_feedback ($question)
 Add a blank combined feedback to a question object.
 
 assemble_category_path ($names)
 Convert a list of category names, possibly preceeded by one of the context tokens like $course$, into a string representation of the category path.
 
 count_questions ($questions)
 Count all non-category questions in the questions array.
 
 count_questions ($questions)
 Count all non-category questions in the questions array.
 
 create_category_path ($catpath, $lastcategoryinfo=null)
 find and/or create the category described by a delimited list e.g.
 
 create_dummy_question_representing_category (string $categoryname, $categoryinfo)
 Create 'dummy' question for category export.
 
 defaultquestion ()
 return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields.
 
 defaultquestion ()
 return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields.
 
 error ($message, $text='', $questionname='')
 Handle parsing error.
 
 error ($message, $text='', $questionname='')
 Handle parsing error.
 
 exportpostprocess ()
 Do an post-processing that may be required.
 
 format_question_text ($question)
 Convert the question text to plain text, so it can safely be displayed during import to let the user see roughly what is going on.
 
 format_question_text ($question)
 Convert the question text to plain text, so it can safely be displayed during import to let the user see roughly what is going on.
 
 get_category_path ($id, $includecontext=true)
 get the category as a path (e.g., tom/dick/harry)
 
 get_name ()
 Return the localized pluginname string for the question format.
 
 presave_process ($content)
 Enable any processing to be done on the content just prior to the file being saved default is to do nothing.
 
 readdata ($filename)
 Return complete file within an array, one item per line.
 
 readquestion ($lines)
 
 readquestion ($lines)
 Given the data known to define a question in this format, this function converts it into a question object suitable for processing and insertion into Moodle.
 
 readquestions ($lines)
 
 readquestions ($lines)
 Parses an array of lines into an array of questions, where each item is a question object as defined by readquestion().
 
 split_category_path ($path)
 Convert a string, as returned by assemble_category_path(), back into an array of category names.
 
 try_exporting_using_qtypes ($name, $question, $extra=null)
 Provide export functionality for plugin questiontypes Do not override.
 
 validate_is_utf8_file (stored_file $file)
 Check if the given file has the required utf8 encoding.
 
 writequestion ($question)
 convert a single question object into text output in the given format.
 

Protected Attributes

 $importcontext = null
 

Detailed Description

Base class for question import and export formats.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Member Function Documentation

◆ add_blank_combined_feedback()

qformat_default::add_blank_combined_feedback ( $question)
protected

Add a blank combined feedback to a question object.

Parameters
objectquestion
Return values
objectquestion

◆ assemble_category_path()

qformat_default::assemble_category_path ( $names)
protected

Convert a list of category names, possibly preceeded by one of the context tokens like $course$, into a string representation of the category path.

Names are separated by / delimiters. And /s in the name are replaced by //.

To reverse the process and split the paths into names, use split_category_path().

Parameters
array$names
Return values
string

◆ can_import_file()

qformat_default::can_import_file ( $file)

Check if the given file is capable of being imported by this plugin.

Note that expensive or detailed integrity checks on the file should not be performed by this method. Simple file type or magic-number tests would be suitable.

Parameters
stored_file$filethe file to check
Return values
boolwhether this plugin can import the file

Reimplemented in qformat_blackboard_six_base.

◆ clean_question_name() [1/2]

qformat_default::clean_question_name ( $name)

Ensure that a question name does not contain anything nasty, and will fit in the DB field.

Parameters
string$namethe raw question name.
Return values
stringa safe question name.

◆ clean_question_name() [2/2]

qformat_default::clean_question_name ( $name)

Ensure that a question name does not contain anything nasty, and will fit in the DB field.

Parameters
string$namethe raw question name.
Return values
stringa safe question name.

◆ count_questions() [1/2]

qformat_default::count_questions ( $questions)
protected

Count all non-category questions in the questions array.

Parameters
arrayquestions An array of question objects.
Return values
intThe count.

◆ count_questions() [2/2]

qformat_default::count_questions ( $questions)
protected

Count all non-category questions in the questions array.

Parameters
arrayquestions An array of question objects.
Return values
intThe count.

◆ create_category_path()

qformat_default::create_category_path ( $catpath,
$lastcategoryinfo = null )
protected

find and/or create the category described by a delimited list e.g.

$course$/tom/dick/harry or tom/dick/harry

removes any context string no matter whether $getcontext is set but if $getcontext is set then ignore the context and use selected category context.

Parameters
stringcatpath delimited category path
object$lastcategoryinfoContains category information
Return values
mixedcategory object or null if fails

◆ create_default_question_name() [1/2]

qformat_default::create_default_question_name ( $questiontext,
$default )

Construct a reasonable default question name, based on the start of the question text.

Parameters
string$questiontextthe question text.
string$defaultdefault question name to use if the constructed one comes out blank.
Return values
stringa reasonable question name.

◆ create_default_question_name() [2/2]

qformat_default::create_default_question_name ( $questiontext,
$default )

Construct a reasonable default question name, based on the start of the question text.

Parameters
string$questiontextthe question text.
string$defaultdefault question name to use if the constructed one comes out blank.
Return values
stringa reasonable question name.

◆ create_dummy_question_representing_category()

qformat_default::create_dummy_question_representing_category ( string $categoryname,
$categoryinfo )
protected

Create 'dummy' question for category export.

Parameters
string$categorynamethe name of the category
object$categoryinfodescription of the category
Return values
stdClass'dummy' question for category

◆ defaultquestion() [1/2]

qformat_default::defaultquestion ( )
protected

return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields.

This should not be overridden.

Return values
objectdefault question

◆ defaultquestion() [2/2]

qformat_default::defaultquestion ( )
protected

return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields.

This should not be overridden.

Return values
objectdefault question

◆ error()

qformat_default::error ( $message,
$text = '',
$questionname = '' )
protected

Handle parsing error.

Parameters
string$messageinformation about error
string$textimported text that triggered the error
string$questionnameimported question name

◆ export_file_extension()

qformat_default::export_file_extension ( )
Return values
stringthe file extension (including .) that is normally used for files handled by this plugin.

Reimplemented in qformat_gift, and qformat_xml.

◆ exportpostprocess()

qformat_default::exportpostprocess ( )
protected

Do an post-processing that may be required.

Return values
boolsuccess

◆ exportpreprocess()

qformat_default::exportpreprocess ( )

Do any pre-processing that may be required.

Parameters
boolsuccess

Reimplemented in qformat_aiken.

◆ exportprocess()

qformat_default::exportprocess ( $checkcapabilities = true)

Perform the export.

For most types this should not need to be overrided.

Parameters
bool$checkcapabilitiesWhether to check capabilities when exporting the questions.
Return values
stringThe content of the export.

◆ get_category_path()

qformat_default::get_category_path ( $id,
$includecontext = true )
protected

get the category as a path (e.g., tom/dick/harry)

Parameters
intid the id of the most nested catgory
Return values
stringthe path

◆ get_name()

qformat_default::get_name ( )
protected

Return the localized pluginname string for the question format.

Return values
stringthe pluginname string for the question format

◆ importpostprocess() [1/2]

qformat_default::importpostprocess ( )

Reimplemented in qformat_blackboard_six_base.

◆ importpostprocess() [2/2]

qformat_default::importpostprocess ( )

Override if any post-processing is required.

Return values
boolsuccess

Reimplemented in qformat_blackboard_six_base.

◆ importpreprocess()

qformat_default::importpreprocess ( )

Perform any required pre-processing.

Return values
boolsuccess

◆ importprocess()

qformat_default::importprocess ( )

Process the file This method should not normally be overidden.

Return values
boolsuccess

◆ mime_type()

qformat_default::mime_type ( )

The string mime-type of the files that this plugin reads or writes.

Reimplemented in qformat_blackboard_six_base, and qformat_xml.

◆ presave_process()

qformat_default::presave_process ( $content)
protected

Enable any processing to be done on the content just prior to the file being saved default is to do nothing.

Parameters
stringoutput text
stringprocessed output text

Reimplemented in qformat_xml.

◆ provide_export()

qformat_default::provide_export ( )
Return values
boolwhether this plugin provides export functionality.

Reimplemented in qformat_aiken, qformat_gift, and qformat_xml.

◆ provide_import() [1/2]

qformat_default::provide_import ( )

Reimplemented in qformat_blackboard_six_base.

◆ provide_import() [2/2]

qformat_default::provide_import ( )
Return values
boolwhether this plugin provides import functionality.

Reimplemented in qformat_blackboard_six_base.

◆ readdata() [1/2]

qformat_default::readdata ( $filename)

Reimplemented in qformat_blackboard_six.

◆ readdata() [2/2]

qformat_default::readdata ( $filename)
protected

Return complete file within an array, one item per line.

Parameters
stringfilename name of file
Return values
mixedcontents array or false on failure

Reimplemented in qformat_blackboard_six.

◆ readquestion()

qformat_default::readquestion ( $lines)
protected

Given the data known to define a question in this format, this function converts it into a question object suitable for processing and insertion into Moodle.

If your format does not use blank lines to delimit questions (e.g. an XML format) you must override 'readquestions' too

Parameters
$linesmixed data that represents question
Return values
objectquestion object

◆ readquestions() [1/2]

qformat_default::readquestions ( $lines)
protected

◆ readquestions() [2/2]

qformat_default::readquestions ( $lines)
protected

Parses an array of lines into an array of questions, where each item is a question object as defined by readquestion().

Questions are defined as anything between blank lines.

NOTE this method used to take $context as a second argument. However, at the point where this method was called, it was impossible to know what context the quetsions were going to be saved into, so the value could be wrong. Also, none of the standard question formats were using this argument, so it was removed. See MDL-32220.

If your format does not use blank lines as a delimiter then you will need to override this method. Even then try to use readquestion for each question

Parameters
arraylines array of lines from readdata
Return values
arrayarray of question objects

Reimplemented in qformat_blackboard_six, qformat_blackboard_six_pool, qformat_blackboard_six_qti, and qformat_xml.

◆ set_can_access_backupdata()

qformat_default::set_can_access_backupdata ( $canaccess)
Parameters
bool$canaccessWhether the current use can access the backup data folder. Determines where export files are saved.

◆ set_display_progress()

qformat_default::set_display_progress ( $displayprogress)

Change whether to display progress messages.

There is normally no need to use this function as the default for $displayprogress is true. Set to false for unit tests.

Parameters
bool$displayprogress

◆ setCategory()

qformat_default::setCategory ( $category)

set the category

Parameters
objectcategory the category object

◆ setCatfromfile()

qformat_default::setCatfromfile ( $catfromfile)

set catfromfile

Parameters
boolcatfromfile allow categories embedded in import file

◆ setCattofile()

qformat_default::setCattofile ( $cattofile)

set cattofile

Parameters
boolcattofile exports categories within export file

◆ setContextfromfile()

qformat_default::setContextfromfile ( $contextfromfile)

set contextfromfile

Parameters
bool$contextfromfileallow contexts embedded in import file

◆ setContexts()

qformat_default::setContexts ( $contexts)

set an array of contexts.

Parameters
context[]$contexts

◆ setContexttofile()

qformat_default::setContexttofile ( $contexttofile)

set contexttofile

Parameters
boolcattofile exports categories within export file

◆ setCourse()

qformat_default::setCourse ( $course)

set the course class variable

Parameters
courseobject Moodle course variable

◆ setFilename()

qformat_default::setFilename ( $filename)

set the filename

Parameters
stringfilename name of file to import/export

◆ setMatchgrades()

qformat_default::setMatchgrades ( $matchgrades)

set matchgrades

Parameters
stringmatchgrades error or nearest for grades

◆ setQuestions()

qformat_default::setQuestions ( $questions)

Set the specific questions to export.

Should not include questions with parents (sub questions of cloze question type). Only used for question export.

Parameters
arrayof question objects

◆ setRealfilename()

qformat_default::setRealfilename ( $realfilename)

set the "real" filename (this is what the user typed, regardless of wha happened next)

Parameters
stringrealfilename name of file as typed by user

◆ setStoponerror()

qformat_default::setStoponerror ( $stoponerror)

set stoponerror

Parameters
boolstoponerror stops database write if any errors reported

◆ split_category_path()

qformat_default::split_category_path ( $path)
protected

Convert a string, as returned by assemble_category_path(), back into an array of category names.

Each category name is cleaned by a call to clean_param(, PARAM_TEXT), which matches the cleaning in question/bank/managecategories/category_form.php.

Parameters
string$path
Return values
arrayof category names.

◆ try_exporting_using_qtypes()

qformat_default::try_exporting_using_qtypes ( $name,
$question,
$extra = null )
protected

Provide export functionality for plugin questiontypes Do not override.

Parameters
namequestiontype name
questionobject data to export
extramixed any addition format specific data needed
Return values
stringthe data to append to export or false if error (or unhandled)

◆ try_importing_using_qtypes() [1/2]

qformat_default::try_importing_using_qtypes ( $data,
$question = null,
$extra = null,
$qtypehint = '' )

Import for questiontype plugins.

Parameters
mixed$dataThe segment of data containing the question
object$questionprocessed (so far) by standard import code if appropriate
object$extramixed any additional format specific data that may be passed by the format
string$qtypehinthint about a question type from format
Return values
objectquestion object suitable for save_options() or false if cannot handle

◆ try_importing_using_qtypes() [2/2]

qformat_default::try_importing_using_qtypes ( $data,
$question = null,
$extra = null,
$qtypehint = '' )

Import for questiontype plugins Do not override.

Parameters
datamixed The segment of data containing the question
questionobject processed (so far) by standard import code if appropriate
extramixed any additional format specific data that may be passed by the format
qtypehinthint about a question type from format
Return values
objectquestion object suitable for save_options() or false if cannot handle

◆ validate_file()

qformat_default::validate_file ( stored_file $file)

Validate the given file.

For more expensive or detailed integrity checks.

Parameters
stored_file$filethe file to check
Return values
stringthe error message that occurred while validating the given file

Reimplemented in qformat_aiken, qformat_gift, qformat_missingword, qformat_multianswer, and qformat_xml.

◆ validate_is_utf8_file()

qformat_default::validate_is_utf8_file ( stored_file $file)
protected

Check if the given file has the required utf8 encoding.

Parameters
stored_file$filethe file to check
Return values
stringthe error message if the file encoding is not UTF-8

◆ writequestion()

qformat_default::writequestion ( $question)
protected

convert a single question object into text output in the given format.

This must be overriden

Parameters
objectquestion question object
Return values
mixedquestion export text or null if not implemented

Reimplemented in qformat_aiken, qformat_gift, and qformat_xml.

Member Data Documentation

◆ $qtypeconvert

qformat_default::$qtypeconvert
Initial value:
= array('numerical' => LESSON_PAGE_NUMERICAL,
'multichoice' => LESSON_PAGE_MULTICHOICE,
'truefalse' => LESSON_PAGE_TRUEFALSE,
'shortanswer' => LESSON_PAGE_SHORTANSWER,
'essay' => LESSON_PAGE_ESSAY
)
const LESSON_PAGE_SHORTANSWER
Short answer question type.
Definition shortanswer.php:29
const LESSON_PAGE_NUMERICAL
Numerical question type.
Definition numerical.php:29
const LESSON_PAGE_MULTICHOICE
Multichoice question type.
Definition multichoice.php:29
const LESSON_PAGE_ESSAY
Essay question type.
Definition essay.php:29
const LESSON_PAGE_TRUEFALSE
True/False question type.
Definition truefalse.php:29
const LESSON_PAGE_MATCHING
Matching question type.
Definition matching.php:29

The documentation for this class was generated from the following files: