'unauthorized']); exit; } $user = unserialize($_SESSION['user']); try { if ($_SERVER['REQUEST_METHOD'] === 'PUT') { // check parameters $raw = file_get_contents('php://input'); $post_data = json_decode($raw, true); // if it's a list // step 1: get subrutas if (empty($post_data)) { http_response_code(400); echo json_encode(['error' => 'No hay clases pendientes']); exit; } if (!(isset($post_data['fecha'], $post_data['bloques'], $post_data['justificacion']))) { http_response_code(400); echo json_encode(['error' => 'Faltan parametros']); exit; } $bloques = $db ->where('id', $post_data['bloques']) ->orderBy('hora_inicio') ->get('bloque_horario', null, 'hora_inicio, hora_fin'); $min_hora_inicio = $bloques[0]['hora_inicio']; $max_hora_fin = $bloques[count($bloques) - 1]['hora_fin']; $pdo->beginTransaction(); $data = $db->query( "INSERT INTO registro (horario_id, registro_fecha_ideal, profesor_id, justificador_id, justificacion, registro_fecha_justificacion, registro_justificada) SELECT DISTINCT horario_id, :fecha::DATE, profesor_id, :justificador_id::INT, :justificacion, NOW(), true from horario_view join horario_profesor using (horario_id) where (:hora_inicio::TIME, :hora_fin::TIME) OVERLAPS (horario_hora, horario_fin) AND horario_dia = EXTRACT(DOW FROM :fecha::DATE) AND periodo_id = :periodo_id AND (horario_view.facultad_id = :facultad_id OR :facultad_id IS NULL) ON CONFLICT (horario_id, registro_fecha_ideal, profesor_id) DO UPDATE SET justificador_id = :justificador_id, justificacion = :justificacion, registro_fecha_justificacion = NOW(), registro_justificada = true RETURNING *;", array( 'justificador_id' => $user->user['id'], 'justificacion' => empty($post_data['justificacion']) ? null : $post_data['justificacion'], 'fecha' => $post_data['fecha'], 'periodo_id' => $user->periodo_id, 'facultad_id' => $user->facultad['facultad_id'], 'hora_inicio' => $min_hora_inicio, 'hora_fin' => $max_hora_fin, ) ); $pdo->commit(); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } else { http_response_code(405); echo json_encode(['error' => 'method not allowed']); exit; } } catch (PDOException $th) { http_response_code(500); echo json_encode([ 'error' => $th->getMessage(), 'query' => $db->getLastQuery(), 'post_data' => $post_data, ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $pdo->rollBack(); exit; } catch (Exception $th) { http_response_code(500); echo json_encode([ 'error' => $th->getMessage(), ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); exit; }