public function getPhotosSorted() { if (isset($this->photos) and !empty($this->photos)) { $photosCollection = new Collection($this->photos); return $photosCollection->sortBy(function ($photo) { return $photo->sort_order; }, SORT_ASC)->toArray(); } return []; }
public function _getLastFiveComments() { $alias = Inflector::classify($this->source()); $comment = TableRegistry::get('Social.Comments'); $comments = $comment->find()->where(['Comments.object_id' => $this->id, 'Comments.object' => $alias])->limit(__MAX_COMMENTS_LISTED)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]); // Reorder the Comments by creation order // (even though we got them by descending order) $collection = new Collection($comments); $comments = $collection->sortBy('Comment.created'); return $comments->toArray(); }
public function get_for_parent($parent, $page) { $comments = $this->Comments->find()->where(['Comments.object_id' => $parent])->limit(__MAX_COMMENTS_LISTED)->page($page)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]); // Reorder the Comments by creation order // (even though we got them by descending order) $collection = new Collection($comments); $comments = $collection->sortBy('Comment.created'); $view = new View($this->request, $this->response, null); $view->layout = 'ajax'; // layout to use or false to disable $view->set('comments', $comments->toArray()); $data['html'] = $view->render('Social.Comments/get_for_parent'); $this->layout = 'ajax'; $this->set('data', $data); $this->render('/Shared/json/data'); }
/** * Tests sort * * @return void */ public function testSortString() { $items = [['a' => ['b' => ['c' => 4]]], ['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]]]; $collection = new Collection($items); $map = $collection->sortBy('a.b.c'); $this->assertInstanceOf('\\Cake\\Collection\\Collection', $map); $expected = [2 => ['a' => ['b' => ['c' => 10]]], 1 => ['a' => ['b' => ['c' => 6]]], 0 => ['a' => ['b' => ['c' => 4]]]]; $this->assertEquals($expected, iterator_to_array($map)); }
public function statistic($tournament_id) { $games = TableRegistry::get('Games')->find()->where(['tournament_id' => $tournament_id]); $statistics['matchs'] = 0; $statistics['goals'] = 0; $statistics['principals_victory'] = 0; $statistics['visitors_victory'] = 0; $statistics['draws'] = 0; foreach ($games as $key => $game) { $statistics['matchs']++; $statistics['goals'] += $game['score_a'] + $game['score_b']; if ($game['score_a'] > $game['score_b']) { $statistics['principals_victory']++; } else { if ($game['score_a'] < $game['score_b']) { $statistics['visitors_victory']++; } else { if (!is_null($game['score_a'])) { $statistics['draws']++; } } } } $conn = ConnectionManager::get('default'); $query = $conn->execute(' SELECT player, team_name, team_shield, IFNULL(sum(goals_pro),0) player_goals_pro, IFNULL(sum(goals_against),0) player_goals_against FROM ( ( SELECT u.id player_id, u.login player, te.name team_name, te.logo team_shield, SUM( score_a ) goals_pro, SUM( score_b ) goals_against FROM games g INNER JOIN tournaments t ON g.tournament_id = t.id INNER JOIN teams_tournaments_users tt ON tt.tournament_id = t.id AND tt.user_id = g.user_a_id INNER JOIN teams te ON te.id = tt.team_id INNER JOIN users u ON u.id = g.user_a_id WHERE t.id =' . $tournament_id . ' GROUP BY g.user_a_id ) UNION ( SELECT u.id player_id, u.login player, te.name team_name, te.logo team_shield, SUM( score_b ) goals_pro, SUM( score_a ) goals_against FROM games g INNER JOIN tournaments t ON g.tournament_id = t.id INNER JOIN teams_tournaments_users tt ON tt.tournament_id = t.id AND tt.user_id = g.user_b_id INNER JOIN teams te ON te.id = tt.team_id INNER JOIN users u ON u.id = g.user_b_id WHERE t.id =' . $tournament_id . ' GROUP BY g.user_b_id ) ORDER BY goals_pro DESC ) a GROUP BY player_id ORDER BY player_goals_pro DESC '); $playersGoals = $query->fetchAll('assoc'); $statistics['best_atack'] = []; $statistics['best_atack'][0]['player'] = $playersGoals[0]['player']; $statistics['best_atack'][0]['team'] = $playersGoals[0]['team_name']; $statistics['best_atack'][0]['shield'] = $playersGoals[0]['team_shield']; $statistics['best_atack'][0]['goals'] = $playersGoals[0]['player_goals_pro']; foreach ($playersGoals as $key => $row) { if ($statistics['best_atack'][0]['goals'] == $row['player_goals_pro']) { $statistics['best_atack'][$key]['player'] = $row['player']; $statistics['best_atack'][$key]['team'] = $row['team_name']; $statistics['best_atack'][$key]['shield'] = $row['team_shield']; $statistics['best_atack'][$key]['goals'] = $row['player_goals_pro']; } } $playersGoalsCollection = new Collection($playersGoals); $playersGoals = $playersGoalsCollection->sortBy('player_goals_against', SORT_ASC, SORT_NUMERIC)->toArray(); $playersGoals = array_values($playersGoals); $statistics['best_defense'] = []; $statistics['best_defense'][0]['player'] = $playersGoals[0]['player']; $statistics['best_defense'][0]['team'] = $playersGoals[0]['team_name']; $statistics['best_defense'][0]['shield'] = $playersGoals[0]['team_shield']; $statistics['best_defense'][0]['goals'] = $playersGoals[0]['player_goals_against']; foreach ($playersGoals as $key => $row) { if ($statistics['best_defense'][0]['goals'] == $row['player_goals_against']) { $statistics['best_defense'][$key]['player'] = $row['player']; $statistics['best_defense'][$key]['team'] = $row['team_name']; $statistics['best_defense'][$key]['shield'] = $row['team_shield']; $statistics['best_defense'][$key]['goals'] = $row['player_goals_against']; } } $playersGoalsCollection = new Collection($playersGoals); $playersGoals = $playersGoalsCollection->sortBy('player_goals_pro', SORT_ASC, SORT_NUMERIC)->toArray(); $playersGoals = array_values($playersGoals); $statistics['worse_atack'] = []; $statistics['worse_atack'][0]['player'] = $playersGoals[0]['player']; $statistics['worse_atack'][0]['team'] = $playersGoals[0]['team_name']; $statistics['worse_atack'][0]['shield'] = $playersGoals[0]['team_shield']; $statistics['worse_atack'][0]['goals'] = $playersGoals[0]['player_goals_pro']; foreach ($playersGoals as $key => $row) { if ($statistics['worse_atack'][0]['goals'] == $row['player_goals_pro']) { $statistics['worse_atack'][$key]['player'] = $row['player']; $statistics['worse_atack'][$key]['team'] = $row['team_name']; $statistics['worse_atack'][$key]['shield'] = $row['team_shield']; $statistics['worse_atack'][$key]['goals'] = $row['player_goals_pro']; } } $playersGoalsCollection = new Collection($playersGoals); $playersGoals = $playersGoalsCollection->sortBy('player_goals_against', SORT_DESC, SORT_NUMERIC)->toArray(); $playersGoals = array_values($playersGoals); $statistics['worse_defense'] = []; $statistics['worse_defense'][0]['player'] = $playersGoals[0]['player']; $statistics['worse_defense'][0]['team'] = $playersGoals[0]['team_name']; $statistics['worse_defense'][0]['shield'] = $playersGoals[0]['team_shield']; $statistics['worse_defense'][0]['goals'] = $playersGoals[0]['player_goals_against']; foreach ($playersGoals as $key => $row) { if ($statistics['worse_defense'][0]['goals'] == $row['player_goals_against']) { $statistics['worse_defense'][$key]['player'] = $row['player']; $statistics['worse_defense'][$key]['team'] = $row['team_name']; $statistics['worse_defense'][$key]['shield'] = $row['team_shield']; $statistics['worse_defense'][$key]['goals'] = $row['player_goals_against']; } } $this->set('statistics', $statistics); }
public function getRanking($product_id, $videogame) { $players = []; $titles = TableRegistry::get('Users')->find()->select(['Users.id', 'Users.login', 'Users.photo', 'titles' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Users.id = Tournaments.champion_user_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id', 'Users.login', 'Users.photo']); foreach ($titles as $key => $value) { if (isset($value['login'])) { $players[$value['id']]['login'] = $value['login']; } if (isset($value['photo'])) { $players[$value['id']]['photo'] = $value['photo']; } $players[$value['id']]['titles'] = $value['titles']; } $victoriesA = TableRegistry::get('Users')->find()->select(['Users.id', 'Users.login', 'Users.photo', 'victories' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_a_id AND Games.score_a > Games.score_b'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']); foreach ($victoriesA as $key => $value) { if (isset($value['login'])) { $players[$value['id']]['login'] = $value['login']; } if (isset($value['photo'])) { $players[$value['id']]['photo'] = $value['photo']; } if (isset($players[$value['id']]['victories'])) { $players[$value['id']]['victories'] += $value['victories']; } else { $players[$value['id']]['victories'] = $value['victories']; } } $victoriesB = TableRegistry::get('Users')->find()->select(['Users.id', 'victories' => TableRegistry::get('Users')->find()->func()->count('Users.id')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_b_id AND Games.score_b > Games.score_a'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']); foreach ($victoriesB as $key => $value) { if (isset($value['login'])) { $players[$value['id']]['login'] = $value['login']; } if (isset($value['photo'])) { $players[$value['id']]['photo'] = $value['photo']; } if (isset($players[$value['id']]['victories'])) { $players[$value['id']]['victories'] += $value['victories']; } else { $players[$value['id']]['victories'] = $value['victories']; } } $goalA = TableRegistry::get('Users')->find()->select(['Users.id', 'goals' => TableRegistry::get('Users')->find()->func()->sum('Games.score_a')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_a_id AND Games.score_a > Games.score_b'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']); foreach ($goalA as $key => $value) { if (isset($value['login'])) { $players[$value['id']]['login'] = $value['login']; } if (isset($value['photo'])) { $players[$value['id']]['photo'] = $value['photo']; } if (isset($players[$value['id']]['goals'])) { $players[$value['id']]['goals'] += $value['goals']; } else { $players[$value['id']]['goals'] = $value['goals']; } } $goalB = TableRegistry::get('Users')->find()->select(['Users.id', 'goals' => TableRegistry::get('Users')->find()->func()->sum('Games.score_b')])->join(['Games' => ['table' => 'games', 'type' => 'INNER', 'conditions' => 'Users.id = Games.user_b_id AND Games.score_b > Games.score_a'], 'Tournaments' => ['table' => 'tournaments', 'type' => 'INNER', 'conditions' => 'Tournaments.id = Games.tournament_id']])->where(['Tournaments.product_id' => $product_id, 'Tournaments.videogame' => $videogame])->group(['Users.id']); foreach ($goalB as $key => $value) { if (isset($value['login'])) { $players[$value['id']]['login'] = $value['login']; } if (isset($value['photo'])) { $players[$value['id']]['photo'] = $value['photo']; } if (isset($players[$value['id']]['goals'])) { $players[$value['id']]['goals'] += $value['goals']; } else { $players[$value['id']]['goals'] = $value['goals']; } } $playersCollection = new Collection($players); $players = $playersCollection->sortBy('titles', SORT_DESC, SORT_NUMERIC)->toArray(); $players = array_values($players); return $players; }
/** * Tests sort * * @return void */ public function testSortString() { $items = [['a' => ['b' => ['c' => 4]]], ['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]]]; $collection = new Collection($items); $map = $collection->sortBy('a.b.c'); $this->assertInstanceOf('Cake\\Collection\\Collection', $map); $expected = [['a' => ['b' => ['c' => 10]]], ['a' => ['b' => ['c' => 6]]], ['a' => ['b' => ['c' => 4]]]]; $this->assertEquals($expected, $map->toList()); }