This commit is contained in:
@@ -48,6 +48,8 @@ class AssignmentController extends BaseController
|
||||
$selectedSemester = (string)($this->request->getGet('semester') ?? $this->semester ?? '');
|
||||
$year = (string)($this->schoolYear ?? '');
|
||||
|
||||
$this->applyPendingDistributionDraftsForEnrolledStudents($year);
|
||||
|
||||
$tcQ = $this->teacherClassModel;
|
||||
if ($year !== '') {
|
||||
$tcQ = $tcQ->where('school_year', $year);
|
||||
@@ -204,6 +206,94 @@ class AssignmentController extends BaseController
|
||||
return view('administrator/class_assignment', $data);
|
||||
}
|
||||
|
||||
private function applyPendingDistributionDraftsForEnrolledStudents(string $year): void
|
||||
{
|
||||
if ($year === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = Database::connect();
|
||||
if (! $db->tableExists('student_section_distribution_drafts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rows = $db->table('student_section_distribution_drafts d')
|
||||
->select('d.id, d.student_id, d.class_section_id')
|
||||
->join(
|
||||
'enrollments e',
|
||||
'e.student_id = d.student_id AND e.school_year = d.school_year',
|
||||
'inner'
|
||||
)
|
||||
->where('d.school_year', $year)
|
||||
->where('d.status', 'pending')
|
||||
->whereIn('e.enrollment_status', ['payment pending', 'enrolled'])
|
||||
->groupBy('d.id, d.student_id, d.class_section_id')
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
if (empty($rows)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = utc_now();
|
||||
$updatedBy = (int)(session()->get('user_id') ?? 0) ?: null;
|
||||
|
||||
$db->transStart();
|
||||
foreach ($rows as $row) {
|
||||
$studentId = (int)($row['student_id'] ?? 0);
|
||||
$sectionId = (int)($row['class_section_id'] ?? 0);
|
||||
if ($studentId <= 0 || $sectionId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$existing = $db->table('student_class')
|
||||
->select('id')
|
||||
->where('student_id', $studentId)
|
||||
->where('school_year', $year)
|
||||
->get()
|
||||
->getRowArray();
|
||||
|
||||
$payload = [
|
||||
'student_id' => $studentId,
|
||||
'class_section_id' => $sectionId,
|
||||
'school_year' => $year,
|
||||
'updated_by' => $updatedBy,
|
||||
'updated_at' => $now,
|
||||
];
|
||||
|
||||
if ($existing) {
|
||||
$db->table('student_class')
|
||||
->where('id', (int)$existing['id'])
|
||||
->update($payload);
|
||||
} else {
|
||||
$payload['created_at'] = $now;
|
||||
$db->table('student_class')->insert($payload);
|
||||
}
|
||||
|
||||
$db->table('enrollments')
|
||||
->where('student_id', $studentId)
|
||||
->where('school_year', $year)
|
||||
->whereIn('enrollment_status', ['payment pending', 'enrolled'])
|
||||
->update([
|
||||
'class_section_id' => $sectionId,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
$db->table('student_section_distribution_drafts')
|
||||
->where('id', (int)$row['id'])
|
||||
->update([
|
||||
'status' => 'applied',
|
||||
'applied_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
$db->transComplete();
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'applyPendingDistributionDraftsForEnrolledStudents failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function save()
|
||||
|
||||
Reference in New Issue
Block a user