xls_reporte.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. setlocale(LC_TIME, 'es_MX.UTF-8');
  3. require_once '../include/bd_pdo.php';
  4. require_once '../classes/Concurso.php';
  5. require_once '../classes/Etapa.php'; '';
  6. require_once '../classes/Resultados.php';
  7. require_once '../adds/phpSpreadsheet/autoload.php';
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\Style\Border;
  11. use PhpOffice\PhpSpreadsheet\Style\Fill;
  12. use PhpOffice\PhpSpreadsheet\Style\Style;
  13. use PhpOffice\PhpSpreadsheet\Shared\Date;
  14. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  15. $files = glob('../temps/*'); // get all file names
  16. foreach($files as $file){ // iterate files
  17. if(is_file($file)) {
  18. unlink($file); // delete file
  19. }
  20. }
  21. $spreadsheet;
  22. if(!isset($_POST['id']) && !isset($_POST['etapa'])){
  23. $return['error'] = 'Primero debes elegir un concurso para consultar';
  24. } else {
  25. if (is_null($pdo)){
  26. $return['error'] = 'No fue posible obtener los datos para generar el archivo';
  27. } else {
  28. $concurso = Concurso::getCouncursoxId($pdo, $_POST['id']);
  29. if (count($concurso) > 0){
  30. $acciones = Proyecto::determinaAccionXEtapa($pdo, $_POST['etapa']);
  31. $accion = '';
  32. if(count($acciones) > 0)
  33. $accion = $acciones['accion'];
  34. $spreadsheet = new Spreadsheet();
  35. $spreadsheet->getProperties()->setCreator('Facultad de Ingeniería / La Salle')
  36. ->setLastModifiedBy('Facultad de Ingeniería / La Salle')
  37. ->setTitle($concurso['nom'] . ' - CIDIT')
  38. ->setDescription('Reporte la ' . $accion . ' de proyectos del '. $concurso['nom'] . ' CIDIT');
  39. $spreadsheet->getDefaultStyle()->getFont()->setName('Indivisa Text Sans');
  40. $listaProyectos = '';
  41. $proyectos = array();
  42. $tipoMarcas = '';
  43. switch (mb_strtoupper(Etapa::getClaveEtapaXID($pdo,$_POST['etapa']))){
  44. case 'REVISION':
  45. $proyectos = Proyecto::getProyectosAutorizados($pdo, $concurso['idconcurso'], 0, 'clave');
  46. $temp = Proyecto::getProyectosFinalistas($pdo, $concurso['idconcurso'], 0, 'clave');
  47. $marcar = array_column($temp,'idproyecto');
  48. $tipoMarcas = 'FINALISTAS';
  49. break;
  50. case 'EVALUACION':
  51. $proyectos = Proyecto::getProyectosFinalistas($pdo, $concurso['idconcurso'], 0, 'clave');
  52. $temp = Proyecto::getProyectosGanadores($pdo, 0, $concurso['idconcurso']);
  53. $marca = array();
  54. foreach ($temp as $t){
  55. $marca[$t['idproyecto']] = $t['lugar'];
  56. }
  57. $tipoMarcas = 'GANADORES';
  58. $usuarios = Etapa::getUsuariosValoraciones($pdo, $concurso['idconcurso'], $_POST['etapa'], 'apellidos');
  59. break;
  60. }
  61. if (count($proyectos) > 0){
  62. $cont = 0;
  63. foreach ($proyectos as $proyecto) {
  64. $banner = null;
  65. switch ($tipoMarcas){
  66. case 'FINALISTAS':
  67. if (in_array($proyecto['idproyecto'], $marca))
  68. $banner = array('texto' => 'Proyecto Finalista','estilo' => $tipoMarcas);
  69. break;
  70. case 'GANADORES':
  71. if (isset($marca[$proyecto['idproyecto']])){
  72. $texto = '';
  73. $color = '';
  74. switch ($marca[$proyecto['idproyecto']]){
  75. case 1: $texto = '1° Lugar'; $color = 'ORO'; break;
  76. case 2: $texto = '2° Lugar'; $color = 'PLATA'; break;
  77. case 3: $texto = '3° Lugar'; $color = 'BRONCE'; break;
  78. }
  79. $texto .= ' - ' . Proyecto::getCategoriaXID($pdo, $proyecto['categoria']);
  80. $banner = array('texto' => $texto, 'estilo' =>$color);
  81. }
  82. break;
  83. }
  84. $tabla = Resultados::evaluacionesXProyecto($pdo, $proyecto['idproyecto'], $_POST['etapa'], 'ARRAY');
  85. generaContenido($cont,$proyecto,$tabla, $banner);
  86. $cont++;
  87. }
  88. if (isset($usuarios))
  89. if (count($usuarios) > 0 )
  90. generaParticipantes ($cont, $usuarios);
  91. }
  92. $sheetIndex = $spreadsheet->getIndex($spreadsheet->getSheetByName('Worksheet'));
  93. $spreadsheet->removeSheetByIndex($sheetIndex);
  94. $spreadsheet->setActiveSheetIndex(0);
  95. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  96. $writer->save('../temps/' . $concurso['nom'] .'-CIDIT.xlsx');
  97. $return['ok'] = 'ok';
  98. $return['nombre'] = $concurso['nom'] .'-CIDIT';
  99. $return['file'] = 'temps/' . $concurso['nom'] .'-CIDIT.xlsx';
  100. }
  101. }
  102. }
  103. $return['json'] = json_encode($return);
  104. echo json_encode($return);
  105. function eligeEstilo($tipo, $indice = 0){
  106. $estilo = new Style();
  107. switch (mb_strtoupper($tipo)){
  108. case 'TITULO':
  109. case 'TÍTULO':
  110. $estilo->applyFromArray([
  111. 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER,'vertical' => Alignment::VERTICAL_CENTER,],
  112. 'borders' => ['bottom' => ['borderStyle' => Border::BORDER_MEDIUM],'color' => ['argb' => 'FFE0E0E0'],],
  113. 'font' => ['size' => 18,'bold' => true,'color' => ['argb' => 'FF001D68'],'name' => 'Indivisa Text Sans',]
  114. ]);
  115. break;
  116. case 'PERSONA':
  117. switch($indice) {
  118. case 1:
  119. $estilo->applyFromArray([
  120. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_LEFT,],
  121. 'borders' => ['right' => ['borderStyle' => Border::BORDER_THIN],'color' => ['argb' => 'FF898A8C'],],
  122. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFE0E0E0'],],
  123. 'font' => ['size' => 11,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FF001D68'],],
  124. ]);
  125. break;
  126. case 2:
  127. $estilo->applyFromArray([
  128. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_LEFT,],
  129. 'borders' => ['right' => ['borderStyle' => Border::BORDER_THIN],'color' => ['argb' => 'FF898A8C'],],
  130. 'font' => ['size' => 11,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FF001D68'],],
  131. ]);
  132. break;
  133. }
  134. break;
  135. case 'RENGLON':
  136. switch($indice) {
  137. case 1:
  138. $estilo->applyFromArray([
  139. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  140. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFE0E0E0'],],
  141. 'font' => ['size' => 11,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FF001D68'],],
  142. ]);
  143. break;
  144. case 2:
  145. $estilo->applyFromArray([
  146. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  147. 'font' => ['size' => 11,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FF001D68'],],
  148. ]);
  149. break;
  150. }
  151. break;
  152. case 'ESTRELLA':
  153. case 'ESTRELLAS':
  154. switch($indice) {
  155. case 1:
  156. $estilo->applyFromArray([
  157. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_RIGHT,],
  158. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFE0E0E0'],],
  159. 'font' => ['size' => 14,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FFF9BF3B'],],
  160. ]);
  161. break;
  162. case 2:
  163. $estilo->applyFromArray([
  164. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_RIGHT,],
  165. 'font' => ['size' => 14,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FFF9BF3B'],],
  166. ]);
  167. break;
  168. }
  169. break;
  170. case 'PROMEDIO':
  171. $estilo->applyFromArray([
  172. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FF001D68'],],
  173. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  174. 'font' => ['size' => 11,'bold' => true,'color' => ['argb' => 'FFFFFFFF'],'name' => 'Indivisa Text Sans',]
  175. ]);
  176. break;
  177. case 'RUBRO':
  178. $estilo->applyFromArray([
  179. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FF001D68'],],
  180. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_RIGHT,],
  181. 'font' => ['size' => 11,'bold' => true,'color' => ['argb' => 'FFFFFFFF'],'name' => 'Indivisa Text Sans',]
  182. ]);
  183. break;
  184. case 'AZUL':
  185. $azules = array('FFFFFF','007BC4','0091CF','009ED7','00A6CE','00ADDE','64BEE6','8AAAC8','7197BA','5285B2','137EA3','0C7696','2D549B','27489B');
  186. $estilo->applyFromArray([
  187. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FF' . $azules[$indice]],],
  188. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  189. 'font' => ['size' => 11,'bold' => true,'color' => ['argb' => 'FFFFFFFF'],'name' => 'Indivisa Text Sans',]
  190. ]);
  191. break;
  192. case 'ORO':
  193. $estilo->applyFromArray([
  194. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  195. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFD4AF37'],],
  196. 'font' => ['size' => 16,'name' => 'Indivisa Text Sans','bold' => true,'color' => ['argb' => 'FFFFFFFF'],],
  197. ]);
  198. break;
  199. case 'PLATA':
  200. $estilo->applyFromArray([
  201. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  202. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFB4B4B4'],],
  203. 'font' => ['size' => 16,'bold' => true,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FFFFFFFF'],],
  204. ]);
  205. break;
  206. case 'BRONCE':
  207. $estilo->applyFromArray([
  208. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  209. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FFAD8A56'],],
  210. 'font' => ['size' => 16,'bold' => true,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FFFFFFFF'],],
  211. ]);
  212. break;
  213. case 'FINALISTA':
  214. $estilo->applyFromArray([
  215. 'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,],
  216. 'fill' => ['fillType' => Fill::FILL_SOLID,'color' => ['argb' => 'FF339933'],],
  217. 'font' => ['size' => 16,'bold' => true,'name' => 'Indivisa Text Sans','color' => ['argb' => 'FFFFFFFF'],],
  218. ]);
  219. break;
  220. }
  221. return $estilo;
  222. }
  223. function generaContenido($hoja,$proyecto,$tablaResultados, $banner = null){
  224. global $spreadsheet;
  225. $ajustables = array();
  226. $spreadsheet->setActiveSheetIndex($hoja);
  227. $spreadsheet->getActiveSheet()->setTitle($proyecto['clave']);
  228. $renglon = 1;
  229. $columna = 'A';
  230. $maxColumn = 'A';
  231. if (!is_null($banner)){
  232. array_push($ajustables,$renglon);
  233. $renglon++;
  234. }
  235. array_push($ajustables,$renglon);
  236. $renglon += 2;
  237. $renglonInicial = $renglon;
  238. foreach ($tablaResultados as $resultados){
  239. foreach ($resultados as $linea){
  240. $columna = 'A';
  241. $cont = 0;
  242. foreach ($linea as $celda){
  243. if (is_numeric($celda['texto']))
  244. $tipo = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC;
  245. else{
  246. $tipo = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING;
  247. }
  248. $spreadsheet->getActiveSheet()->getCell($columna.$renglon)->setValueExplicit($celda['texto'],$tipo);
  249. $spreadsheet->getActiveSheet()->duplicateStyle(eligeEstilo($celda['estilo'],$celda['indice']), $columna.$renglon);
  250. $cont++;
  251. if ($cont < count($linea))
  252. $columna++;
  253. }
  254. if ($columna > $maxColumn)
  255. $maxColumn = $columna;
  256. $renglon++;
  257. }
  258. $renglon += 2;
  259. }
  260. $renglonFinal = $renglon - 2;
  261. foreach ($ajustables as $ajusta){
  262. if (count($ajustables) == 2 && $ajusta == 1){
  263. $spreadsheet->getActiveSheet()->setCellValue('A'.$ajusta, $banner['texto']);
  264. $spreadsheet->getActiveSheet()->duplicateStyle(eligeEstilo($banner['estilo']), 'A'.$ajusta.':'.$maxColumn.$ajusta);
  265. $spreadsheet->getActiveSheet()->mergeCells('A'.$ajusta.':'.$maxColumn.$ajusta);
  266. $spreadsheet->getActiveSheet()->getRowDimension($ajusta)->setRowHeight(35);
  267. } else {
  268. $spreadsheet->getActiveSheet()->setCellValue('A'.$ajusta, $proyecto['proyecto']);
  269. $spreadsheet->getActiveSheet()->mergeCells('A'.$ajusta.':'.$maxColumn.$ajusta);
  270. $spreadsheet->getActiveSheet()->duplicateStyle(eligeEstilo('TITULO'), 'A'.$ajusta.':'.$maxColumn.$ajusta);
  271. $spreadsheet->getActiveSheet()->getRowDimension($ajusta)->setRowHeight(55);
  272. }
  273. }
  274. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  275. for ($ajusta = 'B'; $ajusta <= $maxColumn; $ajusta++){
  276. $spreadsheet->getActiveSheet()->getColumnDimension($ajusta)->setWidth(15);
  277. $spreadsheet->getActiveSheet()-> getStyle($ajusta.$renglonInicial.':'.$ajusta.$renglonFinal)->getAlignment()->setWrapText(true);
  278. }
  279. $renglon++;
  280. $spreadsheet->getActiveSheet()->getStyle('A1:G1')->getAlignment()->setWrapText(true);
  281. for ($ajusta = $renglonInicial; $ajusta <= $renglonFinal; $ajusta++){
  282. $spreadsheet->getActiveSheet()->getRowDimension($ajusta)->setRowHeight(-1);
  283. }
  284. $spreadsheet->getActiveSheet()->setShowGridlines(false);
  285. $spreadsheet->getActiveSheet()->setSelectedCell('A1');
  286. $spreadsheet->createSheet();
  287. }
  288. function generaParticipantes($hoja, $usuarios){
  289. global $spreadsheet;
  290. $spreadsheet->setActiveSheetIndex($hoja);
  291. $spreadsheet->getActiveSheet()->setTitle('Administrativos-Docentes');
  292. $spreadsheet->getActiveSheet()->getCell('A1')->setValue("Administrativos y Docentes Participantes");
  293. $spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
  294. $spreadsheet->getActiveSheet()->duplicateStyle(eligeEstilo('TITULO'), 'A1');
  295. $renglon = 2;
  296. $cont = 1;
  297. foreach ($usuarios as $usuario){
  298. $spreadsheet->getActiveSheet()->getCell('A'.$renglon)->setValueExplicit($usuario['apellidos'] . ' ' . $usuario['nombre'],\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
  299. $spreadsheet->getActiveSheet()->duplicateStyle(eligeEstilo('RENGLON',$cont%3), 'A'.$renglon);
  300. $cont++;
  301. if ($cont % 3 == 0)
  302. $cont++;
  303. $renglon++;
  304. }
  305. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  306. for ($ajusta = 2; $ajusta <= $renglon; $ajusta++){
  307. $spreadsheet->getActiveSheet()->getRowDimension($ajusta)->setRowHeight(-1);
  308. }
  309. $spreadsheet->getActiveSheet()->setShowGridlines(false);
  310. $spreadsheet->getActiveSheet()->setSelectedCell('A1');
  311. $spreadsheet->createSheet();
  312. }