<?php
    require_once("../../php/db_utils.php");
    
    try {
        // Connect to the database
        $database = db_connect();
        
        // Incoming data
        $canvasId = $_POST["canvas_id"];
        $username = $_POST["username"];
        
        // Update last active of the current user
        $query = "UPDATE collaborators SET lastactive = NOW() WHERE canvas_id = '" . $canvasId . "' AND owner = '" . $username . "' AND collaborator = '" . $username . "';";
        
        // Run query
        db_query_no_return($database, $query);
        
        /*
        // Get active users
        $query = "SELECT name, username FROM user WHERE NOT username = ' . $username . ' AND lastactive BETWEEN DATE_SUB(NOW(), INTERVAL 7 SECOND) AND NOW() ORDER BY name ASC;";
         */
            
        // Get collaborators
        /*
        $query = "SELECT user.name, collaborators.collaborator, collaborators.lastactive
                  FROM collaborators
                  INNER JOIN user ON collaborators.collaborator = user.username
                  WHERE canvas_id = '" . $canvasId . "'
                  AND owner = '" . $username . "'
                  AND NOT collaborator = '" . $username . "'
                  ORDER BY name ASC;";
         */
        $query = "SELECT user.name, collaborators.collaborator, collaborators.lastactive
                  FROM collaborators
                  INNER JOIN user ON collaborators.collaborator = user.username
                  WHERE canvas_id = '" . $canvasId . "'
                  AND owner = '" . $username . "'
                  AND NOT collaborator = '" . $username . "'
                  ORDER BY name ASC;";
        // /* AND lastactive BETWEEN DATE_SUB(NOW(), INTERVAL 7 SECOND) AND NOW() */
        
        // Run query
        $recordSet = db_query_return($database, $query);
        
        // Get list of users
        $activeCollaborators = Array();
        
        $index = 0;
    
        while($record = $recordSet->fetch_assoc()) {
            $activeCollaborators[$index]["name"] = $record["name"];
            $activeCollaborators[$index]["collaborator"] = $record["collaborator"];
            
            // $activeCollaborators[$index]["active"] = date("Y-m-d H:i:s", strtotime("-3607 second"));
            
            // If the user was last active within now - 7 seconds ago
            if($record["lastactive"] > date("Y-m-d H:i:s", strtotime("-3607 second"))) {          /* Add 3600 (which is an hour) if time zone is an issue */
                $activeCollaborators[$index]["active"] = 1;
            }
            else {
                $activeCollaborators[$index]["active"] = 0;
            }
            
            $index++;
        }
        
        mysqli_free_result($recordSet);
        
        echo(json_encode($activeCollaborators));
        
        // Close the connection to the database
        db_close($database);
    }
    catch(Exception $e) {
        $errorMsg = $e->getMessage();
        print_r($errorMsg);
    }
?>