| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 | 
							- <?php
 
- /*
 
- idPeriodo: identificador del periodo a consultar (obligatorio, número entero)
 
- claveFacultad: clave de la facultad a consultar (opcional, cadena)
 
- claveCarrera: clave de la carrera a consultar (opcional, cadena)
 
- claveProfesor: clave del empleado a consultar (opcional, cadena)
 
- fecha: fecha de la clase (opcional, cadena en formato yyyy-MM-dd)
 
-  */
 
- ini_set('display_errors', 1);
 
- ini_set('display_startup_errors', 1);
 
- ini_set('post_max_size', 1);
 
- ini_set('max_execution_time', 8*60);
 
- error_reporting(E_ALL);
 
- date_default_timezone_set('America/Mexico_City');
 
- $ruta = "../";
 
- $ruta_superior = dirname(__DIR__);
 
- require_once $ruta_superior."/include/bd_pdo.php";
 
- require_once __DIR__."/token.php";
 
- require_once __DIR__."/LogCambios.php";
 
- //--------------ACTUALIZA HORARIOS--------------------------
 
- if(!empty($_GET["fecha"])){
 
-     $hoy = $_GET["fecha"];
 
- }else{
 
-     $hoy = date("Y-m-d");
 
- }
 
- $dia_hoy = date("w", strtotime($hoy));
 
- function compareByHash($a, $b) {
 
-     return strcmp($a['hash'], $b['hash']);
 
- }
 
- $debug = false;
 
- if(isset($_GET["debug"])){
 
-     $debug = true;
 
-     print_r( $db->querySingle('select now()'));
 
- }
 
- $periodo_sgu_old = 0;
 
- $log_desc = "";
 
- //------------------ACTUALIZA SALONES----------------------
 
- $pag = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial/catalogos/espacios/seleccionar";
 
- $curl = curl_init();
 
- curl_setopt_array($curl, [
 
-     CURLOPT_URL => $pag,
 
-     CURLOPT_RETURNTRANSFER => true,
 
-     //CURLOPT_ENCODING => "",
 
-     //CURLOPT_MAXREDIRS => 10,
 
-     // CURLOPT_TIMEOUT => 0,
 
-     //CURLOPT_CUSTOMREQUEST => "POST",
 
-     CURLOPT_POSTFIELDS => json_encode([]),
 
-     CURLOPT_HTTPHEADER => [
 
-         "token: ".$token,
 
-         "username: SGU_APSA_AUD_ASIST",
 
-         "Content-Type: application/json",
 
-         'Transfer-Encoding: chunked'
 
-     ],
 
- ]);
 
- $response = curl_exec($curl);
 
- $err = curl_error($curl);
 
- curl_close($curl);
 
- if ($err)
 
-     die("cURL Error #:$err");
 
- $salonesData = json_decode($response, true);
 
- //$salonesTotal_rs = $db->count('salon');
 
- $salonesTotal_rs = $db->where('salon_id', 0, '>')->count('salon');
 
- echo "$salonesTotal_rs tiene " . count($salonesData) . " salones<br>";
 
- if($salonesTotal_rs < count($salonesData)){//faltan salones en BD
 
-     $salones_rs = $db->query('SELECT id_espacio_sgu FROM salon');
 
-     //claves de espacios
 
-     $arreglo_espacios = array_map(function ($item) {
 
-         return $item['id_espacio_sgu'];
 
-     }, $salones_rs);
 
-     foreach($salonesData as $data){
 
-         if( !in_array($data["IdEspacio"], $arreglo_espacios)){
 
-             //Insertar espacio
 
-             if($debug){
 
-                 echo "Espacio nuevo: ".$data["NombreEspacio"]."<br>";
 
-             }else{
 
-                 $db->query('INSERT INTO SALON (salon, id_espacio_sgu, id_espacio_padre) VALUES (:salon, :id, :id_padre)',
 
-                     [":salon"=>$data["NombreEspacio"], ":id"=>$data["IdEspacio"], ":id_padre"=>$data["IdEspacioPadre"]]);
 
-             }
 
-         }
 
-     }
 
- }
 
- // -----------------------------
 
- $pag = "https://portal.ulsa.edu.mx/servicios/AuditoriaAsistencialRest/AuditoriaAsistencialService.svc/auditoriaAsistencial/seleccionar";
 
- $elementos_bd_total = [];
 
- $elementos_sgu_total = [];
 
- $periodo_sgu_old = 0;
 
- try{
 
-     $pdo->beginTransaction();
 
-     
 
-     $periodos_rs = $db->query('SELECT periodo_id, id_periodo_sgu, nivel_id FROM periodo WHERE :hoy BETWEEN periodo_fecha_inicio AND periodo_fecha_fin AND id_periodo_sgu != 0 ORDER BY periodo_id',
 
-         [":hoy"=>$hoy]);
 
-     foreach ($periodos_rs as $per){
 
-         //Verifica si el día de hoy es festivo
 
-         $vacacion_rs = $db->querySingle('select es_festivo(:per, :hoy)', [":per"=>$per["periodo_id"], ":hoy"=>$hoy]);
 
-         if($vacacion_rs["es_festivo"]){
 
-             if($debug){
 
-                 echo "<h3>Dia festivo en Periodo: ".$per["id_periodo_sgu"]."[".$per["periodo_id"]."]</h3>";
 
-             }
 
-             continue;
 
-         }
 
-         
 
-         $carreras_rs = $db->query('SELECT c.clave_carrera, c.carrera_id FROM carrera c WHERE nivel_id = :nivel',
 
-         [":nivel"=>$per["nivel_id"]]);
 
-         if($debug){
 
-             echo "<h3>Periodo: ".$per["id_periodo_sgu"]."[".$per["periodo_id"]."] nivel: ".$per["nivel_id"]."</h3>";
 
-             //print_r($carreras_rs);
 
-         }
 
-         if ($periodo_sgu_old != $per["id_periodo_sgu"]){
 
-             $periodo_sgu_old = $per["id_periodo_sgu"];
 
-             $params = [
 
-                 'idPeriodo'=>$per["id_periodo_sgu"],
 
-                 'fecha'=>$hoy
 
-             ];
 
-         
 
-             $curl = curl_init();
 
-             curl_setopt_array($curl, [
 
-                 CURLOPT_URL => $pag,
 
-                 CURLOPT_RETURNTRANSFER => true,
 
-                 //CURLOPT_ENCODING => "",
 
-                 //CURLOPT_MAXREDIRS => 10,
 
-                 //CURLOPT_TIMEOUT => 0,
 
-                 //CURLOPT_CUSTOMREQUEST => "POST",
 
-                 CURLOPT_POSTFIELDS => json_encode($params),
 
-                 CURLOPT_HTTPHEADER => [
 
-                     "token: ".$token,
 
-                     "username: SGU_APSA_AUD_ASIST",
 
-                     "Content-Type: application/json"
 
-                 ],
 
-             ]);
 
-             $response = curl_exec($curl);
 
-             $err = curl_error($curl);
 
-             /*echo "Response<br>";
 
-             print_r($response);*/
 
-             curl_close($curl);
 
-             if ($err)
 
-                 die("cURL Error #:$err");
 
-         }
 
-         $selectedData = json_decode($response, true);
 
-     
 
-         //claves de carreras en el periodo
 
-         $arreglo_claves = array_map(function ($item) {
 
-             return $item['clave_carrera'];
 
-         }, $carreras_rs);
 
-         //print_r($selectedData); exit();
 
-         $sguHash = array();
 
-         if(!empty($selectedData)){
 
-             
 
-             //Recorre SGU y genera hash
 
-             foreach( $selectedData as $row ){
 
-                 if(!$row["EsMateriaPorReposicion"]){
 
-                     $carrera = $row["ClaveCarrera"];
 
-                     if(is_null($carrera) || empty($carrera))
 
-                         $carrera = '0';
 
-                     
 
-                     $sguHash[] = array(
 
-                         "hash"=>( trim($row["HoraInicio"]."|".($row["NombreMateria"])."|".(trim($row["ClaveProfesor"])==""?"000000":trim($row["ClaveProfesor"]))."|".$row["IdEspacio"]."|".$carrera."|".$per["periodo_id"]) ),
 
-                         "data"=>$row,
 
-                         "per"=>$per["periodo_id"]
 
-                     );
 
-                 }else{//reposición
 
-                     
 
-                     if(in_array($row["ClaveCarrera"] , $arreglo_claves)){
 
-                         //busca yyyy-mm-dd hh:mm:ss en la cadena
 
-                         if (preg_match("/\d{4}-\d{2}-\d{2} de \d{2}:\d{2}:\d{2}/", $row["Observaciones"], $matches)) {
 
-                             $fecha_orig =  str_replace(" de", "", $matches[0]);
 
-                             $fecha_nueva = $row["FechaStr"]." ".$row["HoraInicio"];
 
-                             $hora_fin_nueva = $row["HoraFin"];
 
-                             if($debug){
 
-                                 echo "<br>SELECT * FROM fi_reposicion_sgu($fecha_orig, ".$fecha_nueva.",".$hora_fin_nueva." ,".$row["ClaveProfesor"].", ".$per["periodo_id"].", ".$row["IdEspacio"].")";
 
-                             }else{
 
-                                 $db->query('SELECT * FROM fi_reposicion_sgu(:fecha_orig, :hora_fin, :fecha_rep, :prof, :per, :salon)',
 
-                                     [":fecha_orig"=>$fecha_orig, ":hora_fin"=>$hora_fin_nueva, ":fecha_rep"=>$fecha_nueva, ":prof"=>$row["ClaveProfesor"], ":per"=>$per["periodo_id"], ":salon"=>$row["IdEspacio"] ]);
 
-                                 $log_desc .="SELECT * FROM fi_reposicion_sgu($fecha_orig, ".$fecha_nueva.", ".$row["ClaveProfesor"].", ".$per["periodo_id"].") ## ";
 
-                             }
 
-                         }else{
 
-                             if($debug)
 
-                                 echo "No se encontró fecha y hora en: ".$row["Observaciones"]."<br>";
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             unset($selectedData);
 
-         }
 
-         
 
-         echo count($sguHash)."Total <br>";
 
-     
 
-         $horarios_sgu = [];
 
-         
 
-         /*
 
-         print_r($carreras_rs); 
 
-         echo "<br><hr><br>";*/
 
-         print_r($arreglo_claves);
 
-         echo "<br><hr><br>";
 
-         //$areacomun = array();
 
-         foreach($sguHash as $sgu){
 
-             if(in_array($sgu["data"]["ClaveCarrera"] , $arreglo_claves) /*&& !in_array($sgu["data"]["ClaveMateria"], $areacomun)*/){
 
-                 $horarios_sgu[] = $sgu;
 
-             }
 
-         }
 
-         
 
-         //print_r($horarios_sgu);exit();
 
-         unset($sguHash);
 
-         $elementos_sgu_total = array_merge($elementos_sgu_total, $horarios_sgu);
 
-         
 
-         //Recorre BD y genera hash
 
-         $horarios_rs = $db->query('SELECT * FROM fs_horarios_hash(:dia, :periodo, :fecha)',
 
-             [':dia' => $dia_hoy, ':periodo' => $per["periodo_id"], ':fecha'=>$hoy]);
 
-             //echo "**** SELECT * FROM fs_horarios_hash($dia_hoy, ".$per["periodo_id"].")<br>";
 
-         
 
-         //usort($horarios_rs, 'compareByHash');
 
-         $elementos_bd_total = array_merge($elementos_bd_total, $horarios_rs);
 
-         
 
-     }//foreach periodo
 
-     //print_r($elementos_sgu_total); 
 
- //exit();
 
-     if($debug){
 
-         echo "<h3>Resumen</h3>";
 
-         echo "<h5>SGU [".count($elementos_sgu_total)."]</h5>";
 
-         echo "<h5>BD [".count($elementos_bd_total)."]</h5>";
 
-     }
 
-     // Extraer los "hash" de $lista y $lista2 en arreglos separados
 
-     $hashes_sgu = array_column($elementos_sgu_total, 'hash');
 
-     $hashes_bd = array_column($elementos_bd_total, 'hash');
 
-     
 
-     //print_r($elementos_sgu_total); 
 
-     
 
-     //------------------
 
-     // Encontrar los "hash" que están en $sgu pero no están en $bd
 
-     $hashes_no_en_sgu = array_diff($hashes_bd, $hashes_sgu);
 
-     if($debug) echo "hashes_no_en_sgu ".count($hashes_no_en_sgu)."<br>";
 
-     if(count($hashes_no_en_sgu)>0){
 
-         // Ahora puedes obtener los elementos completos que cumplen la condición original
 
-         $elementos_no_en_sgu = array_filter($elementos_bd_total, function ($item) use ($hashes_no_en_sgu) {
 
-             return in_array($item['hash'], $hashes_no_en_sgu);
 
-         });
 
-         if($debug){
 
-             print_r($elementos_no_en_sgu);
 
-             echo "Sobran ".count($elementos_no_en_sgu)." en BD<br>";
 
-         }
 
-         //Update fecha_fin
 
-         $log_desc = "";
 
-         foreach($elementos_no_en_sgu as $row){
 
-             if($debug){
 
-                 echo "<br>SELECT * FROM fu_horario_deshabilita(".$row["horario_id"].");";
 
-             }else{
 
-                 $db->query('SELECT * FROM fu_horario_deshabilita(:horario)', [":horario"=>$row["horario_id"]]);
 
-                 $log_desc .="SELECT * FROM fu_horario_deshabilita(".$row["horario_id"].") ## ";
 
-             }
 
-         }
 
-         if(!$debug && !empty($log_desc)){
 
-             $log = new LogCambios(__DIR__."/log/");
 
-             $log->appendLog($log_desc);
 
-         }
 
-     }
 
-     // Encontrar los "hash" que están en $sgu pero no están en $bd
 
-     $hashes_no_en_bd = array_diff($hashes_sgu, $hashes_bd);
 
-     //echo "hashes_no_en_bd ".count($hashes_no_en_bd)."<br>";
 
-     
 
-     if(count($hashes_no_en_bd)>0){
 
-         // Ahora puedes obtener los elementos completos que cumplen la condición original
 
-         $elementos_no_en_bd = array_filter($elementos_sgu_total, function ($item) use ($hashes_no_en_bd) {
 
-             return in_array($item['hash'], $hashes_no_en_bd);
 
-         });
 
-         if($debug){
 
-             echo "<br>Faltan ".count($elementos_no_en_bd)." en BD<br>";
 
-             print_r($elementos_no_en_bd); echo "<br>";
 
-         }
 
-         
 
-         //Inserts
 
-         foreach($elementos_no_en_bd as $row){
 
-             if($debug){
 
-                 echo "<br>SELECT * FROM fi_horario($dia_hoy, '".$row["data"]["HoraInicio"]."','".$row["data"]["HoraFin"]."','"
 
-                 .$row["data"]["ClaveDependencia"]."','"
 
-                 .$row["data"]["ClaveCarrera"]."','"
 
-                 .$row["data"]["NombreMateria"]."','"
 
-                 .$row["data"]["ClaveMateria"]."','"
 
-                 .$row["data"]["ClaveProfesor"]."','"
 
-                 .$row["data"]["NombreProfesor"]."','"
 
-                 .$row["data"]["CorreoElectronico"]."','"
 
-                 .$row["data"]["Grupo"]."',"
 
-                 .$row["data"]["IdEspacio"].","
 
-                 .$row["per"].");";
 
-                 if($row["data"]["EsMateriaPorAsignacion"]){ echo " ***Asignacion directa***";}
 
-             }else{
 
-                 $horario_new_rs = $db->querySingle('SELECT * FROM fi_horario(:hoy, :ini, :fin, :dep, :carr, :nom_mat, :cve_mat, :cve_prof, :nom_prof, :correo, :gpo, :espacio, :periodo)',
 
-                     [":hoy"=>$dia_hoy,
 
-                     ":ini"=>$row["data"]["HoraInicio"],
 
-                     ":fin"=>$row["data"]["HoraFin"],
 
-                     ":dep"=>$row["data"]["ClaveDependencia"],
 
-                     ":carr"=>$row["data"]["ClaveCarrera"],
 
-                     ":nom_mat"=>$row["data"]["NombreMateria"],
 
-                     ":cve_mat"=>$row["data"]["ClaveMateria"],
 
-                     ":cve_prof"=>$row["data"]["ClaveProfesor"]==""?"000000":$row["data"]["ClaveProfesor"],
 
-                     ":nom_prof"=>$row["data"]["NombreProfesor"],
 
-                     ":correo"=>$row["data"]["CorreoElectronico"],
 
-                     ":gpo"=>$row["data"]["Grupo"],
 
-                     ":espacio"=>$row["data"]["IdEspacio"],
 
-                     ":periodo"=>$row["per"]
 
-                     ]
 
-                 );
 
-                 //echo $horario_new_rs["fi_horario"]."<br>";
 
-                 if($horario_new_rs["fi_horario"] > 0){
 
-                     if($row["data"]["EsMateriaPorAsignacion"]){
 
-                         $matasig_rs = $db->querySingle('SELECT * FROM fi_materia_asignacion(:hor,:mat,:carr, :prof)',
 
-                             [":hor"=>$horario_new_rs["fi_horario"],
 
-                             ":mat"=>$row["data"]["NombreMateria"],
 
-                             ":carr"=>$row["data"]["Carrera"],
 
-                             ":prof"=>$row["data"]["NombreProfesor"]
 
-                             ]
 
-                         );
 
-                     }
 
-                     $log_desc .="SELECT * FROM fi_horario($dia_hoy, '".$row["data"]["HoraInicio"]."','".$row["data"]["HoraFin"]."','"
 
-                     .$row["data"]["ClaveDependencia"]."','"
 
-                     .$row["data"]["ClaveCarrera"]."','"
 
-                     .$row["data"]["NombreMateria"]."','"
 
-                     .$row["data"]["ClaveMateria"]."','"
 
-                     .$row["data"]["ClaveProfesor"]."','"
 
-                     .$row["data"]["NombreProfesor"]."','"
 
-                     .$row["data"]["CorreoElectronico"]."','"
 
-                     .$row["data"]["Grupo"]."',"
 
-                     .$row["data"]["IdEspacio"]."); [ID=".$horario_new_rs["fi_horario"]."] ##";
 
-                 }
 
-                 
 
-             }
 
-         }
 
-         if(!$debug && !empty($log_desc)){
 
-             $log = new LogCambios(__DIR__."/log/");
 
-             $log->appendLog($log_desc);
 
-         }
 
-         
 
-     }
 
-     $stmt = null; // cierra conexion
 
-     if($debug) {
 
-         echo "<br><br><hr><br><br>";
 
-         usort($elementos_sgu_total, 'compareByHash');
 
-         usort($elementos_bd_total, 'compareByHash');
 
-         echo "<table><tr>";
 
-         echo "<td valign='top'>";
 
-         echo "<h5>SGU [".count($elementos_sgu_total)."]</h5>";
 
-         foreach($elementos_sgu_total as $sgu){
 
-             echo $sgu["hash"]."<br>";
 
-         }
 
-         echo "</td>";
 
-         echo "<td valign='top'>";
 
-         echo "<h5>BD [".count($elementos_bd_total)."]</h5>";
 
-         foreach($elementos_bd_total as $sgu){
 
-             echo $sgu["hash"]." [".$sgu["horario_id"]."]<br>";
 
-         }
 
-         echo "</td>";
 
-         echo "</tr></table>";
 
-     }else{
 
-         $pdo->commit();
 
-         echo "Commit";
 
-     }
 
- } catch(PDOException $e) {
 
-     echo "Error";
 
-     "ERROR BD! ".$e->getMessage();
 
-     $pdo->rollBack();
 
-     if(!$debug){
 
-         $log = new LogCambios(__DIR__."/log/");
 
-         $log->appendLog("ERROR BD! ".$e->getMessage());
 
-     }
 
-     print_r($e->getMessage());
 
- } catch(Exception $e2){
 
-     echo "Error";
 
-     print_r($e2->getMessage());
 
- }
 
- ?>
 
 
  |