/**
  * @param User $user
  * @return ReportCard
  */
 public function getForUser(User $user)
 {
     $uid = (string) $user->getId();
     if ($this->cache->contains($uid)) {
         return $this->cache->fetch($uid);
     }
     /** @var EGPlayerStatsRepository $statsRepo */
     $statsRepo = $this->em->getRepository('GotChosenSiteBundle:EGPlayerStats');
     /** @var EGGameResultRepository $resultsRepo */
     $resultsRepo = $this->em->getRepository('GotChosenSiteBundle:EGGameResult');
     /** @var Scholarship $egScholarship */
     $egScholarship = $this->em->getRepository('GotChosenSiteBundle:Scholarship')->getCurrentEvoGames();
     $playerStats = $statsRepo->getOrCreate($user, $egScholarship, date('Y-m'));
     $card = new ReportCard();
     $card->rank = $playerStats->getRank();
     $card->maxRank = $statsRepo->getTotalPlayers(date('Y-m'));
     $card->qualifierPlays = $statsRepo->getTotalPlaySessions($user, $egScholarship, EGPlaySession::PHASE_QUALIFIER);
     $card->contestPlays = $statsRepo->getTotalPlaySessions($user, $egScholarship, EGPlaySession::PHASE_CONTEST);
     $card->wins = $resultsRepo->getTotalWins($user, date('Y-m'));
     $card->feedbacksRated = min(20, $playerStats->getFeedbacksRated());
     $card->pointsWinning = $playerStats->getGameplayPoints();
     $card->pointsBonus = $playerStats->getBonusPoints();
     $card->pointsFeedback = $playerStats->getFeedbackPoints();
     $card->pointsTotal = $playerStats->getTotalPoints();
     $this->cache->save($uid, $card);
     return $card;
 }
 public function recalcFeedbackPoints(User $user, Scholarship $scholarship, $month)
 {
     $em = $this->getEntityManager();
     $conn = $em->getConnection();
     /** @var EGPlayerStatsRepository $statsRepo */
     $statsRepo = $em->getRepository('GotChosenSiteBundle:EGPlayerStats');
     // grab all the rated games with their highest ratings
     $stmt = $conn->prepare('SELECT f.game_id, MAX(f.developerRating) max_rating
          FROM EGFeedback f
          WHERE f.user_id = :user
              AND f.createdDate >= :start
              AND f.createdDate < :end
              AND f.ratedDate IS NOT NULL
          GROUP BY f.game_id
          ORDER BY max_rating DESC');
     $stmt->bindValue('user', $user->getId(), \PDO::PARAM_INT);
     $stmt->bindValue('start', $month . '-01 00:00:00', \PDO::PARAM_STR);
     $stmt->bindValue('end', Dates::nextMonth($month) . '-01 00:00:00', \PDO::PARAM_STR);
     $stmt->execute();
     $totalFeedback = 0;
     $count = 0;
     while ($row = $stmt->fetch()) {
         $totalFeedback += $row['max_rating'];
         $count++;
         // stop at 100 feedback or 20 games
         if ($totalFeedback >= 100 || $count >= 20) {
             break;
         }
     }
     $totalFeedback = min($totalFeedback, 100);
     $pstats = $statsRepo->getOrCreate($user, $scholarship, $month);
     $pstats->setFeedbackPoints($totalFeedback);
     $pstats->updateTotalPoints();
     $em->flush();
 }
 /**
  * @param User $user
  * @param $month
  * @return integer
  */
 public function getTotalWins(User $user, $month)
 {
     $this->assertMonth($month);
     $q = $this->getEntityManager()->createQuery('SELECT SUM(gr.wins) FROM GotChosenSiteBundle:EGGameResult gr
          WHERE gr.statsMonth = :month AND gr.user = :user');
     $q->setParameter('month', $month);
     $q->setParameter('user', $user->getId());
     return $q->getSingleScalarResult() ?: 0;
     // This function was returning NULL instead of 0
 }
 /**
  * Retrieves play sessions for the given user, optionally filtered by month.
  *
  * @param User $player
  * @param null|string $month In the form of YYYY-MM
  * @return array
  */
 public function findPlaySessions(User $player, $month = null)
 {
     $q = $this->getEntityManager()->createQueryBuilder();
     $q->select('ps', 'g')->from('GotChosenSiteBundle:EGPlaySession', 'ps')->join('ps.game', 'g')->where('ps.player = :user')->setParameter('user', $player->getId())->andWhere('ps.isCompleted = 1');
     if ($month && preg_match('/^\\d{4}-\\d{2}$/', $month)) {
         $q->andWhere('ps.endDate >= :start')->setParameter('start', $month . '-01 00:00:00')->andWhere('ps.endDate < :end')->setParameter('end', Dates::nextMonth($month) . '-01 00:00:00');
     }
     $q->orderBy('ps.endDate');
     return $q->getQuery()->getResult();
 }
 /**
  * @param User $sponsor
  * @param Scholarship $sship40k
  * @return EntrySponsor[]
  */
 public function getSponsoring(User $sponsor, Scholarship $sship40k)
 {
     $em = $this->getEntityManager();
     $q = $em->createQuery('SELECT es, e, u FROM GotChosenSiteBundle:EntrySponsor es
          JOIN es.user sponsor
          JOIN es.entry e
          JOIN e.user u
          JOIN e.scholarship s
          WHERE s.id = ?1 AND sponsor.id = ?2');
     $q->setParameter(1, $sship40k->getId());
     $q->setParameter(2, $sponsor->getId());
     return $q->getResult();
 }
 /**
  * Determines if this property can be seen by the given $viewer.
  * If $viewer is the property's owner, or an administrator, returns true. Otherwise it will compare
  * this property's visible networks with the networks of the viewer, and if at least one matches,
  * this method will return true.
  *
  * @param User $viewer
  * @param array $viewerNetworkIds
  * @return bool
  */
 public function isVisibleBy(User $viewer = null, $viewerNetworkIds = null)
 {
     $visibility = $this->getVisibility();
     // if public, true right away. if not public and the viewer is a guest, false right away.
     if ($visibility == self::VISIBLE_PUBLIC) {
         return true;
     } else {
         if (!$viewer) {
             return false;
         }
     }
     // if we are viewing our own profile, true.
     if ($viewer && $viewer->getId() == $this->getUser()->getId()) {
         return true;
     }
     // if we are an admin, true.
     // however we name these... ROLE_ADMIN is part of symfony, Administrators may be a new thing.
     $viewerRoles = $viewer ? $viewer->getRoles() : [];
     if (in_array('ROLE_SUPER_ADMIN', $viewerRoles) || in_array('Administrator', $viewerRoles)) {
         return true;
     }
     // handle private (false except for owner/admin, handled above), and all registered users.
     if ($visibility == self::VISIBLE_PRIVATE) {
         return false;
     } else {
         if ($visibility == self::VISIBLE_MEMBERS) {
             return $viewer ? true : false;
         }
     }
     // handle custom network-based visibility.
     // if a list of the viewer's network ids is passed, use that to save on processing time.
     if ($viewerNetworkIds === null) {
         $viewerNetworkIds = [];
         foreach ($viewer->getNetworks() as $vnet) {
             $viewerNetworkIds[] = $vnet->getNetwork()->getId();
         }
     }
     return $this->hasIntersectingNetworks($viewerNetworkIds);
 }
 public function userHasGame(User $user)
 {
     $gameRepo = $this->em->getRepository('GotChosenSiteBundle:EGGame');
     $game = $gameRepo->findOneBy(['user' => $user->getId()]);
     return !is_null($game);
 }
 public static function makePreview(User $toUser, $template, $subject, array $params)
 {
     $mq = new MassMailQueue();
     $mq->setType(self::TYPE_PREVIEW);
     $mq->setFilterSpec(['userId' => $toUser->getId()]);
     $mq->setTemplate($template);
     $mq->setSubject($subject);
     $mq->setParameters($params);
     return $mq;
 }
 public function getProperties(User $user, $includeGroups = false)
 {
     $gSelect = "";
     $gJoin = "";
     if ($includeGroups) {
         $gSelect = "g, ";
         $gJoin = "JOIN p.groups g";
     }
     $q = $this->getEntityManager()->createQuery("SELECT up, p, {$gSelect} vn FROM GotChosenSiteBundle:UserProfile up\n             JOIN up.property p\n             {$gJoin}\n             LEFT JOIN up.visibleNetworks vn\n             WHERE up.user = ?1");
     $q->setParameter(1, $user->getId());
     return $q->getResult();
 }