functions.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. function ImprimirResultado($Comprobar, &$Puntuaciones) {
  19. $HTML = '';
  20. if ($Comprobar) {
  21. for ($Posicion = 0; $Posicion < count(array_keys($Puntuaciones)); $Posicion++) {
  22. $HTML .= '<div class="row my-1">' . PHP_EOL;
  23. $HTML .= '<div class="col-md-3 col-sm-2 col"></div>' . PHP_EOL;
  24. if ($Posicion == 0) {
  25. $HTML .= '<span class="d-block col-md-6 col-sm-8 col-12 '
  26. . 'bg-success rounded text-white text-center">'
  27. . '<strong>' . array_keys($Puntuaciones)[$Posicion]
  28. . ': </strong>'
  29. . $Puntuaciones[array_keys($Puntuaciones)[$Posicion]]
  30. . '</span>' . PHP_EOL;
  31. } else if ($Posicion == 1) {
  32. $HTML .= '<span class="d-block col-md-6 col-sm-8 col-12 '
  33. . 'bg-danger rounded text-white text-center">'
  34. . '<strong>' . array_keys($Puntuaciones)[$Posicion]
  35. . ': </strong>'
  36. . $Puntuaciones[array_keys($Puntuaciones)[$Posicion]]
  37. . '</span>' . PHP_EOL;
  38. } else {
  39. $HTML .= '<span class="d-block col-md-6 col-sm-8 col-12 '
  40. . 'bg-warning rounded text-white text-center">'
  41. . '<strong>' . array_keys($Puntuaciones)[$Posicion]
  42. . ': </strong>'
  43. . $Puntuaciones[array_keys($Puntuaciones)[$Posicion]]
  44. . '</span>' . PHP_EOL;
  45. }
  46. $HTML .= '<div class="col-md-3 col-sm-2 col"></div>' . PHP_EOL;
  47. $HTML .= '</div>' . PHP_EOL;
  48. }
  49. } else {
  50. $HTML .= '<span class="d-block text-center">--</span>' . PHP_EOL;
  51. }
  52. return $HTML;
  53. }
  54. function ComprobarRespuesta($Pregunta, $RespuestaUsuario, &$Puntuaciones) {
  55. $Respuesta = $Pregunta['respuesta'];
  56. $TextoRespuesta = $Pregunta['opciones'][$Respuesta];
  57. $HTML = '';
  58. if (!is_null($RespuestaUsuario)) {
  59. if ($RespuestaUsuario == $Respuesta) {
  60. $HTML .= '<div class="row bg-success rounded text-white" '
  61. . 'id="comprobacion_' . $Pregunta['numero'] . '">' . PHP_EOL;
  62. $HTML .= '<span class="d-block col-12 text-center my-md-auto '
  63. . 'font-weight-bold">Respuesta correcta.</span>' . PHP_EOL;
  64. $HTML .= '</div>' . PHP_EOL;
  65. $Puntuaciones[array_keys($Puntuaciones)[0]]++;
  66. } else {
  67. $HTML .= '<div class="row bg-danger rounded text-white" '
  68. . 'id="comprobacion_' . $Pregunta['numero'] . '">' . PHP_EOL;
  69. $HTML .= '<span class="d-block col-md-4 col-12 mb-md-0 mb-1 '
  70. . 'my-md-auto text-center font-weight-bold">'
  71. . 'Respuesta incorrecta.</span>' . PHP_EOL;
  72. $HTML .= '<span class="d-block col-md-8 col-12 text-center '
  73. . 'my-md-auto"><strong>La respuesta correcta es: </strong>'
  74. . $TextoRespuesta . '</span>' . PHP_EOL;
  75. $HTML .= '</div>' . PHP_EOL;
  76. $Puntuaciones[array_keys($Puntuaciones)[1]]++;
  77. }
  78. } else {
  79. $HTML .= '<div class="row bg-warning rounded text-white" '
  80. . 'id="comprobacion_' . $Pregunta['numero'] . '">' . PHP_EOL;
  81. $HTML .= '<span class="d-block col-md-4 col-12 mb-md-0 mb-1 '
  82. . 'my-md-auto text-center font-weight-bold">'
  83. . 'Respuesta no contestada.</span>' . PHP_EOL;
  84. $HTML .= '<span class="d-block col-md-8 col-12 text-center '
  85. . 'my-md-auto"><strong>La respuesta correcta es: </strong>'
  86. . $TextoRespuesta . '</span>' . PHP_EOL;
  87. $HTML .= '</div>' . PHP_EOL;
  88. $Puntuaciones[array_keys($Puntuaciones)[2]]++;
  89. }
  90. return $HTML;
  91. }
  92. function CrearOpciones(&$Pregunta, $RespuestaUsuario) {
  93. $HTML = '';
  94. $HTML .= '<div class="col-md-8 col-12 my-auto">' . PHP_EOL;
  95. foreach ($Pregunta['opciones'] as $Posicion => $Opcion) {
  96. $HTML .= '<div class="form-check">' . PHP_EOL;
  97. if (!is_null($RespuestaUsuario) && $RespuestaUsuario == $Posicion) {
  98. $HTML .= '<input class="form-check-input" type="radio" '
  99. . 'name="respuesta_' . $Pregunta['numero'] . '" value="'
  100. . $Posicion . '" id="respuesta_' . $Pregunta['numero']
  101. . '_' . $Posicion . '" checked="" />' . PHP_EOL;
  102. } else {
  103. $HTML .= '<input class="form-check-input" type="radio" '
  104. . 'name="respuesta_' . $Pregunta['numero'] . '" value="'
  105. . $Posicion . '" id="respuesta_' . $Pregunta['numero']
  106. . '_' . $Posicion . '" />' . PHP_EOL;
  107. }
  108. $HTML .= '<label class="form-check-label" for="respuesta_'
  109. . $Pregunta['numero'] . '_' . $Posicion . '">' . $Opcion
  110. . '</label>' . PHP_EOL;
  111. $HTML .= '</div>' . PHP_EOL;
  112. }
  113. $HTML .= '</div>' . PHP_EOL;
  114. return $HTML;
  115. }
  116. function CrearPreguntas(&$Preguntas, &$RespuestasUsuario, $Comprobar,
  117. &$Puntuaciones) {
  118. $HTML = '';
  119. $HTML = '<div class="container mb-3">' . PHP_EOL;
  120. foreach ($Preguntas as $Pregunta) {
  121. $RespuestaUsuario = null;
  122. $HTML .= '<div class="row mb-2 border border-primary rounded p-2">'
  123. . PHP_EOL;
  124. $HTML .= '<div class="container">' . PHP_EOL;
  125. $HTML .= '<div class="row mb-2">' . PHP_EOL;
  126. $HTML .= '<span class="d-block col-lg-1 col-2 my-auto py-1 text-center '
  127. . ' text-white bg-primary rounded-pill font-weight-bold">'
  128. . $Pregunta['numero'] . '</span> ' . PHP_EOL;
  129. $HTML .= '<span class="d-block col-lg-11 col-10 text-center">'
  130. . $Pregunta['pregunta'] . '</span>' . PHP_EOL;
  131. $HTML .= '</div>' . PHP_EOL;
  132. if ($Comprobar) {
  133. $HTML .= '<div class="row mb-2">' . PHP_EOL;
  134. $RespuestaUsuario = $RespuestasUsuario[
  135. 'respuesta_' . $Pregunta['numero']
  136. ];
  137. $HTML .= '<div class="col-md-4 col-12 my-md-auto mb-2">' . PHP_EOL;
  138. $HTML .= '<img class="rounded mx-auto d-block w-100" '
  139. . 'src="' . $Pregunta['imagen'] . '" />' . PHP_EOL;
  140. $HTML .= '</div>' . PHP_EOL;
  141. $HTML .= CrearOpciones($Pregunta, $RespuestaUsuario);
  142. $HTML .= '</div>' . PHP_EOL;
  143. $HTML .= ComprobarRespuesta($Pregunta, $RespuestaUsuario, $Puntuaciones);
  144. } else {
  145. $HTML .= '<div class="row">' . PHP_EOL;
  146. $HTML .= '<div class="col-md-4 col-12 my-md-auto mb-2">' . PHP_EOL;
  147. $HTML .= '<img class="rounded mx-auto d-block w-100" '
  148. . 'src="' . $Pregunta['imagen'] . '" />' . PHP_EOL;
  149. $HTML .= '</div>' . PHP_EOL;
  150. $HTML .= CrearOpciones($Pregunta, $RespuestaUsuario);
  151. $HTML .= '</div>' . PHP_EOL;
  152. }
  153. $HTML .= '</div>' . PHP_EOL;
  154. $HTML .= '</div>' . PHP_EOL;
  155. }
  156. $HTML .= '</div>' . PHP_EOL;
  157. return $HTML;
  158. }