public function actionWriteForUnit($id)
 {
     $unit = $this->setupCourseRegistry($this->unitRepository->find($id));
     $this->deliver($unit);
     if (!$unit->isCurrentPhase(Unit::REVIEWS) and !$this->user->isAllowed('review', 'writeAnytime')) {
         throw new \Nette\Application\BadRequestException('Forbidden', 403);
     }
     $this->template->uploadPath = $this->uploadStorage->path;
     $this->deliver($unit->course);
     $solution = null;
     try {
         // needs splitting into readable methods
         $reviewer = $this->userRepository->find($this->user->id);
         if (!($review = $this->reviewRepository->findUnfinishedReview($unit, $reviewer))) {
             if ($solution = $this->solutionRepository->findSolutionToReview($unit, $reviewer)) {
                 $review = $this->reviewRepository->createReview($solution, $reviewer);
                 $review = $this->reviewRepository->find($review->id);
                 // fetching all columns
                 $this->logEvent($review, 'create');
             } else {
                 return;
             }
         } else {
             $solution = $review->solution;
         }
         $this->template->review = $this->setupCourseRegistry($review);
         $this->template->solution = $solution;
         $assignment = $solution->assignment;
         $this->template->assignment = $this->produce($assignment);
     } catch (\Model\Repository\SolutionToReviewNotFoundException $e) {
         $this->template->message = $this->translator->translate('messages.unit.noSolutionAvailable');
     } catch (\Model\Repository\ReviewLimitReachedException $e) {
         $this->template->message = $this->translator->translate('messages.unit.enoughReviews', NULL, array('count' => $unit->course->reviewCount));
     }
 }
 public function actionDefault($id, $lateEdits = FALSE)
 {
     $info = $this->courseRegistry;
     $unit = $this->unitRepository->find($id);
     $info->insert($unit);
     $product = $this->produce($unit);
     if (!$product->hasBeenPublished()) {
         throw new \Nette\Application\BadRequestException(NULL, 404);
         return;
     }
     $assignment = $this->assignmentRepository->findByUnitAndUser($unit, $this->userInfo);
     if (!$assignment) {
         $assignment = $this->courseFactory->assembleAssignment($unit);
         $assignment->student = $this->userInfo;
         $this->assignmentRepository->persist($assignment);
     }
     $this->template->assignment = $this->courseFactory->produceAssignment($assignment);
     $info->insert($assignment);
     if (isset($assignment->solution)) {
         $info->setSolution($assignment->solution);
         $this->template->solution = $assignment->solution;
     } else {
         $this->template->solution = null;
     }
     $this->template->lateEdits = $lateEdits;
     $this->template->isFavorited = $unit->isFavoritedBy($this->userInfo);
     $this->logEvent($unit, 'open');
     $this['questionsRenderer']->assignment = $this->produce($assignment);
     $this['questionsRenderer']->solution = $this->courseRegistry->solution;
     $this['questionsRenderer']->form = $this['assignmentForm'];
     $this['questionsRenderer']->lateEdits = $lateEdits;
 }