init backend presensi
This commit is contained in:
57
app/Modules/Academic/Models/ClassModel.php
Normal file
57
app/Modules/Academic/Models/ClassModel.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use App\Modules\Academic\Entities\ClassEntity;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Class Model
|
||||
*
|
||||
* Handles database operations for classes.
|
||||
*/
|
||||
class ClassModel extends Model
|
||||
{
|
||||
protected $table = 'classes';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = ClassEntity::class;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
'name',
|
||||
'grade',
|
||||
'major',
|
||||
'wali_user_id',
|
||||
];
|
||||
|
||||
// Dates
|
||||
protected $useTimestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
|
||||
// Validation
|
||||
protected $validationRules = [
|
||||
'name' => 'required|max_length[100]',
|
||||
'grade' => 'required|max_length[50]',
|
||||
'major' => 'required|max_length[50]',
|
||||
'wali_user_id' => 'permit_empty|integer|is_not_unique[users.id]',
|
||||
];
|
||||
|
||||
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 = [];
|
||||
}
|
||||
30
app/Modules/Academic/Models/DapodikRombelMappingModel.php
Normal file
30
app/Modules/Academic/Models/DapodikRombelMappingModel.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Dapodik rombel string -> internal class_id mapping.
|
||||
*/
|
||||
class DapodikRombelMappingModel extends Model
|
||||
{
|
||||
protected $table = 'dapodik_rombel_mappings';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $allowedFields = [
|
||||
'dapodik_rombel',
|
||||
'class_id',
|
||||
'last_seen_at',
|
||||
];
|
||||
|
||||
public function getByRombel(string $rombel): ?array
|
||||
{
|
||||
$row = $this->where('dapodik_rombel', $rombel)->first();
|
||||
return $row !== null ? $row : null;
|
||||
}
|
||||
}
|
||||
36
app/Modules/Academic/Models/DapodikSyncJobModel.php
Normal file
36
app/Modules/Academic/Models/DapodikSyncJobModel.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Dapodik sync job tracker for progress reporting.
|
||||
*/
|
||||
class DapodikSyncJobModel extends Model
|
||||
{
|
||||
protected $table = 'dapodik_sync_jobs';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $allowedFields = [
|
||||
'type',
|
||||
'total_rows',
|
||||
'processed_rows',
|
||||
'status',
|
||||
'message',
|
||||
'started_at',
|
||||
'finished_at',
|
||||
];
|
||||
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_RUNNING = 'running';
|
||||
public const STATUS_COMPLETED = 'completed';
|
||||
public const STATUS_FAILED = 'failed';
|
||||
|
||||
public const TYPE_STUDENTS = 'students';
|
||||
public const TYPE_CLASSES = 'classes';
|
||||
}
|
||||
91
app/Modules/Academic/Models/LessonSlotModel.php
Normal file
91
app/Modules/Academic/Models/LessonSlotModel.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use App\Modules\Academic\Entities\LessonSlot;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Lesson Slot Model
|
||||
*/
|
||||
class LessonSlotModel extends Model
|
||||
{
|
||||
protected $table = 'lesson_slots';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = LessonSlot::class;
|
||||
protected $allowedFields = [
|
||||
'slot_number',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $useTimestamps = false;
|
||||
|
||||
protected $validationRules = [
|
||||
'slot_number' => 'required|integer|greater_than[0]|is_unique[lesson_slots.slot_number,id,{id}]',
|
||||
'start_time' => 'required|valid_date[H:i:s]',
|
||||
'end_time' => 'required|valid_date[H:i:s]',
|
||||
'is_active' => 'permit_empty|in_list[0,1]',
|
||||
];
|
||||
|
||||
protected $validationMessages = [
|
||||
'end_time' => [
|
||||
'required' => 'End time is required.',
|
||||
],
|
||||
];
|
||||
|
||||
protected function getSlotStartBeforeEndRule(): array
|
||||
{
|
||||
return [
|
||||
'end_time' => 'required|valid_date[H:i:s]|greater_than_field[start_time]',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that start_time < end_time (custom rule or after validation).
|
||||
*/
|
||||
public function validateStartBeforeEnd(array $data): bool
|
||||
{
|
||||
$start = $data['start_time'] ?? '';
|
||||
$end = $data['end_time'] ?? '';
|
||||
if ($start === '' || $end === '') {
|
||||
return true;
|
||||
}
|
||||
return strtotime($start) < strtotime($end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override insert to check start < end.
|
||||
*/
|
||||
public function insert($data = null, bool $returnID = true)
|
||||
{
|
||||
if (is_array($data) && !$this->validateStartBeforeEnd($data)) {
|
||||
$this->errors['end_time'] = 'End time must be after start time.';
|
||||
return false;
|
||||
}
|
||||
return parent::insert($data, $returnID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override update to check start < end (merge with existing when partial).
|
||||
*/
|
||||
public function update($id = null, $data = null): bool
|
||||
{
|
||||
if (is_array($data) && (array_key_exists('start_time', $data) || array_key_exists('end_time', $data))) {
|
||||
$existing = $this->find($id);
|
||||
$exStart = $existing && is_object($existing) ? $existing->start_time : ($existing['start_time'] ?? '');
|
||||
$exEnd = $existing && is_object($existing) ? $existing->end_time : ($existing['end_time'] ?? '');
|
||||
$merged = [
|
||||
'start_time' => $data['start_time'] ?? $exStart,
|
||||
'end_time' => $data['end_time'] ?? $exEnd,
|
||||
];
|
||||
if (!$this->validateStartBeforeEnd($merged)) {
|
||||
$this->errors['end_time'] = 'End time must be after start time.';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return parent::update($id, $data);
|
||||
}
|
||||
}
|
||||
195
app/Modules/Academic/Models/ScheduleModel.php
Normal file
195
app/Modules/Academic/Models/ScheduleModel.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use App\Modules\Academic\Entities\Schedule;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Schedule Model
|
||||
*
|
||||
* Handles database operations for schedules.
|
||||
*/
|
||||
class ScheduleModel extends Model
|
||||
{
|
||||
protected $table = 'schedules';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = Schedule::class;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
'class_id',
|
||||
'subject_id',
|
||||
'teacher_user_id',
|
||||
'teacher_name',
|
||||
'lesson_slot_id',
|
||||
'day_of_week',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'room',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
// Dates
|
||||
protected $useTimestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
|
||||
// Validation
|
||||
protected $validationRules = [
|
||||
'class_id' => 'required|integer|is_not_unique[classes.id]',
|
||||
'subject_id' => 'required|integer|is_not_unique[subjects.id]',
|
||||
'teacher_name' => 'required|max_length[255]',
|
||||
'day_of_week' => 'required|integer|greater_than_equal_to[1]|less_than_equal_to[7]',
|
||||
'start_time' => 'required|valid_date[H:i:s]',
|
||||
'end_time' => 'required|valid_date[H:i:s]',
|
||||
'room' => 'permit_empty|max_length[100]',
|
||||
];
|
||||
|
||||
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 = [];
|
||||
|
||||
/**
|
||||
* Find active schedule for class on specific day and time (uses schedule columns only).
|
||||
*
|
||||
* @deprecated Prefer getActiveScheduleRow() for slot/user as source of truth with fallback.
|
||||
* @param int $classId
|
||||
* @param int $dayOfWeek
|
||||
* @param string $time Time in H:i:s format
|
||||
* @return Schedule|null
|
||||
*/
|
||||
public function findActiveSchedule(int $classId, int $dayOfWeek, string $time): ?Schedule
|
||||
{
|
||||
return $this->where('class_id', $classId)
|
||||
->where('day_of_week', $dayOfWeek)
|
||||
->where('start_time <=', $time)
|
||||
->where('end_time >', $time)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active schedule row with lesson_slots and users as source of truth.
|
||||
* start_time, end_time from lesson_slots; teacher_name from users; fallback to schedule columns when NULL.
|
||||
*
|
||||
* @param int $classId
|
||||
* @param int $dayOfWeek
|
||||
* @param string $time Time in H:i:s format
|
||||
* @return array{id: int, class_id: int, subject_id: int, teacher_user_id: int|null, teacher_name: string, room: string|null, start_time: string, end_time: string, day_of_week: int}|null
|
||||
*/
|
||||
public function getActiveScheduleRow(int $classId, int $dayOfWeek, string $time): ?array
|
||||
{
|
||||
$db = \Config\Database::connect();
|
||||
$sql = 'SELECT sch.id AS id, sch.class_id, sch.subject_id, sch.teacher_user_id, '
|
||||
. 'COALESCE(u.name, sch.teacher_name) AS teacher_name, sch.room, '
|
||||
. 'COALESCE(ls.start_time, sch.start_time) AS start_time, COALESCE(ls.end_time, sch.end_time) AS end_time, sch.day_of_week '
|
||||
. 'FROM schedules sch '
|
||||
. 'LEFT JOIN lesson_slots ls ON ls.id = sch.lesson_slot_id '
|
||||
. 'LEFT JOIN users u ON u.id = sch.teacher_user_id '
|
||||
. 'WHERE sch.class_id = ? AND sch.day_of_week = ? '
|
||||
. 'AND ( COALESCE(ls.start_time, sch.start_time) ) <= ? AND ( COALESCE(ls.end_time, sch.end_time) ) > ? '
|
||||
. 'LIMIT 1';
|
||||
$row = $db->query($sql, [$classId, $dayOfWeek, $time, $time])->getRowArray();
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => (int) $row['id'],
|
||||
'class_id' => (int) $row['class_id'],
|
||||
'subject_id' => (int) $row['subject_id'],
|
||||
'teacher_user_id' => isset($row['teacher_user_id']) ? (int) $row['teacher_user_id'] : null,
|
||||
'teacher_name' => (string) ($row['teacher_name'] ?? ''),
|
||||
'room' => isset($row['room']) ? (string) $row['room'] : null,
|
||||
'start_time' => (string) $row['start_time'],
|
||||
'end_time' => (string) $row['end_time'],
|
||||
'day_of_week' => (int) $row['day_of_week'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get latest schedule for class on given day where start_time <= time (may already be ended).
|
||||
* Uses lesson_slots/users as source of truth. ORDER BY start_time DESC, LIMIT 1.
|
||||
*
|
||||
* @param int $classId
|
||||
* @param int $dayOfWeek
|
||||
* @param string $time Time in H:i:s format
|
||||
* @return array{id: int, class_id: int, subject_id: int, teacher_user_id: int|null, teacher_name: string, room: string|null, start_time: string, end_time: string, day_of_week: int}|null
|
||||
*/
|
||||
public function getLatestScheduleForClassToday(int $classId, int $dayOfWeek, string $time): ?array
|
||||
{
|
||||
$db = \Config\Database::connect();
|
||||
$sql = 'SELECT sch.id AS id, sch.class_id, sch.subject_id, sch.teacher_user_id, '
|
||||
. 'COALESCE(u.name, sch.teacher_name) AS teacher_name, sch.room, '
|
||||
. 'COALESCE(ls.start_time, sch.start_time) AS start_time, COALESCE(ls.end_time, sch.end_time) AS end_time, sch.day_of_week '
|
||||
. 'FROM schedules sch '
|
||||
. 'LEFT JOIN lesson_slots ls ON ls.id = sch.lesson_slot_id '
|
||||
. 'LEFT JOIN users u ON u.id = sch.teacher_user_id '
|
||||
. 'WHERE sch.class_id = ? AND sch.day_of_week = ? '
|
||||
. 'AND ( COALESCE(ls.start_time, sch.start_time) ) <= ? '
|
||||
. 'ORDER BY ( COALESCE(ls.start_time, sch.start_time) ) DESC '
|
||||
. 'LIMIT 1';
|
||||
$row = $db->query($sql, [$classId, $dayOfWeek, $time])->getRowArray();
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => (int) $row['id'],
|
||||
'class_id' => (int) $row['class_id'],
|
||||
'subject_id' => (int) $row['subject_id'],
|
||||
'teacher_user_id' => isset($row['teacher_user_id']) ? (int) $row['teacher_user_id'] : null,
|
||||
'teacher_name' => (string) ($row['teacher_name'] ?? ''),
|
||||
'room' => isset($row['room']) ? (string) $row['room'] : null,
|
||||
'start_time' => (string) $row['start_time'],
|
||||
'end_time' => (string) $row['end_time'],
|
||||
'day_of_week' => (int) $row['day_of_week'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one schedule row by id with lesson_slots and users as source of truth.
|
||||
*
|
||||
* @param int $scheduleId
|
||||
* @return array{id: int, class_id: int, subject_id: int, teacher_user_id: int|null, teacher_name: string, start_time: string, end_time: string, day_of_week: int, room: string|null}|null
|
||||
*/
|
||||
public function getScheduleWithSlot(int $scheduleId): ?array
|
||||
{
|
||||
$db = \Config\Database::connect();
|
||||
$sql = 'SELECT sch.id AS id, sch.class_id, sch.subject_id, sch.teacher_user_id, '
|
||||
. 'COALESCE(u.name, sch.teacher_name) AS teacher_name, '
|
||||
. 'COALESCE(ls.start_time, sch.start_time) AS start_time, COALESCE(ls.end_time, sch.end_time) AS end_time, '
|
||||
. 'sch.day_of_week, sch.room '
|
||||
. 'FROM schedules sch '
|
||||
. 'LEFT JOIN lesson_slots ls ON ls.id = sch.lesson_slot_id '
|
||||
. 'LEFT JOIN users u ON u.id = sch.teacher_user_id '
|
||||
. 'WHERE sch.id = ? LIMIT 1';
|
||||
$row = $db->query($sql, [$scheduleId])->getRowArray();
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => (int) $row['id'],
|
||||
'class_id' => (int) $row['class_id'],
|
||||
'subject_id' => (int) $row['subject_id'],
|
||||
'teacher_user_id' => isset($row['teacher_user_id']) ? (int) $row['teacher_user_id'] : null,
|
||||
'teacher_name' => (string) ($row['teacher_name'] ?? ''),
|
||||
'start_time' => (string) $row['start_time'],
|
||||
'end_time' => (string) $row['end_time'],
|
||||
'day_of_week' => (int) $row['day_of_week'],
|
||||
'room' => isset($row['room']) ? (string) $row['room'] : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
77
app/Modules/Academic/Models/StudentModel.php
Normal file
77
app/Modules/Academic/Models/StudentModel.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use App\Modules\Academic\Entities\Student;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Student Model
|
||||
*
|
||||
* Handles database operations for students.
|
||||
*/
|
||||
class StudentModel extends Model
|
||||
{
|
||||
protected $table = 'students';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = Student::class;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
'dapodik_id',
|
||||
'face_external_id',
|
||||
'face_hash',
|
||||
'nisn',
|
||||
'name',
|
||||
'gender',
|
||||
'class_id',
|
||||
'is_active',
|
||||
'parent_link_code',
|
||||
];
|
||||
|
||||
// Dates
|
||||
protected $useTimestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
|
||||
// Validation (class_id nullable for unmapped/Dapodik)
|
||||
protected $validationRules = [
|
||||
'dapodik_id' => 'permit_empty|max_length[64]|is_unique[students.dapodik_id,id,{id}]',
|
||||
'face_external_id' => 'permit_empty|max_length[100]|is_unique[students.face_external_id,id,{id}]',
|
||||
'face_hash' => 'permit_empty|max_length[32]',
|
||||
'nisn' => 'required|max_length[50]|is_unique[students.nisn,id,{id}]',
|
||||
'name' => 'required|max_length[255]',
|
||||
'gender' => 'permit_empty|in_list[L,P]',
|
||||
'class_id' => 'permit_empty|integer|is_not_unique[classes.id]',
|
||||
'is_active' => 'permit_empty|in_list[0,1]',
|
||||
];
|
||||
|
||||
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 = [];
|
||||
|
||||
/**
|
||||
* Find student by NISN
|
||||
*
|
||||
* @param string $nisn
|
||||
* @return Student|null
|
||||
*/
|
||||
public function findByNisn(string $nisn): ?Student
|
||||
{
|
||||
return $this->where('nisn', $nisn)->first();
|
||||
}
|
||||
}
|
||||
53
app/Modules/Academic/Models/SubjectModel.php
Normal file
53
app/Modules/Academic/Models/SubjectModel.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use App\Modules\Academic\Entities\Subject;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Subject Model
|
||||
*
|
||||
* Handles database operations for subjects.
|
||||
*/
|
||||
class SubjectModel extends Model
|
||||
{
|
||||
protected $table = 'subjects';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = Subject::class;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
'name',
|
||||
'code',
|
||||
];
|
||||
|
||||
// Dates
|
||||
protected $useTimestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
|
||||
// Validation
|
||||
protected $validationRules = [
|
||||
'name' => 'required|max_length[255]',
|
||||
'code' => 'permit_empty|max_length[50]',
|
||||
];
|
||||
|
||||
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 = [];
|
||||
}
|
||||
56
app/Modules/Academic/Models/TeacherSubjectModel.php
Normal file
56
app/Modules/Academic/Models/TeacherSubjectModel.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Academic\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class TeacherSubjectModel extends Model
|
||||
{
|
||||
protected $table = 'teacher_subjects';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
'teacher_user_id',
|
||||
'subject_id',
|
||||
];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
|
||||
/**
|
||||
* Get subject IDs for a teacher.
|
||||
*
|
||||
* @param int $teacherId
|
||||
* @return array<int>
|
||||
*/
|
||||
public function getSubjectIdsForTeacher(int $teacherId): array
|
||||
{
|
||||
$rows = $this->select('subject_id')
|
||||
->where('teacher_user_id', $teacherId)
|
||||
->findAll();
|
||||
|
||||
return array_map(static fn ($row) => (int) $row['subject_id'], $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mapping: subject_id => [teacher_user_id, ...].
|
||||
*
|
||||
* @return array<int, array<int>>
|
||||
*/
|
||||
public function getMapSubjectToTeacherIds(): array
|
||||
{
|
||||
$rows = $this->select('teacher_user_id, subject_id')->findAll();
|
||||
$map = [];
|
||||
foreach ($rows as $r) {
|
||||
$s = (int) $r['subject_id'];
|
||||
$t = (int) $r['teacher_user_id'];
|
||||
$map[$s][] = $t;
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user