checador.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var msgTime = 0;
  2. var modalVisible=false;
  3. var ultimos = [];
  4. var checkInterval = null;
  5. function az(i) {if (i<10) {i = "0" + i};return i;}
  6. function init() {
  7. var clockInterval = setInterval(function(){
  8. var current_time = new Date().getTime();
  9. var excecution_time = parseInt((current_time - started_at) / 1000);
  10. var s = new Date((server_time + excecution_time) * 1000);
  11. drawClock(az(s.getHours()), az(s.getMinutes()));
  12. drawFecha(az(s.getDate()), s.getDay(), s.getMonth(), s.getFullYear());
  13. //document.getElementById('server-time').innerHTML = az(s.getDate())+'/'+az(s.getMonth()+1)+'/'+s.getFullYear()+' '+az(s.getHours())+':'+az(s.getMinutes())+':'+az(s.getSeconds());
  14. }, 1000);
  15. }
  16. function drawClock(hora, min){
  17. $('#min').text(min);
  18. $('#hr').text(hora);
  19. }
  20. function drawFecha(dd, dnum, mm, yyyy){
  21. //var dias = ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'];
  22. var meses = ['Ene', 'Feb', 'Mzo', 'Abr', 'Mayo', 'Jun', 'Jul', 'Agt', 'Sep', 'Oct', 'Nov', 'Dic'];
  23. $('#fecha').text(/*dias[dnum] + ' ' + */dd + ' ' + meses[mm]);
  24. $('#ano').text(yyyy);
  25. }
  26. function modalTimer() {
  27. if(checkInterval == null){//no es error de internet
  28. setTimeout(function() {
  29. $(".sub-bloque ").addClass("d-none");
  30. $("#registro").removeClass("d-none");
  31. $("#cve").focus();
  32. }
  33. , 5000+msgTime);
  34. }
  35. }
  36. //--Sin conexión de internet ---
  37. function checadorAlive() {
  38. if(checkInterval == null){
  39. checkInterval = setInterval(isAlive, 2500);
  40. }
  41. }
  42. function isAlive() {
  43. var alive = false;
  44. try{
  45. $.ajax({
  46. url: 'checador_alive.php',
  47. type: 'POST',
  48. dataType: 'json',
  49. async: false,
  50. timeout: 500,/*ms*/
  51. success: function(result) {
  52. if(result["ok"]!= "" && result["ok"] !== undefined){
  53. cambiaVista('#internet', '#registro');
  54. $("#cve").focus();
  55. alive = true;
  56. clearInterval(checkInterval);
  57. checkInterval = null;
  58. }
  59. },
  60. error: function(jqXHR, textStatus, errorThrown ){
  61. console.log("Sin internet");
  62. }
  63. });//ajax*/
  64. }catch(e){
  65. console.log("error! ",e);
  66. }
  67. return alive;
  68. }
  69. //--Fin sin conexión de internet ---
  70. function cambiaVista(objHide, objShow){
  71. $(objHide).addClass("d-none");
  72. $(objShow).removeClass("d-none");
  73. }
  74. $('#formaChecador').on('submit',function(e){
  75. e.preventDefault();
  76. var clave = $("#cve").val();
  77. $.ajax({
  78. url: 'checador_action.php',
  79. type: 'POST',
  80. dataType: 'json',
  81. data: { cve: $("#cve").val(),},
  82. timeout: 3500,/*ms*/
  83. success: function(result) {
  84. //$("#cve").val('');
  85. if(result["error"]!= "" && result["error"] !== undefined){
  86. console.log(result["error"]);
  87. cambiaVista('#registro', '#result_no');
  88. $('#result_no .clave').text(clave);
  89. $("#last-error").text(result["error"]);
  90. }else{
  91. $("#list-result").find(".mat-desc").html("");
  92. $("#list-result").find(".mat-fecha").html("");
  93. $("#list-result").find(".mat-salon").html("");
  94. $("#list-result").find(".mat-gpo").html("");
  95. $("#nombre").html(result["nombre"]);
  96. var rows = $("#list-result > li").length;//limpia tabla actual
  97. var resultRows = 0;
  98. //nuevas
  99. if(result["result"] !== undefined && result["result"]!= null) resultRows += result["result"].length;
  100. //viejas
  101. if(result["asistencias"] !== undefined && result["asistencias"]!= null) resultRows += result["asistencias"].length;
  102. if(rows > resultRows){//sobran
  103. //borrar renglones extra (rows - result.length) pero dejar al menos 1
  104. while(rows > resultRows && rows > 1){
  105. $("#list-result li:last-child").remove();
  106. rows--;
  107. }
  108. }else{//faltan, clonar
  109. for(var i=rows; i<resultRows; i++){
  110. $("#list-result li:first-child").clone(true).appendTo("#list-result");
  111. }
  112. }
  113. //----------
  114. var retardo = false;
  115. //hay elementos checado nuevo?
  116. if(result["result"] !== undefined && result["result"]!= null && result["result"].length != 0){
  117. cambiaVista('#registro', '#result_ok');
  118. $("#list-result").children().each(function(index) {
  119. if(index < result["result"].length){//llenar info
  120. if(result["result"][index]["grupo"] != ""){
  121. if(result["result"][index]["retardo"]){
  122. retardo = true;
  123. }else{
  124. retardo = false;
  125. }
  126. $(this).find(".mat-gpo").html("<b>Grupo:</b> "+result["result"][index]["grupo"]);
  127. }
  128. $(this).find(".mat-desc").html(result["result"][index]["materia"]);
  129. $(this).find(".mat-fecha").html("Inicia "+result["result"][index]["hora_inicio"]);
  130. if(result["result"][index]["salon"] != "")
  131. $(this).find(".mat-salon").html("<b>Salón:</b> "+result["result"][index]["salon"]);
  132. /*else
  133. $(this).find(".mat-salon").html("");*/
  134. }
  135. });
  136. }
  137. //hay elementos checado anterior?
  138. if(result["asistencias"] !== undefined && result["asistencias"]!= null && result["asistencias"].length != 0){
  139. cambiaVista('#registro', '#result_ok');
  140. $("#list-result").children().each(function(index) {
  141. if(index < result["asistencias"].length){//llenar info
  142. if(result["asistencias"][index]["grupo"] != ""){
  143. if(result["asistencias"][index]["retardo"]){
  144. retardo = true;
  145. }else{
  146. retardo = false;
  147. }
  148. $(this).find(".mat-gpo").html("<b>Grupo:</b> "+result["asistencias"][index]["grupo"]);
  149. }
  150. $(this).find(".mat-desc").html(result["asistencias"][index]["materia"]);
  151. $(this).find(".mat-fecha").html("Inicia "+result["asistencias"][index]["hora_inicio"]);
  152. if(result["asistencias"][index]["salon"] !== undefined && result["asistencias"][index]["salon"] != "")
  153. $(this).find(".mat-salon").html("<b>Salón:</b> "+result["asistencias"][index]["salon"]);
  154. }
  155. });
  156. }
  157. if(retardo){
  158. $("#estado").find("img").prop("src", "imagenes/tarde.png");
  159. $("#estado").find("h2").text("Retardo").removeClass("text-success").addClass("text-warning");
  160. }else{
  161. $("#estado").find("img").prop("src", "imagenes/atiempo.png");
  162. $("#estado").find("h2").text("Asistencia").removeClass("text-warning").addClass("text-success");
  163. }
  164. //sin horario
  165. if(resultRows == 0){
  166. cambiaVista('#registro', '#result_no');
  167. }
  168. }
  169. //Avisos
  170. if(result["avisoArr"] && result["avisoArr"].length != 0 ){//hay aviso
  171. $("#aviso-texto").removeClass("d-none");
  172. msgTime = 2500 * result["avisoArr"].length;//aumenta el tiempo por cada aviso
  173. $("#aviso-texto").html("");
  174. for(var i = 0; i < result["avisoArr"].length; i++){
  175. $("#aviso-texto").append("<div class='alert alert-warning' role='alert'>"+result["avisoArr"][i]+"</div>");
  176. }
  177. }else{
  178. msgTime = 0;//no tiempo extra
  179. $("#aviso-texto").addClass("d-none");
  180. }
  181. },
  182. error: function(jqXHR, textStatus, errorThrown ){
  183. cambiaVista('#registro', '#internet');
  184. checadorAlive();//Muestra mensaje sin internet y comienza a checar hasta que haya
  185. },
  186. complete: function( jqXHR, textStatus ){
  187. $("#cve").val('');
  188. modalTimer();
  189. }
  190. });//ajax*/
  191. });