diff --git a/exercises/seq_exercise.php b/exercises/seq_exercise.php index d253c9152571a3b6b34285ed6ffb442807d3b4c8..6676f8037bf362651943f089e61fabb539b8a369 100644 --- a/exercises/seq_exercise.php +++ b/exercises/seq_exercise.php @@ -106,6 +106,44 @@ class seq_exercise extends Exercise return $answers; } + /** + * Check if this answer is a correct assignment for the given id. + */ + public function isCorrectAnswer($answer, $id) + { + if ($answer['id'] == $id) { + return true; + } + + foreach ($this->task['answers'] as $_answer) { + if ($_answer['id'] == $id) { + if ($answer['text'] === $_answer['text']) { + return true; + } + } + } + + return false; + } + + /** + * Check if this sequence of answers is a correct assignment. + */ + public function isCorrectAnswerSequence($answer1, $answer2) + { + $match = false; + + foreach ($this->task['answers'] as $answer) { + if ($match && $answer2['text'] === $answer['text']) { + return true; + } + + $match = $answer1['text'] === $answer['text']; + } + + return false; + } + /** * Evaluates a student's solution for the individual items in this * exercise. Returns an array of ('points' => float, 'safe' => boolean). @@ -119,21 +157,16 @@ class seq_exercise extends Exercise $response = $solution->response; $item_count = $this->itemCount(); $answers = $this->task['answers']; - $pos = array_flip($response); + + foreach ($answers as $answer) { + $answer_by_id[$answer['id']] = $answer; + } for ($i = 0; $i < $item_count; ++$i) { if ($this->task['compare'] === 'sequence') { - if ($pos[$answers[$i]['id']] + 1 == $pos[$answers[$i + 1]['id']]) { - $points = 1; - } else { - $points = 0; - } + $points = $this->isCorrectAnswerSequence($answer_by_id[$response[$i]], $answer_by_id[$response[$i + 1]]) ? 1 : 0; } else { - if ($pos[$answers[$i]['id']] == $i) { - $points = 1; - } else { - $points = 0; - } + $points = $this->isCorrectAnswer($answers[$i], $response[$i]) ? 1 : 0; } if (!$this->task['compare'] && count($result)) { diff --git a/views/exercises/seq_exercise/correct.php b/views/exercises/seq_exercise/correct.php index ed0c92e7668c6bb450bdc61c4a579ebaff82e8ef..38d128309bbaf9c749783f8416c53b8de962e29f 100644 --- a/views/exercises/seq_exercise/correct.php +++ b/views/exercises/seq_exercise/correct.php @@ -17,15 +17,15 @@ <tr style="vertical-align: top;"> <td> <? if ($response): ?> - <? foreach ($response as $n => $id): ?> - <? foreach ($exercise->task['answers'] as $i => $answer): ?> + <? foreach ($response as $i => $id): ?> + <? foreach ($exercise->task['answers'] as $answer): ?> <? if ($answer['id'] === $id): ?> <? if ($exercise->task['compare'] === 'sequence'): ?> <div class="neutral_item"> <?= formatReady($answer['text']) ?> </div> - <? if ($n + 1 < count($response)): ?> + <? if ($i + 1 < count($response)): ?> <div class="correction_marker sequence"> <? if ($results[$i]['points'] == 1): ?> <span style="color: green;">}</span> diff --git a/views/exercises/seq_exercise/print.php b/views/exercises/seq_exercise/print.php index 0f4befa360a5a753d7e128df256391ebf1840a12..0be8323471fd3ce757aab59e7920725d17bf790b 100644 --- a/views/exercises/seq_exercise/print.php +++ b/views/exercises/seq_exercise/print.php @@ -18,15 +18,15 @@ <td> <ol> <? if ($response): ?> - <? foreach ($response as $n => $id): ?> - <? foreach ($exercise->task['answers'] as $i => $answer): ?> + <? foreach ($response as $i => $id): ?> + <? foreach ($exercise->task['answers'] as $answer): ?> <? if ($answer['id'] === $id): ?> <? if ($exercise->task['compare'] === 'sequence'): ?> <li class="neutral_item"> <?= formatReady($answer['text']) ?> </li> - <? if ($print_correction && $n + 1 < count($response)): ?> + <? if ($print_correction && $i + 1 < count($response)): ?> <div class="correction_marker sequence"> <? if ($results[$i]['points'] == 1): ?> <span style="color: green;">}</span>