From 998fb69e443b18cbdefa247633170ea5fba82a04 Mon Sep 17 00:00:00 2001 From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de> Date: Tue, 10 Dec 2024 17:52:53 +0100 Subject: [PATCH] accept responses with the same text but diffent id, fixes #277 --- exercises/seq_exercise.php | 55 +++++++++++++++++++----- views/exercises/seq_exercise/correct.php | 6 +-- views/exercises/seq_exercise/print.php | 6 +-- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/exercises/seq_exercise.php b/exercises/seq_exercise.php index d253c91..6676f80 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 ed0c92e..38d1283 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 0f4befa..0be8323 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> -- GitLab