update policy and fix other issues
This commit is contained in:
@@ -55,7 +55,12 @@ class StudentClassModel extends Model
|
||||
return $this->db->table($this->table);
|
||||
}
|
||||
|
||||
private function includeActiveOrTerminalEnrollment(BaseBuilder $builder, string $schoolYear): BaseBuilder
|
||||
private function includeActiveOrTerminalEnrollment(
|
||||
BaseBuilder $builder,
|
||||
string $schoolYear,
|
||||
bool $hasEnrollmentStatus = false,
|
||||
bool $hasEnrollmentWithdrawn = false
|
||||
): BaseBuilder
|
||||
{
|
||||
$terminalStatuses = array_map([$this->db, 'escape'], [
|
||||
'denied',
|
||||
@@ -66,13 +71,16 @@ class StudentClassModel extends Model
|
||||
$builder->groupStart()
|
||||
->where('students.is_active', 1);
|
||||
|
||||
if ($schoolYear !== '') {
|
||||
if ($schoolYear !== '' && $hasEnrollmentStatus) {
|
||||
$builder->orWhere(
|
||||
'LOWER(TRIM(enrollments.enrollment_status)) IN (' . implode(',', $terminalStatuses) . ')',
|
||||
null,
|
||||
false
|
||||
)
|
||||
->orWhere('enrollments.is_withdrawn', 1);
|
||||
);
|
||||
}
|
||||
|
||||
if ($schoolYear !== '' && $hasEnrollmentWithdrawn) {
|
||||
$builder->orWhere('enrollments.is_withdrawn', 1);
|
||||
}
|
||||
|
||||
return $builder->groupEnd();
|
||||
@@ -446,44 +454,61 @@ class StudentClassModel extends Model
|
||||
|
||||
$schoolYear = $schoolYear !== null ? trim($schoolYear) : '';
|
||||
|
||||
$hasStudentYearStatus = $this->db->tableExists('student_year_status')
|
||||
&& $this->db->fieldExists('is_new', 'student_year_status');
|
||||
$hasEventOnly = $this->db->fieldExists('is_event_only', 'student_class');
|
||||
$hasEnrollments = $this->db->tableExists('enrollments');
|
||||
$hasEnrollmentStatus = $hasEnrollments && $this->db->fieldExists('enrollment_status', 'enrollments');
|
||||
$hasEnrollmentWithdrawn = $hasEnrollments && $this->db->fieldExists('is_withdrawn', 'enrollments');
|
||||
|
||||
$select = [
|
||||
'students.id AS student_id',
|
||||
'students.firstname',
|
||||
'students.lastname',
|
||||
'students.school_id',
|
||||
'students.is_active',
|
||||
$hasStudentYearStatus ? 'COALESCE(sys.is_new, 1) AS is_new' : '1 AS is_new',
|
||||
'students.photo_consent',
|
||||
'students.age',
|
||||
'student_class.school_year',
|
||||
'student_class.class_section_id',
|
||||
$hasEventOnly ? 'student_class.is_event_only' : '0 AS is_event_only',
|
||||
$hasEnrollmentStatus ? 'enrollments.enrollment_status' : 'NULL AS enrollment_status',
|
||||
$hasEnrollmentWithdrawn ? 'enrollments.is_withdrawn' : '0 AS is_withdrawn',
|
||||
];
|
||||
|
||||
$builder = $this->freshBuilder()
|
||||
->select([
|
||||
'students.id AS student_id',
|
||||
'students.firstname',
|
||||
'students.lastname',
|
||||
'students.school_id',
|
||||
'students.is_active',
|
||||
'COALESCE(sys.is_new, 1) AS 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',
|
||||
])
|
||||
->select($select)
|
||||
->join(
|
||||
'students',
|
||||
'students.id = student_class.student_id',
|
||||
'inner'
|
||||
)
|
||||
->join(
|
||||
);
|
||||
|
||||
if ($hasStudentYearStatus) {
|
||||
$builder->join(
|
||||
'student_year_status sys',
|
||||
$schoolYear !== ''
|
||||
? 'sys.student_id = students.id AND sys.school_year = ' . $this->db->escape($schoolYear)
|
||||
: 'sys.student_id = students.id AND sys.school_year = student_class.school_year',
|
||||
'left'
|
||||
)
|
||||
->join(
|
||||
'left',
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
if ($hasEnrollments) {
|
||||
$builder->join(
|
||||
'enrollments',
|
||||
$this->latestEnrollmentJoinCondition($schoolYear),
|
||||
'left',
|
||||
false
|
||||
)
|
||||
->whereIn(
|
||||
'student_class.class_section_id',
|
||||
$classSectionIds
|
||||
);
|
||||
}
|
||||
|
||||
$builder->whereIn(
|
||||
'student_class.class_section_id',
|
||||
$classSectionIds
|
||||
);
|
||||
|
||||
if ($schoolYear !== '') {
|
||||
$builder->where(
|
||||
@@ -492,7 +517,12 @@ class StudentClassModel extends Model
|
||||
);
|
||||
}
|
||||
|
||||
$this->includeActiveOrTerminalEnrollment($builder, $schoolYear);
|
||||
$this->includeActiveOrTerminalEnrollment(
|
||||
$builder,
|
||||
$schoolYear,
|
||||
$hasEnrollmentStatus,
|
||||
$hasEnrollmentWithdrawn
|
||||
);
|
||||
|
||||
return $builder
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
|
||||
Reference in New Issue
Block a user