Add question set management
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?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');
|
||||
$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()
|
||||
{
|
||||
return Factory::getApplication()->getUserState('com_testquestions.edit.set.data', []) ?: $this->getItem();
|
||||
}
|
||||
|
||||
public function save($data)
|
||||
{
|
||||
$questionIds = array_values(array_unique(array_filter(array_map('intval', (array) ($data['question_ids'] ?? [])))));
|
||||
unset($data['question_ids']);
|
||||
|
||||
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) {
|
||||
$db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->delete($db->quoteName('#__testquestions_set_questions'))
|
||||
->where($db->quoteName('set_id') . ' IN (' . implode(',', $ids) . ')')
|
||||
)->execute();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user