File "FormacionModelo.php"
Full Path: C:/wamp64/www/Formaciones/Modelos/FormacionModelo.php
File size: 5.67 KB
MIME-type: text/x-php
Charset: utf-8
<?php
// Modelos/FormacionModelo.php
class FormacionModelo {
private $db;
public function __construct($db) {
$this->db = $db;
}
public function getDB() {
return $this->db;
}
public function guardar($area, $tipo_formacion, $cedula, $responsable, $cargo, $f_ini, $f_fin, $temas, $file_data, $quien_registra) {
// LOG PARA DEBUGGING
error_log("=== MODELO GUARDAR - PARÁMETROS RECIBIDOS ===");
error_log("1. area: " . $area);
error_log("2. tipo_formacion: " . $tipo_formacion);
error_log("3. cedula: " . $cedula);
error_log("4. responsable: " . $responsable);
error_log("5. cargo: " . $cargo);
error_log("6. f_ini: " . $f_ini);
error_log("7. f_fin: " . $f_fin);
error_log("8. temas: " . $temas);
error_log("9. file_data: " . (($file_data !== null) ? strlen($file_data) . ' bytes' : 'NULL'));
error_log("10. quien_registra: " . $quien_registra);
// SQL con las 10 columnas + created_at (NOW())
$sql = "INSERT INTO formacion (
area,
tipo_formacion,
cedula_usuario,
capacitador,
cargoCap,
fecha_Inicial,
fecha_Final,
temas,
evidencia_datos,
namePro,
created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
error_log("SQL Query: " . $sql);
$stmt = $this->db->prepare($sql);
if (!$stmt) {
error_log("❌ ERROR preparando statement: " . $this->db->error);
return false;
}
$null = NULL;
// BIND: 8 strings + 1 blob + 1 string = ssssssssbs
// Posiciones: 1=area, 2=tipo_formacion, 3=cedula, 4=responsable, 5=cargo,
// 6=f_ini, 7=f_fin, 8=temas, 9=evidencia_datos(blob), 10=quien_registra
$bind_result = $stmt->bind_param("ssssssssbs",
$area, // 1 - string
$tipo_formacion, // 2 - string
$cedula, // 3 - string
$responsable, // 4 - string
$cargo, // 5 - string
$f_ini, // 6 - string (date)
$f_fin, // 7 - string (date)
$temas, // 8 - string (text)
$null, // 9 - blob (evidencia_datos) - se envía con send_long_data
$quien_registra // 10 - string (namePro)
);
if (!$bind_result) {
error_log("❌ ERROR en bind_param: " . $stmt->error);
return false;
}
// Enviar el archivo PDF si existe (índice 8 = 9º parámetro)
if ($file_data !== null) {
$resultado_long_data = $stmt->send_long_data(8, $file_data);
if ($resultado_long_data) {
error_log("✅ PDF enviado correctamente: " . strlen($file_data) . " bytes");
} else {
error_log("❌ ERROR enviando PDF");
}
}
// Ejecutar la consulta
$resultado = $stmt->execute();
if ($resultado) {
error_log("✅ INSERT ejecutado exitosamente. ID: " . $this->db->insert_id);
} else {
error_log("❌ ERROR ejecutando INSERT: " . $stmt->error);
error_log("❌ Errno: " . $stmt->errno);
}
$stmt->close();
return $resultado;
}
public function listarPaginadas($busqueda = '', $limit = 10, $offset = 0) {
$sql = "SELECT i.*, u.name as nombre_usuario
FROM formacion i
LEFT JOIN users u ON i.cedula_usuario = u.cedula
WHERE 1=1";
if (!empty($busqueda)) {
$sql .= " AND (i.area LIKE ? OR i.cedula_usuario LIKE ? OR i.capacitador LIKE ?)";
}
$sql .= " ORDER BY i.created_at DESC LIMIT ? OFFSET ?";
$stmt = $this->db->prepare($sql);
if (!empty($busqueda)) {
$term = "%$busqueda%";
$stmt->bind_param("sssii", $term, $term, $term, $limit, $offset);
} else {
$stmt->bind_param("ii", $limit, $offset);
}
$stmt->execute();
return $stmt->get_result();
}
public function contarTodas($busqueda = '') {
$sql = "SELECT COUNT(*) as total FROM formacion i WHERE 1=1";
if (!empty($busqueda)) {
$sql .= " AND (area LIKE ? OR cedula_usuario LIKE ? OR capacitador LIKE ?)";
$stmt = $this->db->prepare($sql);
$term = "%$busqueda%";
$stmt->bind_param("sss", $term, $term, $term);
} else {
$stmt = $this->db->prepare($sql);
}
$stmt->execute();
return $stmt->get_result()->fetch_assoc()['total'];
}
public function obtenerPorId($id) {
$stmt = $this->db->prepare("SELECT * FROM formacion WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
// Obtener todas las áreas para el select
public function obtenerAreas() {
$query = "SELECT area FROM areas ORDER BY area ASC";
return $this->db->query($query);
}
public function borrar($id) {
$stmt = $this->db->prepare("DELETE FROM formacion WHERE id = ?");
$stmt->bind_param("i", $id);
return $stmt->execute();
}
}