Add frontend test scoring

This commit is contained in:
Wojtek
2026-10-15 12:22:11 +02:00
parent 270143ad6a
commit 9742c273de
8 changed files with 113 additions and 9 deletions
@@ -23,8 +23,17 @@ use Joomla\CMS\HTML\HTMLHelper;
<div class="alert alert-info"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_SELECT_TEST_SYMBOL'); ?></div>
<?php elseif ($this->symbol !== '') : ?>
<h2><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_TEST_SHEET'); ?>: <?php echo htmlspecialchars($this->symbol, ENT_QUOTES, 'UTF-8'); ?></h2>
<?php if ($this->results) : ?>
<div class="alert alert-success">
<strong><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULT'); ?>:</strong>
<?php echo (int) $this->results['correct']; ?> / <?php echo (int) $this->results['total']; ?>
(<?php echo htmlspecialchars((string) $this->results['percentage'], ENT_QUOTES, 'UTF-8'); ?>%)
</div>
<?php endif; ?>
<?php endif; ?>
<?php if ($this->items) : ?>
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8'); ?>" method="post">
<?php foreach ($this->items as $number => $item) : ?>
<article class="testquestion mb-4">
<h2><?php echo ($number + 1) . '. '; ?><?php echo $item->question; ?></h2>
@@ -37,15 +46,42 @@ use Joomla\CMS\HTML\HTMLHelper;
</p>
<?php if ($item->question_type === 'open') : ?>
<label for="answer-<?php echo (int) $number; ?>"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_ENTER_ANSWER'); ?></label>
<input type="text" id="answer-<?php echo (int) $number; ?>" name="answers[<?php echo (int) $number; ?>]" class="form-control">
<input type="text" id="answer-<?php echo (int) $number; ?>" name="answers[<?php echo (int) $item->id; ?>]" class="form-control" value="<?php echo $this->results ? htmlspecialchars($this->results['items'][$item->id]['selected'], ENT_QUOTES, 'UTF-8') : ''; ?>">
<?php else : ?>
<ol type="A">
<li><?php echo $item->answer_a; ?></li>
<li><?php echo $item->answer_b; ?></li>
<li><?php echo $item->answer_c; ?></li>
<li><?php echo $item->answer_d; ?></li>
<ol type="A" class="list-unstyled">
<?php foreach (['answer_a', 'answer_b', 'answer_c', 'answer_d'] as $answerIndex => $answerField) : ?>
<?php $isSelected = $this->results && $this->results['items'][$item->id]['selected'] === (string) ($answerIndex + 1); ?>
<?php $isCorrectAnswer = $this->results && (string) $item->correct_answer === (string) ($answerIndex + 1); ?>
<li class="mb-2">
<label>
<input type="radio" name="answers[<?php echo (int) $item->id; ?>]" value="<?php echo $answerIndex + 1; ?>" <?php echo $isSelected ? 'checked' : ''; ?>>
<strong><?php echo chr(65 + $answerIndex); ?>.</strong> <?php echo $item->$answerField; ?>
<?php if ($isSelected) : ?>
<span class="badge bg-info"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_SELECTED_ANSWER'); ?></span>
<?php endif; ?>
<?php if ($isCorrectAnswer) : ?>
<span class="badge bg-success"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_CORRECT_ANSWER'); ?></span>
<?php endif; ?>
</label>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if ($this->results) : ?>
<div class="mt-2 <?php echo $this->results['items'][$item->id]['correct'] ? 'text-success' : 'text-danger'; ?>">
<?php echo $this->results['items'][$item->id]['correct'] ? '✓ ' . \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_ANSWER_CORRECT') : '✗ ' . \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_ANSWER_INCORRECT'); ?>
<?php if ($item->question_type === 'closed') : ?>
— <?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_CORRECT_ANSWER'); ?>: <?php echo chr(64 + (int) $item->correct_answer); ?>
<?php else : ?>
— <?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_CORRECT_PATTERN'); ?>: <?php echo htmlspecialchars($item->answer_open, ENT_QUOTES, 'UTF-8'); ?>
<?php endif; ?>
</div>
<?php endif; ?>
</article>
<?php endforeach; ?>
<input type="hidden" name="task" value="check">
<?php echo \Joomla\CMS\HTML\HTMLHelper::_('form.token'); ?>
<button type="submit" class="btn btn-primary"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_FINISH_TEST'); ?></button>
</form>
<?php endif; ?>
</div>