From f521f105ca8a9624300231f5cccb6cf6ae2e83dc Mon Sep 17 00:00:00 2001 From: bugalot Date: Fri, 22 Sep 2023 15:31:35 +0200 Subject: [PATCH] feat: implement branchRules at testPart level. --- .../runtime/tests/AbstractSessionManager.php | 10 +++++ .../AssessmentTestSessionBranchingsTest.php | 18 +++++++++ .../branchings/branchings_testparts.xml | 37 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/samples/custom/runtime/branchings/branchings_testparts.xml diff --git a/src/qtism/runtime/tests/AbstractSessionManager.php b/src/qtism/runtime/tests/AbstractSessionManager.php index 9fa7d5aa3..68bf751ea 100644 --- a/src/qtism/runtime/tests/AbstractSessionManager.php +++ b/src/qtism/runtime/tests/AbstractSessionManager.php @@ -31,6 +31,7 @@ use qtism\data\IAssessmentItem; use qtism\data\NavigationMode; use qtism\data\SubmissionMode; +use qtism\data\TestPart; /** * The AbstractSessionManager class is a bed for instantiating @@ -148,6 +149,7 @@ protected function createRoute(AssessmentTest $test): Route { $routeStack = []; + /** @var TestPart $testPart */ foreach ($test->getTestParts() as $testPart) { $assessmentSectionStack = []; @@ -212,10 +214,18 @@ protected function createRoute(AssessmentTest $test): Route } } } + + // $route contains the currently processed testPart. + // Now, decorate last RouteItem of SelectableRoute with BranchRule objects if any. + if (!empty($route) && $route->count() > 0) { + $route->getLastRouteItem()->addBranchRules($testPart->getBranchRules()); + } } $finalRoutes = $routeStack; $route = new SelectableRoute(); + + /** @var SelectableRoute $finalRoute */ foreach ($finalRoutes as $finalRoute) { $route->appendRoute($finalRoute); } diff --git a/test/qtismtest/runtime/tests/AssessmentTestSessionBranchingsTest.php b/test/qtismtest/runtime/tests/AssessmentTestSessionBranchingsTest.php index 7265502f1..a1fc8f8c0 100644 --- a/test/qtismtest/runtime/tests/AssessmentTestSessionBranchingsTest.php +++ b/test/qtismtest/runtime/tests/AssessmentTestSessionBranchingsTest.php @@ -248,4 +248,22 @@ public function testBranchingOnPreconditon(): void $this::assertNull($session['Q03.SCORE']); $this::assertSame(1.0, $session['Q04.SCORE']->getValue()); } + + public function testBranchingOnTestPartsSimple1(): void + { + $session = self::instantiate(self::samplesDir() . 'custom/runtime/branchings/branchings_testparts.xml'); + $session->beginTestSession(); + + // We are starting at item Q01, in testPart P01. + $this->assertEquals('Q01', $session->getCurrentAssessmentItemRef()->getIdentifier()); + + // Let's jump to testPart P03. + $session->beginAttempt(); + $session->endAttempt(new State([new ResponseVariable('RESPONSE', Cardinality::SINGLE, BaseType::IDENTIFIER, new QtiIdentifier('GotoP03'))])); + $session->moveNext(); + + // We expect to land in testPart P03, item Q03. + $this->assertEquals('Q03', $session->getCurrentAssessmentItemRef()->getIdentifier()); + $this->assertEquals('P03', $session->getCurrentTestPart()->getIdentifier()); + } } diff --git a/test/samples/custom/runtime/branchings/branchings_testparts.xml b/test/samples/custom/runtime/branchings/branchings_testparts.xml new file mode 100644 index 000000000..881cf54d1 --- /dev/null +++ b/test/samples/custom/runtime/branchings/branchings_testparts.xml @@ -0,0 +1,37 @@ + + + + + + + + GotoP02 + + + + + + + GotoP03 + + + + + + + + + + + + + + + + + + +