58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Audition;
|
|
use App\Models\Entry;
|
|
use App\Models\ScoreSheet;
|
|
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 = ScoreSheet::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');
|
|
|
|
// Eager load the count of related models
|
|
$auditions->loadCount(['judges', 'entries']);
|
|
|
|
// Iterate over the auditions and calculate the scored_entries_count
|
|
return $auditions->map(function ($audition) use ($scoreCountByEntry) {
|
|
$audition->scored_entries_count = $audition->entries->reduce(function ($carry, $entry) use ($audition, $scoreCountByEntry) {
|
|
$entry->fully_scored = $audition->judges_count == $scoreCountByEntry[$entry->id];
|
|
return $carry + ($entry->fully_scored ? 1 : 0);
|
|
}, 0);
|
|
return $audition;
|
|
});
|
|
}
|
|
}
|