Add per-question result assessment fields
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Controller\BaseController;
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Session\Session;
|
||||
|
||||
class ResultController extends BaseController
|
||||
{
|
||||
public function save(): void
|
||||
{
|
||||
if (!Session::checkToken()) {
|
||||
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
||||
}
|
||||
|
||||
$input = Factory::getApplication()->getInput();
|
||||
$id = $input->post->getInt('id');
|
||||
$points = (array) $input->post->get('awarded_points', [], 'array');
|
||||
$comments = (array) $input->post->get('comments', [], 'array');
|
||||
$model = $this->getModel('Result');
|
||||
|
||||
if (!$model->saveAssessment($id, $points, $comments)) {
|
||||
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_RESULT_NOT_FOUND'));
|
||||
}
|
||||
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=result&id=' . $id, false),
|
||||
Text::_('COM_TESTQUESTIONS_RESULT_SAVED')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class ResultModel extends BaseDatabaseModel
|
||||
if ($ids) {
|
||||
$item->questions = $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select(['id', 'question_type', 'question', 'answer_a', 'answer_b', 'answer_c', 'answer_d', 'answer_open', 'correct_answer'])
|
||||
->select(['id', 'question_type', 'question', 'answer_a', 'answer_b', 'answer_c', 'answer_d', 'answer_open', 'correct_answer', 'points'])
|
||||
->from($db->quoteName('#__testquestions_questions'))
|
||||
->where('id IN (' . implode(',', $ids) . ')')
|
||||
)->loadObjectList('id');
|
||||
@@ -41,4 +41,28 @@ class ResultModel extends BaseDatabaseModel
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function saveAssessment(int $id, array $awardedPoints, array $comments): bool
|
||||
{
|
||||
$item = $this->getItem($id);
|
||||
|
||||
if (!$item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($item->answers as $questionId => &$answer) {
|
||||
$key = (string) $questionId;
|
||||
$answer['awarded_points'] = max(0, (int) ($awardedPoints[$key] ?? 0));
|
||||
$answer['comment'] = trim((string) ($comments[$key] ?? ''));
|
||||
}
|
||||
unset($answer);
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$row = (object) [
|
||||
'id' => $id,
|
||||
'answers' => json_encode($item->answers, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
|
||||
return (bool) $db->updateObject('#__testquestions_results', $row, 'id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user