This commit is contained in:
@@ -16,6 +16,17 @@ class WhatsappGroupLinkModel extends Model
|
||||
];
|
||||
protected $useTimestamps = true; // requires created_at / updated_at columns
|
||||
|
||||
private ?bool $hasSchoolYearColumn = null;
|
||||
|
||||
private function hasSchoolYearColumn(): bool
|
||||
{
|
||||
if ($this->hasSchoolYearColumn === null) {
|
||||
$this->hasSchoolYearColumn = $this->db->fieldExists('school_year', $this->table);
|
||||
}
|
||||
|
||||
return $this->hasSchoolYearColumn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link for a specific section in a specific term.
|
||||
*
|
||||
@@ -36,8 +47,11 @@ class WhatsappGroupLinkModel extends Model
|
||||
$sem = trim($sem);
|
||||
|
||||
$b = $this->asArray()
|
||||
->where('class_section_id', $sectionId)
|
||||
->where('school_year', $year);
|
||||
->where('class_section_id', $sectionId);
|
||||
|
||||
if ($this->hasSchoolYearColumn()) {
|
||||
$b = $b->where('school_year', $year);
|
||||
}
|
||||
|
||||
if ($onlyActive) {
|
||||
$b = $b->where('active', 1);
|
||||
@@ -69,8 +83,11 @@ class WhatsappGroupLinkModel extends Model
|
||||
$year = trim($year);
|
||||
$sem = trim($sem);
|
||||
|
||||
$b = $this->asArray()
|
||||
->where('school_year', $year);
|
||||
$b = $this->asArray();
|
||||
|
||||
if ($this->hasSchoolYearColumn()) {
|
||||
$b = $b->where('school_year', $year);
|
||||
}
|
||||
|
||||
if ($onlyActive === true) {
|
||||
$b = $b->where('active', 1);
|
||||
@@ -98,16 +115,23 @@ class WhatsappGroupLinkModel extends Model
|
||||
$payload = [
|
||||
'class_section_id' => $sectionId,
|
||||
'class_section_name' => $sectionName,
|
||||
'school_year' => trim($year),
|
||||
'invite_link' => trim($inviteLink),
|
||||
'active' => $active ? 1 : 0,
|
||||
];
|
||||
|
||||
if ($this->hasSchoolYearColumn()) {
|
||||
$payload['school_year'] = trim($year);
|
||||
}
|
||||
|
||||
// Try to find existing row (exact term)
|
||||
$existing = $this->asArray()
|
||||
->where('class_section_id', $sectionId)
|
||||
->where('school_year', $payload['school_year'])
|
||||
->first();
|
||||
->where('class_section_id', $sectionId);
|
||||
|
||||
if ($this->hasSchoolYearColumn()) {
|
||||
$existing = $existing->where('school_year', $payload['school_year']);
|
||||
}
|
||||
|
||||
$existing = $existing->first();
|
||||
|
||||
if ($existing) {
|
||||
$this->update((int)$existing['id'], $payload);
|
||||
|
||||
Reference in New Issue
Block a user