This commit is contained in:
@@ -21,6 +21,7 @@ class ClassProgressReportModel extends Model
|
||||
protected $allowedFields = [
|
||||
'class_section_id',
|
||||
'teacher_id',
|
||||
'school_year',
|
||||
'week_start',
|
||||
'week_end',
|
||||
'subject',
|
||||
@@ -50,6 +51,7 @@ class ClassProgressReportModel extends Model
|
||||
protected $validationRules = [
|
||||
'class_section_id' => 'required|integer',
|
||||
'teacher_id' => 'required|integer',
|
||||
'school_year' => 'permit_empty|string|max_length[9]',
|
||||
'week_start' => 'required|valid_date[Y-m-d]',
|
||||
'week_end' => 'required|valid_date[Y-m-d]',
|
||||
'subject' => 'required|string|max_length[160]',
|
||||
|
||||
@@ -15,6 +15,7 @@ class PrintRequestModel extends Model
|
||||
'teacher_id',
|
||||
'admin_id',
|
||||
'class_id',
|
||||
'school_year',
|
||||
'file_path',
|
||||
'page_selection',
|
||||
'num_copies',
|
||||
|
||||
@@ -24,11 +24,87 @@ class SubjectCurriculumModel extends Model
|
||||
|
||||
public function getOptionsForClass(int $classId, string $subject): array
|
||||
{
|
||||
return $this->where('class_id', $classId)
|
||||
->where('subject', $subject)
|
||||
$rows = $this->orderedOptionsBuilder()
|
||||
->where('subject_curriculum_items.class_id', $classId)
|
||||
->where('subject_curriculum_items.subject', $subject)
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
if (! empty($rows)) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$fallbackClassIds = $this->classIdsWithSameName($classId);
|
||||
if (empty($fallbackClassIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->orderedOptionsBuilder()
|
||||
->whereIn('subject_curriculum_items.class_id', $fallbackClassIds)
|
||||
->where('subject_curriculum_items.subject', $subject)
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
return $this->uniqueCurriculumRows($rows);
|
||||
}
|
||||
|
||||
private function orderedOptionsBuilder()
|
||||
{
|
||||
return $this->db->table($this->table)
|
||||
->select('subject_curriculum_items.*')
|
||||
->orderBy('unit_number', 'ASC')
|
||||
->orderBy("CAST(SUBSTRING_INDEX(chapter_name, '.', 1) AS UNSIGNED)", 'ASC', false)
|
||||
->orderBy('chapter_name', 'ASC')
|
||||
->findAll();
|
||||
->orderBy('id', 'ASC');
|
||||
}
|
||||
|
||||
private function classIdsWithSameName(int $classId): array
|
||||
{
|
||||
$class = $this->db->table('classes')
|
||||
->select('class_name')
|
||||
->where('id', $classId)
|
||||
->get()
|
||||
->getRowArray();
|
||||
|
||||
$className = trim((string) ($class['class_name'] ?? ''));
|
||||
if ($className === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->db->table('classes')
|
||||
->select('id')
|
||||
->where('class_name', $className)
|
||||
->where('id !=', $classId)
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
return array_values(array_filter(array_map(
|
||||
static fn (array $row): int => (int) ($row['id'] ?? 0),
|
||||
$rows
|
||||
)));
|
||||
}
|
||||
|
||||
private function uniqueCurriculumRows(array $rows): array
|
||||
{
|
||||
$seen = [];
|
||||
$unique = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$key = implode('|', [
|
||||
(string) ($row['subject'] ?? ''),
|
||||
(string) ($row['unit_number'] ?? ''),
|
||||
(string) ($row['unit_title'] ?? ''),
|
||||
(string) ($row['chapter_name'] ?? ''),
|
||||
]);
|
||||
|
||||
if (isset($seen[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen[$key] = true;
|
||||
$unique[] = $row;
|
||||
}
|
||||
|
||||
return $unique;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user