Files
System-testowy/administrator/components/com_testquestions/src/Model/QuestionsModel.php
T

66 lines
2.6 KiB
PHP

<?php
namespace Komponent\Component\Testquestions\Administrator\Model;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
class QuestionsModel extends ListModel
{
protected function populateState($ordering = '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'
));
$this->setState('filter.category_id', $this->getUserStateFromRequest(
$this->context . '.filter.category_id', 'filter_category_id', '', 'int'
));
$this->setState('filter.show_answers', $this->getUserStateFromRequest(
$this->context . '.filter.show_answers', 'filter_show_answers', '0', 'int'
));
}
protected function getListQuery()
{
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true)
->select('q.*, c.title AS category_title')
->from($db->quoteName('#__testquestions_questions', 'q'))
->leftJoin($db->quoteName('#__testquestions_categories', 'c') . ' ON c.id = q.category_id');
$search = trim((string) $this->getState('filter.search'));
$published = $this->getState('filter.published');
if ($search !== '') {
$search = '%' . $db->escape($search, true) . '%';
$query->where($db->quoteName('q.question') . ' LIKE ' . $db->quote($search, false));
}
if ($published !== '' && $published !== null) {
$query->where($db->quoteName('q.published') . ' = ' . (int) $published);
}
if ((int) $this->getState('filter.category_id') > 0) {
$query->where('q.category_id = ' . (int) $this->getState('filter.category_id'));
}
return $query->order($db->quoteName('q.' . $this->getState('list.ordering', 'ordering')) . ' '
. $db->escape($this->getState('list.direction', 'ASC')));
}
public function getCategories()
{
$db = Factory::getContainer()->get('DatabaseDriver');
return $db->setQuery(
$db->getQuery(true)
->select(['id', 'title'])
->from($db->quoteName('#__testquestions_categories'))
->where($db->quoteName('published') . ' = 1')
->order($db->quoteName('ordering') . ' ASC, ' . $db->quoteName('title') . ' ASC')
)->loadObjectList();
}
}