91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Tasks;
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
use Livewire\Attributes\Lazy;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.dashboard', ['active' => 'tasks'])]
|
|
#[Lazy]
|
|
class Run extends Component
|
|
{
|
|
public string $key = '';
|
|
public array $task = [];
|
|
public array $questions = [];
|
|
public int $currentStep = 1;
|
|
public int $totalSteps = 0;
|
|
public bool $finished = false;
|
|
public int $correctAnswers = 0;
|
|
public ?int $lastAnswerIndex = null;
|
|
public ?bool $lastAnswerCorrect = null;
|
|
|
|
public function mount(string $key): void
|
|
{
|
|
$task = $this->findTask($key);
|
|
abort_if($task === null, 404, "Task '{$key}' not found.");
|
|
|
|
$this->key = $key;
|
|
$this->task = $task;
|
|
$this->questions = config("task-questions.{$task['color']}", []);
|
|
$this->totalSteps = count($this->questions);
|
|
|
|
abort_if($this->totalSteps === 0, 404, "No questions configured for color '{$task['color']}'.");
|
|
}
|
|
|
|
public static function placeholder(array $params = []): View
|
|
{
|
|
return view('livewire.tasks.run-placeholder');
|
|
}
|
|
|
|
public function currentQuestion(): array
|
|
{
|
|
return $this->questions[$this->currentStep - 1] ?? $this->questions[0];
|
|
}
|
|
|
|
public function submitAnswer(int $optionIndex): void
|
|
{
|
|
$q = $this->currentQuestion();
|
|
$isCorrect = $optionIndex === ($q['correct'] ?? -1);
|
|
|
|
$this->lastAnswerIndex = $optionIndex;
|
|
$this->lastAnswerCorrect = $isCorrect;
|
|
|
|
if ($isCorrect) {
|
|
$this->correctAnswers++;
|
|
}
|
|
}
|
|
|
|
public function nextStep(): void
|
|
{
|
|
if ($this->currentStep >= $this->totalSteps) {
|
|
$this->finished = true;
|
|
return;
|
|
}
|
|
$this->currentStep++;
|
|
$this->lastAnswerIndex = null;
|
|
$this->lastAnswerCorrect = null;
|
|
}
|
|
|
|
public function restart(): void
|
|
{
|
|
$this->currentStep = 1;
|
|
$this->finished = false;
|
|
$this->correctAnswers = 0;
|
|
$this->lastAnswerIndex = null;
|
|
$this->lastAnswerCorrect = null;
|
|
}
|
|
|
|
/** Locate task by key — placeholder data (later DB-backed). */
|
|
protected function findTask(string $key): ?array
|
|
{
|
|
return collect(config('tasks', []))->firstWhere('key', $key);
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.tasks.run');
|
|
}
|
|
}
|