/** * 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'] . "_%'"); } }
/** * Trigger an event * * Denotes the triggering of an event in the eFront system * All functionalities that should take place during an event triggering * like logging, notification sending etc, should be defined here * <br/>Example: * <code> * $fields = array('name' => 'new event', 'languages_NAME' => 'english'); * $event = EfrontEvent :: triggerEvent($fields); * </code> * * @param array $fields The new fields * @param boolean $send_notification Send notification or not * @return EfrontEvent the new event * @since 3.6.0 * @access public */ public static function triggerEvent($fields, $send_notification = true) { // Check and create all necessary fields if (!isset($fields['type'])) { throw new EfrontEventException(_NOEVENTCODEDEFINED, EfrontEventException::NOEVENTCODE_DEFINED); } //These are the mandatory fields. In case one of these is absent, fill it in with a default value // If no user is defined the currentuser will be used as user triggering the event if (!isset($fields['users_LOGIN'])) { if (isset($GLOBALS['currentUser'])) { $fields['users_LOGIN'] = $GLOBALS['currentUser']->user['login']; $fields['users_name'] = $GLOBALS['currentUser']->user['name']; $fields['users_surname'] = $GLOBALS['currentUser']->user['surname']; } else { $fields['users_LOGIN'] = $_SESSION['s_login']; } } // If a users login is defined, but without any name/surname fields, then get them from the DB if (!isset($fields['users_name']) || !isset($fields['users_surname'])) { $users_id = eF_getTableData("users", "name, surname", "login = '******'users_LOGIN'] . "'"); if ($users_id) { $fields['users_name'] = $users_id[0]['name']; $fields['users_surname'] = $users_id[0]['surname']; } else { $fields['users_name'] = ''; $fields['users_surname'] = ''; } } // Events that canBePreceded might be triggered from now for the future - these events have their timestamp field already set if (!isset($fields['timestamp'])) { $fields['timestamp'] = time(); } // Get the events array and get the information for this event type $event_types = EfrontEvent::getEventTypes(); $type = $event_types[abs($fields['type'])]; // the $fields['lessons_ID'] may refer to either courses or lessons according to the type of the event if ($type['category'] == "courses") { // Allow multiple course ids for each event if (is_array($fields['lessons_ID'])) { $event_courses = eF_getTableData("courses", "id, name", "id in (" . implode(",", $fields['lessons_ID']) . ")"); $result = true; //$fields['lessons'] = array(); $fields['lessons_name'] = ""; foreach ($event_courses as $course) { //$fields['lessons'][] = array("lessons_ID" => $course['id'], 'lessons_name' => $course['name']); if ($fields['lessons_name'] != "") { $fields['lessons_name'] .= ", "; } $fields['lessons_name'] .= $course['name']; } } else { // if not pre-defined if (!isset($fields['lessons_name']) || $fields['lessons_name'] == "") { $event_courses = eF_getTableData("courses", "id, name", "id = '" . $fields['lessons_ID'] . "'"); $fields['lessons_name'] = $event_courses[0]['name']; } } } else { // Allow multiple lesson ids for each event if (isset($fields['lessons_ID']) && is_array($fields['lessons_ID'])) { $event_lessons = eF_getTableData("lessons", "id, name", "id in (" . implode(",", $fields['lessons_ID']) . ")"); $result = true; //$fields['lessons'] = array(); $fields['lessons_name'] = ""; foreach ($event_lessons as $lesson) { //$fields['lessons'][] = array("lessons_ID" => $lesson['id'], 'lessons_name' => $lesson['name']); if ($fields['lessons_name'] != "") { $fields['lessons_name'] .= ", "; } $fields['lessons_name'] .= $lesson['name']; } } else { // if not pre-defined if (isset($fields['lessons_ID']) && (!isset($fields['lessons_name']) || $fields['lessons_name'] == "")) { $event_lessons = eF_getTableData("lessons", "id, name", "id = '" . $fields['lessons_ID'] . "'"); $fields['lessons_name'] = $event_lessons[0]['name']; } } } if ($fields['type'] == EfrontEvent::CONTENT_COMPLETION) { if (isset($fields['entity_ID']) && !isset($fields['entity_name'])) { $unit_info = new EfrontUnit($fields['entity_ID']); $fields['entity_name'] = $unit_info['name']; } } $replace = false; if (isset($fields['replace']) && $fields['replace'] === true) { $replace = true; unset($fields['replace']); } $create_negative = true; if (isset($fields['create_negative']) && $fields['create_negative'] === false) { $create_negative = false; unset($fields['create_negative']); } $delete_negative = false; if (isset($fields['delete_negative']) && $fields['delete_negative'] === true) { $delete_negative = true; unset($fields['delete_negative']); } // If social eFront module is enabled then log this event // Negative events like not visited, not completed etc are not to be logged if ($fields['type'] > 0 && (!isset($event_types[$fields['type']]['notToBeLogged']) || $event_types[$fields['type']]['notToBeLogged'] == 0)) { if (isset($fields['explicitly_selected'])) { $explicitly_selected = $fields['explicitly_selected']; unset($fields['explicitly_selected']); } EfrontEvent::logEvent($fields); if (isset($explicitly_selected)) { $fields['explicitly_selected'] = $explicitly_selected; } } // By default all notifications will be sent if ($send_notification) { $event = new EfrontEvent($fields); // this should create an event instance for our class $event->appendNewNotification($event_types, $replace, $create_negative, $delete_negative); // append this notification to the email queue } }