Add read-only test question preview
This commit is contained in:
@@ -4,7 +4,11 @@ namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Controller\AdminController;
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Session\Session;
|
||||
|
||||
class QuestionsController extends AdminController
|
||||
{
|
||||
@@ -14,4 +18,28 @@ class QuestionsController extends AdminController
|
||||
{
|
||||
return parent::getModel($name, $prefix, $config);
|
||||
}
|
||||
|
||||
public function preview(): void
|
||||
{
|
||||
if (!Session::checkToken()) {
|
||||
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
||||
}
|
||||
|
||||
$user = Factory::getApplication()->getIdentity();
|
||||
if (!$user->authorise('core.edit', 'com_testquestions') && !$user->authorise('core.manage', 'com_testquestions')) {
|
||||
throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403);
|
||||
}
|
||||
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', (array) $this->input->post->get('cid', [], 'array')))));
|
||||
if (!$ids) {
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=questions', false),
|
||||
Text::_('COM_TESTQUESTIONS_PREVIEW_SELECT_QUESTIONS'),
|
||||
'warning'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setRedirect(Route::_('index.php?option=com_testquestions&view=preview&ids=' . implode(',', $ids), false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
|
||||
|
||||
class PreviewModel extends BaseDatabaseModel
|
||||
{
|
||||
public function getItems(): array
|
||||
{
|
||||
$input = Factory::getApplication()->getInput();
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', explode(',', $input->getString('ids', ''))))));
|
||||
|
||||
if (!$ids) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$rows = $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select(['id', 'question_type', 'question', 'answer_a', 'answer_b', 'answer_c', 'answer_d', 'answer_open', 'correct_answer', 'image'])
|
||||
->from($db->quoteName('#__testquestions_questions'))
|
||||
->where($db->quoteName('id') . ' IN (' . implode(',', $ids) . ')')
|
||||
)->loadObjectList('id');
|
||||
|
||||
$items = [];
|
||||
foreach ($ids as $id) {
|
||||
if (isset($rows[$id])) {
|
||||
$items[] = $rows[$id];
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Preview;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||
use Joomla\CMS\Toolbar\ToolbarHelper;
|
||||
|
||||
class HtmlView extends BaseHtmlView
|
||||
{
|
||||
protected $items = [];
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$user = Factory::getApplication()->getIdentity();
|
||||
if (!$user->authorise('core.edit', 'com_testquestions') && !$user->authorise('core.manage', 'com_testquestions')) {
|
||||
throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403);
|
||||
}
|
||||
|
||||
$this->items = $this->get('Items');
|
||||
$this->getDocument()->getWebAssetManager()->registerAndUseStyle(
|
||||
'com_testquestions.exam',
|
||||
'media/com_testquestions/egzamin.css'
|
||||
);
|
||||
ToolbarHelper::title(Text::_('COM_TESTQUESTIONS_PREVIEW_TITLE'));
|
||||
ToolbarHelper::link('index.php?option=com_testquestions&view=questions', 'COM_TESTQUESTIONS', 'arrow-left');
|
||||
|
||||
return parent::display($tpl);
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ class HtmlView extends BaseHtmlView
|
||||
}
|
||||
if ($user->authorise('core.edit', 'com_testquestions')) {
|
||||
ToolbarHelper::editList('question.edit', 'JTOOLBAR_EDIT');
|
||||
ToolbarHelper::custom('questions.preview', 'eye', 'eye', 'COM_TESTQUESTIONS_PREVIEW_SELECTED', true);
|
||||
}
|
||||
if ($user->authorise('core.delete', 'com_testquestions')) {
|
||||
ToolbarHelper::deleteList('', 'questions.delete', 'JTOOLBAR_DELETE');
|
||||
|
||||
Reference in New Issue
Block a user