student();
}
if ($isTeacher) {
$this->teacher();
}
if (!$isStudent && !$isTeacher) {
e107::getRender()->tablerender('TimeKeeper', USER ? 'Your account has no TimeKeeper role (student or teacher).' : 'Please log in to use TimeKeeper.');
}
}
/**
* Run the sequence for teachers
*/
private function teacher(): void {
}
/**
* Run the sequence for students
*/
private function student(): void {
$user = e107::getUser()->getLoginName();
$this->checkAnswer($user);
$this->getScores($user);
$this->showMessage();
// If the last question has not been answered ask again.
if ($this->getLastQuestion()) {
return;
}
$this->getSubject();
$this->getQuestion();
}
/**
* Check if the last question was answered and check the answer
* @param string $user
*/
private function checkAnswer($user): void {
$subject = filter_input(INPUT_POST, 'subject');
if (is_string($subject) && $subject !== '') {
require_once __DIR__ . '/answerChecker2.php';
$ac = new answerChecker2();
$ac->checkQuestionSubject($subject, $user);
$db = e107::getDb();
$db->delete('tkLastQuestion', "user = '" . $db->escape($user) . "'");
}
}
/**
* Show the last question asked if it hasn't been answered.
* @return bool True when a question was shown.
*/
private function getLastQuestion(): bool {
$db = e107::getDb();
$lastQuestion = $db->retrieve("SELECT LastQuestionHtml FROM `" . MPREFIX . "tkLastQuestion` WHERE user = '" . $db->escape($this->data['user']) . "' ORDER BY id DESC LIMIT 1");
if (!empty($lastQuestion)) {
e107::getRender()->tablerender('Last Question', base64_decode($lastQuestion));
return true;
}
return false;
}
/**
* Get the question based on selected subject
* @return void
*/
private function getQuestion(): void {
switch ($this->data['selectedSubject']) {
case 'numbers':
require_once __DIR__ . '/questionGenerators/math/numbers2.php';
$questionGen = new numbers2($this->data['user']);
$questionGen->getNumbers();
break;
case 'addition':
case 'subtraction':
case 'multiplication':
case 'division':
case 'rounding':
require_once __DIR__ . '/questionGenerators/math/numeracy2.php';
$questionGen = new numeracy2($this->data['user'], $this->data['selectedSubject']);
$questionGen->getNumeracy();
break;
case 'worded_add':
case 'worded_sub':
case 'worded_mult':
case 'worded_devi':
case 'spelling':
require_once __DIR__ . '/questionGenerators/english/spelling2.php';
$questionGen = new spelling2($this->data['user']);
$questionGen->getSpellingWord();
break;
case 'anagrams':
require_once __DIR__ . '/questionGenerators/english/anagrams.php';
$questionGen = new anagrams($this->data['user']);
$questionGen->getanagram();
break;
case 'elements':
require_once __DIR__ . '/questionGenerators/science/elements2.php';
$questionGen = new elements2($this->data['user']);
$questionGen->getElements();
break;
case 'generalKnowledge':
case 'continents':
case 'countries':
case 'flags':
case 'capitalCities':
case 'artists':
case 'malaySpelling':
case 'nameBodyParts':
case 'nameBodyOrgans':
case 'basicFirstAid':
default:
// Not built yet (or unknown): none of these are in the random pool.
e107::getRender()->tablerender('TimeKeeper', 'This subject is not available yet. Please refresh the page.');
break;
}
}
/**
* Get the list of available subjects and select a random one.
* @return void
*/
private function getSubject(): void {
$usableSubjects = [];
foreach ($this->subjectsList as $subjects) {
foreach ($subjects as $subject) {
if ($subject[1] === 1 && $this->data['difficulty'] >= $subject[2]) {
$usableSubjects[] = $subject[0];
}
}
}
if (empty($usableSubjects)) {
$usableSubjects[] = 'numbers';
}
$this->data['selectedSubject'] = $usableSubjects[mt_rand(0, count($usableSubjects) - 1)];
}
/**
* Show an initial stats message to the user.
* @return void
*/
private function showMessage(): void {
$text = "You have answered " . (int) $this->data['cntr'] . " questions this month.
"
. "Your accuacy for the month is " . htmlspecialchars((string) $this->data['percent']) . "%.
"
. "Your difficulty is set at " . htmlspecialchars((string) $this->data['difficulty']) . ".
";
e107::getRender()->tablerender('Your stats ' . htmlspecialchars(ucfirst($this->data['user'])), $text);
}
/**
* Get the initial values needed to generate a sane question.
* @param string $user
* @return void
*/
private function getScores($user): void {
$db = e107::getDb();
$u = $db->escape($user);
$returnArray = [];
$returnArray['totalQuestionCount'] = (int) $db->count('tkReport', '(id)', "username = '{$u}'");
$db->gen("SELECT COUNT(id) AS cntr, CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
FROM `" . MPREFIX . "tkReport` WHERE username = '{$u}'
AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE() - INTERVAL 1 MONTH)
AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
$row = $db->fetch();
$returnArray['cntr'] = is_array($row) ? (int) $row['cntr'] : 0;
$returnArray['percent'] = (is_array($row) && $row['percent'] !== null) ? (float) $row['percent'] : 0;
$returnArray['user'] = $user;
$returnArray['difficulty'] = $returnArray['totalQuestionCount'] * $returnArray['percent'];
$this->data = $returnArray;
}
}