lernschiff/app/Livewire/Tasks/Modals/Runner.php

75 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Livewire\Tasks\Modals;
use Illuminate\Contracts\View\View;
use LivewireUI\Modal\ModalComponent;
class Runner extends ModalComponent
{
public array $task = [];
public int $currentStep = 1;
public int $totalSteps = 4;
public bool $finished = false;
public ?string $answer = null;
public int $correctAnswers = 0;
/** @var array<int,array<string,mixed>> Placeholder mini-quiz; future = DB-loaded items */
protected array $steps = [
['question' => 'Was ergibt 12 × 8?', 'options' => ['64', '88', '96', '102'], 'correct' => 2],
['question' => 'Was ergibt 15 × 7?', 'options' => ['95', '105', '115', '125'], 'correct' => 1],
['question' => 'Was ergibt 9 × 11?', 'options' => ['91', '99', '108', '111'], 'correct' => 1],
['question' => 'Was ergibt 14 × 6?', 'options' => ['72', '78', '84', '88'], 'correct' => 2],
];
public function mount(array $task = []): void
{
$this->task = $task;
$this->totalSteps = count($this->steps);
}
public function currentQuestion(): array
{
return $this->steps[$this->currentStep - 1] ?? $this->steps[0];
}
public function submitAnswer(int $optionIndex): void
{
$q = $this->currentQuestion();
if ($optionIndex === ($q['correct'] ?? -1)) {
$this->correctAnswers++;
}
if ($this->currentStep >= $this->totalSteps) {
$this->finished = true;
return;
}
$this->currentStep++;
$this->answer = null;
}
public function restart(): void
{
$this->currentStep = 1;
$this->finished = false;
$this->correctAnswers = 0;
$this->answer = null;
}
public static function modalMaxWidth(): string
{
return '2xl';
}
public static function closeModalOnClickAway(): bool
{
return false;
}
public function render(): View
{
return view('livewire.tasks.modals.runner');
}
}