src/Controller/QuizController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Answear;
  4. use App\Entity\MemoryGame;
  5. use App\Entity\Question;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\StreamedResponse;
  13. /**
  14. * Class QuizController
  15. * @package App\Controller
  16. * @Route("/quiz")
  17. */
  18. class QuizController extends AbstractController
  19. {
  20. public $questionCount = 6;
  21. /**
  22. * @Route("/", name="app_quiz_info")
  23. */
  24. public function info(): Response
  25. {
  26. $quizNr = $this->getQuizNr();
  27. $em = $this->getDoctrine()->getManager();
  28. $answear = $em
  29. ->getRepository(Answear::class)
  30. ->findOneBy(['user' => $this->getUser()]);
  31. if ($answear instanceof Answear && $answear->getEnd()) {
  32. $memory = $em
  33. ->getRepository(MemoryGame::class)
  34. ->findOneBy(['user' => $this->getUser()]);
  35. if ($memory instanceof MemoryGame && $memory->getEnd()) {
  36. return new RedirectResponse($this->get('router')->generate('app_summary'));
  37. } else {
  38. $points = $answear->getPoints();
  39. //$time = $answear->getEnd() - $answear->getStart();
  40. //$place = $em->getRepository(Answear::class)->getMyPosition($quizNr, $this->getUser()->getId());
  41. $ansU = [];
  42. $questions = [];
  43. foreach ($em->getRepository(Question::class)->findBy(['quiz' =>$answear->getQuiz()], ['number' => 'ASC']) as $q) {
  44. $questions[$q->getNumber()] = $q;
  45. }
  46. $num = unserialize($answear->getQuestionsStart());
  47. $ans = unserialize($answear->getAnswear());
  48. foreach ($num as $k=>$n) {
  49. if ($questions[$n]->getGoodAnswear() == $ans[$k]) {
  50. $ansU[$k] = 1;
  51. }
  52. else {
  53. $ansU[$k] = 0;
  54. }
  55. }
  56. // $answears = $em
  57. // ->getRepository(Answear::class)
  58. // ->findBy(['user' => $this->getUser()]);
  59. // $lastAns =[];
  60. // foreach($answears as $a) {
  61. // if($a->getQuiz()!=$quizNr) {
  62. // $lastAns[$a->getQuiz()]['quiz'] = $a->getQuiz();
  63. // $lastAns[$a->getQuiz()]['points'] = $a->getPoints();
  64. // $lastAns[$a->getQuiz()]['time'] = round($a->getEnd() - $a->getStart(),3);
  65. // $lastAns[$a->getQuiz()]['place']= $em->getRepository(Answear::class)->getMyPosition($a->getQuiz(), $this->getUser()->getId());
  66. // }
  67. // }
  68. return $this->render('page/quiz.html.twig', [
  69. 'quiz' => $quizNr,
  70. 'points' => $points,
  71. // 'time' => round($time, 3),
  72. //'place' => $place,
  73. // 'lastAns' => $lastAns,
  74. 'answear' => $answear,
  75. 'ansU' => $ansU
  76. ]);
  77. }
  78. } else {
  79. return $this->render('page/start-quiz.html.twig', [
  80. 'answear' => $answear,
  81. 'questionCount' => $this->questionCount,
  82. ]);
  83. }
  84. }
  85. /**
  86. * @Route("/start", name="app_quiz_start", methods={"POST","PUT"})
  87. */
  88. public function start(Request $request): Response
  89. {
  90. $submittedToken = $request->request->get('tkn');
  91. if ($this->isCsrfTokenValid('quiz', $submittedToken)) {
  92. } else {
  93. return new JsonResponse(['data' => ['error' => 'Wystąpił błąd autoryzacji. Spróbuj ponownie.']]);
  94. }
  95. if(date('Y-m-d') >='2026-06-15') {
  96. return new JsonResponse(['data' => ['error' => 'Konkurs zakończony. Dziękujemy za udział w zabawie.']]);
  97. }
  98. $em = $this->getDoctrine()->getManager();
  99. $quizNr = $this->getQuizNr();
  100. $answear = $em
  101. ->getRepository(Answear::class)
  102. ->findOneBy(['user' => $this->getUser(),'quiz' => $quizNr]);
  103. $ret = [];
  104. $qS = [];
  105. // $factory = new PasswordHasherFactory([
  106. // 'common' => ['algorithm' => 'bcrypt'],
  107. // ]);
  108. // $hasher = $factory->getPasswordHasher('common');
  109. if ($answear instanceof Answear) {
  110. $questions = $em
  111. ->getRepository(Question::class)
  112. ->findBy(['quiz' => $answear->getQuiz()]);
  113. if ($answear->getEnd() > 0) {
  114. return new JsonResponse(['data' => ['error' => 'Ten quiz został już przez Ciebie rozwiązany.']]);
  115. } else {
  116. $qS = unserialize($answear->getQuestionsStart());
  117. if($qS == false || count($qS)) {
  118. $ans = unserialize($answear->getAnswear());
  119. if(count($qS)>$this->questionCount) {
  120. return new JsonResponse(['data' => ['error' => 'Wystąpił nieoczekiwany błąd. Spróbuj ponownie.']]);
  121. }
  122. if($ans == false || count($ans) != count($qS) ) {
  123. foreach ($questions as $q) {
  124. if($qS == false || end($qS) == $q->getNumber()) {
  125. //$ans = array_merge([$q->getGoodAnswear()], explode('|', $q->getBadAnswears()));
  126. $ans = $this->_createHash($q);
  127. shuffle($ans);
  128. $ret = [
  129. 'q' => $q->getContent(),
  130. // 'g' => $q->getGoodAnswear(),
  131. 'a' => $ans,
  132. 'n' => count($qS),
  133. // 'p' => $hasher->verify('$2y$13$DZ7nwFg/Ma.PRGdEhFMBTuV84ZfJi3stYKiETAVFppK2fZC/b4Xyi', $q->getGoodAnswear())
  134. ];
  135. }
  136. }
  137. } else {
  138. }
  139. } else {
  140. return new JsonResponse(['data' => ['error' => 'Wystąpił nieoczekiwany błąd. Jeżeli problem się powtarza zgłoś się do BOK.']]);
  141. }
  142. }
  143. } else {
  144. // verify that a given string matches the hash calculated above
  145. // $hasher->verify($hash, 'invalid'); // false
  146. //$quizNr = rand(1,7);
  147. $questions = $em
  148. ->getRepository(Question::class)
  149. ->findBy(['quiz' => $quizNr]);
  150. shuffle($questions);
  151. $q = array_slice($questions,0,1)[0];
  152. $qS[] = "".$q->getNumber();
  153. // $ans = array_merge([$q->getGoodAnswear().'['.$hasher->hash($q->getGoodAnswear()).']'], explode('|', $q->getBadAnswears()));
  154. //$ans = array_merge([$q->getGoodAnswear()], explode('|', $q->getBadAnswears()));
  155. $ans = $this->_createHash($q);
  156. shuffle($ans);
  157. $ret = [
  158. 'q' => $q->getContent(),
  159. // 'g' => $q->getGoodAnswear(),
  160. 'a' => $ans,
  161. 'n' => 1,
  162. ];
  163. $answear = new Answear();
  164. $answear->setUser($this->getUser())
  165. ->setStart(microtime(true))
  166. ->setQuiz($quizNr)
  167. ->setQuestionsStart(serialize($qS));
  168. $em->persist($answear);
  169. $em->flush();
  170. }
  171. return new JsonResponse(['data' => $ret]);
  172. }
  173. /**
  174. * @Route("/next", name="app_quiz_next", methods={"POST","PUT"})
  175. */
  176. public function next(Request $request): Response
  177. {
  178. $submittedToken = $request->request->get('tkn');
  179. if ($this->isCsrfTokenValid('quiz', $submittedToken)) {
  180. } else {
  181. return new JsonResponse(['data' => ['error' => 'Wystąpił błąd autoryzacji. Spróbuj ponownie.']]);
  182. }
  183. if(date('Y-m-d') >='2026-06-15') {
  184. return new JsonResponse(['data' => ['error' => 'Konkurs zakończony. Dziękujemy za udział w zabawie.']]);
  185. }
  186. $em = $this->getDoctrine()->getManager();
  187. // $quizNr = $this->getQuizNr();
  188. $ret = [];
  189. $answear = $em
  190. ->getRepository(Answear::class)
  191. ->findOneBy(['user' => $this->getUser()]);
  192. if ($answear instanceof Answear) {
  193. if ($answear->getEnd() > 0) {
  194. return new JsonResponse(['data' => ['error' => 'Ten quiz został już przez Ciebie rozwiązany.']]);
  195. } else {
  196. $userAnswear = $request->request->get('ans');
  197. $questions = $em
  198. ->getRepository(Question::class)
  199. ->findBy(['quiz' => $answear->getQuiz()]);
  200. $ansTxt = 'send:'.$userAnswear;
  201. foreach($questions as $q) {
  202. $ans = array_merge([$q->getGoodAnswear()], explode('|',$q->getBadAnswears()));
  203. foreach($ans as $i=>$a){
  204. if(md5($a.$this->getUser()->getId()) === $userAnswear) {
  205. $ansTxt = $a;
  206. continue;
  207. }
  208. }
  209. }
  210. $latestAnswears = unserialize($answear->getAnswear());
  211. if($latestAnswears == false) $latestAnswears = [];
  212. $answear->setAnswear(serialize(array_merge($latestAnswears,[$ansTxt])));
  213. $qS = unserialize($answear->getQuestionsStart());
  214. if(count($qS)>5) {
  215. return new JsonResponse(['data' => ['error' => 'Ten quiz został już przez Ciebie rozwiązany.']]);
  216. }
  217. shuffle($questions);
  218. foreach ($questions as $q) {
  219. if(!in_array($q->getNumber(),$qS)) {
  220. $qS[] = "".$q->getNumber();
  221. // $ans = array_merge([$q->getGoodAnswear()], explode('|', $q->getBadAnswears()));
  222. $ans = $this->_createHash($q);
  223. shuffle($ans);
  224. $ret = [
  225. 'q' => $q->getContent(),
  226. // 'g' => $q->getGoodAnswear(),
  227. 'a' => $ans,
  228. 'n' => count($qS)
  229. ];
  230. break;
  231. }
  232. }
  233. $answear ->setQuestionsStart(serialize($qS));
  234. $em->flush();
  235. }
  236. } else {
  237. return new JsonResponse(['data' => ['error' => 'Quiz nie został rozpoczęty.']]);
  238. }
  239. return new JsonResponse(['data' => $ret]);
  240. }
  241. /**
  242. * @Route("/end", name="app_quiz_end", methods={"POST","PUT"})
  243. */
  244. public function end(Request $request): Response
  245. {
  246. $submittedToken = $request->request->get('tkn');
  247. if ($this->isCsrfTokenValid('quiz', $submittedToken)) {
  248. } else {
  249. return new JsonResponse(['data' => ['error' => 'Wystąpił błąd autoryzacji. Spróbuj ponownie.']]);
  250. }
  251. if(date('Y-m-d') >='2026-06-15') {
  252. return new JsonResponse(['data' => ['error' => 'Konkurs zakończony. Dziękujemy za udział w zabawie.']]);
  253. }
  254. $em = $this->getDoctrine()->getManager();
  255. // $quizNr = $this->getQuizNr();
  256. $answear = $em
  257. ->getRepository(Answear::class)
  258. ->findOneBy(['user' => $this->getUser()]);
  259. if ($answear instanceof Answear) {
  260. if ($answear->getEnd() > 0) {
  261. return new JsonResponse(['data' => ['error' => 'Ten quiz został już przez Ciebie rozwiązany.']]);
  262. }
  263. $endTime = microtime(true);
  264. $questions = [];
  265. foreach ($em->getRepository(Question::class)->findBy(['quiz' => $answear->getQuiz()], ['number' => 'ASC']) as $q) {
  266. $questions[$q->getNumber()] = $q;
  267. }
  268. $userAnswear = $request->request->get('ans');
  269. $ansTxt = 'send:'.$userAnswear;
  270. foreach($questions as $q) {
  271. $ans = array_merge([$q->getGoodAnswear()], explode('|',$q->getBadAnswears()));
  272. foreach($ans as $i=>$a){
  273. if(md5($a.$this->getUser()->getId()) === $userAnswear) {
  274. $ansTxt = $a;
  275. continue;
  276. }
  277. }
  278. }
  279. $latestAnswears = unserialize($answear->getAnswear());
  280. if($latestAnswears == false) $latestAnswears = [];
  281. $ans = array_merge($latestAnswears,[$ansTxt]);
  282. if(count($ans)!=$this->questionCount) {
  283. return new JsonResponse(['data' => ['error' => 'Proszę dokończyć quiz.']]);
  284. }
  285. $points = 0;
  286. $num = unserialize($answear->getQuestionsStart());
  287. foreach ($num as $k=>$n) {
  288. if ($questions[$n]->getGoodAnswear() == $ans[$k]) {
  289. $points += 100;
  290. };
  291. }
  292. $answear->setAnswear(serialize($ans))
  293. ->setQuestions(serialize($num))
  294. ->setEnd($endTime)
  295. ->setPoints($points);
  296. $em->flush();
  297. //$place = $em->getRepository(Answear::class)->getMyPosition($quizNr, $this->getUser()->getId());
  298. // $ret = ['points' => $points, 'place' => $place];
  299. $ret = ['points' => $points];
  300. } else {
  301. // errorr kończy a nie zaczął!!!
  302. $ret = ['error' => 'Wystąpił nieoczekiwany błąd. Spróbuj jeszcze raz.'];
  303. }
  304. return new JsonResponse(['data' => $ret]);
  305. }
  306. protected function getQuizNr()
  307. {
  308. $now = date('Y-m-d');
  309. if ($now < '2026-06-08') {
  310. return 1;
  311. } else if ($now >= '2026-05-18' && $now < '2026-05-25') {
  312. return 2;
  313. } else if ($now >= '2026-05-25' && $now < '2026-06-01') {
  314. return 3;
  315. } else if ($now >= '2026-06-01') {
  316. return 4;
  317. }
  318. return 1;
  319. }
  320. /**
  321. * @Route("/ansimg/{hash}", name="app_quiz_img", methods={"GET"})
  322. */
  323. public function image(Request $request, $hash): Response
  324. {
  325. $txt = '';
  326. $em = $this->getDoctrine()->getManager();
  327. // $quizNr = $this->getQuizNr();
  328. $questions = $em
  329. ->getRepository(Question::class)
  330. ->findAll();
  331. foreach($questions as $q) {
  332. $ans = array_merge([$q->getGoodAnswear()], explode('|',$q->getBadAnswears()));
  333. foreach($ans as $i=>$a){
  334. if(md5($a.$this->getUser()->getId()) === $hash) {
  335. $txt = $a;
  336. continue;
  337. }
  338. }
  339. }
  340. putenv('GDFONTPATH=' . realpath('.'));
  341. $im = @imagecreate(300, 24)
  342. or die("Cannot Initialize new GD image stream");
  343. $bg = imagecolorallocate($im, 255, 255, 255);
  344. $text_color = imagecolorallocate($im, 0, 0, 0);
  345. $font = '/UI/img/euro24/font/UEFAEuro-LightNarrow.ttf';
  346. // imagestring($im, 20, 0, 0, $txt, $text_color);
  347. imagettftext($im, 14, 0, 0, 20, $text_color, $font, $txt);
  348. imagecolortransparent($im,$bg);
  349. imagedestroy($im);
  350. return new StreamedResponse(fn () => imagepng($im), 200, ['Content-Type' => 'image/png']);
  351. }
  352. private function _createHash($question) {
  353. $ans = array_merge([$question->getGoodAnswear()], explode('|',$question->getBadAnswears()));
  354. foreach($ans as $i=>$v){
  355. $ans[$i] = md5($v.$this->getUser()->getId());
  356. }
  357. return $ans;
  358. }
  359. }