Exemplo n.º 1
0
 /**
  * @Route("/{id}/upload-test", name="project_course_upload_test")
  */
 public function uploadTestAction($id, Request $request)
 {
     try {
         $repository = $this->crudInfo->getRepository();
         $item = $repository->getItem($id);
         $form = $this->createForm(CourseTestUploadForm::class);
         $form->handleRequest($request);
         if ($form->isValid()) {
             $data = $form->getData();
             if (!$data['file'] instanceof UploadedFile) {
                 return $this->showPageWithError($this->trans('An error occurred during uploading the test questions.'), $this->crudInfo->getInfoPage(), array('id' => $id, 'slug' => $this->getSlug()));
             }
             if ($data['file']->getMimeType() != 'application/xml') {
                 return $this->showPageWithError($this->trans('Please upload an XML file!'), $this->crudInfo->getInfoPage(), array('id' => $id, 'slug' => $this->getSlug()));
             }
             $content = file_get_contents($data['file']->getRealPath());
             $item->createTest($content);
             $this->verifyFileCorrectness($item->getTest());
             $repository->saveTest($item);
             return $this->showPageWithMessage($this->trans('The course test questions have been uploaded correctly.'), $this->crudInfo->getInfoPage(), array('id' => $id, 'slug' => $this->getSlug()));
         }
         $this->breadcrumbs()->link($item->getName(), 'project_course_info', ['id' => $item->getId(), 'slug' => $this->getSlug()])->link($this->trans('Upload test'), 'project_course_upload_test', ['id' => $item->getId(), 'slug' => $this->getSlug()]);
         return $this->render($this->crudInfo->getTemplateLocation() . 'upload-test.html.twig', array('pageTitle' => $this->crudInfo->getPageTitle(), 'pageSubtitle' => $this->crudInfo->getPageSubtitle(), 'item' => $item, 'form' => $form->createView()));
     } catch (ItemNotFoundException $exception) {
         return $this->showPageWithError($this->crudInfo->getItemNotFoundErrorMessage(), $this->crudInfo->getIndexPage());
     } catch (ModelException $exception) {
         return $this->showPageWithError($this->trans($exception->getMessage()), $this->crudInfo->getIndexPage());
     }
 }