SpreadQuiz/class/Task.php
2025-09-30 22:44:06 +02:00

144 lines
3.1 KiB
PHP

<?php
class Task implements JsonSerializable
{
protected string $type; // task type
protected string $question; // the task title
protected mixed $player_answer; // answer given by the player
protected mixed $correct_answer;
protected float $max_mark; // maximum points that can be collected at this task
protected bool $is_template; // this task is a template
protected array $flags; // task flags
function __construct(string $type, array &$a = null)
{
$this->type = $type;
$this->is_template = $a["is_template"] ?? false;
$this->max_mark = $a["max_mark"] ?? 1.0;
$this->question = $a["question"] ?? "";
$this->flags = $a["flags"] ?? [];
$this->player_answer = $a["player_answer"] ?? null;
$this->correct_answer = $a["correct_answer"] ?? null;
}
function setQuestion(string $question): void
{
$this->question = $question;
}
function getQuestion(): string
{
return $this->question;
}
// save answer
function saveAnswer(mixed $ans): bool
{
$this->player_answer = $ans;
return true;
}
// clear answer
function clearAnswer(): void
{
$this->player_answer = "";
}
// set task type
function setType(string $type): void {
$this->type = $type;
}
// get task type
function getType(): string
{
return $this->type;
}
function setMaxMark(float $max_mark): void
{
$this->max_mark = $max_mark;
}
function getMaxMark(): float
{
return $this->max_mark;
}
function getMark(): float
{
return 1.0;
}
function toArray(): array
{
$a = [
"type" => $this->type,
"question" => $this->question,
"max_mark" => $this->max_mark,
"is_template" => $this->is_template,
"flags" => $this->flags,
"correct_answer" => $this->correct_answer,
];
if (!$this->isTemplate()) {
$a["player_answer"] = $this->player_answer;
}
return $a;
}
function jsonSerialize(): mixed
{
return $this->toArray();
}
function setTemplate(bool $is_template): void
{
$this->is_template = $is_template;
}
function isTemplate(): bool
{
return $this->is_template;
}
function getFlags(): array
{
return $this->flags;
}
function setFlags(array $flags): void
{
$this->flags = $flags;
}
function hasFlag(string $flag): bool
{
return in_array($flag, $this->flags);
}
function getPlayerAnswer(): mixed {
return $this->player_answer;
}
// /* FIXME: ez ugyanaz, mint a saveAnswer() */
// function setPlayerAnswer(mixed $player_answer): void {
// $this->player_answer = $player_answer;
// }
function setCorrectAnswer(mixed $correct_answer): void
{
$this->correct_answer = $correct_answer;
}
function getCorrectAnswer(): mixed
{
return $this->correct_answer;
}
function randomize(): void
{
return;
}
}