File "MaterialModelo.php"

Full Path: C:/wamp64/www/Formaciones/Modelos/MaterialModelo.php
File size: 7.23 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Modelos/MaterialModelo.php

class MaterialModelo {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function listarTodos() {
        $sql = "SELECT m.id, m.course_id, m.title, m.type, m.url, m.file_path,
                       m.firmaPro, m.namePro, m.created_at, m.cargoPro,
                       c.title as curso_nombre 
                FROM materials m 
                LEFT JOIN courses c ON m.course_id = c.id 
                ORDER BY m.id DESC";
        return $this->db->query($sql);
    }

    public function subir($curso_id, $titulo, $tipo, $namePro, $firmaPro, $ruta_o_url = null) {
        error_log("[v0] Modelo.subir - ParĂ¡metros recibidos:");
        error_log("[v0] - curso_id: $curso_id");
        error_log("[v0] - titulo: $titulo");
        error_log("[v0] - tipo: $tipo");
        error_log("[v0] - namePro: $namePro");
        error_log("[v0] - ruta_o_url: " . ($ruta_o_url ?? 'NULL'));
        
        if ($tipo === 'file') {
            // Guardar ruta del archivo en file_path
            $sql = "INSERT INTO materials (course_id, title, type, file_path, namePro, firmaPro, created_at) 
                    VALUES (?, ?, ?, ?, ?, ?, NOW())";
            
            error_log("[v0] SQL para tipo file: $sql");
            
            $stmt = $this->db->prepare($sql);
            
            if (!$stmt) {
                error_log("[v0] Error en prepare: " . $this->db->error);
                return false;
            }
            
            $stmt->bind_param("isssss", $curso_id, $titulo, $tipo, $ruta_o_url, $namePro, $firmaPro);
            
            error_log("[v0] Ejecutando INSERT con file_path: " . $ruta_o_url);
            
            $resultado = $stmt->execute();
            
            if (!$resultado) {
                error_log("[v0] Error en execute: " . $stmt->error);
            } else {
                error_log("[v0] INSERT exitoso - ID insertado: " . $this->db->insert_id);
            }
            
            $stmt->close();
            return $resultado;
            
        } elseif ($tipo === 'url') {
            // Guardar URL en la columna url
            $sql = "INSERT INTO materials (course_id, title, type, url, namePro, firmaPro, created_at) 
                    VALUES (?, ?, ?, ?, ?, ?, NOW())";
            
            error_log("[v0] SQL para tipo url: $sql");
            
            $stmt = $this->db->prepare($sql);
            
            if (!$stmt) {
                error_log("[v0] Error en prepare: " . $this->db->error);
                return false;
            }
            
            $stmt->bind_param("isssss", $curso_id, $titulo, $tipo, $ruta_o_url, $namePro, $firmaPro);
            
            error_log("[v0] Ejecutando INSERT con url: " . $ruta_o_url);
            
            $resultado = $stmt->execute();
            
            if (!$resultado) {
                error_log("[v0] Error en execute: " . $stmt->error);
            } else {
                error_log("[v0] INSERT exitoso - ID insertado: " . $this->db->insert_id);
            }
            
            $stmt->close();
            return $resultado;
        }
        
        return false;
    }

    public function obtenerPorId($id) {
        $stmt = $this->db->prepare("SELECT m.*, c.title as curso_nombre 
                                     FROM materials m 
                                     LEFT JOIN courses c ON m.course_id = c.id 
                                     WHERE m.id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $resultado = $stmt->get_result();
        $material = $resultado->fetch_assoc();
        $stmt->close();
        return $material;
    }

    public function eliminar($id) {
        $stmt = $this->db->prepare("DELETE FROM materials WHERE id = ?");
        $stmt->bind_param("i", $id);
        $resultado = $stmt->execute();
        $stmt->close();
        return $resultado;
    }

    public function actualizar($id, $curso_id, $titulo, $tipo, $namePro, $firmaPro, $ruta_o_url = null) {
        if ($tipo === 'file' && $ruta_o_url !== null) {
            $sql = "UPDATE materials 
                    SET course_id = ?, title = ?, type = ?, file_path = ?, 
                        namePro = ?, firmaPro = ? 
                    WHERE id = ?";
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("isssssi", $curso_id, $titulo, $tipo, $ruta_o_url, $namePro, $firmaPro, $id);
            
        } else {
            $sql = "UPDATE materials 
                    SET course_id = ?, title = ?, type = ?, url = ?, namePro = ?, firmaPro = ? 
                    WHERE id = ?";
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("isssssi", $curso_id, $titulo, $tipo, $ruta_o_url, $namePro, $firmaPro, $id);
        }
        
        $resultado = $stmt->execute();
        $stmt->close();
        return $resultado;
    }

    public function obtenerPorCurso($curso_id) {
        $stmt = $this->db->prepare("SELECT id, course_id, title, type, url, file_path,
                                     firmaPro, namePro, created_at, cargoPro 
                                     FROM materials WHERE course_id = ? ORDER BY id DESC");
        $stmt->bind_param("i", $curso_id);
        $stmt->execute();
        $resultado = $stmt->get_result();
        $materiales = [];
        while ($row = $resultado->fetch_assoc()) {
            $materiales[] = $row;
        }
        $stmt->close();
        return $materiales;
    }
    
    public function contarTodos($busqueda = '') {
        $sql = "SELECT COUNT(*) as total 
                FROM materials m 
                LEFT JOIN courses c ON m.course_id = c.id 
                WHERE 1=1";
        
        if (!empty($busqueda)) {
            $sql .= " AND (m.title LIKE ? OR c.title LIKE ? OR m.namePro 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 listarPaginados($busqueda = '', $limit = 10, $offset = 0) {
        $sql = "SELECT m.id, m.course_id, m.title, m.type, m.url, m.file_path,
                       m.firmaPro, m.namePro, m.created_at, m.cargoPro,
                       c.title as curso_nombre
                FROM materials m 
                LEFT JOIN courses c ON m.course_id = c.id 
                WHERE 1=1";
        
        if (!empty($busqueda)) {
            $sql .= " AND (m.title LIKE ? OR c.title LIKE ? OR m.namePro LIKE ?)";
        }

        $sql .= " ORDER BY m.id 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();
    }
}