Initial Joomla test questions component
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
|
||||
class CategoriesModel extends ListModel
|
||||
{
|
||||
protected function getListQuery()
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
return $db->getQuery(true)
|
||||
->select('*')
|
||||
->from($db->quoteName('#__testquestions_categories'))
|
||||
->order($db->quoteName('ordering') . ' ASC, ' . $db->quoteName('title') . ' ASC');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Model\AdminModel;
|
||||
|
||||
class CategoryModel extends AdminModel
|
||||
{
|
||||
public function getTable($name = 'Category', $prefix = 'Administrator', $config = [])
|
||||
{
|
||||
return $this->getMVCFactory()->createTable($name, $prefix, $config);
|
||||
}
|
||||
|
||||
public function getForm($data = [], $loadData = true)
|
||||
{
|
||||
return $this->loadForm('com_testquestions.category', 'category', ['control' => 'jform', 'load_data' => $loadData]);
|
||||
}
|
||||
|
||||
protected function loadFormData()
|
||||
{
|
||||
return Factory::getApplication()->getUserState('com_testquestions.edit.category.data', []) ?: $this->getItem();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Model;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\MVC\Model\AdminModel;
|
||||
|
||||
class QuestionModel extends AdminModel
|
||||
{
|
||||
public function getTable($name = 'Question', $prefix = 'Administrator', $config = [])
|
||||
{
|
||||
return $this->getMVCFactory()->createTable($name, $prefix, $config);
|
||||
}
|
||||
|
||||
public function getForm($data = [], $loadData = true)
|
||||
{
|
||||
$form = $this->loadForm('com_testquestions.question', 'question', ['control' => 'jform', 'load_data' => $loadData]);
|
||||
|
||||
if (!$form) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
protected function loadFormData()
|
||||
{
|
||||
$data = Factory::getApplication()->getUserState('com_testquestions.edit.question.data', []);
|
||||
|
||||
if (!$data) {
|
||||
$data = $this->getItem();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function prepareTable($table)
|
||||
{
|
||||
if (empty($table->id)) {
|
||||
$table->created = Factory::getDate()->toSql();
|
||||
$table->created_by = Factory::getApplication()->getIdentity()->id;
|
||||
}
|
||||
|
||||
$table->modified = Factory::getDate()->toSql();
|
||||
$table->modified_by = Factory::getApplication()->getIdentity()->id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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'
|
||||
));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user