/**
  * @Route("/claim", name="icap_badge_claim_badge")
  * @ParamConverter("user", options={"authenticatedUser" = true})
  * @Template()
  */
 public function claimAction(Request $request, User $user)
 {
     $badgeClaim = new BadgeClaim();
     $badgeClaim->setUser($user);
     $form = $this->createForm($this->get('icap_badge.form.claimBadge'), $badgeClaim);
     if ($request->isMethod('POST')) {
         $form->handleRequest($request);
         $flashBag = $this->get('session')->getFlashBag();
         if ($form->isValid()) {
             $translator = $this->get('translator');
             try {
                 $entityManager = $this->getDoctrine()->getManager();
                 $badge = $form->get('badge')->getData();
                 if (null !== $badge) {
                     /** @var \Claroline\CoreBundle\Manager\BadgeManager $badgeManager */
                     $badgeManager = $this->get('icap_badge.manager.badge');
                     $badgeManager->makeClaim($badge, $user);
                     $flashBag->add('success', $translator->trans('badge_claim_success_message', array(), 'icap_badge'));
                 } else {
                     $flashBag->add('warning', $translator->trans('badge_claim_nothing_selected_warning_message', array(), 'icap_badge'));
                 }
             } catch (\Exception $exception) {
                 $flashBag->add('error', $translator->trans($exception->getMessage(), array(), 'icap_badge'));
             }
             return $this->redirect($this->generateUrl('icap_badge_profile_view_badges'));
         }
     }
     return array('form' => $form->createView());
 }
 /**
  * @Route("/my_badges/claim/{badge_id}", name="icap_badge_workspace_tool_claim_badge")
  * @ParamConverter(
  *     "workspace",
  *     class="ClarolineCoreBundle:Workspace\Workspace",
  *     options={"id" = "workspaceId"}
  * )
  * @ParamConverter("user", options={"authenticatedUser" = true})
  * @ParamConverter("badge", class="IcapBadgeBundle:Badge", options={"id" = "badge_id"})
  * @Template()
  */
 public function claimAction(Workspace $workspace, User $user, Badge $badge)
 {
     $badgeClaim = new BadgeClaim();
     $badgeClaim->setUser($user);
     try {
         $flashBag = $this->get('session')->getFlashBag();
         $translator = $this->get('translator');
         /** @var \Icap\BadgeBundle\Manager\BadgeManager $badgeManager */
         $badgeManager = $this->get('icap_badge.manager.badge');
         $badgeManager->makeClaim($badge, $user);
         $flashBag->add('success', $translator->trans('badge_claim_success_message', array(), 'icap_badge'));
     } catch (\Exception $exception) {
         $flashBag->add('error', $translator->trans($exception->getMessage(), array(), 'icap_badge'));
     }
     return $this->redirect($this->generateUrl('icap_badge_workspace_tool_my_badges', array('workspaceId' => $workspace->getId())));
 }
Esempio n. 3
0
 public function makeClaim(Badge $badge, User $user)
 {
     $userBadge = $this->entityManager->getRepository('IcapBadgeBundle:UserBadge')->findOneByBadgeAndUser($badge, $user);
     if (null !== $userBadge) {
         throw new \Exception('badge_already_award_message');
     }
     $badgeClaim = $this->entityManager->getRepository('IcapBadgeBundle:BadgeClaim')->findOneByBadgeAndUser($badge, $user);
     if (null !== $badgeClaim) {
         throw new \Exception('badge_already_claim_message');
     }
     $badgeClaim = new BadgeClaim();
     $badgeClaim->setUser($user)->setBadge($badge);
     try {
         $this->entityManager->persist($badgeClaim);
         $this->entityManager->flush();
     } catch (\Exception $exception) {
         throw new \Exception('badge_claim_error_message', 0, $exception);
     }
 }