<?php

header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
//only accessible if logged in
if (!isset($_SESSION['loggedInUser'])) {
    redirectTo("index.php");
}
require_once "model.php";
if (isFlagSet('script_running')) {
    // script is already running
    echo "data: waiting\n";
} else {
    if (!isFlagSet('script_running') && !isFlagSet('results_ready')) {
        echo "data: run\n";
    } else {
        if (isFlagSet('results_ready')) {
            clearFlag('results_ready');
            echo "data: result ready\n";
        }
    }
}
echo "\n";
ob_flush();
flush();
<?php

session_start();
//only accessible if logged in
if (!isset($_SESSION['loggedInUser'])) {
    redirectTo("index.php");
}
clearFlag("result_ready");
Example #3
0
 function calculateAttentionScore()
 {
     global $user;
     logit(INFO, "Starting Attention Score Calculator");
     $att = new AttentionAlerts();
     $med = new Meds();
     $user = new User();
     $trends = new UserMetrics();
     //check for flag
     if ($time = getFlag("MetiAttentionRun")) {
         $e = round((time() - $time) / 60, 2);
         //get hours
         if ($e > 360) {
             logit(WARN, "Error: calculateAttentionScore says it's been running for 6 hours " . __FILE__ . " on line: " . __LINE__);
         }
         logit(REPORT, " MetiAttention run aborted because it says it's still running. (Runtime: {$e} minutes, flag: MetiAttentionRun) in " . __FILE__ . " on line: " . __LINE__);
         return false;
     } else {
         setFlag("MetiAttentionRun", time());
     }
     //ok flags are fine let's run this. Round up them doggies
     $sql = "SELECT * FROM users WHERE userType = 1 AND disabled = 0 ";
     if ($rc = dbQuery($sql)) {
         while ($row = dbFetch($rc)) {
             if (CLI_ECHO) {
                 logit(INFO, "uRec: {$row["uRec"]}");
             }
             $uRec = $row["uRec"];
             $user->setUser($uRec);
             $user->getUser();
             //$trends->setUser();
             //$trends->getLatestSurveyCats();
             $this->attentionAggregate = array();
             $this->attentionAggregate["meds"] = 0;
             $this->attentionAggregate["survey"] = 0;
             $this->attentionAggregate["activity"] = 0;
             $this->attentionAggregate["medsAbs"] = 0;
             //remove med related scres
             $sql = "DELETE FROM attentionFlags WHERE uRec = '{$uRec}' AND (type = '{$att->attentionMeds}' OR type = '{$att->attentionMedAbs}') ";
             if ($rctemp = dbQuery($sql)) {
                 //calc med score.
                 $sql = "SELECT * FROM userMedCompliance WHERE uRec = '{$uRec}' AND period = '30' AND days = '30' ";
                 if ($rcMed = dbQuery($sql)) {
                     $med30 = array();
                     $med30Abs = array();
                     while ($rowMed = dbFetch($rcMed)) {
                         $med30[$rowMed["medId"]] = $rowMed["taken"] / $rowMed["doses"];
                         //if patient has reported more than 50% record med compliance
                         if ($rowMed["doses"] and $rowMed["unreported"] / $rowMed["doses"] < 0.5) {
                             $med30Abs[$rowMed["medId"]] = $rowMed["taken"] / $rowMed["doses"];
                         }
                     }
                 } else {
                     logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                 }
                 //calculate abs med compliance
                 if (is_array($med30) or is_array($med30Abs)) {
                     $med->setUser();
                     $med->getUserMeds(TRUE, FALSE);
                 }
                 if (is_array($med30Abs)) {
                     $temp = array();
                     //let's write flags for each
                     foreach ($med30Abs as $key => $value) {
                         $score = 0;
                         if ($value < 0.8) {
                             if ($value < 0.6) {
                                 $score = 3;
                             } elseif ($value < 0.7) {
                                 $score = 2;
                             } else {
                                 $score = 1;
                             }
                             $temp[] = $score;
                             $in = "";
                             $in["uRec"] = $uRec;
                             $in["ref"] = $key;
                             $in["weight"] = $score;
                             $in["type"] = $att->attentionMedAbs;
                             $in["note"] = $med->userMeds[$key]["medOther"] . " compliance is " . round($value * 100, 0) . "%";
                             $in["expire"] = dbDate("+7 days");
                             $sql = "INSERT INTO attentionFlags " . makeSql($in, "insert");
                             if ($rcTemp = dbQuery($sql)) {
                             } else {
                                 logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                             }
                         }
                     }
                     //end for
                     //get highest score
                     if (count($temp) > 0) {
                         $this->attentionAggregate["medsAbs"] = max($temp);
                     }
                 }
                 $medAlertList = array();
                 //calculate window trend for med compliance
                 //ok we got all of the 30s now let's get the 7s
                 if (is_array($med30)) {
                     foreach ($med30 as $key => $value) {
                         $sql = "SELECT * FROM userMedCompliance WHERE medId = '{$key}' AND uRec = '{$uRec}' AND period = '7' AND days = '7' ";
                         if ($rcMed = dbQuery($sql)) {
                             while ($rowMed = dbFetch($rcMed)) {
                                 $med7 = $rowMed["taken"] / $rowMed["doses"];
                                 if ($value > 0) {
                                     $d = ($med7 - $value) / $value;
                                     $delta = round($d * 100);
                                 } else {
                                     $delta = 0;
                                 }
                                 //got the delta, now let's calc att score
                                 $medScore = 0;
                                 if (is_numeric($delta) and $delta < -19) {
                                     if ($delta < -60) {
                                         $medScore = 3;
                                     } elseif ($delta < -40) {
                                         $medScore = 2;
                                     } else {
                                         $medScore = 1;
                                     }
                                     $this->attentionAggregate["meds"] = max($this->attentionAggregate["meds"], $medScore);
                                     //save score for this med.
                                     $in = "";
                                     $in["uRec"] = $uRec;
                                     $in["ref"] = $key;
                                     $in["weight"] = $medAlertList[] = $medScore;
                                     $in["type"] = $att->attentionMeds;
                                     $in["note"] = $med->userMeds[$key]["medOther"] . " compliance has declined by " . abs($delta) . "%";
                                     $in["expire"] = dbDate("+7 days");
                                     $sql = "INSERT INTO attentionFlags " . makeSql($in, "insert");
                                     if ($rcTemp = dbQuery($sql)) {
                                     } else {
                                         logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                                     }
                                 }
                             }
                         } else {
                             logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                         }
                     }
                     //end med 30 loop
                 }
                 //aggregate meds
                 //if (count($medAlertList) > 0 ) $this->attentionAggregate["meds"] = array_sum($medAlertList) / count($medAlertList);
             } else {
                 logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
             }
             //lookup survey
             $sql = "DELETE FROM attentionFlags WHERE uRec = '{$uRec}' AND type = '{$att->attentionSurvey}' ";
             if ($rctemp = dbQuery($sql)) {
                 $trends->setUser();
                 $s = $trends->getLatestSurveyCats();
                 $delta = $s["min"];
                 $surveyScore = 0;
                 if (is_numeric($delta) and $delta < -11) {
                     if ($delta < -39) {
                         $surveyScore = 3;
                     } elseif ($delta < -19) {
                         $surveyScore = 2;
                     } else {
                         $surveyScore = 1;
                     }
                     $this->attentionAggregate["survey"] = max($this->attentionAggregate["survey"], $surveyScore);
                     //save score
                     $in = "";
                     $in["uRec"] = $uRec;
                     $in["ref"] = 0;
                     $in["weight"] = $surveyScore;
                     $in["type"] = $att->attentionSurvey;
                     $in["note"] = "We\\'ve noticed a drop in some survey answers in the past 30 days.";
                     $in["expire"] = dbDate("+7 days");
                     $sql = "INSERT INTO attentionFlags " . makeSql($in, "insert");
                     if ($rcTemp = dbQuery($sql)) {
                     } else {
                         logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                     }
                 }
             } else {
                 logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
             }
             //lookup activity
             $sql = "DELETE FROM attentionFlags WHERE uRec = '{$uRec}' AND type = '{$att->attentionActivity}' ";
             if ($rctemp = dbQuery($sql)) {
                 $delta = $row["activityChange"];
                 $activityScore = 0;
                 if (is_numeric($delta) and $delta < -19) {
                     if ($delta < -59) {
                         $activityScore = 3;
                     } elseif ($delta < -39) {
                         $activityScore = 2;
                     } else {
                         $activityScore = 1;
                     }
                     $this->attentionAggregate["activity"] = $activityScore;
                     //save score
                     $in = "";
                     $in["uRec"] = $uRec;
                     $in["ref"] = 0;
                     $in["weight"] = $activityScore;
                     $in["type"] = $att->attentionActivity;
                     $in["note"] = "C3HealthLink interaction has dropped by " . abs($delta) . "% in the past 30 days.";
                     $in["expire"] = dbDate("+7 days");
                     $sql = "INSERT INTO attentionFlags " . makeSql($in, "insert");
                     if ($rcTemp = dbQuery($sql)) {
                     } else {
                         logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                     }
                 }
             }
             //let's get the other alert scores
             $sqlArr = array();
             $sqlArr["attentionDiaryRank"] = "SELECT MAX(weight) FROM attentionFlags WHERE uRec= {$uRec} AND type = {$att->attentionDiaryRank} ";
             $sqlArr["attentionDiaryKeyword"] = "SELECT MAX(weight) FROM attentionFlags WHERE uRec= {$uRec} AND type = {$att->attentionDiaryKeyword} ";
             $sqlArr["attentionSideEffect"] = "SELECT MAX(weight) FROM attentionFlags WHERE uRec= {$uRec} AND type = {$att->attentionSideEffect} ";
             $sqlArr["attentionMedComment"] = "SELECT MAX(weight) FROM attentionFlags WHERE uRec= {$uRec} AND type = {$att->attentionMedComment} ";
             foreach ($sqlArr as $key => $value) {
                 $this->attentionAggregate[$key] = 0;
                 if ($rcTemp = dbQuery($value)) {
                     if ($rowTemp = dbFetch($rcTemp)) {
                         $t = array_pop($rowTemp);
                         if ($t) {
                             $this->attentionAggregate[$key] = $t;
                         }
                         //logit(INFO,"$key : {$this->attentionAggregate[$key]}");
                     }
                 } else {
                     logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
                 }
             }
             //logit(INFO,"ME: ".print_r($this->attentionAggregate,1));
             //calculate
             /*
             	The max score is 18 but it's very rare anyone will hit max in all six categories so we assume anything above 6 is a red alert
             */
             $attAggTotal = array_sum($this->attentionAggregate);
             if ($attAggTotal > 0 and $attAggTotal < 4) {
                 $attScore = 2;
             } elseif ($attAggTotal > 3 and $attAggTotal < 7) {
                 $attScore = 3;
             } elseif ($attAggTotal > 6) {
                 $attScore = 4;
             } elseif ($user->active) {
                 $attScore = 1;
             } else {
                 $attScore = 0;
             }
             //logit(INFO,"SCORE: $attScore\t MEAN: $attAggMean\tTOTAL: $attAggTotal\tMAX: $attMax");
             //				$sql = "UPDATE users SET attScore = $attScore WHERE uRec = '$uRec' ";
             //				if (!$rcTemp = dbQuery($sql)) logit(WARN,"Error:  in ".__FILE__." on line: ".__LINE__);
         }
         //urec loop
     } else {
         logit(WARN, " DB Error:  {$sql} in " . __FILE__ . " on line: " . __LINE__);
     }
     clearFlag("MetiAttentionRun");
     logit(INFO, "Ending Attention Score Calculator");
     return true;
 }