2020-05-18 15:42:14 +02:00
|
|
|
<?php
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function connectDatabase(string $user, string $pass, string $db) {
|
2020-05-18 15:42:14 +02:00
|
|
|
$unix_socket = '.mysql/mysql.sock';
|
|
|
|
$charset = 'utf8mb4';
|
|
|
|
$dsn = "mysql:unix_socket=$unix_socket;dbname=$db;charset=$charset";
|
2020-06-14 03:12:32 +02:00
|
|
|
$pdo = new PDO($dsn, $user, $pass);
|
|
|
|
return $pdo;
|
2020-05-18 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 03:12:32 +02:00
|
|
|
function closeDatabase($pdo) {
|
|
|
|
$pdo = null;
|
2020-05-18 15:42:14 +02:00
|
|
|
}
|
2020-06-14 03:12:32 +02:00
|
|
|
|
|
|
|
function displayResults($query) {
|
2020-06-16 22:50:35 +02:00
|
|
|
foreach($query->fetch(PDO::FETCH_ASSOC) as $row){
|
2020-06-14 03:12:32 +02:00
|
|
|
echo $row . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listRows($pdo, $table) {
|
|
|
|
$query = "SELECT * FROM ?";
|
|
|
|
$result = $pdo->prepare($query)->execute([$table]);
|
|
|
|
displayResults($query);
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function findRows($pdo, string $table, string $attr, string $param) {
|
2020-06-14 03:12:32 +02:00
|
|
|
$query = "SELECT * FROM ? WHERE ? = ?";
|
|
|
|
$result = $pdo->prepare($query)->execute([$table, $attr, $param]);
|
|
|
|
displayResults($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPatient($pdo, $data) {
|
2020-06-16 18:56:35 +02:00
|
|
|
$query = "INSERT INTO paciente
|
|
|
|
(nombre, apellido, fecha_de_nacimiento, documento_identificativo, tipo_documento, direccion, localidad, provincia, pais)
|
|
|
|
VALUES (?,?,?,?,?,?,?,?,?)";
|
|
|
|
$pdo->prepare($query)->execute([$data["nombre"], $data["apellido"], $data["fecha_de_nacimiento"],
|
|
|
|
$data["documento_identificativo"], $data["tipo_documento"], $data["direccion"],
|
|
|
|
$data["localidad"], $data["provincia"], $data["pais"]]);
|
2020-06-14 03:12:32 +02:00
|
|
|
return "Paciente creado con éxito";
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function editPatient($pdo, string $attr, string $param, string $id) {
|
2020-06-14 03:12:32 +02:00
|
|
|
$query = "UPDATE paciente SET ? = ? WHERE documento_identificativo = ?";
|
|
|
|
$pdo->prepare($query)->execute([$attr, $param, $id]);
|
|
|
|
return "Paciente modificado con éxito";
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function deletePatient($pdo, string $id) {
|
2020-06-14 03:12:32 +02:00
|
|
|
$check = "SELECT * FROM informes where paciente = ?";
|
|
|
|
$result = $pdo->prepare($check)->execute([$id]);
|
|
|
|
if($result->columnCount() == 0){
|
|
|
|
$statement = "DELETE FROM paciente where document_identificado = ?";
|
|
|
|
$pdo->prepare($statement)->execute([$id]);
|
|
|
|
return "El paciente se ha eliminado correctamente";
|
|
|
|
}
|
|
|
|
return "No se puede eliminar el paciente dado que tiene citas";
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHoliday($pdo, $data) {
|
2020-06-16 18:56:35 +02:00
|
|
|
$query = "INSERT INTO festivo (fecha_festivo, tipo_festivo, medico) VALUES (?,?,?)";
|
2020-06-14 03:12:32 +02:00
|
|
|
$pdo->prepare($query)->execute([$data["fecha_festivo"], $data["tipo_festivo"], $data["medico"]]);
|
|
|
|
return "Festivo creado con éxito";
|
|
|
|
}
|
|
|
|
|
|
|
|
function editHoliday($pdo, $attr, $param, $id) {
|
|
|
|
$query = "UPDATE festivo SET ? = ? WHERE id = ?";
|
|
|
|
$pdo->prepare($query)->execute([$attr, $param, $id]);
|
|
|
|
return "Festivo modificado con éxito";
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteHoliday($pdo, str $id) {
|
|
|
|
$statement = "DELETE FROM festivo where id = ?";
|
|
|
|
$pdo->prepare($statement)->execute([$id]);
|
|
|
|
return "Festivo borrado con éxito";
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUser($pdo, $data) {
|
2020-06-16 18:56:35 +02:00
|
|
|
$query = "INSERT INTO usuario (nombre, usuario, contraseña, rol, correo) VALUES (?,?,?,?,?)";
|
2020-06-14 04:25:11 +02:00
|
|
|
$pdo->prepare($query)->execute([$data["nombre"], $data["usuario"], $data["contraseña"], $data["rol"], $data["correo"]]);
|
2020-06-14 03:12:32 +02:00
|
|
|
return "Usuario creado con éxito";
|
|
|
|
}
|
|
|
|
|
|
|
|
function editUser($pdo, $attr, $param, $id) {
|
|
|
|
$query = "UPDATE usuario SET ? = ? WHERE usuario = ?";
|
|
|
|
$pdo->prepare($query)->execute([$attr, $param, $id]);
|
|
|
|
return "Usuario modificado con éxito";
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function deactivateUser($pdo, string $id) {
|
2020-06-14 03:12:32 +02:00
|
|
|
editUser($pdo, "fecha_baja", "CURRENT_TIMESTAMP", $id);
|
|
|
|
return "Usuario desactivado con éxito";
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:56:35 +02:00
|
|
|
function listAppointments($pdo, string $doctor) {
|
2020-06-14 03:12:32 +02:00
|
|
|
findRows($pdo, "cita", "medico", $doctor);
|
|
|
|
}
|
|
|
|
|
2020-06-16 22:50:35 +02:00
|
|
|
|
|
|
|
function displayUsers($query) {
|
|
|
|
foreach($query->fetch(PDO::FETCH_ASSOC) as $row){
|
|
|
|
echo $row["nombre"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listUsers($pdo) {
|
|
|
|
$query = "SELECT usuario.nombre, usuario.usuario, rol.nombre, usuario.correo FROM usuario
|
|
|
|
INNER JOIN rol ON usuario.rol = rol.codigo";
|
|
|
|
$result = $pdo->query($query)->fetchAll();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-06-14 03:12:32 +02:00
|
|
|
?>
|