Exemplo n.º 1
0
    /**
     * @Route("/admin/module/add/test/{courseId}", name = "admin_course_module_add_test")
     * @Template("LearningMainBundle:Admin:course_module_test_.html.twig")
     */
    public function AdminModuleAddTestAction($courseId)
    {
        $course = $this->getDoctrine()->getRepository('LearningMainBundle:Course')->findOneById($courseId);
        $request = $this->getRequest();
        if ($request->isMethod('POST')) {
            $em = $this->getDoctrine()->getManager();
            $maxPosition = $em->createQuery('
				SELECT MAX(m.position)
				FROM LearningMainBundle:CourseModule m
				WHERE m.course = :courseId
			')->setParameter('courseId', $courseId)->getSingleScalarResult();
            $position = $maxPosition == null ? 1 : $maxPosition + 1;
            $module = new CourseModule();
            $module->setTitle($request->request->get('title'));
            $module->setText($request->request->get('text'));
            $module->setType('test');
            $module->setPosition($position);
            $module->setCourse($course);
            $module->setWrong($request->request->get('wrong'));
            $module->setTimeToLearn($request->request->get('timeToLearn'));
            $em->persist($module);
            $em->flush($module);
            # надо установить родительский ид. для версионности модуля
            $em->refresh($module);
            $module->setParentId($module->getId());
            $em->flush($module);
            $question = array();
            $answer = array();
            $answerTrue = $request->request->get('answerTrue');
            foreach ($request->request->get('quest') as $key => $value) {
                $question[$key] = new CourseQuestion();
                $question[$key]->setTitle($value);
                $question[$key]->setModule($module);
                $answer[] = array();
                $em->persist($question[$key]);
                $em->flush($question[$key]);
                $em->refresh($question[$key]);
                foreach ($request->request->get('answer')[$key] as $skey => $svalue) {
                    $answer[$key][$skey] = new CourseAnswer();
                    $answer[$key][$skey]->setText($svalue);
                    $answer[$key][$skey]->setQuestion($question[$key]);
                    if (isset($answerTrue[$key][$skey])) {
                        $answer[$key][$skey]->setCorrect(true);
                    }
                    $em->persist($answer[$key][$skey]);
                    $em->flush($answer[$key][$skey]);
                    $em->refresh($answer[$key][$skey]);
                }
            }
            $em->flush();
            $this->get('session')->getFlashBag()->add('popup', 'Добавлен');
            return $this->redirect($this->generateUrl('admin_course_edit', array('courseId' => $courseId)) . '#lists');
        }
        return array('course' => $course);
    }
Exemplo n.º 2
0
 /**
  * @Route("/admin/generate-sql-for-test/{testId}/{courseId}")
  */
 public function generateSqlAction($testId, $courseId)
 {
     $course = $this->getDoctrine()->getRepository('LearningMainBundle:Course')->findOneById($courseId);
     $oldTest = $this->getDoctrine()->getRepository('LearningMainBundle:CourseModule')->findOneById($testId);
     $test = new CourseModule();
     $test->setTitle($oldTest->getTitle());
     $test->setCreated(new \DateTime());
     $test->setUpdated(new \DateTime());
     $test->setStep(true);
     $test->setCourse($course);
     $test->setType($oldTest->getType());
     $test->setPosition($oldTest->getPosition());
     $this->getDoctrine()->getManager()->persist($test);
     $this->getDoctrine()->getManager()->flush($test);
     $this->getDoctrine()->getManager()->refresh($test);
     foreach ($oldTest->getQuestions() as $oldQuestion) {
         $question = new CourseQuestion();
         $question->setTitle($oldQuestion->getTitle());
         $question->setPosition($oldQuestion->getPosition());
         $question->setCreated(new \DateTime());
         $question->setUpdated(new \DateTime());
         $question->setModule($test);
         $this->getDoctrine()->getManager()->persist($question);
         $this->getDoctrine()->getManager()->flush($question);
         $this->getDoctrine()->getManager()->refresh($question);
         foreach ($oldQuestion->getAnswers() as $oldAnswer) {
             $answer = new CourseAnswer();
             $answer->setCreated(new \DateTime());
             $answer->setUpdated(new \DateTime());
             $answer->setText($oldAnswer->getText());
             $answer->setCorrect($oldAnswer->isCorrect());
             $answer->setQuestion($question);
             $this->getDoctrine()->getManager()->persist($answer);
             $this->getDoctrine()->getManager()->flush($answer);
             $this->getDoctrine()->getManager()->refresh($answer);
         }
     }
     return new Response('Копирование завершено');
     //		echo "INSERT coursemodule SET course_id=$newCourseId, title='2', type='test', position='1', isStep=1, timeToLearn=0, created='2016-01-14 00:00:00', updated='2016-01-14 00:00:00'<br />";
     //		foreach ($test->getQuestions() as $question){
     //			echo "INSERT coursequestion SET module_id={moduleId}, title='".$question->getTitle()."', position='".$question->getPosition()."', enabled=1, created='2016-01-14 00:00:00', updated='2016-01-14 00:00:00'<br />";
     ////			foreach ($question->getAnswers as $answer) {
     ////				echo "INSERT courseanswer SET module_id={moduleId}, title='".$question->getTitle()."', position='".$question->getPosition()."', enabled=1, created='2016-01-14 00:00:00', updated='2016-01-14 00:00:00'<br />";
     ////			}
     //		}
 }