/** * Return true if the given $user can view the given $tag. * The $entity can be viewed by the $user if the user is linked to that $tag. * * @param TagInterface $tag * @param TaggableInterface $user * * @return bool */ private function canView(TagInterface $tag, TaggableInterface $user) { return in_array($tag, $user->getTags()->toArray()); }
/** * Return true if the given $user can view the given $entity. * The $entity can be viewed by the $user if they have AT LEAST ONE common tag. * * @param TaggableInterface $entity * @param TaggableInterface $user * * @return bool */ private function canView(TaggableInterface $entity, TaggableInterface $user) { $badgeTagIds = $entity->getTags()->map(function (TagInterface $tag) { return $tag->getId(); })->toArray(); $userTagIds = $user->getTags()->map(function (TagInterface $tag) { return $tag->getId(); })->toArray(); return count(array_intersect($badgeTagIds, $userTagIds)) > 0; }
/** * {@inheritdoc} */ public function getCompletedAdventuresForUser(TaggableInterface $user) { $tagIds = []; foreach ($user->getTags() as $tag) { $tagIds[] = $tag->getId(); } $qb = $this->createQueryBuilder('adventure'); $qb->leftJoin('adventure.tags', 'tags')->leftJoin('adventure.steps', 's')->leftJoin('s.completions', 'sc')->having('COUNT(sc) = COUNT(s)')->andWhere('tags.id IN (:tagIds)')->setMaxResults(15)->groupBy('adventure.id')->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY); return $qb->getQuery()->getResult(); }
/** * {@inheritdoc} */ public function getAvailableQuestsForUser(TaggableInterface $user) { $tagIds = []; foreach ($user->getTags() as $tag) { $tagIds[] = $tag->getId(); } $today = new \DateTime('today midnight'); $qb = $this->createQueryBuilder('quest'); $qb->leftJoin('quest.tags', 'tags')->leftJoin('quest.completions', 'qc', 'WITH', 'qc.user = :user AND qc.pending = 0')->where('qc.id IS NULL')->andWhere('tags.id IN (:tagIds)')->andWhere('quest.startDate <= :today')->andWhere('quest.endDate >= :today')->setMaxResults(15)->orderBy('quest.endDate')->groupBy('quest.id')->setParameter('user', $user)->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY)->setParameter('today', $today); return $qb->getQuery()->getResult(); }