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(); }
/** * 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(); }