82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php namespace App\Models;
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
class WhatsappGroupMembership extends BaseModel
|
|
{
|
|
protected $table = 'whatsapp_group_memberships';
|
|
protected $primaryKey = 'id';
|
|
protected $fillable = [
|
|
'class_section_id',
|
|
'school_year',
|
|
'semester',
|
|
'subject_type',
|
|
'subject_id',
|
|
'is_member',
|
|
'verified_by',
|
|
'verified_at',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
public $timestamps = true; // created_at, updated_at
|
|
|
|
/**
|
|
* Fetch all membership rows for the given sections and term.
|
|
* Returns an associative index ["<sid>:<type>:<id>" => row].
|
|
*/
|
|
public function getBySectionsAndTerm(array $sectionIds, string $year, string $sem): array
|
|
{
|
|
if (empty($sectionIds)) return [];
|
|
|
|
$rows = $this->asArray()
|
|
->whereIn('class_section_id', array_values(array_unique(array_map('intval', $sectionIds))))
|
|
->where('school_year', trim($year))
|
|
->where('semester', trim($sem))
|
|
->findAll();
|
|
|
|
$out = [];
|
|
foreach ($rows as $r) {
|
|
$k = sprintf('%d:%s:%d', (int)$r['class_section_id'], (string)$r['subject_type'], (int)$r['subject_id']);
|
|
$out[$k] = $r;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* Upsert membership row for a specific subject (parent) and class/term.
|
|
*/
|
|
public function upsertMembership(int $classSectionId, string $year, string $sem, string $type, int $subjectId, bool $isMember, ?int $verifiedBy = null): int
|
|
{
|
|
$type = $type === 'second' ? 'second' : 'primary';
|
|
$payload = [
|
|
'class_section_id' => $classSectionId,
|
|
'school_year' => trim($year),
|
|
'semester' => trim($sem),
|
|
'subject_type' => $type,
|
|
'subject_id' => $subjectId,
|
|
'is_member' => $isMember ? 1 : 0,
|
|
];
|
|
|
|
if ($verifiedBy) {
|
|
$payload['verified_by'] = $verifiedBy;
|
|
$payload['verified_at'] = utc_now();
|
|
}
|
|
|
|
$existing = $this->asArray()
|
|
->where('class_section_id', $payload['class_section_id'])
|
|
->where('school_year', $payload['school_year'])
|
|
->where('semester', $payload['semester'])
|
|
->where('subject_type', $payload['subject_type'])
|
|
->where('subject_id', $payload['subject_id'])
|
|
->first();
|
|
|
|
if ($existing) {
|
|
$this->update((int)$existing['id'], $payload);
|
|
return (int)$existing['id'];
|
|
}
|
|
|
|
return (int) $this->insert($payload, true);
|
|
}
|
|
}
|
|
|