Пример #1
0
    /**
     * @Route("/admin/module/add/module/{courseId}", name = "admin_course_module_add_module")
     * @Template("LearningMainBundle:Admin:course_module_.html.twig")
     */
    public function AdminModuleAddAction($courseId)
    {
        ini_set('memory_limit', -1);
        $request = $this->getRequest();
        $courseModule = new CourseModule();
        $course = $this->getDoctrine()->getRepository('LearningMainBundle:Course')->findOneById($courseId);
        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();
            $type = $request->request->get('type');
            $file = $request->files->get('file_up');
            # проверка, что файл загружен при выборе видео/файла
            if (empty($type) || ($type == 'video' || $type == 'file') && empty($file)) {
                $post_max_size = ini_get('post_max_size');
                $error = 'Не удалось загрузить файл. Сервер принимает файлы размером до ' . $post_max_size;
                return array('course' => $course, 'error' => $error, 'module' => $courseModule);
            }
            $position = $maxPosition == null ? 1 : $maxPosition + 1;
            $courseModule->setPosition($position);
            $courseModule->setTitle($request->request->get('title'));
            $courseModule->setEnabled($request->request->get('enabled'));
            $courseModule->setType($request->request->get('type'));
            $courseModule->setText($request->request->get('text'));
            $courseModule->setTimeToLearn($request->request->get('timeToLearn'));
            /********************************************/
            if ($file != NULL) {
                $name = $this->rus2translit($file->getClientOriginalName());
                $pathinfo = pathinfo($name);
                if (false == in_array($pathinfo['extension'], array('flv', 'pdf'))) {
                    return array('course' => $course, 'module' => $courseModule);
                }
                $name = substr($pathinfo['filename'], 0, 50) . '_' . uniqid() . '.' . $pathinfo['extension'];
                $file->move('upload/files', $name);
                $courseModule->setFile($name);
                # у видео надо сохранить размеры и длительность
                if ($request->request->get('type') == 'video') {
                    $this->setVideoMeta($courseModule, $name);
                }
            }
            /********************************************/
            $courseModule->setCourse($course);
            if ($type == 'text' || $file != NULL) {
                $em->persist($courseModule);
                $em->flush();
                # надо установить родительский ид. для версионности модуля
                $em->refresh($courseModule);
                $courseModule->setParentId($courseModule->getId());
                $em->flush($courseModule);
            } else {
                $error = 'Ошибка сохранения: необходимо загрузить pdf-файл или flv-видео';
                return array('course' => $course, 'error' => $error, 'module' => $courseModule);
            }
            $this->get('session')->getFlashBag()->add('popup', 'Добавлен');
            return $this->redirect($this->generateUrl('admin_course_edit', array('courseId' => $courseId)));
        }
        return array('course' => $course);
    }
Пример #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 />";
     ////			}
     //		}
 }