xls_horariogrupo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_once("../classes/ArchivoMaterias.php");
  9. require "../../include/phpSpreadsheet/autoload.php";
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Style\Border;
  13. use PhpOffice\PhpSpreadsheet\Style\Fill;
  14. use PhpOffice\PhpSpreadsheet\Style\Style;
  15. use PhpOffice\PhpSpreadsheet\Shared\Date;
  16. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  17. function getHora($h, $i){
  18. $cadena = "";
  19. if($h<10){
  20. $cadena.="0";
  21. }
  22. $cadena.=$h.":";
  23. if($i*DURACION_STEP < 10){
  24. $cadena.='0';
  25. }
  26. $cadena.=($i*DURACION_STEP);
  27. return $cadena;
  28. }
  29. function getCoordenada($dia, $hora, $hora_ini, $row = 4){
  30. $inicio = explode(':',$hora_ini);
  31. $inicio = intval($inicio[0]);
  32. $letra = chr(ord('A')+$dia);
  33. $tmp = explode(":", $hora);
  34. $row = (intval($tmp[0])-$inicio)*FRACCION_HORA + $row;
  35. $row+=(intval($tmp[1]))/60*FRACCION_HORA;
  36. return $letra.$row;
  37. }
  38. function getCoordenadaMerge($dia, $hora, $dura, $hora_ini, $row = 4){
  39. $start = $row;
  40. $inicio = explode(':',$hora_ini);
  41. $inicio = intval($inicio[0]);
  42. $letra = chr(ord('A')+$dia);
  43. $tmp = explode(":", $hora);
  44. $row = (intval($tmp[0])-$inicio)*FRACCION_HORA + $row;
  45. $row+=(intval($tmp[1]))/60*FRACCION_HORA;
  46. $range = $letra.$row.":";
  47. $letra = chr(ord('A')+$dia);
  48. $row = $start;
  49. $tmp = explode(":", $hora);
  50. $row = (intval($tmp[0])-$inicio)*FRACCION_HORA + $row;
  51. $row+=(intval($tmp[1]))/60*FRACCION_HORA;
  52. $row+=$dura/15 - 1;
  53. $range .= $letra.$row;
  54. return $range;
  55. }
  56. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  57. $objSesion = new ValidaSesion($pdo, 272, APSA);
  58. if(!$objSesion->tieneAcceso()){
  59. echo "Error de sesión. No tienes acceso a esta página.";
  60. exit();
  61. }
  62. unset($objValida);
  63. $filter_modificados = 0;//todos
  64. if(isset($_POST["grupo"]) && $_POST["grupo"]!=0){
  65. $gpo = filter_input(INPUT_POST, "grupo", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  66. $stmt = $pdo->prepare('Select * from fs_grupo(:gpo, :periodo)');
  67. $stmt->bindParam(":gpo", $gpo);
  68. }
  69. else{
  70. $stmt = $pdo->prepare('Select * from fs_grupo(NULL, :periodo)');
  71. }
  72. $stmt->bindParam(":periodo", $_SESSION["periodo_id"]);
  73. if(!$stmt->execute()){
  74. $errorDesc = "Ocurrió un error al cargar el grupo.";
  75. }else{
  76. $grupo_rs = $stmt->fetchAll();
  77. if(!is_array($grupo_rs) || count($grupo_rs) == 0){
  78. $stmt->closeCursor();
  79. header("Location: ../grupos.php?error=6");
  80. exit();
  81. }
  82. }
  83. $stmt->closeCursor();
  84. $stmt = $pdo->prepare('Select * from fs_dia(NULL)');
  85. if(!$stmt->execute()){
  86. echo "Error al obtener los días";
  87. print_r($stmt->errorInfo());
  88. exit();
  89. }
  90. $dias_rs = $stmt->fetchAll();
  91. $stmt->closeCursor();
  92. //--------
  93. //
  94. // Create new Spreadsheet object
  95. $spreadsheet = new Spreadsheet();
  96. // Set document properties
  97. $spreadsheet->getProperties()->setCreator('Ingeniería La Salle')
  98. ->setLastModifiedBy('Ingeniería La Salle')
  99. ->setTitle('Horarios')
  100. ->setDescription('Horarios del grupo '.$gpo);
  101. $headerStyle = new Style();
  102. $headerStyle->applyFromArray(
  103. [
  104. 'fill' => [
  105. 'fillType' => Fill::FILL_SOLID,
  106. 'color' => ['argb' => 'FF001d68'],
  107. ],
  108. 'borders' => [
  109. 'bottom' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['argb' => 'FFFFFFFF'],],
  110. 'right' => ['borderStyle' => Border::BORDER_MEDIUM, 'color' => ['argb' => 'FFFFFFFF'],],
  111. ],
  112. 'font' => [
  113. 'bold' => true,
  114. 'color' => ['argb' => 'FFFFFFFF'],
  115. 'name' => 'Indivisa Text Sans',
  116. ],
  117. 'alignment' => [
  118. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  119. ],
  120. ]
  121. );
  122. $titleStyle = new Style();
  123. $titleStyle->applyFromArray(
  124. [
  125. 'font' => [
  126. 'bold' => true,
  127. 'color' => ['argb' => 'FF001e61'],
  128. 'size' => 18,
  129. 'name' => 'Indivisa Text Sans',
  130. ],
  131. 'alignment' => [
  132. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  133. ],
  134. ]
  135. );
  136. $bodyStyle = new Style();
  137. $bodyStyle->applyFromArray(
  138. [
  139. 'alignment' => [
  140. 'vertical' => Alignment::VERTICAL_TOP,
  141. 'wrapText' => true
  142. ],
  143. 'font' => [
  144. 'size' => 10,
  145. 'name' => 'Indivisa Text Sans',
  146. ],
  147. 'borders' => [
  148. /*'allBorders' => [
  149. 'borderStyle' => Border::BORDER_THIN,
  150. 'color' => ['argb' => 'FF001d68'],
  151. ],*/
  152. 'allBorders' => [
  153. 'borderStyle' => Border::BORDER_THICK,
  154. 'color' => ['argb' => 'FFFFFFFF'],
  155. ],
  156. ],
  157. /*'fill'=>[
  158. 'fillType' => Fill::FILL_SOLID,
  159. 'color' => ['argb' => 'FFDEE2E6'],
  160. ]*/
  161. ]
  162. );
  163. $spreadsheet->getDefaultStyle()->getFont()->setName('Indivisa Text Sans');
  164. $row_base = 6;
  165. $page = 0;
  166. foreach($grupo_rs as $grupo){
  167. if($page>0){
  168. $spreadsheet->createSheet();
  169. }
  170. $spreadsheet->setActiveSheetIndex($page);
  171. //crea imagen
  172. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  173. $drawing->setName('La Salle');
  174. $drawing->setDescription('La Salle');
  175. $drawing->setPath('../../img/logopdf.png'); // put your path and image here
  176. $drawing->setCoordinates('A1');
  177. $drawing->setHeight(100);
  178. $drawing->setOffsetX(10);
  179. //agrega imagen
  180. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  181. //crea imagen
  182. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  183. $drawing->setName('Facultad de Ingenería');
  184. $drawing->setDescription('Facultad de Ingenería');
  185. $drawing->setPath('../../img/logoIngenieria.png'); // put your path and image here
  186. $drawing->setCoordinates('G1');
  187. $drawing->setHeight(100);
  188. $drawing->setOffsetX(-20);
  189. //agrega imagen
  190. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  191. // Rename worksheet
  192. $gpo = $grupo["Grupo_desc"]." ".$grupo["Carrera_prefijo"];
  193. $spreadsheet->getActiveSheet()->setTitle($gpo);
  194. $row = $row_base;
  195. //Obtiene horario del grupo
  196. $stmt = $pdo->prepare('Select * from fs_horariogrupo(:gpo, NULL, NULL) order by "Dia_id", "Horario_hora"');
  197. $stmt->bindParam(":gpo", $grupo["Grupo_id"]);
  198. if(!$stmt->execute()){
  199. echo "Ocurrió un error al cargar los horarios del grupo";
  200. print_r($stmt->errorInfo());
  201. //header("Location: ".$pag."?error=2");
  202. exit();
  203. }else{
  204. $horario_rs = $stmt->fetchAll();
  205. $horaMin = horaMin($horario_rs);
  206. $horaMax = horaMax($horario_rs);
  207. }
  208. $stmt->closeCursor();
  209. $hora_ini = date('H', strtotime($horaMin));
  210. $hora_fin = date('H', strtotime($horaMax));
  211. if(intval(date('i', strtotime($horaMax))) != 0)
  212. $hora_fin++;
  213. // Add some data
  214. $spreadsheet->getActiveSheet()
  215. ->setCellValue('A'.$row, 'Grupo '.$gpo);
  216. $spreadsheet->getActiveSheet()->mergeCells('A'.$row.':C'.$row);
  217. $spreadsheet->getActiveSheet()->duplicateStyle($titleStyle, 'A'.$row.':E'.$row);
  218. //$spreadsheet->getActiveSheet()->duplicateStyle($bodyStyle, 'A4:G'.(4+FRACCION_HORA*(HORA_FINAL-HORA_INICIO)));
  219. $row += 2;
  220. $spreadsheet->getActiveSheet()
  221. ->setCellValue('A'.$row, "Hora");
  222. $col = 2;
  223. foreach($dias_rs as $dia){
  224. $spreadsheet->getActiveSheet()
  225. ->setCellValueByColumnAndRow($col++, $row, $dia["Dia_desc"]);
  226. }
  227. $spreadsheet->getActiveSheet()->duplicateStyle($headerStyle, 'A'.$row.':G'.$row);
  228. $row++;
  229. $start_row = $row;
  230. $spreadsheet->getActiveSheet()->duplicateStyle($bodyStyle,
  231. getCoordenada(1, $hora_ini, $hora_ini, $start_row).":".getCoordenada(6, $hora_fin, $hora_ini, $start_row)
  232. );
  233. // HORA_INICIO a HORA_FINAL cada FRACCION_HORA (4) o DURACION_STEP (15)
  234. // for($h = $hora_ini, $i=0; $h < $hora_fin; $h++, $i++){
  235. //for($h = HORA_INICIO; $h < HORA_FINAL; $h++){
  236. for($h = intval($hora_ini); $h < intval($hora_fin); $h++){
  237. for($i=0; $i<FRACCION_HORA; $i++){
  238. $spreadsheet->getActiveSheet()
  239. ->setCellValueByColumnAndRow(1, $row++, getHora($h, $i));
  240. }
  241. }//for each materias
  242. foreach($horario_rs as $bloque){
  243. $coord = getCoordenada($bloque["Dia_id"], $bloque["Horario_hora"], $hora_ini, $start_row);
  244. $salon = "Pendiente";
  245. if(!empty($bloque["Salon_desc"])){
  246. $salon=$bloque["Salon_desc"];
  247. }
  248. $profesores_all = "";
  249. if(empty($bloque["TipoSubmateria_id"]) ){
  250. $stmt = $pdo->prepare('Select * from fs_profesorhorariogrupo(:id)');
  251. $stmt->bindParam(":id", $bloque["HorarioGrupo_id"]);
  252. if(!$stmt->execute()){
  253. $t = $stmt->errorInfo();
  254. $errorDesc = "Ocurrió un error al obtener los datos de los profesores ".$t[2];
  255. break;
  256. }
  257. $profesores_rs = $stmt->fetchAll();
  258. $stmt->closeCursor();
  259. $prof_nombre = "";
  260. if(isset($profesores_rs) && count($profesores_rs) > 0){
  261. foreach($profesores_rs as $profesor){
  262. $prof_nombre = $profesor["Usuario_apellidos"]." ".$profesor["Usuario_nombre"]."\n";
  263. //$profesores_all .= $prof_clave.$prof_nombre."<br>";
  264. $profesores_all .=$prof_nombre;
  265. }
  266. $profesores_all = substr($profesores_all, 0, -1);
  267. }
  268. }
  269. $richText = new \PhpOffice\PhpSpreadsheet\RichText\RichText();
  270. $richText->createText(substr($bloque["Horario_hora"], 0, -3)." ");
  271. $titulo = $richText->createTextRun($bloque["Materia_desc"]);
  272. $titulo->getFont()->setBold(true);
  273. $richText->createText("\nSalón: ".$salon."\n".$profesores_all);
  274. $spreadsheet->getActiveSheet()
  275. //->setCellValue($coord, $bloque["Materia_desc"]."\nSalón: ".$salon."\n".$profesores_all);
  276. ->setCellValue($coord, $richText);
  277. $spreadsheet->getActiveSheet()->mergeCells( getCoordenadaMerge($bloque["Dia_id"], $bloque["Horario_hora"], $bloque["Horario_duracion"], $hora_ini, $start_row) );
  278. $spreadsheet->getActiveSheet()->getStyle($coord)->getFill()
  279. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  280. ->getStartColor()
  281. ->setARGB('FFDEE2E6');
  282. }
  283. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(6);
  284. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  285. $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  286. $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  287. $spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  288. $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(20);
  289. $spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  290. $page++;
  291. }
  292. $spreadsheet->setActiveSheetIndex(0);
  293. // Redirect output to a client’s web browser (Xlsx)
  294. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  295. if(count($grupo_rs)==1)
  296. header('Content-Disposition: attachment;filename="horario_'.$gpo.'.xlsx"');
  297. else
  298. header('Content-Disposition: attachment;filename="horarioGrupos.xlsx"');
  299. header('Cache-Control: max-age=0');
  300. // If you're serving to IE 9, then the following may be needed
  301. header('Cache-Control: max-age=1');
  302. // If you're serving to IE over SSL, then the following may be needed
  303. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  304. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  305. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  306. header('Pragma: public'); // HTTP/1.0
  307. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  308. $writer->save('php://output');
  309. exit;