/**
  * @param int $badgeId
  *
  * @return Badge|null
  *
  * @throws \Symfony\Component\Form\Exception\TransformationFailedException
  */
 public function reverseTransform($badgeId)
 {
     if (!$badgeId) {
         return;
     }
     $badge = $this->badgeManager->getById($badgeId);
     if (null === $badge) {
         throw new TransformationFailedException();
     }
     return $badge;
 }
Esempio n. 2
0
 /**
  * @DI\Observe("list_all_my_badges")
  *
  * @param DisplayToolEvent $event
  *
  * @return string (content)
  */
 public function onListAllMyBadges(DisplayToolEvent $event)
 {
     $userBadges = $this->badgeManager->getLoggedUserBadges();
     $content = $this->templateingEngine->render('IcapBadgeBundle:Profile:myProfileWidgetBadges.html.twig', array('userBadges' => $userBadges));
     $event->setContent($content);
     $event->stopPropagation();
 }
Esempio n. 3
0
 /**
  * @param Badge $badge
  *
  * @return bool True on successfull processing, false otherwise
  */
 public function handleEdit(Badge $badge, $badgeManager = null, $unawardBadge = false)
 {
     $this->form->setData($badge);
     /** @var BadgeRule[]|\Doctrine\Common\Collections\ArrayCollection $originalRules */
     $originalRules = $badge->getRules();
     if ($this->request->isMethod('POST')) {
         $this->form->handleRequest($this->request);
         if ($this->form->isValid()) {
             $this->handleUpload($this->form->get('file')->getData(), $badge);
             $badgeRules = $badge->getRules();
             $userBadges = $badge->getUserBadges();
             if (0 < count($userBadges) && $this->badgeManager->isRuleChanged($badgeRules, $originalRules)) {
                 /** @var \Doctrine\ORM\UnitOfWork $unitOfWork */
                 $unitOfWork = $this->entityManager->getUnitOfWork();
                 $newBadge = clone $badge;
                 $newBadge->setVersion($badge->getVersion() + 1);
                 $unitOfWork->refresh($badge);
                 $badge->setDeletedAt(new \DateTime());
                 $this->entityManager->persist($newBadge);
                 // If the new badge has to be revoked from users already awarded, skip the next part
                 if (!$unawardBadge) {
                     foreach ($userBadges as $userBadge) {
                         // Award new version to previous users
                         $badgeManager->addBadgeToUser($newBadge, $userBadge->getUser());
                     }
                 }
             } else {
                 // Compute which rules was deleted
                 foreach ($badgeRules as $rule) {
                     if ($originalRules->contains($rule)) {
                         $originalRules->removeElement($rule);
                     }
                 }
                 // Delete rules
                 foreach ($originalRules as $rule) {
                     $this->entityManager->remove($rule);
                 }
             }
             $this->entityManager->persist($badge);
             $this->entityManager->flush();
             return true;
         }
     }
     return false;
 }
 /**
  * @DI\Observe("widget_badge_usage")
  *
  * @param DisplayWidgetEvent $event
  */
 public function onDisplay(DisplayWidgetEvent $event)
 {
     $widgetInstance = $event->getInstance();
     $workspace = $widgetInstance->getWorkspace();
     $user = $this->tokenStorage->getToken()->getUser();
     $badgeWidgetConfig = $this->badgeWidgetManager->getBadgeUsageConfigForInstance($widgetInstance);
     $simple_view_widget = $badgeWidgetConfig->isSimpleView();
     $nbLastAwarded = $badgeWidgetConfig->getNumberLastAwardedBadge();
     $nbMostAwarded = $badgeWidgetConfig->getNumberMostAwardedBadge();
     $mostAwardedBadges = [];
     $availableBadges = [];
     if (!is_null($workspace)) {
         $lastAwardedBadges = $user !== 'anon.' ? $this->badgeManager->getWorkspaceLastAwardedBadgesToLoggedUser($workspace, $nbLastAwarded) : [];
         $mostAwardedBadges = $this->badgeManager->getWorkspaceMostAwardedBadges($workspace, $nbMostAwarded);
         $availableBadges = $this->badgeManager->getWorkspaceAvailableBadges($workspace);
         $widgetType = 'workspace';
     } else {
         $lastAwardedBadges = $this->badgeManager->getLoggedUserLastAwardedBadges($nbLastAwarded);
         $widgetType = 'desktop';
     }
     $content = $this->templating->render('IcapBadgeBundle:Widget:badge_usage.html.twig', ['lastAwardedBadges' => $lastAwardedBadges, 'mostAwardedBadges' => $mostAwardedBadges, 'availableBadges' => $availableBadges, 'simple_view_widget' => $simple_view_widget, 'systemName' => $this->platformName, 'widgetType' => $widgetType]);
     $event->setContent($content);
     $event->stopPropagation();
 }