83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Entry;
|
|
use App\Models\Student;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DoublerService
|
|
{
|
|
protected $doublersCacheKey = 'doublers';
|
|
protected $auditionCacheService;
|
|
protected $tabulationService;
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct(AuditionCacheService $auditionCacheService, TabulationService $tabulationService)
|
|
{
|
|
$this->auditionCacheService = $auditionCacheService;
|
|
$this->tabulationService = $tabulationService;
|
|
}
|
|
|
|
/**
|
|
* Returns a collection of students that have more than one entry
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public function getDoublers(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
// TODO creating or destroying an entry should refresh the doubler cache
|
|
return Cache::remember($this->doublersCacheKey, 3600, function () {
|
|
return Student::withCount('entries')
|
|
->with('entries')
|
|
->havingRaw('entries_count > ?', [1])
|
|
->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns an array of information about each entry for a specific doubler. Info for each entry includes
|
|
* auditionID
|
|
* auditionName
|
|
* rank => This student's rank in the given audition
|
|
* unscored => How many entries remain to be scored in this audition
|
|
*
|
|
* @param int $studentId The ID of the doubler
|
|
* @return array
|
|
*/
|
|
public function getDoublerInfo($studentId): array
|
|
{
|
|
// When getting a doubler we need to know
|
|
// 1) What their entries are
|
|
// 2) For each audition they're entered in, what is their rank
|
|
// 3) For each audition they're entered in, how many entries are unscored
|
|
// 4) How many are accepted on that instrument
|
|
$doubler = $this->getDoublers()->firstWhere('id',$studentId);
|
|
$info = [];
|
|
|
|
foreach ($doubler->entries as $entry) {
|
|
$info[$entry->id] = [
|
|
'auditionID' => $entry->audition_id,
|
|
'auditionName' => $this->auditionCacheService->getAudition($entry->audition_id)->name,
|
|
'rank' => $this->tabulationService->entryRank($entry),
|
|
'unscored' => $this->tabulationService->remainingEntriesForAudition($entry->audition_id)
|
|
];
|
|
$entry->audition = $this->auditionCacheService->getAudition($entry->audition_id);
|
|
}
|
|
|
|
return $info;
|
|
}
|
|
|
|
|
|
/**
|
|
* Checks if a student is a doubler based on the given student ID
|
|
*
|
|
* @param int $studentId The ID of the student to check
|
|
* @return bool Returns true if the student is a doubler, false otherwise
|
|
*/
|
|
public function studentIsDoubler($studentId): bool
|
|
{
|
|
return $this->getDoublers()->contains('id', $studentId);
|
|
}
|
|
}
|