auditionadmin/app/Services/TabulationService.php

62 lines
1.8 KiB
PHP

<?php
namespace App\Services;
use App\Models\Audition;
use App\Models\Entry;
use Illuminate\Support\Facades\Cache;
use App\Services\AuditionCacheService;
use Illuminate\Support\Facades\DB;
class TabulationService
{
protected $cacheKey = 'entries';
protected $auditionCacheService;
/**
* Create a new class instance.
*/
public function __construct(AuditionCacheService $scoringGuideCacheService)
{
$this->auditionCacheService = $scoringGuideCacheService;
}
public function getScoredEntries()
{
return Cache::remember($this->cacheKey, 10, function () {
$entries = Entry::with(['scoreSheets'])->get();
return $entries->keyBy('id');
});
}
public function getAuditionsWithStatus()
{
// Create an array with the number of scores for each entry
$scoreCountByEntry = DB::table('score_sheets')
->select('entry_id', DB::raw('count(*) as count'))
->groupBy('entry_id')
->get()
->pluck('count','entry_id');
// Retrieve auditions from the cache and load entry IDs
$auditions = $this->auditionCacheService->getAuditions();
$auditions->load('entries');
$auditions->loadCount('judges');
$auditions->loadCount('entries');
foreach ($auditions as $audition) {
$audition->scored_entries_count = 0;
foreach ($audition->entries as $entry) {
if ($audition->judges_count == $scoreCountByEntry[$entry->id]) {
$entry->fully_scored = true;
$audition->scored_entries_count++;
} else {
$entry->fully_scored = false;
}
}
}
return $auditions;
}
}