Skip to content

Commit

Permalink
Allow ResponseIf/ResponseElseIf statements without responseRules.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 20, 2016
1 parent a4f7219 commit 6dcac1b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 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.9.32",
"version": "0.9.33",
"authors": [
{
"name": "Open Assessment Technologies S.A.",
Expand Down
12 changes: 2 additions & 10 deletions qtism/data/rules/ResponseElseIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2013-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts, <[email protected]>
* @license GPLv2
Expand Down Expand Up @@ -61,7 +61,6 @@ class ResponseElseIf extends QtiComponent {
*
* @param Expression $expression An expression to be evaluated with the Else If statement.
* @param ResponseRuleCollection $responseRules A collection of ResponseRule objects.
* @throws InvalidArgumentException If $responseRules is an empty collection.
*/
public function __construct(Expression $expression, ResponseRuleCollection $responseRules) {
$this->setExpression($expression);
Expand Down Expand Up @@ -101,16 +100,9 @@ public function getResponseRules() {
* to the Else If statement returns true.
*
* @param ResponseRuleCollection $responseRules A collection of ResponseRule objects.
* @throws InvalidArgumentException If $responseRules is an empty collection.
*/
public function setResponseRules(ResponseRuleCollection $responseRules) {
if (count($responseRules) > 0) {
$this->responseRules = $responseRules;
}
else {
$msg = "A ResponseElseIf object must be bound to at lease one ResponseRule object.";
throw new InvalidArgumentException($msg);
}
$this->responseRules = $responseRules;
}

public function getQtiClassName() {
Expand Down
19 changes: 6 additions & 13 deletions qtism/data/rules/ResponseIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2013-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts, <[email protected]>
* @license GPLv2
Expand All @@ -25,7 +25,6 @@
namespace qtism\data\rules;

use qtism\data\QtiComponentCollection;

use qtism\data\QtiComponent;
use qtism\data\expressions\Expression;
use \InvalidArgumentException;
Expand Down Expand Up @@ -67,7 +66,6 @@ class ResponseIf extends QtiComponent {
*
* @param Expression $expression The expression to be evaluated with the If statement.
* @param ResponseRuleCollection $responseRules A collection of sub expressions to be evaluated if the Expression returns true.
* @throws InvalidArgumentException If $responseRules does not contain any ResponseRule object.
*/
public function __construct(Expression $expression, ResponseRuleCollection $responseRules) {
$this->setExpression($expression);
Expand Down Expand Up @@ -97,16 +95,9 @@ public function setExpression(Expression $expression) {
* evaluated with the If statement returns true.
*
* @param ResponseRuleCollection $responseRules A collection of ResponseRule objects.
* @throws InvalidArgumentException If $responseRules is an empty collection.
*/
public function setResponseRules(ResponseRuleCollection $responseRules) {
if (count($responseRules) > 0) {
$this->responseRules = $responseRules;
}
else {
$msg = "A ResponseIf object must be bound to at least one ResponseRule.";
throw new InvalidArgumentException($msg);
}
$this->responseRules = $responseRules;
}

/**
Expand All @@ -124,8 +115,10 @@ public function getQtiClassName() {
}

public function getComponents() {
$comp = array_merge(array($this->getExpression()),
$this->getResponseRules()->getArrayCopy());
$comp = array_merge(
array($this->getExpression()),
$this->getResponseRules()->getArrayCopy()
);

return new QtiComponentCollection($comp);
}
Expand Down
57 changes: 56 additions & 1 deletion test/qtism/runtime/processing/ResponseProcessingEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,61 @@ public function testResponseProcessingMatchCorrect() {
$this->assertInstanceOf('qtism\\common\\datatypes\\Float', $context['SCORE']);
$this->assertEquals(1.0, $context['SCORE']->getValue());
}

public function testResponseProcessingNoResponseRule() {
$responseProcessing = $this->createComponentFromXml('
<responseProcessing>
<responseCondition>
<responseIf>
<match>
<variable identifier="RESPONSE"/>
<baseValue baseType="identifier">ChoiceA</baseValue>
</match>
<!-- Do nothing... -->
</responseIf>
<responseElseIf>
<match>
<variable identifier="RESPONSE"/>
<baseValue baseType="identifier">ChoiceB</baseValue>
</match>
<!-- Do nothing... -->
</responseElseIf>
<responseElse>
<setOutcomeValue identifier="SCORE">
<baseValue baseType="float">1.0</baseValue>
</setOutcomeValue>
</responseElse>
</responseCondition>
</responseProcessing>
');

$responseDeclaration = $this->createComponentFromXml('
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier"/>
');

$outcomeDeclaration = $this->createComponentFromXml('
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float"/>
');

$respVar = ResponseVariable::createFromDataModel($responseDeclaration);
$outVar = OutcomeVariable::createFromDataModel($outcomeDeclaration);
$context = new State(array($respVar, $outVar));

$engine = new ResponseProcessingEngine($responseProcessing, $context);

$context['RESPONSE'] = new Identifier('ChoiceA');
$engine->process();
$this->assertNull($context['SCORE']);

$context['RESPONSE'] = new Identifier('ChoiceB');
$engine->process();
$this->assertNull($context['SCORE']);

$context['RESPONSE'] = new Identifier('ChoiceC');
$engine->process();
$this->assertInstanceOf('qtism\\common\\datatypes\\Float', $context['SCORE']);
$this->assertEquals(1.0, $context['SCORE']->getValue());
}

public function testResponseProcessingExitResponse() {
$responseProcessing = $this->createComponentFromXml('
Expand All @@ -64,4 +119,4 @@ public function testResponseProcessingExitResponse() {
$this->assertEquals(RuleProcessingException::EXIT_RESPONSE, $e->getCode());
}
}
}
}

0 comments on commit 6dcac1b

Please sign in to comment.