Skip to content

Commit

Permalink
Grade previous feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
afedukov committed Oct 19, 2024
1 parent 44bf969 commit 4edef13
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
6 changes: 1 addition & 5 deletions app/Actions/Evaluations/GradeSearchEvaluation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 18 additions & 0 deletions app/Livewire/GiveFeedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,6 +32,7 @@ public function mount(?int $evaluationId = null): void

if ($this->evaluation === null || $this->evaluation->isActive()) {
$this->fetchFeedback();
$this->previous = $this->getPreviousFeedback();
}
}

Expand All @@ -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')
Expand All @@ -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');
}
}
37 changes: 31 additions & 6 deletions app/Services/Evaluations/UserFeedbackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion resources/views/components/scales/scale-button.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
<x-dynamic-component :component="$scale->getScaleButtonComponent()"
scale-key="{{ $key }}"
:shortcut="$scale->getShortcuts()[$key] ?? null"
class="px-3 sm:px-5 py-2.5 btn font-mono block text-white font-semibold rounded-lg text-xs sm:text-sm focus:outline-none disabled:opacity-25 transition"
@class([
'px-3 sm:px-5 py-2.5 btn font-mono block text-white font-semibold rounded-lg text-xs sm:text-sm focus:outline-none disabled:opacity-25 transition',
'ring-2 ring-offset-2 ring-blue-500 ring-offset-white dark:ring-offset-slate-800' => $feedback->grade === $key,
])
wire:click="grade('{{ $feedback->id }}', '{{ $key }}')"
wire:loading.attr="disabled"
>
Expand Down
28 changes: 21 additions & 7 deletions resources/views/livewire/pages/give-feedback.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,27 @@
</p>
</div>
@else
<div class="mb-8 flex items-center gap-3">
<span class="font-medium text-lg text-gray-500 dark:text-gray-400">
Keyword
</span>
<x-block.bordered-label class="font-bold text-lg text-gray-900 dark:text-white">
{{ $feedback->snapshot->keyword->keyword }}
</x-block.bordered-label>
<div class="mb-8 flex items-center gap-3 justify-between">
<div>
<span class="font-medium text-lg text-gray-500 dark:text-gray-400">
Keyword
</span>
<x-block.bordered-label class="font-bold text-lg text-gray-900 dark:text-white">
{{ $feedback->snapshot->keyword->keyword }}
</x-block.bordered-label>
</div>
<div>
@if ($previous)
<button
class="btn gap-1 ml-2 px-3 py-1 font-medium rounded-lg text-xs text-gray-900 bg-white border border-gray-300 focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700"
wire:click="goPrevious"
wire:loading.attr="disabled"
>
Previous
<x-dynamic-component :component="$previous->snapshot->keyword->evaluation->getScale()->getScaleBadgeComponent()" :grade="$previous->grade" size="sm" />
</button>
@endif
</div>
</div>

<div class="text-lg font-semibold text-gray-900 dark:text-white mb-8">
Expand Down

0 comments on commit 4edef13

Please sign in to comment.