Skip to content
Commits on Source (19)
...@@ -7,14 +7,16 @@ ...@@ -7,14 +7,16 @@
* Time: 13:51 * Time: 13:51
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class CompositeBuilderTest extends PHPUnit_Framework_TestCase { class CompositeBuilderTest extends PHPUnit_Framework_TestCase
{
/** /**
* @var CompositeBuilder * @var CompositeBuilder
*/ */
protected $builder; protected $builder;
public function setUp(){ public function setUp()
{
include_once __DIR__ . '/../classes/CompositeBuilder.php'; include_once __DIR__ . '/../classes/CompositeBuilder.php';
$this->builder = new CompositeBuilder; $this->builder = new CompositeBuilder;
} }
......
...@@ -9,8 +9,8 @@ include_once __DIR__ . '/../classes/ConditionParser.php'; ...@@ -9,8 +9,8 @@ include_once __DIR__ . '/../classes/ConditionParser.php';
* Time: 11:05 * Time: 11:05
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ConditionParserProxy extends ConditionParser{ class ConditionParserProxy extends ConditionParser
{
public function fetchExpressions() public function fetchExpressions()
{ {
parent::fetchExpressions(); // TODO: Change the autogenerated stub parent::fetchExpressions(); // TODO: Change the autogenerated stub
...@@ -59,5 +59,4 @@ class ConditionParserProxy extends ConditionParser{ ...@@ -59,5 +59,4 @@ class ConditionParserProxy extends ConditionParser{
{ {
return $this->operators; return $this->operators;
} }
} }
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
* Time: 16:08 * Time: 16:08
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ConditionParserTest extends \PHPUnit_Framework_TestCase { class ConditionParserTest extends \PHPUnit_Framework_TestCase
{
protected $key_constraint; protected $key_constraint;
protected $type_constraint; protected $type_constraint;
protected $array_constraint; protected $array_constraint;
...@@ -24,7 +24,8 @@ class ConditionParserTest extends \PHPUnit_Framework_TestCase { ...@@ -24,7 +24,8 @@ class ConditionParserTest extends \PHPUnit_Framework_TestCase {
protected static $EXPECTED_OPERATORS = array("=","&","<=","|",">="); protected static $EXPECTED_OPERATORS = array("=","&","<=","|",">=");
protected static $CANNONICALIZED_CONDITION = "n o n o (n o n o n o n)"; protected static $CANNONICALIZED_CONDITION = "n o n o (n o n o n o n)";
public function setUp(){ public function setUp()
{
include_once 'ConditionParserProxy.php'; include_once 'ConditionParserProxy.php';
include_once __DIR__ . '/../classes/AbstractComposite.php'; include_once __DIR__ . '/../classes/AbstractComposite.php';
$this->parser = new ConditionParserProxy(); $this->parser = new ConditionParserProxy();
...@@ -138,19 +139,15 @@ class ConditionParserTest extends \PHPUnit_Framework_TestCase { ...@@ -138,19 +139,15 @@ class ConditionParserTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals("n o n o !(n o n) o !(n o n)", $this->parser->getCondition()); $this->assertEquals("n o n o !(n o n) o !(n o n)", $this->parser->getCondition());
} }
private function recursiveNodeTest($nodes){ private function recursiveNodeTest($nodes)
foreach($nodes as $key => $node)
{
if(!is_array($node))
{ {
foreach ($nodes as $key => $node) {
if (!is_array($node)) {
$this->assertThat($key, $this->key_constraint); $this->assertThat($key, $this->key_constraint);
if($key == "type") if ($key == "type") {
{
$this->assertThat($node, $this->type_constraint); $this->assertThat($node, $this->type_constraint);
} }
} } else {
else
{
$this->assertThat($key, $this->array_constraint); $this->assertThat($key, $this->array_constraint);
$this->recursiveNodeTest($node); $this->recursiveNodeTest($node);
} }
......
...@@ -7,9 +7,8 @@ ...@@ -7,9 +7,8 @@
* Time: 15:51 * Time: 15:51
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ExpressionTest extends PHPUnit_Framework_TestCase { class ExpressionTest extends PHPUnit_Framework_TestCase
{
public function test_AnswerOfQuestionExpression() public function test_AnswerOfQuestionExpression()
{ {
include_once __DIR__ . '/../../classes/Expressions/AnswerOfQuestionExpression.php'; include_once __DIR__ . '/../../classes/Expressions/AnswerOfQuestionExpression.php';
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
* Time: 12:30 * Time: 12:30
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase { class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase
{
/** /**
* @var ReflectionClass * @var ReflectionClass
...@@ -24,7 +25,8 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -24,7 +25,8 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase {
*/ */
protected $expected_pattern = '/%[0-9\.]+%|#-?[0-9\.]+#|\+[0-9]+\+|(Q\d+)(?=\=|<|>|\s|$)|Q[0-9]+\[[0-9]+\]|~.*?~|;[0-9]+:[0-9]+;|\$[0-9]+(?:,[0-9]+)*\$|\*[0-9]+(?:,[0-9]+)*\*/'; protected $expected_pattern = '/%[0-9\.]+%|#-?[0-9\.]+#|\+[0-9]+\+|(Q\d+)(?=\=|<|>|\s|$)|Q[0-9]+\[[0-9]+\]|~.*?~|;[0-9]+:[0-9]+;|\$[0-9]+(?:,[0-9]+)*\$|\*[0-9]+(?:,[0-9]+)*\*/';
public function setUp(){ public function setUp()
{
include_once __DIR__ . '/../../classes/Factory/ExpressionManufacturer.php'; include_once __DIR__ . '/../../classes/Factory/ExpressionManufacturer.php';
$this->reflection = new ReflectionClass("ExpressionManufacturer"); $this->reflection = new ReflectionClass("ExpressionManufacturer");
...@@ -61,8 +63,7 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -61,8 +63,7 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase {
$subject = 'Q5 Q66 Q52[92] ~Hallo_Welt~ #5# +32+ %098% $3,4,5$ ;2:6;'; $subject = 'Q5 Q66 Q52[92] ~Hallo_Welt~ #5# +32+ %098% $3,4,5$ ;2:6;';
$matches = $this->manufacturer->match($subject); $matches = $this->manufacturer->match($subject);
$tokens = \explode(" ", $subject); $tokens = \explode(" ", $subject);
foreach($tokens as $token) foreach ($tokens as $token) {
{
$this->assertContains($token, $matches); $this->assertContains($token, $matches);
} }
} }
...@@ -80,8 +81,7 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -80,8 +81,7 @@ class ExpressionManufacturerTest extends PHPUnit_Framework_TestCase {
"$2,3,4,5$" => "OrderingResultExpression" "$2,3,4,5$" => "OrderingResultExpression"
); );
foreach($expressions as $key => $expression) foreach ($expressions as $key => $expression) {
{
include_once __DIR__ . "/../../classes/Expressions/" . $expression . ".php"; include_once __DIR__ . "/../../classes/Expressions/" . $expression . ".php";
$expression_object = $this->manufacturer->manufacture($key); $expression_object = $this->manufacturer->manufacture($key);
$this->assertInstanceOf($expression, $expression_object); $this->assertInstanceOf($expression, $expression_object);
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
* Time: 12:30 * Time: 12:30
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class OperationManufacturerTest extends PHPUnit_Framework_TestCase { class OperationManufacturerTest extends PHPUnit_Framework_TestCase
{
/** /**
* @var ReflectionClass * @var ReflectionClass
...@@ -24,7 +25,8 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -24,7 +25,8 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase {
*/ */
protected $expected_pattern = '/[&\|<>=]+/'; protected $expected_pattern = '/[&\|<>=]+/';
public function setUp(){ public function setUp()
{
include_once __DIR__ . '/../../classes/Factory/OperationManufacturer.php'; include_once __DIR__ . '/../../classes/Factory/OperationManufacturer.php';
$this->reflection = new ReflectionClass("OperationManufacturer"); $this->reflection = new ReflectionClass("OperationManufacturer");
...@@ -61,8 +63,7 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -61,8 +63,7 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase {
$subject = '< = > & | <> <= >='; $subject = '< = > & | <> <= >=';
$matches = $this->manufacturer->match($subject); $matches = $this->manufacturer->match($subject);
$tokens = \explode(" ", $subject); $tokens = \explode(" ", $subject);
foreach($tokens as $token) foreach ($tokens as $token) {
{
$this->assertContains($token, $matches); $this->assertContains($token, $matches);
} }
} }
...@@ -80,8 +81,7 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase { ...@@ -80,8 +81,7 @@ class OperationManufacturerTest extends PHPUnit_Framework_TestCase {
"|" => "OrOperation" "|" => "OrOperation"
); );
foreach($operations as $key => $operation) foreach ($operations as $key => $operation) {
{
include_once __DIR__ . "/../../classes/Operations/" . $operation . ".php"; include_once __DIR__ . "/../../classes/Operations/" . $operation . ".php";
$operation_object = $this->manufacturer->manufacture($key); $operation_object = $this->manufacturer->manufacture($key);
$this->assertInstanceOf($operation, $operation_object); $this->assertInstanceOf($operation, $operation_object);
......
...@@ -9,7 +9,8 @@ include_once 'CompositeInterface.php'; ...@@ -9,7 +9,8 @@ include_once 'CompositeInterface.php';
* Time: 10:05 * Time: 10:05
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
abstract class AbstractComposite implements CompositeInterface { abstract class AbstractComposite implements CompositeInterface
{
/** /**
* @var AbstractComposite[] * @var AbstractComposite[]
...@@ -33,11 +34,14 @@ abstract class AbstractComposite implements CompositeInterface { ...@@ -33,11 +34,14 @@ abstract class AbstractComposite implements CompositeInterface {
public function describe() public function describe()
{ {
$description = ""; $description = "";
if(is_array($this->nodes)) if (is_array($this->nodes)) {
{ if (\count($this->nodes) > 0) {
if(\count($this->nodes) > 0) $description .= "(" . $this->nodes[0]->describe(); $description .= "(" . $this->nodes[0]->describe();
}
$description .= $this->getDescription(); $description .= $this->getDescription();
if(\count($this->nodes) > 0) $description .= $this->nodes[1]->describe() . ") "; if (\count($this->nodes) > 0) {
$description .= $this->nodes[1]->describe() . ") ";
}
} }
return $description; return $description;
} }
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
* Time: 12:18 * Time: 12:18
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class CompositeBuilder { class CompositeBuilder
{
/** /**
* This array defines the weights and direction of operators.<br /> * This array defines the weights and direction of operators.<br />
...@@ -33,33 +34,26 @@ class CompositeBuilder { ...@@ -33,33 +34,26 @@ class CompositeBuilder {
*/ */
public function create($nodes) public function create($nodes)
{ {
if($nodes['type'] == 'group') if ($nodes['type'] == 'group') {
{ foreach ($nodes['nodes'] as $key => $child) {
foreach($nodes['nodes'] as $key => $child)
{
$nodes['nodes'][$key] = $this->create($child); $nodes['nodes'][$key] = $this->create($child);
} }
$counted_nodes = 0; $counted_nodes = 0;
if(is_array($nodes['nodes'])) if (is_array($nodes['nodes'])) {
{
$counted_nodes = count($nodes['nodes']); $counted_nodes = count($nodes['nodes']);
} }
foreach($this->operators as $next_operator) foreach ($this->operators as $next_operator) {
{
do { do {
$index = -1; $index = -1;
for($i = 0; $i < $counted_nodes; $i++) for ($i = 0; $i < $counted_nodes; $i++) {
{ if (!is_object($nodes['nodes'][$i]) && $nodes['nodes'][$i]['type'] == 'operator' && $nodes['nodes'][$i]['value'] == $next_operator) {
if(!is_object($nodes['nodes'][$i]) && $nodes['nodes'][$i]['type'] == 'operator' && $nodes['nodes'][$i]['value'] == $next_operator)
{
$index = $i; $index = $i;
break; break;
} }
} }
if($index >= 0) if ($index >= 0) {
{
$operation_manufacture = OperationManufacturer::_getInstance(); $operation_manufacture = OperationManufacturer::_getInstance();
$operator = $operation_manufacture->manufacture($nodes['nodes'][$index]['value']); $operator = $operation_manufacture->manufacture($nodes['nodes'][$index]['value']);
...@@ -92,8 +86,7 @@ class CompositeBuilder { ...@@ -92,8 +86,7 @@ class CompositeBuilder {
$manufacturer = ExpressionManufacturer::_getInstance(); $manufacturer = ExpressionManufacturer::_getInstance();
$expression = $node['nodes'][$index]; $expression = $node['nodes'][$index];
if(!($expression instanceof AbstractComposite)) if (!($expression instanceof AbstractComposite)) {
{
$expression = $manufacturer->manufacture($node['nodes'][$index]['value']); $expression = $manufacturer->manufacture($node['nodes'][$index]['value']);
} }
return $expression; return $expression;
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
* Time: 13:27 * Time: 13:27
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class CompositeEvaluator { class CompositeEvaluator
{
/** /**
* @var ilParserQuestionProvider * @var ilParserQuestionProvider
...@@ -36,8 +37,7 @@ class CompositeEvaluator { ...@@ -36,8 +37,7 @@ class CompositeEvaluator {
*/ */
public function evaluate(AbstractComposite $composite) public function evaluate(AbstractComposite $composite)
{ {
if(is_array($composite->nodes) && count($composite->nodes) > 0) if (is_array($composite->nodes) && count($composite->nodes) > 0) {
{
$composite->nodes[0] = $this->evaluate($composite->nodes[0]); $composite->nodes[0] = $this->evaluate($composite->nodes[0]);
$composite->nodes[1] = $this->evaluate($composite->nodes[1]); $composite->nodes[1] = $this->evaluate($composite->nodes[1]);
$composite = $this->evaluateSubTree($composite); $composite = $this->evaluateSubTree($composite);
...@@ -63,8 +63,7 @@ class CompositeEvaluator { ...@@ -63,8 +63,7 @@ class CompositeEvaluator {
$solutions = $question->getUserQuestionResult($this->session->getActiveId(), $this->session->getPass()); $solutions = $question->getUserQuestionResult($this->session->getActiveId(), $this->session->getPass());
if($question instanceof assClozeTest) if ($question instanceof assClozeTest) {
{
// @todo for Thomas J.: Move to interface / implement in concrete class (req. for future releases) // @todo for Thomas J.: Move to interface / implement in concrete class (req. for future releases)
/** /**
* @var $gap assClozeGap * @var $gap assClozeGap
...@@ -73,29 +72,22 @@ class CompositeEvaluator { ...@@ -73,29 +72,22 @@ class CompositeEvaluator {
$result = $solutions->getSolutionForKey($index); $result = $solutions->getSolutionForKey($index);
$gap = $question->getAvailableAnswerOptions($index - 1); $gap = $question->getAvailableAnswerOptions($index - 1);
if($rightNode instanceof StringResultExpression) if ($rightNode instanceof StringResultExpression) {
{ if ($gap->getType() == 1) {
if($gap->getType() == 1)
{
$answer = $gap->getItem($result['value'] - 1); $answer = $gap->getItem($result['value'] - 1);
$solutions->removeByKey($index); $solutions->removeByKey($index);
$solutions->addKeyValue($index, $answer->getAnswertext()); $solutions->addKeyValue($index, $answer->getAnswertext());
} }
} } elseif (
else if(
$rightNode instanceof PercentageResultExpression && $rightNode instanceof PercentageResultExpression &&
$composite->nodes[0] instanceof ResultOfAnswerOfQuestionExpression) $composite->nodes[0] instanceof ResultOfAnswerOfQuestionExpression) {
{
require_once './Services/Randomization/classes/class.ilArrayElementShuffler.php';
/** /**
* @var $answers assAnswerCloze[] * @var $answers assAnswerCloze[]
*/ */
$answers = $gap->getItems(new ilArrayElementShuffler()); $answers = $gap->getItems(new ilArrayElementShuffler());
$max_points = 0; $max_points = 0;
foreach($answers as $answer) foreach ($answers as $answer) {
{ if ($max_points < $answer->getPoints()) {
if($max_points < $answer->getPoints())
{
$max_points = $answer->getPoints(); $max_points = $answer->getPoints();
} }
} }
...@@ -103,29 +95,29 @@ class CompositeEvaluator { ...@@ -103,29 +95,29 @@ class CompositeEvaluator {
$item = null; $item = null;
$reached_points = null; $reached_points = null;
// @todo for Thomas J.: Maybe handle identical scoring for every type // @todo for Thomas J.: Maybe handle identical scoring for every type
switch($gap->getType()) switch ($gap->getType()) {
{
case CLOZE_TEXT: case CLOZE_TEXT:
for($order = 0; $order < $gap->getItemCount(); $order++) for ($order = 0; $order < $gap->getItemCount(); $order++) {
{
$answer = $gap->getItem($order); $answer = $gap->getItem($order);
$item_points = $question->getTextgapPoints($answer->getAnswertext(), $result['value'], $answer->getPoints()); $item_points = $question->getTextgapPoints($answer->getAnswertext(), $result['value'], $answer->getPoints());
if($item_points > $reached_points) $reached_points = $item_points; if ($item_points > $reached_points) {
$reached_points = $item_points;
}
} }
break; break;
case CLOZE_NUMERIC: case CLOZE_NUMERIC:
for($order = 0; $order < $gap->getItemCount(); $order++) for ($order = 0; $order < $gap->getItemCount(); $order++) {
{
$answer = $gap->getItem($order); $answer = $gap->getItem($order);
$item_points = $question->getNumericgapPoints($answer->getAnswertext(), $result["value"], $answer->getPoints(), $answer->getLowerBound(), $answer->getUpperBound()); $item_points = $question->getNumericgapPoints($answer->getAnswertext(), $result["value"], $answer->getPoints(), $answer->getLowerBound(), $answer->getUpperBound());
if($item_points > $reached_points) $reached_points = $item_points; if ($item_points > $reached_points) {
$reached_points = $item_points;
}
} }
break; break;
case CLOZE_SELECT: case CLOZE_SELECT:
if($result['value'] != null) if ($result['value'] != null) {
{
$answer = $gap->getItem($result['value'] - 1); $answer = $gap->getItem($result['value'] - 1);
$reached_points = $answer->getPoints(); $reached_points = $answer->getPoints();
} }
...@@ -133,8 +125,7 @@ class CompositeEvaluator { ...@@ -133,8 +125,7 @@ class CompositeEvaluator {
} }
$percentage = 0; $percentage = 0;
if($max_points != 0 && $reached_points !== null) if ($max_points != 0 && $reached_points !== null) {
{
$percentage = (int) (($reached_points / $max_points) * 100); $percentage = (int) (($reached_points / $max_points) * 100);
} }
$solutions->setReachedPercentage($percentage); $solutions->setReachedPercentage($percentage);
...@@ -145,16 +136,14 @@ class CompositeEvaluator { ...@@ -145,16 +136,14 @@ class CompositeEvaluator {
$question instanceof assFormulaQuestion && $question instanceof assFormulaQuestion &&
$rightNode instanceof PercentageResultExpression && $rightNode instanceof PercentageResultExpression &&
$composite->nodes[0] instanceof ResultOfAnswerOfQuestionExpression $composite->nodes[0] instanceof ResultOfAnswerOfQuestionExpression
) ) {
{
// @todo for Thomas J.: Move to interface / implement in concrete class (req. for future releases) // @todo for Thomas J.: Move to interface / implement in concrete class (req. for future releases)
$result = $solutions->getSolutionForKey($index); $result = $solutions->getSolutionForKey($index);
$answer = $question->getAvailableAnswerOptions($index - 1); $answer = $question->getAvailableAnswerOptions($index - 1);
$unit = $solutions->getSolutionForKey($index . "_unit"); $unit = $solutions->getSolutionForKey($index . "_unit");
$key = null; $key = null;
if(is_array($unit)) if (is_array($unit)) {
{
$key = $unit['value']; $key = $unit['value'];
} }
...@@ -162,19 +151,15 @@ class CompositeEvaluator { ...@@ -162,19 +151,15 @@ class CompositeEvaluator {
$points = $answer->getReachedPoints($question->getVariables(), $question->getResults(), $result["value"], $key, $question->getUnitrepository()->getUnits()); $points = $answer->getReachedPoints($question->getVariables(), $question->getResults(), $result["value"], $key, $question->getUnitrepository()->getUnits());
$percentage = 0; $percentage = 0;
if($max_points != 0) if ($max_points != 0) {
{
$percentage = (int) (($points / $max_points) * 100); $percentage = (int) (($points / $max_points) * 100);
} }
$solutions->setReachedPercentage($percentage); $solutions->setReachedPercentage($percentage);
} }
$result = $rightNode->checkResult($solutions, $composite->getPattern(), $index); $result = $rightNode->checkResult($solutions, $composite->getPattern(), $index);
} } else {
else switch ($composite->getPattern()) {
{
switch($composite->getPattern())
{
case "&": case "&":
$result = $composite->nodes[0] && $composite->nodes[1]; $result = $composite->nodes[0] && $composite->nodes[1];
break; break;
...@@ -186,11 +171,9 @@ class CompositeEvaluator { ...@@ -186,11 +171,9 @@ class CompositeEvaluator {
} }
} }
if($composite->isNegated()) if ($composite->isNegated()) {
{
return !$result; return !$result;
} }
return $result; return $result;
} }
} }
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
interface CompositeInterface { interface CompositeInterface
{
/** /**
* Adds an CompositeInterface object to the node array which represents the condition tree structure * Adds an CompositeInterface object to the node array which represents the condition tree structure
......
...@@ -53,8 +53,7 @@ class CompositeValidator ...@@ -53,8 +53,7 @@ class CompositeValidator
public function validate(AbstractComposite $composite) public function validate(AbstractComposite $composite)
{ {
if(is_array($composite->nodes) && count($composite->nodes) > 0) if (is_array($composite->nodes) && count($composite->nodes) > 0) {
{
$this->validate($composite->nodes[0]); $this->validate($composite->nodes[0]);
$this->validate($composite->nodes[1]); $this->validate($composite->nodes[1]);
$this->validateSubTree($composite); $this->validateSubTree($composite);
...@@ -77,13 +76,11 @@ class CompositeValidator ...@@ -77,13 +76,11 @@ class CompositeValidator
$this->checkQuestionExists($question, $question_index); $this->checkQuestionExists($question, $question_index);
$this->checkQuestionIsReachable($question, $question_index); $this->checkQuestionIsReachable($question, $question_index);
if($this->isResultOfAnswerExpression($question_expression)) if ($this->isResultOfAnswerExpression($question_expression)) {
{
$answer_index = $question_expression->getAnswerIndex() - 1; $answer_index = $question_expression->getAnswerIndex() - 1;
$this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index); $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index);
} }
if($answer_expression instanceof NumberOfResultExpression && !($question instanceof assClozeTest)) if ($answer_expression instanceof NumberOfResultExpression && !($question instanceof assClozeTest)) {
{
$this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_expression->getNumericValue() - 1); $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_expression->getNumericValue() - 1);
} }
...@@ -92,47 +89,37 @@ class CompositeValidator ...@@ -92,47 +89,37 @@ class CompositeValidator
if ($answer_expression instanceof OrderingResultExpression && if ($answer_expression instanceof OrderingResultExpression &&
($question instanceof assOrderingHorizontal || $question instanceof assOrderingQuestion) ($question instanceof assOrderingHorizontal || $question instanceof assOrderingQuestion)
) ) {
{ foreach ($answer_expression->getOrdering() as $order) {
foreach($answer_expression->getOrdering() as $order)
{
$count = 0; $count = 0;
foreach($answer_expression->getOrdering() as $element) foreach ($answer_expression->getOrdering() as $element) {
{ if ($element == $order) {
if($element == $order)
{
$count++; $count++;
} }
} }
if($count > 1) if ($count > 1) {
{
throw new DuplicateElement($order); throw new DuplicateElement($order);
} }
$this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $order - 1); $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $order - 1);
} }
} }
if($question instanceof assClozeTest) if ($question instanceof assClozeTest) {
{
$this->validateClozeTest($answer_index, $question, $answer_expression, $question_index); $this->validateClozeTest($answer_index, $question, $answer_expression, $question_index);
} } elseif (
elseif(
$answer_expression instanceof PercentageResultExpression && $answer_expression instanceof PercentageResultExpression &&
$this->isResultOfAnswerExpression($question_expression) && $this->isResultOfAnswerExpression($question_expression) &&
!($question instanceof assFormulaQuestion) !($question instanceof assFormulaQuestion)
) ) {
{
throw new ExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index . "[" . ($answer_index + 1) . "]"); throw new ExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index . "[" . ($answer_index + 1) . "]");
} }
} } elseif (
elseif(
($composite->nodes[0] instanceof AbstractOperation && ($composite->nodes[0] instanceof AbstractOperation &&
$composite->nodes[1] instanceof ExpressionInterface) || $composite->nodes[1] instanceof ExpressionInterface) ||
($composite->nodes[0] instanceof ExpressionInterface && ($composite->nodes[0] instanceof ExpressionInterface &&
$composite->nodes[1] instanceof AbstractOperation) || $composite->nodes[1] instanceof AbstractOperation) ||
($composite->nodes[0] instanceof SolutionExpressionInterface) ($composite->nodes[0] instanceof SolutionExpressionInterface)
) ) {
{
throw new UnableToParseCondition(""); throw new UnableToParseCondition("");
} }
} }
...@@ -147,63 +134,47 @@ class CompositeValidator ...@@ -147,63 +134,47 @@ class CompositeValidator
*/ */
private function validateClozeTest($answer_index, $question, $answer_expression, $question_index) private function validateClozeTest($answer_index, $question, $answer_expression, $question_index)
{ {
if($answer_index !== null) if ($answer_index !== null) {
{
$options = $question->getAvailableAnswerOptions($answer_index); $options = $question->getAvailableAnswerOptions($answer_index);
$found = false; $found = false;
switch($options->getType()) switch ($options->getType()) {
{
case 0: // text case 0: // text
if ( if (
$answer_expression instanceof StringResultExpression $answer_expression instanceof StringResultExpression
) ) {
{
$found = true; $found = true;
} }
break; break;
case 1: // select case 1: // select
if($answer_expression instanceof StringResultExpression) if ($answer_expression instanceof StringResultExpression) {
{ foreach ($options->getItems(new ilArrayElementShuffler()) as $item) {
require_once './Services/Randomization/classes/class.ilArrayElementShuffler.php'; if ($item->getAnswertext() == $answer_expression->getText()) {
foreach($options->getItems(new ilArrayElementShuffler()) as $item)
{
if($item->getAnswertext() == $answer_expression->getText())
{
$found = true; $found = true;
} }
} }
} } elseif ($answer_expression instanceof NumberOfResultExpression) {
elseif($answer_expression instanceof NumberOfResultExpression) foreach ($options->getItems(new ilArrayElementShuffler()) as $item) {
{ if ($item->getOrder() == $answer_expression->getNumericValue() - 1) {
require_once './Services/Randomization/classes/class.ilArrayElementShuffler.php';
foreach($options->getItems(new ilArrayElementShuffler()) as $item)
{
if($item->getOrder() == $answer_expression->getNumericValue()-1)
{
$found = true; $found = true;
} }
} }
} }
break; break;
case 2: // numeric case 2: // numeric
if($answer_expression instanceof NumericResultExpression) if ($answer_expression instanceof NumericResultExpression) {
{
$found = true; $found = true;
} }
break; break;
} }
if($answer_expression instanceof EmptyAnswerExpression) if ($answer_expression instanceof EmptyAnswerExpression) {
{
$found = true; $found = true;
} }
if(!$found && !($answer_expression instanceof PercentageResultExpression)) if (!$found && !($answer_expression instanceof PercentageResultExpression)) {
{
throw new AnswerValueNotExist($question_index, $answer_expression->getValue(), $answer_index + 1); throw new AnswerValueNotExist($question_index, $answer_expression->getValue(), $answer_index + 1);
} }
} }
} }
...@@ -217,8 +188,7 @@ class CompositeValidator ...@@ -217,8 +188,7 @@ class CompositeValidator
private function checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index) private function checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index)
{ {
$answer_options = $question->getAvailableAnswerOptions($answer_index); $answer_options = $question->getAvailableAnswerOptions($answer_index);
if($answer_options == null) if ($answer_options == null) {
{
throw new AnswerIndexNotExist($question_index, $answer_index + 1); throw new AnswerIndexNotExist($question_index, $answer_index + 1);
} }
} }
...@@ -231,8 +201,7 @@ class CompositeValidator ...@@ -231,8 +201,7 @@ class CompositeValidator
*/ */
private function checkQuestionExists($question, $index) private function checkQuestionExists($question, $index)
{ {
if($question == null) if ($question == null) {
{
throw new QuestionNotExist($index); throw new QuestionNotExist($index);
} }
} }
...@@ -256,8 +225,7 @@ class CompositeValidator ...@@ -256,8 +225,7 @@ class CompositeValidator
*/ */
private function checkAnswerExpressionExist($expressions, $answer_expression, $question_index) private function checkAnswerExpressionExist($expressions, $answer_expression, $question_index)
{ {
if(!in_array($answer_expression::$identifier, $expressions)) if (!in_array($answer_expression::$identifier, $expressions)) {
{
throw new ExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index); throw new ExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index);
} }
} }
...@@ -271,8 +239,7 @@ class CompositeValidator ...@@ -271,8 +239,7 @@ class CompositeValidator
*/ */
private function checkOperatorExistForExpression($operators, $answer_expression, $pattern) private function checkOperatorExistForExpression($operators, $answer_expression, $pattern)
{ {
if(!in_array($pattern, $operators)) if (!in_array($pattern, $operators)) {
{
throw new OperatorNotSupportedByExpression($answer_expression->getValue(), $pattern); throw new OperatorNotSupportedByExpression($answer_expression->getValue(), $pattern);
} }
} }
...@@ -286,24 +253,19 @@ class CompositeValidator ...@@ -286,24 +253,19 @@ class CompositeValidator
private function checkQuestionIsReachable($question, $index) private function checkQuestionIsReachable($question, $index)
{ {
$reachable = false; $reachable = false;
foreach($this->data as $node) foreach ($this->data as $node) {
{ if ($node["node_index"] > $this->node->getIndex()) {
if($node["node_index"] > $this->node->getIndex())
{
break; break;
} }
if($node["question_fi"] == $question->getId()) if ($node["question_fi"] == $question->getId()) {
{
$reachable = true; $reachable = true;
break; break;
} }
} }
if(!$reachable) if (!$reachable) {
{
throw new QuestionNotReachable($index); throw new QuestionNotReachable($index);
} }
} }
} }
\ No newline at end of file
...@@ -11,7 +11,8 @@ require_once "Exception/ConditionParserException.php"; ...@@ -11,7 +11,8 @@ require_once "Exception/ConditionParserException.php";
* Time: 13:54 * Time: 13:54
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ConditionParser { class ConditionParser
{
/** /**
* The condition which should be parsed into a ParserComposite to match a branch condition * The condition which should be parsed into a ParserComposite to match a branch condition
...@@ -53,7 +54,8 @@ class ConditionParser { ...@@ -53,7 +54,8 @@ class ConditionParser {
/** /**
* Construct requirements * Construct requirements
*/ */
public function __construct(){ public function __construct()
{
include_once __DIR__ . '/Factory/ExpressionManufacturer.php'; include_once __DIR__ . '/Factory/ExpressionManufacturer.php';
include_once __DIR__ . '/Factory/OperationManufacturer.php'; include_once __DIR__ . '/Factory/OperationManufacturer.php';
include_once "CompositeBuilder.php"; include_once "CompositeBuilder.php";
...@@ -67,7 +69,8 @@ class ConditionParser { ...@@ -67,7 +69,8 @@ class ConditionParser {
* @see CompositeBuilder::create() * @see CompositeBuilder::create()
* @return array * @return array
*/ */
public function parse($condition){ public function parse($condition)
{
$this->condition = $condition; $this->condition = $condition;
// $this->checkBrackets(); // $this->checkBrackets();
$this->fetchExpressions(); $this->fetchExpressions();
...@@ -86,7 +89,8 @@ class ConditionParser { ...@@ -86,7 +89,8 @@ class ConditionParser {
* @see ExpressionManufacturer::getPattern() * @see ExpressionManufacturer::getPattern()
* @see Parser::$expressions * @see Parser::$expressions
*/ */
protected function fetchExpressions(){ protected function fetchExpressions()
{
$manufacturer = ExpressionManufacturer::_getInstance(); $manufacturer = ExpressionManufacturer::_getInstance();
$this->expressions = $manufacturer->match($this->condition); $this->expressions = $manufacturer->match($this->condition);
} }
...@@ -98,7 +102,8 @@ class ConditionParser { ...@@ -98,7 +102,8 @@ class ConditionParser {
* @see OperationManufacturer::getPattern() * @see OperationManufacturer::getPattern()
* @see Parser::$operators * @see Parser::$operators
*/ */
protected function fetchOperators(){ protected function fetchOperators()
{
$manufacturer = OperationManufacturer::_getInstance(); $manufacturer = OperationManufacturer::_getInstance();
$this->operators = $manufacturer->match($this->condition); $this->operators = $manufacturer->match($this->condition);
} }
...@@ -110,7 +115,8 @@ class ConditionParser { ...@@ -110,7 +115,8 @@ class ConditionParser {
* <br /> * <br />
* (n o n) o (n o n) o n * (n o n) o (n o n) o n
*/ */
protected function cannonicalizeCondition(){ protected function cannonicalizeCondition()
{
$manufacturer = ExpressionManufacturer::_getInstance(); $manufacturer = ExpressionManufacturer::_getInstance();
$this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition); $this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition);
$manufacturer = OperationManufacturer::_getInstance(); $manufacturer = OperationManufacturer::_getInstance();
...@@ -118,10 +124,8 @@ class ConditionParser { ...@@ -118,10 +124,8 @@ class ConditionParser {
$this->condition = preg_replace("/no/", "n o", $this->condition); $this->condition = preg_replace("/no/", "n o", $this->condition);
$this->condition = preg_replace("/on/", "o n", $this->condition); $this->condition = preg_replace("/on/", "o n", $this->condition);
for($i = 0; $i < strlen($this->condition); $i++) for ($i = 0; $i < strlen($this->condition); $i++) {
{ if ($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i)) {
if($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i))
{
$this->surroundNegationExpression($i); $this->surroundNegationExpression($i);
} }
} }
...@@ -132,12 +136,10 @@ class ConditionParser { ...@@ -132,12 +136,10 @@ class ConditionParser {
$num_brackets_open = substr_count($this->condition, "("); $num_brackets_open = substr_count($this->condition, "(");
$num_brackets_close = substr_count($this->condition, ")"); $num_brackets_close = substr_count($this->condition, ")");
if($num_brackets_open > $num_brackets_close) if ($num_brackets_open > $num_brackets_close) {
{
throw new MissingBracket(")"); throw new MissingBracket(")");
} }
if($num_brackets_open < $num_brackets_close) if ($num_brackets_open < $num_brackets_close) {
{
throw new MissingBracket("("); throw new MissingBracket("(");
} }
} }
...@@ -170,44 +172,29 @@ class ConditionParser { ...@@ -170,44 +172,29 @@ class ConditionParser {
$group = array(); $group = array();
$negation = false; $negation = false;
while($this->index < strlen($this->condition)) while ($this->index < strlen($this->condition)) {
{
$a = $this->condition[$this->index]; $a = $this->condition[$this->index];
if(trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected)) if (trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected)) {
{ if ($this->condition[$this->index] == ')') {
if ($this->condition[$this->index] == ')')
{
return $group; return $group;
} } elseif ($this->condition[$this->index] == 'n') {
else if ($this->condition[$this->index] == 'n')
{
$group[] = array('type' => 'expression', 'value' => array_shift($this->expressions)); $group[] = array('type' => 'expression', 'value' => array_shift($this->expressions));
$expected = array("o", ")"); $expected = array("o", ")");
} } elseif ($this->condition[$this->index] == 'o') {
else if ($this->condition[$this->index] == 'o')
{
$group[] = array('type' => 'operator', 'value' => array_shift($this->operators)); $group[] = array('type' => 'operator', 'value' => array_shift($this->operators));
$expected = array("n", "(", "!"); $expected = array("n", "(", "!");
} } elseif ($this->condition[$this->index] == '(') {
else if ($this->condition[$this->index] == '(')
{
$this->index++; $this->index++;
$elements = $this->createNodeArray(); $elements = $this->createNodeArray();
$group[] = array('type' => "group", "negated" => $negation, 'nodes' => $elements); $group[] = array('type' => "group", "negated" => $negation, 'nodes' => $elements);
$negation = false; $negation = false;
$expected = array("o",")"); $expected = array("o",")");
} } elseif ($this->condition[$this->index] == "!") {
else if($this->condition[$this->index] == "!")
{
$negation = true; $negation = true;
} }
} } elseif (trim($this->condition[$this->index]) != "") {
elseif(trim($this->condition[$this->index]) != "")
{
throw new ConditionParserException($this->index - $this->spaces + 1); throw new ConditionParserException($this->index - $this->spaces + 1);
} } else {
else
{
$this->spaces++; $this->spaces++;
} }
...@@ -232,13 +219,11 @@ class ConditionParser { ...@@ -232,13 +219,11 @@ class ConditionParser {
$start = strpos($this->condition, "n", $index + 1); $start = strpos($this->condition, "n", $index + 1);
$end = false; $end = false;
if($start !== false) if ($start !== false) {
{
$end = strpos($this->condition, "n", $start + 1); $end = strpos($this->condition, "n", $start + 1);
} }
if($start !== false && $end !== false) if ($start !== false && $end !== false) {
{
$this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1); $this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1);
} }
} }
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class AnswerIndexNotExist extends \RuntimeException{ class AnswerIndexNotExist extends \RuntimeException
{
/** /**
* @var int * @var int
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class AnswerValueNotExist extends \RuntimeException{ class AnswerValueNotExist extends \RuntimeException
{
/** /**
* @var int * @var int
...@@ -37,8 +38,7 @@ class AnswerValueNotExist extends \RuntimeException{ ...@@ -37,8 +38,7 @@ class AnswerValueNotExist extends \RuntimeException{
$this->value = $value; $this->value = $value;
$message = 'The value "%s" does not exist for the question Q%s[%s]'; $message = 'The value "%s" does not exist for the question Q%s[%s]';
if($this->answer_index === null) if ($this->answer_index === null) {
{
$message = 'The value "%s" does not exist for the question Q%s'; $message = 'The value "%s" does not exist for the question Q%s';
} }
......
...@@ -35,4 +35,3 @@ class ConditionParserException extends \RuntimeException ...@@ -35,4 +35,3 @@ class ConditionParserException extends \RuntimeException
return $this->column; return $this->column;
} }
} }
\ No newline at end of file
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class DuplicateElement extends \RuntimeException{ class DuplicateElement extends \RuntimeException
{
/** /**
* @var string * @var string
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class ExpressionNotSupportedByQuestion extends \RuntimeException{ class ExpressionNotSupportedByQuestion extends \RuntimeException
{
/** /**
* @var string * @var string
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class MissingBracket extends \RuntimeException{ class MissingBracket extends \RuntimeException
{
/** /**
* @var string * @var string
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class OperatorNotSupportedByExpression extends \RuntimeException{ class OperatorNotSupportedByExpression extends \RuntimeException
{
/** /**
* @var string * @var string
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* Time: 15:15 * Time: 15:15
* @author Thomas Joußen <tjoussen@databay.de> * @author Thomas Joußen <tjoussen@databay.de>
*/ */
class QuestionNotExist extends \RuntimeException{ class QuestionNotExist extends \RuntimeException
{
/** /**
* @var int * @var int
......