Initial Joomla test questions component
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\AdminController;
|
||||
|
||||
class CategoriesController extends AdminController
|
||||
{
|
||||
protected $text_prefix = 'COM_TESTQUESTIONS';
|
||||
|
||||
public function getModel($name = 'Category', $prefix = 'Administrator', $config = ['ignore_request' => true])
|
||||
{
|
||||
return parent::getModel($name, $prefix, $config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\FormController;
|
||||
|
||||
class CategoryController extends FormController
|
||||
{
|
||||
protected $view_item = 'category';
|
||||
protected $view_list = 'categories';
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\BaseController;
|
||||
|
||||
class DisplayController extends BaseController
|
||||
{
|
||||
protected $default_view = 'questions';
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\FormController;
|
||||
|
||||
class QuestionController extends FormController
|
||||
{
|
||||
protected $view_item = 'question';
|
||||
protected $view_list = 'questions';
|
||||
|
||||
public function cancel($key = null)
|
||||
{
|
||||
$this->setRedirect('index.php?option=com_testquestions&view=questions');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\AdminController;
|
||||
|
||||
class QuestionsController extends AdminController
|
||||
{
|
||||
protected $text_prefix = 'COM_TESTQUESTIONS';
|
||||
|
||||
public function getModel($name = 'Question', $prefix = 'Administrator', $config = ['ignore_request' => true])
|
||||
{
|
||||
return parent::getModel($name, $prefix, $config);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Table;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Table\Table;
|
||||
use Joomla\Database\DatabaseDriver;
|
||||
|
||||
class CategoryTable extends Table
|
||||
{
|
||||
public function __construct(DatabaseDriver $db)
|
||||
{
|
||||
parent::__construct('#__testquestions_categories', 'id', $db);
|
||||
}
|
||||
|
||||
public function check($updateNulls = false): bool
|
||||
{
|
||||
$this->title = trim((string) $this->title);
|
||||
return $this->title !== '' && parent::check($updateNulls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\Table;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Table\Table;
|
||||
use Joomla\Database\DatabaseDriver;
|
||||
|
||||
class QuestionTable extends Table
|
||||
{
|
||||
public function __construct(DatabaseDriver $db)
|
||||
{
|
||||
parent::__construct('#__testquestions_questions', 'id', $db);
|
||||
}
|
||||
|
||||
public function check($updateNulls = false): bool
|
||||
{
|
||||
$this->category_id = (int) $this->category_id ?: 1;
|
||||
$this->time_limit = (int) $this->time_limit;
|
||||
$this->points = (int) $this->points;
|
||||
|
||||
if (!in_array($this->time_limit, [30, 45, 60, 90], true) || $this->points < 1 || $this->points > 9) {
|
||||
$this->setError('COM_TESTQUESTIONS_ERROR_META');
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->question_type = $this->question_type ?: 'closed';
|
||||
|
||||
if (!in_array($this->question_type, ['closed', 'open'], true)) {
|
||||
$this->setError('COM_TESTQUESTIONS_ERROR_QUESTION_TYPE');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->question_type === 'open') {
|
||||
if (trim((string) $this->answer_open) === '') {
|
||||
$this->setError('COM_TESTQUESTIONS_ERROR_OPEN_PATTERN');
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->correct_answer = 0;
|
||||
} else {
|
||||
foreach (['answer_a', 'answer_b', 'answer_c', 'answer_d'] as $answer) {
|
||||
if (trim((string) $this->$answer) === '') {
|
||||
$this->setError('COM_TESTQUESTIONS_ERROR_ALL_ANSWERS');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->correct_answer = (int) $this->correct_answer;
|
||||
|
||||
if ($this->question_type === 'closed' && ($this->correct_answer < 1 || $this->correct_answer > 4)) {
|
||||
$this->setError('COM_TESTQUESTIONS_ERROR_CORRECT_ANSWER');
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::check($updateNulls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Categories;
|
||||
|
||||
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 $items;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$this->items = $this->get('Items');
|
||||
ToolbarHelper::title(Text::_('COM_TESTQUESTIONS_CATEGORIES'));
|
||||
ToolbarHelper::addNew('category.add', 'JTOOLBAR_NEW');
|
||||
ToolbarHelper::editList('category.edit', 'JTOOLBAR_EDIT');
|
||||
ToolbarHelper::deleteList('', 'categories.delete', 'JTOOLBAR_DELETE');
|
||||
ToolbarHelper::link('index.php?option=com_testquestions&view=questions', 'COM_TESTQUESTIONS', 'back');
|
||||
return parent::display($tpl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Category;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
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');
|
||||
ToolbarHelper::save('category.save', 'JTOOLBAR_SAVE');
|
||||
ToolbarHelper::link(
|
||||
'index.php?option=com_testquestions&view=questions',
|
||||
'JTOOLBAR_CANCEL',
|
||||
'cancel'
|
||||
);
|
||||
return parent::display($tpl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Question;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||
use Joomla\CMS\Language\Text;
|
||||
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');
|
||||
$this->addToolbar();
|
||||
return parent::display($tpl);
|
||||
}
|
||||
|
||||
protected function addToolbar()
|
||||
{
|
||||
$this->getDocument()->setTitle($this->item->id
|
||||
? \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_EDIT_QUESTION')
|
||||
: \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_NEW_QUESTION'));
|
||||
ToolbarHelper::apply('question.apply', 'JTOOLBAR_APPLY');
|
||||
ToolbarHelper::save('question.save', 'JTOOLBAR_SAVE');
|
||||
ToolbarHelper::link(
|
||||
'index.php?option=com_testquestions&view=questions',
|
||||
'JTOOLBAR_CANCEL',
|
||||
'cancel'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Komponent\Component\Testquestions\Administrator\View\Questions;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Toolbar\ToolbarHelper;
|
||||
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||
|
||||
class HtmlView extends BaseHtmlView
|
||||
{
|
||||
protected $items;
|
||||
protected $pagination;
|
||||
protected $state;
|
||||
protected $categories;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$this->items = $this->get('Items');
|
||||
$this->pagination = $this->get('Pagination');
|
||||
$this->state = $this->get('State');
|
||||
$this->categories = $this->get('Categories');
|
||||
|
||||
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'));
|
||||
|
||||
if ($user->authorise('core.create', 'com_testquestions')) {
|
||||
ToolbarHelper::addNew('question.add', 'JTOOLBAR_NEW');
|
||||
}
|
||||
if ($user->authorise('core.edit', 'com_testquestions')) {
|
||||
ToolbarHelper::editList('question.edit', 'JTOOLBAR_EDIT');
|
||||
}
|
||||
if ($user->authorise('core.delete', 'com_testquestions')) {
|
||||
ToolbarHelper::deleteList('', 'questions.delete', 'JTOOLBAR_DELETE');
|
||||
}
|
||||
|
||||
ToolbarHelper::link(
|
||||
'index.php?option=com_testquestions&view=categories',
|
||||
'COM_TESTQUESTIONS_MANAGE_CATEGORIES',
|
||||
'list'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user