Skip to content

Commit

Permalink
TemplateProcessing is now more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed May 9, 2016
1 parent fd38526 commit ba25bee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
11 changes: 10 additions & 1 deletion src/qtism/runtime/tests/AssessmentItemSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ public function beginItemSession()
$templateProcessing = $this->templateProcessing();
}

// Apply default values of outcomes variables.
// Apply default values of outcomes variables. We do it at this stage
// as templateProcessing could have change the default value of some
// Outcome Variables.
$this->resetOutcomeVariables();

// The session gets the INITIAL state, ready for a first attempt, and
Expand Down Expand Up @@ -1343,6 +1345,13 @@ public function templateProcessing()
$templateProcessingEngine = new TemplateProcessingEngine($templateProcessing, $this);
$templateProcessingEngine->process();

if ($this->mustAutoTemplateProcessing() === false) {
// Apply default values of outcomes variables. We do it at this stage
// as templateProcessing could have change the default value of some
// Outcome Variables.
$this->resetOutcomeVariables();
}

return true;
} else {

Expand Down
6 changes: 4 additions & 2 deletions src/qtism/runtime/tests/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use qtism\data\IAssessmentItem;

/**
* An SessionManager implementation that creates default AssessmentTestSession and
* A SessionManager implementation that creates default AssessmentTestSession and
* AssessmentItemSession objects.
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
Expand Down Expand Up @@ -56,6 +56,8 @@ protected function instantiateAssessmentTestSession(AssessmentTest $test, Route
*/
protected function instantiateAssessmentItemSession(IAssessmentItem $assessmentItem, $navigationMode, $submissionMode)
{
return new AssessmentItemSession($assessmentItem, $navigationMode, $submissionMode);
// When instantiating an AssessmentItemSession for a test, template processing must not occur automatically.
// is always false.
return new AssessmentItemSession($assessmentItem, $navigationMode, $submissionMode, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -700,54 +700,74 @@ public function testTemplateProcessingBasic1() {
$QTPL1Sessions = $session->getAssessmentItemSessions('QTPL1');
$QTPL1Session = $QTPL1Sessions[0];

// The default values should be correctly initialized within their respective item sessions...
// The GOODSCORE and WRONGSCORE variable values should not have changed yet. Indeed,
// we are in a linear test part. In such a context, template processing occurs
// just prior the first attempt.
$this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL1Session->getState());
$this->assertEquals(1.0, $QTPL1Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(0.0, $QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());
$this->assertEquals(1.0, $QTPL1Session['GOODSCORE']->getValue());
$this->assertNull($QTPL1Session->getVariable('GOODSCORE')->getDefaultValue());
$this->assertNull($QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue());
$this->assertEquals(0.0, $session['QTPL1.GOODSCORE']->getValue());
$this->assertEquals(0.0, $QTPL1Session['GOODSCORE']->getValue());
$this->assertEquals(0.0, $session['QTPL1.WRONGSCORE']->getValue());
$this->assertEquals(0.0, $QTPL1Session['WRONGSCORE']->getValue());

$QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
$QTPL2Session = $QTPL2Sessions[0];

$this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL2Session->getState());
$this->assertEquals(2.0, $QTPL2Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(-1.0, $QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(2.0, $session['QTPL2.GOODSCORE']->getValue());
$this->assertEquals(2.0, $QTPL2Session['GOODSCORE']->getValue());
$this->assertEquals(-1.0, $session['QTPL2.WRONGSCORE']->getValue());
$this->assertEquals(-1.0, $QTPL2Session['WRONGSCORE']->getValue());
$this->assertNull($QTPL2Session->getVariable('GOODSCORE')->getDefaultValue());
$this->assertNull($QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue());
$this->assertEquals(0.0, $session['QTPL2.GOODSCORE']->getValue());
$this->assertEquals(0.0, $QTPL2Session['GOODSCORE']->getValue());
$this->assertEquals(0.0, $session['QTPL2.WRONGSCORE']->getValue());
$this->assertEquals(0.0, $QTPL2Session['WRONGSCORE']->getValue());

// Now let's make sure the persistence works correctly when templating is in force...
// We do this by testing again that default values are correctly initialized within their respective
// item sessions...
// We do this by testing again that default values and values are stil the same after
// persistence.
$storage->persist($session);
unset($session);
$session = $storage->retrieve($test, $sessionId);

$QTPL1Sessions = $session->getAssessmentItemSessions('QTPL1');
$QTPL1Session = $QTPL1Sessions[0];

$this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL1Session->getState());
$this->assertEquals(1.0, $QTPL1Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(0.0, $QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());
$this->assertEquals(1.0, $QTPL1Session['GOODSCORE']->getValue());
$this->assertNull($QTPL1Session->getVariable('GOODSCORE')->getDefaultValue());
$this->assertNull($QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue());
$this->assertEquals(0.0, $session['QTPL1.GOODSCORE']->getValue());
$this->assertEquals(0.0, $QTPL1Session['GOODSCORE']->getValue());
$this->assertEquals(0.0, $session['QTPL1.WRONGSCORE']->getValue());
$this->assertEquals(0.0, $QTPL1Session['WRONGSCORE']->getValue());

$QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
$QTPL2Session = $QTPL2Sessions[0];

$this->assertEquals(AssessmentItemSessionState::INITIAL, $QTPL2Session->getState());
$this->assertEquals(2.0, $QTPL2Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(-1.0, $QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(2.0, $session['QTPL2.GOODSCORE']->getValue());
$this->assertEquals(2.0, $QTPL2Session['GOODSCORE']->getValue());
$this->assertEquals(-1.0, $session['QTPL2.WRONGSCORE']->getValue());
$this->assertEquals(-1.0, $QTPL2Session['WRONGSCORE']->getValue());

// It seems to be ok! Let's take the test!
$this->assertNull($QTPL2Session->getVariable('GOODSCORE')->getDefaultValue());
$this->assertNull($QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue());
$this->assertEquals(0.0, $session['QTPL2.GOODSCORE']->getValue());
$this->assertEquals(0.0, $QTPL2Session['GOODSCORE']->getValue());
$this->assertEquals(0.0, $session['QTPL2.WRONGSCORE']->getValue());
$this->assertEquals(0.0, $QTPL2Session['WRONGSCORE']->getValue());

// It seems to be ok! Let's take the test! By beginning the first attempt,
// template processing is applied on QTPL1. However, anything related to QTPL2 should not
// have changed.
$session->beginAttempt();

// Let's check the values. Q01 should be affected by template processing, QTPL2 should not.
$QTPL1Sessions = $session->getAssessmentItemSessions('QTPL1');
$QTPL1Session = $QTPL1Sessions[0];

$this->assertEquals(AssessmentItemSessionState::INTERACTING, $QTPL1Session->getState());
$this->assertEquals(1.0, $QTPL1Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(0.0, $QTPL1Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());
$this->assertEquals(1.0, $QTPL1Session['GOODSCORE']->getValue());
$this->assertEquals(0.0, $session['QTPL1.WRONGSCORE']->getValue());
$this->assertEquals(0.0, $QTPL1Session['WRONGSCORE']->getValue());

// TPL1's responses should be applied their default values if any at the
// beginning of the first attempt.
$this->assertEquals('ChoiceB', $session['QTPL1.RESPONSE']->getValue());
Expand Down Expand Up @@ -784,6 +804,18 @@ public function testTemplateProcessingBasic1() {
// -- TPL2 - Correct response.
$session->beginAttempt();

// QTPL2 should now be affected by Templat eProcessing.
$QTPL2Sessions = $session->getAssessmentItemSessions('QTPL2');
$QTPL2Session = $QTPL2Sessions[0];

$this->assertEquals(AssessmentItemSessionState::INTERACTING, $QTPL2Session->getState());
$this->assertEquals(2.0, $QTPL2Session->getVariable('GOODSCORE')->getDefaultValue()->getValue());
$this->assertEquals(-1.0, $QTPL2Session->getVariable('WRONGSCORE')->getDefaultValue()->getValue());
$this->assertEquals(2.0, $session['QTPL2.GOODSCORE']->getValue());
$this->assertEquals(2.0, $QTPL2Session['GOODSCORE']->getValue());
$this->assertEquals(-1.0, $session['QTPL2.WRONGSCORE']->getValue());
$this->assertEquals(-1.0, $QTPL2Session['WRONGSCORE']->getValue());

// TPL2's responses should be at their default values if any at
// the beginning of the first attempt.
$this->assertEquals('ChoiceA', $session['QTPL2.RESPONSE']->getValue());
Expand Down Expand Up @@ -826,7 +858,7 @@ public function testTemplateProcessingBasic1() {

public function testTemplateDefault1() {
$doc = new XmlCompactDocument();
$doc->load(self::samplesDir() . 'custom/runtime/templates/template_test_simple.xml');
$doc->load(self::samplesDir() . 'custom/runtime/templates/template_default_test_simple.xml');
$test = $doc->getDocumentComponent();

$sessionManager = new SessionManager($doc->getDocumentComponent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class AssessmentTestSessionTemplatesTest extends QtiSmAssessmentTestSessionTestCase {

public function testSimpleTemplating() {
$session = self::instantiate(self::samplesDir() . 'custom/runtime/templates/template_test_simple.xml');
$session = self::instantiate(self::samplesDir() . 'custom/runtime/templates/template_default_test_simple.xml');
$session->beginTestSession();
// We are in linear mode with no branching/preconditions, so the sessions are alive...
// But the templateDefaults/templateProcessings will only occur at the beginning of the
Expand Down

0 comments on commit ba25bee

Please sign in to comment.