periodos.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?
  2. $ruta = "../../";
  3. require_once "$ruta/include/bd_pdo.php";
  4. header('Content-Type: application/json');
  5. // json data from service\periodos.v1.php (input)
  6. $data = json_decode(file_get_contents('php://input'), true);
  7. // check if the input is empty
  8. if (is_response_empty($data)) {
  9. echo json_encode([
  10. 'status' => 'error',
  11. 'message' => 'No se recibieron datos',
  12. ]);
  13. exit;
  14. }
  15. /*
  16. {
  17. "IdNivel": 1,
  18. "IdPeriodo": 635,
  19. "NombreNivel": "LICENCIATURA",
  20. "NombrePeriodo": "241",
  21. "in_db": false
  22. inicio,
  23. fin
  24. }
  25. */
  26. // insert into database
  27. setlocale(LC_TIME, 'es_MX.UTF-8');
  28. $formatter = new IntlDateFormatter('es_MX', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Mexico_City', IntlDateFormatter::GREGORIAN, 'MMMM');
  29. $inicio = strtotime($data['inicio']);
  30. $fin = strtotime($data['fin']);
  31. try {
  32. $result = $db->insert('periodo', [
  33. 'id_reference' => $data['IdPeriodo'],
  34. 'periodo_nombre' => "{$data['NombreNivel']}: {$formatter->format($inicio)} - {$formatter->format($fin)} " . date('Y', $inicio),
  35. 'nivel_id' => $data['IdNivel'],
  36. 'periodo_fecha_inicio' => $data['inicio'],
  37. 'periodo_fecha_fin' => $data['fin'],
  38. 'estado_id' => 4,
  39. 'periodo_clave' => $data['NombrePeriodo']
  40. ], ['id_reference']);
  41. } catch (PDOException $th) {
  42. echo json_encode([
  43. 'success' => false,
  44. 'message' => $th->getMessage()
  45. ]);
  46. exit;
  47. }
  48. echo json_encode($result ? [
  49. 'success' => true,
  50. 'message' => 'Periodo agregado correctamente'
  51. ] : [
  52. 'success' => false,
  53. 'message' => 'Error al agregar el periodo'
  54. ]);