fix gitlab tests
This commit is contained in:
@@ -2,14 +2,13 @@
|
||||
|
||||
namespace App\Services\Attendance;
|
||||
|
||||
use App\Controllers\View\AttendanceController;
|
||||
use App\Models\AttendanceDay;
|
||||
use App\Models\ClassSection;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\StaffAttendance;
|
||||
use App\Models\TeacherClass;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use App\Models\ClassSection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
@@ -24,7 +23,7 @@ class StaffAttendanceService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* legacy {@see AttendanceController::index} — single date / section teacher grid JSON.
|
||||
* legacy {@see \App\Controllers\View\AttendanceController::index} — single date / section teacher grid JSON.
|
||||
*/
|
||||
public function teacherAttendanceDayIndex(?string $semester, ?string $schoolYear, string $dateYmd, int $sectionCode): array
|
||||
{
|
||||
@@ -45,7 +44,7 @@ class StaffAttendanceService
|
||||
->where('class_section_id', $code)
|
||||
->value('class_section_name');
|
||||
$name = trim((string) $name);
|
||||
$labels[$code] = $name !== '' ? $name : ('Section #'.$code);
|
||||
$labels[$code] = $name !== '' ? $name : ('Section #' . $code);
|
||||
}
|
||||
|
||||
$grid = [];
|
||||
@@ -77,7 +76,7 @@ class StaffAttendanceService
|
||||
$tid = (int) ($t['teacher_id'] ?? 0);
|
||||
$posRaw = strtolower((string) ($t['position'] ?? 'main'));
|
||||
$pos = in_array($posRaw, ['main', 'ta'], true) ? $posRaw : 'main';
|
||||
$name = trim(($t['firstname'] ?? '').' '.($t['lastname'] ?? '')) ?: ('User#'.$tid);
|
||||
$name = trim(($t['firstname'] ?? '') . ' ' . ($t['lastname'] ?? '')) ?: ('User#' . $tid);
|
||||
|
||||
$cell = $statusMap[$tid] ?? ['status' => null, 'reason' => null];
|
||||
|
||||
@@ -109,7 +108,7 @@ class StaffAttendanceService
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk save for legacy {@see AttendanceController::save} (method missing in legacy; implemented here).
|
||||
* Bulk save for legacy {@see \App\Controllers\View\AttendanceController::save} (method missing in legacy; implemented here).
|
||||
*/
|
||||
public function saveTeacherAttendanceBulk(Authenticatable $user, array $payload): array
|
||||
{
|
||||
@@ -182,7 +181,7 @@ class StaffAttendanceService
|
||||
|
||||
public function monthData(?string $semester, ?string $schoolYear): array
|
||||
{
|
||||
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
|
||||
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
|
||||
[
|
||||
'school_year' => $schoolYear,
|
||||
'current_year' => $currentYear,
|
||||
@@ -233,7 +232,7 @@ class StaffAttendanceService
|
||||
$sectionCodes = array_keys($assignedBySection);
|
||||
$labels = [];
|
||||
|
||||
if (! empty($sectionCodes)) {
|
||||
if (!empty($sectionCodes)) {
|
||||
$rows = $this->classSection->query()
|
||||
->select(['class_section_id', 'class_section_name'])
|
||||
->whereIn('class_section_id', $sectionCodes)
|
||||
@@ -241,20 +240,20 @@ class StaffAttendanceService
|
||||
->toArray();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$labels[(int) $row['class_section_id']] = trim((string) $row['class_section_name']) ?: 'Section #'.(int) $row['class_section_id'];
|
||||
$labels[(int)$row['class_section_id']] = trim((string)$row['class_section_name']) ?: 'Section #' . (int)$row['class_section_id'];
|
||||
}
|
||||
}
|
||||
|
||||
$teacherIds = [];
|
||||
foreach ($assignedBySection as $rows) {
|
||||
foreach ($rows as $row) {
|
||||
$teacherIds[(int) $row['teacher_id']] = true;
|
||||
$teacherIds[(int)$row['teacher_id']] = true;
|
||||
}
|
||||
}
|
||||
$teacherIds = array_keys($teacherIds);
|
||||
|
||||
$statusByTeacher = [];
|
||||
if (! empty($teacherIds) && ! empty($days)) {
|
||||
if (!empty($teacherIds) && !empty($days)) {
|
||||
$query = DB::table('staff_attendance')
|
||||
->select('user_id', 'date', 'status', 'reason')
|
||||
->where('school_year', $schoolYear)
|
||||
@@ -268,8 +267,8 @@ class StaffAttendanceService
|
||||
$rows = $query->get();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$statusByTeacher[(int) $row->user_id][(string) $row->date] = [
|
||||
'status' => strtolower((string) $row->status),
|
||||
$statusByTeacher[(int)$row->user_id][(string)$row->date] = [
|
||||
'status' => strtolower((string)$row->status),
|
||||
'reason' => $row->reason,
|
||||
];
|
||||
}
|
||||
@@ -285,7 +284,7 @@ class StaffAttendanceService
|
||||
|
||||
$noSchoolDays = [];
|
||||
foreach ($noSchoolRows as $row) {
|
||||
$noSchoolDays[] = (string) $row->date;
|
||||
$noSchoolDays[] = (string)$row->date;
|
||||
}
|
||||
|
||||
$sections = [];
|
||||
@@ -293,9 +292,9 @@ class StaffAttendanceService
|
||||
$teachersPayload = [];
|
||||
|
||||
foreach (($assignedBySection[$code] ?? []) as $row) {
|
||||
$teacherId = (int) $row['teacher_id'];
|
||||
$name = trim(($row['firstname'] ?? '').' '.($row['lastname'] ?? '')) ?: ('User#'.$teacherId);
|
||||
$position = strtolower((string) ($row['position'] ?? 'main'));
|
||||
$teacherId = (int)$row['teacher_id'];
|
||||
$name = trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? '')) ?: ('User#' . $teacherId);
|
||||
$position = strtolower((string)($row['position'] ?? 'main'));
|
||||
$position = in_array($position, ['main', 'ta'], true) ? $position : 'main';
|
||||
|
||||
$cells = [];
|
||||
@@ -303,7 +302,7 @@ class StaffAttendanceService
|
||||
|
||||
foreach ($days as $date) {
|
||||
$cell = $statusByTeacher[$teacherId][$date] ?? null;
|
||||
if (! $cell || empty($cell['status'])) {
|
||||
if (!$cell || empty($cell['status'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -328,15 +327,15 @@ class StaffAttendanceService
|
||||
}
|
||||
|
||||
$sections[] = [
|
||||
'section_code' => (int) $code,
|
||||
'label' => $labels[$code] ?? ('Section #'.(int) $code),
|
||||
'section_code' => (int)$code,
|
||||
'label' => $labels[$code] ?? ('Section #' . (int)$code),
|
||||
'teachers' => $teachersPayload,
|
||||
];
|
||||
}
|
||||
|
||||
$adminStatusByUser = [];
|
||||
$adminIds = [];
|
||||
if (! empty($days)) {
|
||||
if (!empty($days)) {
|
||||
$query = DB::table('staff_attendance')
|
||||
->select('user_id', 'date', 'status', 'reason')
|
||||
->where('school_year', $schoolYear)
|
||||
@@ -362,7 +361,7 @@ class StaffAttendanceService
|
||||
|
||||
$adminIds = array_keys($adminIds);
|
||||
$admins = [];
|
||||
if (! empty($adminIds)) {
|
||||
if (!empty($adminIds)) {
|
||||
$rolesRows = DB::table('user_roles as ur')
|
||||
->select('ur.user_id', 'r.name as role_name', 'r.slug as role_slug', 'r.priority')
|
||||
->join('roles as r', 'r.id', '=', 'ur.role_id')
|
||||
@@ -390,20 +389,20 @@ class StaffAttendanceService
|
||||
|
||||
$namesByUser = [];
|
||||
foreach ($userRows as $row) {
|
||||
$namesByUser[(int) $row->id] = trim(((string) ($row->firstname ?? '')).' '.((string) ($row->lastname ?? '')));
|
||||
$namesByUser[(int) $row->id] = trim(((string) ($row->firstname ?? '')) . ' ' . ((string) ($row->lastname ?? '')));
|
||||
}
|
||||
|
||||
foreach ($adminIds as $uid) {
|
||||
$picked = null;
|
||||
foreach (($rolesByUser[$uid] ?? []) as $role) {
|
||||
$slug = strtolower((string) ($role['role_slug'] ?? $role['role_name'] ?? ''));
|
||||
if (! in_array($slug, $excludeSlugs, true)) {
|
||||
if (!in_array($slug, $excludeSlugs, true)) {
|
||||
$picked = $role;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $picked) {
|
||||
if (!$picked) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -411,7 +410,7 @@ class StaffAttendanceService
|
||||
$cells = [];
|
||||
foreach ($days as $date) {
|
||||
$cell = $adminStatusByUser[$uid][$date] ?? null;
|
||||
if (! $cell || empty($cell['status'])) {
|
||||
if (!$cell || empty($cell['status'])) {
|
||||
continue;
|
||||
}
|
||||
$cells[$date] = $cell;
|
||||
@@ -426,7 +425,7 @@ class StaffAttendanceService
|
||||
|
||||
$admins[] = [
|
||||
'user_id' => $uid,
|
||||
'name' => $namesByUser[$uid] !== '' ? $namesByUser[$uid] : ('User#'.$uid),
|
||||
'name' => $namesByUser[$uid] !== '' ? $namesByUser[$uid] : ('User#' . $uid),
|
||||
'role' => (string) ($picked['role_name'] ?? 'Admin'),
|
||||
'cells' => $cells,
|
||||
'totals' => $totals,
|
||||
@@ -509,7 +508,7 @@ class StaffAttendanceService
|
||||
}
|
||||
}
|
||||
|
||||
if ($currentYear !== '' && ! in_array($currentYear, $schoolYears, true)) {
|
||||
if ($currentYear !== '' && !in_array($currentYear, $schoolYears, true)) {
|
||||
array_unshift($schoolYears, $currentYear);
|
||||
}
|
||||
|
||||
@@ -524,7 +523,7 @@ class StaffAttendanceService
|
||||
? $requestedSchoolYear
|
||||
: ($currentYear !== '' ? $currentYear : ($schoolYears[0] ?? ''));
|
||||
|
||||
if ($resolvedYear !== '' && ! in_array($resolvedYear, $schoolYears, true)) {
|
||||
if ($resolvedYear !== '' && !in_array($resolvedYear, $schoolYears, true)) {
|
||||
array_unshift($schoolYears, $resolvedYear);
|
||||
}
|
||||
|
||||
@@ -540,7 +539,7 @@ class StaffAttendanceService
|
||||
protected function buildRangeLabel(string $schoolYear, string $semesterNorm, string $rangeStart, string $rangeEnd): string
|
||||
{
|
||||
if ($semesterNorm === '') {
|
||||
return 'School Year '.$schoolYear;
|
||||
return 'School Year ' . $schoolYear;
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
@@ -554,14 +553,14 @@ class StaffAttendanceService
|
||||
public function adminsAttendanceData(?string $date, ?string $semester, ?string $schoolYear): array
|
||||
{
|
||||
$date = $date ?: now()->toDateString();
|
||||
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
|
||||
$schoolYear = $schoolYear ?: (string) $this->configuration->getConfig('school_year');
|
||||
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
|
||||
$schoolYear = $schoolYear ?: (string)$this->configuration->getConfig('school_year');
|
||||
|
||||
$allStaff = $this->staffAttendance->staffWithStatusForDay($date, $semester, $schoolYear, []);
|
||||
$userIds = [];
|
||||
|
||||
foreach ($allStaff as $row) {
|
||||
$uid = (int) ($row['user_id'] ?? 0);
|
||||
$uid = (int)($row['user_id'] ?? 0);
|
||||
if ($uid > 0) {
|
||||
$userIds[$uid] = true;
|
||||
}
|
||||
@@ -578,19 +577,19 @@ class StaffAttendanceService
|
||||
|
||||
$rolesByUser = [];
|
||||
foreach ($rolesRows as $row) {
|
||||
$rolesByUser[(int) $row->user_id][] = (array) $row;
|
||||
$rolesByUser[(int)$row->user_id][] = (array)$row;
|
||||
}
|
||||
|
||||
foreach ($rolesByUser as &$list) {
|
||||
usort($list, fn ($a, $b) => ((int) ($a['priority'] ?? 100)) <=> ((int) ($b['priority'] ?? 100)));
|
||||
usort($list, fn($a, $b) => ((int)($a['priority'] ?? 100)) <=> ((int)($b['priority'] ?? 100)));
|
||||
}
|
||||
|
||||
$excludeSlugs = ['parent', 'guest', 'teacher', 'teacher_assistant', 'ta', 'assistant_teacher'];
|
||||
|
||||
$staffByUser = [];
|
||||
foreach ($allStaff as $row) {
|
||||
$uid = (int) ($row['user_id'] ?? 0);
|
||||
if ($uid > 0 && ! isset($staffByUser[$uid])) {
|
||||
$uid = (int)($row['user_id'] ?? 0);
|
||||
if ($uid > 0 && !isset($staffByUser[$uid])) {
|
||||
$staffByUser[$uid] = $row;
|
||||
}
|
||||
}
|
||||
@@ -601,14 +600,14 @@ class StaffAttendanceService
|
||||
$picked = null;
|
||||
|
||||
foreach ($roles as $role) {
|
||||
$slug = strtolower((string) ($role['role_slug'] ?? $role['role_name'] ?? ''));
|
||||
if (! in_array($slug, $excludeSlugs, true)) {
|
||||
$slug = strtolower((string)($role['role_slug'] ?? $role['role_name'] ?? ''));
|
||||
if (!in_array($slug, $excludeSlugs, true)) {
|
||||
$picked = $role;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $picked) {
|
||||
if (!$picked) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -619,9 +618,8 @@ class StaffAttendanceService
|
||||
}
|
||||
|
||||
usort($adminsGrid, function ($a, $b) {
|
||||
$an = trim(($a['firstname'] ?? '').' '.($a['lastname'] ?? ''));
|
||||
$bn = trim(($b['firstname'] ?? '').' '.($b['lastname'] ?? ''));
|
||||
|
||||
$an = trim(($a['firstname'] ?? '') . ' ' . ($a['lastname'] ?? ''));
|
||||
$bn = trim(($b['firstname'] ?? '') . ' ' . ($b['lastname'] ?? ''));
|
||||
return strcasecmp($an, $bn);
|
||||
});
|
||||
|
||||
@@ -635,20 +633,20 @@ class StaffAttendanceService
|
||||
|
||||
public function saveAdmins(Authenticatable $user, array $data): array
|
||||
{
|
||||
$date = (string) $data['date'];
|
||||
$semester = (string) $data['semester'];
|
||||
$schoolYear = (string) $data['school_year'];
|
||||
$admins = (array) ($data['admins'] ?? $data['staff'] ?? []);
|
||||
$date = (string)$data['date'];
|
||||
$semester = (string)$data['semester'];
|
||||
$schoolYear = (string)$data['school_year'];
|
||||
$admins = (array)($data['admins'] ?? $data['staff'] ?? []);
|
||||
$allowedStatuses = ['present', 'absent', 'late'];
|
||||
$excludedRoleSlugs = ['parent', 'guest', 'teacher', 'teacher_assistant'];
|
||||
|
||||
DB::transaction(function () use ($admins, $date, $semester, $schoolYear, $allowedStatuses, $excludedRoleSlugs) {
|
||||
foreach ($admins as $uid => $row) {
|
||||
$uid = (int) $uid;
|
||||
$status = strtolower(trim((string) ($row['status'] ?? '')));
|
||||
$reason = trim((string) ($row['reason'] ?? ''));
|
||||
$roleName = (string) ($row['role_name'] ?? '');
|
||||
$roleSlug = strtolower(str_replace(['-', ' '], '_', (string) ($row['role_slug'] ?? $roleName)));
|
||||
$uid = (int)$uid;
|
||||
$status = strtolower(trim((string)($row['status'] ?? '')));
|
||||
$reason = trim((string)($row['reason'] ?? ''));
|
||||
$roleName = (string)($row['role_name'] ?? '');
|
||||
$roleSlug = strtolower(str_replace(['-', ' '], '_', (string)($row['role_slug'] ?? $roleName)));
|
||||
|
||||
if (in_array($roleSlug, $excludedRoleSlugs, true)) {
|
||||
continue;
|
||||
@@ -661,11 +659,10 @@ class StaffAttendanceService
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array($status, $allowedStatuses, true)) {
|
||||
if (!in_array($status, $allowedStatuses, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -695,13 +692,13 @@ class StaffAttendanceService
|
||||
|
||||
public function saveCell(Authenticatable $user, array $data): array
|
||||
{
|
||||
$date = (string) $data['date'];
|
||||
$status = (string) ($data['status'] ?? '');
|
||||
$targetId = (int) $data['user_id'];
|
||||
$roleName = trim((string) ($data['role_name'] ?? ''));
|
||||
$semester = (string) $data['semester'];
|
||||
$schoolYear = (string) $data['school_year'];
|
||||
$reason = trim((string) ($data['reason'] ?? ''));
|
||||
$date = (string)$data['date'];
|
||||
$status = (string)($data['status'] ?? '');
|
||||
$targetId = (int)$data['user_id'];
|
||||
$roleName = trim((string)($data['role_name'] ?? ''));
|
||||
$semester = (string)$data['semester'];
|
||||
$schoolYear = (string)$data['school_year'];
|
||||
$reason = trim((string)($data['reason'] ?? ''));
|
||||
|
||||
if ($status === '') {
|
||||
DB::table('staff_attendance')
|
||||
@@ -735,9 +732,9 @@ class StaffAttendanceService
|
||||
|
||||
public function monthCsv(?string $semester, ?string $schoolYear, ?string $scope): StreamedResponse
|
||||
{
|
||||
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
|
||||
$schoolYear = $schoolYear ?: (string) $this->configuration->getConfig('school_year');
|
||||
$scope = strtolower((string) $scope);
|
||||
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
|
||||
$schoolYear = $schoolYear ?: (string)$this->configuration->getConfig('school_year');
|
||||
$scope = strtolower((string)$scope);
|
||||
|
||||
[$rangeStart, $rangeEnd] = $this->semesterRangeService->getSchoolYearRange($schoolYear);
|
||||
$semesterNorm = $this->semesterRangeService->normalizeSemester($semester);
|
||||
@@ -751,9 +748,9 @@ class StaffAttendanceService
|
||||
$daysList = $this->semesterRangeService->buildSundayList($rangeStart, $rangeEnd);
|
||||
|
||||
$filename = ($scope === 'admins' ? 'admin_attendance_' : 'teacher_attendance_')
|
||||
.$semester.'_'.str_replace('/', '-', $schoolYear).'.csv';
|
||||
. $semester . '_' . str_replace('/', '-', $schoolYear) . '.csv';
|
||||
|
||||
return response()->streamDownload(function () use ($schoolYear, $scope, $semesterNorm, $rangeStart, $rangeEnd) {
|
||||
return response()->streamDownload(function () use ($semester, $schoolYear, $scope, $daysList, $semesterNorm, $rangeStart, $rangeEnd) {
|
||||
$out = fopen('php://output', 'w');
|
||||
|
||||
if ($scope === 'admins') {
|
||||
@@ -773,13 +770,12 @@ class StaffAttendanceService
|
||||
fputcsv($out, [
|
||||
$row->user_id,
|
||||
$row->date,
|
||||
strtoupper(substr((string) $row->status, 0, 1)),
|
||||
strtoupper(substr((string)$row->status, 0, 1)),
|
||||
$row->reason,
|
||||
]);
|
||||
}
|
||||
|
||||
fclose($out);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -799,7 +795,7 @@ class StaffAttendanceService
|
||||
fputcsv($out, [
|
||||
$row->user_id,
|
||||
$row->date,
|
||||
strtoupper(substr((string) $row->status, 0, 1)),
|
||||
strtoupper(substr((string)$row->status, 0, 1)),
|
||||
$row->reason,
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user