57 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Models;
 | |
| 
 | |
| use Illuminate\Database\Eloquent\Factories\HasFactory;
 | |
| use Illuminate\Database\Eloquent\Model;
 | |
| 
 | |
| class Evento extends Model
 | |
| {
 | |
|     use HasFactory;
 | |
| 
 | |
|     public function dias() {
 | |
|         return $this->hasMany(Dia::class);
 | |
|     }
 | |
| 
 | |
|     // devuelve los tres mejores marzullos de los días que tiene asociados
 | |
|     public function coordinar(): array {
 | |
|         $marzullos = [];
 | |
|         //devuelve array de
 | |
|             // [dia::string, cantidad::integer, inicio::integer, fin::integer, duracion::integer
 | |
|                 // inicioLindo::string, finLindo::string, duracionLinda::string]
 | |
|         foreach ($this->dias as $dia) {
 | |
|             [$a, $b, $c, $d] = $dia->marzullo();
 | |
|             $marzullos[] = ["dia" => $dia->nombre, "cantidad" => $a,
 | |
|                 "inicio" => $b, "fin" => $c, "duracion" => $d,
 | |
|                 "inicioLindo" => Utils::parseHora($b), "finLindo" => Utils::parseHora($c),
 | |
|                 "duracionLinda" => Utils::parseHora($d)];
 | |
|         }
 | |
|         usort($marzullos, array("Evento", "cmp_cant_marzullo"));
 | |
|         return array_slice($marzullos, 0, 3);
 | |
|     }
 | |
| 
 | |
|     /*                         ---                            */
 | |
| 
 | |
|     //compara marzullos
 | |
|     // $a,$b::[nombre::string, cantidad::integer, horario::string, duracion::integer]
 | |
| 
 | |
|     // por cantidad
 | |
|     function cmp_cant_marzullo($a, $b): int {
 | |
|         $cant_a = $a["cantidad"];
 | |
|         $cant_b = $b["cantidad"];
 | |
|         if ($cant_a == $cant_b) {
 | |
|             return $this->cmp_dur_marzullo($a, $b);
 | |
|         }
 | |
|         return ($cant_a > $cant_b) ? -1 : 1;
 | |
|     }
 | |
| 
 | |
|     // por duracion
 | |
|     private function cmp_dur_marzullo($a, $b): int {
 | |
|         $dur_a = $a["duracion"];
 | |
|         $dur_b = $b["duracion"];
 | |
|         if ($dur_a == $dur_b) {
 | |
|             return 0;
 | |
|         }
 | |
|         return ($dur_a > $dur_b) ? -1 : 1;
 | |
|     }
 | |
| }
 |