Add multi-variant test sets
This commit is contained in:
@@ -25,6 +25,14 @@ class SetModel extends AdminModel
|
||||
|
||||
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'))
|
||||
@@ -41,7 +49,32 @@ class SetModel extends AdminModel
|
||||
|
||||
protected function loadFormData()
|
||||
{
|
||||
return Factory::getApplication()->getUserState('com_testquestions.edit.set.data', []) ?: $this->getItem();
|
||||
$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()
|
||||
@@ -69,12 +102,41 @@ class SetModel extends AdminModel
|
||||
)->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;
|
||||
}
|
||||
@@ -111,12 +173,28 @@ class SetModel extends AdminModel
|
||||
$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;
|
||||
|
||||
@@ -21,9 +21,10 @@ class SetsModel extends ListModel
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$query = $db->getQuery(true)
|
||||
->select(['s.*', 'sub.title AS subject_title', 'COUNT(sq.question_id) AS question_count'])
|
||||
->select(['s.*', 'sub.title AS subject_title', 'p.title AS parent_title', 'COUNT(sq.question_id) AS question_count'])
|
||||
->from($db->quoteName('#__testquestions_sets', 's'))
|
||||
->leftJoin($db->quoteName('#__testquestions_subjects', 'sub') . ' ON sub.id = s.subject_id')
|
||||
->leftJoin($db->quoteName('#__testquestions_sets', 'p') . ' ON p.id = s.parent_id')
|
||||
->leftJoin($db->quoteName('#__testquestions_set_questions', 'sq') . ' ON sq.set_id = s.id')
|
||||
->group('s.id');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user