Skip to content

Commit

Permalink
refactor: move common logic to wirisstep
Browse files Browse the repository at this point in the history
  • Loading branch information
Pol Torrent i Soler committed Mar 13, 2024
1 parent 3905042 commit 246dbb2
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ public function grade_response(array $response) {
$answer = $this->get_matching_answer($response);
if ($answer) {
$fraction = $answer->fraction;

// Multiply Moodle fraction by quizzes grade (due to custom function
// grading or compound grade distribution).
$grade = $this->step->get_var('_matching_answer_grade');

if (empty($grade)) {
$responsehash = md5($response['answer']);
$grade = $this->step->get_var('_' . substr($responsehash, 0, 6) . '_matching_answer_grade');
}
$grade = $this->step->get_var_in_answer_cache('_matching_answer_grade', $response['answer']);

if (!empty($grade)) {
$fraction = $fraction * $grade;
Expand Down Expand Up @@ -160,16 +156,11 @@ public function get_matching_answer(array $response) {
}

// Optimization in order to avoid a service call.
$responsehash = md5($response['answer']);
$cachedresponses = $this->step->get_var('_response_hash') ?? '';

if (str_contains($cachedresponses, $responsehash)) {
$matchinganswer = $this->step->get_var('_matching_answer');

if (empty($matchinganswer)) {
$matchinganswer = $this->step->get_var('_' . substr($responsehash, 0, 6) . '_matching_answer');
}
$answer = $response['answer'];
$responsehash = md5($answer);

if ($this->step->is_answer_cached($answer)) {
$matchinganswer = $this->step->get_var_in_answer_cache('_matching_answer', $answer);
if (!empty($matchinganswer)) {
return $this->base->answers[$matchinganswer];
} else if (!is_null($matchinganswer)) {
Expand Down

0 comments on commit 246dbb2

Please sign in to comment.