* @copyright Copyright 2012 Queen's University. All Rights Reserved.
 *
*/
@set_time_limit(0);
@set_include_path(implode(PATH_SEPARATOR, array(dirname(__FILE__) . "/../core", dirname(__FILE__) . "/../core/includes", dirname(__FILE__) . "/../core/library", get_include_path())));
/**
 * Include the Entrada init code.
 */
require_once "init.inc.php";
//queue notifications for each user with the evaluations which have opened for them in the last 24 hours.
$query = "SELECT *, '0' AS `event_id` FROM `evaluations` AS a\n\t\t\tJOIN `evaluation_forms` AS b\n\t\t\tON a.`eform_id` = b.`eform_id`\n\t\t\tJOIN `evaluations_lu_targets` AS c\n\t\t\tON b.`target_id` = c.`target_id`\n\t\t\tWHERE a.`evaluation_start` >= " . $db->qstr(strtotime("-1 day")) . "\n\t\t\tAND a.`evaluation_start` <= " . $db->qstr(time()) . "\n\t\t\tAND a.`evaluation_finish` >= " . $db->qstr(time() - ONE_WEEK * 10) . "\n\t\t\tAND c.`target_shortname` NOT IN ('preceptor', 'rotation_core', 'rotation_elective')\n\t\t\t\n\t\t\tUNION\n\t\t\t\n\t\t\tSELECT a.*, b.*, c.*, e.`event_id` FROM `evaluations` AS a\n\t\t\tJOIN `evaluation_forms` AS b\n\t\t\tON a.`eform_id` = b.`eform_id`\n\t\t\tJOIN `evaluations_lu_targets` AS c\n\t\t\tON b.`target_id` = c.`target_id`\n\t\t\tAND c.`target_shortname` IN ('preceptor', 'rotation_core', 'rotation_elective')\n\t\t\tJOIN `evaluation_targets` AS d\n\t\t\tON a.`evaluation_id` = d.`evaluation_id`\n\t\t\tAND d.`target_type` = 'rotation_id'\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`events` AS e\n\t\t\tON d.`target_value` = e.`rotation_id`\n\t\t\tAND a.`evaluation_start` <= e.`event_finish`\n\t\t\tAND a.`evaluation_finish` >= e.`event_finish`\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`event_contacts` AS f\n\t\t\tON e.`event_id` = f.`event_id`\n\t\t\tJOIN `evaluation_evaluators` AS g\n\t\t\tON a.`evaluation_id` = g.`evaluation_id`\n\t\t\tAND g.`evaluator_type` = 'cohort'\n\t\t\tJOIN `group_members` AS h\n\t\t\tON g.`evaluator_value` = h.`group_id`\n\t\t\tAND h.`proxy_id` = f.`etype_id`\n\t\t\tAND h.`member_active`\n\t\t\tWHERE e.`event_finish` <= " . $db->qstr(strtotime("+5 days")) . "\n\t\t\tAND e.`event_finish` >= " . $db->qstr(strtotime("-36 hours")) . "\n\t\t\t\n\t\t\t\n\t\t\tUNION\n\t\t\t\n\t\t\tSELECT a.*, b.*, c.*, e.`event_id` FROM `evaluations` AS a\n\t\t\tJOIN `evaluation_forms` AS b\n\t\t\tON a.`eform_id` = b.`eform_id`\n\t\t\tJOIN `evaluations_lu_targets` AS c\n\t\t\tON b.`target_id` = c.`target_id`\n\t\t\tAND c.`target_shortname` IN ('preceptor', 'rotation_core', 'rotation_elective')\n\t\t\tJOIN `evaluation_targets` AS d\n\t\t\tON a.`evaluation_id` = d.`evaluation_id`\n\t\t\tAND d.`target_type` = 'rotation_id'\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`events` AS e\n\t\t\tON d.`target_value` = e.`rotation_id`\n\t\t\tAND a.`evaluation_start` <= e.`event_finish`\n\t\t\tAND a.`evaluation_finish` >= e.`event_finish`\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`event_contacts` AS f\n\t\t\tON e.`event_id` = f.`event_id`\n\t\t\tJOIN `evaluation_evaluators` AS g\n\t\t\tON a.`evaluation_id` = g.`evaluation_id`\n\t\t\tAND g.`evaluator_type` = 'cgroup_id'\n\t\t\tJOIN `course_group_audience` AS h\n\t\t\tON g.`evaluator_value` = h.`cgroup_id`\n\t\t\tAND h.`proxy_id` = f.`etype_id`\n\t\t\tAND h.`active` = 1\n\t\t\tWHERE e.`event_finish` <= " . $db->qstr(strtotime("+5 days")) . "\n\t\t\tAND e.`event_finish` >= " . $db->qstr(strtotime("-36 hours")) . "\n\t\t\t\n\t\t\t\n\t\t\tUNION\n\t\t\t\n\t\t\tSELECT a.*, b.*, c.*, e.`event_id` FROM `evaluations` AS a\n\t\t\tJOIN `evaluation_forms` AS b\n\t\t\tON a.`eform_id` = b.`eform_id`\n\t\t\tJOIN `evaluations_lu_targets` AS c\n\t\t\tON b.`target_id` = c.`target_id`\n\t\t\tAND c.`target_shortname` IN ('preceptor', 'rotation_core', 'rotation_elective')\n\t\t\tJOIN `evaluation_targets` AS d\n\t\t\tON a.`evaluation_id` = d.`evaluation_id`\n\t\t\tAND d.`target_type` = 'rotation_id'\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`events` AS e\n\t\t\tON d.`target_value` = e.`rotation_id`\n\t\t\tAND a.`evaluation_start` <= e.`event_finish`\n\t\t\tAND a.`evaluation_finish` >= e.`event_finish`\n\t\t\tJOIN `" . CLERKSHIP_DATABASE . "`.`event_contacts` AS f\n\t\t\tON e.`event_id` = f.`event_id`\n\t\t\tJOIN `evaluation_evaluators` AS g\n\t\t\tON a.`evaluation_id` = g.`evaluation_id`\n\t\t\tAND g.`evaluator_type` = 'proxy_id'\n\t\t\tAND g.`evaluator_value` = f.`etype_id`\n\t\t\tWHERE e.`event_finish` <= " . $db->qstr(strtotime("+5 days")) . "\n\t\t\tAND e.`event_finish` >= " . $db->qstr(strtotime("-36 hours"));
$new_evaluations = $db->GetAll($query);
$pending_evaluations = array();
if ($new_evaluations) {
    foreach ($new_evaluations as $evaluation) {
        $pending_evaluations[$evaluation["evaluation_id"] . (isset($evaluation["event_id"]) && $evaluation["event_id"] ? "-" . $evaluation["event_id"] : "")] = Models_Evaluation::getEvaluationsPending($evaluation, true);
    }
}
foreach ($pending_evaluations as $pending_evaluation_users) {
    foreach ($pending_evaluation_users as $pending_evaluation) {
        $evaluation_id = $pending_evaluation["evaluation_id"];
        require_once "Models/notifications/NotificationUser.class.php";
        require_once "Models/notifications/Notification.class.php";
        $proxy_id = $pending_evaluation["user"]["id"];
        $notification_user = NotificationUser::get($proxy_id, "evaluation", $evaluation_id, $proxy_id);
        if (!$notification_user) {
            $notification_user = NotificationUser::add($proxy_id, "evaluation", $evaluation_id, $proxy_id);
        }
        $query = "SELECT * FROM `notifications` \n                    WHERE `nuser_id` = " . $db->qstr($notification_user->getID()) . " \n                    AND `proxy_id` = " . $db->qstr($proxy_id) . "\n                    AND (`sent_date` = 0 OR `sent_date` >= " . $db->qstr(strtotime("-7 days")) . ")";
        $recent_notifications = $db->GetAll($query);
        if (!isset($recent_notifications) || !$recent_notifications) {