public function checkInput()
 {
     $error = false;
     $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_post']));
     $_POST['gap'] = ilUtil::stripSlashesRecursive($_POST['gap']);
     $gaps_used_in_combination = array();
     if (array_key_exists('gap_combination', $_POST)) {
         $_POST['gap_combination'] = ilUtil::stripSlashesRecursive($_POST['gap_combination']);
         $_POST['gap_combination_values'] = ilUtil::stripSlashesRecursive($_POST['gap_combination_values']);
         $gap_with_points = array();
         for ($i = 0; $i < count($_POST['gap_combination']['select']); $i++) {
             foreach ($_POST['gap_combination']['select'][$i] as $key => $item) {
                 if ($item == 'none_selected_minus_one') {
                     return false;
                 }
                 $gaps_used_in_combination[$item] = $item;
                 $check_points_for_best_scoring = false;
                 foreach ($_POST['gap_combination_values'][$i] as $index => $answeritems) {
                     foreach ($answeritems as $answeritem) {
                         if ($answeritem == 'none_selected_minus_one') {
                             return false;
                         }
                     }
                     $points = $_POST['gap_combination']['points'][$i][$index];
                     if ($points > 0) {
                         $check_points_for_best_scoring = true;
                     }
                 }
                 if (!$check_points_for_best_scoring) {
                     return false;
                 }
             }
         }
     }
     if (isset($_POST['gap']) && is_array($_POST['gap'])) {
         foreach ($_POST['gap'] as $key => $item) {
             $_POST['clozetype_' . $key] = ilUtil::stripSlashes($_POST['clozetype_' . $key]);
             $getType = $_POST['clozetype_' . $key];
             $gapsize = $_POST['gap_' . $key . '_gapsize'];
             $json[0][$key]->text_field_length = $gapsize > 0 ? $gapsize : '';
             if ($getType == CLOZE_TEXT || $getType == CLOZE_SELECT) {
                 $_POST['gap_' . $key] = ilUtil::stripSlashesRecursive($_POST['gap_' . $key]);
                 $gapText = $_POST['gap_' . $key];
                 foreach ($gapText['answer'] as $row => $answer) {
                     if (!isset($answer) || $answer == '') {
                         $error = true;
                     }
                 }
                 $points_sum = 0;
                 if (array_key_exists('points', $gapText)) {
                     foreach ($gapText['points'] as $row => $points) {
                         if (isset($points) && $points != '' && is_numeric($points)) {
                             $points_sum += $points;
                         } else {
                             $error = true;
                         }
                     }
                     if (is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
                         $points_sum += $gap_with_points[$key];
                     }
                     if ($points_sum == 0) {
                         if (!array_key_exists($key, $gaps_used_in_combination)) {
                             $error = true;
                         }
                     }
                     if ($getType == CLOZE_SELECT) {
                         $_POST['shuffle_' . $key] = ilUtil::stripSlashes($_POST['shuffle_' . $key]);
                         if (!isset($_POST['shuffle_' . $key])) {
                             $error = true;
                         }
                     }
                 } else {
                     $error = true;
                 }
             }
             if ($getType == CLOZE_NUMERIC) {
                 $_POST['gap_' . $key . 'numeric'] = ilUtil::stripSlashes($_POST['gap_' . $key . 'numeric'], FALSE);
                 $_POST['gap_' . $key . 'numeric_lower'] = ilUtil::stripSlashes($_POST['gap_' . $key . 'numeric_lower'], FALSE);
                 $_POST['gap_' . $key . 'numeric_upper'] = ilUtil::stripSlashes($_POST['gap_' . $key . 'numeric_upper'], FALSE);
                 $_POST['gap_' . $key . 'numeric_points'] = ilUtil::stripSlashes($_POST['gap_' . $key . 'numeric_points']);
                 $mark_errors = array('answer' => false, 'lower' => false, 'upper' => false, 'points' => false);
                 $eval = new EvalMath();
                 $eval->suppress_errors = true;
                 $formula = $_POST['gap_' . $key . '_numeric'];
                 $result = $eval->e(str_replace(',', '.', $_POST['gap_' . $key . '_numeric'], $formula));
                 if ($result === false) {
                     $error = true;
                 }
                 $lower = $_POST['gap_' . $key . '_numeric_lower'];
                 $assClozeTestObject = new assClozeTest();
                 $has_valid_chars = $assClozeTestObject->checkForValidFormula($lower);
                 $result = $eval->e(str_replace(',', '.', $lower), FALSE);
                 if ($result === false || !$has_valid_chars) {
                     $error = true;
                 }
                 $_POST['gap_' . $key . '_numeric_lower'] = $result;
                 $result = $eval->e(str_replace(',', '.', $_POST['gap_' . $key . '_numeric_upper']), FALSE);
                 if ($result === false) {
                     $error = true;
                 }
                 $_POST['gap_' . $key . '_numeric_upper'] = $result;
                 $points = $_POST['gap_' . $key . '_numeric_points'];
                 if (is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
                     $points += $gap_with_points[$key];
                 }
                 if (!isset($points) || $points == '' || !is_numeric($points) || $points == 0) {
                     if (!array_key_exists($key, $gaps_used_in_combination)) {
                         $error = true;
                     }
                 }
                 $json[0][$key]->values[0]->error = $mark_errors;
             }
         }
     }
     $_POST['gap_json_post'] = json_encode($json);
     return !$error;
 }
Beispiel #2
0
 public function test_setGetFixedTextLength_shouldReturnValueUnchanged()
 {
     // Arrange
     require_once './Modules/TestQuestionPool/classes/class.assClozeTest.php';
     $instance = new assClozeTest();
     $expected = 123;
     $instance->setFixedTextLength($expected);
     $actual = $instance->getFixedTextLength();
     $this->assertEquals($expected, $actual);
 }
 /**
  * Calculate points
  *
  * This function calculates the points for a given answer.
  * Better would be to re-use from T&A here in the future.
  * When this code has been written this has not been possible yet.
  *
  * @param
  * @return
  */
 static function calculatePoints($a_type, $a_id, $a_choice)
 {
     global $ilLog;
     switch ($a_type) {
         case "assSingleChoice":
             include_once "./Modules/TestQuestionPool/classes/class.assSingleChoice.php";
             $q = new assSingleChoice();
             $q->loadFromDb($a_id);
             $points = 0;
             foreach ($q->getAnswers() as $key => $answer) {
                 if (isset($a_choice[0]) && $key == $a_choice[0]) {
                     $points += $answer->getPoints();
                 }
             }
             break;
         case "assMultipleChoice":
             include_once "./Modules/TestQuestionPool/classes/class.assMultipleChoice.php";
             $q = new assMultipleChoice();
             $q->loadFromDb($a_id);
             $points = 0;
             foreach ($q->getAnswers() as $key => $answer) {
                 if (is_array($a_choice) && in_array($key, $a_choice)) {
                     $points += $answer->getPoints();
                 } else {
                     $points += $answer->getPointsUnchecked();
                 }
             }
             break;
         case "assClozeTest":
             include_once "./Modules/TestQuestionPool/classes/class.assClozeTest.php";
             $q = new assClozeTest();
             $q->loadFromDb($a_id);
             $points = 0;
             foreach ($q->getGaps() as $id => $gap) {
                 $choice = $a_choice[$id];
                 switch ($gap->getType()) {
                     case CLOZE_TEXT:
                         $gappoints = 0;
                         for ($order = 0; $order < $gap->getItemCount(); $order++) {
                             $answer = $gap->getItem($order);
                             $gotpoints = $q->getTextgapPoints($answer->getAnswertext(), $choice, $answer->getPoints());
                             if ($gotpoints > $gappoints) {
                                 $gappoints = $gotpoints;
                             }
                         }
                         $points += $gappoints;
                         //$ilLog->write("ct: ".$gappoints);
                         break;
                     case CLOZE_NUMERIC:
                         $gappoints = 0;
                         for ($order = 0; $order < $gap->getItemCount(); $order++) {
                             $answer = $gap->getItem($order);
                             $gotpoints = $q->getNumericgapPoints($answer->getAnswertext(), $choice, $answer->getPoints(), $answer->getLowerBound(), $answer->getUpperBound());
                             if ($gotpoints > $gappoints) {
                                 $gappoints = $gotpoints;
                             }
                         }
                         $points += $gappoints;
                         //$ilLog->write("cn: ".$gappoints);
                         break;
                     case CLOZE_SELECT:
                         for ($order = 0; $order < $gap->getItemCount(); $order++) {
                             $answer = $gap->getItem($order);
                             if ($choice == $answer->getOrder()) {
                                 $answerpoints = $answer->getPoints();
                                 $points += $answerpoints;
                                 //$ilLog->write("cs: ".$answerpoints);
                             }
                         }
                         break;
                 }
             }
             break;
         case "assMatchingQuestion":
             include_once "./Modules/TestQuestionPool/classes/class.assMatchingQuestion.php";
             $q = new assMatchingQuestion();
             $q->loadFromDb($a_id);
             $points = 0;
             for ($i = 0; $i < $q->getMatchingPairCount(); $i++) {
                 $pair = $q->getMatchingPair($i);
                 if (is_array($a_choice) && in_array($pair->definition->identifier . "-" . $pair->term->identifier, $a_choice)) {
                     $points += $pair->points;
                 }
             }
             break;
         case "assOrderingQuestion":
             include_once "./Modules/TestQuestionPool/classes/class.assOrderingQuestion.php";
             $q = new assOrderingQuestion();
             $q->loadFromDb($a_id);
             $points = 0;
             $cnt = 1;
             $right = true;
             foreach ($q->getAnswers() as $answer) {
                 if ($a_choice[$cnt - 1] != $cnt) {
                     $right = false;
                 }
                 $cnt++;
             }
             if ($right) {
                 $points = $q->getPoints();
             }
             break;
         case "assImagemapQuestion":
             include_once "./Modules/TestQuestionPool/classes/class.assImagemapQuestion.php";
             $q = new assImagemapQuestion();
             $q->loadFromDb($a_id);
             $points = 0;
             foreach ($q->getAnswers() as $key => $answer) {
                 if (is_array($a_choice) && in_array($key, $a_choice)) {
                     $points += $answer->getPoints();
                 }
             }
             break;
     }
     if ($points < 0) {
         $points = 0;
     }
     return (int) $points;
 }