xls_oportunidades.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. setlocale(LC_TIME, 'es_MX.UTF-8');
  3. require_once("../../include/nocache.php");
  4. require_once("../../include/constantes.php");
  5. require_once("../../include/bd_pdo.php");
  6. require_once("../../include/util.php");
  7. require_once("../../classes/ValidaSesion.php");
  8. require "../../include/phpSpreadsheet/autoload.php";
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Style\Border;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use PhpOffice\PhpSpreadsheet\Style\Style;
  14. \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
  15. function numberToLetter ($number){
  16. $temp = ($number-1) % 26;
  17. return chr($temp + 65);
  18. };
  19. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  20. $objSesion = new ValidaSesion($pdo, 125, GEMA);
  21. if(!$objSesion->tieneAcceso()){
  22. echo "No tienes permiso de ver esta página. Revisa que tu sesión siga activa.";
  23. exit();
  24. }
  25. unset($objValida);
  26. if(isset($_POST["plan"], $_POST["intentos"], $_POST["prefijo"]) ) {
  27. $filter_plan = filter_input(INPUT_POST, "plan", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  28. $filter_intentos = filter_input(INPUT_POST, "intentos", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  29. $nom = trim(filter_input(INPUT_POST, "prefijo", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto
  30. }else{
  31. echo "No se recibieron los datos.";
  32. exit();
  33. }
  34. $query = "";
  35. if($filter_plan != 0){
  36. $query .= ":plan,";
  37. }else
  38. $query .= "NULL,";
  39. $stmt = $pdo->prepare('Select * from fs_materiasintentos('.$query.' NULL, :intentos)');
  40. $stmt->bindParam(":intentos", $filter_intentos);
  41. if($filter_plan != 0){ $stmt->bindParam(":plan", $filter_plan); }
  42. if(!$stmt->execute()){
  43. echo "Ocurrió un error al cargar los cambios de carrera.";
  44. //print_r($stmt->errorInfo());
  45. exit();
  46. }else{
  47. $intentos_rs = $stmt->fetchAll();
  48. /*$intentosArr = array();
  49. $last = -1;
  50. $matriasArr = array();
  51. $i = 0;
  52. $total = 0;
  53. foreach($intentos_rs as $intento){
  54. if($last != $intento["Usuario_claveULSA"]){
  55. if($last != -1){
  56. $intentosArr[$i]["materiaArr"]=$materiasArr;
  57. $intentosArr[$i]["total"] = $total;
  58. $i++;
  59. $total = 0;
  60. }
  61. $last = $intento["Usuario_claveULSA"];
  62. $materiasArr = array();
  63. }
  64. $intentosArr[$i] = array("id" => $intento["Usuario_claveULSA"], "nombre" =>$intento["Usuario_apellidos"]." ".$intento["Usuario_nombre"], "carrera"=>$intento["Carrera_desc"]." ".$intento["PlanEstudio_desc"],
  65. "fecha"=>$intento["Alumno_fecha_ingreso"], "total"=>0, "materiaArr"=>array());
  66. $materiasArr[] = array("id" => $intento["Materia_id"], "desc" =>$intento["Materia_desc"], "total"=>$intento["Intentos_total"]);
  67. $total+= $intento["Intentos_total"];
  68. }
  69. if($last != -1){
  70. $intentosArr[$i]["materiaArr"]=$materiasArr;
  71. $intentosArr[$i]["total"] = $total;
  72. }
  73. unset($i);*/
  74. }
  75. $stmt->closeCursor();
  76. //--------
  77. //
  78. // Create new Spreadsheet object
  79. $spreadsheet = new Spreadsheet();
  80. // Set document properties
  81. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  82. ->setLastModifiedBy('Ingeniería La Salle')
  83. ->setTitle('Oportunidades de aprobación')
  84. ->setDescription('Oportunidades para aprobar una materia.');
  85. $headerStyle = new Style();
  86. $headerStyle->applyFromArray(
  87. [
  88. 'fill' => [
  89. 'fillType' => Fill::FILL_SOLID,
  90. 'color' => ['argb' => 'FF001d68'],
  91. ],
  92. 'borders' => [
  93. 'bottom' => ['borderStyle' => Border::BORDER_THIN],
  94. 'right' => ['borderStyle' => Border::BORDER_MEDIUM],
  95. ],
  96. 'font' => [
  97. 'bold' => true,
  98. 'color' => ['argb' => 'FFFFFFFF'],
  99. ]
  100. ]
  101. );
  102. //--------
  103. $sheet = 0;
  104. // Rename worksheet
  105. $spreadsheet->getActiveSheet()->setTitle('Oportunidades');
  106. $row = 1;
  107. $col = 1;
  108. $spreadsheet->setActiveSheetIndex($sheet)
  109. ->setCellValueByColumnAndRow($col++, 1, "Clave")
  110. ->setCellValueByColumnAndRow($col++, 1, "Nombre")
  111. ->setCellValueByColumnAndRow($col++, 1, "Carrera")
  112. ->setCellValueByColumnAndRow($col++, 1, "Fecha de ingreso")
  113. ->setCellValueByColumnAndRow($col++, 1, "Materia no acreditadas")
  114. ->setCellValueByColumnAndRow($col++, 1, "Veces no acreditada");
  115. $colMax = $col;
  116. $colLetter = numberToLetter($colMax-1);
  117. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A1:'.$colLetter.'1');
  118. $row = 2;
  119. foreach($intentos_rs as $intento){
  120. $col = 1;
  121. $spreadsheet->setActiveSheetIndex($sheet)
  122. ->setCellValueByColumnAndRow($col++, $row, $intento["Usuario_claveULSA"])
  123. ->setCellValueByColumnAndRow($col++, $row, $intento["Usuario_apellidos"]." ".$intento["Usuario_nombre"])
  124. ->setCellValueByColumnAndRow($col++, $row, $intento["Carrera_desc"]." ".$intento["PlanEstudio_desc"])
  125. ->setCellValueByColumnAndRow($col++, $row, $intento["Alumno_fecha_ingreso"])
  126. ->setCellValueByColumnAndRow($col++, $row, $intento["Materia_desc"])
  127. ->setCellValueByColumnAndRow($col, $row, $intento["Intentos_total"]);
  128. $colLetter = numberToLetter(4);
  129. $spreadsheet->setActiveSheetIndex($sheet)->getStyle($colLetter.$row)->getNumberFormat()
  130. ->setFormatCode('dd/mm/yyyy');
  131. $row++;
  132. }
  133. //Auto ajustar anchos
  134. for($col = 1; $col < $colMax; $col++)
  135. $spreadsheet->getActiveSheet()->getColumnDimension(numberToLetter($col))->setAutoSize(true);
  136. //$spreadsheet->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
  137. $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
  138. //---------
  139. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  140. $spreadsheet->setActiveSheetIndex(0);
  141. // Redirect output to a client’s web browser (Xlsx)
  142. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  143. header('Content-Disposition: attachment;filename="oportunidades'.$nom.'.xlsx"');
  144. header('Cache-Control: max-age=0');
  145. // If you're serving to IE 9, then the following may be needed
  146. header('Cache-Control: max-age=1');
  147. // If you're serving to IE over SSL, then the following may be needed
  148. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  149. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  150. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  151. header('Pragma: public'); // HTTP/1.0
  152. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  153. $writer->save('php://output');
  154. exit;