Add per-question result assessment fields

This commit is contained in:
Wojtek
2026-10-15 21:07:44 +02:00
parent 2af8f7c878
commit f3efb81af4
7 changed files with 85 additions and 3 deletions
@@ -33,7 +33,7 @@ class ResultModel extends BaseDatabaseModel
if ($ids) {
$item->questions = $db->setQuery(
$db->getQuery(true)
->select(['id', 'question_type', 'question', 'answer_a', 'answer_b', 'answer_c', 'answer_d', 'answer_open', 'correct_answer'])
->select(['id', 'question_type', 'question', 'answer_a', 'answer_b', 'answer_c', 'answer_d', 'answer_open', 'correct_answer', 'points'])
->from($db->quoteName('#__testquestions_questions'))
->where('id IN (' . implode(',', $ids) . ')')
)->loadObjectList('id');
@@ -41,4 +41,28 @@ class ResultModel extends BaseDatabaseModel
return $item;
}
public function saveAssessment(int $id, array $awardedPoints, array $comments): bool
{
$item = $this->getItem($id);
if (!$item) {
return false;
}
foreach ($item->answers as $questionId => &$answer) {
$key = (string) $questionId;
$answer['awarded_points'] = max(0, (int) ($awardedPoints[$key] ?? 0));
$answer['comment'] = trim((string) ($comments[$key] ?? ''));
}
unset($answer);
$db = Factory::getContainer()->get('DatabaseDriver');
$row = (object) [
'id' => $id,
'answers' => json_encode($item->answers, JSON_UNESCAPED_UNICODE),
];
return (bool) $db->updateObject('#__testquestions_results', $row, 'id');
}
}