auditionadmin/app/Services/TabulationService.php

59 lines
1.6 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()
{
$scoreCountByEntry = DB::table('score_sheets')
->select('entry_id', DB::raw('count(*) as count'))
->groupBy('entry_id')
->get()
->pluck('count','entry_id');
$auditions = $this->auditionCacheService->getAuditions();
$auditions->load(['entries' => function ($query) {
$query->select('id');
}]);
$auditions->loadCount('judges');
$auditions->loadCount('entries');
foreach ($auditions as $audition)
{
// Get the count of entries that have been scored
$audition->scored_entries = $audition->entries->filter(function ($entry) use ($scoreCountByEntry) {
return $entry->score_sheets_count == $scoreCountByEntry[$entry->id];
});
// WHY WILL THIS NOT WORK?????
}
return $auditions;
}
}