78 lines
2.7 KiB
PHP
78 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Parents;
|
|
|
|
use App\Models\StudentModel;
|
|
use App\Models\UserModel;
|
|
use App\Services\EmailService;
|
|
use Throwable;
|
|
|
|
class ParentRegistrationNotificationService
|
|
{
|
|
public function __construct(
|
|
private readonly UserModel $userModel,
|
|
private readonly StudentModel $studentModel,
|
|
private readonly EmailService $emailService,
|
|
private readonly string $adminEmail = 'registration@alrahmaisgl.org',
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $studentIds
|
|
*/
|
|
public function sendAdminNewStudentEmails(array $studentIds, int $parentId): void
|
|
{
|
|
$parent = $this->userModel->find($parentId);
|
|
if (! is_array($parent)) {
|
|
log_message('warning', 'Unable to send admin student registration email: parent not found for ID ' . $parentId);
|
|
return;
|
|
}
|
|
|
|
foreach (array_values(array_unique(array_filter(array_map('intval', $studentIds)))) as $studentId) {
|
|
$student = $this->studentModel->find($studentId);
|
|
if (! is_array($student)) {
|
|
log_message('warning', 'Unable to send admin student registration email: student not found for ID ' . $studentId);
|
|
continue;
|
|
}
|
|
|
|
$this->sendAdminNewStudentEmail($student, $parent, $parentId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $student
|
|
* @param array<string, mixed> $parent
|
|
*/
|
|
private function sendAdminNewStudentEmail(array $student, array $parent, int $parentId): void
|
|
{
|
|
$studentId = (int) ($student['id'] ?? 0);
|
|
$studentFullName = trim((string) ($student['firstname'] ?? '') . ' ' . (string) ($student['lastname'] ?? ''))
|
|
?: 'Student ID ' . $studentId;
|
|
|
|
$payload = $student;
|
|
$payload['parents'] = [
|
|
'user_id' => $parentId,
|
|
'firstname' => (string) ($parent['firstname'] ?? ''),
|
|
'lastname' => (string) ($parent['lastname'] ?? ''),
|
|
'email' => (string) ($parent['email'] ?? ''),
|
|
];
|
|
|
|
$adminMessage = view('emails/admin_student_registered', ['student' => $payload], ['saveData' => true]);
|
|
|
|
try {
|
|
$sent = $this->emailService->send(
|
|
$this->adminEmail,
|
|
'New Student Registered: ' . $studentFullName,
|
|
$adminMessage,
|
|
'notifications'
|
|
);
|
|
|
|
if (! $sent) {
|
|
log_message('error', 'Admin student registration email failed for student ID ' . $studentId);
|
|
}
|
|
} catch (Throwable $e) {
|
|
log_message('error', 'Admin student registration email failed for student ID ' . $studentId . ': ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|