/** * Export metadata of the Exercise in a JSON-encodable format. * * @param Exercise $exercise * * @return array */ private function exportMetadata(Exercise $exercise) { $node = $exercise->getResourceNode(); $creator = $node->getCreator(); $authorName = sprintf('%s %s', $creator->getFirstName(), $creator->getLastName()); // Accessibility dates $startDate = $node->getAccessibleFrom() ? $node->getAccessibleFrom()->format('Y-m-d\\TH:i:s') : null; $endDate = $node->getAccessibleUntil() ? $node->getAccessibleUntil()->format('Y-m-d\\TH:i:s') : null; $correctionDate = $exercise->getDateCorrection() ? $exercise->getDateCorrection()->format('Y-m-d\\TH:i:s') : null; return ['authors' => [['name' => $authorName]], 'created' => $node->getCreationDate()->format('Y-m-d\\TH:i:s'), 'title' => $node->getName(), 'description' => $exercise->getDescription(), 'type' => $exercise->getType(), 'pick' => $exercise->getPickSteps(), 'random' => $exercise->getShuffle(), 'keepSteps' => $exercise->getKeepSteps(), 'maxAttempts' => $exercise->getMaxAttempts(), 'lockAttempt' => $exercise->getLockAttempt(), 'dispButtonInterrupt' => $exercise->getDispButtonInterrupt(), 'metadataVisible' => $exercise->isMetadataVisible(), 'statistics' => $exercise->hasStatistics(), 'anonymous' => $exercise->getAnonymous(), 'duration' => $exercise->getDuration(), 'markMode' => $exercise->getMarkMode(), 'correctionMode' => $exercise->getCorrectionMode(), 'correctionDate' => $correctionDate, 'startDate' => $startDate, 'endDate' => $endDate, 'published' => $node->isPublished(), 'publishedOnce' => $exercise->wasPublishedOnce(), 'minimalCorrection' => $exercise->isMinimalCorrection()]; }
/** * Creates a new exercise paper for a given user. * * @param Exercise $exercise * @param User $user * * @return Paper */ public function createPaper(Exercise $exercise, User $user = null) { // Get the number of the new Paper $paperNum = 1; if ($user) { $lastPaper = $this->om->getRepository('UJMExoBundle:Paper')->findOneBy(['user' => $user, 'exercise' => $exercise], ['start' => 'DESC']); if ($lastPaper) { $paperNum = $lastPaper->getNumPaper() + 1; } } // Generate the list of Steps and Questions for the Paper $order = ''; if (!empty($lastPaper) && $exercise->getKeepSteps()) { // Get steps order from the last user Paper $order = $lastPaper->getOrdreQuestion(); } else { // Generate paper step order $questions = $this->pickQuestions($exercise); foreach ($questions as $question) { $order .= $question->getId() . ';'; } } // Create the new Paper entity $paper = new Paper(); $paper->setExercise($exercise); $paper->setUser($user); $paper->setNumPaper($paperNum); $paper->setOrdreQuestion($order); $paper->setAnonymous($exercise->getAnonymous()); $this->om->persist($paper); $this->om->flush(); return $paper; }