This commit is contained in:
@@ -27,6 +27,8 @@ class TeacherClassModel extends Model
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $skipValidation = false;
|
||||
protected $afterInsert = ['syncStaffDirectoryAfterWrite'];
|
||||
protected $afterUpdate = ['syncStaffDirectoryAfterWrite'];
|
||||
/** Request-lifetime memo cache */
|
||||
protected array $assignedCache = [];
|
||||
protected array $assignedBySectionCache = [];
|
||||
@@ -57,6 +59,43 @@ class TeacherClassModel extends Model
|
||||
],
|
||||
];
|
||||
|
||||
protected function syncStaffDirectoryAfterWrite(array $data): array
|
||||
{
|
||||
try {
|
||||
$userIds = [];
|
||||
$teacherId = (int) ($data['data']['teacher_id'] ?? 0);
|
||||
if ($teacherId > 0) {
|
||||
$userIds[] = $teacherId;
|
||||
}
|
||||
|
||||
if ($userIds === [] && ! empty($data['id'])) {
|
||||
$ids = is_array($data['id']) ? $data['id'] : [$data['id']];
|
||||
$ids = array_values(array_filter(array_map('intval', $ids), static fn (int $id): bool => $id > 0));
|
||||
if ($ids !== []) {
|
||||
$rows = $this->db->table($this->table)
|
||||
->select('teacher_id')
|
||||
->whereIn($this->primaryKey, $ids)
|
||||
->get()
|
||||
->getResultArray();
|
||||
foreach ($rows as $row) {
|
||||
$rowTeacherId = (int) ($row['teacher_id'] ?? 0);
|
||||
if ($rowTeacherId > 0) {
|
||||
$userIds[] = $rowTeacherId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_unique($userIds) as $userId) {
|
||||
service('staffDirectorySync')->syncUser((int) $userId);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'TeacherClassModel staff directory sync failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getClassSectionIdByUserId($user_id)
|
||||
{
|
||||
$result = $this->where('teacher_id', $user_id)->first();
|
||||
|
||||
Reference in New Issue
Block a user