checadorclave.php 21 KB

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