Esempio n. 1
0
 echo generate_calendar("encounter", "Encounter Date", true, $entry->getEncounterDate() ? $entry->getEncounterDate() : time(), true);
 ?>
     </table>
     <br />
     <div class="control-group row-fluid">
         <label for="course_id" class="form-required span3">Rotation</label>
         <span class="controls span8">
             <?php 
 $courses = $logbook->getLoggingCourses();
 ?>
             <select id="course_id" name="course_id" style="width: 100%" onchange="loadObjectivesList()">
             <option value="0">-- Select Rotation --</option>
             <?php 
 if ($courses) {
     $found_enrolled = false;
     $enrolled_course_ids = groups_get_explicitly_enrolled_course_ids($ENTRADA_USER->getID(), true);
     foreach ($courses as $course) {
         echo "<option value=\"" . (int) $course["course_id"] . "\"" . ($entry->getCourseID() == (int) $course["course_id"] || !$entry->getCourseID() && in_array($course["course_id"], $enrolled_course_ids) && !$found_enrolled ? " selected=\"selected\"" : "") . ">" . $course["course_name"] . "</option>\n";
         if (!$entry->getCourseID() && !$found_enrolled && in_array($course["course_id"], $enrolled_course_ids)) {
             $entry->setCourseID($course["course_id"]);
             $found_enrolled = true;
         }
     }
 }
 ?>
             </select>
         </span>
     </div>
     <div class="control-group row-fluid">
         <label for="institution_id" class="form-required span3">Institution</label>
         <span class="controls span8">
/**
 * Function used by public events and admin events index to generate the SQL queries based on the users
 * filter settings and results that can be iterated through by these views.
 */
function events_fetch_filtered_events($proxy_id = 0, $user_group = "", $user_role = "", $organisation_id = 0, $sort_by = "", $sort_order = "", $date_type = "", $timestamp_start = 0, $timestamp_finish = 0, $filters = array(), $pagination = true, $current_page = 1, $results_per_page = 15, $community_id = false, $respect_time_release = true)
{
    global $db, $ENTRADA_ACL, $ENTRADA_USER, $ENTRADA_CACHE;
    $output = array("duration_start" => 0, "duration_end" => 0, "total_rows" => 0, "total_pages" => 0, "page_current" => 0, "page_previous" => 0, "page_next" => 0, "result_ids_map" => array(), "events" => array());
    if (!($proxy_id = (int) $proxy_id)) {
        return false;
    }
    $user_group = clean_input($user_group);
    $user_role = clean_input($user_role);
    if (!($organisation_id = (int) $organisation_id)) {
        return false;
    }
    $sort_by = clean_input($sort_by);
    $sort_order = strtoupper($sort_order) == "ASC" ? "ASC" : "DESC";
    $date_type = clean_input($date_type);
    if (!($timestamp_start = (int) $timestamp_start)) {
        return false;
    }
    $timestamp_finish = (int) $timestamp_finish;
    if (!is_array($filters)) {
        $filters = array();
    }
    $pagination = (bool) $pagination;
    if (!($current_page = (int) $current_page)) {
        $current_page = 1;
    }
    if (!($results_per_page = (int) $results_per_page)) {
        $results_per_page = 15;
    }
    $filter_clerkship_events = false;
    if ($user_group == "student" && $ENTRADA_ACL->amIAllowed("clerkship", "read")) {
        $query = "\tSELECT a.*, c.*\n\t\t\t\t\tFROM `" . CLERKSHIP_DATABASE . "`.`events` AS a\n\t\t\t\t\tLEFT JOIN `" . CLERKSHIP_DATABASE . "`.`event_contacts` AS b\n\t\t\t\t\tON b.`event_id` = a.`event_id`\n\t\t\t\t\tLEFT JOIN `" . CLERKSHIP_DATABASE . "`.`global_lu_rotations` AS c\n\t\t\t\t\tON c.`rotation_id` = a.`rotation_id`\n\t\t\t\t\tWHERE (a.`event_status` = 'published' OR a.`event_status` = 'approval')\n\t\t\t\t\tAND b.`econtact_type` = 'student'\n\t\t\t\t\tAND b.`etype_id` = " . $db->qstr($proxy_id) . "\n\t\t\t\t\tORDER BY a.`event_start` ASC";
        $clerkship_events = $db->GetAll($query);
        if ($clerkship_events) {
            $course_ids_string = "";
            $course_ids = array();
            $clerkship_start = $clerkship_events[0]["event_start"];
            $clerkship_finish = $clerkship_events[0]["event_finish"];
            $time_periods = array();
            foreach ($clerkship_events as $clerkship_event) {
                if ($clerkship_event["event_start"] < $clerkship_start) {
                    $clerkship_start = $clerkship_event["event_start"];
                }
                if ($clerkship_event["event_finish"] > $clerkship_finish) {
                    $clerkship_finish = $clerkship_event["event_finish"];
                }
                $filter_clerkship_events = true;
                if ($clerkship_event["course_id"] && $clerkship_event["rotation_id"] != MAX_ROTATION) {
                    if (array_search($clerkship_event["course_id"], $course_ids) === false) {
                        if ($course_ids_string) {
                            $course_ids_string .= ", " . $db->qstr($clerkship_event["course_id"]);
                        } else {
                            $course_ids_string = $db->qstr($clerkship_event["course_id"]);
                        }
                        $course_ids[] = $clerkship_event["course_id"];
                    }
                    if (count($time_periods)) {
                        $time_periods[] = "OR (`courses`.`course_id` = " . $db->qstr($clerkship_event["course_id"]) . " AND ((`events`.`event_start` >= " . $db->qstr($clerkship_event["event_start"]) . " AND `events`.`event_start` <= " . $db->qstr($clerkship_event["event_finish"]) . ") OR (`events`.`event_finish` <= " . $db->qstr($clerkship_event["event_finish"]) . " AND `events`.`event_finish` >= " . $db->qstr($clerkship_event["event_start"]) . ") OR (`events`.`event_start` <= " . $db->qstr($clerkship_event["event_start"]) . " AND `events`.`event_finish` >= " . $db->qstr($clerkship_event["event_start"]) . ")))";
                    } else {
                        $time_periods[] = "(`courses`.`course_id` = " . $db->qstr($clerkship_event["course_id"]) . " AND ((`events`.`event_start` >= " . $db->qstr($clerkship_event["event_start"]) . " AND `events`.`event_start` <= " . $db->qstr($clerkship_event["event_finish"]) . ") OR (`events`.`event_finish` <= " . $db->qstr($clerkship_event["event_finish"]) . " AND `events`.`event_finish` >= " . $db->qstr($clerkship_event["event_start"]) . ") OR (`events`.`event_start` <= " . $db->qstr($clerkship_event["event_start"]) . " AND `events`.`event_finish` >= " . $db->qstr($clerkship_event["event_start"]) . ")))";
                    }
                }
            }
            $time_periods[] = "OR (`events`.`event_start` < " . $db->qstr($clerkship_start) . ")";
            $time_periods[] = "OR (`events`.`event_finish` > " . $db->qstr($clerkship_finish) . ")";
        }
    }
    $sort_by = events_fetch_sorting_query($sort_by, $sort_order);
    /**
     * This fetches the unix timestamps from the first and last second of the day, week, month, year, etc.
     */
    $display_duration = fetch_timestamps($date_type, $timestamp_start, $timestamp_finish);
    $output["duration_start"] = $display_duration["start"];
    $output["duration_end"] = $display_duration["end"];
    $query_events = "";
    $query_events_select = "SELECT '" . (int) $respect_time_release . "' AS `respect_time_release`,\n                            `events`.`event_id`,\n                            `events`.`course_id`,\n                            `events`.`parent_id`,\n                            `events`.`recurring_id`,\n                            `events`.`event_title`,\n                            `events`.`event_description`,\n                            `events`.`event_duration`,\n                            `events`.`event_message`,\n                            `events`.`event_location`,\n                            `events`.`event_start`,\n                            `events`.`event_finish`,\n                            `events`.`release_date`,\n                            `events`.`release_until`,\n                            `events`.`updated_date`,\n\t\t\t\t\t\t\t`events`.`objectives_release_date`,\n                            `event_audience`.`audience_type`,\n                            `courses`.`organisation_id`,\n                            `courses`.`course_code`,\n                            `courses`.`course_name`,\n                            `courses`.`permission`,\n                            `curriculum_lu_types`.`curriculum_type_id`,\n                            `curriculum_lu_types`.`curriculum_type_name` AS `event_phase`,\n                            `curriculum_lu_types`.`curriculum_type_name` AS `event_term`,\n                            CONCAT_WS(', ', `" . AUTH_DATABASE . "`.`user_data`.`lastname`, `" . AUTH_DATABASE . "`.`user_data`.`firstname`) AS `fullname`\n                            FROM `events`";
    $query_events_count = "SELECT COUNT(DISTINCT `events`.`event_id`) AS `event_count` FROM `events`";
    /**
     * If there are filters set by the user, build the SQL to reflect the filters.
     */
    if (is_array($filters) && !empty($filters)) {
        $build_query = array();
        $where_teacher = array();
        $where_student_course_ids = array();
        // Students' enrolled in courses only
        $where_student_cohorts = array();
        // Students' cohort events
        $where_student_proxy_ids = array();
        // Students' indivdual events
        $where_student_cgroup_ids = array();
        // Students' course small groups events
        $where_cohort = array();
        $where_course = array();
        $where_term = array();
        $where_eventtype = array();
        $where_clinical_presentation = array();
        $where_curriculum_objective = array();
        $where_topic = array();
        $where_department = array();
        $join_event_contacts = array();
        $contact_sql = "";
        $objective_sql = "";
        $topic_sql = "";
        $query_events .= "\tLEFT JOIN `event_contacts` AS `primary_teacher`\n\t\t\t\t\t\t\tON `primary_teacher`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\tAND `primary_teacher`.`contact_order` = '0'\n\t\t\t\t\t\t\tLEFT JOIN `event_eventtypes`\n\t\t\t\t\t\t\tON `event_eventtypes`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\tLEFT JOIN `event_audience`\n\t\t\t\t\t\t\tON `event_audience`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\t%CONTACT_JOIN%\n\t\t\t\t\t\t\tLEFT JOIN `" . AUTH_DATABASE . "`.`user_data`\n\t\t\t\t\t\t\tON `" . AUTH_DATABASE . "`.`user_data`.`id` = `primary_teacher`.`proxy_id`\n\t\t\t\t\t\t\tLEFT JOIN `courses`\n\t\t\t\t\t\t\tON `courses`.`course_id` = `events`.`course_id`\n\t\t\t\t\t\t\tLEFT JOIN `curriculum_lu_types`\n\t\t\t\t\t\t\tON `curriculum_lu_types`.`curriculum_type_id` = `courses`.`curriculum_type_id`\n\t\t\t\t\t\t\t%OBJECTIVE_JOIN%\n\t\t\t\t\t\t\t%TOPIC_JOIN%\n\t\t\t\t\t\t\tWHERE `courses`.`organisation_id` = " . $db->qstr($organisation_id) . "\n\t\t\t\t\t\t\t" . ($filter_clerkship_events && $course_ids_string ? "AND (`courses`.`course_id` NOT IN (" . $course_ids_string . ")\n OR (" . implode("\n", $time_periods) . "))" : "") . "\n\t\t\t\t\t\t\t" . ($display_duration ? " AND `events`.`event_start` BETWEEN " . $db->qstr($display_duration["start"]) . " AND " . $db->qstr($display_duration["end"]) : "");
        if (!is_array($filters) || empty($filters)) {
            // Apply default filters.
        }
        if (!empty($filters)) {
            foreach ($filters as $filter_type => $filter_contents) {
                if (is_array($filter_contents) && !empty($filter_contents)) {
                    foreach ($filter_contents as $filter_key => $filter_value) {
                        switch ($filter_type) {
                            case "teacher":
                                $where_teacher[] = (int) $filter_value;
                                break;
                            case "student":
                                if ($user_group != "student" || $filter_value == $proxy_id) {
                                    // Students' enrolled in courses only
                                    $course_ids = groups_get_explicitly_enrolled_course_ids((int) $filter_value, false, $organisation_id);
                                    if ($course_ids) {
                                        $where_student_course_ids = $course_ids;
                                    }
                                    // Students' cohort events
                                    $cohorts = groups_get_cohorts((int) $filter_value);
                                    if ($cohorts) {
                                        foreach ($cohorts as $cohort) {
                                            $where_student_cohorts[] = $cohort["group_id"];
                                        }
                                    }
                                    // Students' indivdual events
                                    $where_student_proxy_ids[] = (int) $filter_value;
                                    // Students' course small groups events
                                    $cgroup_ids = course_fetch_enrolled_course_groups((int) $filter_value);
                                    if ($cgroup_ids) {
                                        $where_student_cgroup_ids = $cgroup_ids;
                                    }
                                }
                                break;
                            case "group":
                                $where_cohort[] = (int) $filter_value;
                                break;
                            case "course":
                                $where_course[] = (int) $filter_value;
                                break;
                            case "term":
                                $where_term[] = (int) $filter_value;
                                break;
                            case "eventtype":
                                $where_eventtype[] = (int) $filter_value;
                                break;
                            case "cp":
                                $where_clinical_presentation[] = (int) $filter_value;
                                break;
                            case "co":
                                $where_curriculum_objective[] = (int) $filter_value;
                                break;
                            case "topic":
                                $where_topic[] = (int) $filter_value;
                                break;
                            case "department":
                                $where_department[] = (int) $filter_value;
                                break;
                            default:
                                continue;
                                break;
                        }
                    }
                }
            }
        }
        if ($where_teacher) {
            $build_query[] = "(`primary_teacher`.`proxy_id` IN (" . implode(", ", $where_teacher) . ") OR `event_contacts`.`proxy_id` IN (" . implode(", ", $where_teacher) . "))";
        }
        if ($where_student_course_ids || $where_student_cohorts || $where_student_proxy_ids || $where_student_cgroup_ids) {
            $where_student = array();
            if ($where_student_course_ids) {
                $where_student_course_ids = array_unique($where_student_course_ids);
                $where_student[] = "(`event_audience`.`audience_type` = 'course_id' AND `event_audience`.`audience_value` IN (" . implode(", ", $where_student_course_ids) . "))";
            }
            if ($where_student_cohorts) {
                $where_student_cohorts = array_unique($where_student_cohorts);
                $where_student[] = "(`event_audience`.`audience_type` = 'cohort' AND `event_audience`.`audience_value` IN (" . implode(", ", $where_student_cohorts) . "))";
            }
            if ($where_student_proxy_ids) {
                $where_student_proxy_ids = array_unique($where_student_proxy_ids);
                $where_student[] = "(`event_audience`.`audience_type` = 'proxy_id' AND `event_audience`.`audience_value` IN (" . implode(", ", $where_student_proxy_ids) . "))";
            }
            if ($where_student_cgroup_ids) {
                $where_student_cgroup_ids = array_unique($where_student_cgroup_ids);
                $where_student[] = "(`event_audience`.`audience_type` = 'group_id' AND `event_audience`.`audience_value` IN (" . implode(", ", $where_student_cgroup_ids) . "))";
            }
            $build_query[] = "(" . implode(" OR ", $where_student) . ")";
        }
        if ($where_cohort) {
            $build_query[] = "(`event_audience`.`audience_type` = 'cohort' AND `event_audience`.`audience_value` IN (" . implode(", ", $where_cohort) . "))";
        }
        if ($where_course) {
            $build_query[] = "(`events`.`course_id` IN (" . implode(", ", $where_course) . "))";
        }
        if ($where_term) {
            $build_query[] = "(`curriculum_lu_types`.`curriculum_type_id` IN (" . implode(", ", $where_term) . "))";
        }
        if ($where_eventtype) {
            $build_query[] = "(`event_eventtypes`.`eventtype_id` IN (" . implode(", ", $where_eventtype) . "))";
        }
        if ($where_clinical_presentation) {
            $build_query[] = "(`event_objectives`.`objective_id` IN (" . implode(", ", $where_clinical_presentation) . "))";
        }
        if ($where_curriculum_objective) {
            $build_query[] = "(`event_objectives`.`objective_id` IN (" . implode(", ", $where_curriculum_objective) . "))";
        }
        if ($where_topic) {
            $build_query[] = "(`event_topics`.`topic_id` IN (" . implode(", ", $where_topic) . "))";
        }
        if ($build_query) {
            $query_events .= " AND (" . implode(") AND (", $build_query) . ")";
        }
        if ($where_teacher) {
            $contact_sql = "\tLEFT JOIN `event_contacts`\n\t\t\t\t\t\t\t\tON `event_contacts`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\t\tAND (`event_contacts`.`proxy_id` IN (" . implode(", ", $where_teacher) . "))";
        }
        if ($where_clinical_presentation || $where_curriculum_objective) {
            $objective_sql = "\tLEFT JOIN `event_objectives`\n\t\t\t\t\t\t\t\tON `event_objectives`.`event_id` = `events`.`event_id`";
        }
        if ($where_topic) {
            $topic_sql = "\tLEFT JOIN `event_topics`\n\t\t\t\t\t\t\tON `event_topics`.`event_id` = `events`.`event_id`";
        }
        if ($where_department) {
            $event_ids = "";
            // fetch the user_id of members in the selected departments
            $department_members_query = "\tSELECT `a`.`id`\n\t\t\t\t\t\t\t\t\t\t\tFROM `" . AUTH_DATABASE . "`.`user_data` AS `a`\n\t\t\t\t\t\t\t\t\t\t\tJOIN `" . AUTH_DATABASE . "`.`user_departments` AS `b`\n\t\t\t\t\t\t\t\t\t\t\tON `a`.`id` = `b`.`user_id`\n\t\t\t\t\t\t\t\t\t\t\tJOIN `" . AUTH_DATABASE . "`.`departments` AS `c`\n\t\t\t\t\t\t\t\t\t\t\tON `b`.`dep_id` = `c`.`department_id`\n\t\t\t\t\t\t\t\t\t\t\tWHERE `b`.`dep_id` IN (" . implode(',', $where_department) . ")\n\t\t\t\t\t\t\t\t\t\t\tGROUP BY `a`.`id`";
            $department_members = $db->GetAll($department_members_query);
            if ($department_members) {
                foreach ($department_members as $member) {
                    $members_list[] = $member["id"];
                }
                // fetch the event_id the members are assigned to
                $department_events_query = "\tSELECT `a`.`event_id`\n\t\t\t\t\t\t\t\t\t\t\t\tFROM `events` AS `a`\n\t\t\t\t\t\t\t\t\t\t\t\tJOIN `event_contacts` AS `b`\n\t\t\t\t\t\t\t\t\t\t\t\tON `a`.`event_id` = `b`.`event_id`\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE `b`.`proxy_id` IN (" . implode(',', $members_list) . ")\n\t\t\t\t\t\t\t\t\t\t\t\tAND `a`.`event_start` > " . $db->qstr($display_duration["start"]) . "\n\t\t\t\t\t\t\t\t\t\t\t\tAND `a`.`event_finish` < " . $db->qstr($display_duration["end"]) . "\n\t\t\t\t\t\t\t\t\t\t\t\tGROUP BY `a`.`event_id`";
                $department_events = $db->GetAll($department_events_query);
                if ($department_events) {
                    foreach ($department_events as $event) {
                        $event_list[] = $event["event_id"];
                    }
                }
                $event_ids = !empty($event_list) ? implode(", ", $event_list) : '';
            }
            $query_events .= " AND `events`.`event_id` IN (" . $event_ids . ")";
        }
        $query_events = str_replace("%CONTACT_JOIN%", $contact_sql, $query_events);
        $query_events = str_replace("%OBJECTIVE_JOIN%", $objective_sql, $query_events);
        $query_events = str_replace("%TOPIC_JOIN%", $topic_sql, $query_events);
    } else {
        $query_events .= "\tLEFT JOIN `event_contacts`\n\t\t\t\t\t\t\tON `event_contacts`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\tAND `event_contacts`.`contact_order` = '0'\n\t\t\t\t\t\t\tLEFT JOIN `event_audience`\n\t\t\t\t\t\t\tON `event_audience`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\tLEFT JOIN `" . AUTH_DATABASE . "`.`user_data`\n\t\t\t\t\t\t\tON `" . AUTH_DATABASE . "`.`user_data`.`id` = `event_contacts`.`proxy_id`\n\t\t\t\t\t\t\tLEFT JOIN `courses`\n\t\t\t\t\t\t\tON (`courses`.`course_id` = `events`.`course_id`)\n\t\t\t\t\t\t\tLEFT JOIN `curriculum_lu_types`\n\t\t\t\t\t\t\tON `curriculum_lu_types`.`curriculum_type_id` = `courses`.`curriculum_type_id`\n\t\t\t\t\t\t\tWHERE `courses`.`organisation_id` = " . $db->qstr($organisation_id) . "\n\t\t\t\t\t\t\t" . ($filter_clerkship_events && $course_ids_string ? "AND (`courses`.`course_id` NOT IN (" . $course_ids_string . ")\n OR (" . implode("\n", $time_periods) . "))" : "") . "\n\t\t\t\t\t\t\t" . ($display_duration ? "AND `events`.`event_start` BETWEEN " . $db->qstr($display_duration["start"]) . " AND " . $db->qstr($display_duration["end"]) : "");
    }
    /**
     * This builds the counting query that is run to see whether or not
     * the cache gets hit.
     */
    $query_events_count .= " " . $query_events;
    $query_events_select .= " " . $query_events . "GROUP BY `events`.`event_id` ORDER BY %s";
    $limitless_query_events = sprintf($query_events_select, $sort_by);
    /**
     * Provide the previous query so we can have previous / next event links on the details page.
     */
    if (session_id()) {
        $stored_query = false;
        $stored_events_count = false;
        if ($community_id && isset($_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["limitless_query"]) && $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["limitless_query"] || isset($_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["limitless_query"]) && $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["limitless_query"]) {
            if ($community_id == false) {
                if (isset($_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"])) {
                    $stored_query = $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["limitless_query"];
                    $stored_events_count = $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["total_returned_rows"];
                }
            } else {
                if (isset($_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"])) {
                    $stored_query = $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["limitless_query"];
                    $stored_events_count = $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["total_returned_rows"];
                }
            }
        }
        $live_events_count = (int) $db->GetOne($query_events_count);
        $current_result_ids_map = $ENTRADA_CACHE->load(($community_id ? "community_" . $community_id . "_" : "") . "events_map_" . AUTH_APP_ID . "_" . $ENTRADA_USER->getID());
        if (!$stored_query || $stored_query != $limitless_query_events || !isset($current_result_ids_map) || !$current_result_ids_map || !$stored_events_count || $live_events_count != $stored_events_count) {
            $all_events = $db->GetAll($limitless_query_events);
            $result_ids_map = array();
            if ($all_events) {
                if ($user_group == "student") {
                    $enroled_courses = groups_get_enrolled_course_ids($proxy_id, true, $output["duration_start"], $output["duration_end"]);
                } else {
                    $enroled_courses = array();
                }
                foreach ($all_events as $map_event) {
                    if ($user_group != "student" || in_array($map_event["course_id"], $enroled_courses)) {
                        if ($respect_time_release) {
                            $event_resource = new EventResource($map_event["event_id"], $map_event["course_id"], $map_event["organisation_id"]);
                            if ((!$map_event["release_date"] || $map_event["release_date"] <= time()) && (!$map_event["release_until"] || $map_event["release_until"] >= time()) && $ENTRADA_ACL->amIAllowed($event_resource, "read", true)) {
                                $result_ids_map[] = $map_event["event_id"];
                            }
                        } else {
                            $result_ids_map[] = $map_event["event_id"];
                        }
                    }
                }
            }
            $ENTRADA_CACHE->save($result_ids_map, ($community_id ? "community_" . $community_id . "_" : "") . "events_map_" . AUTH_APP_ID . "_" . $ENTRADA_USER->getID(), array("events", "community"), 10800);
        } else {
            $result_ids_map = $current_result_ids_map;
        }
        $output["total_rows"] = count($result_ids_map);
        if ($output["total_rows"] <= $results_per_page) {
            $output["total_pages"] = 1;
        } elseif ($output["total_rows"] % $results_per_page == 0) {
            $output["total_pages"] = (int) ($output["total_rows"] / $results_per_page);
        } else {
            $output["total_pages"] = (int) ($output["total_rows"] / $results_per_page) + 1;
        }
        /**
         * Check if pv variable is set and see if it's a valid page, other wise page 1 it is.
         */
        if ($current_page) {
            $output["page_current"] = (int) trim($current_page);
            if ($output["page_current"] < 1 || $output["page_current"] > $output["total_pages"]) {
                $output["page_current"] = 1;
            }
        } else {
            $output["page_current"] = 1;
        }
        $output["page_previous"] = $output["page_current"] > 1 ? $output["page_current"] - 1 : false;
        $output["page_next"] = $output["page_current"] < $output["total_pages"] ? $output["page_current"] + 1 : false;
        $output["result_ids_map"] = $result_ids_map;
        if ($pagination) {
            $event_ids_string = "";
            for ($i = ($output["page_current"] - 1) * $results_per_page; $i < $output["page_current"] * $results_per_page; $i++) {
                if ($i + 1 > count($result_ids_map)) {
                    break;
                }
                $event_ids_string .= ($event_ids_string ? ", " : "") . $db->qstr($result_ids_map[$i]);
            }
            if (!strlen($event_ids_string)) {
                $event_ids_string = "0";
            }
            $query_events = "\tSELECT `events`.`event_id`,\n\t\t\t\t\t\t\t\t`events`.`course_id`,\n\t\t\t\t\t\t\t\t`events`.`parent_id`,\n\t\t\t\t\t\t\t\t`events`.`recurring_id`,\n\t\t\t\t\t\t\t\t`events`.`event_title`,\n\t\t\t\t\t\t\t\t`events`.`event_description`,\n\t\t\t\t\t\t\t\t`events`.`event_duration`,\n\t\t\t\t\t\t\t\t`events`.`event_message`,\n\t\t\t\t\t\t\t\t`events`.`event_location`,\n\t\t\t\t\t\t\t\t`events`.`event_start`,\n\t\t\t\t\t\t\t\t`events`.`event_finish`,\n\t\t\t\t\t\t\t\t`events`.`release_date`,\n\t\t\t\t\t\t\t\t`events`.`release_until`,\n\t\t\t\t\t\t\t\t`events`.`updated_date`,\n\t\t\t\t\t\t\t\t`events`.`objectives_release_date`,\n\t\t\t\t\t\t\t\t`event_audience`.`audience_type`,\n\t\t\t\t\t\t\t\t`courses`.`organisation_id`,\n\t\t\t\t\t\t\t\t`courses`.`course_code`,\n\t\t\t\t\t\t\t\t`courses`.`course_name`,\n\t\t\t\t\t\t\t\t`courses`.`permission`,\n\t\t\t\t\t\t\t\t`curriculum_lu_types`.`curriculum_type_id`,\n\t\t\t\t\t\t\t\t`curriculum_lu_types`.`curriculum_type_name` AS `event_phase`,\n\t\t\t\t\t\t\t\t`curriculum_lu_types`.`curriculum_type_name` AS `event_term`,\n\t\t\t\t\t\t\t\tCONCAT_WS(', ', `" . AUTH_DATABASE . "`.`user_data`.`lastname`, `" . AUTH_DATABASE . "`.`user_data`.`firstname`) AS `fullname`\n\t\t\t\t\t\t\t\tFROM `events`\n\t\t\t\t\t\t\t\tLEFT JOIN `event_contacts`\n\t\t\t\t\t\t\t\tON `event_contacts`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\t\tAND `event_contacts`.`contact_order` = '0'\n\t\t\t\t\t\t\t\tLEFT JOIN `event_audience`\n\t\t\t\t\t\t\t\tON `event_audience`.`event_id` = `events`.`event_id`\n\t\t\t\t\t\t\t\tLEFT JOIN `" . AUTH_DATABASE . "`.`user_data`\n\t\t\t\t\t\t\t\tON `" . AUTH_DATABASE . "`.`user_data`.`id` = `event_contacts`.`proxy_id`\n\t\t\t\t\t\t\t\tLEFT JOIN `courses`\n\t\t\t\t\t\t\t\tON (`courses`.`course_id` = `events`.`course_id`)\n\t\t\t\t\t\t\t\tLEFT JOIN `curriculum_lu_types`\n\t\t\t\t\t\t\t\tON `curriculum_lu_types`.`curriculum_type_id` = `courses`.`curriculum_type_id`\n\t\t\t\t\t\t\t\tWHERE `events`.`event_id` IN (" . $event_ids_string . ")\n\t\t\t\t\t\t\t\tGROUP BY `events`.`event_id`\n\t\t\t\t\t\t\t\tORDER BY %s";
            $query_events = sprintf($query_events, $sort_by);
            $learning_events = $db->GetAll($query_events);
        } else {
            $learning_events = $db->GetAll($limitless_query_events);
        }
        if ($learning_events) {
            if ($respect_time_release) {
                $i = 0;
                foreach ($learning_events as $event) {
                    if ($event["course_id"]) {
                        $event_resource = new EventResource($event["event_id"], $event["course_id"], $event["organisation_id"]);
                    }
                    if ($event["release_date"] != 0 && $event["release_date"] > time() || $event["release_until"] != 0 && $event["release_until"] < time() || !$ENTRADA_ACL->amIAllowed($event_resource, "read", true)) {
                        unset($learning_events[$i]);
                    }
                    $i++;
                }
            }
            if ($user_group == "student") {
                $event_ids = array();
                foreach ($learning_events as $event) {
                    $event_ids[] = $event["event_id"];
                }
                if (!empty($event_ids)) {
                    $query = "\tSELECT `action_value` AS `event_id`, MAX(`statistics`.`timestamp`) AS `last_visited` FROM `statistics`\n\t\t\t\t\t\t\t\tWHERE `action_value` IN (" . implode(", ", $event_ids) . ")\n\t\t\t\t\t\t\t\tAND `module` = 'events'\n\t\t\t\t\t\t\t\tAND `proxy_id` = " . $db->qstr($proxy_id) . "\n\t\t\t\t\t\t\t\tAND `action` = 'view'\n\t\t\t\t\t\t\t\tAND `action_field` = 'event_id'\n\t\t\t\t\t\t\t\tGROUP BY `proxy_id`, `module`, `action_field`, `action`, `action_value`";
                    $last_visited_dates = $db->GetAll($query);
                    if (!empty($last_visited_dates)) {
                        $dates_array = array();
                        foreach ($last_visited_dates as $event_last_visited) {
                            $dates_array[$event_last_visited["event_id"]] = $event_last_visited["last_visited"];
                        }
                        foreach ($learning_events as &$event) {
                            if (array_key_exists($event["event_id"], $dates_array)) {
                                $event["last_visited"] = $dates_array[$event["event_id"]];
                            }
                        }
                    }
                }
            }
            $parent_ids = array();
            foreach ($learning_events as $temp_event) {
                if ($temp_event["parent_id"]) {
                    $parent_ids[] = $temp_event["parent_id"];
                }
            }
            if (!empty($parent_ids)) {
                $query = "\tSELECT * FROM `events`\n\t\t\t\t\t\t\tWHERE `event_id` IN (" . implode(", ", $parent_ids) . ")\n\t\t\t\t\t\t\tGROUP BY `event_id`";
                $parent_events = $db->GetAll($query);
                if (!empty($parent_events)) {
                    $parent_events_array = array();
                    foreach ($parent_events as $parent_event) {
                        $parent_events_array[$parent_event["event_id"]] = $parent_event;
                    }
                }
            }
            $output["events"] = $learning_events;
        }
        if ($community_id == false) {
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["dashboard"]["previous_query"]["query"] = $query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["dashboard"]["previous_query"]["limitless_query"] = $limitless_query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["dashboard"]["previous_query"]["total_returned_rows"] = $live_events_count;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["dashboard"]["previous_query"]["total_rows"] = $output["total_rows"];
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["query"] = $query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["limitless_query"] = $limitless_query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["total_returned_rows"] = $live_events_count;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["events"]["previous_query"]["total_rows"] = $output["total_rows"];
        } else {
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["query"] = $query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["limitless_query"] = $limitless_query_events;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["total_returned_rows"] = $live_events_count;
            $_SESSION[APPLICATION_IDENTIFIER]["tmp"]["community_page"][$community_id]["previous_query"]["total_rows"] = $output["total_rows"];
        }
    }
    return $output;
}