update controllers logic
This commit is contained in:
@@ -2,11 +2,12 @@
|
||||
|
||||
namespace App\Services\Attendance;
|
||||
|
||||
use App\Models\AttendanceDay;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\StaffAttendance;
|
||||
use App\Models\TeacherClass;
|
||||
use App\Models\ClassSection;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use App\Models\ClassSection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
@@ -20,6 +21,163 @@ class StaffAttendanceService
|
||||
protected SemesterRangeService $semesterRangeService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* CodeIgniter {@see \App\Controllers\View\AttendanceController::index} — single date / section teacher grid JSON.
|
||||
*/
|
||||
public function teacherAttendanceDayIndex(?string $semester, ?string $schoolYear, string $dateYmd, int $sectionCode): array
|
||||
{
|
||||
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
|
||||
$schoolYear = $schoolYear ?: (string) $this->configuration->getConfig('school_year');
|
||||
|
||||
$assignedBySection = TeacherClass::assignedBySectionForTerm($semester, $schoolYear);
|
||||
$sectionCodes = array_keys($assignedBySection);
|
||||
|
||||
$labels = [];
|
||||
foreach ($sectionCodes as $code) {
|
||||
$code = (int) $code;
|
||||
if ($code <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $this->classSection->query()
|
||||
->where('class_section_id', $code)
|
||||
->value('class_section_name');
|
||||
$name = trim((string) $name);
|
||||
$labels[$code] = $name !== '' ? $name : ('Section #' . $code);
|
||||
}
|
||||
|
||||
$grid = [];
|
||||
$locked = false;
|
||||
|
||||
if ($sectionCode > 0) {
|
||||
$assigned = TeacherClass::assignedForSectionTerm($sectionCode, $semester, $schoolYear);
|
||||
$teacherIds = array_values(array_filter(array_map(static fn ($r) => (int) ($r['teacher_id'] ?? 0), $assigned)));
|
||||
|
||||
$statusMap = [];
|
||||
if ($teacherIds !== []) {
|
||||
$rows = DB::table('staff_attendance as sa')
|
||||
->select(['sa.user_id', 'sa.status', 'sa.reason'])
|
||||
->where('sa.semester', $semester)
|
||||
->where('sa.school_year', $schoolYear)
|
||||
->whereDate('sa.date', $dateYmd)
|
||||
->whereIn('sa.user_id', $teacherIds)
|
||||
->get();
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$statusMap[(int) $r->user_id] = [
|
||||
'status' => $r->status ?? null,
|
||||
'reason' => $r->reason ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($assigned as $t) {
|
||||
$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);
|
||||
|
||||
$cell = $statusMap[$tid] ?? ['status' => null, 'reason' => null];
|
||||
|
||||
$grid[] = [
|
||||
'teacher_id' => $tid,
|
||||
'name' => $name,
|
||||
'position' => $pos,
|
||||
'status' => $cell['status'],
|
||||
'reason' => $cell['reason'],
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
$locked = AttendanceDay::isFinalized($sectionCode, $dateYmd, $semester, $schoolYear);
|
||||
} catch (\Throwable) {
|
||||
$locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'date' => $dateYmd,
|
||||
'class_section_id' => $sectionCode,
|
||||
'sections' => $labels,
|
||||
'grid' => $grid,
|
||||
'locked' => $locked,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk save for CI {@see \App\Controllers\View\AttendanceController::save} (method missing in CI; implemented here).
|
||||
*/
|
||||
public function saveTeacherAttendanceBulk(Authenticatable $user, array $payload): array
|
||||
{
|
||||
$sectionCode = (int) ($payload['class_section_id'] ?? 0);
|
||||
$date = substr((string) ($payload['date'] ?? ''), 0, 10);
|
||||
$semester = (string) ($payload['semester'] ?? '');
|
||||
$schoolYear = (string) ($payload['school_year'] ?? '');
|
||||
$teachers = (array) ($payload['teachers'] ?? []);
|
||||
|
||||
if ($sectionCode <= 0 || $date === '' || $semester === '' || $schoolYear === '') {
|
||||
throw new \RuntimeException('Missing required fields.');
|
||||
}
|
||||
|
||||
$assigned = TeacherClass::assignedForSectionTerm($sectionCode, $semester, $schoolYear);
|
||||
$assignedByTeacher = [];
|
||||
foreach ($assigned as $row) {
|
||||
$assignedByTeacher[(int) ($row['teacher_id'] ?? 0)] = $row;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($teachers, $assignedByTeacher, $date, $semester, $schoolYear) {
|
||||
foreach ($teachers as $tidKey => $row) {
|
||||
$tid = (int) (is_numeric((string) $tidKey) ? $tidKey : ($row['teacher_id'] ?? 0));
|
||||
if ($tid <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$status = strtolower(trim((string) ($row['status'] ?? '')));
|
||||
$reason = trim((string) ($row['reason'] ?? ''));
|
||||
|
||||
$assignment = $assignedByTeacher[$tid] ?? null;
|
||||
$roleLabel = $assignment
|
||||
? ((strtolower((string) ($assignment['position'] ?? '')) === 'ta') ? 'Teacher Assistant' : 'Teacher')
|
||||
: 'Teacher';
|
||||
|
||||
if ($status === '') {
|
||||
DB::table('staff_attendance')
|
||||
->where('user_id', $tid)
|
||||
->whereDate('date', $date)
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array($status, ['present', 'absent', 'late'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DB::table('staff_attendance')->updateOrInsert(
|
||||
[
|
||||
'user_id' => $tid,
|
||||
'date' => $date,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
],
|
||||
[
|
||||
'role_name' => $roleLabel,
|
||||
'status' => $status,
|
||||
'reason' => $reason !== '' ? $reason : null,
|
||||
'updated_at' => now(),
|
||||
'created_at' => now(),
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return ['ok' => true, 'message' => 'Teacher attendance saved successfully.'];
|
||||
}
|
||||
|
||||
public function monthData(?string $semester, ?string $schoolYear): array
|
||||
{
|
||||
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
|
||||
|
||||
Reference in New Issue
Block a user