Persist test results

This commit is contained in:
Wojtek
2026-10-15 20:43:52 +02:00
parent 3eabeebf6b
commit ceb6a6c6ef
7 changed files with 68 additions and 2 deletions
@@ -70,4 +70,29 @@ class SetModel extends BaseDatabaseModel
->order('sub.title ASC, s.title ASC')
)->loadObjectList();
}
public function saveResult(string $participantName, int $extraTime, array $result): bool
{
$db = Factory::getContainer()->get('DatabaseDriver');
$setId = $this->getSetId();
$user = Factory::getApplication()->getIdentity();
if ($setId < 1 || trim($participantName) === '') {
return false;
}
$row = (object) [
'set_id' => $setId,
'participant_name' => trim($participantName),
'extra_time' => max(0, $extraTime),
'correct_count' => (int) ($result['correct'] ?? 0),
'total_count' => (int) ($result['total'] ?? 0),
'percentage' => (float) ($result['percentage'] ?? 0),
'answers' => json_encode($result['items'] ?? [], JSON_UNESCAPED_UNICODE),
'created' => Factory::getDate()->toSql(),
'created_by' => $user ? (int) $user->id : 0,
];
return (bool) $db->insertObject('#__testquestions_results', $row);
}
}
@@ -30,6 +30,14 @@ class HtmlView extends BaseHtmlView
&& $input->post->getInt('set_id', 0) > 0;
$this->results = $this->buildResults($this->items);
if ($this->results) {
$this->getModel()->saveResult(
$input->post->getString('participant_name', ''),
$input->post->getInt('extra_time', 0),
$this->results
);
}
return parent::display($tpl);
}