diff --git a/app/Models/Contest.php b/app/Models/Contest.php index cee8bcd..77e05de 100755 --- a/app/Models/Contest.php +++ b/app/Models/Contest.php @@ -62,6 +62,8 @@ protected static function boot(): void ->setTimezone('UTC') ->format('Y-m-d H:i:s'); } + + $model->ensureAllVotesHaveRatings(); }); } @@ -207,4 +209,39 @@ public function isVotingOpen(): bool return $isOpen; } + + /* Utilities */ + + /** + * Ensure all votes have ratings for all entries and rating factors + * + * @return void + */ + public function ensureAllVotesHaveRatings(): void + { + $entries = $this->entries; + $ratingFactors = $this->ratingFactors; + $votes = $this->votes; + + // If there's not a vote rating for the vote, entry, and rating factor, create one with the rating of 0. + foreach ($votes as $vote) { + foreach ($entries as $entry) { + foreach ($ratingFactors as $ratingFactor) { + $voteRating = $vote->voteRatings() + ->where('entry_id', $entry->id) + ->where('rating_factor_id', $ratingFactor->id) + ->first(); + if (!isset($voteRating)) { + // Create a new vote rating with a rating of 0 + $vote->voteRatings()->create([ + 'entry_id' => $entry->id, + 'rating_factor_id' => $ratingFactor->id, + 'rating' => 0, + ]); + } + } + } + $vote->refreshSummary(); + } + } } diff --git a/app/Models/Vote.php b/app/Models/Vote.php index 44ea024..1e84541 100755 --- a/app/Models/Vote.php +++ b/app/Models/Vote.php @@ -72,32 +72,35 @@ public function getEntryTotalRating(int $entryId): float */ public function refreshSummary(): void { + $summaryJSON = $this->summary ?? "[]"; + $summary = json_decode($summaryJSON, true); foreach ($this->contest->entries as $entry) { $entrySum = 0; $hasAnyRating = false; foreach ($this->contest->ratingFactors as $ratingFactor) { - $rating = $this->voteRatings() + $voteRating = $this->voteRatings() ->where('entry_id', $entry->id) ->where('rating_factor_id', $ratingFactor->id) - ->first() - ->rating ?? null; + ->first(); + $rating = $voteRating->rating ?? null; if (isset($rating)) { $entrySum += $rating; $hasAnyRating = true; } - $this->summary[$entry->id][$ratingFactor->id] = $rating; + $summary[$entry->id][$ratingFactor->id] = $rating; } // If there are no ratings, set the total to null so it doesn't affect the average - if ($hasAnyRating) { + if (!$hasAnyRating) { $entrySum = null; } // Save the total - $this->summary[$entry->id]['total'] = $entrySum; + $summary[$entry->id]['total'] = $entrySum; } + $this->summary = $summary; $this->save(); } } diff --git a/resources/views/components/select-input.blade.php b/resources/views/components/select-input.blade.php new file mode 100755 index 0000000..5950404 --- /dev/null +++ b/resources/views/components/select-input.blade.php @@ -0,0 +1,3 @@ + diff --git a/resources/views/contests/vote.blade.php b/resources/views/contests/vote.blade.php index 0c72353..8964fa3 100644 --- a/resources/views/contests/vote.blade.php +++ b/resources/views/contests/vote.blade.php @@ -19,11 +19,10 @@ Factors @foreach ($contest->entries as $entry) @php - if ($contest->entry_description_display_type == 'tooltip'): + $tooltip = null; + if ($contest->entry_description_display_type == 'tooltip') { $tooltip = $entry->description; - else: - $tooltip = null; - endif; + } @endphp {{ $entry->name }} @@ -39,11 +38,10 @@ @foreach ($contest->ratingFactors as $ratingFactor) @php - if ($contest->entry_description_display_type == 'tooltip'): - $tooltip = $ratingFactor->description; - else: - $tooltip = null; - endif; + $tooltip = null; + if ($contest->entry_description_display_type == 'tooltip') { + $tooltip = $entry->description; + } @endphp {{ $ratingFactor->name }}