<?php


function sendAnnouncementNotification($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $message, $workspace_id, $web_app_title, $attachment = null)
{
    // Check if there is an attachment
    $attachment_message = ($attachment === 1)
        ? "<p><strong>Attachment:</strong> There is an attachment related to this announcement. Please log in to the website to download it.</p>"
        : "";

    // Additional data fields for students
    $additionaldata = ['id', 'institution_id', 'branch_id', 'admission_number', 'name', 'mobile', 'email', 'fathername', 'user_id'];

    // Step 1: Get all students in the specified class and section
    $students = getStudentsForNotification($conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $student_id = null, $additionaldata);

    // Step 2: Loop through each student and send the announcement notification
    foreach ($students as $student) {
        // Get the student's name, email, and user ID
        $student_name = $student['name'];
        $student_email = $student['email'];  // Assuming the student array has an 'email' field
        $student_userid = $student['user_id'];

        // Prepare the email subject and content
        $subject = 'Announcement Notification - ' . $web_app_title;

        $format = "
        <h1>Announcement - <span class='web_app_title'>$web_app_title</span></h1>
        <p>Dear $student_name,</p>
        <p>We have an important announcement for you:</p>
        <p><strong>Details of the Announcement:</strong></p>
        <ul>
            <li><strong>Message:</strong> $message</li>
        </ul>
        $attachment_message
        <p>Please ensure that you take note of this announcement and act accordingly.</p>
        <p>If you have any questions or concerns, feel free to contact the school administration.</p>";

        // Step 3: Send the email using the existing function
        require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/mail_system/mail_Template1.php';
        sendMailTemplate1($conn_rjllearning, 'no-reply@rejoicelearning.in', $student_email, $subject, $format, $workspace_id, $institution_id, $branch_id, $student_userid);
    }
}
