init backend presensi

This commit is contained in:
mwpn
2026-03-05 14:37:36 +07:00
commit b4fda6b9c9
319 changed files with 27261 additions and 0 deletions

View File

@@ -0,0 +1 @@
# Attendance Module - Models

View File

@@ -0,0 +1,88 @@
<?php
namespace App\Modules\Attendance\Models;
use App\Modules\Attendance\Entities\AttendanceSession;
use CodeIgniter\Model;
/**
* Attendance Session Model
*
* Handles database operations for attendance sessions.
*/
class AttendanceSessionModel extends Model
{
protected $table = 'attendance_sessions';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = AttendanceSession::class;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'student_id',
'schedule_id',
'checkin_type',
'attendance_date',
'device_id',
'checkin_at',
'latitude',
'longitude',
'confidence',
'status',
];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [
'student_id' => 'required|integer|is_not_unique[students.id]',
'schedule_id' => 'permit_empty|integer|is_not_unique[schedules.id]',
'checkin_type' => 'permit_empty|in_list[mapel,masuk,pulang]',
'attendance_date' => 'required|valid_date[Y-m-d]',
'device_id' => 'required|integer|is_not_unique[devices.id]',
'checkin_at' => 'required|valid_date[Y-m-d H:i:s]',
'latitude' => 'required|decimal',
'longitude' => 'required|decimal',
'confidence' => 'permit_empty|decimal',
'status' => 'required|in_list[PRESENT,LATE,OUTSIDE_ZONE,NO_SCHEDULE,INVALID_DEVICE]',
];
/**
* Check if attendance already exists for student + schedule on the given date (server date).
* Used for duplicate protection: one attendance per (student_id, schedule_id, attendance_date).
*
* @param int $studentId
* @param int $scheduleId
* @param string $attendanceDate Date in Y-m-d format (use server date)
* @return bool
*/
public function hasAttendanceFor(int $studentId, int $scheduleId, string $attendanceDate): bool
{
$row = $this->where('student_id', $studentId)
->where('schedule_id', $scheduleId)
->where('attendance_date', $attendanceDate)
->first();
return $row !== null;
}
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Modules\Attendance\Models;
use CodeIgniter\Model;
/**
* Token QR untuk absen mapel: guru generate, siswa scan.
*/
class QrAttendanceTokenModel extends Model
{
protected $table = 'qr_attendance_tokens';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $allowedFields = [
'schedule_id',
'token',
'expires_at',
'created_by_user_id',
];
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
/** Token valid (default) 15 menit */
public const VALID_MINUTES = 15;
/**
* Generate token untuk schedule_id. Returns token string or null on failure.
*/
public function generateForSchedule(int $scheduleId, ?int $createdByUserId = null): ?string
{
$token = bin2hex(random_bytes(16));
$expires = date('Y-m-d H:i:s', strtotime('+' . self::VALID_MINUTES . ' minutes'));
$id = $this->insert([
'schedule_id' => $scheduleId,
'token' => $token,
'expires_at' => $expires,
'created_by_user_id' => $createdByUserId,
]);
return $id ? $token : null;
}
/**
* Validate token: return row (schedule_id, expires_at) if valid and not expired; null otherwise.
*/
public function validateToken(string $token): ?array
{
$row = $this->where('token', $token)->first();
if (!$row || !is_array($row)) {
return null;
}
$expiresAt = $row['expires_at'] ?? null;
if (!$expiresAt || strtotime($expiresAt) < time()) {
return null;
}
return [
'schedule_id' => (int) $row['schedule_id'],
'expires_at' => $row['expires_at'],
];
}
}