public function listOpenQMatchesByTournament($tournamentid)
 {
     $qb = $this->em->createQuery("select m.id as mid,m.matchno,m.date,m.time," . "p.id as pid," . "g.id as gid," . "q.id as rid,q.awayteam,q.rank,gq.id as rgrp " . "from " . $this->entity->getRepositoryPath('QMatchRelation') . " q, " . $this->entity->getRepositoryPath('MatchRelation') . " r, " . $this->entity->getRepositoryPath('Match') . " m, " . $this->entity->getRepositoryPath('Group') . " g, " . $this->entity->getRepositoryPath('Category') . " cat, " . $this->entity->getRepositoryPath('Playground') . " p, " . $this->entity->getRepositoryPath('Group') . " gq " . "where cat.tournament=:tournament and " . "g.category=cat.id and " . "m.group=g.id and " . "p.id=m.playground and " . "q.match=m.id and " . "q.group=gq.id and " . "(select count(r.match) from matchrelations r where r.match=m.id) = 0 " . "order by m.id");
     $qb->setParameter('tournament', $tournamentid);
     // Collect match relations as matches
     $matchList = array();
     foreach ($qb->getResult() as $qmatch) {
         $matchList[$qmatch['matchno']][$qmatch['awayteam'] == 'Y' ? 'A' : 'H'] = $qmatch;
     }
     // Prepare match list for output
     $matches = array();
     foreach ($matchList as $matchRelList) {
         // There must be two relations for each match - otherwise ignore the match
         if (count($matchRelList) == 2) {
             $match = new QMatch();
             $match->setId($matchRelList['H']['mid']);
             $match->setMatchno($matchRelList['H']['matchno']);
             $match->setPid($matchRelList['H']['gid']);
             $match->setPlayground($matchRelList['H']['pid']);
             $match->setDate($matchRelList['H']['date']);
             $match->setTime($matchRelList['H']['time']);
             $match->setGroupA($matchRelList['H']['rgrp']);
             $match->setGroupB($matchRelList['A']['rgrp']);
             $match->setRankA($matchRelList['H']['rank']);
             $match->setRankB($matchRelList['A']['rank']);
             $matches[] = $match;
         }
     }
     // Sort the matches based on schedule
     usort($matches, function (QMatch $match1, QMatch $match2) {
         $p1 = $match2->getPid() - $match1->getPid();
         $p2 = $match2->getDate() - $match1->getDate();
         $p3 = $match2->getPlayground() - $match1->getPlayground();
         $p4 = $match2->getTime() - $match1->getTime();
         if ($p1 == 0 && $p2 == 0 && $p3 == 0 && $p4 == 0) {
             return 0;
         } elseif ($p1 < 0 || $p1 == 0 && $p2 < 0 || $p1 == 0 && $p2 == 0 && $p3 < 0 || $p1 == 0 && $p2 == 0 && $p3 == 0 && $p4 < 0) {
             return 1;
         } else {
             return -1;
         }
     });
     return $matches;
 }
 private function copyMatchForm(Match $match)
 {
     $matchForm = new MatchForm();
     $matchForm->setId($match->getId());
     $matchForm->setPid($match->getGroup()->getId());
     $matchForm->setMatchno($match->getMatchno());
     $matchdate = Date::getDateTime($match->getDate(), $match->getTime());
     $dateformat = $this->get('translator')->trans('FORMAT.DATE');
     $matchForm->setDate(date_format($matchdate, $dateformat));
     $timeformat = $this->get('translator')->trans('FORMAT.TIME');
     $matchForm->setTime(date_format($matchdate, $timeformat));
     $matchForm->setPlayground($match->getPlayground());
     /* @var $homeRel QMatchRelation */
     $homeRel = $this->get('match')->getQMatchRelationByMatch($match->getId(), false);
     if ($homeRel != null) {
         $matchForm->setGroupA($homeRel->getGroup()->getId());
         $matchForm->setRankA($homeRel->getRank());
     }
     /* @var $awayRel QMatchRelation */
     $awayRel = $this->get('match')->getQMatchRelationByMatch($match->getId(), true);
     if ($awayRel != null) {
         $matchForm->setGroupB($awayRel->getGroup()->getId());
         $matchForm->setRankB($awayRel->getRank());
     }
     return $matchForm;
 }