fix is_active student flag and apply it in all pages
This commit is contained in:
@@ -55,6 +55,32 @@ class StudentClassModel extends Model
|
||||
return $this->db->table($this->table);
|
||||
}
|
||||
|
||||
private function includeActiveOrTerminalEnrollment(BaseBuilder $builder, string $schoolYear): BaseBuilder
|
||||
{
|
||||
$terminalStatuses = array_map([$this->db, 'escape'], [
|
||||
'denied',
|
||||
'withdrawn',
|
||||
'widthran',
|
||||
'widthrawan',
|
||||
'withdrawan',
|
||||
'waitlist',
|
||||
]);
|
||||
|
||||
$builder->groupStart()
|
||||
->where('students.is_active', 1);
|
||||
|
||||
if ($schoolYear !== '') {
|
||||
$builder->orWhere(
|
||||
'LOWER(TRIM(enrollments.enrollment_status)) IN (' . implode(',', $terminalStatuses) . ')',
|
||||
null,
|
||||
false
|
||||
)
|
||||
->orWhere('enrollments.is_withdrawn', 1);
|
||||
}
|
||||
|
||||
return $builder->groupEnd();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh builder scoped to active students.
|
||||
*/
|
||||
@@ -131,7 +157,8 @@ class StudentClassModel extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active students assigned to a class section.
|
||||
* Get students assigned to a class section, including inactive students with
|
||||
* terminal current-year enrollment statuses.
|
||||
*
|
||||
* student_class is scoped by school year only. It has no semester column.
|
||||
*/
|
||||
@@ -139,7 +166,22 @@ class StudentClassModel extends Model
|
||||
int $classSectionId,
|
||||
?string $schoolYear = null
|
||||
): array {
|
||||
$builder = $this->activeStudentsBuilder()
|
||||
$schoolYear = $schoolYear !== null ? trim($schoolYear) : '';
|
||||
|
||||
$builder = $this->freshBuilder()
|
||||
->select('student_class.*, students.is_active, enrollments.enrollment_status, enrollments.is_withdrawn')
|
||||
->join(
|
||||
'students',
|
||||
'students.id = student_class.student_id',
|
||||
'inner'
|
||||
)
|
||||
->join(
|
||||
'enrollments',
|
||||
$schoolYear !== ''
|
||||
? 'enrollments.student_id = student_class.student_id AND enrollments.school_year = ' . $this->db->escape($schoolYear)
|
||||
: 'enrollments.student_id = student_class.student_id',
|
||||
'left'
|
||||
)
|
||||
->where(
|
||||
'student_class.class_section_id',
|
||||
$classSectionId
|
||||
@@ -150,13 +192,15 @@ class StudentClassModel extends Model
|
||||
false
|
||||
);
|
||||
|
||||
if ($schoolYear !== null && trim($schoolYear) !== '') {
|
||||
if ($schoolYear !== '') {
|
||||
$builder->where(
|
||||
'student_class.school_year',
|
||||
trim($schoolYear)
|
||||
$schoolYear
|
||||
);
|
||||
}
|
||||
|
||||
$this->includeActiveOrTerminalEnrollment($builder, $schoolYear);
|
||||
|
||||
$rows = $builder
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
@@ -195,7 +239,6 @@ class StudentClassModel extends Model
|
||||
'student_class.class_section_id',
|
||||
$classSectionId
|
||||
)
|
||||
->where('students.is_active', 1)
|
||||
->where(
|
||||
'student_class.class_section_id IS NOT NULL',
|
||||
null,
|
||||
@@ -206,9 +249,16 @@ class StudentClassModel extends Model
|
||||
$builder->where(
|
||||
'student_class.school_year',
|
||||
$schoolYear
|
||||
)
|
||||
->join(
|
||||
'enrollments',
|
||||
'enrollments.student_id = student_class.student_id AND enrollments.school_year = ' . $this->db->escape($schoolYear),
|
||||
'left'
|
||||
);
|
||||
}
|
||||
|
||||
$this->includeActiveOrTerminalEnrollment($builder, $schoolYear);
|
||||
|
||||
$rows = $builder
|
||||
->groupBy('student_class.student_id')
|
||||
->get()
|
||||
@@ -364,7 +414,8 @@ class StudentClassModel extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Return active students for a set of class-section IDs.
|
||||
* Return students for a set of class-section IDs, including inactive
|
||||
* students with terminal current-year enrollment statuses.
|
||||
*/
|
||||
public function getStudentsByClassSectionIds(
|
||||
array $classSectionIds,
|
||||
@@ -379,37 +430,50 @@ class StudentClassModel extends Model
|
||||
return [];
|
||||
}
|
||||
|
||||
$schoolYear = $schoolYear !== null ? trim($schoolYear) : '';
|
||||
|
||||
$builder = $this->freshBuilder()
|
||||
->select([
|
||||
'students.id AS student_id',
|
||||
'students.firstname',
|
||||
'students.lastname',
|
||||
'students.school_id',
|
||||
'students.is_active',
|
||||
'students.is_new',
|
||||
'students.photo_consent',
|
||||
'students.age',
|
||||
'student_class.school_year',
|
||||
'student_class.class_section_id',
|
||||
'student_class.is_event_only',
|
||||
'enrollments.enrollment_status',
|
||||
'enrollments.is_withdrawn',
|
||||
])
|
||||
->join(
|
||||
'students',
|
||||
'students.id = student_class.student_id',
|
||||
'inner'
|
||||
)
|
||||
->join(
|
||||
'enrollments',
|
||||
$schoolYear !== ''
|
||||
? 'enrollments.student_id = student_class.student_id AND enrollments.school_year = ' . $this->db->escape($schoolYear)
|
||||
: 'enrollments.student_id = student_class.student_id',
|
||||
'left'
|
||||
)
|
||||
->whereIn(
|
||||
'student_class.class_section_id',
|
||||
$classSectionIds
|
||||
)
|
||||
->where('students.is_active', 1);
|
||||
);
|
||||
|
||||
if ($schoolYear !== null && trim($schoolYear) !== '') {
|
||||
if ($schoolYear !== '') {
|
||||
$builder->where(
|
||||
'student_class.school_year',
|
||||
trim($schoolYear)
|
||||
$schoolYear
|
||||
);
|
||||
}
|
||||
|
||||
$this->includeActiveOrTerminalEnrollment($builder, $schoolYear);
|
||||
|
||||
return $builder
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
@@ -504,7 +568,6 @@ class StudentClassModel extends Model
|
||||
'students.id = student_class.student_id',
|
||||
'inner'
|
||||
)
|
||||
->where('students.is_active', 1)
|
||||
->where(
|
||||
'student_class.class_section_id IS NOT NULL',
|
||||
null,
|
||||
@@ -513,12 +576,22 @@ class StudentClassModel extends Model
|
||||
->groupBy('student_class.class_section_id');
|
||||
|
||||
if ($schoolYear !== null && trim($schoolYear) !== '') {
|
||||
$schoolYear = trim($schoolYear);
|
||||
$builder->where(
|
||||
'student_class.school_year',
|
||||
trim($schoolYear)
|
||||
$schoolYear
|
||||
)
|
||||
->join(
|
||||
'enrollments',
|
||||
'enrollments.student_id = student_class.student_id AND enrollments.school_year = ' . $this->db->escape($schoolYear),
|
||||
'left'
|
||||
);
|
||||
} else {
|
||||
$schoolYear = '';
|
||||
}
|
||||
|
||||
$this->includeActiveOrTerminalEnrollment($builder, $schoolYear);
|
||||
|
||||
$rows = $builder
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
+109
-21
@@ -292,25 +292,42 @@ class StudentModel extends Model
|
||||
|
||||
public function getByClassAndYear($class_section_id, $school_year, ?string $semester = null): array
|
||||
{
|
||||
$studentClassModel = new \App\Models\StudentClassModel();
|
||||
$school_year = trim((string) $school_year);
|
||||
|
||||
$builder = $studentClassModel
|
||||
->select('student_id')
|
||||
->where('class_section_id', $class_section_id)
|
||||
->where('school_year', $school_year);
|
||||
$builder = $this->select('students.*, e.enrollment_status, e.is_withdrawn')
|
||||
->join('student_class sc', 'sc.student_id = students.id', 'inner')
|
||||
->join(
|
||||
'enrollments e',
|
||||
'e.student_id = students.id AND e.school_year = ' . $this->db->escape($school_year),
|
||||
'left'
|
||||
)
|
||||
->where('sc.class_section_id', $class_section_id)
|
||||
->where('sc.school_year', $school_year)
|
||||
->groupStart()
|
||||
->where('students.is_active', 1)
|
||||
->orWhere("LOWER(TRIM(e.enrollment_status)) IN ('denied','withdrawn','widthran','widthrawan','withdrawan','waitlist')", null, false)
|
||||
->orWhere('e.is_withdrawn', 1)
|
||||
->groupEnd();
|
||||
|
||||
$studentIds = $builder->findAll();
|
||||
return $builder
|
||||
->distinct()
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
$studentIds = array_column($studentIds, 'student_id');
|
||||
public function getActiveByClassAndYear($class_section_id, $school_year, ?string $semester = null): array
|
||||
{
|
||||
$school_year = trim((string) $school_year);
|
||||
|
||||
if (empty($studentIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->whereIn('id', $studentIds)
|
||||
->where('is_active', 1)
|
||||
->orderBy('firstname', 'ASC')
|
||||
->orderBy('lastname', 'ASC')
|
||||
return $this->select('students.*')
|
||||
->join('student_class sc', 'sc.student_id = students.id', 'inner')
|
||||
->where('sc.class_section_id', $class_section_id)
|
||||
->where('sc.school_year', $school_year)
|
||||
->where('students.is_active', 1)
|
||||
->distinct()
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
@@ -322,12 +339,36 @@ class StudentModel extends Model
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->select('students.id, students.school_id, students.firstname, students.lastname')
|
||||
return $this->select('students.id, students.school_id, students.firstname, students.lastname, students.is_active, e.enrollment_status, e.is_withdrawn')
|
||||
->join('student_class', 'student_class.student_id = students.id', 'inner')
|
||||
->join('enrollments e', 'e.student_id = students.id AND e.school_year = ' . $this->db->escape($schoolYear), 'left')
|
||||
->where('students.parent_id', $parentId)
|
||||
->where('student_class.school_year', $schoolYear)
|
||||
->groupStart()
|
||||
->where('students.is_active', 1)
|
||||
->orWhere("LOWER(TRIM(e.enrollment_status)) IN ('denied','withdrawn','widthran','widthrawan','withdrawan','waitlist')", null, false)
|
||||
->orWhere('e.is_withdrawn', 1)
|
||||
->groupEnd()
|
||||
->groupBy('students.id, students.school_id, students.firstname, students.lastname, students.is_active, e.enrollment_status, e.is_withdrawn')
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
public function getActiveByParentAndYear(int $parentId, string $schoolYear): array
|
||||
{
|
||||
$schoolYear = trim($schoolYear);
|
||||
|
||||
if ($parentId <= 0 || $schoolYear === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->select('students.id, students.school_id, students.firstname, students.lastname, students.is_active')
|
||||
->join('student_class', 'student_class.student_id = students.id', 'inner')
|
||||
->where('students.parent_id', $parentId)
|
||||
->where('students.is_active', 1)
|
||||
->where('student_class.school_year', $schoolYear)
|
||||
->groupBy('students.id, students.school_id, students.firstname, students.lastname')
|
||||
->where('students.is_active', 1)
|
||||
->groupBy('students.id, students.school_id, students.firstname, students.lastname, students.is_active')
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->orderBy('students.firstname', 'ASC')
|
||||
->findAll();
|
||||
@@ -443,16 +484,28 @@ class StudentModel extends Model
|
||||
$builder = $this->builder();
|
||||
|
||||
$builder
|
||||
->select('students.id AS student_id, students.school_id, students.firstname, students.lastname, sc.class_section_id')
|
||||
->select('students.id AS student_id, students.school_id, students.firstname, students.lastname, students.is_active, sc.class_section_id, e.enrollment_status, e.is_withdrawn')
|
||||
->join('student_class sc', 'sc.student_id = students.id', 'inner')
|
||||
->where('sc.class_section_id', $classSectionId)
|
||||
->where('students.is_active', 1);
|
||||
->where('sc.class_section_id', $classSectionId);
|
||||
|
||||
// Optional term scoping (safe no-ops if null)
|
||||
if ($schoolYear !== null) {
|
||||
$builder->where('sc.school_year', $schoolYear);
|
||||
$builder->join(
|
||||
'enrollments e',
|
||||
'e.student_id = students.id AND e.school_year = ' . $this->db->escape($schoolYear),
|
||||
'left'
|
||||
);
|
||||
} else {
|
||||
$builder->join('enrollments e', 'e.student_id = students.id', 'left');
|
||||
}
|
||||
|
||||
$builder->groupStart()
|
||||
->where('students.is_active', 1)
|
||||
->orWhere("LOWER(TRIM(e.enrollment_status)) IN ('denied','withdrawn','widthran','widthrawan','withdrawan','waitlist')", null, false)
|
||||
->orWhere('e.is_withdrawn', 1)
|
||||
->groupEnd();
|
||||
|
||||
// Prevent accidental dupes if student_class has multiple rows
|
||||
$builder->distinct();
|
||||
$builder->orderBy('students.lastname', 'ASC')
|
||||
@@ -472,6 +525,41 @@ class StudentModel extends Model
|
||||
return $results ?: [];
|
||||
}
|
||||
|
||||
public function getActiveStudentInfoByClassSectionId($classSectionId, ?string $semester = null, ?string $schoolYear = null): array
|
||||
{
|
||||
$classSectionId = (int) $classSectionId;
|
||||
if ($classSectionId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$builder = $this->builder();
|
||||
$builder
|
||||
->select('students.id AS student_id, students.school_id, students.firstname, students.lastname, students.is_active, sc.class_section_id')
|
||||
->join('student_class sc', 'sc.student_id = students.id', 'inner')
|
||||
->where('sc.class_section_id', $classSectionId)
|
||||
->where('students.is_active', 1);
|
||||
|
||||
if ($schoolYear !== null) {
|
||||
$builder->where('sc.school_year', $schoolYear);
|
||||
}
|
||||
|
||||
$builder->distinct();
|
||||
$builder->orderBy('students.lastname', 'ASC')
|
||||
->orderBy('students.firstname', 'ASC');
|
||||
|
||||
$results = $builder->get()->getResultArray();
|
||||
|
||||
$userId = (int) (session()->get('user_id') ?? 0);
|
||||
if (!empty($results)) {
|
||||
foreach ($results as &$row) {
|
||||
$row['updated_by'] = $userId;
|
||||
}
|
||||
unset($row);
|
||||
}
|
||||
|
||||
return $results ?: [];
|
||||
}
|
||||
|
||||
|
||||
// In your existing StudentModel
|
||||
public function getStudentBasic(int $studentId): ?array
|
||||
|
||||
Reference in New Issue
Block a user