You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Proceso extends Model
{
use HasFactory;
protected $table = 'procesos';
protected $fillable = [
'nombre',
'descripcion',
'estado',
'duracion',
'intentos_maximos',
'requiere_pago',
'precio',
'calificacion_id',
'slug',
'tipo_simulacro',
'tipo_proceso',
'activo',
'publico',
'fecha_inicio',
'fecha_fin',
'tiempo_por_pregunta',
'cantidad_pregunta',
];
protected $casts = [
'requiere_pago' => 'boolean',
'activo' => 'boolean',
'publico' => 'boolean',
'duracion' => 'integer',
'intentos_maximos' => 'integer',
'tiempo_por_pregunta' => 'integer',
'precio' => 'decimal:2',
'fecha_inicio' => 'datetime',
'fecha_fin' => 'datetime',
];
// Examen → Calificación (opcional)
public function calificacion()
{
return $this->belongsTo(Calificacion::class);
}
/* =============================
| SCOPES
============================= */
public function scopeActivos($query)
{
return $query->where('activo', true);
}
public function scopePublicos($query)
{
return $query->where('publico', true);
}
public function scopePorProceso($query, $proceso)
{
return $query->where('tipo_proceso', $proceso);
}
/* =============================
| ACCESSORS
============================= */
public function getEstaDisponibleAttribute(): bool
{
$now = now();
if (!$this->activo) {
return false;
}
if ($this->fecha_inicio && $now->lt($this->fecha_inicio)) {
return false;
}
if ($this->fecha_fin && $now->gt($this->fecha_fin)) {
return false;
}
return true;
}
/* =============================
| EVENTS
============================= */
protected static function booted()
{
static::creating(function ($examen) {
if (empty($examen->slug)) {
$examen->slug = Str::slug($examen->nombre) . '-' . uniqid();
}
});
}
public function areas()
{
return $this->belongsToMany(
Area::class,
'area_proceso' // nombre de la tabla pivot
)
->withPivot('id') // columnas extra de la tabla pivot que quieres acceder
->withTimestamps(); // para manejar created_at y updated_at automáticamente
}
}