getDocument()->getWebAssetManager()->registerAndUseStyle( 'com_testquestions.exam', 'media/com_testquestions/egzamin.css' ); $this->set = $this->get('Set'); $this->items = $this->get('Items'); $this->sets = $this->get('Sets'); $input = Factory::getApplication()->getInput(); $this->started = $input->getMethod() === 'POST' && $input->post->getString('participant_name', '') !== '' && $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), $input->post->getInt('test_started_at', 0), $this->results ); } return parent::display($tpl); } protected function buildResults(array $items) { $input = Factory::getApplication()->getInput(); if ($input->getCmd('task') !== 'check' || $input->getMethod() !== 'POST') { return null; } $answers = (array) $input->post->get('answers', [], 'array'); $results = []; $correct = 0; foreach ($items as $item) { $selected = isset($answers[$item->id]) ? trim((string) $answers[$item->id]) : ''; $isCorrect = false; if ($item->question_type === 'open') { $patterns = preg_split('/\s*\|\s*/', (string) $item->answer_open, -1, PREG_SPLIT_NO_EMPTY); foreach ($patterns as $pattern) { if (mb_strtolower(trim($pattern), 'UTF-8') === mb_strtolower($selected, 'UTF-8')) { $isCorrect = true; break; } } } else { $isCorrect = (string) $item->correct_answer === $selected; } if ($isCorrect) { $correct++; } $results[$item->id] = [ 'selected' => $selected, 'correct' => $isCorrect, 'points' => (int) ($item->points ?? 0), ]; } return [ 'items' => $results, 'correct' => $correct, 'total' => count($items), 'percentage' => count($items) ? round(($correct / count($items)) * 100, 2) : 0, ]; } }