From dc192b17b90a9f646ba2fb04d5878c5b8600609e Mon Sep 17 00:00:00 2001 From: = Date: Sat, 2 Jul 2016 01:09:42 +0200 Subject: [PATCH] Explode Rubric Blocks can be called from the outside. --- composer.json | 2 +- qtism/data/storage/xml/XmlCompactDocument.php | 91 +++++++++++-------- .../xml/XmlCompactAssessmentDocumentTest.php | 11 +++ 3 files changed, 64 insertions(+), 40 deletions(-) diff --git a/composer.json b/composer.json index ee971ccc2..bb00bf184 100644 --- a/composer.json +++ b/composer.json @@ -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.", diff --git a/qtism/data/storage/xml/XmlCompactDocument.php b/qtism/data/storage/xml/XmlCompactDocument.php index d3d75a044..b52e23c32 100644 --- a/qtism/data/storage/xml/XmlCompactDocument.php +++ b/qtism/data/storage/xml/XmlCompactDocument.php @@ -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; + } } diff --git a/test/qtism/data/storage/xml/XmlCompactAssessmentDocumentTest.php b/test/qtism/data/storage/xml/XmlCompactAssessmentDocumentTest.php index eadedffc2..5d77b9feb 100644 --- a/test/qtism/data/storage/xml/XmlCompactAssessmentDocumentTest.php +++ b/test/qtism/data/storage/xml/XmlCompactAssessmentDocumentTest.php @@ -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() {