67 lines
2.8 KiB
PHP
67 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Komponent\Component\Testquestions\Administrator\Model;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\MVC\Model\ListModel;
|
|
|
|
class ResultsModel extends ListModel
|
|
{
|
|
protected function populateState($ordering = 'r.created', $direction = 'desc')
|
|
{
|
|
parent::populateState($ordering, $direction);
|
|
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
|
|
$this->setState('filter.set_id', $this->getUserStateFromRequest($this->context . '.filter.set_id', 'filter_set_id', 0, 'int'));
|
|
$this->setState('filter.date_from', $this->getUserStateFromRequest($this->context . '.filter.date_from', 'filter_date_from', '', 'string'));
|
|
$this->setState('filter.date_to', $this->getUserStateFromRequest($this->context . '.filter.date_to', 'filter_date_to', '', 'string'));
|
|
}
|
|
|
|
protected function getListQuery()
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$query = $db->getQuery(true)
|
|
->select(['r.*', 's.title AS set_title', 'sub.title AS subject_title'])
|
|
->from($db->quoteName('#__testquestions_results', 'r'))
|
|
->leftJoin($db->quoteName('#__testquestions_sets', 's') . ' ON s.id = r.set_id')
|
|
->leftJoin($db->quoteName('#__testquestions_subjects', 'sub') . ' ON sub.id = s.subject_id');
|
|
|
|
$search = trim((string) $this->getState('filter.search'));
|
|
if ($search !== '') {
|
|
$needle = $db->quote('%' . $db->escape($search, true) . '%', false);
|
|
$query->where('(r.participant_name LIKE ' . $needle . ' OR s.title LIKE ' . $needle . ')');
|
|
}
|
|
|
|
$setId = (int) $this->getState('filter.set_id');
|
|
if ($setId > 0) {
|
|
$query->where('r.set_id = ' . $setId);
|
|
}
|
|
|
|
$dateFrom = (string) $this->getState('filter.date_from');
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateFrom)) {
|
|
$query->where('r.created >= ' . $db->quote($dateFrom . ' 00:00:00'));
|
|
}
|
|
|
|
$dateTo = (string) $this->getState('filter.date_to');
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateTo)) {
|
|
$query->where('r.created <= ' . $db->quote($dateTo . ' 23:59:59'));
|
|
}
|
|
|
|
return $query->order('r.created DESC, r.id DESC');
|
|
}
|
|
|
|
public function getSets(): array
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
|
|
return $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select(['s.id', 's.title', 's.published', 'sub.title AS subject_title'])
|
|
->from($db->quoteName('#__testquestions_sets', 's'))
|
|
->leftJoin($db->quoteName('#__testquestions_subjects', 'sub') . ' ON sub.id = s.subject_id')
|
|
->order('sub.title ASC, s.title ASC, s.id ASC')
|
|
)->loadObjectList();
|
|
}
|
|
}
|