/** * @param Badge $badge * @param Constraint $constraint */ public function validate($badge, Constraint $constraint) { if ($badge->isExpiring()) { if (null === $badge->getExpireDuration()) { $this->context->addViolationAt('expire_duration', $constraint->message); } } }
/** * Sets the locale on a badge. * * @param Badge $badge * @param LifecycleEventArgs $event */ public function postLoad(Badge $badge, LifecycleEventArgs $event) { $platformLocale = $this->platformConfigHandler->getParameter('locale_language'); $userLocale = null; if ($token = $this->tokenStorage->getToken()) { if ('anon.' !== ($user = $token->getUser())) { $userLocale = $user->getLocale(); } } $badge->setLocale($userLocale ?: $platformLocale); }
public function buildForm(FormBuilderInterface $builder, array $options) { $platformLanguage = $this->platformConfigHandler->getParameter('locale_language'); $languages = array_values($this->localeManager->getAvailableLocales()); $sortedLanguages = array(); usort($languages, function ($language1, $language2) use($platformLanguage) { if ($language1 === $platformLanguage) { return -1; } elseif ($language2 === $platformLanguage) { return 1; } else { return 0; } }); $translationBuilder = $builder->create('translations', 'form', array('virtual' => true)); foreach ($languages as $language) { $fieldName = sprintf('%sTranslation', $language); $translationBuilder->add($fieldName, new BadgeTranslationType()); } $builder->add($translationBuilder)->add('automatic_award', 'checkbox', array('required' => false))->add('file', 'file', array('label' => 'badge_form_image'))->add('is_expiring', 'checkbox', array('required' => false))->add('expire_duration', 'integer', array('attr' => array('class' => 'input-sm', 'min' => 1)))->add('expire_period', 'choice', array('choices' => Badge::getExpirePeriodLabels(), 'attr' => array('class' => 'input-sm'))); $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { /** @var \Icap\BadgeBundle\Entity\Badge $badge */ $badge = $event->getData(); if ($badge && null !== $badge) { $this->badgeRuleType->setBadgeId($badge->getId()); $form = $event->getForm(); $form->add('rules', 'collection', array('type' => $this->badgeRuleType, 'by_reference' => false, 'attr' => array('class' => 'rule-collections'), 'theme_options' => array('label_width' => 'col-md-3'), 'prototype' => true, 'allow_add' => true, 'allow_delete' => true)); } }); }
/** * Get details. * * @return array */ public function getNotificationDetails() { $receiver = $this->getReceiver(); $workspace = $this->badge->getWorkspace() ? $this->badge->getWorkspace()->getId() : null; $notificationDetails = array('workspace' => $workspace, 'badge' => array('id' => $this->badge->getId(), 'name' => $this->badge->getName(), 'slug' => $this->badge->getSlug()), 'receiver' => array('id' => $receiver->getId(), 'publicUrl' => $receiver->getPublicUrl(), 'lastName' => $receiver->getLastName(), 'firstName' => $receiver->getFirstName())); return $notificationDetails; }
/** * @param Badge $badge * @param Constraint $constraint */ public function validate($badge, Constraint $constraint) { $translations = $badge->getTranslations(); $hasEmptyTranslation = 0; foreach ($translations as $translation) { // Have to put all method call in variable because of empty doesn't // support result of method as parameter (prior to PHP 5.5) $name = $translation->getName(); $description = $translation->getDescription(); $criteria = $translation->getCriteria(); if (empty($name) && empty($description) && empty($criteria)) { ++$hasEmptyTranslation; } } if (count($translations) === $hasEmptyTranslation) { $this->context->addViolation($constraint->message); } }
/** * @param Badge $badge * @param Constraint $constraint */ public function validate($badge, Constraint $constraint) { if (null === $badge->getFile() && null === $badge->getImagePath()) { $this->context->addViolationAt('file', $constraint->message); } }
/** * @return array */ public function getData() { return array('badge' => $this->badge->getId(), 'name' => $this->badge->getName(), 'img' => $this->badge->getWebPath()); }
/** * To verify if an user obtained the badge. * * * @param int $userId id User * @param \Icap\BadgeBundle\Entity\Badge $badge * * @return \Icap\BadgeBundle\Entity\UserBadge */ private function getUserBadge($userId, $badge) { $em = $this->doctrine->getManager(); $userBadge = $em->getRepository('IcapBadgeBundle:UserBadge')->findOneBy(array('user' => $userId, 'badge' => $badge->getId())); return $userBadge; }
/** * @param Badge $badge * @param Constraint $constraint */ public function validate($badge, Constraint $constraint) { if ($badge->getAutomaticAward() && 0 >= count($badge->getRules())) { $this->context->addViolation($constraint->message, array(), null); } }
/** * @Route("/unaward/{id}/{username}", name="icap_badge_workspace_tool_badges_unaward") * @ParamConverter( * "workspace", * class="ClarolineCoreBundle:Workspace\Workspace", * options={"id" = "workspaceId"} * ) * @ParamConverter("user", options={"mapping": {"username": "******"}}) * @Template */ public function unawardAction(Request $request, Workspace $workspace, Badge $badge, User $user) { if (null === $badge->getWorkspace()) { throw $this->createNotFoundException('No badge found.'); } $this->checkUserIsAllowed($workspace); /** @var \Symfony\Component\Translation\TranslatorInterface $translator */ $translator = $this->get('translator'); try { $doctrine = $this->getDoctrine(); /** @var \Doctrine\ORM\EntityManager $entityManager */ $entityManager = $doctrine->getManager(); $userBadge = $doctrine->getRepository('IcapBadgeBundle:UserBadge')->findOneByBadgeAndUser($badge, $user); $entityManager->remove($userBadge); $entityManager->flush(); $this->get('session')->getFlashBag()->add('success', $translator->trans('badge_unaward_success_message', array(), 'icap_badge')); } catch (\Exception $exception) { if (!$request->isXmlHttpRequest()) { $this->get('session')->getFlashBag()->add('error', $translator->trans('badge_unaward_error_message', array(), 'icap_badge')); } else { return new Response($exception->getMessage(), 500); } } if ($request->isXmlHttpRequest()) { return new JsonResponse(array('error' => false)); } return $this->redirect($this->generateUrl('icap_badge_workspace_tool_badges_edit', array('workspaceId' => $workspace->getId(), 'slug' => $badge->getSlug()))); }
public function findUsersNotAwardedWithBadge(Badge $badge) { $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $notIn = $qb->select('IDENTITY(ub.user)')->from('IcapBadgeBundle:Userbadge', 'ub')->where('ub.badge = ?1'); $qb1 = $em->createQueryBuilder(); $qb1->select('u')->from('ClarolineCoreBundle:User', 'u')->where($qb1->expr()->notIn('u.id', $notIn->getDQL()))->setParameter(1, $badge->getId()); return $qb1->getQuery()->getResult(); }
/** * @param Badge $badge * @param \DateTime|null $currentDate * * @return \DateTime */ public function generateExpireDate(Badge $badge, \DateTime $currentDate = null) { if (null === $currentDate) { $currentDate = new \DateTime(); } $modifier = sprintf('+%d %s', $badge->getExpireDuration(), $badge->getExpirePeriodTypeLabel($badge->getExpirePeriod())); return $currentDate->modify($modifier); }
private function handleUpload(UploadedFile $file = null, Badge $badge) { $ds = DIRECTORY_SEPARATOR; if ($file !== null) { if (file_exists($this->uploadDir . $ds . $badge->getImagePath())) { @unlink($this->uploadDir . $ds . $badge->getImagePath()); } $filename = sha1(uniqid(mt_rand(), true)) . '.' . $file->guessExtension(); $badge->setImagePath($filename); $realpathUploadRootDir = realpath($this->uploadDir); if (false === $realpathUploadRootDir) { throw new \Exception(sprintf("Invalid upload root dir '%s'for uploading badge images.", $this->uploadDir)); } $file->move($this->uploadDir, $filename); } }