reconstruction of the project
This commit is contained in:
+82
-73
@@ -1,12 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClassSection extends BaseModel
|
||||
{
|
||||
protected $table = 'classSection'; // Correct table name
|
||||
protected $primaryKey = 'id'; // Specify the primary key field
|
||||
// CI table name is "classSection"
|
||||
protected $table = 'classSection';
|
||||
|
||||
// ✅ CI: useTimestamps = true
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'class_id',
|
||||
'class_section_id',
|
||||
@@ -16,115 +22,118 @@ class ClassSection extends BaseModel
|
||||
'semester',
|
||||
'school_year',
|
||||
];
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
// Set default return type
|
||||
protected $casts = [
|
||||
'class_id' => 'integer',
|
||||
'class_section_id' => 'integer',
|
||||
];
|
||||
|
||||
// Method to get class sections with class and section names
|
||||
public function getClassSections(?string $schoolYear = null, ?string $semester = null)
|
||||
/* =========================
|
||||
* Relationships (optional)
|
||||
* ========================= */
|
||||
public function schoolClass()
|
||||
{
|
||||
$builder = $this->select('ClassSection.*', 'classes.class_name', 'sections.section_name')
|
||||
->join('classes', 'ClassSection.class_id', '=', 'classes.id')
|
||||
->leftJoin('sections', 'ClassSection.class_section_id', '=', 'sections.id');
|
||||
|
||||
if ($schoolYear !== null) {
|
||||
$builder->where('ClassSection.school_year', $schoolYear);
|
||||
}
|
||||
|
||||
if ($semester !== null) {
|
||||
$builder->where('ClassSection.semester', $semester);
|
||||
}
|
||||
|
||||
return $builder->findAll();
|
||||
// If you used SchoolClass for classes model name
|
||||
return $this->belongsTo(SchoolClass::class, 'class_id');
|
||||
}
|
||||
|
||||
public function getCurrentAcademicSection(int $classId, string $schoolYear, string $semester): ?array
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Get class sections with class name (like CI join result).
|
||||
* Returns array of rows: class_section_id, class_section_name, class_name
|
||||
*/
|
||||
public static function getClassSections(): array
|
||||
{
|
||||
return $this->newQuery()
|
||||
->where('class_id', $classId)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('semester', $semester)
|
||||
->orderBy('class_section_name', 'ASC')
|
||||
->first();
|
||||
return DB::table('classSection')
|
||||
->leftJoin('classes', 'classSection.class_id', '=', 'classes.id')
|
||||
->select('classSection.class_section_id', 'classSection.class_section_name', 'classes.class_name')
|
||||
->orderBy('classSection.class_section_name', 'ASC')
|
||||
->get()
|
||||
->map(fn ($r) => (array) $r)
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
* Get the parent class_id for a given class_section_id.
|
||||
*/
|
||||
public static function getClassId($classSectionId): ?int
|
||||
{
|
||||
if ($classSectionId === null || $classSectionId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = static::query()
|
||||
->select('class_id')
|
||||
->where('class_section_id', $classSectionId)
|
||||
->orderByDesc('id') // in case of duplicates, take latest
|
||||
->first();
|
||||
|
||||
return $row ? (int) $row->class_id : 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)
|
||||
public static function getClassSectionNameBySectionId($sectionId): ?string
|
||||
{
|
||||
$result = $this->where('class_section_id', $sectionId)->first();
|
||||
return $result['class_section_name'] ?? null;
|
||||
$row = static::query()
|
||||
->select('class_section_name')
|
||||
->where('class_section_id', $sectionId)
|
||||
->first();
|
||||
|
||||
return $row?->class_section_name;
|
||||
}
|
||||
|
||||
public function getClassSectionNameByClassId($classId)
|
||||
public static function getClassSectionNameByClassId($classId): ?string
|
||||
{
|
||||
$result = $this->where('class_id', $classId)->first();
|
||||
return $result['class_section_name'] ?? null;
|
||||
$row = static::query()
|
||||
->select('class_section_name')
|
||||
->where('class_id', $classId)
|
||||
->first();
|
||||
|
||||
return $row?->class_section_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
public static function getSectionIDFromClassSectionName(string $classSectionName): ?int
|
||||
{
|
||||
// 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)
|
||||
$row = static::query()
|
||||
->select('class_section_id')
|
||||
->where('class_section_name', $classSectionName)
|
||||
->first();
|
||||
|
||||
// Return the class section_id if the result is found, otherwise return null
|
||||
return $result ? $result['class_section_id'] : null;
|
||||
return $row ? (int) $row->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
|
||||
public static 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)
|
||||
$row = static::query()
|
||||
->where('class_id', $classId)
|
||||
->whereRaw("class_section_name NOT LIKE '%-%'")
|
||||
->orderBy('id', 'ASC')
|
||||
->first();
|
||||
|
||||
return $row ? $row->toArray() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return lettered sections for a class (e.g., '3-A','3-B',...). Ordered by name asc.
|
||||
*/
|
||||
public function getLetterSectionsByClassId(int $classId): array
|
||||
public static function getLetterSectionsByClassId(int $classId): array
|
||||
{
|
||||
return $this->where('class_id', $classId)
|
||||
->like('class_section_name', '-', 'both')
|
||||
return static::query()
|
||||
->where('class_id', $classId)
|
||||
->where('class_section_name', 'like', '%-%')
|
||||
->orderBy('class_section_name', 'ASC')
|
||||
->findAll();
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user