Skip to content

Commit

Permalink
add fsm
Browse files Browse the repository at this point in the history
  • Loading branch information
fey committed Jan 21, 2023
1 parent 45dbb2a commit 8c5abe3
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 11 deletions.
15 changes: 15 additions & 0 deletions app/Http/Controllers/ExerciseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\CompletedExercise;
use App\Models\Exercise;
use App\Models\User;
use Illuminate\View\View;
Expand All @@ -20,6 +21,7 @@ public function index(): View
public function show(Exercise $exercise): View
{
$exercise->load('chapter', 'users');
/** @var User $authUser */
$authUser = auth()->user() ?? new User();
$userCompletedExercise = $authUser->completedExercises()
->where('exercise_id', $exercise->id)
Expand All @@ -33,6 +35,19 @@ public function show(Exercise $exercise): View
->get();

$isShowSavedSolutionsButton = collect($userSolutions)->isEmpty();
/** @var CompletedExercise $completedExercise */
$completedExercise = $authUser
->completedExercises()
->whereBelongsTo($exercise)->firstOrNew([]);

if (
$authUser->isRegistered() &&
$completedExercise->isNotFinished()
) {
$completedExercise->user()->associate($authUser);
$completedExercise->exercise()->associate($exercise);
$completedExercise->save();
}

return view('exercise.show', compact(
'exercise',
Expand Down
31 changes: 31 additions & 0 deletions app/Models/CompletedExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Iben\Statable\Statable;

/**
* @method static CompletedExerciseFactory factory(...$parameters)
*/
class CompletedExercise extends Model
{
use HasFactory;
use Statable;

protected $attributes = [
'state' => 'started',
];

protected function getGraph()
{
return 'completed_exercise';
}

protected function saveBeforeTransition()
{
return true;
}

public function user(): BelongsTo
{
Expand All @@ -23,4 +39,19 @@ public function exercise(): BelongsTo
{
return $this->belongsTo(Exercise::class);
}

public function isFinished(): bool
{
return $this->state === 'finished';
}

public function isNotFinished(): bool
{
return !$this->isFinished();
}

public function finish(): void
{
$this->apply('finish');
}
}
7 changes: 5 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

use function PHPUnit\Framework\isFalse;

/**
* App\Models\User
* @property int $id
Expand Down Expand Up @@ -121,6 +119,11 @@ public function isGuest(): bool
return $this->exists === false;
}

public function isRegistered(): bool
{
return $this->exists;
}

public function haveRead(Chapter $chapter): bool
{
return $this->chapters->contains($chapter);
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"guzzlehttp/guzzle": "^7.4",
"hemp/presenter": "^2.5",
"http-interop/http-factory-guzzle": "^1.0",
"iben12/laravel-statable": "^1.5",
"laminas/laminas-diactoros": "^2.5",
"laracasts/flash": "^3.0",
"laraeast/laravel-bootstrap-forms": "^5.2",
Expand All @@ -43,6 +44,7 @@
"mcamara/laravel-localization": "^1.7",
"mikehaertl/php-shellcommand": "^1.6",
"rollbar/rollbar-laravel": "~7.0",
"sebdesign/laravel-state-machine": "^3.3",
"spatie/laravel-activitylog": "^4.0.0",
"spatie/laravel-query-builder": "^5.0",
"spatie/laravel-sitemap": "^6.0",
Expand Down
Loading

0 comments on commit 8c5abe3

Please sign in to comment.