Question Format
A Qformat plugin in Moodle allows users to import and export quiz questions in various file formats. Developing a qformat plugin enables you to support custom formats beyond the default ones like Moodle XML or GIFT.
The most important aspects of developing a qformat plugin involve handling the parsing of external data during imports, and generating structured data during exports.
Key Aspects of Developing a Qformat Plugin
-
Plugin Structure and Location Qformat plugins reside in the
/question/format/
directory of Moodle. When developing a new plugin (let’s call itmyformat
), you will create the following directory:/question/format/myformat/
Essential files include:
- format.php: Core logic for import/export functionality.
- lang/en/qformat_myformat.php: Language strings.
- db/install.xml (optional): For any database-related changes.
-
format.php
: Core Plugin FileThis is where the majority of the import and export logic resides. Your plugin will extend either the
qformat_default
class (for text-based formats like GIFT or Moodle XML) orqformat_base
if you're developing a completely new format. Here, you define methods to read questions from a file and write questions to a file.Key methods: - readquestions: Parses the incoming data (e.g., from a file) and converts it into an array of Moodle question objects. - writequestions: Exports Moodle question objects into your desired format.
Example Skeleton for
format.php
:class qformat_myformat extends qformat_default { public function readquestions($lines) { $questions = array(); // Custom logic for parsing the input data into Moodle question objects. return $questions; } public function writequestions($questions) { $output = ''; foreach ($questions as $question) { // Custom logic for converting question objects into the export format. $output .= $this->format_question($question); } return $output; } // Optional: You can define other helper functions specific to your format. }
-
Importing Questions
The readquestions() method is responsible for parsing the imported file (like a CSV or JSON file) and converting its data into Moodle's internal question object format. The main challenge here is accurately mapping the external file structure to Moodle’s question structure. You may need to validate the file format, handle encoding issues, and check for missing fields.
- Ensure file parsing is robust: Invalid or missing data should be handled gracefully (e.g., by reporting an error to the user).
- Use Moodle’s built-in question classes (like
question
) and fill in the required fields based on the parsed data.
-
Exporting Questions
The writequestions() method takes an array of Moodle question objects and converts them into the desired file format (e.g., JSON, CSV, XML). You’ll need to create the correct structure to meet the requirements of the target format.
- Make sure to include all relevant question details like question text, answer choices, feedback, and grades.
- Handle special characters and encoding issues to ensure the exported file is valid.
-
Handling File Types
Depending on your file format, Moodle can support different MIME types for import/export. The
format.php
file may need to define how to handle specific file extensions and types.public function mime_type() { return 'application/myformat'; // Define your specific format's MIME type. }
-
Error Handling and Validation
It’s important to implement thorough error handling during both the import and export processes. Any invalid question format, unsupported data, or unexpected characters should be handled gracefully, with user-friendly error messages.
- For import errors, you can use Moodle’s notification system to show errors and logs.
- Export should avoid generating broken files by validating all the questions before outputting the final file.
-
Language Strings
The
lang/en/qformat_myformat.php
file contains all the language strings necessary for your plugin’s interface. Ensure all error messages, titles, and settings are translatable.Example:
$string['pluginname'] = 'MyFormat question format'; $string['exporterror'] = 'Error exporting questions in MyFormat'; $string['importerror'] = 'Error importing questions in MyFormat';
-
Backup and Restore Considerations
When developing a qformat plugin, you should consider how the import/export process interacts with Moodle's backup and restore mechanisms. Questions exported or imported via your plugin should integrate seamlessly with Moodle’s backup format.
- Ensure your format correctly handles metadata like question categories, feedback, and version history, which may be relevant during backup/restore.
-
Testing
Properly test your qformat plugin across various scenarios:
- Different question types: Ensure that your plugin supports importing and exporting multiple question types like multiple choice, true/false, short answer, etc.
- Edge cases: Handle questions with complex metadata (e.g., embedded media, equations).
- Error conditions: Verify that errors like malformed files, missing fields, or incorrect encodings are handled well.
Example of a Qformat Plugin: CSV-based Plugin
Let’s say you are developing a CSV-based qformat plugin that allows users to import/export quiz questions in CSV format.
-
In the
readquestions
method, you would parse the CSV file, split the fields, and map them to Moodle’s question structure:- First row: Question text
- Second row: Possible answers
- Third row: Correct answers
Example:
public function readquestions($lines) { $questions = array(); foreach ($lines as $line) { $data = str_getcsv($line); // Assuming $data contains question and answer details. $question = new stdClass(); $question->questiontext = $data[0]; $question->answer = $data[1]; // More logic here to fill in question details. $questions[] = $question; } return $questions; }
-
In the
writequestions
method, you would convert Moodle’s question objects into a CSV format:Example:
public function writequestions($questions) { $output = "Question,Answer\n"; foreach ($questions as $question) { $output .= $question->questiontext . "," . $question->answer . "\n"; } return $output; }
Conclusion
The most important aspects of developing a qformat plugin in Moodle involve handling the parsing of external data during import and generating properly structured data during export. The core logic resides in the format.php
file, where you define the logic for reading questions from an input file and writing questions to an output file.
By implementing robust error handling, ensuring seamless integration with Moodle’s question bank, and thoroughly testing across different question types, you can create a flexible and reliable qformat plugin.