<?php 
session_start();
/*
 * logout.php
 * 
 * Copyright 2009, 2018 Pedro Quaresma <pedro@mat.uc.pt>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

require_once('servidor.php');

// cabecalho sem menu de opcoes
require($entradaFILE.'header.php');

?>

<center>
<h2>GeoThms - Geometry Theorems Framework</h2>
</center>


<?php


if (isset($_SESSION['username'])) { // só para o caso de já ter feito o login
  require($entradaFILE.'login4logout.php');
  
  
  // ==================================================
  // Vai preencher os dados na tabela de acessos (logs)
  //
  // Vai fazer o registo de saída
  // ==================================================

  $username=$_SESSION['username'];


  $sqlUser = "SELECT userId
                FROM users
               WHERE username='$username'";
  
  $resultadoUser = mysqli_query($ligacao,$sqlUser);
  $linhaUser = mysqli_fetch_assoc($resultadoUser);

  $vUserId = $linhaUser['userId'];
  $timeLogout=time();

  // vai ver qual é a última entrada de um dado utilizador no sistema
  // GeoThms
  $sqlTimeLogin = "SELECT MAX(timeLogin)
                     FROM logs 
                    WHERE userId=$vUserId
                      AND websystem='GeoThms'";


  $resultadoTimeLogin = mysqli_query($ligacao,$sqlTimeLogin);
  $linhaTimeLogin = mysqli_fetch_assoc($resultadoTimeLogin);

  $vTimeLogin = $linhaTimeLogin['MAX(timeLogin)'];

  $vLoginLogout = $timeLogout-$vTimeLogin;

  $sqlLogs = "UPDATE logs
                 SET timeLogout=$timeLogout,
                     loginLogout=$vLoginLogout
               WHERE userId=$vUserId
                 AND timeLogin=$vTimeLogin";
  
  $resultadoLogs = mysqli_query($ligacao,$sqlLogs);
  
  if (!$resultadoLogs) {
    echo "<p>Erro: Logs</p>";
    echo "<p>$sqlLogs</p>";
    exit;
  }
  
  // Apaga as eventuais entradas deste utilizador nas tabelas CodeTmp e
  // CodeTmpProver
  $username = $_SESSION['username'];
  
  // Apaga em CodeTmp (figuras)
  $sqlDeleteCodeTmp = "DELETE 
                         FROM codeTmp
                        WHERE username ='$username'
                          AND name = 'temporary'";
  
  $resultadoDeleteCodeTmp = mysqli_query($ligacao,$sqlDeleteCodeTmp);
  
  // Apaga em CodeTmp (figuras)
  $sqlDeleteCodeTmpProver = "DELETE 
                               FROM codeTmpProver
                              WHERE username ='$username' 
                                AND name = 'temporary'";
  
  $resultadoDeleteProverCodeTmp = mysqli_query($ligacao,$sqlDeleteCodeTmpProver);
 
 
  echo "<h2>Disconnected from the Database</h2>\n";

}

?>

<?php

require($entradaFILE."footer.php");

// Limpa as variáveis de Sessão e termina a Sessão
// remove all the variable in the session 
session_unset(); 
//destroy the session 
session_destroy();

echo "<meta http-equiv='refresh' content='2; url=index.php' />\n";

?>

