From ceb6a6c6efe0ba4964028907c15fbe7eee52dc93 Mon Sep 17 00:00:00 2001 From: Wojtek Date: Thu, 15 Oct 2026 20:43:52 +0200 Subject: [PATCH] Persist test results --- README.md | 3 ++- .../sql/install.mysql.utf8mb4.sql | 16 ++++++++++++ .../sql/uninstall.mysql.utf8mb4.sql | 1 + .../sql/updates/mysql/1.17.0.sql | 15 +++++++++++ com_testquestions.xml | 2 +- .../com_testquestions/src/Model/SetModel.php | 25 +++++++++++++++++++ .../src/View/Set/HtmlView.php | 8 ++++++ 7 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 administrator/components/com_testquestions/sql/updates/mysql/1.17.0.sql diff --git a/README.md b/README.md index e7d3cb2..61d7164 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych. - frontendowy widok testu na podstawie wybranego zestawu pytań, - dwuetapowe rozpoczęcie testu z loginem użytkownika i możliwością przedłużenia czasu, - zegar odliczający czas testu i automatyczne zakończenie po jego upływie, +- zapisywanie wyników testów i zaznaczonych odpowiedzi w bazie danych, - pozycję menu „Arkusz egzaminacyjny” z opcjonalnym symbolem arkusza, - listę wyboru dostępnych symboli arkuszy na stronie klienckiej, - możliwość rozwiązania arkusza i wyświetlenia wyniku oraz odpowiedzi poprawnych i zaznaczonych, @@ -27,6 +28,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych. ## Instalacja -W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.16.3). +W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.17.0). Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**. diff --git a/administrator/components/com_testquestions/sql/install.mysql.utf8mb4.sql b/administrator/components/com_testquestions/sql/install.mysql.utf8mb4.sql index a58240f..67dfcc0 100644 --- a/administrator/components/com_testquestions/sql/install.mysql.utf8mb4.sql +++ b/administrator/components/com_testquestions/sql/install.mysql.utf8mb4.sql @@ -73,3 +73,19 @@ CREATE TABLE IF NOT EXISTS `#__testquestions_set_questions` ( PRIMARY KEY (`set_id`, `question_id`), KEY `idx_question_id` (`question_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS `#__testquestions_results` ( + `id` int unsigned NOT NULL AUTO_INCREMENT, + `set_id` int unsigned NOT NULL, + `participant_name` varchar(255) NOT NULL DEFAULT '', + `extra_time` smallint unsigned NOT NULL DEFAULT 0, + `correct_count` smallint unsigned NOT NULL DEFAULT 0, + `total_count` smallint unsigned NOT NULL DEFAULT 0, + `percentage` decimal(5,2) NOT NULL DEFAULT 0.00, + `answers` longtext NULL, + `created` datetime NOT NULL, + `created_by` int unsigned NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + KEY `idx_set_id` (`set_id`), + KEY `idx_created` (`created`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci; diff --git a/administrator/components/com_testquestions/sql/uninstall.mysql.utf8mb4.sql b/administrator/components/com_testquestions/sql/uninstall.mysql.utf8mb4.sql index b555188..a741a02 100644 --- a/administrator/components/com_testquestions/sql/uninstall.mysql.utf8mb4.sql +++ b/administrator/components/com_testquestions/sql/uninstall.mysql.utf8mb4.sql @@ -1,4 +1,5 @@ DROP TABLE IF EXISTS `#__testquestions_set_questions`; DROP TABLE IF EXISTS `#__testquestions_sets`; DROP TABLE IF EXISTS `#__testquestions_subjects`; +DROP TABLE IF EXISTS `#__testquestions_results`; DROP TABLE IF EXISTS `#__testquestions_questions`; diff --git a/administrator/components/com_testquestions/sql/updates/mysql/1.17.0.sql b/administrator/components/com_testquestions/sql/updates/mysql/1.17.0.sql new file mode 100644 index 0000000..ba88844 --- /dev/null +++ b/administrator/components/com_testquestions/sql/updates/mysql/1.17.0.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS `#__testquestions_results` ( + `id` int unsigned NOT NULL AUTO_INCREMENT, + `set_id` int unsigned NOT NULL, + `participant_name` varchar(255) NOT NULL DEFAULT '', + `extra_time` smallint unsigned NOT NULL DEFAULT 0, + `correct_count` smallint unsigned NOT NULL DEFAULT 0, + `total_count` smallint unsigned NOT NULL DEFAULT 0, + `percentage` decimal(5,2) NOT NULL DEFAULT 0.00, + `answers` longtext NULL, + `created` datetime NOT NULL, + `created_by` int unsigned NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + KEY `idx_set_id` (`set_id`), + KEY `idx_created` (`created`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci; diff --git a/com_testquestions.xml b/com_testquestions.xml index 6b22f79..167088a 100644 --- a/com_testquestions.xml +++ b/com_testquestions.xml @@ -4,7 +4,7 @@ 2026-10-14 Komponent GNU General Public License version 2 or later - 1.16.3 + 1.17.0 COM_TESTQUESTIONS_DESCRIPTION com_testquestions Komponent\Component\Testquestions diff --git a/components/com_testquestions/src/Model/SetModel.php b/components/com_testquestions/src/Model/SetModel.php index 8fdca1b..e2bf57b 100644 --- a/components/com_testquestions/src/Model/SetModel.php +++ b/components/com_testquestions/src/Model/SetModel.php @@ -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); + } } diff --git a/components/com_testquestions/src/View/Set/HtmlView.php b/components/com_testquestions/src/View/Set/HtmlView.php index 45a5774..37727b0 100644 --- a/components/com_testquestions/src/View/Set/HtmlView.php +++ b/components/com_testquestions/src/View/Set/HtmlView.php @@ -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); }