/**
  * Get user certificates
  *
  * This function gets all certificates that have been issued for the user
  * <br/>Example:
  * <code>
  * $user -> getIssuedCertificates();	   //Get an array with the information on the certificates
  * </code>
  *
  * @return an array of the format [] => [course name, certificate key, date issued, date expire, issuing authority]
  * @since 3.6.1
  * @access public
  */
 public function getIssuedCertificates()
 {
     $constraints = array('archive' => false, 'active' => true, 'condition' => 'issued_certificate != 0 or issued_certificate is not null');
     $constraints['return_objects'] = false;
     $courses = $this->getUserCourses($constraints);
     $certificates = array();
     foreach ($courses as $course) {
         if ($certificateInfo = unserialize($course['issued_certificate'])) {
             $certificateInfo = unserialize($course['issued_certificate']);
             $courseOptions = unserialize($course['options']);
             if ($course['certificate_expiration']) {
                 $expirationArray = convertTimeToDays($course['certificate_expiration']);
                 $expire_certificateTimestamp = getCertificateExpirationTimestamp($certificateInfo['date'], $expirationArray);
             }
             $certificates[] = array("courses_ID" => $course['id'], "course_name" => $course['name'], "serial_number" => $certificateInfo['serial_number'], "grade" => $certificateInfo['grade'], "issue_date" => $certificateInfo['date'], "active" => $course['active'], "export_method" => $courseOptions['certificate_export_method'], "expiration_date" => $course['certificate_expiration'] ? $expire_certificateTimestamp : _NEVER);
         }
     }
     return $certificates;
 }
 $issued_data = unserialize($result[0]['issued_certificate']);
 $templateData = eF_getTableData("certificate_templates", "certificate_xml", "id=" . $certificate_tpl_id);
 foreach (eF_loadAllModules() as $module) {
     $module->onXMLExportCourseCertificate($issued_data, $templateData, $course, $_GET['user']);
 }
 $userName = $issued_data['user_name'];
 $userSurName = $issued_data['user_surname'];
 $courseName = $issued_data['course_name'];
 $courseGrade = $issued_data['grade'];
 $serialNumber = $issued_data['serial_number'];
 if (eF_checkParameter($issued_data['date'], 'timestamp')) {
     $certificateDate = formatTimestamp($issued_data['date']);
 }
 if ($course->course['certificate_expiration'] != 0) {
     $expirationArray = convertTimeToDays($course->course['certificate_expiration']);
     $expire_certificateTimestamp = getCertificateExpirationTimestamp($issued_data['date'], $expirationArray);
     $expireDate = formatTimestamp($expire_certificateTimestamp);
 }
 $xmlExport = new XMLExport($templateData[0]['certificate_xml']);
 $creator = $xmlExport->getCreator();
 $author = $xmlExport->getAuthor();
 $subjct = $xmlExport->getSubject($userName . ' ' . $userSurName);
 $keywrd = $xmlExport->getKeywords();
 $orientation = $xmlExport->getOrientation();
 $pdf = new TCPDF($orientation, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 $pdf->SetCreator($creator);
 $pdf->SetAuthor($author);
 $pdf->SetTitle($subjct);
 $pdf->SetSubject($subjct);
 $pdf->SetKeywords($keywrd);
 $xmlExport->setBackground($pdf);
 /**
  * Initialize the notifications for the ones sent prior or after some time to an event, i.e.
  * Find all users that are related to this notification, see when they should have triggered this
  * notification (when this notification was not declared) and see to it that they get their message
  * when they should
  *
  * We should create the users that should be sent the newly created/edited notification according to
  * - the current time (time())
  * - the "after time" of the notification (1 - 60 days)
  * - the conditions set for this event
  * Note that all $users_to_notify results should have a SPECIFIC FORM which is:
  * users_LOGIN, users_name, users_surname, timestamp, [lessons_ID, lessons_name, entity_ID, entity_name]
  * which together with the 'type' field of the event will be passed as arguments to the appendNewNotification
  * which works with these arguments
  *
  * <br/>Example:
  * <code>
  * EfrontNotification :: initializeEventNotification($fields);
  * </code>
  *
  * @param $fields: the descripting fields of the event notification
  * @since 3.6.0
  * @access public
  */
 public static function initializeEventNotification($event_notification)
 {
     $event_types = EfrontEvent::getEventTypes();
     // The same regardless whether $event_notification['after_time'] is positive (After Event)
     // or negative (Before Event): we will compare timestamps with past or future timestamps
     // respectively and send now only the notifications that make (have not expired)
     $timediff = time() - $event_notification['after_time'];
     if (EfrontEvent::SYSTEM_JOIN == $event_notification['event_type']) {
         $users_to_notify = eF_getTableData("users", "login as users_LOGIN, name as users_name, surname as users_surname, timestamp", "timestamp > " . $timediff);
     } else {
         if (EfrontEvent::SYSTEM_VISITED == abs($event_notification['event_type'])) {
             $users_result = eF_getTableData("logs JOIN users ON logs.users_LOGIN = users.login", "distinct users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, logs.timestamp", "action = 'login' AND users.active=1 AND logs.timestamp > " . $timediff, "users.login ASC, logs.timestamp DESC");
             // Removing duplicates to keep only last record of each user - since the list is sorted this will work
             $previous_user = "";
             $users_to_notify = array();
             $users_having_entered = array();
             foreach ($users_result as $key => $user) {
                 if ($user['users_LOGIN'] != $previous_user) {
                     $users_to_notify[] = $user;
                     $previous_user = $user['users_LOGIN'];
                     $users_having_entered[] = $user['users_LOGIN'];
                 }
             }
             $users_never_entered = eF_getTableData("users", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, users.timestamp", "active=1 AND login NOT IN ('" . implode("','", $users_having_entered) . "') AND timestamp > " . $timediff);
             foreach ($users_never_entered as $key => $user) {
                 $users_to_notify[] = $user;
             }
         } else {
             if (EfrontEvent::LESSON_ACQUISITION_AS_STUDENT == $event_notification['event_type'] || EfrontEvent::LESSON_ACQUISITION_AS_PROFESSOR == $event_notification['event_type'] || EfrontEvent::LESSON_COMPLETION == abs($event_notification['event_type']) || EfrontEvent::LESSON_PROGRESS_RESET == abs($event_notification['event_type']) || EfrontEvent::LESSON_PROGRAMMED_START == abs($event_notification['event_type']) || EfrontEvent::LESSON_PROGRAMMED_EXPIRY == abs($event_notification['event_type'])) {
                 // for the corresponding BEFORE event
                 $conditions = unserialize($event_notification['send_conditions']);
                 $extra_condition = "";
                 if (EfrontEvent::LESSON_ACQUISITION_AS_STUDENT == $event_notification['event_type']) {
                     $extra_condition = "users_to_lessons.user_type = 'student' AND ";
                     $timestamp_column = "users_to_lessons.from_timestamp";
                 } else {
                     if (EfrontEvent::LESSON_ACQUISITION_AS_PROFESSOR == $event_notification['event_type']) {
                         $extra_condition = "users_to_lessons.user_type = 'professor' AND ";
                         $timestamp_column = "users_to_lessons.from_timestamp";
                     } else {
                         if (EfrontEvent::LESSON_COMPLETION == $event_notification['event_type']) {
                             $extra_condition = "users_to_lessons.completed = '1' AND ";
                             $timestamp_column = "users_to_lessons.to_timestamp";
                         } else {
                             if (EfrontEvent::LESSON_COMPLETION == -1 * $event_notification['event_type']) {
                                 $extra_condition = "users_to_lessons.completed = '0' AND ";
                                 $timestamp_column = "users_to_lessons.to_timestamp";
                             } else {
                                 if (EfrontEvent::LESSON_PROGRAMMED_START == abs($event_notification['event_type'])) {
                                     $timestamp_column = "lessons.from_timestamp";
                                 } else {
                                     if (EfrontEvent::LESSON_PROGRAMMED_EXPIRY == abs($event_notification['event_type'])) {
                                         $timestamp_column = "lessons.to_timestamp";
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if ($conditions['lessons_ID'] != 0) {
                     $extra_condition .= " lessons.id = " . $conditions['lessons_ID'] . " AND ";
                 }
                 if (EfrontEvent::LESSON_PROGRAMMED_START != abs($event_notification['event_type']) && EfrontEvent::LESSON_PROGRAMMED_EXPIRY != abs($event_notification['event_type'])) {
                     $users_to_notify = eF_getTableData("users_to_lessons JOIN users ON users_to_lessons.users_LOGIN = users.login JOIN lessons ON users_to_lessons.lessons_ID = lessons.id", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, users_to_lessons.lessons_ID, lessons.name as lessons_name, " . $timestamp_column . " as timestamp", $extra_condition . $timestamp_column . "> " . $timediff . " and users.archive=0 and users_to_lessons.archive=0");
                 } else {
                     $users_to_notify = eF_getTableData("lessons", "lessons.id as lessons_ID, lessons.name as lessons_name, " . $timestamp_column . " as timestamp", $extra_condition . $timestamp_column . "> " . $timediff);
                 }
             } else {
                 if (EfrontEvent::LESSON_VISITED == abs($event_notification['event_type'])) {
                     $conditions = unserialize($event_notification['send_conditions']);
                     if ($conditions['lessons_ID'] != 0) {
                         $extra_condition .= " logs.lessons_ID = " . $conditions['lessons_ID'] . " AND ";
                     }
                     $users_result = eF_getTableData("logs JOIN users ON logs.users_LOGIN = users.login JOIN lessons ON lessons.id = logs.lessons_ID", "distinct users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, logs.timestamp, lessons.id as lessons_ID, lessons.name as lessons_name", $extra_condition . " action = 'lesson' AND logs.timestamp > " . $timediff, "users.login ASC, logs.timestamp DESC");
                     // Removing duplicates to keep only last record of each user - since the list is sorted this will work
                     $previous_user = "";
                     $users_to_notify = array();
                     foreach ($users_result as $key => $user) {
                         if ($user['users_LOGIN'] != $previous_user) {
                             $users_to_notify[] = $user;
                             $previous_user = $user['users_LOGIN'];
                         }
                     }
                 } else {
                     if (EfrontEvent::PROJECT_SUBMISSION == $event_notification['event_type']) {
                         $conditions = unserialize($event_notification['send_conditions']);
                         if ($conditions['lessons_ID'] != 0) {
                             $extra_condition .= " projects.lessons_ID = " . $conditions['lessons_ID'] . " AND ";
                         }
                         $timestamp_column = "users_to_projects.upload_timestamp";
                         $users_to_notify = eF_getTableData("users_to_projects JOIN users ON users_to_projects.users_LOGIN = users.login JOIN projects ON users_to_projects.projects_ID = projects.id JOIN lessons ON lessons.id = projects.lessons_ID", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, projects.lessons_ID, lessons.name as lessons_name, " . $timestamp_column . " as timestamp, projects.id as entity_ID, projects.title as entity_name", $extra_condition . $timestamp_column . "> " . $timediff);
                     } else {
                         if (EfrontEvent::PROJECT_EXPIRY == abs($event_notification['event_type'])) {
                             $timestamp_column = "projects.deadline";
                             if ($conditions['lessons_ID'] != 0) {
                                 $extra_condition .= " projects.lessons_ID = " . $conditions['lessons_ID'] . " AND ";
                             }
                             $users_to_notify = eF_getTableData("projects JOIN lessons ON lessons.id = projects.lessons_ID", "projects.lessons_ID, lessons.name as lessons_name, " . $timestamp_column . " as timestamp, projects.id as entity_ID, projects.title as entity_name", $extra_condition . $timestamp_column . "> " . $timediff);
                         } else {
                             if (EfrontEvent::COURSE_ACQUISITION_AS_STUDENT == $event_notification['event_type'] || EfrontEvent::COURSE_ACQUISITION_AS_PROFESSOR == $event_notification['event_type'] || EfrontEvent::COURSE_COMPLETION == abs($event_notification['event_type']) || EfrontEvent::COURSE_PROGRAMMED_START == abs($event_notification['event_type']) || EfrontEvent::COURSE_PROGRAMMED_EXPIRY == abs($event_notification['event_type'])) {
                                 // for the corresponding BEFORE event
                                 $conditions = unserialize($event_notification['send_conditions']);
                                 $extra_condition = "";
                                 if (EfrontEvent::COURSE_ACQUISITION_AS_STUDENT == $event_notification['event_type']) {
                                     $extra_condition = "users_to_courses.user_type = 'student' AND ";
                                     $timestamp_column = "users_to_courses.from_timestamp";
                                 } else {
                                     if (EfrontEvent::COURSE_ACQUISITION_AS_PROFESSOR == $event_notification['event_type']) {
                                         $extra_condition = "users_to_courses.user_type = 'professor' AND ";
                                         $timestamp_column = "users_to_courses.from_timestamp";
                                     } else {
                                         if (EfrontEvent::COURSE_COMPLETION == $event_notification['event_type'] || EfrontEvent::COURSE_CERTIFICATE_ISSUE == $event_notification['event_type']) {
                                             $extra_condition = "users_to_courses.completed = '1' AND ";
                                             $timestamp_column = "users_to_courses.to_timestamp";
                                         } else {
                                             if (EfrontEvent::COURSE_COMPLETION == -1 * $event_notification['event_type']) {
                                                 $extra_condition = "users_to_courses.completed = '0' AND ";
                                                 $timestamp_column = "users_to_courses.from_timestamp";
                                             } else {
                                                 if (EfrontEvent::COURSE_PROGRAMMED_START == abs($event_notification['event_type'])) {
                                                     $timestamp_column = "courses.start_date";
                                                 } else {
                                                     if (EfrontEvent::COURSE_PROGRAMMED_EXPIRY == abs($event_notification['event_type'])) {
                                                         $timestamp_column = "courses.end_date";
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 if ($conditions['courses_ID'] != 0) {
                                     $extra_condition .= " courses.id = " . $conditions['courses_ID'] . " AND ";
                                 }
                                 if (EfrontEvent::COURSE_PROGRAMMED_START == abs($event_notification['event_type']) || EfrontEvent::COURSE_PROGRAMMED_EXPIRY == abs($event_notification['event_type'])) {
                                     $users_to_notify = eF_getTableData("courses", "courses.id as lessons_ID, courses.name as lessons_name, " . $timestamp_column . " as timestamp", $extra_condition . $timestamp_column . "> " . $timediff);
                                 } else {
                                     $users_to_notify = eF_getTableData("users_to_courses JOIN users ON users_to_courses.users_LOGIN = users.login JOIN courses ON users_to_courses.courses_ID = courses.id", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, users_to_courses.courses_ID, courses.name as courses_name, " . $timestamp_column . " as timestamp", $extra_condition . $timestamp_column . "> " . $timediff . " and users.archive=0 and users_to_courses.archive=0");
                                 }
                             } else {
                                 if (EfrontEvent::COURSE_CERTIFICATE_ISSUE == $event_notification['event_type']) {
                                     $users_result = eF_getTableData("users_to_courses JOIN users ON users_to_courses.users_LOGIN = users.login JOIN courses ON users_to_courses.courses_ID = courses.id", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, users_to_courses.courses_ID, courses.name as courses_name, users_to_courses.issued_certificate", "users_to_courses.completed = '1' AND users_to_courses.issued_certificate <> '' and users.archive=0 and users_to_courses.archive=0");
                                     $users_to_notify = array();
                                     foreach ($users_result as $key => $user) {
                                         $certificate = unserialize($user['issued_certificate']);
                                         if ($certificate['date'] > $timediff) {
                                             $user['timestamp'] = $certificate['date'];
                                             $users_to_notify[] = $user;
                                         }
                                     }
                                 } else {
                                     if (EfrontEvent::COURSE_CERTIFICATE_EXPIRY == abs($event_notification['event_type'])) {
                                         $users_result = eF_getTableData("users_to_courses JOIN users ON users_to_courses.users_LOGIN = users.login JOIN courses ON users_to_courses.courses_ID = courses.id", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, users_to_courses.courses_ID, courses.name as courses_name, courses.certificate_expiration, users_to_courses.issued_certificate", "users_to_courses.completed = '1' AND users_to_courses.issued_certificate <> '' and courses.certificate_expiration !=0 and users.archive=0 and users_to_courses.archive=0");
                                         $users_to_notify = array();
                                         foreach ($users_result as $key => $user) {
                                             $dateTable = unserialize($user['issued_certificate']);
                                             $expirationArray = convertTimeToDays($user['certificate_expiration']);
                                             $timeExpire = getCertificateExpirationTimestamp($dateTable['date'], $expirationArray);
                                             if ($event_notification['after_time'] < 0) {
                                                 $resetArray = convertTimeToDays(abs($event_notification['after_time']));
                                                 $resetTimestamp = getCertificateResetTimestamp($expirationTimestamp, $resetArray);
                                                 if ($resetTimestamp < time() && $timeExpire > time()) {
                                                     $users_to_notify[] = $user;
                                                 }
                                             } elseif ($timeExpire && $timeExpire < time()) {
                                                 $users_to_notify[] = $user;
                                             }
                                         }
                                     } else {
                                         if (EfrontEvent::COURSE_VISITED == abs($event_notification['event_type'])) {
                                             $conditions = unserialize($event_notification['send_conditions']);
                                             if ($conditions['courses_ID'] != 0) {
                                                 $extra_condition .= " logs.courses_ID = " . $conditions['courses_ID'] . " AND ";
                                             }
                                             $users_result = eF_getTableData("logs JOIN users ON logs.users_LOGIN = users.login JOIN courses ON courses.id = logs.courses_ID", "distinct users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, logs.timestamp, courses.id as courses_ID, courses.name as courses_name", $extra_condition . " action = 'course' AND logs.timestamp > " . $timediff, "users.login ASC, logs.timestamp DESC");
                                             // Removing duplicates to keep only last record of each user - since the list is sorted this will work
                                             $previous_user = "";
                                             $users_to_notify = array();
                                             foreach ($users_result as $key => $user) {
                                                 if ($user['users_LOGIN'] != $previous_user) {
                                                     $users_to_notify[] = $user;
                                                     $previous_user = $user['users_LOGIN'];
                                                 }
                                             }
                                         } else {
                                             if (EfrontEvent::NEW_SURVEY == $event_notification['event_type']) {
                                                 $conditions = unserialize($event_notification['send_conditions']);
                                                 // if ($conditions['lessons_ID'] != 0) {
                                                 //     $extra_condition .= " projects.lessons_ID = " . $conditions['lessons_ID'] . " AND ";
                                                 // }
                                                 $timestamp_column = "surveys.start_date";
                                                 $users_to_notify = eF_getTableData("surveys JOIN users ON surveys.author = users.login JOIN lessons ON lessons.id = surveys.lessons_ID JOIN users_to_surveys ON users_to_surveys.surveys_ID=surveys.id", "users.login as users_LOGIN, users.name as users_name, users.surname as users_surname, surveys.lessons_ID, lessons.name as lessons_name, surveys.id as entity_ID, surveys.name as entity_name, " . $timestamp_column . " as timestamp", " users_to_surveys.users_LOGIN=users.login and users_to_surveys.last_post = '' and " . $extra_condition . $timestamp_column . "> " . $timediff);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     global $currentUser;
     if (sizeof($users_to_notify) > 0) {
         foreach ($users_to_notify as $user_event_fields) {
             if (!isset($user_event_fields['users_LOGIN'])) {
                 $user_event_fields['users_LOGIN'] = $currentUser->user['login'];
                 $user_event_fields['users_name'] = $currentUser->user['name'];
                 $user_event_fields['users_surname'] = $currentUser->user['surname'];
             }
             $user_event_fields['type'] = $event_notification['event_type'];
             $user_event_fields['send_interval'] = $event_notification['after_time'];
             $event = new EfrontEvent($user_event_fields);
             // this should create an event instance for our class
             $event->appendNewNotification($event_types, true, false);
             // append this notification to the email queue
         }
     } else {
         eF_deleteTableData("notifications", "id_type_entity LIKE '" . $event_notification['id'] . "_%'");
     }
 }
 /**
  * Check if a course must be reset because of certificate expiry or 'before expiry' reset
  *
  * @param mixed lesson A lesson id or an EfrontLesson object
  * @since 3.6.3
  * @access public
  */
 public static function checkCertificateExpire()
 {
     $courses = eF_getTableData("courses", "id,reset_interval,reset", "certificate_expiration !=0");
     $notifications = eF_getTableData("event_notifications", "id,event_type,after_time,send_conditions", "event_type=-56 and active=1");
     $notifications_on_event = eF_getTableData("event_notifications", "id,event_type,after_time,send_conditions", "event_type=56 and active=1");
     foreach ($courses as $value) {
         $course = new EfrontCourse($value['id']);
         $constraints = array('archive' => false, 'active' => true, 'condition' => 'issued_certificate != ""');
         $users = $course->getStudentUsers(false, $constraints);
         foreach ($users as $user) {
             $login = $user['login'];
             $dateTable = unserialize($user['issued_certificate']);
             if (eF_checkParameter($dateTable['date'], 'timestamp')) {
                 //new way that issued date saves
                 $expirationArray = convertTimeToDays($course->course['certificate_expiration']);
                 $expirationTimestamp = getCertificateExpirationTimestamp($dateTable['date'], $expirationArray);
                 if ($course->course['reset_interval'] != 0) {
                     $resetArray = convertTimeToDays($value['reset_interval']);
                     $resetTimestamp = getCertificateResetTimestamp($expirationTimestamp, $resetArray);
                     if ($resetTimestamp < time()) {
                         $user = EfrontUserFactory::factory($user);
                         $user->resetProgressInCourse($course, true, true);
                     }
                 }
                 if ($course->course['reset']) {
                     //If student completed again the course with reset_interval, he has a new expire date so he will not be reset,(so it is not elseif)
                     if ($expirationTimestamp < time()) {
                         if (!$user instanceof EfrontUser) {
                             $user = EfrontUserFactory::factory($user);
                         }
                         $user->resetProgressInCourse($course, true);
                         foreach ($notifications_on_event as $notification) {
                             $send_conditions = unserialize($notification['send_conditions']);
                             $courses_ID = $send_conditions['courses_ID'];
                             if ($courses_ID == $value['id'] || $courses_ID == 0) {
                                 if ($notification['after_time'] == 0) {
                                     EfrontEvent::triggerEvent(array("type" => EfrontEvent::COURSE_CERTIFICATE_EXPIRY, "users_LOGIN" => $user->user['login'], "lessons_ID" => $course->course['id'], "lessons_name" => $course->course['name'], 'create_negative' => false));
                                 }
                             }
                         }
                     }
                 }
                 if (!$course->course['reset'] && !$course->course['reset_interval']) {
                     if ($expirationTimestamp < time()) {
                         eF_updateTableData("users_to_courses", array("issued_certificate" => ""), "users_LOGIN='******' and courses_ID = " . $course->course['id']);
                         foreach ($notifications_on_event as $notification) {
                             $send_conditions = unserialize($notification['send_conditions']);
                             $courses_ID = $send_conditions['courses_ID'];
                             if ($courses_ID == $value['id'] || $courses_ID == 0) {
                                 if ($notification['after_time'] == 0) {
                                     EfrontEvent::triggerEvent(array("type" => EfrontEvent::COURSE_CERTIFICATE_REVOKE, "users_LOGIN" => $login, "lessons_ID" => $course->course['id'], "lessons_name" => $course->course['name'], "create_negative" => false));
                                 }
                             }
                         }
                     }
                 }
                 foreach ($notifications as $notification) {
                     $send_conditions = unserialize($notification['send_conditions']);
                     $courses_ID = $send_conditions['courses_ID'];
                     if ($courses_ID == $value['id'] || $courses_ID == 0) {
                         if ($notification['after_time'] < 0) {
                             $resetArray = convertTimeToDays(abs($notification['after_time']));
                             $resetTimestamp = getCertificateResetTimestamp($expirationTimestamp, $resetArray);
                             // in order notification to be sent one (not every day after $resetTimestamp)
                             if ($GLOBALS['configuration']['last_reset_certificate'] < $resetTimestamp && $resetTimestamp < time() && $expirationTimestamp > time()) {
                                 EfrontEvent::triggerEvent(array("type" => -1 * EfrontEvent::COURSE_CERTIFICATE_EXPIRY, "users_LOGIN" => $login, "lessons_ID" => $course->course['id'], "lessons_name" => $course->course['name'], "create_negative" => false));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
function replaceCustomFieldsCertificate($custom, $issuedTimestamp, $login = '', $ceu = '', $hours = '')
{
    if ($login != '') {
        $result = eF_getTableData("users", "*", "login='******'");
    }
    if (preg_match("/###([0-9]{1,100})([mdy])###/", $custom, $matches)) {
        switch ($matches[2]) {
            case 'd':
                $time = 86400 * $matches[1];
                break;
            case 'm':
                $time = 30 * 86400 * $matches[1];
                break;
            case 'y':
                $time = 12 * 30 * 86400 * $matches[1];
                break;
        }
        $timeArray = convertTimeToDays($time);
        $convertTimestamp = getCertificateExpirationTimestamp($issuedTimestamp, $timeArray);
        $convertDate = formatTimestamp($convertTimestamp);
        $custom = str_replace($matches[0], $convertDate, $custom);
    }
    if ($login != '') {
        $userProfile = eF_getTableDataFlat("user_profile", "*", "active=1 AND type <> 'branchinfo' AND type <> 'groupinfo'");
        foreach ($userProfile['name'] as $key => $value) {
            if (preg_match("/###" . $value . "###/", $custom, $matches)) {
                if ($userProfile['type'][$key] == 'date') {
                    $custom = str_replace($matches[0], formatTimestamp($result[0][$value]), $custom);
                } else {
                    if ($userProfile['type'][$key] == 'select') {
                        $userProfileValues = unserialize($userProfile['options'][$key]);
                        $custom = str_replace($matches[0], $userProfileValues[$result[0][$value]], $custom);
                    } else {
                        $custom = str_replace($matches[0], $result[0][$value], $custom);
                    }
                }
            }
        }
        foreach ($result[0] as $key => $value) {
            if (preg_match("/###" . $key . "###/", $custom, $matches)) {
                $custom = str_replace($matches[0], $value, $custom);
            }
        }
    }
    if ($ceu) {
        if (preg_match("/###ceu###/", $custom, $matches)) {
            $custom = str_replace($matches[0], $ceu, $custom);
        }
    }
    if ($hours) {
        if (preg_match("/###training_hours###/", $custom, $matches)) {
            $custom = str_replace($matches[0], $hours, $custom);
        }
    }
    return $custom;
}