add controllers, servoices

This commit is contained in:
root
2026-03-09 02:52:13 -04:00
parent c8de5f7edc
commit d76c871cb7
501 changed files with 34439 additions and 21843 deletions
@@ -0,0 +1,73 @@
<?php
namespace App\Services\CompetitionScores;
use App\Models\ClassSection;
use App\Models\Configuration;
use App\Models\TeacherClass;
class CompetitionScoresContextService
{
public function getTeacherContext(int $userId): array
{
$schoolYear = (string) (Configuration::getConfig('school_year') ?? '');
$semester = (string) (Configuration::getConfig('semester') ?? '');
$assignments = TeacherClass::getClassAssignmentsByUserId($userId, $schoolYear, $semester);
return [$assignments, $schoolYear, $semester];
}
public function getAllowedClassIds(array $assignments): array
{
$ids = array_map(static function ($row) {
return (int) ($row['class_section_id'] ?? 0);
}, $assignments);
return array_values(array_filter(array_unique($ids)));
}
public function resolveActiveClassId(array $assignments, int $requestedClassId = 0): int
{
$allowed = $this->getAllowedClassIds($assignments);
if ($requestedClassId > 0 && in_array($requestedClassId, $allowed, true)) {
return $requestedClassId;
}
return (int) ($allowed[0] ?? 0);
}
public function getActiveClassName(array $assignments, int $classSectionId): ?string
{
if ($classSectionId <= 0) {
return null;
}
foreach ($assignments as $row) {
if ((int) ($row['class_section_id'] ?? 0) === $classSectionId) {
return $row['class_section_name'] ?? null;
}
}
return ClassSection::getClassSectionNameBySectionId($classSectionId);
}
public function getClassSectionMap(): array
{
$sections = ClassSection::query()
->select('class_section_id', 'class_section_name')
->orderBy('class_section_name', 'ASC')
->get()
->map(fn ($row) => (array) $row)
->all();
$map = [];
foreach ($sections as $section) {
$id = (int) ($section['class_section_id'] ?? 0);
if ($id > 0) {
$map[$id] = $section['class_section_name'] ?? (string) $id;
}
}
return $map;
}
}
@@ -0,0 +1,118 @@
<?php
namespace App\Services\CompetitionScores;
use App\Models\Competition;
use App\Models\CompetitionClassWinner;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CompetitionScoresQueryService
{
public function getCompetitionsForClass(int $classSectionId, string $schoolYear, string $semester): array
{
$builder = Competition::query()->orderBy('id', 'DESC');
if ($classSectionId > 0) {
$builder->where(function ($q) use ($classSectionId) {
$q->where('class_section_id', $classSectionId)
->orWhere('class_section_id', 0)
->orWhereNull('class_section_id');
});
}
if ($schoolYear !== '') {
$builder->where(function ($q) use ($schoolYear) {
$q->where('school_year', $schoolYear)
->orWhere('school_year', '')
->orWhereNull('school_year');
});
}
if ($semester !== '') {
$builder->where(function ($q) use ($semester) {
$q->where('semester', $semester)
->orWhere('semester', '')
->orWhereNull('semester');
});
}
return $builder->get()->map(fn ($row) => (array) $row)->all();
}
public function getQuestionCounts(array $competitionIds, int $classSectionId): array
{
if (!$this->hasQuestionCount() || empty($competitionIds)) {
return [];
}
$rows = CompetitionClassWinner::query()
->select('competition_id', 'question_count')
->where('class_section_id', $classSectionId)
->whereIn('competition_id', $competitionIds)
->get()
->map(fn ($row) => (array) $row)
->all();
$out = [];
foreach ($rows as $row) {
$compId = (int) ($row['competition_id'] ?? 0);
if ($compId > 0 && $row['question_count'] !== null) {
$out[$compId] = (int) $row['question_count'];
}
}
return $out;
}
public function getScoreCounts(array $competitionIds, int $classSectionId): array
{
if (empty($competitionIds)) {
return [];
}
$rows = DB::table('competition_scores')
->selectRaw('competition_id, COUNT(*) AS total')
->where('class_section_id', $classSectionId)
->whereIn('competition_id', $competitionIds)
->groupBy('competition_id')
->get()
->map(fn ($row) => (array) $row)
->all();
$out = [];
foreach ($rows as $row) {
$compId = (int) ($row['competition_id'] ?? 0);
if ($compId > 0) {
$out[$compId] = (int) ($row['total'] ?? 0);
}
}
return $out;
}
public function getQuestionCountForCompetition(int $competitionId, int $classSectionId): ?int
{
if (!$this->hasQuestionCount()) {
return null;
}
$row = CompetitionClassWinner::query()
->select('question_count')
->where('competition_id', $competitionId)
->where('class_section_id', $classSectionId)
->first();
if ($row && $row->question_count !== null) {
return (int) $row->question_count;
}
return null;
}
private function hasQuestionCount(): bool
{
return Schema::hasTable('competition_class_winners')
&& Schema::hasColumn('competition_class_winners', 'question_count');
}
}
@@ -0,0 +1,67 @@
<?php
namespace App\Services\CompetitionScores;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CompetitionScoresRosterService
{
private string $classStudentTable;
public function __construct()
{
if (Schema::hasTable('class_student')) {
$this->classStudentTable = 'class_student';
} elseif (Schema::hasTable('student_class')) {
$this->classStudentTable = 'student_class';
} else {
$this->classStudentTable = '';
}
}
public function getClassStudentCount(int $classSectionId, ?string $schoolYear): int
{
if ($classSectionId <= 0 || $this->classStudentTable === '') {
return 0;
}
$builder = DB::table($this->classStudentTable)
->selectRaw('COUNT(*) AS total')
->where('class_section_id', $classSectionId);
if ($schoolYear && Schema::hasColumn($this->classStudentTable, 'school_year')) {
$builder->where('school_year', $schoolYear);
}
$row = (array) $builder->first();
return (int) ($row['total'] ?? 0);
}
public function getStudentsForCompetition(array $competition, int $classSectionId): array
{
if ($classSectionId <= 0 || $this->classStudentTable === '') {
return [];
}
$builder = DB::table('students as s')
->select('s.id', 's.school_id', 's.firstname', 's.lastname')
->join($this->classStudentTable . ' as cs', 'cs.student_id', '=', 's.id')
->where('cs.class_section_id', $classSectionId);
if (Schema::hasColumn($this->classStudentTable, 'school_year')) {
$schoolYear = (string) ($competition['school_year'] ?? '');
if ($schoolYear !== '') {
$builder->where('cs.school_year', $schoolYear);
}
}
return $builder
->distinct()
->orderBy('s.lastname', 'ASC')
->orderBy('s.firstname', 'ASC')
->get()
->map(fn ($row) => (array) $row)
->all();
}
}
@@ -0,0 +1,63 @@
<?php
namespace App\Services\CompetitionScores;
use App\Models\CompetitionScore;
class CompetitionScoresSaveService
{
public function filterScores(array $scores): array
{
$cleanScores = [];
$invalidScores = [];
foreach ($scores as $studentId => $scoreValue) {
$scoreValue = trim((string) $scoreValue);
if ($scoreValue === '') {
continue;
}
if (!preg_match('/^\\d+$/', $scoreValue)) {
$invalidScores[] = (int) $studentId;
continue;
}
$cleanScores[(int) $studentId] = (int) $scoreValue;
}
return [$cleanScores, $invalidScores];
}
public function saveScores(int $competitionId, int $classSectionId, array $scores): void
{
foreach ($scores as $studentId => $scoreValue) {
CompetitionScore::query()->updateOrCreate(
[
'competition_id' => $competitionId,
'student_id' => (int) $studentId,
'class_section_id' => $classSectionId,
],
[
'score' => (int) $scoreValue,
'class_section_id' => $classSectionId,
]
);
}
}
public function getScoreMap(int $competitionId, int $classSectionId): array
{
$rows = CompetitionScore::query()
->select('student_id', 'score')
->where('competition_id', $competitionId)
->where('class_section_id', $classSectionId)
->get()
->map(fn ($row) => (array) $row)
->all();
$map = [];
foreach ($rows as $row) {
$map[(int) $row['student_id']] = $row['score'];
}
return $map;
}
}