Skip to content

Commit

Permalink
Ajustes na log e process_score
Browse files Browse the repository at this point in the history
  • Loading branch information
ramosbrp committed Jun 28, 2024
1 parent acbb2b4 commit 9a9489c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
23 changes: 21 additions & 2 deletions controllers/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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."];
}
}

Expand Down
56 changes: 56 additions & 0 deletions controllers/process_score.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use ApplicationInsights\Telemetry_Client;

require_once '../vendor/autoload.php';

// Apenas carregar dotenv em ambientes que não são de produção
if (getenv('APP_ENV') === 'development') {
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->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"<script>console.log()</script>";
} 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 "<script>console.log('Level: ' $level)</script>";
$responseArray = getScore($level);
echo json_encode($responseArray);
}

0 comments on commit 9a9489c

Please sign in to comment.