Skip to content

Commit

Permalink
Explode Rubric Blocks can be called from the outside.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jul 1, 2016
1 parent c194eb0 commit dc192b1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "qtism/qtism",
"description": "OAT QTI Software Module Library",
"type": "library",
"version": "0.10.2",
"version": "0.10.3",
"authors": [
{
"name": "Open Assessment Technologies S.A.",
Expand Down
91 changes: 52 additions & 39 deletions qtism/data/storage/xml/XmlCompactDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,45 +311,58 @@ public function beforeSave(QtiComponent $documentComponent, $uri) {
// actual rubricBlocks in rubricBlockRefs.
if ($this->mustExplodeRubricBlocks() === true) {

// Get all rubricBlock elements...
$iterator = new QtiComponentIterator($documentComponent, array('rubricBlock'));
$sectionCount = new SplObjectStorage();

foreach ($iterator as $rubricBlock) {
// $section contains the assessmentSection the rubricBlock is related to.
$section = $iterator->parent();

// determine the occurence number of the rubricBlock relative to its section.
if (isset($sectionCount[$section]) === false) {
$sectionCount[$section] = 0;
}

$sectionCount[$section] = $sectionCount[$section] + 1;
$occurence = $sectionCount[$section];

// determine a suitable file name for the external rubricBlock definition.
$rubricBlockRefId = 'RB_' . $section->getIdentifier() . '_' . $occurence;
$href = './rubricBlock_' . $rubricBlockRefId . '.xml';

$doc = new XmlDocument();
$doc->setDocumentComponent($rubricBlock);

try {
$pathinfo = pathinfo($uri);
$doc->save($pathinfo['dirname'] . DIRECTORY_SEPARATOR . $href);

// replace the rubric block with a reference.
$sectionRubricBlocks = $section->getRubricBlocks();
$sectionRubricBlocks->remove($rubricBlock);

$sectionRubricBlockRefs = $section->getRubricBlockRefs();
$sectionRubricBlockRefs[] = new RubricBlockRef($rubricBlockRefId, $href);
}
catch (XmlStorageException $e) {
$msg = "An error occured while creating external rubrickBlock definition(s).";
throw new XmlStorageException($msg, $e);
}
}
foreach ($this->explodeRubricBlocks() as $href => $rubricBlock) {
try {
$doc = new XmlDocument();
$doc->setDocumentComponent($rubricBlock);

$pathinfo = pathinfo($uri);
$doc->save($pathinfo['dirname'] . DIRECTORY_SEPARATOR . $href);
} catch (XmlStorageException $e) {
$msg = "An error occured while creating external rubrickBlock definition(s).";
throw new XmlStorageException($msg, $e);
}
}
}
}

/**
* Explode Rubric Blocks into RubricBlockRefs.
*
* @return array where keys are RubricBlockRefs href and values are RubricBlocs as QtiComponent objets.
*/
public function explodeRubricBlocks() {
// Get all rubricBlock elements...
$iterator = new QtiComponentIterator($this->getDocumentComponent(), array('rubricBlock'));
$sectionCount = new SplObjectStorage();
$references = array();

foreach ($iterator as $rubricBlock) {
// $section contains the assessmentSection the rubricBlock is related to.
$section = $iterator->parent();

// determine the occurence number of the rubricBlock relative to its section.
if (isset($sectionCount[$section]) === false) {
$sectionCount[$section] = 0;
}

$sectionCount[$section] = $sectionCount[$section] + 1;
$occurence = $sectionCount[$section];

// determine a suitable file name for the external rubricBlock definition.
$rubricBlockRefId = 'RB_' . $section->getIdentifier() . '_' . $occurence;
$href = './rubricBlock_' . $rubricBlockRefId . '.xml';

// replace the rubric block with a reference.
$sectionRubricBlocks = $section->getRubricBlocks();
$sectionRubricBlocks->remove($rubricBlock);

$sectionRubricBlockRefs = $section->getRubricBlockRefs();
$sectionRubricBlockRefs[] = new RubricBlockRef($rubricBlockRefId, $href);

$references[$href] = $rubricBlock;
}

return $references;
}
}
11 changes: 11 additions & 0 deletions test/qtism/data/storage/xml/XmlCompactAssessmentDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ public function testExplodeRubricBlocks() {
$this->assertFalse(file_exists($path));

unlink($file);

// Check rubricBlockRefs.
$rubricBlockRefs = $doc->getDocumentComponent()->getComponentsByClassName('rubricBlockRef');
$this->assertEquals(3, count($rubricBlockRefs));

$this->assertEquals('./R01.xml', $rubricBlockRefs[0]->getHref());
$this->assertEquals('R01', $rubricBlockRefs[0]->getIdentifier());
$this->assertEquals('./rubricBlock_RB_S01_1.xml', $rubricBlockRefs[1]->getHref());
$this->assertEquals('RB_S01_1', $rubricBlockRefs[1]->getIdentifier());
$this->assertEquals('./rubricBlock_RB_S01_2.xml', $rubricBlockRefs[2]->getHref());
$this->assertEquals('RB_S01_2', $rubricBlockRefs[2]->getIdentifier());
}

public function testCreateFromAssessmentTestInvalidAssessmentItemRefResolution() {
Expand Down

0 comments on commit dc192b1

Please sign in to comment.