}
        }
    }
}
$result = array();
for ($i = 0; $i < count($data) - 1; $i++) {
    if (match_analysis($data[$i]['description'])) {
        if ($data[$i]['user_id'] == $data[$i + 1]['user_id']) {
            array_push($result, $data[$i]);
            array_push($result, $data[$i + 1]);
        }
    }
}
//print_r($result);
//distinct_ip_match_analysis($result);
calculate_baseline($result, $module_id, $book_id);
//////////////////////////////////////////////////////////////////////////////////////////////
function distinct_ip_match_analysis($result)
{
    $user_ip = array();
    $ip_record = array();
    for ($i = 0; $i < count($result); $i++) {
        if (!search_assoc($user_ip, $result[$i]["ip_address"])) {
            $ip_record["user_id"] = $result[$i]["user_id"];
            $ip_record["ip_address"] = $result[$i]["ip_address"];
            array_push($user_ip, $ip_record);
        }
    }
    print_r($user_ip);
}
function calculate_baseline($result, $module_id, $book_id)
function calculate_time_AAV($AAV_ID)
{
    global $module_id;
    global $start_date;
    global $end_date;
    global $db_server;
    global $db_username;
    global $db_pass;
    global $db_name;
    global $book_id;
    $email_table = "";
    $email_column = "";
    if ($book_id == "VT") {
        $email_table = "CS3114VTRoster";
        $email_column = "user_email";
    } else {
        if ($book_id == "CNP") {
            $email_table = "CNPRoster";
            $email_column = "email";
        }
    }
    $query = "SELECT * \r\n                  FROM  `opendsa_userbutton` A INNER JOIN `auth_user` B ON\r\n                  A.user_id = B.id \r\n                  WHERE email IN \r\n                 (Select {$email_column} from {$email_table}) \r\n                  AND module_id = {$module_id}\r\n                  AND action_time BETWEEN '{$start_date}' AND '{$end_date}'\r\n                  ORDER BY email, action_time, A.id";
    $data = array();
    if (@mysql_connect($db_server, $db_username, $db_pass)) {
        if (@mysql_select_db($db_name)) {
            if ($query_run = mysql_query($query)) {
                while ($query_result = mysql_fetch_assoc($query_run)) {
                    array_push($data, $query_result);
                }
            }
        }
    }
    $result = array();
    $first_record = null;
    for ($i = 0; $i < count($data); $i++) {
        if ($data[$i]['exercise_id'] == $AAV_ID && !isset($first_record)) {
            $first_record = $data[$i];
            $start_session = true;
        }
        //The first Interaction is captured
        if (isset($first_record)) {
            if ($data[$i]["email"] != $first_record["email"]) {
                //Return back one record and add it
                array_push($result, $first_record, $data[$i - 1]);
                //Check if the new user's Interaction is AAV
                if ($data[$i]["exercise_id"] == $AAV_ID) {
                    $first_record = $data[$i];
                } else {
                    $first_record = null;
                }
            } else {
                if ($data[$i]["email"] == $first_record["email"] && $data[$i]["exercise_id"] != $first_record["exercise_id"]) {
                    //Return back one record
                    array_push($result, $first_record, $data[$i]);
                    $first_record = null;
                }
            }
            //If the last intearction is an AAV interction
            if ($i == count($data) - 1) {
                array_push($result, $first_record, $data[$i]);
            }
        }
    }
    //print_r($result);
    calculate_baseline($result, $AAV_ID);
}