Ejemplo n.º 1
0
function sendNotification($params)
{
    $db = JFactory::getDBO();
    $mailManager =& MailManager::getChildInstance();
    $app = JFactory::getApplication();
    $notification_event = NotificationsManager::getNotificationEventByActionName($params['action_name']);
    if (!$notification_event) {
        return '';
    }
    if (!isset($params['sender'])) {
        $params['sender'] = array($app->getCfg('mailfrom'), $app->getCfg('fromname'));
    }
    $sql = "SELECT learner_template, manager_template, selected_manager_roles, learner_template_disabled, manager_template_disabled,  disabled FROM #__lms_email_notifications WHERE id = " . $db->quote($notification_event->id);
    $db->setQuery($sql);
    $row = $db->loadObject();
    if ($row) {
        $notification_event->disabled = $row->disabled;
        $notification_event->learner_template = $row->learner_template;
        $notification_event->manager_template = $row->manager_template;
        $notification_event->learner_template_disabled = $row->learner_template_disabled;
        $notification_event->manager_template_disabled = $row->manager_template_disabled;
        $notification_event->selected_manager_roles = explode(',', $row->selected_manager_roles);
    }
    if ($notification_event->disabled || !$notification_event->learner_template && !$notification_event->manager_template) {
        return '';
    }
    $learner_template = '';
    $manager_template = '';
    if ($notification_event->learner_template) {
        $learner_template = NotificationsManager::getEmailTemplate($notification_event->learner_template);
    }
    if ($notification_event->manager_template) {
        $manager_template = NotificationsManager::getEmailTemplate($notification_event->manager_template);
    }
    if (!is_object($learner_template) && !is_object($manager_template)) {
        return '';
    }
    if (isset($params['markers']) && is_array($params['markers'])) {
        $params['markers'] = NotificationsManager::addDateTimeMarkers($params['markers']);
    } else {
        $params['markers'] = NotificationsManager::addDateTimeMarkers(array());
    }
    if (isset($params['markers']) && count($params['markers'])) {
        $markers_keys = array_keys($params['markers']);
        $markers_values = array_values($params['markers']);
        if (isset($params['markers_nohtml']) && is_array($params['markers_nohtml'])) {
            $markers_nohtml_keys = array_keys($params['markers_nohtml']);
            $markers_nohtml_values = array_values($params['markers_nohtml']);
        } else {
            $markers_nohtml_keys = array();
            $markers_nohtml_values = array();
        }
        if (is_object($learner_template) && $notification_event->learner_template && $notification_event->use_learner_template) {
            $learner_template->subject = str_replace($markers_keys, $markers_values, $learner_template->subject);
            $learner_template->template_html = str_replace($markers_keys, $markers_values, $learner_template->template_html);
            $learner_template->template_alt_text = str_replace($markers_nohtml_keys, $markers_nohtml_values, $learner_template->template_alt_text);
            $learner_template->template_alt_text = str_replace($markers_keys, $markers_values, $learner_template->template_alt_text);
        }
        if (is_object($manager_template) && $notification_event->manager_template && $notification_event->use_manager_template && !$notification_event->skip_managers) {
            $manager_template->subject = str_replace($markers_keys, $markers_values, $manager_template->subject);
            $manager_template->template_html = str_replace($markers_keys, $markers_values, $manager_template->template_html);
            $manager_template->template_alt_text = str_replace($markers_nohtml_keys, $markers_nohtml_values, $manager_template->template_alt_text);
            $manager_template->template_alt_text = str_replace($markers_keys, $markers_values, $manager_template->template_alt_text);
        }
    }
    if (isset($params['wrappers']) && count($params['wrappers'])) {
        if (is_object($learner_template) && $notification_event->learner_template && $notification_event->use_learner_template) {
            $learner_template->subject = NotificationsManager::replaceWrappers($params['wrappers'], $learner_template->subject);
            $learner_template->template_html = NotificationsManager::replaceWrappers($params['wrappers'], $learner_template->template_html);
            $learner_template->template_alt_text = NotificationsManager::replaceWrappers($params['wrappers'], $learner_template->template_alt_text);
        }
        if (is_object($manager_template) && $notification_event->manager_template && $notification_event->use_manager_template && !$notification_event->skip_managers) {
            $manager_template->subject = NotificationsManager::replaceWrappers($params['wrappers'], $manager_template->subject);
            $manager_template->template_html = NotificationsManager::replaceWrappers($params['wrappers'], $manager_template->template_html);
            $manager_template->template_alt_text = NotificationsManager::replaceWrappers($params['wrappers'], $manager_template->template_alt_text);
        }
    }
    $managers_array = array();
    $already_sent_users = array();
    if (is_object($learner_template) && $notification_event->use_learner_template && !$notification_event->learner_template_disabled) {
        if (isset($params['user_id']) && $params['user_id']) {
            $sql = "SELECT email, name FROM #__users WHERE id = " . $db->quote($params['user_id']);
            $db->setQuery($sql);
            $user = $db->loadObject();
            $params['recipient'] = $user->email;
            //array( $user->name, $user->email );
            $params['subject'] = $learner_template->subject;
            $params['body'] = $learner_template->template_html;
            $params['alttext'] = $learner_template->template_alt_text;
            if (isset($notification_event->notification_type) && $notification_event->notification_type == 2) {
                $params['attachment']->file_source = getCertificateFile($params);
                $params['attachment']->file_name = 'Certificate.png';
            }
            $mailManager->prepareEmail($params);
            $mailManager->sendEmail();
            $already_sent_users[] = intval($params['user_id']);
            unset($params['attachment']);
        }
    }
    if (is_object($manager_template) && $notification_event->use_manager_template && !$notification_event->manager_template_disabled && !$notification_event->skip_managers) {
        if (isset($params['course_id']) && $params['course_id'] && isset($notification_event->selected_manager_roles[0])) {
            $all_teachers_role_ids = JLMS_ACL_HELPER::getTeachersRoleIds();
            $teachers_role_ids = array_intersect($notification_event->selected_manager_roles, $all_teachers_role_ids);
            $teachers = JLMS_ACL_HELPER::getCourseTeachers($params['course_id'], $teachers_role_ids);
            $all_assistans_role_ids = JLMS_ACL_HELPER::getAssistansRolesIds();
            $assistans_role_ids = array_intersect($notification_event->selected_manager_roles, $all_assistans_role_ids);
            $teachers = JLMS_ACL_HELPER::getCourseTeachers($params['course_id'], $teachers_role_ids);
            $assistans = JLMS_ACL_HELPER::getCourseAssistans($params['course_id'], $assistans_role_ids);
            if (isset($teachers[0])) {
                foreach ($teachers as $teacher) {
                    if (!in_array($teacher->id, $already_sent_users)) {
                        $params['recipient'] = $teacher->email;
                        //array( $teacher->name, $teacher->email );
                        $params['subject'] = $manager_template->subject;
                        $params['body'] = $manager_template->template_html;
                        $params['alttext'] = $manager_template->template_alt_text;
                        $mailManager->prepareEmail($params);
                        $mailManager->sendEmail();
                        $already_sent_users[] = $teacher->id;
                    }
                }
            }
            if (isset($assistans[0])) {
                foreach ($assistans as $assistan) {
                    if (!in_array($assistan->id, $already_sent_users)) {
                        $params['recipient'] = $assistan->email;
                        //array( $assistan->name, $assistan->email );
                        $params['subject'] = $manager_template->subject;
                        $params['body'] = $manager_template->template_html;
                        $params['alttext'] = $manager_template->template_alt_text;
                        $mailManager->prepareEmail($params);
                        $mailManager->sendEmail();
                        $already_sent_users[] = $assistan->id;
                    }
                }
            }
        }
        if (isset($params['user_id']) && $params['user_id'] && isset($notification_event->selected_manager_roles[0])) {
            $all_CEO_role_ids = JLMS_ACL_HELPER::getCEORoleIds();
            $CEO_role_ids = array_intersect($notification_event->selected_manager_roles, $all_CEO_role_ids);
            $all_user_CEO = JLMS_ACL_HELPER::getUserCEO($params['user_id'], $CEO_role_ids);
            if (isset($all_user_CEO[0])) {
                foreach ($all_user_CEO as $CEO) {
                    if (!in_array($CEO->id, $already_sent_users)) {
                        $params['recipient'] = $CEO->email;
                        //array( $CEO->name, $CEO->email );
                        $params['subject'] = $manager_template->subject;
                        $params['body'] = $manager_template->template_html;
                        $params['alttext'] = $manager_template->template_alt_text;
                        $mailManager->prepareEmail($params);
                        $mailManager->sendEmail();
                        $already_sent_users[] = $CEO->id;
                    }
                }
            }
        }
        if (isset($notification_event->selected_manager_roles[0])) {
            $all_admin_role_ids = JLMS_ACL_HELPER::getAdminsRoleIds();
            $admin_role_ids = array_intersect($notification_event->selected_manager_roles, $all_admin_role_ids);
            $admins = JLMS_ACL_HELPER::getAdmins($admin_role_ids);
            if (isset($admins[0])) {
                foreach ($admins as $admin) {
                    if (!in_array($admin->id, $already_sent_users)) {
                        $params['recipient'] = $admin->email;
                        //array( $admin->name, $admin->email );
                        $params['subject'] = $manager_template->subject;
                        $params['body'] = $manager_template->template_html;
                        $params['alttext'] = $manager_template->template_alt_text;
                        $mailManager->prepareEmail($params);
                        $mailManager->sendEmail();
                        $already_sent_users[] = $admin->id;
                    }
                }
            }
        }
    }
}