Files
alrahma_sunday_school_api/app/Models/StudentClass.php
T
2026-03-05 12:29:37 -05:00

110 lines
3.8 KiB
PHP

<?php
namespace App\Models;
use App\Models\BaseModel;
class StudentClass extends BaseModel
{
protected $table = 'student_class';
protected $primaryKey = 'id';
protected $fillable = [
'student_id',
'class_section_id',
'is_event_only',
'semester',
'school_year',
'description',
'created_at',
'updated_at',
'updated_by',
];
public $timestamps = true;
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
public function getClassSectionNameByStudentId(int $studentId): ?string
{
return $this->db->table($this->table)
->select('cs.class_section_name')
->join('class_sections as cs', 'cs.class_section_id', '=', 'student_class.class_section_id')
->where('student_class.student_id', $studentId)
->get()
->getRow('class_section_name');
}
// Custom findAll() method
public function findAll($limit = 0, $offset = 0)
{
// Optional: Add custom logic before fetching all records
// For example, you can add default conditions:
// $this->where('semester', 'Fall');
// Then call the parent findAll method to fetch the records
return parent::findAll($limit, $offset);
}
// Method to get all students in a specific class section
public function getClassStudents($classSectionId)
{
return $this->where('class_section_id', $classSectionId)->findAll();
}
public function getClassSectionsByStudentId($studentId, string $schoolYear)
{
// Build the query
$builder = $this->db->table('student_class')
->select('cs.class_section_name')
->join('class_sections as cs', 'student_class.class_section_id', '=', 'cs.class_section_id')
->where('student_class.student_id', $studentId) // Filter by student ID
->where('student_class.class_section_id IS NOT NULL') // Ensure class_section_id is not null
->where('student_class.school_year', $schoolYear); // Filter by school year
// Fetch the result
$result = $builder->get()->getRowArray();
// Check if the result is not null before accessing the 'class_section_name'
if ($result && isset($result['class_section_name'])) {
return $result['class_section_name']; // Return the class_section_name
} else {
return ''; // Return an empty string if no class section is found
}
}
public function getStudentsByClassSectionIds(array $classSectionIds)
{
return $this->select('students.id as student_id, students.firstname, students.lastname, students.school_id, students.is_new,students.photo_consent ,students.age, student_class.semester, student_class.school_year, student_class.class_section_id')
->join('students', 'students.id', '=', 'student_class.student_id')
->whereIn('student_class.class_section_id', $classSectionIds)
->get()
->getResultArray();
}
/**
* Get the class ID (grade) for a specific student.
*
* @param int $studentId
* @return string class_id or 'N/A'
*/
public function getStudentGrade(int $studentId): string
{
$studentClass = $this->where('student_id', $studentId)
->orderBy('created_at', 'DESC') // in case of multiple entries, get latest
->first();
if ($studentClass && isset($studentClass['class_section_id'])) {
$classSection = $this->db->table('class_sections')
->where('class_section_id', $studentClass['class_section_id'])
->get()
->getRowArray();
return $classSection['class_id'] ?? 'N/A';
}
log_message('error', "Student class or section not found for student ID: $studentId");
return 'N/A';
}
}