diff --git a/app/Actions/Evaluations/GradeSearchEvaluation.php b/app/Actions/Evaluations/GradeSearchEvaluation.php index 0559b1b..7544387 100644 --- a/app/Actions/Evaluations/GradeSearchEvaluation.php +++ b/app/Actions/Evaluations/GradeSearchEvaluation.php @@ -22,14 +22,10 @@ public function grade(UserFeedback $feedback, User $user, int $grade): void throw new \RuntimeException('Snapshot assigned to another user'); } - if ($feedback->user_id === $user->id && $feedback->isAssignmentExpired()) { + if ($feedback->grade === null && $feedback->isAssignmentExpired()) { throw new \RuntimeException('Snapshot assignment expired'); } - if ($feedback->grade !== null) { - throw new \RuntimeException('Snapshot is already graded'); - } - $feedback->grade = $grade; $feedback->save(); } diff --git a/app/Livewire/GiveFeedback.php b/app/Livewire/GiveFeedback.php index fc916a5..2fe0d8e 100644 --- a/app/Livewire/GiveFeedback.php +++ b/app/Livewire/GiveFeedback.php @@ -18,6 +18,8 @@ class GiveFeedback extends Component public ?UserFeedback $feedback = null; + public ?UserFeedback $previous = null; + public function mount(?int $evaluationId = null): void { if ($evaluationId === null) { @@ -30,6 +32,7 @@ public function mount(?int $evaluationId = null): void if ($this->evaluation === null || $this->evaluation->isActive()) { $this->fetchFeedback(); + $this->previous = $this->getPreviousFeedback(); } } @@ -38,6 +41,11 @@ private function fetchFeedback(): void $this->feedback = app(UserFeedbackService::class)->fetch(Auth::user(), $this->evaluation); } + private function getPreviousFeedback(): ?UserFeedback + { + return app(UserFeedbackService::class)->previous(Auth::user(), $this->evaluation); + } + public function render(): View { return view('livewire.pages.give-feedback') @@ -52,7 +60,17 @@ public function grade(UserFeedback $feedback, int $grade, GradeSearchEvaluation Toaster::error($e->getMessage()); } + $this->previous = $feedback; + $this->fetchFeedback(); $this->dispatch('$refresh'); } + + public function goPrevious(): void + { + $this->feedback = $this->previous; + $this->previous = null; + + $this->dispatch('$refresh'); + } } diff --git a/app/Services/Evaluations/UserFeedbackService.php b/app/Services/Evaluations/UserFeedbackService.php index 29e994f..18af766 100644 --- a/app/Services/Evaluations/UserFeedbackService.php +++ b/app/Services/Evaluations/UserFeedbackService.php @@ -5,6 +5,7 @@ use App\Models\SearchEvaluation; use App\Models\User; use App\Models\UserFeedback; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Cache; @@ -28,11 +29,7 @@ class UserFeedbackService */ public function fetch(User $user, ?SearchEvaluation $evaluation): ?UserFeedback { - if ($evaluation === null) { - $pool = UserFeedback::globalPool($user); - } else { - $pool = UserFeedback::evaluationPool($evaluation->id); - } + $pool = $this->getPool($user, $evaluation); $assignedFeedback = $pool->clone() ->with('snapshot.keyword.evaluation.tags') @@ -87,6 +84,34 @@ public function fetch(User $user, ?SearchEvaluation $evaluation): ?UserFeedback return $feedback; } + /** + * Get previously graded feedback for the user. + * + * @param User $user + * @param SearchEvaluation|null $evaluation + * + * @return UserFeedback|null + */ + public function previous(User $user, ?SearchEvaluation $evaluation): ?UserFeedback + { + return $this->getPool($user, $evaluation) + ->clone() + ->with('snapshot.keyword.evaluation.tags') + ->graded() + ->where(UserFeedback::FIELD_USER_ID, $user->id) + ->orderByDesc(UserFeedback::FIELD_UPDATED_AT) + ->first(); + } + + private function getPool(User $user, ?SearchEvaluation $evaluation): Builder|UserFeedback + { + if ($evaluation === null) { + return UserFeedback::globalPool($user); + } + + return UserFeedback::evaluationPool($evaluation->id); + } + private function getGlobalPoolSnapshotsCount(User $user): int { $count = 0; @@ -129,7 +154,7 @@ public function getUngradedSnapshotsCountCached(User $user): int $tag = $this->getUngradedSnapshotsCountCacheTag($user->current_team_id); $key = $this->getUngradedSnapshotsCountCacheKey($user->id); - return Cache::tags($tag)->remember($key, 3600, fn () => $this->getGlobalPoolSnapshotsCount($user)); + return Cache::tags($tag)->remember($key, 300, fn () => $this->getGlobalPoolSnapshotsCount($user)); } public static function getUngradedSnapshotsCountCacheTag(int $teamId): string diff --git a/resources/views/components/scales/scale-button.blade.php b/resources/views/components/scales/scale-button.blade.php index 7798579..e50f590 100644 --- a/resources/views/components/scales/scale-button.blade.php +++ b/resources/views/components/scales/scale-button.blade.php @@ -8,7 +8,10 @@ $feedback->grade === $key, + ]) wire:click="grade('{{ $feedback->id }}', '{{ $key }}')" wire:loading.attr="disabled" > diff --git a/resources/views/livewire/pages/give-feedback.blade.php b/resources/views/livewire/pages/give-feedback.blade.php index 9f31630..9d383fd 100644 --- a/resources/views/livewire/pages/give-feedback.blade.php +++ b/resources/views/livewire/pages/give-feedback.blade.php @@ -29,13 +29,27 @@

@else -
- - Keyword - - - {{ $feedback->snapshot->keyword->keyword }} - +
+
+ + Keyword + + + {{ $feedback->snapshot->keyword->keyword }} + +
+
+ @if ($previous) + + @endif +