update-collaborators.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. require_once("../../php/db_utils.php");
  3. try {
  4. // Connect to the database
  5. $database = db_connect();
  6. // Incoming data
  7. $canvasId = $_POST["canvas_id"];
  8. $username = $_POST["username"];
  9. // Update last active of the current user
  10. $query = 'UPDATE collaborators SET last_active = NOW() WHERE canvas_id = "' . $canvasId . '" AND collaborator = "' . $username . '";';
  11. // Run query
  12. db_query_no_return($database, $query);
  13. // Get collaborators
  14. $query = 'SELECT user.name, collaborators.collaborator, collaborators.last_active
  15. FROM collaborators
  16. INNER JOIN user ON collaborators.collaborator = user.username
  17. WHERE canvas_id = "' . $canvasId . '"
  18. ORDER BY name ASC;';
  19. // Run query
  20. $recordSet = db_query_return($database, $query);
  21. // Get list of users
  22. $activeCollaborators = Array();
  23. // Declare index
  24. $index = 0;
  25. // For every collaborator
  26. while($record = $recordSet->fetch_assoc()) {
  27. // Declare the collaborator
  28. $activeCollaborators[$index]["name"] = $record["name"];
  29. $activeCollaborators[$index]["collaborator"] = $record["collaborator"];
  30. // If the user was last active within now - 7 seconds ago
  31. if($record["last_active"] > date("Y-m-d H:i:s", strtotime("-3607 second"))) {
  32. // The collaborator is active
  33. $activeCollaborators[$index]["active"] = 1;
  34. }
  35. else {
  36. // The collaborator is not active
  37. $activeCollaborators[$index]["active"] = 0;
  38. }
  39. $index++;
  40. }
  41. mysqli_free_result($recordSet);
  42. echo(json_encode($activeCollaborators));
  43. // Close the connection to the database
  44. db_close($database);
  45. }
  46. catch(Exception $e) {
  47. $errorMsg = $e->getMessage();
  48. print_r($errorMsg);
  49. }
  50. ?>