125 lines
4.5 KiB
PHP
125 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Komponent\Component\Testquestions\Site\View\Set;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
|
|
|
class HtmlView extends BaseHtmlView
|
|
{
|
|
protected $set;
|
|
protected $items;
|
|
protected $sets;
|
|
protected $results;
|
|
protected $started;
|
|
|
|
public function display($tpl = null)
|
|
{
|
|
$this->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();
|
|
if ($input->getCmd('task') === 'check' && $input->getMethod() === 'POST' && $this->items) {
|
|
$submittedQuestionOrder = array_map('intval', (array) $input->post->get('question_order', [], 'array'));
|
|
$expectedQuestionIds = array_map(static function ($item) {
|
|
return (int) $item->id;
|
|
}, $this->items);
|
|
$sortedSubmittedOrder = $submittedQuestionOrder;
|
|
$sortedExpectedIds = $expectedQuestionIds;
|
|
sort($sortedSubmittedOrder);
|
|
sort($sortedExpectedIds);
|
|
|
|
if ($sortedSubmittedOrder === $sortedExpectedIds) {
|
|
$itemsById = [];
|
|
foreach ($this->items as $item) {
|
|
$itemsById[(int) $item->id] = $item;
|
|
}
|
|
$this->items = array_values(array_map(static function ($questionId) use ($itemsById) {
|
|
return $itemsById[$questionId];
|
|
}, $submittedQuestionOrder));
|
|
}
|
|
}
|
|
$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');
|
|
$submittedAnswerOrder = (array) $input->post->get('answer_order', [], '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;
|
|
}
|
|
|
|
$answerOrder = array_map('intval', explode(',', (string) ($submittedAnswerOrder[$item->id] ?? '')));
|
|
sort($answerOrder);
|
|
if ($item->question_type !== 'closed' || $answerOrder !== [1, 2, 3, 4]) {
|
|
$answerOrder = [1, 2, 3, 4];
|
|
} else {
|
|
$answerOrder = array_map('intval', explode(',', (string) $submittedAnswerOrder[$item->id]));
|
|
}
|
|
|
|
if ($isCorrect) {
|
|
$correct++;
|
|
}
|
|
|
|
$results[$item->id] = [
|
|
'selected' => $selected,
|
|
'correct' => $isCorrect,
|
|
'points' => (int) ($item->points ?? 0),
|
|
'answer_order' => $answerOrder,
|
|
];
|
|
}
|
|
|
|
return [
|
|
'items' => $results,
|
|
'correct' => $correct,
|
|
'total' => count($items),
|
|
'points_total' => array_sum(array_map(static function ($item) {
|
|
return (int) ($item->points ?? 0);
|
|
}, $items)),
|
|
'percentage' => count($items) ? round(($correct / count($items)) * 100, 2) : 0,
|
|
];
|
|
}
|
|
}
|