Skip to content
Jérôme Bogaerts edited this page Apr 29, 2019 · 10 revisions

Data Model

The QTI-SDK comes with a complete QTI Data Model. Each QTI class has its own representation as a PHP class in the QTI-SDK Data Model.

In QTI-SDK, any QTI class instance will be represented as a qtism\data\QtiComponent object. As an example, a QTI AssessmentItem class instance will be represented as a qtism\data\AssessmentItem object.

Loading and Saving QTI Content

QTI-SDK provides a very convenient mechanism to load QTI contents by using implementations of the qtism\data\QtiDocument class. In this example, we will use an implementation able to read QTI contents from XML, make some modifications, and save the changes.

The example below is loading the XML file "Composition of Water" from the IMS consortium.

use qtism\data\storage\xml\XmlDocument;
use qtism\data\storage\StorageException;

// The XmlDocument class can load any kind of QTI 2.0, 2.1 and 2.2 contents.
$qtiDoc = new XmlDocument();

try {
    // Load the QTI contents from file system.
    $qtiDoc->load('/path/to/choice_multiple.xml');

    // $rootComponent is an instance of AssessmentItem
    $rootComponent = $qtiDoc->getDocumentComponent();

    // Change the title of the test.
    $rootComponent->setTitle('My Composition of Water');

    // Persist the changes.
    $qtiDoc->save('/path/to/choice_multiple.xml');
} catch (StorageException $e) {
    // An error occured while loading or persisting data e.g. parsing issue, invalid XML, ...
}

Traversing QTI Content

QTI-SDK also enables you to traverse the loaded QTI contents. In the example below, we traverse the AssessmentItem children elements to find all QtiComponent objects representing QTI simpleChoice elements from the loaded XML file.

The example below is loading the XML file "Composition of Water" from the IMS consortium.

use qtism\data\storage\xml\XmlDocument;
use qtism\data\storage\StorageException;

$qtiDoc = new XmlDocument();

try {
    // Load the QTI contents from file system.
    $qtiDoc->load('/path/to/choice_multiple.xml');

    // $rootComponent is an instance of AssessmentItem.
    // It corresponds to the root element of the loaded QTI document.
    $rootComponent = $qtiDoc->getDocumentComponent();

    // Get all the objects representing QTI simpleChoice elements in the AssessmentItem, recursively.
    $simpleChoices = $rootComponent->getComponentsByClassName('simpleChoice', true);

    foreach ($simpleChoices as $simpleChoice) {
        echo "<". $qtiClassName . "> with identifier '" . $simpleChoice->getIdentifier() . "'\n";
    }
} catch (StorageException $e) {
    // An error occured while loading data e.g. parsing issue, invalid XML, ...
}

The script above will output

<simpleChoice> with identifier 'H'
<simpleChoice> with identifier 'He'
<simpleChoice> with identifier 'C'
<simpleChoice> with identifier 'O'
<simpleChoice> with identifier 'N'
<simpleChoice> with identifier 'Cl'