/** * Get the match schedule using Round Robin scheme * @param array ordered list of project teams * @param int $scheduling use 0 for a single and 1 for a double Round Robin scheme * @return the match schedule (array) if everything went ok, false otherwise */ private function getRoundRobinSchedule($teams, $scheduling) { require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/rrobin.php'; $helper = new RRobin(); $result = $helper->create($teams); if (!$result) { return false; } $schedule = $helper->getSchedule($scheduling + 1); return $schedule; }
/** * Populate project with matchdays * * @param int $scheduling scheduling type * @param string $time start time for games * @param int $interval interval between rounds * @param string $start start date for new roundsrounds * @param string $roundname round name format (use %d for round number) * @return boolean true on success */ function populate($project_id, $scheduling, $time, $interval, $start, $roundname, $teamsorder = null) { if (!strtotime($start)) { $start = strftime('%Y-%m-%d'); } if (!preg_match("/^[0-9]+:[0-9]+\$/", $time)) { $time = '20:00'; } $teams = $this->getProjectTeams(); if ($teamsorder) { $ordered = array(); foreach ($teamsorder as $ptid) { foreach ($teams as $t) { if ($t->projectteam_id == $ptid) { $ordered[] = $t; break; } } } if (count($ordered)) { $teams = $ordered; } } // diddipoeler $rrteams = array(); foreach ($teams as $team) { $rrteams[] = $team; } $roundrobin = new roundrobin($rrteams); $roundrobin->free_ticket = false; // free tickets off $roundrobin->create_matches(); echo '<pre>', print_r($roundrobin->matches, true), '</pre><br>'; if ($roundrobin->finished) { $i = 1; while ($roundrobin->next_matchday()) { echo "-------Matchday " . $i . "-------<br />"; while ($match = $roundrobin->next_match()) { //echo $match[0]." <b>vs</b> ".$match[1]."<br />"; } $i++; echo "<br />"; } } if (!$teams || !count($teams)) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_NO_TEAM')); return false; } $rounds = $this->getData(); $rounds = $rounds ? $rounds : array(); if ($scheduling < 2) { require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'RRobin.class.php'; $helper = new RRobin(); $helper->create($teams); $schedule = $helper->getSchedule($scheduling + 1); } else { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_UNDEFINED_SCHEDULING')); return false; } $current_date = null; $current_code = 0; foreach ($schedule as $k => $games) { if (isset($rounds[$k])) { $round_id = $rounds[$k]->id; $current_date = $rounds[$k]->round_date_first; $current_code = $rounds[$k]->roundcode; } else { $round = JTable::getInstance('Round', 'Table'); $round->project_id = $project_id; $round->round_date_first = strtotime($current_date) ? strftime('%Y-%m-%d', strtotime($current_date) + $interval * 24 * 3600) : $start; $round->round_date_last = $round->round_date_first; $round->roundcode = $current_code ? $current_code + 1 : 1; $round->name = sprintf($roundname, $round->roundcode); if (!($round->check() && $round->store())) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_ROUND') . ': ' . $round->getError()); return false; } $current_date = $round->round_date_first; $current_code = $round->roundcode; $round_id = $round->id; } // create games ! foreach ($games as $g) { if (!isset($g[0]) || !isset($g[1])) { // happens if number of team is odd ! one team gets a by continue; } $game = JTable::getInstance('Match', 'Table'); $game->round_id = $round_id; $game->division_id = 0; $game->projectteam1_id = $g[0]->projectteam_id; $game->projectteam2_id = $g[1]->projectteam_id; $game->published = 1; $game->match_date = $current_date . ' ' . $time; if (!($game->check() && $game->store())) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_GAME') . ': ' . $game->getError()); return false; } } } //echo '<pre>';print_r($schedule); echo '</pre>'; //exit; return true; }
/** * Populate project with matchdays * * @param int $project_id project id * @param int $scheduling scheduling type * @param string $time start time for games * @param int $interval interval between rounds * @param string $start start date for new roundsrounds * @param string $roundname round name format (use %d for round number) * @param array $teamsorder list of teams * @param int $matchnummer starting by number, increasing by 1 * @return boolean true on success */ function populate($project_id, $scheduling, $time, $interval, $start, $roundname, $teamsorder = null, $iMatchnumber = 0) { if (!strtotime($start)) { $start = strftime('%Y-%m-%d'); } if (!preg_match("/^[0-9]+:[0-9]+\$/", $time)) { $time = '20:00'; } $teams = $this->getProjectTeams(); if ($teamsorder) { $ordered = array(); foreach ($teamsorder as $ptid) { foreach ($teams as $t) { if ($t->projectteam_id == $ptid) { $ordered[] = $t; break; } } } if (count($ordered)) { $teams = $ordered; } } if (!$teams || !count($teams)) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_NO_TEAM')); return false; } $rounds = $this->getData(); $rounds = $rounds ? $rounds : array(); if ($scheduling < 2) { require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/rrobin.php'; $helper = new RRobin(); $helper->create($teams); $schedule = $helper->getSchedule($scheduling + 1); } else { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_UNDEFINED_SCHEDULING')); return false; } $current_date = null; $current_code = 0; foreach ($schedule as $k => $games) { if (isset($rounds[$k])) { $round_id = $rounds[$k]->id; $current_date = $rounds[$k]->round_date_first; $current_code = $rounds[$k]->roundcode; } else { $tblRound = JTable::getInstance('Round', 'Table'); $tblRound->project_id = $project_id; $tblRound->round_date_first = strtotime($current_date) ? strftime('%Y-%m-%d', strtotime($current_date) + $interval * 24 * 3600) : $start; $tblRound->round_date_last = $round->round_date_first; $tblRound->roundcode = $current_code ? $current_code + 1 : 1; $tblRound->name = sprintf($roundname, $tblRound->roundcode); if (!($tblRound->check() && $tblRound->store())) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_ROUND') . ': ' . $tblRound->getError()); return false; } $current_date = $tblRound->round_date_first; $current_code = $tblRound->roundcode; $round_id = $tblRound->id; } // create games ! // we need to convert game date+time to utc $project_tz = $this->getProject()->timezone; $utc_tz = new DateTimeZone('UTC'); $date = JFactory::getDate($current_date . ' ' . $time, $project_tz)->setTimezone($utc_tz); $utc_sql_date = $date->toSql(); foreach ($games as $g) { if (!isset($g[0]) || !isset($g[1])) { // happens if number of team is odd ! one team gets a by continue; } $tblMatch = JTable::getInstance('Match', 'Table'); $tblMatch->round_id = $round_id; $tblMatch->projectteam1_id = $g[0]->projectteam_id; $tblMatch->projectteam2_id = $g[1]->projectteam_id; $tblMatch->match_date = $utc_sql_date; $tblMatch->published = 1; if ($iMatchnumber > 0) { $tblMatch->match_number = $iMatchnumber++; } if (!($tblMatch->check() && $tblMatch->store())) { $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_GAME') . ': ' . $tblMatch->getError()); return false; } } } return true; }