/**
  * @param string $permission
  *
  * @param Exercise $exo
  *
  * @return bool
  */
 protected function isUserGranted($permission, Exercise $exo, $collection = null)
 {
     if ($collection === null) {
         $collection = new ResourceCollection(array($exo->getResourceNode()));
     }
     $checkPermission = false;
     if ($this->get('security.authorization_checker')->isGranted($permission, $collection)) {
         $checkPermission = true;
     }
     return $checkPermission;
 }
 /**
  * Create an Exercise
  *
  * @access private
  *
  * @param \UJM\ExoBundle\Entity\Exercise $exercise exercise to create
  */
 private function onSuccess(Exercise $exercise)
 {
     // \ pour instancier un objet du namespace global et non pas de l'actuel
     $exercise->setDateCreate(new \Datetime());
     $exercise->setNbQuestionPage(1);
     $this->em->persist($exercise);
     $this->em->flush();
     if ($this->action == 'add') {
         $subscription = new Subscription($this->user, $exercise);
         $subscription->setAdmin(1);
         $subscription->setCreator(1);
         $this->em->persist($subscription);
         $this->em->flush();
     }
 }
 /**
  * The user must be registered (and the dates must be good or the user must to be admin for the exercise)
  *
  * @access public
  *
  * @param boolean $exoAdmin
  * @param \UJM\ExoBundle\Entity\Exercise $exercise
  *
  * @return boolean
  */
 public function controlDate($exoAdmin, $exercise)
 {
     if ($exercise->getStartDate()->format('Y-m-d H:i:s') <= date('Y-m-d H:i:s') && ($exercise->getUseDateEnd() == 0 || $exercise->getEndDate()->format('Y-m-d H:i:s') >= date('Y-m-d H:i:s')) || $exoAdmin === true) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * return steps of an exercise in an array.
  *
  * @param Exercise $object
  * @param string   $exoTitle
  * @param array    $files
  *
  * @return array
  */
 private function getStepsToExport(Exercise $object, $exoTitle, array &$files)
 {
     /** @var \UJM\ExoBundle\Repository\QuestionRepository $questionRepo */
     $questionRepo = $this->om->getRepository('UJMExoBundle:Question');
     $steps = [];
     foreach ($object->getSteps() as $step) {
         $s = ['text' => $step->getText(), 'order' => $step->getOrder(), 'shuffle' => $step->getShuffle(), 'nbQuestion' => $step->getNbQuestion(), 'keepSameQuestion' => $step->getKeepSameQuestion(), 'duration' => $step->getDuration(), 'maxAttempts' => $step->getMaxAttempts()];
         $steps[] = $s;
         // TODO : do not load the Questions from DB they already are in `$step->getStepQuestions()`
         $questions = $questionRepo->findByStep($step);
         $this->qtiService->createQuestionsDirectory($questions, $step->getOrder());
         $dirs = $this->qtiService->sortPathOfQuestions($this->qtiRepository, $step->getOrder());
         $i = 'a';
         foreach ($dirs as $dir) {
             $iterator = new \DirectoryIterator($dir);
             /** @var \DirectoryIterator $element */
             foreach ($iterator as $element) {
                 if (!$element->isDot() && $element->isFile()) {
                     $localPath = 'qti/' . $exoTitle . '/' . $step->getOrder() . '/' . $step->getOrder() . '_question_' . $i . '/' . $element->getFilename();
                     $files[$localPath] = $element->getPathname();
                 }
             }
             $i .= 'a';
         }
     }
     return $steps;
 }
 public function onCopy(CopyResourceEvent $event)
 {
     $em = $this->container->get('doctrine.orm.entity_manager');
     $resource = $event->getResource();
     $exerciseToCopy = $em->getRepository('UJMExoBundle:Exercise')->find($resource->getId());
     $listQuestionsExoToCopy = $em->getRepository('UJMExoBundle:ExerciseQuestion')->findBy(array('exercise' => $exerciseToCopy->getId()));
     $newExercise = new Exercise();
     $newExercise->setName($resource->getName());
     $newExercise->setTitle($exerciseToCopy->getTitle());
     $newExercise->setDescription($exerciseToCopy->getDescription());
     $newExercise->setShuffle($exerciseToCopy->getShuffle());
     $newExercise->setNbQuestion($exerciseToCopy->getNbQuestion());
     $newExercise->setDateCreate($exerciseToCopy->getDateCreate());
     $newExercise->setDuration($exerciseToCopy->getDuration());
     $newExercise->setNbQuestionPage($exerciseToCopy->getNbQuestionPage());
     $newExercise->setDoprint($exerciseToCopy->getDoprint());
     $newExercise->setMaxAttempts($exerciseToCopy->getMaxAttempts());
     $newExercise->setCorrectionMode($exerciseToCopy->getCorrectionMode());
     $newExercise->setDateCorrection($exerciseToCopy->getDateCorrection());
     $newExercise->setMarkMode($exerciseToCopy->getMarkMode());
     $newExercise->setStartDate($exerciseToCopy->getStartDate());
     $newExercise->setUseDateEnd($exerciseToCopy->getUseDateEnd());
     $newExercise->setEndDate($exerciseToCopy->getEndDate());
     $newExercise->setDispButtonInterrupt($exerciseToCopy->getDispButtonInterrupt());
     $newExercise->setLockAttempt($exerciseToCopy->getLockAttempt());
     $newExercise->setPublished($exerciseToCopy->getPublished());
     $em->persist($newExercise);
     $em->flush();
     foreach ($listQuestionsExoToCopy as $eq) {
         $questionToAdd = $em->getRepository('UJMExoBundle:Question')->find($eq->getQuestion());
         $exerciseQuestion = new ExerciseQuestion($newExercise, $questionToAdd);
         $exerciseQuestion->setOrdre($eq->getOrdre());
         $em->persist($exerciseQuestion);
     }
     $user = $this->container->get('security.token_storage')->getToken()->getUser();
     $subscription = new Subscription($user, $newExercise);
     $subscription->setAdmin(true);
     $subscription->setCreator(true);
     $em->persist($subscription);
     $em->flush();
     $event->setCopy($newExercise);
     $event->stopPropagation();
 }
 /**
  * To check the right to open exo or not
  *
  * @access private
  *
  * @param \UJM\ExoBundle\Entity\Exercise $exo
  *
  * @return exception
  */
 private function checkAccess($exo)
 {
     $collection = new ResourceCollection(array($exo->getResourceNode()));
     if (!$this->get('security.authorization_checker')->isGranted('OPEN', $collection)) {
         throw new AccessDeniedException($collection->getErrorsForDisplay());
     }
 }
 private function assertHasPermission($permission, Exercise $exercise)
 {
     $collection = new ResourceCollection([$exercise->getResourceNode()]);
     if (!$this->get('security.authorization_checker')->isGranted($permission, $collection)) {
         throw new AccessDeniedHttpException();
     }
 }
 /**
  * To control the max attempts, allow to know if an user can again execute an exercise.
  *
  *
  * @param \UJM\ExoBundle\Entity\Exercise $exercise
  * @param int                            $uid
  * @param bool                           $exoAdmin
  *
  * @return bool
  */
 public function controlMaxAttemps($exercise, $uid, $exoAdmin)
 {
     if ($exoAdmin === false && $exercise->getMaxAttempts() > 0 && $exercise->getMaxAttempts() <= $this->getNbPaper($uid, $exercise->getId(), true)) {
         return false;
     } else {
         return true;
     }
 }
 private function getStepsToExport(Exercise $exercise, $title, $zip)
 {
     $em = $this->getDoctrine()->getManager();
     $questionRepo = $em->getRepository('UJMExoBundle:Question');
     $qtiSer = $this->container->get('ujm.exo_qti');
     $qtiRepo = $this->container->get('ujm.exo_qti_repository');
     foreach ($exercise->getSteps() as $step) {
         // TODO : do not load the Questions from DB they already are in `$step->getStepQuestions()`
         $questions = $questionRepo->findByStep($step);
         $qtiSer->createQuestionsDirectory($questions, $step->getOrder());
         $dirs = $qtiSer->sortPathOfQuestions($qtiRepo, $step->getOrder());
         $i = 'a';
         foreach ($dirs as $dir) {
             $iterator = new \DirectoryIterator($dir);
             /** @var \DirectoryIterator $element */
             foreach ($iterator as $element) {
                 if (!$element->isDot() && $element->isFile()) {
                     $partDirectory = $title . '/' . $step->getOrder() . '/' . $step->getOrder() . '_question_' . $i . '/' . $element->getFilename();
                     $zip->addFile($element->getPathname(), $partDirectory);
                 }
             }
             $i .= 'a';
         }
     }
 }
 private function isAdmin(Exercise $exercise)
 {
     $collection = new ResourceCollection([$exercise->getResourceNode()]);
     return $this->authorization->isGranted('ADMINISTRATE', $collection);
 }
Exemple #11
0
 /**
  * @param string        $title
  * @param Question[]    $questions
  * @param User          $user
  *
  * @return Exercise
  */
 public function exercise($title, array $questions = [], User $user = null)
 {
     $exercise = new Exercise();
     if ($user) {
         if (!isset($this->exoType)) {
             $this->exoType = new ResourceType();
             $this->exoType->setName('exercise');
             $this->om->persist($this->exoType);
         }
         $node = new ResourceNode();
         $node->setName($title);
         $node->setCreator($user);
         $node->setResourceType($this->exoType);
         $node->setWorkspace($user->getPersonalWorkspace());
         $node->setClass('UJM\\ExoBundle\\Entity\\Exercise');
         $node->setGuid(time());
         $exercise->setResourceNode($node);
         $this->om->persist($node);
     }
     $this->om->persist($exercise);
     for ($i = 0, $max = count($questions); $i < $max; ++$i) {
         $step = new Step();
         $step->setText('step');
         $step->setOrder($i);
         // Add step to the exercise
         $exercise->addStep($step);
         $this->om->persist($step);
         $stepQuestion = new StepQuestion();
         $stepQuestion->setStep($step);
         $stepQuestion->setQuestion($questions[$i]);
         $stepQuestion->setOrdre(0);
         $this->om->persist($stepQuestion);
     }
     return $exercise;
 }
 /**
  * @param Exercise $exercise
  * @param array    $details
  */
 public function __construct(Exercise $exercise, $details)
 {
     parent::__construct($exercise->getResourceNode(), $details);
 }
 /**
  * @param Exercise $exercise
  * @param string   $grade
  */
 public function __construct(Exercise $exercise, $grade)
 {
     $details = array('exercise' => array('id' => $exercise->getId(), 'name' => $exercise->getName(), 'title' => $exercise->getTitle()), 'result' => $grade['scorePaper'], 'resultMax' => $grade['maxExoScore']);
     parent::__construct($exercise->getResourceNode(), $details);
 }
 public function load(ObjectManager $manager)
 {
     $resourceType = $manager->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('ujm_exercise');
     $rtid = $resourceType->getId();
     //$event = $this->dispatcher->dispatch('create_' . $resourceType, 'CreateResource', array($parent, $resourceType));
     $exercise = new Exercise();
     $exercise->setTitle('ExoFIX Title 1');
     $exercise->setNbQuestion(0);
     $exercise->setDateCreate(new \Datetime());
     $exercise->setDuration(0);
     $exercise->setNbQuestionPage(1);
     $exercise->setMaxAttempts(0);
     $exercise->setCorrectionMode(4);
     //availability_of_correction: 1:at_the_end_of_assessment, 2: after_the_last_attempt, 3: from, 4: never
     $exercise->setMarkMode(1);
     //availability_of_score: 1:at_the_same_time_that_the_correction, 2: at_the_end_of_assessment
     $exercise->setStartDate(new \Datetime());
     $exercise->setPublished(true);
     $manager->persist($exercise);
     $manager->flush();
     $this->addReference('exercise1', $exercise);
 }
 /**
  * Returns a step list according to the *shuffle* and
  * nbStep* parameters of an exercise, i.e. filtered
  * and/or randomized if needed.
  *
  * @param Exercise $exercise
  *
  * @return array
  */
 public function pickSteps(Exercise $exercise)
 {
     $steps = $exercise->getSteps()->toArray();
     if ($exercise->getShuffle()) {
         shuffle($steps);
     }
     if (($stepToPick = $exercise->getPickSteps()) > 0) {
         $steps = $this->pickItem($stepToPick, $steps);
     }
     return $steps;
 }
 /**
  * Export exercise with steps with questions.
  *
  * @param Exercise $exercise
  * @param bool     $withSolutions
  *
  * @return array
  */
 public function exportSteps(Exercise $exercise, $withSolutions = true)
 {
     $steps = $exercise->getSteps();
     $data = [];
     foreach ($steps as $step) {
         $data[] = $this->stepManager->exportStep($step, $withSolutions);
     }
     return $data;
 }
 /**
  * create the exercise
  *
  * @param String $title
  * @param Object User $user
  */
 private function createExo($title, $user)
 {
     $newExercise = new Exercise();
     $newExercise->setTitle($title);
     $newExercise->setDateCreate(new \Datetime());
     $newExercise->setNbQuestionPage(1);
     $newExercise->setNbQuestion(0);
     $newExercise->setDuration(0);
     $newExercise->setMaxAttempts(0);
     $newExercise->setStartDate(new \Datetime());
     $newExercise->setEndDate(new \Datetime());
     $newExercise->setDateCorrection(new \Datetime());
     $newExercise->setCorrectionMode('1');
     $newExercise->setMarkMode('1');
     $newExercise->setPublished(FALSE);
     $this->om->persist($newExercise);
     $this->om->flush();
     $subscription = new Subscription($user, $newExercise);
     $subscription->setAdmin(1);
     $subscription->setCreator(1);
     $this->om->persist($subscription);
     $this->om->flush();
     return $newExercise;
 }