203 lines
7.2 KiB
PHP
203 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace Komponent\Component\Testquestions\Administrator\Model;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\MVC\Model\AdminModel;
|
|
|
|
class SetModel extends AdminModel
|
|
{
|
|
public function getTable($name = 'Set', $prefix = 'Administrator', $config = [])
|
|
{
|
|
return $this->getMVCFactory()->createTable($name, $prefix, $config);
|
|
}
|
|
|
|
public function getForm($data = [], $loadData = true)
|
|
{
|
|
return $this->loadForm('com_testquestions.set', 'set', ['control' => 'jform', 'load_data' => $loadData]);
|
|
}
|
|
|
|
public function getItem($pk = null)
|
|
{
|
|
$item = parent::getItem($pk);
|
|
|
|
if ($item && !empty($item->id)) {
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
if ((int) $item->parent_id > 0) {
|
|
$item->parent_title = (string) $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select($db->quoteName('title'))
|
|
->from($db->quoteName('#__testquestions_sets'))
|
|
->where($db->quoteName('id') . ' = ' . (int) $item->parent_id)
|
|
)->loadResult();
|
|
}
|
|
$item->question_ids = $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select($db->quoteName('question_id'))
|
|
->from($db->quoteName('#__testquestions_set_questions'))
|
|
->where($db->quoteName('set_id') . ' = ' . (int) $item->id)
|
|
->order($db->quoteName('ordering') . ' ASC')
|
|
)->loadColumn();
|
|
} else {
|
|
$item->question_ids = [];
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
protected function loadFormData()
|
|
{
|
|
$data = Factory::getApplication()->getUserState('com_testquestions.edit.set.data', []) ?: $this->getItem();
|
|
$parentId = Factory::getApplication()->getInput()->getInt('parent_id');
|
|
|
|
$hasId = is_array($data) ? !empty($data['id']) : !empty($data->id);
|
|
|
|
if ($parentId > 0 && !$hasId) {
|
|
$parent = $this->getItem($parentId);
|
|
$variantCount = count($this->getVariants($parentId));
|
|
|
|
if ($parent) {
|
|
$data = [
|
|
'parent_id' => $parentId,
|
|
'set_type' => 'variant',
|
|
'title' => $parent->title . ' — wariant ' . ($variantCount + 1),
|
|
'subject_id' => $parent->subject_id,
|
|
'duration' => $parent->duration,
|
|
'test_content' => $parent->test_content,
|
|
'image' => $parent->image,
|
|
'description' => $parent->description,
|
|
'published' => $parent->published,
|
|
'question_ids' => [],
|
|
];
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getQuestions()
|
|
{
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
|
|
return $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select(['q.id', 'q.question', 'q.question_type', 'q.answer_a', 'q.answer_b', 'q.answer_c', 'q.answer_d', 'q.answer_open', 'q.correct_answer', '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 getVariants($parentId = null)
|
|
{
|
|
$parentId = (int) ($parentId ?: Factory::getApplication()->getInput()->getInt('id'));
|
|
|
|
if ($parentId < 1) {
|
|
return [];
|
|
}
|
|
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
|
|
return $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select(['id', 'title', 'published'])
|
|
->from($db->quoteName('#__testquestions_sets'))
|
|
->where($db->quoteName('parent_id') . ' = ' . $parentId)
|
|
->order($db->quoteName('ordering') . ' ASC, ' . $db->quoteName('id') . ' ASC')
|
|
)->loadObjectList();
|
|
}
|
|
|
|
public function save($data)
|
|
{
|
|
$formData = Factory::getApplication()->getInput()->post->get('jform', [], 'array');
|
|
$questionIds = array_values(array_unique(array_filter(array_map('intval', (array) ($formData['question_ids'] ?? $data['question_ids'] ?? [])))));
|
|
unset($data['question_ids']);
|
|
|
|
$parentId = (int) ($formData['parent_id'] ?? $data['parent_id'] ?? 0);
|
|
$parentId = max(0, $parentId);
|
|
$data['parent_id'] = $parentId;
|
|
|
|
if ($parentId > 0) {
|
|
$data['set_type'] = 'variant';
|
|
} elseif (!in_array(($data['set_type'] ?? 'ordinary'), ['ordinary', 'multi'], true)) {
|
|
$data['set_type'] = 'ordinary';
|
|
}
|
|
|
|
if (!parent::save($data)) {
|
|
return false;
|
|
}
|
|
|
|
$setId = (int) $this->getState($this->getName() . '.id');
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$db->setQuery(
|
|
$db->getQuery(true)
|
|
->delete($db->quoteName('#__testquestions_set_questions'))
|
|
->where($db->quoteName('set_id') . ' = ' . $setId)
|
|
)->execute();
|
|
|
|
if ($questionIds) {
|
|
$query = $db->getQuery(true)
|
|
->insert($db->quoteName('#__testquestions_set_questions'))
|
|
->columns([$db->quoteName('set_id'), $db->quoteName('question_id'), $db->quoteName('ordering')]);
|
|
|
|
foreach ($questionIds as $ordering => $questionId) {
|
|
$query->values(implode(',', [$setId, $questionId, $ordering]));
|
|
}
|
|
|
|
$db->setQuery($query)->execute();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function delete(&$pks)
|
|
{
|
|
if (!parent::delete($pks)) {
|
|
return false;
|
|
}
|
|
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$ids = array_map('intval', (array) $pks);
|
|
|
|
if ($ids) {
|
|
$childIds = $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select($db->quoteName('id'))
|
|
->from($db->quoteName('#__testquestions_sets'))
|
|
->where($db->quoteName('parent_id') . ' IN (' . implode(',', $ids) . ')')
|
|
)->loadColumn();
|
|
$ids = array_values(array_unique(array_merge($ids, array_map('intval', $childIds))));
|
|
}
|
|
|
|
if ($ids) {
|
|
$db->setQuery(
|
|
$db->getQuery(true)
|
|
->delete($db->quoteName('#__testquestions_set_questions'))
|
|
->where($db->quoteName('set_id') . ' IN (' . implode(',', $ids) . ')')
|
|
)->execute();
|
|
|
|
$db->setQuery(
|
|
$db->getQuery(true)
|
|
->delete($db->quoteName('#__testquestions_sets'))
|
|
->where($db->quoteName('id') . ' IN (' . implode(',', $ids) . ')')
|
|
)->execute();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|