Resultados.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. require_once 'Etapa.php';
  3. require_once 'Usuario.php';
  4. require_once 'Proyecto.php';
  5. require_once 'Colores.php';
  6. class Resultados {
  7. /********** GENERAL **********/
  8. private static function obtenerTipoPregunta($pdo, $idtipo){
  9. $tipos = array();
  10. if ($idtipo == 0)
  11. $stmt = $pdo->prepare('Select * from cidit_fs_tipopreguntaxid(null)');
  12. else{
  13. $stmt = $pdo->prepare('Select * from cidit_fs_tipopreguntaxid(:id)');
  14. $stmt->bindParam(':id', $idtipo);
  15. }
  16. if($stmt->execute())
  17. $tipos = $stmt->fetchAll();
  18. $stmt->closeCursor();
  19. $stmt = null;
  20. return $tipos;
  21. }
  22. private static function obtenerTablaResultadosxPregunta($pdo, $id, $tipo){
  23. $tabla = '';
  24. $stmt = $pdo->prepare('Select * from cidit_fs_tablaresultadosxpregunta(:id,:tipo)');
  25. $stmt->bindParam(':id', $id);
  26. $stmt->bindParam(':tipo', $tipo);
  27. if($stmt->execute())
  28. $tabla = $stmt->fetch();
  29. $stmt->closeCursor();
  30. $stmt = null;
  31. return $tabla;
  32. }
  33. private static function obtenerValoresOpcionesPregunta($pdo, $idpregunta){
  34. $valores = array();
  35. $stmt = $pdo->prepare('Select * from cidit_fs_valoresxpregunta(:id)');
  36. $stmt->bindParam(':id', $idpregunta);
  37. if($stmt->execute())
  38. $valores = $stmt->fetchAll();
  39. $stmt->closeCursor();
  40. $stmt = null;
  41. return $valores;
  42. }
  43. public static function esPromediable($pdo, $idtipo){
  44. $tipos = self::obtenerTipoPregunta($pdo, $idtipo);
  45. $promediable = false;
  46. switch (mb_strtoupper($tipos[0]['tipo'])){
  47. case 'ESTRELLA':
  48. case 'ESTRELLAS':
  49. case 'RANKING':
  50. case 'OPCION MULTIPLE':
  51. case 'OPCIÓN MULTIPLE':
  52. case 'OPCIÓN MÚLTIPLE':
  53. case 'OPCION MÚLTIPLE':
  54. case 'OPCIONES':
  55. $promediable = true;
  56. break;
  57. case 'ABIERTA':
  58. case 'ABIERTAS':
  59. $promediable = false;
  60. break;
  61. }
  62. return $promediable;
  63. }
  64. /********** OBTENER RESULTADOS **********/
  65. private static function getResultadosEvaluacion($pdo, $etapa, $proyecto, $pregunta){
  66. $evaluaciones = array();
  67. if ($proyecto == 0 && $pregunta == 0)
  68. $stmt = $pdo->prepare('Select * from cidit_fs_evaluacionxetapa(null,null,:etapa)');
  69. else{
  70. if ($proyecto == 0){
  71. $stmt = $pdo->prepare('Select * from cidit_fs_evaluacionxetapa(null,:pregunta,:etapa)');
  72. $stmt->bindParam(':pregunta', $pregunta);
  73. } else {
  74. if ($pregunta == 0){
  75. $stmt = $pdo->prepare('Select * from cidit_fs_evaluacionxetapa(:proyecto, null,:etapa)');
  76. $stmt->bindParam(':proyecto', $proyecto);
  77. } else {
  78. $stmt = $pdo->prepare('Select * from cidit_fs_evaluacionxetapa(:proyecto,:pregunta,:etapa)');
  79. $stmt->bindParam(':proyecto', $proyecto);
  80. $stmt->bindParam(':pregunta', $pregunta);
  81. }
  82. }
  83. }
  84. $stmt->bindParam(':etapa', $etapa);
  85. if($stmt->execute())
  86. $evaluaciones = $stmt->fetchAll();
  87. $stmt->closeCursor();
  88. $stmt = null;
  89. return $evaluaciones;
  90. }
  91. private static function getResultadosValoracion($pdo, $etapa, $proyecto, $pregunta){
  92. $valoraciones = array();
  93. if ($proyecto == 0 && $pregunta == 0)
  94. $stmt = $pdo->prepare('Select * from cidit_fs_valoracionxetapa(null,null,:etapa)');
  95. else{
  96. if ($proyecto == 0){
  97. $stmt = $pdo->prepare('Select * from cidit_fs_valoracionxetapa(null,:pregunta,:etapa)');
  98. $stmt->bindParam(':pregunta', $pregunta);
  99. } else {
  100. if ($pregunta == 0){
  101. $stmt = $pdo->prepare('Select * from cidit_fs_valoracionxetapa(:proyecto, null,:etapa)');
  102. $stmt->bindParam(':proyecto', $proyecto);
  103. } else {
  104. $stmt = $pdo->prepare('Select * from cidit_fs_valoracionxetapa(:proyecto,:pregunta,:etapa)');
  105. $stmt->bindParam(':proyecto', $proyecto);
  106. $stmt->bindParam(':pregunta', $pregunta);
  107. }
  108. }
  109. }
  110. $stmt->bindParam(':etapa', $etapa);
  111. if($stmt->execute())
  112. $valoraciones = $stmt->fetchAll();
  113. $stmt->closeCursor();
  114. $stmt = null;
  115. return $valoraciones;
  116. }
  117. private static function determinaTabla($pdo, $etapa, $proyecto, $pregunta, $tabla){
  118. $resultados = array();
  119. switch (mb_strtoupper($tabla['tabla'])){
  120. case 'EVALUACION':
  121. case 'EVALUACIÓN':
  122. $resultados = self::getResultadosEvaluacion($pdo, $etapa, $proyecto, $pregunta);
  123. break;
  124. case 'VALORACION':
  125. case 'VALORACIÓN':
  126. $resultados = self::getResultadosValoracion($pdo, $etapa, $proyecto, $pregunta);
  127. break;
  128. }
  129. return $resultados;
  130. }
  131. //---------- GRÁFICAS ----------//
  132. public static function valoresGraficaXPregunta($pdo, $pregunta, $proyecto, $etapa){
  133. $resultados = 0;
  134. $tabla = self::obtenerTablaResultadosxPregunta($pdo, $pregunta, 'pregunta');
  135. if (count($tabla) > 0){
  136. $resultados_rs = self::determinaTabla($pdo, $etapa, $proyecto, $pregunta, $tabla);
  137. if (count($resultados_rs) > 0){
  138. $tipo = $resultados_rs[0]['tipo'];
  139. if (self::esPromediable($pdo, $tipo)){
  140. $temp = 0;
  141. $cont = 0;
  142. foreach ($resultados_rs as $eval){
  143. $temp += $eval['respuesta'];
  144. $cont++;
  145. }
  146. if ($cont > 0)
  147. $resultados = Proyecto::estandarizaNumeros(round($temp/$cont,1));
  148. }
  149. }
  150. }
  151. return $resultados;
  152. }
  153. //---------- PROYECTOS ----------//
  154. public static function evaluacionesXProyecto($pdo, $proyecto, $etapa, $tipoRetorno){
  155. switch ($tipoRetorno){
  156. case 'HTML': $tablas = ''; break;
  157. case 'ARRAY': $tablas = array(); break;
  158. }
  159. $resultados = array();
  160. $rubros = Etapa::getRubrosXEtapa($pdo, $etapa);
  161. if (count($rubros) > 0){
  162. foreach($rubros as $rubro){
  163. $tabla = self::obtenerTablaResultadosxPregunta($pdo, $rubro['idrubro'], 'rubro');
  164. $resultados = self::determinaTabla($pdo, $etapa, $proyecto, 0, $tabla);
  165. if (count($resultados) > 0){
  166. $preguntas = Etapa::getPreguntasXRubro($pdo, $rubro['idrubro']);
  167. $temp = array_column($resultados, 'idusuario');
  168. $temp = array_unique($temp);
  169. $usuarios = array();
  170. foreach ($temp as $user){
  171. array_push($usuarios, array('idusuario' => $user) + Usuario::getNombreUsuario($pdo, $user));
  172. }
  173. array_multisort(array_column($usuarios, 'apellidos'),SORT_ASC,$usuarios);
  174. $cont = 1;
  175. $filtradas = array();
  176. foreach($preguntas as $pregunta) {
  177. if (in_array($pregunta['idpregunta'], array_column($resultados, 'idpregunta')) && self::esPromediable($pdo, $pregunta['tipo']))
  178. array_push($filtradas,$pregunta);
  179. }
  180. switch (mb_strtoupper($tabla['tabla'])){
  181. case 'EVALUACION':
  182. case 'EVALUACIÓN':
  183. switch ($tipoRetorno){
  184. case 'HTML': $tablas .= self::generarTabla($rubro['rubro'], $filtradas, $resultados, $usuarios, true); break;
  185. case 'ARRAY': array_push($tablas, self::generarArreglo($rubro['rubro'], $filtradas, $resultados, $usuarios, true)); break;
  186. }
  187. break;
  188. case 'VALORACION':
  189. case 'VALORACIÓN':
  190. switch ($tipoRetorno){
  191. case 'HTML': $tablas .= self::generarSeccion($pdo, $filtradas, $resultados); break;
  192. case 'ARRAY': array_push($tablas,self::generarArregloSeccion($pdo, $filtradas, $resultados)); break;
  193. }
  194. break;
  195. }
  196. $restantes = array_diff(array_column($pregunta,'idpregunta'), array_column($filtradas,'idpregunta'));
  197. if (count($restantes) > 0){
  198. $filtradas = array();
  199. foreach($restantes as $falta) {
  200. while($pregunta = current($preguntas)){
  201. if ($pregunta['idpregunta'] == $falta){
  202. array_push($filtradas,$pregunta);
  203. end($resultados);
  204. }
  205. next($resultados);
  206. }
  207. }
  208. switch ($tipoRetorno){
  209. case 'HTML': $tablas .= self::generarTabla($rubro['rubro'], $filtradas, $resultados, $usuarios, false); break;
  210. case 'ARRAY': array_push($tablas,self::generarArreglo($rubro['rubro'], $filtradas, $resultados, $usuarios, false)); break;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return $tablas;
  217. }
  218. private static function generarTabla($rubro, $preguntas, $resultados, $usuarios, $promedio){
  219. $tablas = '<div class="table-responsive-md mt-2">
  220. <table class="tablaProyecto table">
  221. <thead class="text-white">
  222. <tr>
  223. <td class="bg-primary align-middle text-right amplio">' . $rubro . '</td>';
  224. $cont = 1;
  225. foreach($preguntas as $pregunta) {
  226. $tablas .= '<td class="text-center align-middle bg-azul' . $cont%14 . '">' . $pregunta['pregunta'] . '</td>';
  227. $cont++;
  228. if($cont % 14 == 0)
  229. $cont++;
  230. }
  231. $tablas .= '</tr>
  232. </thead>';
  233. foreach ($usuarios as $usuario){
  234. $tablas .= '<tr>
  235. <td class="amplio align-middle">' . $usuario['apellidos'] . ' ' . $usuario['nombre'] . '</td>';
  236. foreach ($preguntas as $pregunta){
  237. $tablas .= '<td class="text-center align-middle">' . self::obtenerRespuesta($resultados, $usuario['idusuario'], $pregunta) . '</td>';
  238. }
  239. $tablas .= '</tr>';
  240. }
  241. if($promedio){
  242. $tablas .= self::getPromedio($resultados, $preguntas, 'HTML');
  243. }
  244. $tablas .= '</tbody>
  245. </table>
  246. </div>';
  247. return $tablas;
  248. }
  249. private static function generarArreglo($rubro, $preguntas, $resultados, $usuarios, $promedio){
  250. $tabla = array();
  251. $temp = array();
  252. array_push($temp,array('texto' => $rubro, 'estilo' => 'RUBRO', 'indice' => 0));
  253. $cont = 1;
  254. foreach($preguntas as $pregunta) {
  255. array_push($temp,array('texto' => $pregunta['pregunta'], 'estilo' => 'AZUL', 'indice' => $cont%14));
  256. $cont++;
  257. if($cont % 14 == 0)
  258. $cont++;
  259. }
  260. array_push($tabla,$temp);
  261. $cont = 1;
  262. foreach ($usuarios as $usuario){
  263. $temp = array();
  264. array_push($temp,array('texto' => $usuario['apellidos'] . ' ' . $usuario['nombre'], 'estilo' => 'PERSONA', 'indice' => $cont%3));
  265. foreach ($preguntas as $pregunta){
  266. array_push($temp,array('texto' => self::obtenerRespuesta($resultados, $usuario['idusuario'], $pregunta), 'estilo' => 'RENGLON', 'indice' => $cont%3));
  267. }
  268. $cont++;
  269. if($cont % 3 == 0)
  270. $cont++;
  271. array_push($tabla,$temp);
  272. }
  273. if($promedio){
  274. $temp = self::getPromedio($resultados, $preguntas,'ARRAY');
  275. $temp2 = array();
  276. if (count($temp) > 0){
  277. array_push($temp2,array('texto' => '', 'estilo' => 'PROMEDIO', 'indice' => 0));
  278. foreach ($temp as $t){
  279. array_push($temp2,array('texto' => $t, 'estilo' => 'PROMEDIO', 'indice' => 0));
  280. }
  281. array_push($tabla,$temp2);
  282. }
  283. }
  284. return $tabla;
  285. }
  286. private static function obtenerRespuesta($resultados, $user, $preg){
  287. $flag = true;
  288. $respuesta = '-';
  289. reset($resultados);
  290. while($resultado = current($resultados)){
  291. if ($resultado['idusuario'] == $user && $resultado['idpregunta'] == $preg['idpregunta']){
  292. switch (mb_strtoupper($preg['tipo'])){
  293. case 'ESTRELLA':
  294. case 'ESTRELLAS':
  295. case 'RANKING':
  296. $respuesta = '<div class="d-inline-flex">';
  297. for ($cont = 0; $cont < intval($resultado['respuesta']); $cont++) {
  298. $respuesta .= '<div class="ing-estrella1 ml-2"></div>';
  299. }
  300. $respuesta .= '</div>';
  301. break;
  302. case 'OPCION MULTIPLE':
  303. case 'OPCIÓN MULTIPLE':
  304. case 'OPCIÓN MÚLTIPLE':
  305. case 'OPCION MÚLTIPLE':
  306. case 'OPCIONES':
  307. if (is_numeric($resultado['respuesta']))
  308. $respuesta = Proyecto::estandarizaNumeros(floatval($resultado['respuesta']));
  309. else
  310. $respuesta = Proyecto::estandarizaNumeros($resultado['respuesta']);
  311. break;
  312. case 'ABIERTA':
  313. case 'ABIERTAS':
  314. $respuesta = $resultado['respuesta'];
  315. }
  316. end($resultados);
  317. }
  318. next($resultados);
  319. }
  320. return $respuesta;
  321. }
  322. private static function getPromedio($resultados, $preguntas, $tipoRetorno){
  323. switch ($tipoRetorno){
  324. case 'HTML': $promedio = ''; break;
  325. case 'ARRAY': $promedio = array(); break;
  326. }
  327. if (count($resultados) > 0){
  328. if ($tipoRetorno == 'HTML')
  329. $promedio = '<tr style="height: 30px;">
  330. <td class="amplio"></td>';
  331. foreach($preguntas as $pregunta){
  332. $cont = 0;
  333. $calif = 0;
  334. foreach ($resultados as $res){
  335. if ($res['idpregunta'] == $pregunta['idpregunta'] && is_numeric($res['respuesta'])){
  336. $calif += $res['respuesta'];
  337. $cont++;
  338. }
  339. }
  340. if ($cont > 0) {
  341. $prom = Proyecto::estandarizaNumeros(round($calif/$cont,1));
  342. switch ($tipoRetorno){
  343. case 'HTML': $promedio .= '<td class="position-relative"><div class="promedio position-absolute">'. $prom . '</div></td>'; break;
  344. case 'ARRAY':
  345. array_push($promedio, $prom);
  346. break;
  347. }
  348. } else {
  349. switch ($tipoRetorno){
  350. case 'HTML': $promedio .= '<td class="position-relative"></td>'; break;
  351. case 'ARRAY': array_push($promedio, ' '); break;
  352. }
  353. }
  354. }
  355. if ($tipoRetorno == 'HTML')
  356. $promedio .= '</tr>';
  357. }
  358. return $promedio;
  359. }
  360. private static function generarSeccion($pdo, $preguntas, $resultados){
  361. $secciones = '';
  362. foreach($preguntas as $pregunta) {
  363. switch (mb_strtoupper($pregunta['tipo'])){
  364. case 'ESTRELLA':
  365. case 'ESTRELLAS':
  366. case 'RANKING':
  367. case 'OPCION MULTIPLE':
  368. case 'OPCIÓN MULTIPLE':
  369. case 'OPCIÓN MÚLTIPLE':
  370. case 'OPCION MÚLTIPLE':
  371. case 'OPCIONES':
  372. $div = self::obtenerEstadistica($pdo, $pregunta, $resultados, 'HTML');
  373. if ($div != ''){
  374. $secciones .= '<div class="w-100">' . $div . '</div>';
  375. }
  376. break;
  377. case 'ABIERTA':
  378. case 'ABIERTAS':
  379. break;
  380. }
  381. }
  382. return $secciones;
  383. }
  384. private static function generarArregloSeccion($pdo, $preguntas, $resultados){
  385. $secciones = array();
  386. foreach($preguntas as $pregunta) {
  387. switch (mb_strtoupper($pregunta['tipo'])){
  388. case 'ESTRELLA':
  389. case 'ESTRELLAS':
  390. case 'RANKING':
  391. case 'OPCION MULTIPLE':
  392. case 'OPCIÓN MULTIPLE':
  393. case 'OPCIÓN MÚLTIPLE':
  394. case 'OPCION MÚLTIPLE':
  395. case 'OPCIONES':
  396. $secciones = self::obtenerEstadistica($pdo, $pregunta, $resultados, 'ARRAY');
  397. break;
  398. case 'ABIERTA':
  399. case 'ABIERTAS':
  400. break;
  401. }
  402. }
  403. return $secciones;
  404. }
  405. private static function obtenerEstadistica($pdo, $pregunta, $resultados, $tipoRetorno){
  406. $valores = self::obtenerValoresOpcionesPregunta($pdo, $pregunta['idpregunta']);
  407. $estadistica = array();
  408. switch ($tipoRetorno){
  409. case 'HTML': $div = ''; break;
  410. case 'ARRAY': $div = array(); break;
  411. }
  412. if (count($valores) > 0){
  413. foreach ($valores as $valor){
  414. array_push($estadistica, array('texto' => $valor['opcion'],'valor' => $valor['valor'], 'porcentaje' => 0, 'cantidad' => 0));
  415. }
  416. reset($estadistica);
  417. while($est = current($estadistica)){
  418. $index = key($estadistica);
  419. foreach ($resultados as $res){
  420. if ($res['respuesta'] == $est['valor'])
  421. $estadistica[$index]['cantidad']++;
  422. }
  423. next($estadistica);
  424. }
  425. $max = max(array_column($estadistica, 'cantidad'));
  426. reset($estadistica);
  427. while($est = current($estadistica)){
  428. $index = key($estadistica);
  429. $estadistica[$index]['porcentaje'] = Proyecto::estandarizaNumeros(round($estadistica[$index]['cantidad']/$max,1)*100);
  430. switch (mb_strtoupper($pregunta['tipo'])){
  431. case 'ESTRELLA':
  432. case 'ESTRELLAS':
  433. case 'RANKING':
  434. $estrellas = intval($estadistica[$index]['valor']);
  435. switch ($tipoRetorno){
  436. case 'HTML':
  437. $estadistica[$index]['valor'] = '<label class="text-primary indivisa-text-italic small mb-0">' . $estadistica[$index]['texto'] . '</label>';
  438. for($cont = 0; $cont < $estrellas; $cont++){
  439. $estadistica[$index]['valor'] .= '<div class="ing-estrella1 ml-2"></div>';
  440. }
  441. break;
  442. case 'ARRAY':
  443. $estadistica[$index]['valor'] = '';
  444. for($cont = 0; $cont < $estrellas; $cont++){
  445. $estadistica[$index]['valor'] .= '★';
  446. }
  447. break;
  448. }
  449. break;
  450. case 'OPCION MULTIPLE':
  451. case 'OPCIÓN MULTIPLE':
  452. case 'OPCIÓN MÚLTIPLE':
  453. case 'OPCION MÚLTIPLE':
  454. case 'OPCIONES':
  455. $estadistica[$index]['valor'] = Proyecto::estandarizaNumeros($estadistica[$index]['valor']);
  456. break;
  457. }
  458. next($estadistica);
  459. }
  460. if (count($estadistica) > 0){
  461. switch ($tipoRetorno){
  462. case 'HTML':
  463. $div .= '<div class="row justify-content-md-center mb-3">
  464. <div class="col-lg-2 align-self-center display-6 text-primary">' . $pregunta['pregunta'] . '</div>
  465. <div class="estadistica linea position-relative col-lg-7">';
  466. $cont = 0;
  467. foreach ($estadistica as $est){
  468. $div .= '<div class="row align-items-center py-2">
  469. <div class="col-5 d-flex flex-row justify-content-end align-items-center">' . $est['valor'] . '</div>
  470. <div class="col-7 d-flex flex-row justify-content-start align-items-center">
  471. <div class="barra ' . Colores::getBGColor($cont) . '" style="width:' . $est['porcentaje'] .'%"></div>
  472. <div class="indivisa-text-bold pl-2 text-primary">' . $est['cantidad'] .'</div>
  473. </div>
  474. </div>';
  475. $cont++;
  476. }
  477. $div .= '</div>
  478. </div>';
  479. break;
  480. case 'ARRAY':
  481. $temp = array();
  482. array_push($temp,array('texto' => ' ', 'estilo' => 'RUBRO', 'indice' => 0));
  483. array_push($temp,array('texto' => $pregunta['pregunta'], 'estilo' => 'RUBRO', 'indice' => 0));
  484. array_push($temp,array('texto' => 'Porcentaje de votos', 'estilo' => 'AZUL', 'indice' => 1));
  485. array_push($temp,array('texto' => 'No. de votos', 'estilo' => 'AZUL', 'indice' => 2));
  486. array_push($div, $temp);
  487. $cont = 1;
  488. foreach ($estadistica as $est){
  489. $temp = array();
  490. if (is_numeric($est['valor']))
  491. array_push($temp,array('texto' => $est['valor'], 'estilo' => 'RENGLON', 'indice' => $cont%3));
  492. else
  493. array_push($temp,array('texto' => $est['valor'], 'estilo' => 'ESTRELLAS', 'indice' => $cont%3));
  494. array_push($temp,array('texto' => $est['texto'], 'estilo' => 'PERSONA', 'indice' => $cont%3));
  495. array_push($temp,array('texto' => $est['porcentaje'] . '%', 'estilo' => 'RENGLON', 'indice' => $cont%3));
  496. array_push($temp,array('texto' => $est['cantidad'], 'estilo' => 'RENGLON', 'indice' => $cont%3));
  497. array_push($div, $temp);
  498. $cont++;
  499. if ($cont % 3 == 0)
  500. $cont++;
  501. }
  502. break;
  503. }
  504. }
  505. }
  506. return $div;
  507. }
  508. }