echo display_error();
    application_log("error", "Group [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["group"] . "] and role [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["role"] . "] does not have access to this module [" . $MODULE . "]");
} else {
    if ($EVENT_ID) {
        $query = "\tSELECT a.*, b.`organisation_id`\n\t\t\t\t\tFROM `events` AS a\n\t\t\t\t\tLEFT JOIN `courses` AS b\n\t\t\t\t\tON b.`course_id` = a.`course_id`\n\t\t\t\t\tWHERE a.`event_id` = " . $db->qstr($EVENT_ID);
        $event_info = $db->GetRow($query);
        if ($event_info) {
            if (!$ENTRADA_ACL->amIAllowed(new EventContentResource($event_info["event_id"], $event_info["course_id"], $event_info["organisation_id"]), "update")) {
                application_log("error", "Someone attempted to view statistics for an event [" . $EVENT_ID . "] that they were not the coordinator for.");
                header("Location: " . ENTRADA_URL . "/admin/" . $MODULE);
                exit;
            } else {
                $BREADCRUMB[] = array("url" => ENTRADA_URL . "/admin/events?" . replace_query(array("section" => "Statistics", "id" => $EVENT_ID)), "title" => "Event Statistics");
                $PROCESSED["proxy_id"] = $ENTRADA_USER->getID();
                //This will create a record set that has the proxyid, firstname, lastname, last timestamp, view per user.
                $statistics = Models_Statistic::getEventViews($EVENT_ID);
                $total_views = 0;
                events_subnavigation($event_info, 'statistics');
                ?>
                <div class="content-small"><?php 
                echo fetch_course_path($event_info["course_id"]);
                ?>
</div>
                <h1 id="page-top" class="event-title"><?php 
                echo html_encode($event_info["event_title"]);
                ?>
</h1>
                <h2 title="Event Statistics Section">Event Statistics</h2>
                <?php 
                $HEAD[] = "<script type=\"text/javascript\" src=\"" . ENTRADA_RELATIVE . "/javascript/jquery/jquery.dataTables.min.js?release=" . html_encode(APPLICATION_VERSION) . "\"></script>";
                ?>
 echo "\t\t<td colspan=\"4\" style=\"padding-top: 10px\">\n";
 echo "\t\t\t" . ($results ? "<input type=\"button\" class=\"btn btn-danger\" value=\"Delete Selected\" onclick=\"confirmLinkDelete()\" />" : "&nbsp;");
 echo "\t\t</td>\n";
 echo "\t</tr>\n";
 echo "</tfoot>\n";
 echo "<tbody>\n";
 if ($results) {
     foreach ($results as $result) {
         $student_hidden = $result["student_hidden"];
         if ($student_hidden) {
             $link_src = "htmlh";
         } else {
             $link_src = "html";
         }
         $link_statistics[$result["elink_id"]]["title"] = $result["link_title"];
         $link_statistics[$result["elink_id"]]["statistics"] = Models_Statistic::getEventLinkViews($result["elink_id"]);
         echo "<tr>\n";
         echo "\t<td class=\"modified\" style=\"width: 50px; white-space: nowrap\">\n";
         echo "\t\t<input type=\"checkbox\" name=\"delete[]\" value=\"" . $result["elink_id"] . "\" style=\"vertical-align: middle\" />\n";
         echo "\t\t<a href=\"" . ENTRADA_URL . "/link-event.php?id=" . $result["elink_id"] . "\" target=\"_blank\"><img src=\"" . ENTRADA_URL . "/images/url-visit.gif\" width=\"16\" height=\"16\" alt=\"Visit " . html_encode($result["link"]) . "\" title=\"Visit " . html_encode($result["link"]) . "\" style=\"vertical-align: middle\" border=\"0\" /></a>\n";
         echo "\t</td>\n";
         echo "\t<td class=\"title\" style=\"white-space: normal; overflow: visible\">\n";
         echo "<img src='" . ENTRADA_URL . "/serve-icon.php?ext=" . $link_src . "' width='16' height='16' alt='" . strtoupper($link_src) . " Document' title='" . strtoupper($link_src) . " Document' style='vertical-align: middle; margin-right: 4px' />\n";
         echo "\t\t<a " . ($student_hidden ? "class='hidden_shares'" : "") . "  href=\"#link-listing\" onclick=\"openDialog('" . ENTRADA_URL . "/api/link-wizard-event.api.php?action=edit&id=" . $EVENT_ID . "&lid=" . $result["elink_id"] . "')\" title=\"Click to edit " . html_encode($result["link"]) . "\" style=\"font-weight: bold\">" . ($result["link_title"] != "" ? html_encode($result["link_title"]) : $result["link"]) . "</a>\n";
         echo "\t</td>\n";
         echo "\t<td class=\"date-small\"><span class=\"content-date\">" . ((int) $result["release_date"] ? date(DEFAULT_DATE_FORMAT, $result["release_date"]) : "No Restrictions") . "</span></td>\n";
         echo "\t<td class=\"date-small\"><span class=\"content-date\">" . ((int) $result["release_until"] ? date(DEFAULT_DATE_FORMAT, $result["release_until"]) : "No Restrictions") . "</span></td>\n";
         echo "\t<td class=\"accesses\" style=\"text-align: center\"><a href=\"#link-statistic-" . $result["elink_id"] . "\" data-toggle=\"modal\" title=\"Click to see access log " . html_encode($result["accesses"]) . "\" style=\"font-weight: bold\">" . html_encode($result["accesses"]) . "</a></td>\n";
         echo "</tr>\n";
     }
 } else {
/**
 * Include the Entrada init code.
 */
require_once "init.inc.php";
ob_clear_open_buffers();
if (!isset($_SESSION["isAuthorized"]) || !$_SESSION["isAuthorized"]) {
    add_error("It appears as though your session has expired; you will now be taken back to the login page.");
    echo json_encode(array("status" => "error", "data" => $ERRORSTR));
    exit;
} else {
    if (isset($_GET["assessment_id"]) && ($tmp_input = clean_input($_GET["assessment_id"]))) {
        $PROCESSED["assessment_id"] = $tmp_input;
    }
    if ($PROCESSED["assessment_id"]) {
        if ($ENTRADA_ACL->amIAllowed(new GradebookResource($PROCESSED["assessment_id"], $result["course_id"], $result["organisation_id"]), "update")) {
            $assessment_statistics = Models_Statistic::getGradebookViews($PROCESSED["assessment_id"]);
            if ($assessment_statistics) {
                foreach ($assessment_statistics as $assessment_statistic) {
                    $output[] = array($assessment_statistic["lastname"] . ", " . $assessment_statistic["firstname"], $assessment_statistic["views"], $assessment_statistic["first_viewed_time"], $assessment_statistic["last_viewed_time"]);
                }
                echo json_encode(array("status" => "success", "data" => $output));
            } else {
                echo json_encode(array("status" => "success", "data" => array("This assessessment has not been viewed")));
            }
        } else {
            add_error("Your MEdTech account does not have the permissions required to use this feature of this module. If you believe you are receiving this message in error please contact the MEdTech Unit at 613-533-6000 x74918 and we can assist you.");
            echo json_encode(array("status" => "error", "data" => $ERRORSTR));
            application_log("error", "Group [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["group"] . "] and role [" . $_SESSION["permissions"][$ENTRADA_USER->getAccessId()]["role"] . "] does not have access to the file wizard.");
        }
    } else {
        add_error("The provided assessment identifier does not exist in this system.");
 //        $EFILE_ID = $_POST["EFILE_ID"];
 //    }
 if (isset($_POST["action"]) && $_POST["action"] != "") {
     $action = $_POST["action_field"];
 }
 if (isset($_POST["action_field"]) && $_POST["action_field"] != "") {
     $action_field = $_POST["action_field"];
 }
 if (isset($_POST["action_value"]) && $_POST["action_value"] != "") {
     $action_value = $_POST["action_value"];
 }
 if (isset($_POST["module"]) && $_POST["module"] != "") {
     $module = $_POST["module"];
 }
 $html = "";
 $file_views = Models_Statistic::getCommunityFileViews($module, $action_value);
 if ($file_views) {
     foreach ($file_views as $file_view) {
         $html .= "<tr>";
         $html .= "<td>" . $file_view["lastname"] . ", " . $file_view["firstname"] . "</td>";
         $html .= "<td class='centered'>" . $file_view["views"] . "</td>";
         $html .= "<td>" . date("Y-m-d H:i", $file_view["last_viewed_time"]) . "</td>";
         $html .= "</tr>";
     }
 } else {
     $html .= "<tr>";
     $html .= "<td colspan='3'>This file has not yet been viewed by users.</td>";
     $html .= "</tr>";
 }
 $record = array();
 $record["html"] = $html;
                            text-align:left;
                        }
                    </style>

                    <div id="file-views" style="display:none;">
                        <table class="table table-bordered table-striped" id="file-views-table" cellspacing="0">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Views</th>
                                    <th>Last Viewed</th>
                                </tr>
                            </thead>
                            <tbody>
                        <?php 
                $file_views = Models_Statistic::getCommunityFileViews("community:" . $COMMUNITY_ID . ":shares", $result["csfile_id"]);
                if ($file_views) {
                    foreach ($file_views as $file_view) {
                        ?>
                                    <tr>
                                        <td><?php 
                        echo $file_view["lastname"] . ", " . $file_view["firstname"];
                        ?>
</td>
                                        <td class="centered"><?php 
                        echo $file_view["views"];
                        ?>
</td>
                                        <td><?php 
                        echo date("Y-m-d H:i", $file_view["last_viewed_time"]);
                        ?>
 }
 if ($ENTRADA_ACL->amIAllowed(new CourseContentResource($course_details["course_id"], $course_details["organisation_id"]), "update")) {
     echo "<td><a href=\"{$url}\">" . html_encode($result["name"]) . "</a></td>";
     echo "<td><a href=\"{$url}\">" . $result["grade_weighting"] . "%</a></td>";
 } else {
     echo "<td>" . html_encode($result["name"]) . "</td>";
     echo "<td>" . $result["grade_weighting"] . "%</td>";
 }
 $query = "\tSELECT a.`course_id`, a.`assignment_id`, a.`assignment_title` \n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM `assignments` a\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE a.`assessment_id` = " . $db->qstr($result["assessment_id"]) . "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND a.`assignment_active` = 1";
 $assignment = $db->GetRow($query);
 $action_field = "assessment_id";
 $action = "view";
 $query = "SELECT b.`id` AS `proxy_id`, CONCAT_WS(', ', b.`lastname`, b.`firstname`) AS `fullname`, b.`number`\n                                                        FROM `" . AUTH_DATABASE . "`.`user_data` AS b\n                                                        JOIN `" . AUTH_DATABASE . "`.`user_access` AS c\n                                                        ON c.`user_id` = b.`id`\n                                                        AND c.`app_id` IN (" . AUTH_APP_IDS_STRING . ")\n                                                        AND c.`account_active` = 'true'\n                                                        AND (c.`access_starts` = '0' OR c.`access_starts`<=" . $db->qstr(time()) . ")\n                                                        AND (c.`access_expires` = '0' OR c.`access_expires`>=" . $db->qstr(time()) . ")\n                                                        JOIN `group_members` AS c\n                                                        ON b.`id` = c.`proxy_id`\n                                                        WHERE c.`group` = 'student'\n                                                        AND c.`group_id` = " . $db->qstr($output_cohort["group_id"]) . "\n                                                        AND c.`member_active` = '1'\n                                                        ORDER BY b.`lastname` ASC, b.`firstname` ASC";
 $students = $db->GetAll($query);
 $params = array("module" => "gradebook", "action" => "view", "action_field" => "assessment_id", "action_value" => $result["assessment_id"]);
 $assessment_views = Models_Statistic::getCountByParams($params);
 if ($assignment && $ENTRADA_ACL->amIAllowed(new AssignmentResource($course_details["course_id"], $course_details["organisation_id"], $assignment["assignment_id"]), "update")) {
     $url = ENTRADA_URL . "/admin/gradebook/assignments?section=grade&amp;id=" . $COURSE_ID . "&amp;assignment_id=" . $assignment["assignment_id"];
     echo "<td id=\"assignment-" . $assignment["assignment_id"] . "\">";
     echo "  <a href=\"" . ENTRADA_URL . "/admin/gradebook/assignments?section=download-submissions&assignment_id=" . $assignment["assignment_id"] . "&id=" . $COURSE_ID . "\"><i class=\"icon-download-alt\"></i></a>";
     if ($ENTRADA_ACL->amIAllowed(new CourseContentResource($course_details["course_id"], $course_details["organisation_id"]), "update")) {
         echo "<a href=\"" . ENTRADA_URL . "/admin/gradebook/assignments?section=delete&id=" . $COURSE_ID . "&delete=" . $assignment["assignment_id"] . "\"><i class=\"icon-minus-sign\"></i></a>";
     }
     echo "  <a href=\"" . $url . "\">" . html_encode($assignment["assignment_title"]) . "</a>";
     echo "</td>";
 } else {
     echo "<td>\n";
     if ($ENTRADA_ACL->amIAllowed(new CourseContentResource($course_details["course_id"], $course_details["organisation_id"]), "update")) {
         echo "<a href=\"" . ENTRADA_URL . "/admin/gradebook/assignments?section=add&id=" . $COURSE_ID . "&assessment_id=" . $result["assessment_id"] . "\"><i class=\"icon-plus-sign\"></i> Add New Assignment</a>";
     } else {
         echo "&nbsp;";