Initial Joomla test questions component
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user