/** * Проверка теста * @Route("/demo_check_test/{moduleId}", name = "demo_check_test") * @Template("LearningMainBundle:Education:demo_test_failed.html.twig") */ public function demo_check_test(Request $request, $moduleId) { if ($request->getMethod() != 'POST') { throw $this->createNotFoundException(); } $em = $this->getDoctrine()->getManager(); #$userCourse = $em->getRepository('LearningMainBundle:UserCourse')->findOneById(36); $data = new \DateTime(); $userCourse = new UserCourse(); $userCourse->setBallsRoh(0); $userCourse->setCourse($this->getDoctrine()->getRepository('LearningMainBundle:Course')->findOneById(47)); $userCourse->setModules(array(0 => 71, 1 => 80)); $userCourse->setCountModules(2); $userCourse->setPosition(2); $userCourse->setBallsRoh(0); $userCourse->setPassed(false); $userCourse->setStarts(new \DateTime()); $userCourse->setEnds(new \DateTime('+ 1 Month')); #$this->checkAccessUserCourse($userCourse); $module = $em->getRepository('LearningMainBundle:CourseModule')->findOneById($moduleId); $questioned = $em->getRepository('LearningMainBundle:CourseModule')->countQuestions($moduleId); $repo = $em->getRepository('LearningMainBundle:CourseAnswer'); $questions = $request->request->get('questions'); if ($questioned != count($questions)) { # если студент сжульничал и удалил часть вопросов перед отправкой $referer = $request->headers->get('referer'); return new RedirectResponse($referer); } # выясняем, на сколько вопросов студент ответил и на сколько не ответил $answered = $failed = 0; foreach ($questions as $questionId => $answers) { $answersPost = array_keys($answers); $answersCorrect = $repo->correctIdsOfQuestion($questionId); $diff = array_diff($answersPost, $answersCorrect); if (empty($diff)) { $answered++; } else { $failed++; } } # эффективность прохождения теста $evaluation = $answered / $questioned * 100; $userTest = new UserTest(); $userTest->setUserCourse($userCourse); $userTest->setModule($module); $userTest->setQuestioned($questioned); $userTest->setAnswered($answered); $userTest->setFailed($failed); $userTest->setEvaluation($evaluation); # надо так же указать, какой раз проходится этот тест #$tries = $em->getRepository('LearningMainBundle:UserTest')->countTries($userCourseId, $moduleId); #$userTest->setTries($tries + 1); if ($failed > $module->getWrong()) { # не прошел return array('userCourse' => $userCourse, 'userTest' => $userTest, 'module' => $module); } else { # прошел тест $userTest->setPassed(new \DateTime()); $userCourse->addPassedTests(); $userCourse->addEvaluation($evaluation); return $this->render('LearningMainBundle:Education:demo_success.html.twig'); } }