alumnos_importar_old.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* AJAX
  3. * Selecciona los datos de la carrera
  4. * Recibe:
  5. * id - ID de la carrera
  6. * Return:
  7. * resultado o cadena de error
  8. */
  9. require_once("../../include/constantes.php");
  10. require_once("../../include/nocache.php");
  11. require_once("../../include/util.php");
  12. require_once("../../include/bd_pdo.php");
  13. require_once("../../classes/ValidaSesion.php");
  14. require "../../include/phpSpreadsheet/autoload.php";
  15. use PhpOffice\PhpSpreadsheet\IOFactory;
  16. $pag = "../alumnos_subir.php";
  17. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  18. $objSesion = new ValidaSesion($pdo, 101, GEMA);
  19. if(!$objSesion->tieneAcceso()){
  20. header("Location: ".$pag."?error=2");
  21. exit();
  22. }else if(!isset($_FILES['archivo']['name']) || $_FILES['archivo']['name'] == null || !isset($_POST["ingreso"], $_POST["plan"])){
  23. header("Location: ".$pag."?error=0");
  24. exit();
  25. }else{
  26. //$inputFileName =__DIR__. '/tmp/alumnos_tmp.xlsx';
  27. $archivo = $_FILES['archivo']['name'];
  28. // Check file size
  29. if ($_FILES["archivo"]["size"] > 10 * 1024 * 1024) {//en bytes
  30. header("Location: ".$pag."?error=3");
  31. exit();
  32. }
  33. $exts_arr = array("xls","xlsx");
  34. $dot_arr = explode('.', $archivo);
  35. $ext = end($dot_arr);
  36. if(!in_array($ext, $exts_arr)){
  37. header("Location: ".$pag."?error=4");
  38. exit();
  39. }
  40. //$newfilename = "tmp_al_".$_SESSION["gpo_id"].".".$ext;
  41. $inputFileName = $_FILES["archivo"]["tmp_name"];
  42. $inputFileType = IOFactory::identify($inputFileName);
  43. $reader = IOFactory::createReader($inputFileType);
  44. $spreadsheet = $reader->load($inputFileName);
  45. $sheetData = $spreadsheet->getActiveSheet()->toArray();
  46. try {
  47. $pdo->beginTransaction();
  48. $alumnoArr = array();
  49. $al = 0;
  50. $row = 1;
  51. $repetido = 0;
  52. $fechaingreso = trim(filter_input(INPUT_POST, "ingreso"));//limpia texto
  53. $fechaingreso = fechaGuion($fechaingreso);
  54. $plan = filter_input(INPUT_POST, "plan", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  55. foreach($sheetData as $data){
  56. if($row > 1){
  57. if($data[0] != "" && $data[1] != "" && $data[2] != "" && $data[3] != ""){
  58. $apellidos = mb_strtoupper(trim($data[0]));
  59. $nombre = mb_strtoupper(trim($data[1]));
  60. $clave = intval($data[2]);
  61. $curp = mb_strtoupper(trim($data[3]));
  62. $correo = $data[4];
  63. $telcasa = $data[5];
  64. $telcel = $data[6];
  65. $stmt = $pdo->prepare('Select * from fi_alumno(:nombre, :apellidos, :curp)');//no administrador, devuelve: Usuario_id
  66. $stmt->bindParam(":nombre", $nombre);
  67. $stmt->bindParam(":apellidos", $apellidos);
  68. $stmt->bindParam(":curp", $curp);
  69. if(!$stmt->execute()){
  70. $pdo->rollBack();
  71. header("Location:".$pag."?error=1");
  72. exit();
  73. }
  74. $usr_rs = $stmt->fetch();
  75. $usr_id = $usr_rs["Usuario_id"];
  76. $usr_existe = $usr_rs["Usuario_existe"];
  77. $stmt->closeCursor(); // cierra conexion de resultado
  78. $usr_rs = null;
  79. unset($usr_rs);
  80. if(!$usr_existe){
  81. $stmt = $pdo->prepare('Select * from fi_alumno_planestudio(:id, :plan, :claveulsa, :fechai)');
  82. $stmt->bindParam(":id", $usr_id);
  83. $stmt->bindParam(":plan", $plan);
  84. $stmt->bindParam(":claveulsa", $clave);
  85. $stmt->bindParam(":fechai", $fechaingreso);
  86. if(!$stmt->execute()){
  87. $stmt->closeCursor();
  88. $pdo->rollBack();
  89. header("Location:".$pag."?error=7");
  90. exit();
  91. }
  92. $stmt->closeCursor();
  93. //Inserta datos de contacto
  94. if($telcasa != ""){
  95. $stmt = $pdo->prepare('Select * from fi_contacto(:valor, 1, :usr, 1, NULL)');//inserta tel casa
  96. $stmt->bindParam(":usr", $usr_id);
  97. $stmt->bindParam(":valor", $telcasa);
  98. if(!$stmt->execute()){
  99. $pdo->rollBack();
  100. //echo "Error fi_contacto [".$valorArr[$i]."] [".$tipoArr[$i]."] [".$usr_id."]<br>";
  101. //print_r($stmt->errorInfo());
  102. header("Location:".$pag."?error=6");
  103. exit();
  104. }
  105. }
  106. if($telcel != ""){
  107. $stmt = $pdo->prepare('Select * from fi_contacto(:valor, 3, :usr, 1, NULL)');//inserta tel cel
  108. $stmt->bindParam(":usr", $usr_id);
  109. $stmt->bindParam(":valor", $telcel);
  110. if(!$stmt->execute()){
  111. $pdo->rollBack();
  112. //echo "Error fi_contacto [".$valorArr[$i]."] [".$tipoArr[$i]."] [".$usr_id."]<br>";
  113. //print_r($stmt->errorInfo());
  114. header("Location:".$pag."?error=6");
  115. exit();
  116. }
  117. }
  118. if($correo != ""){
  119. $stmt = $pdo->prepare('Select * from fi_contacto(:valor, 4, :usr, 1, NULL)');//inserta correo
  120. $stmt->bindParam(":usr", $usr_id);
  121. $stmt->bindParam(":valor", $correo);
  122. if(!$stmt->execute()){
  123. $pdo->rollBack();
  124. //echo "Error fi_contacto [".$valorArr[$i]."] [".$tipoArr[$i]."] [".$usr_id."]<br>";
  125. //print_r($stmt->errorInfo());
  126. header("Location:".$pag."?error=6");
  127. exit();
  128. }
  129. }
  130. $stmt->closeCursor(); // cierra conexion de resultado
  131. }else{
  132. $repetido++;
  133. }
  134. }
  135. }
  136. $row++;
  137. }
  138. $pdo->commit();
  139. $stmt = null; // cierra conexion
  140. if($repetido > 0){
  141. header("Location: ../alumnos.php?ok=4&rep=".$repetido);
  142. }else
  143. header("Location: ../alumnos.php?ok=3");
  144. //$alumnoArr;
  145. } catch(PDOException $e) {
  146. $pdo->rollBack();
  147. header("Location:".$pag."?error=1");
  148. //print_r($e);
  149. }
  150. }
  151. ?>