Add question set management
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\FormController;
|
||||
|
||||
class SetController extends FormController
|
||||
{
|
||||
protected $view_item = 'set';
|
||||
protected $view_list = 'sets';
|
||||
|
||||
public function cancel($key = null)
|
||||
{
|
||||
$this->setRedirect('index.php?option=com_testquestions&view=sets');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\AdminController;
|
||||
|
||||
class SetsController extends AdminController
|
||||
{
|
||||
protected $text_prefix = 'COM_TESTQUESTIONS';
|
||||
|
||||
public function getModel($name = 'Set', $prefix = 'Administrator', $config = ['ignore_request' => true])
|
||||
{
|
||||
return parent::getModel($name, $prefix, $config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Model\AdminModel;
|
||||
|
||||
class SetModel extends AdminModel
|
||||
{
|
||||
public function getTable($name = 'Set', $prefix = 'Administrator', $config = [])
|
||||
{
|
||||
return $this->getMVCFactory()->createTable($name, $prefix, $config);
|
||||
}
|
||||
|
||||
public function getForm($data = [], $loadData = true)
|
||||
{
|
||||
return $this->loadForm('com_testquestions.set', 'set', ['control' => 'jform', 'load_data' => $loadData]);
|
||||
}
|
||||
|
||||
public function getItem($pk = null)
|
||||
{
|
||||
$item = parent::getItem($pk);
|
||||
|
||||
if ($item && !empty($item->id)) {
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$item->question_ids = $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select($db->quoteName('question_id'))
|
||||
->from($db->quoteName('#__testquestions_set_questions'))
|
||||
->where($db->quoteName('set_id') . ' = ' . (int) $item->id)
|
||||
->order($db->quoteName('ordering') . ' ASC')
|
||||
)->loadColumn();
|
||||
} else {
|
||||
$item->question_ids = [];
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
protected function loadFormData()
|
||||
{
|
||||
return Factory::getApplication()->getUserState('com_testquestions.edit.set.data', []) ?: $this->getItem();
|
||||
}
|
||||
|
||||
public function save($data)
|
||||
{
|
||||
$questionIds = array_values(array_unique(array_filter(array_map('intval', (array) ($data['question_ids'] ?? [])))));
|
||||
unset($data['question_ids']);
|
||||
|
||||
if (!parent::save($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$setId = (int) $this->getState($this->getName() . '.id');
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->delete($db->quoteName('#__testquestions_set_questions'))
|
||||
->where($db->quoteName('set_id') . ' = ' . $setId)
|
||||
)->execute();
|
||||
|
||||
if ($questionIds) {
|
||||
$query = $db->getQuery(true)
|
||||
->insert($db->quoteName('#__testquestions_set_questions'))
|
||||
->columns([$db->quoteName('set_id'), $db->quoteName('question_id'), $db->quoteName('ordering')]);
|
||||
|
||||
foreach ($questionIds as $ordering => $questionId) {
|
||||
$query->values(implode(',', [$setId, $questionId, $ordering]));
|
||||
}
|
||||
|
||||
$db->setQuery($query)->execute();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete(&$pks)
|
||||
{
|
||||
if (!parent::delete($pks)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$ids = array_map('intval', (array) $pks);
|
||||
|
||||
if ($ids) {
|
||||
$db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->delete($db->quoteName('#__testquestions_set_questions'))
|
||||
->where($db->quoteName('set_id') . ' IN (' . implode(',', $ids) . ')')
|
||||
)->execute();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
|
||||
class SetsModel extends ListModel
|
||||
{
|
||||
protected function populateState($ordering = 's.ordering', $direction = 'asc')
|
||||
{
|
||||
parent::populateState($ordering, $direction);
|
||||
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
|
||||
$this->setState('filter.published', $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '', 'string'));
|
||||
}
|
||||
|
||||
protected function getListQuery()
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$query = $db->getQuery(true)
|
||||
->select(['s.*', 'COUNT(sq.question_id) AS question_count'])
|
||||
->from($db->quoteName('#__testquestions_sets', 's'))
|
||||
->leftJoin($db->quoteName('#__testquestions_set_questions', 'sq') . ' ON sq.set_id = s.id')
|
||||
->group('s.id');
|
||||
|
||||
$search = trim((string) $this->getState('filter.search'));
|
||||
if ($search !== '') {
|
||||
$query->where('s.title LIKE ' . $db->quote('%' . $db->escape($search, true) . '%', false));
|
||||
}
|
||||
|
||||
$published = $this->getState('filter.published');
|
||||
if ($published !== '' && $published !== null) {
|
||||
$query->where('s.published = ' . (int) $published);
|
||||
}
|
||||
|
||||
return $query->order('s.ordering ASC, s.title ASC');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Table;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Table\Table;
|
||||
use Joomla\Database\DatabaseDriver;
|
||||
|
||||
class SetTable extends Table
|
||||
{
|
||||
public function __construct(DatabaseDriver $db)
|
||||
{
|
||||
parent::__construct('#__testquestions_sets', 'id', $db);
|
||||
}
|
||||
|
||||
public function check($updateNulls = false): bool
|
||||
{
|
||||
$this->title = trim((string) $this->title);
|
||||
|
||||
return $this->title !== '' && parent::check($updateNulls);
|
||||
}
|
||||
}
|
||||
@@ -51,5 +51,11 @@ class HtmlView extends BaseHtmlView
|
||||
'COM_TESTQUESTIONS_MANAGE_CATEGORIES',
|
||||
'list'
|
||||
);
|
||||
|
||||
ToolbarHelper::link(
|
||||
'index.php?option=com_testquestions&view=sets',
|
||||
'COM_TESTQUESTIONS_SETS',
|
||||
'list'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Set;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||
use Joomla\CMS\Toolbar\ToolbarHelper;
|
||||
|
||||
class HtmlView extends BaseHtmlView
|
||||
{
|
||||
protected $form;
|
||||
protected $item;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$this->form = $this->get('Form');
|
||||
$this->item = $this->get('Item');
|
||||
|
||||
if (count($errors = $this->get('Errors'))) {
|
||||
throw new \RuntimeException(implode("\n", $errors));
|
||||
}
|
||||
|
||||
$this->addToolbar();
|
||||
|
||||
return parent::display($tpl);
|
||||
}
|
||||
|
||||
protected function addToolbar()
|
||||
{
|
||||
ToolbarHelper::title($this->item->id ? Text::_('COM_TESTQUESTIONS_EDIT_SET') : Text::_('COM_TESTQUESTIONS_NEW_SET'));
|
||||
ToolbarHelper::apply('set.apply', 'JTOOLBAR_APPLY');
|
||||
ToolbarHelper::save('set.save', 'JTOOLBAR_SAVE');
|
||||
ToolbarHelper::cancel('set.cancel', 'JTOOLBAR_CANCEL');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Sets;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||
use Joomla\CMS\Toolbar\ToolbarHelper;
|
||||
|
||||
class HtmlView extends BaseHtmlView
|
||||
{
|
||||
protected $items;
|
||||
protected $pagination;
|
||||
protected $state;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$this->items = $this->get('Items');
|
||||
$this->pagination = $this->get('Pagination');
|
||||
$this->state = $this->get('State');
|
||||
|
||||
if (count($errors = $this->get('Errors'))) {
|
||||
throw new \RuntimeException(implode("\n", $errors));
|
||||
}
|
||||
|
||||
$this->addToolbar();
|
||||
|
||||
return parent::display($tpl);
|
||||
}
|
||||
|
||||
protected function addToolbar()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
$this->getDocument()->setTitle(Text::_('COM_TESTQUESTIONS_SETS'));
|
||||
|
||||
if ($user->authorise('core.create', 'com_testquestions')) {
|
||||
ToolbarHelper::addNew('set.add', 'JTOOLBAR_NEW');
|
||||
}
|
||||
if ($user->authorise('core.edit', 'com_testquestions')) {
|
||||
ToolbarHelper::editList('set.edit', 'JTOOLBAR_EDIT');
|
||||
}
|
||||
if ($user->authorise('core.delete', 'com_testquestions')) {
|
||||
ToolbarHelper::deleteList('', 'sets.delete', 'JTOOLBAR_DELETE');
|
||||
}
|
||||
|
||||
ToolbarHelper::link(
|
||||
'index.php?option=com_testquestions&view=questions',
|
||||
'COM_TESTQUESTIONS_QUESTIONS',
|
||||
'list'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user