Add category filter to set question selection
This commit is contained in:
@@ -8,6 +8,5 @@
|
||||
<option value="1">JPUBLISHED</option>
|
||||
<option value="0">JUNPUBLISHED</option>
|
||||
</field>
|
||||
<field name="question_ids" type="sql" label="COM_TESTQUESTIONS_SET_QUESTIONS" multiple="true" layout="joomla.form.field.list-fancy-select" query="SELECT id, question FROM #__testquestions_questions ORDER BY ordering ASC, id ASC" key_field="id" value_field="question" translate_query="false" />
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
@@ -53,3 +53,6 @@ COM_TESTQUESTIONS_SET_DESCRIPTION="Opis zestawu"
|
||||
COM_TESTQUESTIONS_SET_QUESTIONS="Pytania w zestawie"
|
||||
COM_TESTQUESTIONS_SET_QUESTION_COUNT="Liczba pytań"
|
||||
COM_TESTQUESTIONS_SET_SEARCH="Szukaj zestawów..."
|
||||
COM_TESTQUESTIONS_SET_CATEGORY_FILTER="Filtruj pytania według kategorii"
|
||||
COM_TESTQUESTIONS_SET_ALL_CATEGORIES="Wszystkie kategorie"
|
||||
COM_TESTQUESTIONS_UNCATEGORIZED="Bez kategorii"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
-- No database changes required for version 1.9.1.
|
||||
@@ -44,6 +44,31 @@ class SetModel extends AdminModel
|
||||
return Factory::getApplication()->getUserState('com_testquestions.edit.set.data', []) ?: $this->getItem();
|
||||
}
|
||||
|
||||
public function getQuestions()
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
|
||||
return $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select(['q.id', 'q.question', 'q.category_id', 'c.title AS category_title'])
|
||||
->from($db->quoteName('#__testquestions_questions', 'q'))
|
||||
->leftJoin($db->quoteName('#__testquestions_categories', 'c') . ' ON c.id = q.category_id')
|
||||
->order('c.title ASC, q.ordering ASC, q.id ASC')
|
||||
)->loadObjectList();
|
||||
}
|
||||
|
||||
public function getCategories()
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
|
||||
return $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select(['id', 'title'])
|
||||
->from($db->quoteName('#__testquestions_categories'))
|
||||
->order($db->quoteName('title') . ' ASC')
|
||||
)->loadObjectList();
|
||||
}
|
||||
|
||||
public function save($data)
|
||||
{
|
||||
$questionIds = array_values(array_unique(array_filter(array_map('intval', (array) ($data['question_ids'] ?? [])))));
|
||||
|
||||
@@ -12,11 +12,15 @@ class HtmlView extends BaseHtmlView
|
||||
{
|
||||
protected $form;
|
||||
protected $item;
|
||||
protected $questions;
|
||||
protected $categories;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
$this->form = $this->get('Form');
|
||||
$this->item = $this->get('Item');
|
||||
$this->questions = $this->get('Questions');
|
||||
$this->categories = $this->get('Categories');
|
||||
|
||||
if (count($errors = $this->get('Errors'))) {
|
||||
throw new \RuntimeException(implode("\n", $errors));
|
||||
|
||||
@@ -12,8 +12,46 @@ HTMLHelper::_('behavior.formvalidator');
|
||||
<?php echo $this->form->renderField('title'); ?>
|
||||
<?php echo $this->form->renderField('description'); ?>
|
||||
<?php echo $this->form->renderField('published'); ?>
|
||||
<?php echo $this->form->renderField('question_ids'); ?>
|
||||
<div class="mb-3">
|
||||
<label for="set-question-category" class="form-label"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_SET_CATEGORY_FILTER'); ?></label>
|
||||
<select id="set-question-category" class="form-select">
|
||||
<option value=""><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_SET_ALL_CATEGORIES'); ?></option>
|
||||
<?php foreach ($this->categories as $category) : ?>
|
||||
<option value="<?php echo (int) $category->id; ?>"><?php echo $this->escape($category->title); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<fieldset class="mb-3">
|
||||
<legend><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_SET_QUESTIONS'); ?></legend>
|
||||
<div id="set-question-list" class="border rounded p-3">
|
||||
<?php foreach ($this->questions as $question) : ?>
|
||||
<div class="form-check set-question-row" data-category="<?php echo (int) $question->category_id; ?>">
|
||||
<input class="form-check-input" type="checkbox" name="jform[question_ids][]" value="<?php echo (int) $question->id; ?>" id="set-question-<?php echo (int) $question->id; ?>" <?php echo in_array((int) $question->id, (array) $this->item->question_ids, true) ? 'checked' : ''; ?>>
|
||||
<label class="form-check-label" for="set-question-<?php echo (int) $question->id; ?>">
|
||||
<span class="badge bg-secondary"><?php echo $this->escape($question->category_title ?: \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_UNCATEGORIZED')); ?></span>
|
||||
<?php echo $this->escape(trim(strip_tags($question->question))); ?>
|
||||
</label>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</fieldset>
|
||||
</fieldset>
|
||||
<input type="hidden" name="task" value="">
|
||||
<?php echo HTMLHelper::_('form.token'); ?>
|
||||
</form>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const filter = document.getElementById('set-question-category');
|
||||
const rows = document.querySelectorAll('.set-question-row');
|
||||
|
||||
if (!filter) {
|
||||
return;
|
||||
}
|
||||
|
||||
filter.addEventListener('change', function () {
|
||||
rows.forEach(function (row) {
|
||||
row.classList.toggle('d-none', filter.value !== '' && row.dataset.category !== filter.value);
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user