Skip to content

Commit

Permalink
Maybe a final solution for templateProcessing + templateDefaults?
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed May 6, 2016
1 parent 037c9eb commit fd38526
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
18 changes: 17 additions & 1 deletion src/qtism/runtime/common/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function offsetGet($offset)
}

/**
* Reset all test-level outcome variables to their defaults.
* Reset all outcome variables to their defaults.
*
* @param boolean $preserveBuiltIn Whether the built-in outcome variable 'completionStatus' should be preserved.
*/
Expand All @@ -181,6 +181,22 @@ public function resetOutcomeVariables($preserveBuiltIn = true)
}
}
}

/**
* Reset all template variables to their defaults.
*
* @return void
*/
public function resetTemplateVariables()
{
$data = &$this->getDataPlaceHolder();

foreach (array_keys($data) as $k) {
if ($data[$k] instanceof TemplateVariable) {
$data[$k]->applyDefaultValue();
}
}
}

/**
* @see \qtism\common\collections\AbstractCollection::checkType()
Expand Down
16 changes: 6 additions & 10 deletions src/qtism/runtime/tests/AssessmentItemSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,22 +634,18 @@ public function beginItemSession()
}
}

// Apply default values to Template variables.
$this->resetTemplateVariables();

// Apply templateProcessing if needed.
$templateProcessing = false;
if ($this->mustAutoTemplateProcessing() === true) {
$templateProcessing = $this->templateProcessing();

foreach ($data as $identifier => $variable) {
if (in_array($identifier, $filter) === false) {

// Outcome variables are applied their default value if any.
if ($variable instanceof OutcomeVariable || ($variable instanceof TemplateVariable && $templateProcessing === false)) {
$variable->applyDefaultValue();
}
}
}
}

// Apply default values of outcomes variables.
$this->resetOutcomeVariables();

// The session gets the INITIAL state, ready for a first attempt, and
// built-in variables get their initial value set.
$this->setState(AssessmentItemSessionState::INITIAL);
Expand Down
1 change: 1 addition & 0 deletions src/qtism/runtime/tests/AssessmentTestSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@ protected function applyTemplateDefaults(AssessmentItemSession $session)
if (is_null($variable) === false) {
$val = $expressionEngine->process();
$variable->setDefaultValue($val);
$variable->applyDefaultValue();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ public function testTemplateDefault1() {
// TPL1's responses should be applied their default values if any at the
// beginning of the first attempt.
$this->assertEquals(null, $session['QTPL1.RESPONSE']);
$this->assertEquals(1.0, $session['QTPL1.GOODSCORE']->getValue());

// Noisy persistence ...
$storage->persist($session);
Expand Down
16 changes: 0 additions & 16 deletions test/samples/custom/runtime/templates/template_test_simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float"/>
<templateDeclaration identifier="GOODSCORE" cardinality="single" baseType="float"/>
<templateDeclaration identifier="WRONGSCORE" cardinality="single" baseType="float"/>
<templateProcessing>
<setTemplateValue identifier="GOODSCORE">
<default identifier="GOODSCORE"/>
</setTemplateValue>
<setTemplateValue identifier="WRONGSCORE">
<default identifier="WRONGSCORE"/>
</setTemplateValue>
</templateProcessing>
<responseProcessing>
<responseCondition>
<responseIf>
Expand Down Expand Up @@ -63,14 +55,6 @@
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float"/>
<templateDeclaration identifier="GOODSCORE" cardinality="single" baseType="float"/>
<templateDeclaration identifier="WRONGSCORE" cardinality="single" baseType="float"/>
<templateProcessing>
<setTemplateValue identifier="GOODSCORE">
<default identifier="GOODSCORE"/>
</setTemplateValue>
<setTemplateValue identifier="WRONGSCORE">
<default identifier="WRONGSCORE"/>
</setTemplateValue>
</templateProcessing>
<responseProcessing>
<responseCondition>
<responseIf>
Expand Down

0 comments on commit fd38526

Please sign in to comment.