Files
alrahma_sunday_school/app/Models/ClassSectionModel.php
T
2026-02-10 22:11:06 -05:00

113 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class ClassSectionModel extends Model
{
protected $table = 'classSection'; // Correct table name
protected $primaryKey = 'id'; // Specify the primary key field
protected $allowedFields = [
'class_id',
'class_section_id',
'class_section_name',
'created_at',
'updated_at',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
// Set default return type
protected $returnType = 'array';
// Method to get class sections with class and section names
public function getClassSections(): array
{
return $this->select('classSection.class_section_id, classSection.class_section_name, classes.class_name')
->join('classes', 'classSection.class_id = classes.id', 'left')
->orderBy('classSection.class_section_name', 'ASC')
->findAll();
}
/**
* Get the parent class_id for a given class_section_id.
*
* @param int|string $classSectionId
* @return int|null class_id if found, otherwise null
*/
public function getClassId($classSectionId): ?int
{
if ($classSectionId === null || $classSectionId === '') {
return null;
}
$row = $this->select('class_id')
->where('class_section_id', $classSectionId)
->orderBy($this->primaryKey, 'DESC') // in case of duplicates, take latest
->first();
return $row ? (int) $row['class_id'] : null;
}
/**
* Get the class_section_name for a given section_id.
*
* @param int|string $sectionId
* @return string|null
*/
public function getClassSectionNameBySectionId($sectionId)
{
$result = $this->where('class_section_id', $sectionId)->first();
return $result['class_section_name'] ?? null;
}
public function getClassSectionNameByClassId($classId)
{
$result = $this->where('class_id', $classId)->first();
return $result['class_section_name'] ?? null;
}
/**
* Get section ID from class section name.
*
* @param string $classSectionName The name of the class section.
* @return mixed The section ID or null if no match is found.
*/
public function getSectionIDFromClassSectionName($classSectionName)
{
// Query the table to find the class section by its name
$result = $this->where('class_section_name', $classSectionName)
->first(); // Get the first result (assuming class section name is unique)
// Return the class section_id if the result is found, otherwise return null
return $result ? $result['class_section_id'] : null;
}
/**
* Return the base (non-letter) section row for a given class_id. E.g., '3' vs '3-A'.
*/
public function getBaseSectionByClassId(int $classId): ?array
{
// Heuristic: names without a dash are base sections (e.g., '3', 'KG', 'youth')
return $this->where('class_id', $classId)
->where("class_section_name NOT LIKE '%-%'", null, false)
->orderBy('id', 'ASC')
->first();
}
/**
* Return lettered sections for a class (e.g., '3-A','3-B',...). Ordered by name asc.
*/
public function getLetterSectionsByClassId(int $classId): array
{
return $this->where('class_id', $classId)
->like('class_section_name', '-', 'both')
->orderBy('class_section_name', 'ASC')
->findAll();
}
}