This commit is contained in:
2023-09-18 19:08:35 +00:00
parent d6bc6dde37
commit 76fe06b0df
28 changed files with 1259 additions and 1064 deletions

147
class/c_abstract_data.php Normal file
View File

@@ -0,0 +1,147 @@
<?
$ruta = '../';
require "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
trait DatabaseModel
{
public function __construct(protected string $tableName, protected array $columns = [])
{
}
public function get(array $params = [], string $what = '*')
{
global $db;
$conditions = [];
foreach ($params as $key => $value) {
$conditions[] = "$key = :$key";
}
$sql = "SELECT $what FROM $this->tableName";
if ($conditions) {
$sql .= " WHERE " . implode(" AND ", $conditions);
}
$result = $db->query($sql, $params);
return $result;
}
protected function insert__(array $params = [], ?string $where = null)
{
global $db;
if ($where === null) {
$where = $this->tableName;
}
$columns = [];
foreach ($params as $key => $value) {
$columns[] = "$key = :$key";
}
$sql = "INSERT INTO $where SET " . implode(", ", $columns);
$result = $db->query($sql, $params);
return $result;
}
}
abstract class WebServiceSGU
{
const BASE_URL = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial";
private static array $keys = [
'username' => 'SGU_APSA_AUD_ASIST',
'password' => 'B4qa594JFPr2ufHrZdHS8A==',
];
private static ?JsonSchema\Validator $validator = null;
private string $baseUrl;
public function __construct(protected string $endpoint, protected ?string $schema = null)
{
$this->baseUrl = self::BASE_URL . $endpoint;
}
private static function initCurl(array $options = [])
{
$ch = curl_init();
curl_setopt_array($ch, $options);
return $ch;
}
private static function get_token(): string
{
$curl = self::initCurl([
CURLOPT_URL => self::BASE_URL . "/inicioSesion/seleccionar",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode(self::$keys),
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
throw new Exception("cURL Error: $err");
return trim($response, '"'); // remove quotes
}
protected function validate_schema($data): bool
{
if ($this->schema === null)
return true;
self::getValidator()->validate($data, (object) json_decode($this->schema));
return self::getValidator()->isValid();
}
public static function getValidator(): JsonSchema\Validator
{
return self::$validator ??= new JsonSchema\Validator();
}
public function get_errors(): array
{
return self::getValidator()->getErrors();
}
public function get(array $data = []): array
{
if (!$this->validate_schema($data)) {
throw new Exception('Invalid schema');
}
$ch = self::initCurl([
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_URL => $this->baseUrl,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'username: ' . self::$keys['username'],
'token: ' . self::get_token(),
],
CURLOPT_RETURNTRANSFER => 1,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('cURL Error: ' . curl_error($ch));
}
curl_close($ch);
$response = json_decode($response, true);
if ($response === null) {
throw new Exception('Invalid response');
}
return $response;
}
}

119
class/database.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
header('Content-Type: application/json charset=utf-8');
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_abstract_data.php";
final class Periodo_v1 extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/periodos/v1/seleccionar");
}
}
final class Periodo_v2 extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/periodos/v2/seleccionar");
}
}
final class Periodos extends WebServiceSGU
{
// use DatabaseModel;
private readonly Periodo_v1 $periodo_v1;
private readonly Periodo_v2 $periodo_v2;
public function __construct()
{
parent::__construct("/catalogos/periodos/seleccionar");
$this->periodo_v1 = new Periodo_v1();
$this->periodo_v2 = new Periodo_v2();
}
public function get_v1(): array
{
return $this->periodo_v1->get();
}
public function get_v2(): array
{
return $this->periodo_v2->get();
}
public function get_merged(): array
{
$v2Data = $this->get_v2();
// Create an associative array with IdPeriodo as the key for faster lookup
$v2Lookup = array_column($v2Data, null, 'IdPeriodo');
return array_map(function ($itemV1) use ($v2Lookup) {
if (isset($v2Lookup[$itemV1['IdPeriodo']])) {
$itemV2 = $v2Lookup[$itemV1['IdPeriodo']];
$mergedItem = array_merge($itemV1, $itemV2);
unset($mergedItem['NombreCarrera']); // Remove NombreCarrera as specified
return $mergedItem;
}
return $itemV1; // If no matching IdPeriodo found in v2, return the original v1 item
}, $this->get_v1());
}
}
final class Espacios extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/espacios/seleccionar");
}
}
final class Carreras extends WebServiceSGU
{
public function __construct()
{
parent::__construct("/catalogos/carreras/seleccionar");
}
}
final class Horarios extends WebServiceSGU
{
public function __construct()
{
parent::__construct(
"/seleccionar",
<<<JSON
{
"\$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["idPeriodo"],
"properties": {
"idPeriodo": {
"type": "integer",
"description": "Identificador del periodo a consultar."
},
"claveFacultad": {
"type": "string",
"description": "Clave de la facultad a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"claveCarrera": {
"type": "string",
"description": "Clave de la carrera a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"claveProfesor": {
"type": "string",
"description": "Clave del empleado a consultar.",
"pattern": "^[a-zA-Z0-9]*$"
},
"fecha": {
"type": "string",
"description": "Fecha de la clase.",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
}
}
}
JSON
);
}
}

View File

@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"required": [
"IdNivel",
"IdPeriodo",
"NombreNivel",
"NombrePeriodo"
],
"properties": {
"IdNivel": {
"type": "integer"
},
"IdPeriodo": {
"type": "integer"
},
"NombreNivel": {
"type": "string",
"enum": [
"LICENCIATURA",
"ESPECIALIDAD",
"MAESTRÍA",
"DOCTORADO",
"COORDINACIÓN DE EDUCACIÓN FÍSICA Y DEPORTES",
"COORDINACIÓN DE IMPULSO Y VIDA ESTUDIANTIL",
"COORDINACIÓN DE FORMACIÓN CULTURAL",
"VICERRECTORÍA DE BIENESTAR Y FORMACIÓN",
"CENTRO DE IDIOMAS"
]
},
"NombrePeriodo": {
"type": "string"
}
}
}
}