checadorclave_tst.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. //no index
  3. header("X-Robots-Tag: noindex, nofollow", true);
  4. //no caché
  5. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  6. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  7. header('Cache-Control: no-store, no-cache, must-revalidate');
  8. header('Cache-Control: post-check=0, pre-check=0', false);
  9. header('Pragma: no-cache');
  10. date_default_timezone_set('America/Mexico_City');
  11. //header("Content-Type: text/xml; charset=UTF-8");
  12. require_once("../include/util.php");
  13. require_once("../include/nusoap/nusoap.php");
  14. require_once("../include/bd_pdo.php");
  15. //define("PAG", "http://200.13.89.27/webservice/materiainfo_service");//pruebas
  16. define("PAG", "http://200.13.89.8/webservice/materiainfo_service");
  17. $server = new soap_server();
  18. $server->configureWSDL("checador_service", PAG);
  19. //Create a complex types
  20. $server->wsdl->addComplexType('HorarioComplex', 'complexType', 'struct', 'all','',
  21. array( 'salon' => array('name' => 'salon','type' => 'xsd:string'),
  22. 'hora' => array('name' => 'hora','type' => 'xsd:string'),
  23. 'hora_inicio' => array('name' => 'hora_inicio','type' => 'xsd:string'),
  24. 'tipo' => array('name' => 'tipo','type' => 'xsd:integer'),
  25. 'fecha' => array('name' => 'fecha','type' => 'xsd:string'),
  26. 'retardo' => array('name' => 'retardo','type' => 'xsd:boolean'),
  27. 'materia' => array('name' => 'materia','type' => 'xsd:string'),
  28. 'grupo' => array('name' => 'grupo','type' => 'xsd:string')
  29. )
  30. );
  31. $server->wsdl->addComplexType('HorariosList', 'complexType', 'array', '', 'SOAP-ENC:Array', array(),
  32. array(
  33. array('ref' => 'SOAP-ENC:arrayType',
  34. 'wsdl:arrayType' => 'tns:HorarioComplex[]')
  35. ),
  36. 'tns:HorarioComplex'
  37. );
  38. $server->wsdl->addComplexType('MensajeComplex', 'complexType', 'struct', 'all','',
  39. array(
  40. 'texto' => array('name' => 'texto','type' => 'xsd:string')
  41. )
  42. );
  43. $server->wsdl->addComplexType('MensajesList', 'complexType', 'array', '', 'SOAP-ENC:Array', array(),
  44. array(
  45. array('ref' => 'SOAP-ENC:arrayType',
  46. 'wsdl:arrayType' => 'tns:MensajeComplex[]')
  47. ),
  48. 'tns:MensajeComplex'
  49. );
  50. //---------
  51. function registraAsistencia($pdo, $usr_id){
  52. try {
  53. $fecha = date("Y-m-d");
  54. $hora = date("H:i");
  55. $fecha = '2022-08-09';
  56. $hora = '18:53:40';
  57. $fecha_hora = $fecha." ".$hora;
  58. $esProf = false;
  59. $esAdmin = false;
  60. $asistRegistrada = false;
  61. $msg = 0;
  62. $error = false;
  63. $resultMsg = "";
  64. $hasAsistencia = false;//revisa si tiene asistenias nuevas
  65. //Arrelo para guardar los resultados de las validaciones
  66. $asistenciasArr = array(
  67. "asistencia" => array("profesor"=>array(), "administrativo"=>array()),
  68. "retardo" => array("profesor"=>array(), "administrativo"=>array())
  69. );
  70. //--Profesor LICENCIATURA
  71. if(!$error){
  72. //Lee asistencias
  73. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_busca(:fecha, :hora, :usr, -30, 16, 1)');//Limite inferior es incluyente, limite superior excluyente
  74. $stmt->bindParam(":usr", $usr_id);
  75. $stmt->bindParam(":fecha", $fecha);
  76. $stmt->bindParam(":hora", $hora);
  77. if(!$stmt->execute()){
  78. //$t = $stmt->errorInfo();
  79. $resultMsg = "No se pudieron leer los horarios.";
  80. $error = true;
  81. }else{
  82. $esProf = true;
  83. $rs = $stmt->fetchAll();
  84. if(count($rs) > 0) $hasAsistencia = true;
  85. $asistenciasArr["asistencia"]["profesor"] = array_merge($asistenciasArr["asistencia"]["profesor"], $rs);
  86. }
  87. $stmt->closeCursor();
  88. }
  89. if(!$error){
  90. //Lee retardos
  91. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_busca(:fecha, :hora, :usr, 16, 31, 1)');//Limite inferior es incluyente, limite superior excluyente
  92. $stmt->bindParam(":usr", $usr_id);
  93. $stmt->bindParam(":fecha", $fecha);
  94. $stmt->bindParam(":hora", $hora);
  95. if(!$stmt->execute()){
  96. //$t = $stmt->errorInfo();
  97. $resultMsg = "No se pudieron leer los horarios. ";
  98. $error = true;
  99. }else{
  100. $esProf = true;
  101. $rs = $stmt->fetchAll();
  102. if(count($rs) > 0) $hasAsistencia = true;
  103. $asistenciasArr["retardo"]["profesor"] = array_merge($asistenciasArr["retardo"]["profesor"], $rs);
  104. }
  105. $stmt->closeCursor();
  106. }
  107. //--Profesor POSGRADO
  108. if(!$error){
  109. //Lee asistencias
  110. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_busca(:fecha, :hora, :usr, -60, 10, 2)');//Limite inferior es incluyente, limite superior excluyente
  111. $stmt->bindParam(":usr", $usr_id);
  112. $stmt->bindParam(":fecha", $fecha);
  113. $stmt->bindParam(":hora", $hora);
  114. if(!$stmt->execute()){
  115. //$t = $stmt->errorInfo();
  116. $resultMsg = "No se pudieron leer los horarios. ";
  117. $error = true;
  118. }else{
  119. $esProf = true;
  120. $rs = $stmt->fetchAll();
  121. if(count($rs) > 0) $hasAsistencia = true;
  122. $asistenciasArr["asistencia"]["profesor"] = array_merge($asistenciasArr["asistencia"]["profesor"], $rs);
  123. }
  124. $stmt->closeCursor();
  125. }
  126. if(!$error){
  127. //Lee retardos
  128. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_busca(:fecha, :hora, :usr, 10, 31, 2)');//Limite inferior es incluyente, limite superior excluyente
  129. $stmt->bindParam(":usr", $usr_id);
  130. $stmt->bindParam(":fecha", $fecha);
  131. $stmt->bindParam(":hora", $hora);
  132. if(!$stmt->execute()){
  133. //$t = $stmt->errorInfo();
  134. $resultMsg = "No se pudieron leer los horarios. ";
  135. $error = true;
  136. }else{
  137. $esProf = true;
  138. $rs = $stmt->fetchAll();
  139. if(count($rs) > 0) $hasAsistencia = true;
  140. $asistenciasArr["retardo"]["profesor"] = array_merge($asistenciasArr["retardo"]["profesor"], $rs);
  141. }
  142. $stmt->closeCursor();
  143. }
  144. //--Administrativo
  145. if(!$error){
  146. //Lee asistencias
  147. $stmt = $pdo->prepare('Select * from fs_asistenciaadministrativo_busca(:fecha, :hora, :usr)');//Limite inferior es incluyente, limite superior excluyente
  148. $stmt->bindParam(":usr", $usr_id);
  149. $stmt->bindParam(":fecha", $fecha);
  150. $stmt->bindParam(":hora", $hora);
  151. if(!$stmt->execute()){
  152. $t = $stmt->errorInfo();
  153. $resultMsg = "No se pudieron leer los horarios administrativos. ";
  154. $error = true;
  155. }else{
  156. $esAdmin = true;
  157. $rs = $stmt->fetchAll();
  158. if(count($rs) > 0) $hasAsistencia = true;
  159. $asistenciasArr["asistencia"]["administrativo"] = array_merge($asistenciasArr["asistencia"]["administrativo"], $rs);
  160. }
  161. $stmt->closeCursor();
  162. }
  163. /*if(!$error){
  164. //Lee retardos no se usan
  165. $stmt = $pdo->prepare('Select * from fs_asistenciaadministrativo_busca(:fecha, :hora, :usr, 16, 31)');//Limite inferior es incluyente, limite superior excluyente
  166. $stmt->bindParam(":usr", $usr_rs["Usuario_id"]);
  167. $stmt->bindParam(":fecha", $fecha);
  168. $stmt->bindParam(":hora", $hora);
  169. if(!$stmt->execute()){
  170. //$t = $stmt->errorInfo();
  171. $error = 2; // "No se pudieron leer los horarios. ";
  172. $error = true;
  173. }else{
  174. $rs = $stmt->fetchAll();
  175. if(count($rs) > 0) $hasAsistencia = true;
  176. $asistenciasArr["retardo"]["administrativo"] = array_merge($asistenciasArr["retardo"]["administrativo"], $rs);
  177. }
  178. $stmt->closeCursor();
  179. }*/
  180. //--Inserta en tabla de asistencia
  181. $resultArr = array();
  182. if($hasAsistencia){
  183. $pdo->beginTransaction();
  184. foreach($asistenciasArr as $keyAsist=>$asistArr){
  185. if($keyAsist == "asistencia") $retardo = 'false'; else $retardo = 'true';
  186. if($keyAsist != "asistencia") $retardo = 'true';
  187. foreach($asistArr as $keyTipo=>$tipoArr){
  188. if($keyTipo == "profesor") $tipo = 1; else $tipo = 2;
  189. foreach($tipoArr as $horario){
  190. $query = "";
  191. if($tipo == 1 && isset($horario["Reposicion_id"]) && $horario["Reposicion_id"] != "") $query .= ":rep_id";
  192. else $query .= "NULL";
  193. $isRepo = "false";
  194. if($tipo == 1 && isset($horario["Asistencia_isReposicion"]) && $horario["Asistencia_isReposicion"]){
  195. $isRepo = "true";
  196. }
  197. if($horario["HorarioGrupo_id"] != ""){
  198. $stmt = $pdo->prepare('Select * from fi_asistenciausuario(:usr, :tipo, :fecha_evento, :horario, :retardo, '.$isRepo.', :fecha_reg,'.$query.')');
  199. $stmt->bindParam(":horario", $horario["HorarioGrupo_id"]);
  200. }else
  201. $stmt = $pdo->prepare('Select * from fi_asistenciausuario(:usr, :tipo, :fecha_evento, NULL, :retardo, '.$isRepo.', :fecha_reg,'.$query.')');
  202. $stmt->bindParam(":usr", $usr_id);
  203. $stmt->bindParam(":tipo", $tipo);
  204. $stmt->bindParam(":fecha_evento", $horario["Fecha"]);
  205. $stmt->bindParam(":retardo", $retardo);
  206. $stmt->bindParam(":fecha_reg", $fecha_hora);
  207. if($tipo == 1 && isset($horario["Reposicion_id"]) && $horario["Reposicion_id"] != "") $stmt->bindParam(":rep_id", $horario["Reposicion_id"]);
  208. if(!$stmt->execute()){
  209. //$t = $stmt->errorInfo();
  210. $error = 3; // "No se pudo insertar la asistencia. ";
  211. $error = true;
  212. break;
  213. }else{
  214. $asist_rs = $stmt->fetch();
  215. $stmt->closeCursor();
  216. if($asist_rs["fi_asistenciausuario"] != 0){//insertó algo
  217. $salon = "";
  218. if(isset($horario["Salon_desc"])){//para profesores
  219. if($horario["Salon_desc_larga"]!= ""){
  220. $salon = $horario["Salon_desc"]." (".$horario["Salon_desc_larga"].")";
  221. }else{
  222. $salon = $horario["Salon_desc"];
  223. }
  224. }
  225. if($tipo == 1){
  226. $resultArr[] = array("salon"=>$salon, "hora"=>$hora, "hora_inicio"=>substr($horario["Horario_hora"], 0, 5), "tipo"=>$tipo, "fecha"=>$horario["Fecha"], "retardo"=>$retardo, "materia"=>$horario["Materia_desc"], "grupo"=>$horario["Grupo_desc"]." ".$horario["Carrera_prefijo"]);
  227. }
  228. else{
  229. $resultArr[] = array("salon"=>$salon, "hora"=>$hora, "hora_inicio"=>substr($horario["Horario_hora"], 0, 5), "tipo"=>$tipo, "fecha"=>$horario["Fecha"], "retardo"=>$retardo, "materia"=>'Horario administrativo', "grupo"=>'');
  230. }
  231. $asistRegistrada = true;
  232. }/*else{
  233. $resultMsg = "Ya se registró la asistencia para los horarios disponibles. ";
  234. }*/
  235. }
  236. }//foreach horario
  237. }//foreach prof/admin
  238. }//foreach asistencia/retardo
  239. if($error){
  240. $pdo->rollBack();
  241. }else
  242. $pdo->commit();
  243. }else{
  244. //revisa si ya se había registrado
  245. //Profesor Lic
  246. $registradasArr = null;
  247. $registradasArr = array();
  248. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_registrada(:fecha, :hora, :usr, -30, 31, 1)');//Limite inferior es incluyente, limite superior excluyente
  249. $stmt->bindParam(":usr", $usr_id);
  250. $stmt->bindParam(":fecha", $fecha);
  251. $stmt->bindParam(":hora", $hora);
  252. if(!$stmt->execute()){
  253. //$t = $stmt->errorInfo();
  254. $resultMsg = "No se pudieron leer los horarios.";
  255. $error = true;
  256. }else{
  257. $rs = $stmt->fetchAll();
  258. foreach($rs as $row){
  259. $salon = "";
  260. if(isset($row["Salon_desc"])){//para profesores
  261. if($row["Salon_desc_larga"]!= ""){
  262. $salon = $row["Salon_desc"]." (".$row["Salon_desc_larga"].")";
  263. }else{
  264. $salon = $row["Salon_desc"];
  265. }
  266. }
  267. $registradasArr[]= array("salon"=>$salon, "hora"=>$row["Asistencia_hora"], "hora_inicio"=>substr($row["Horario_hora"], 0, 5), "tipo"=>1, "fecha"=>$row["Fecha"], "retardo"=>$row["Asistencia_isRetardo"], "materia"=>$row["Materia_desc"], "grupo"=>$row["Grupo_desc"]." ".$row["Carrera_prefijo"]);
  268. }
  269. }
  270. $stmt->closeCursor();
  271. //Profesor Posgrado
  272. $stmt = $pdo->prepare('Select * from fs_asistenciaprofesor_registrada(:fecha, :hora, :usr, -60, 31, 2)');//Limite inferior es incluyente, limite superior excluyente
  273. $stmt->bindParam(":usr", $usr_id);
  274. $stmt->bindParam(":fecha", $fecha);
  275. $stmt->bindParam(":hora", $hora);
  276. if(!$stmt->execute()){
  277. //$t = $stmt->errorInfo();
  278. $resultMsg = "No se pudieron leer los horarios.";
  279. $error = true;
  280. }else{
  281. $rs = $stmt->fetchAll();
  282. foreach($rs as $row){
  283. $salon = "";
  284. if(isset($row["Salon_desc"])){//para profesores
  285. if($row["Salon_desc_larga"]!= ""){
  286. $salon = $row["Salon_desc"]." (".$row["Salon_desc_larga"].")";
  287. }else{
  288. $salon = $row["Salon_desc"];
  289. }
  290. }
  291. $registradasArr[]= array("salon"=>$salon, "hora"=>$row["Asistencia_hora"], "hora_inicio"=>substr($row["Horario_hora"], 0, 5), "tipo"=>1, "fecha"=>$row["Fecha"], "retardo"=>$row["Asistencia_isRetardo"], "materia"=>$row["Materia_desc"], "grupo"=>$row["Grupo_desc"]." ".$row["Carrera_prefijo"]);
  292. }
  293. }
  294. $stmt->closeCursor();
  295. //Administrativo
  296. $stmt = $pdo->prepare('Select * from fs_asistenciaadministrativo_registrada(:fecha, :hora, :usr)');//Sin límites administrativos
  297. $stmt->bindParam(":usr", $usr_id);
  298. $stmt->bindParam(":fecha", $fecha);
  299. $stmt->bindParam(":hora", $hora);
  300. if(!$stmt->execute()){
  301. $t = $stmt->errorInfo();
  302. $resultMsg = "No se pudieron leer los horarios.";// (Select * from fs_asistenciaadministrativo_registrada('$fecha', '$hora', ".$usr_rs["Usuario_id"]."))".$t[2];
  303. $error = true;
  304. }else{
  305. $rs = $stmt->fetchAll();
  306. foreach($rs as $row){
  307. $salon = "";
  308. $registradasArr[]= array("salon"=>$salon, "hora"=>$row["Asistencia_hora"], "hora_inicio"=>substr($row["Horario_hora"], 0, 5), "tipo"=>2, "fecha"=>$row["Fecha"], "retardo"=>$row["Asistencia_isRetardo"], "materia"=>"Horario administrativo", "grupo"=>"");
  309. }
  310. }
  311. $stmt->closeCursor();
  312. }//fin asistencias registrdas
  313. if(!$error){
  314. if(count($resultArr) == 0){
  315. if(!isset($resultMsg)) $resultMsg = "";
  316. if(count($registradasArr) > 0){
  317. $resultMsg .= "Ya se registraron asistencias.";
  318. }else{
  319. $resultMsg .= "No hay más horarios para registrar.";
  320. //$pdo->rollBack();
  321. $error = true;
  322. }
  323. }else{
  324. $resultMsg = "Registrado [".$hora."]";
  325. //$return["result"] = $resultArr;
  326. //$pdo->commit();//comment to test
  327. }
  328. }/*else{
  329. $pdo->rollBack();
  330. }*/
  331. }catch(PDOException $e) {
  332. $pdo->rollBack();
  333. $resultMsg = "Ocurrió un error al insertar la asistencia ";
  334. $error = true;
  335. }
  336. //Inserta log de asistencias
  337. /*$log = new LogAsistencias();
  338. if($asistRegistrada)
  339. $log->appendLog($clave, "Registrada");
  340. else
  341. $log->appendLog($clave, "No registrada");*/
  342. return array(
  343. 'result' => !$error,
  344. 'horario_array' => $resultArr,
  345. 'asistencia_array' => $registradasArr,
  346. 'result_msg' => $resultMsg
  347. );
  348. }
  349. //---------
  350. //Recibe clave::string, regresa HorarioComplex
  351. function gethorarios($clave = ""){
  352. global $pdo;
  353. $clave = intval($clave);
  354. //Manda clave ULSA y recibe id de Usuario
  355. $stmt = $pdo->prepare('Select * from fs_validaclaveulsa(:clave) AS "Usuario_id"');//Obtiene todo el calendario
  356. $stmt->bindParam(":clave", $clave);
  357. if(!$stmt->execute()){
  358. $resultMsg = "No existe la clave";
  359. $error = true;
  360. }else{
  361. $usr_rs = $stmt->fetch();
  362. }
  363. $stmt->closeCursor();
  364. //Datos de usuario
  365. if(!$error){
  366. $stmt = $pdo->prepare('Select * from fs_usuario(:id)');
  367. $stmt->bindParam(":id", $usr_rs["Usuario_id"]);
  368. if(!$stmt->execute()){
  369. $resultMsg = "No existe el usuario";
  370. $error = true;
  371. }else{
  372. $usr_datos = $stmt->fetch();//Devuelve sólo 1 resultado
  373. $stmt->closeCursor(); // cierra conexion de resultado
  374. $stmt = null; // cierra conexion
  375. }
  376. }
  377. //Avisos
  378. //Busca avisos de hoy
  379. $avisoArr = array();
  380. $stmt = $pdo->prepare('Select * from fs_avisochecador(:usr, now()::date)');
  381. $stmt->bindParam(":usr", $usr_rs["Usuario_id"]);
  382. if($stmt->execute()){
  383. $aviso_rs = $stmt->fetchAll();
  384. foreach($aviso_rs as $aviso){
  385. $avisoArr[] = $aviso["Aviso_texto"];
  386. }
  387. //$return["avisoArr"] = $avisoArr;
  388. }else{
  389. $t = $stmt->errorInfo();
  390. //$return["errorAviso"] = "error: ".$t[2];
  391. $resultMsg = "No se pudieron obtener los avisos".$t[2];
  392. }
  393. $stmt->closeCursor();
  394. $array = registraAsistencia($pdo, $usr_rs["Usuario_id"]);
  395. $array['usuario_nombre'] = $usr_datos["Usuario_apellidos"]." ".$usr_datos["Usuario_nombre"];
  396. $array['mensaje_array'] = $avisoArr;
  397. return $array;
  398. }
  399. //Recibe clave::string, regresa HorarioComplex
  400. function checahorarios($usr_id = ""){
  401. global $pdo;
  402. $usr_id = intval($usr_id);
  403. return registraAsistencia($pdo, $usr_id);
  404. }
  405. // Parametros de salida
  406. $server->register("gethorarios",
  407. array("clave" => "xsd:integer"),//recibe clave ulsa
  408. array('result' => 'xsd:boolean', 'usuario_nombre' => 'xsd:string', 'horario_array' => 'tns:HorariosList', 'asistencia_array' => 'tns:HorariosList', 'mensaje_array' => 'tns:MensajesList', 'result_msg' => 'xsd:string'),//regresa
  409. PAG,
  410. PAG."#gethorarios",
  411. "rpc",
  412. "encoded",
  413. "Obtiene listado de materias checadas y listado de mensajes para la clave de usuario");
  414. $server->register("checahorarios",
  415. array("usr_id" => "xsd:integer"),//recibe id de usuario
  416. array('result' => 'xsd:boolean', 'horario_array' => 'tns:HorariosList', 'asistencia_array' => 'tns:HorariosList', 'result_msg' => 'xsd:string'),//regresa
  417. PAG,
  418. PAG."#checahorarios",
  419. "rpc",
  420. "encoded",
  421. "Obtiene listado de materias checadas para el id de usuario");
  422. @$server->service(file_get_contents("php://input"));
  423. ?>