Skip to content

Commit

Permalink
User - display last bets
Browse files Browse the repository at this point in the history
  • Loading branch information
kgaut committed Jun 20, 2018
1 parent 3f09c61 commit 0934eab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 62 deletions.
20 changes: 14 additions & 6 deletions mespronos.module
Expand Up @@ -237,6 +237,11 @@ function mespronos_entity_extra_field_info() {
'weight' => 100,
'visible' => FALSE,
);
$extra['user']['user']['display']['last_bets'] = array(
'label' => t('Derniers pronostics'),
'weight' => 100,
'visible' => FALSE,
);
$extra['game']['game']['display']['better_bets'] = array(
'label' => t('Betters\' bets'),
'weight' => 100,
Expand Down Expand Up @@ -274,20 +279,23 @@ function mespronos_user_view(array &$build, Drupal\user\Entity\User $entity, Ent
$build['hall_of_flame'] = [];
$build['hall_of_flame']['content'] = [
'#type' => 'container',
"element-content" => $render,
'element-content' => $render,
];
}
if ($display->getComponent('last_results')) {
$lastBetController = new LastBetsController();
$user = \Drupal\user\Entity\User::load($entity->id());
$build['last_results'] = $lastBetController->lastBets(NULL,50,'BLOCK', $user,TRUE);
}
if ($display->getComponent('last_bets')) {
$build['last_bets'] = \Drupal::service('mespronos.bet_manager')->getRecentBetsForUserTable($user, 64);
}
}
if ($display->getComponent('pseudo')) {
$build['username'] = [
'#markup' => '<h3>'.$entity->label().'</h3>',
];
}
if ($display->getComponent('last_results')) {
$lastBetController = new LastBetsController();
$user = \Drupal\user\Entity\User::load($entity->id());
$build['last_results'] = $lastBetController->lastBets(null,50,'BLOCK',$user,true);
}
}

function mespronos_game_view(array &$build, Game $entity, EntityViewDisplay $display, $view_mode) {
Expand Down
4 changes: 4 additions & 0 deletions mespronos.services.yml
Expand Up @@ -27,6 +27,10 @@ services:
class: Drupal\mespronos\GameManager
arguments: ['@module_handler']

mespronos.bet_manager:
class: Drupal\mespronos\BetManager
arguments: []

mespronos.user_manager:
class: Drupal\mespronos\UserManager
arguments: ['@module_handler']
Expand Down
76 changes: 76 additions & 0 deletions src/BetManager.php
@@ -0,0 +1,76 @@
<?php

namespace Drupal\mespronos;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\domain\Entity\Domain;
use Drupal\mespronos\Entity\Bet;
use Drupal\mespronos\Entity\Day;
use Drupal\mespronos\Entity\Game;
use Drupal\mespronos_group\Entity\Group;
use Drupal\user\Entity\User;

class BetManager {
public function getRecentBetsForUserTable(User $user, $nb_bets = 20, Day $day = NULL) {
$bets = $this->getRecentBetsForUser($user, $nb_bets);
$rows = [];
$leagues = [];
foreach ($bets as $bet) {
$game = $bet->getGame(TRUE);
$day = $game->getDay();
if (!isset($leagues[$day->getLeagueID()])) {
$leagues[$day->getLeagueID()] = $day->getLeague();
}

$day_renderable = $day->getRenderableLabel();
$row = [
'data' => [
'day' => [
'data' => render($day_renderable),
'class' => ['day-cell']
],
$game->labelTeams(),
$game->labelScore(),
$bet->label(),
['data' => $bet->getPoints(), 'class'=>'points'],
],
'class' => $leagues[$day->getLeagueID()]->getPointsCssClass($bet->getPoints()),
];

$rows[] = $row;
}

$header = [
t('League', array(), array('context' => 'mespronos')),
t('Game', array(), array('context' => 'mespronos')),
t('Score', array(), array('context' => 'mespronos')),
t('Bet', array(), array('context' => 'mespronos')),
t('Points', array(), array('context' => 'mespronos')),
];

$table_array = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
'#cache' => [
'contexts' => ['user'],
'tags' => ['lastbets', 'user:'.$user->id()],
],
];

return $table_array;
}

public function getRecentBetsForUser($user, $nb) {
$ids = \Drupal::entityQuery('bet')
->condition('better', $user->id())
->condition('points', '', 'IS NOT NULL')
->sort('created', 'DESC')
->range(0, $nb)
->execute();
if (\count($ids) > 0) {
return Bet::loadMultiple($ids);
}
return [];
}
}
58 changes: 2 additions & 56 deletions src/Controller/BetController.php
Expand Up @@ -111,53 +111,8 @@ public static function updateBetsForLeagueOver($success, $results, $operations)
}

public static function getLastUserBetsTable(User $user, $nb_bets = 20, Day $day = NULL) {
$bets = self::getLastUserBets($user, $nb_bets);
$rows = [];
$leagues = [];
foreach ($bets as $bet) {
$game = $bet->getGame(TRUE);
$day = $game->getDay();
if (!isset($leagues[$day->getLeagueID()])) {
$leagues[$day->getLeagueID()] = $day->getLeague();
}

$day_renderable = $day->getRenderableLabel();
$row = [
'data' => [
'day' => [
'data' => render($day_renderable),
'class' => ['day-cell']
],
$game->labelTeams(),
$game->labelScore(),
$bet->label(),
$bet->getPoints(),
],
'class' => $leagues[$day->getLeagueID()]->getPointsCssClass($bet->getPoints()),
];

$rows[] = $row;
}

$header = [
t('League', array(), array('context' => 'mespronos')),
t('Game', array(), array('context' => 'mespronos')),
t('Score', array(), array('context' => 'mespronos')),
t('Bet', array(), array('context' => 'mespronos')),
t('Points', array(), array('context' => 'mespronos')),
];

$table_array = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
'#cache' => [
'contexts' => ['user'],
'tags' => ['lastbets', 'user:'.$user->id()],
],
];
return \Drupal::service('mespronos.bet_manager')->getRecentBetsForUserTable($user, $nb_bets, $day);

return $table_array;
}

/**
Expand All @@ -166,16 +121,7 @@ public static function getLastUserBetsTable(User $user, $nb_bets = 20, Day $day
* @return \Drupal\mespronos\Entity\Bet[]
*/
public static function getLastUserBets(User $user, $nb_bets = 20) {
$ids = \Drupal::entityQuery('bet')
->condition('better', $user->id())
->condition('points', '', 'IS NOT NULL')
->sort('created', 'DESC')
->range(0, $nb_bets)
->execute();
if (\count($ids) > 0) {
return Bet::loadMultiple($ids);
}
return [];
return \Drupal::service('mespronos.bet_manager')->getRecentBetsForUser($user, $nb_bets);
}

/**
Expand Down

0 comments on commit 0934eab

Please sign in to comment.