From 9a9489cd712d48b26dc71c0e7a39e460887d0e9e Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 28 Jun 2024 19:36:29 -0300 Subject: [PATCH] Ajustes na log e process_score --- controllers/login.php | 23 ++++++++++++-- controllers/process_score.php | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 controllers/process_score.php diff --git a/controllers/login.php b/controllers/login.php index 7812350..2eb12fc 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -65,15 +65,34 @@ function validateUser($login, $password) $logger->logEvent($userID, $action, $details); return ['success' => true, 'message' => 'Login bem-sucedido!', 'username' => $login]; } else { + $userID = $user['id']; // ID do usuário que logou + $action = "Login"; + $details = "Senha incorreta."; + + $logger->logEvent($userID, $action, $details); return ['success' => false, 'message' => 'Senha incorreta.']; } } else { + + $userID = null; // ID do usuário que logou + $action = "Login"; + $details = "Usuário $login não encontrado."; + + $logger->logEvent($userID, $action, $details); return ['success' => false, 'message' => "Usuário $login não encontrado."]; } } catch (PDOException $e) { // $client->trackException($e); - error_log("Database query error: " . $e->getMessage()); - return ['success' => false, 'message' => 'Erro ao acessar o banco de dados.']; + + $message = $e->getMessage(); + $userID = $user['id'] ? $user['id'] : null; // ID do usuário que logou + $action = "Login"; + $details = "Erro inesperado: $message"; + + $logger->logEvent($userID, $action, $details); + + error_log("Database query error: " . $message); + return ['success' => false, 'message' => "Erro ao consultar banco de dados."]; } } diff --git a/controllers/process_score.php b/controllers/process_score.php new file mode 100644 index 0000000..91a6a49 --- /dev/null +++ b/controllers/process_score.php @@ -0,0 +1,56 @@ +load(); +} + +$instrumentalKey = getenv('INSTRUMENTATION_KEY'); + +// Configuração do Application Insights +$client = new Telemetry_Client(); +$client->getContext()->setInstrumentationKey($instrumentalKey); + +function createDatabaseConnection() +{ + $dbHost = getenv('DB_HOST'); + $dbDatabase = getenv('DB_DATABASE'); + $dbUser = getenv('DB_USERNAME'); + $dbPassword = getenv('DB_PASSWORD'); + + + + global $client; // Usar o cliente de telemetria global + try { + $conn = new PDO("sqlsrv:server = tcp:$dbHost; Database = $dbDatabase", $dbUser, $dbPassword); + + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + return $conn; + } catch (Exception $e) { + $client->trackException($e); + error_log("Error connecting to SQL Server: " . $e->getMessage()); + die("Error connecting to SQL Server."); + // echo""; + } finally { + $client->flush(); + } +} + +function getScore(){ + +} + +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + $user_id = isset($_POST['score']) ? intval($_POST['score']) : 0; + $quiz_id = isset($_POST['level']) ? intval($_POST['level']) : 0; + $score = isset($_POST['optionId']) ? intval($_POST['optionId']) : 0; + + // echo ""; + $responseArray = getScore($level); + echo json_encode($responseArray); +} \ No newline at end of file