DbObj) $DataObj->DoDbConnect(); $Sql = "select A.FirstName, A.LastName, SO.StartOrder, C.countries_iso_code_3 as countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.GroupId, SO.Result,SO.LockedGrouping, SO.ResultPoints, SO.ResultAttempts, SO.Qualified, SO.Rank, SO.Tshirt, SO.Timer from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? order by $OrderBy"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Round)); // var_dump($DataObj->DbObj->last_query); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } return array(true, '', $SqlResult); } function GetStageResult(&$DataObj, $Competition, $Category) { switch($OrderByField) { default: $OrderBy = 'SO.GroupId asc, SO.StartOrder asc '; break; // case 'total': // $OrderBy = 'SO.GroupId asc, SO.Rank asc'; // break; // case 'total_speed': // $OrderBy = 'SO.GroupId asc, SO.Rank asc'; } if (!$DataObj->DbObj) $DataObj->DoDbConnect(); $Sql = "select A.FirstName, A.LastName, SO.StartOrder, C.countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.GroupId, SO.Result,SO.LockedGrouping, SO.ResultPoints, SO.ResultAttempts, SO.Qualified, SO.Rank, SO.Tshirt, SO.Timer, RoundId from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? order by RoundId desc, $OrderBy"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Round)); //echo $DataObj->DbObj->last_query; if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } return array(true, '', $SqlResult); } function GetOverallResults(&$DataObj, $Competition, $Category, $RoundsConversionTable) { if (!$DataObj->DbObj) $DataObj->DoDbConnect(); $RoundsConversionTable = array_reverse($RoundsConversionTable); $AdjustedRank = 0; foreach($RoundsConversionTable as $Id => $Val) { $Sql = "select A.FirstName, A.LastName, C.countries_iso_code_3 as countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.Rank,SO.Tshirt from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = " . $Val['id'] . " and SO.AthleteId not in ('" . @implode("','", $ExcludedAthletes) . "') and SO.Rank > 0 order by SO.Rank asc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; $OldRank = -1; foreach($SqlResult as $Key => $Value) { //var_dump($Value); if($Value['rank'] > 0) { if ($OldRank != $Value['rank']) { $AdjustedRank = $AdjustedRank + $i; $OldRank = $Value['rank']; $i = 0; } $Value['rank'] = $AdjustedRank; } else $Value['rank'] = '-'; $ArrayRankings[$Value['athleteid']] = $Value; $i++; } $ExcludedAthletes = @array_keys($ArrayRankings); } //var_dump($ArrayRankings); return array(true, '', $ArrayRankings); } function GetUIAAPoints(&$DataObj) { if (!$DataObj->DbObj) $DataObj->DoDbConnect(); $Sql = "select * from c1iwc.rank_points"; $SqlResult = $DataObj->DbObj->getAll($Sql, DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $Ranks[$Value['rank']] = $Value['points']; } return array(true, '', $Ranks); } function GenerateList(&$DataObj, $Competition, $Category, $Round) { if (!$DataObj->DbObj) $DataObj->DoDbConnect(); include_once("Competitions.php"); list($Returned, $Message, $StageConversionTable) = Competitions::GetStagesForCompetition($DataObj, intval($_POST['comp'])); foreach($StageConversionTable as $Result) { $Stages[$Result['id']] = $Result; } $RoundOriginalId = $this->GetOriginalRoundId(&$DataObj, $Competition, $Round); //qualifications if ($RoundOriginalId == 1) { $Sql = "select CS.*, A.Id as Athlete, BIB from c1iwc.Athletes A inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id where CS.CompetitionId = ? and CS.Id = ? order by RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; foreach ($SqlResult as $Athlete) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?,?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Athlete['competitionid'], $Athlete['id'], $Round, $Athlete['athlete'], $i, 0, $Athlete['bib'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, 0, $Athlete['bib'], $Athlete['competitionid'], $Athlete['id'], $Round, $Athlete['athlete'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } return array(true, 'Operation was successfull', true); } // Semifinals if ($RoundOriginalId == 2) { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 1)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; if ($Stages[$Category]['stageid'] == 5 || $Stages[$Category]['stageid'] == 6) { $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? and Qualified = 1 order by Rank desc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } } else { if ($Stages[$Category]['stageid'] == '2' || $Stages[$Category]['stageid'] == '4') { $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? and Qualified = 1 order by StartOrder asc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } } else { $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? and Qualified = 1 order by Rank desc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); //echo $DataObj->DbObj->last_query; if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } } } $i = 1; foreach ($SqlResult as $Athlete) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Athlete['competitionid'], $Athlete['stageid'], $Round, $Athlete['athleteid'], $i, 0, $Athlete['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, 0, $Athlete['tshirt'], $Athlete['competitionid'], $Athlete['stageid'], $Round, $Athlete['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } return array(true, 'Operation was successfull', true); } // finals (se vor sorta in ordine descrescatoare rezultatului din runda precedenta) cei de pe acelasi loc se vor sorta aleator if ($RoundOriginalId == 3) { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 2)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; if ($Stages[$Category] == 5 || $Stages[$Category] == 6) { $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? and SO.StageId = ? and Qualified = 1 order by SO.ResultPoints asc, SO.ResultAttempts desc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $PreviousRoundId, $Category), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $ArrayValues[$Value['resultpoints'] . '_' .$Value['resultattempts']][] = $Value['athleteid']; $ArrayRecords[$Value['athleteid']] = $Value; } foreach($ArrayValues as $Key => $Value) { shuffle($Value); $NewArrayValues[$Key] = $Value; } $i = 1; foreach($NewArrayValues as $Key => $Value) { foreach($Value as $AthleteId) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($ArrayRecords[$AthleteId]['competitionid'], $ArrayRecords[$AthleteId]['stageid'], $Round, $AthleteId, $i, 1, $ArrayRecords[$AthleteId]['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, 1, $ArrayRecords[$AthleteId]['tshirt'], $ArrayRecords[$AthleteId]['competitionid'], $ArrayRecords[$AthleteId]['stageid'], $Round, $AthleteId)); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } } } else { $Sql = "select SO.* from c1iwc.StartOrder SO where CompetitionId = ? and StageId = ? and RoundId = ? and Qualified = 1 order by Rank desc, RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); //var_dump($DataObj->DbObj->last_query); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; //var_dump($Round); foreach ($SqlResult as $Athlete) { // var_dump($Athlete['athleteid'] .' - ' . $i); $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Athlete['competitionid'], $Athlete['stageid'], $Round, $Athlete['athleteid'], $i, 0, $Athlete['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, 0, $Athlete['tshirt'], $Athlete['competitionid'], $Athlete['stageid'], $Round, $Athlete['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } } return array(true, 'Operation was successfull', true); } // finals (se vor sorta in ordine descrescatoare rezultatului din runda precedenta) cei de pe acelasi loc se vor sorta aleator if ($RoundOriginalId == 4) { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 3)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? and Qualified = 1 order by SO.ResultPoints asc, SO.ResultAttempts desc, RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; foreach($SqlResult as $Key => $Value) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'], $i, 0, $Value['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, 0, $Value['tshirt'], $Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } return array(true, 'Operation was successfull', true); } if ($RoundOriginalId == '5') { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 2)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? and Qualified = 1 order by SO.Rank asc, RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; $ArrayGroups = Array(1,3,4,2,2,4,3,1); foreach($SqlResult as $Key => $Value) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'], $i, $ArrayGroups[$i-1], $Value['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, $ArrayGroups[$i-1], $Value['tshirt'], $Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } return array(true,'',true); } if ($RoundOriginalId == '6') { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 5)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? and Qualified = 1 order by SO.Timer asc, RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; $ArrayGroups = Array(1,2,2,1); foreach($SqlResult as $Key => $Value) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'], $i, $ArrayGroups[$i-1], $Value['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, $ArrayGroups[$i-1], $Value['tshirt'], $Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } } if ($RoundOriginalId == '7') { // get round id for previous round $Sql = "select Id from c1iwc.competitions_rounds where CompetitionId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, 6)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $PreviousRoundId = $SqlResult['id']; $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and SO.StageId = ? and RoundId = ? order by Qualified desc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Category, $PreviousRoundId), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; foreach($SqlResult as $Key => $Value) { $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`, TShirt) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'], $i, ($Value['qualified'] == '1' ? '1' : '2'), $Value['tshirt'])); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ?, TShirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, ($Value['qualified'] == '1' ? '1' : '2'), $Value['tshirt'], $Value['competitionid'], $Value['stageid'], $Round, $Value['athleteid'])); if (DB::isError($SqlResult1)) { return array(false, 'Mysql Error:' . $SqlResult1->getMessage(), false); } } $i++; } return array(true,'',true); } } function GenerateStartOrder(&$DataObj, $Competition, $Category, $Round) { if (!$DataObj->DbObj) $DataObj->DoDbConnect(); $RoundOriginalId = $this->GetOriginalRoundId(&$DataObj, $Competition, $Round); if ($RoundOriginalId > 2) return array(false, 'Start Order already generated', false); $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? and GroupId = 1 order by RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Round), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; foreach ($SqlResult as $Athlete) { $Sql = "update c1iwc.StartOrder set StartOrder = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ? and GroupId = 1"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, $Athlete['competitionid'], $Athlete['stageid'], $Athlete['roundid'], $Athlete['athleteid'])); $i++; } $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? and GroupId = 2 order by RAND()"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Round), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $i = 1; foreach ($SqlResult as $Athlete) { $Sql = "update c1iwc.StartOrder set StartOrder = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ? and GroupId = 2"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, $Athlete['competitionid'], $Athlete['stageid'], $Athlete['roundid'], $Athlete['athleteid'])); $i++; } return array(true, 'Operation was successfull', true); } function GetOriginalRoundId(&$DataObj, $Competition, $Round) { $Sql = "select RoundId from c1iwc.competitions_rounds where CompetitionId = ? and Id = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Competition, $Round)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error:' . $SqlResult->getMessage(), false); } $RoundOriginalId = $SqlResult['roundid']; if (!$RoundOriginalId) return array(false, 'Couldn`t retrieve original round id', false); return $RoundOriginalId; } function GenerateGroupForRestOfAthletes(&$DataObj, $Competition, $Category, $Round) { $RoundOriginalId = $this->GetOriginalRoundId(&$DataObj, $Competition, $Round); //qualifications if ($RoundOriginalId == 1) { $Sql = 'SELECT CS. * , A.Id AS Athlete FROM c1iwc.Athletes A INNER JOIN c1iwc.athletes_to_competitions AC ON A.Id = AC.AthleteId INNER JOIN c1iwc.competitions_stages CS ON AC.CompStageId = CS.Id WHERE CS.CompetitionId = ? AND CS.Id = ? AND A.Id NOT IN ( SELECT AthleteId FROM c1iwc.StartOrder SO WHERE SO.CompetitionId = ? AND SO.StageId = ? AND SO.RoundId = ? AND GroupId > 0 ) ORDER BY RAND( ) '; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Category, $Competition, $Category, $Round)); if(is_array($SqlResult) && count($SqlResult) > 0) { $i = 0; foreach ($SqlResult as $Athlete) { $CurrentGroup = ($i % 2) + 1; $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, StartOrder, `GroupId`) values (?, ?, ?, ?, ?, ?)"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Athlete['competitionid'], $Athlete['id'], $Round, $Athlete['athlete'], $i, $CurrentGroup)); if (DB::isError($SqlResult1)) { $Sql = "update c1iwc.StartOrder set StartOrder = ?, `GroupId` = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($i, $CurrentGroup, $Athlete['competitionid'], $Athlete['id'], $Round, $Athlete['athlete'])); } $i++; } return array(true, 'Operation was successfull', true); } } } function UpdateGrouping(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Val) { $Sql = "update c1iwc.StartOrder set GroupId = ? where CompetitionId = ? and StageId = ? And RoundId = ? And AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Val, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '', true); } else { return array(false, 'Nothing to update', false); } } function UpdateZone(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Route, $Zone) { $Sql = "select Result from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Comp, $Stage, $Round, $Athlete), DB_FETCHMODE_ASSOC); $ResultString = $SqlResult[0]['result']; $ExplodedArray = @explode(", ", $ResultString); $RecArray = @explode("_", $ExplodedArray[$Route-1]); $RecArray[0] = ($Zone ? $Zone : "0"); $RecArray[1] = ($RecArray[1] ? $RecArray[1] : "0"); $RecString = @implode("_", $RecArray); $ExplodedArray[$Route-1] = $RecString; $ResultString = @implode(", ", $ExplodedArray); $Sql = "Update c1iwc.StartOrder set Result = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($ResultString, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateAttempt(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Route, $Attempt) { $Sql = "select Result from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Comp, $Stage, $Round, $Athlete), DB_FETCHMODE_ASSOC); $ResultString = $SqlResult[0]['result']; $ExplodedArray = @explode(", ", $ResultString); $RecArray = @explode("_", $ExplodedArray[$Route-1]); $RecArray[0] = ($RecArray[0] ? $RecArray[0] : "0"); $RecArray[1] = ($Attempt ? $Attempt : "0"); $RecString = @implode("_", $RecArray); $ExplodedArray[$Route-1] = $RecString; $ResultString = @implode(", ", $ExplodedArray); $Sql = "Update c1iwc.StartOrder set Result = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($ResultString, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateRank(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Rank) { $Sql = "Update c1iwc.StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Rank, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateDifficulty(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Route, $Difficulty) { $Sql = "select Result from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Comp, $Stage, $Round, $Athlete), DB_FETCHMODE_ASSOC); $ResultString = $SqlResult[0]['result']; $ExplodedArray = @explode(", ", $ResultString); $ExplodedArray[$Route-1] = $Difficulty ? $Difficulty : 0; $ResultString = @implode(", ", $ExplodedArray); $Sql = "Update c1iwc.StartOrder set Result = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($ResultString, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateTShirt(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Val) { $Sql = "Update c1iwc.StartOrder set Tshirt = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Val, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateTime(&$DataObj, $Comp, $Stage, $Round, $Athlete, $Val) { $Sql = "Update c1iwc.StartOrder set Timer = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Val, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function UpdateStartOrder(&$DataObj, $Comp, $Stage, $Round, $Athlete, $StartOrder) { $Sql = "Update c1iwc.StartOrder set `StartOrder` = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($StartOrder, $Comp, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return array(true, '',true); } else { return array(false, 'No changes made', false); } } function GetNotRegisteredAthletes(&$DataObj, $Comp, $Stage, $Round) { $Sql = "select Athletes.Id,Athletes.FirstName, Athletes.LastName from c1iwc.Athletes inner join c1iwc.athletes_licenses on Athletes.Id = athletes_licenses.AthleteId where Year = ? order by LastName, FirstName"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($_SESSION['Season'])); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $Athletes[] = $Value['id']; $Names[] = $Value['lastname'].' '.$Value['firstname']; } $Sql = "select AthleteId from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Comp, $Stage, $Round)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $ExcludedAthletes[] = $Value['athleteid']; } foreach($Athletes as $Key => $Value) { if(!@in_array($Value, $ExcludedAthletes)) { $List[] = Array($Value, $Names[$Key]); } } return array(true, '', $List); } function LockGroups($DataObj, $Competition, $Stage, $Round) { $Sql = "select SO.* from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ?"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round), DB_FETCHMODE_ASSOC); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if (is_array($SqlResult) && count($SqlResult) > 0) { foreach($SqlResult as $Key => $Value) { $Sql = "update c1iwc.StartOrder set LockedGrouping = " . ($Value['lockedgrouping'] == '1' ? '0' : '1') . ' where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?'; $SqlResult = $DataObj->DbObj->query($Sql, Array($Value['competitionid'],$Value['stageid'], $Value['roundid'], $Value['athleteid'])); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } } return Array(true, '', false); } } function GenerateDifficultyResults(&$DataObj, $Competition, $Stage, $Round) { $Sql = "select * from c1iwc.rank_points"; $SqlResult = $DataObj->DbObj->getAll($Sql); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error E10: ' . $SqlResult->getMessage(), false); } if(is_array($SqlResult) && count($SqlResult) > 0) { foreach($SqlResult as $Key => $Value) { $UIAARankings[$Value['rank']] = $Value['points']; } } $Sql = "select AthleteId, GroupId, Result from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? order by GroupId asc "; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); //var_dump($SqlResult); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error E11: ' . $SqlResult->getMessage(), false); } $TotalRoutes = 0; foreach($SqlResult as $Key => $Value){ $Result = explode(',', $Value['result']); if($TotalRoutes < count($Result)){ $TotalRoutes = count($Result); } } foreach($SqlResult as $Key => $Value) { $Result = explode(",", $Value['result']); if($TotalRoutes == 1){ foreach($Result as $Key1 => $Value1) { $ArrayResults[$Value['groupid']][$Key1][$Value['athleteid']] = $Value1; } }else{ for($j=0;$j<$TotalRoutes;$j++){ $ArrayResults[$j][$Value['athleteid']] = floatval($Result[$j]); } } } if($TotalRoutes == 1){ foreach($ArrayResults as $Group => $Routes) { foreach($Routes as $Route => $Values) { arsort($Values); // var_dump($Values); // exit; $Rank = 0; $ArrayTimes = Array(); $OldRezultat = -1; $i = 0; foreach($Values as $Athlete => $Rezultat) { // var_dump($Rezultat); if(floor($Rezultat) == 100) { $Rez = @explode(".", $Rezultat); $ArrayTimes[$Athlete] = $Rez[1] / 100; } } $TopRanks = Array(); asort($ArrayTimes, SORT_NUMERIC); $Keys = array_keys($ArrayTimes); $Keys = array_flip($Keys); $ArKeys = array_keys($Keys); $OldTime = -1; $TopRank = 1; $contor = 0; for($kk = 0; $kk < count($Keys);$kk++) { if($OldTime != $ArrayTimes[$ArKeys[$kk]]) { $TopRanks[$kk] = $TopRank; $OldTime = $ArrayTimes[$ArKeys[$kk]]; $TopRank += $contor; $contor = 1; } else { $contor++; } $TopRanks[$ArKeys[$kk]] = $TopRank; } // var_dump($TopRanks); foreach($Values as $Athlete => $Rezultat) { if ($OldRezultat != $Rezultat) { if (floor($Rezultat) == 100) { $Rank = $TopRanks[$Athlete]; $OldRezultat = $Rezultat; } else { $Rank += $i; if($Rank == 0) $Rank = 1; $OldRezultat = $Rezultat; $i = 0; } } $Rankings[$Group][$Athlete][$Route] = $Rank; $i++; } } } foreach($Rankings as $Group => $Athletes) { foreach($Athletes as $Athlete => $Routes) { foreach($Routes as $Route => $Rank) { $Sql = "update c1iwc.StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank, $Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult1)) { return Array(false, 'Mysql Error E02: ' . $SqlResult1->getMessage(), false); } } } } }else{ // total routes = 2 foreach($ArrayResults as $Route => $Values) { arsort($Values); // var_dump($Values); // exit; $Rank = 0; $ArrayTimes = Array(); $OldRezultat = -1; $i = 0; foreach($Values as $Athlete => $Rezultat) { // var_dump($Rezultat); if(floor($Rezultat) == 100) { $Rez = @explode(".", $Rezultat); $ArrayTimes[$Athlete] = $Rez[1] / 100; } } $TopRanks = Array(); asort($ArrayTimes, SORT_NUMERIC); $Keys = array_keys($ArrayTimes); $Keys = array_flip($Keys); $ArKeys = array_keys($Keys); $OldTime = -1; $TopRank = 1; $contor = 0; for($kk = 0; $kk < count($Keys);$kk++) { if($OldTime != $ArrayTimes[$ArKeys[$kk]]) { $TopRanks[$kk] = $TopRank; $OldTime = $ArrayTimes[$ArKeys[$kk]]; $TopRank += $contor; $contor = 1; } else { $contor++; } $TopRanks[$ArKeys[$kk]] = $TopRank; } // var_dump($TopRanks); foreach($Values as $Athlete => $Rezultat) { if($Rezultat != ''){ if ($OldRezultat != $Rezultat) { if (floor($Rezultat) == 100) { $Rank = $TopRanks[$Athlete]; $OldRezultat = $Rezultat; } else { $Rank += $i; if($Rank == 0) $Rank = 1; $OldRezultat = $Rezultat; $i = 0; } } $Rankings[0][$Athlete][$Route] = $Rank; $SameRanking[0][$Route][$Rank] += 1; }else{ $Rank = ''; } $i++; } } foreach($Rankings as $Group => $Athletes) { foreach($Athletes as $Athlete => $Routes) { foreach($Routes as $Route => $Rank) { $Average = 0; $AdjustedRanking[$Route] = 0; for($i=0;$i<$SameRanking[$Group][$Route][$Rank];$i++) { $Average += $Rank + $i; } $AdjustedRanking[$Route] = $Average / $SameRanking[$Group][$Route][$Rank]; } $TotalPoints[$Athlete] = 1; foreach($AdjustedRanking as $Route => $Val){ $TotalPoints[$Athlete] *= $Val; } } asort($TotalPoints); $Rank = 0; $CurrentRank = -1; $Offset = 1; foreach($TotalPoints as $Athlete => $Points){ if($Points != $CurrentRank){ $Rank += $Offset; $Offset = 1; }else{ $Offset++; } $Sql = "update c1iwc.StartOrder set Rank = ?, ResultPoints = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank, $Points, $Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult1)) { return Array(false, 'Mysql Error E02: ' . $SqlResult1->getMessage(), false); } $CurrentRank = $Points; } } } /* $TotalPoints = Array(); foreach($Rankings as $Group => $Athletes) { foreach($Athletes as $Athlete => $Routes) { foreach($Routes as $Route => $Rank) { $TotalPoints[$Athlete] += $UIAARankings[$Rank]; } } } */ /* arsort($TotalPoints); $Rank = 1; $OldPoints = 0; $Offset = 0; foreach($TotalPoints as $Key => $Value) { if($Value != $OldPoints) { $OldPoints = $Value; $Rank += $Offset; $Offset = 0; } $FinalRankings[$Key] = $Rank; $Offset ++; } foreach($FinalRankings as $Athlete => $Rank) { $Sql = "update StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank, $Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult1)) { return Array(false, 'Mysql Error E02: ' . $SqlResult1->getMessage(), false); } } */ return array(true,'',true); } function GenerateSpeedResults(&$DataObj, $Competition, $Stage, $Round) { $Sql = "select AthleteId, GroupId, Result, Timer from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? order by GroupId asc "; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error E11: ' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $Result = explode(", ", $Value['result']); foreach($Result as $Key1 => $Value1) { $ArrayResults[$Value['groupid']][$Value['athleteid']][$Key1] = ($Value1 == '-' ? 9999 : $Value1); } } foreach($ArrayResults as $Group => $AthleteResult) { foreach($AthleteResult as $Athlete => $RouteResults) { $AthleteTotalResult = 0; foreach($RouteResults as $Key => $Value) { if ($Value == '9999') { $AthleteTotalResult = $Value; break; } else $AthleteTotalResult += $Value; } $ArrayResultsTotal[$Group][$Athlete] = floatval($AthleteTotalResult); } } foreach($ArrayResultsTotal as $Key => $Group) { asort($Group, SORT_NUMERIC); $ArrayResultsTotal[$Key] = $Group; } //var_dump($ArrayResultsTotal); //exit; foreach($ArrayResultsTotal as $Key => $Group) { $Rank = 1; $MovedAthletes = array(); $SortCriteria = array(); foreach($Group as $Athlete => $Total) { if($Total == 0) { $Sql = "update c1iwc.StartOrder set Rank = '' where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); continue; } if($Total == 9999) { $Sql = "update c1iwc.StartOrder set Rank = '9999' where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); continue; } if($ArrayResults[$Key][$Athlete][1] != '9999' && $ArrayResults[$Key][$Athlete][0] != '9999') { $Sql = "update c1iwc.StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank++, $Competition, $Stage, $Round, $Athlete)); } if($ArrayResults[$Key][$Athlete][1] == '9999' && $ArrayResults[$Key][$Athlete][0] != '9999') { $MovedAthletes[] = $Athlete; $SortCriteria[$Athlete] = floatval($ArrayResults[$Key][$Athlete][0]); } } if($SortCriteria) { asort($SortCriteria, SORT_NUMERIC); } foreach($SortCriteria as $Athlete => $Value) { if($Value != '9999') { $Sql = "update c1iwc.StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank++, $Competition, $Stage, $Round, $Athlete)); } } $Sql = "update c1iwc.StartOrder set Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and GroupId = ? and Rank = 0"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank++, $Competition, $Stage, $Round, $Key)); } foreach($SqlResult as $Key => $Value) { $Athlete = $Value['athleteid']; $Group = $Value['groupid']; $Sql = "update c1iwc.StartOrder set Timer = '". ($ArrayResultsTotal[$Group][$Athlete] == '9999' ? '-' : number_format($ArrayResultsTotal[$Group][$Athlete],2,'.',','))."' where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult1)) { return Array(false, 'Mysql Error E02: ' . $SqlResult1->getMessage(), false); } } return Array(true, '', true); } function GenerateSpeedSingleResults(&$DataObj, $Competition, $Stage, $Round) { $Sql = "select AthleteId, GroupId, Result, Timer from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? order by GroupId asc "; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error E11: ' . $SqlResult->getMessage(), false); } foreach($SqlResult as $Key => $Value) { $Result = explode(", ", $Value['result']); foreach($Result as $Key1 => $Value1) { $ArrayResults[$Value['groupid']][$Value['athleteid']][$Key1] = ($Value1 == '-' ? 9999 : $Value1); } } foreach($ArrayResults as $Group => $AthleteResult) { $Min = "9999"; foreach($AthleteResult as $Athlete => $RouteResults) { $Min = min($RouteResults); if($Min == 0) $Min = 9999; $ArrayResultsTotal[$Group][$Athlete] = floatval($Min); } } foreach($ArrayResultsTotal as $Key => $Group) { asort($Group, SORT_NUMERIC); $ArrayResultsTotal[$Key] = $Group; } foreach($ArrayResultsTotal as $Key => $Group) { $Rank = 1; foreach($Group as $Athlete => $Total) { if($Total != '9999') { $Sql = "update c1iwc.StartOrder set Rank = ?, Timer = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rank++, $Total, $Competition, $Stage, $Round, $Athlete)); } else { $Sql = "update c1iwc.StartOrder set Rank = 9999, Timer = 9999 where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); } } } return Array(true, '', true); } function GenerateResults(&$DataObj, $Competition, $Stage, $Round) { $Sql = "select AthleteId, GroupId, Result from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? order by GroupId asc "; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error E01: ' . $SqlResult->getMessage(), false); } if (is_array($SqlResult) && count($SqlResult)) { foreach($SqlResult as $Key => $Value) { // var_dump($Value); $ExplodedArray = @explode(", ", $Value['result']); $PairArray = Array(); $i = 0; foreach($ExplodedArray as $Value1) { $RecArray = Array(); $RecArray = @explode(".", $Value1); $PairArray[] = $RecArray; $Zones[$i][$RecArray[0]] = 1; $i++; } $Results[$Value['groupid']][] = array($Value['athleteid'], $PairArray); } } if ($Results) { foreach($Results as $Group => $Value) { foreach($Value as $Value1) { $Athlete = $Value1[0]; foreach($Value1[1] as $Route => $Pair) { $Zone = $Pair[0]; $Attempts = $Pair[1]; for($i = $Zone; $i >= 1 ; $i --) if($Zones[$Route][$i]) $TotalZone[$Group][$Route + 1]['Zone ' . $i] ++; $NumarIncercari[$Group][$Athlete] += $Attempts; } } } } // var_dump($TotalZone); // calculeaza punctaj zona if(count($TotalZone)) foreach($TotalZone as $Group => $Value) { foreach($Value as $Route => $Value1) { foreach($Value1 as $Zone => $TotalReusite) { $TotalZone[$Group][$Route][$Zone] = Array('Reusite' => $TotalReusite, 'Points' => round(1000 / $TotalReusite,2)); } } } if ($Results) { foreach($Results as $Group => $Value) { foreach($Value as $Value1) { $Athlete = $Value1[0]; $TotalAttempts = 0; foreach($Value1[1] as $Route => $Pair) { $Zone = $Pair[0]; $Attempts = $Pair[1]; for($i = $Zone; $i >= 1 ; $i --) { // $Rezultat[$Group][$Athlete][$Route + 1] += $TotalZone[$Group][$Route + 1]['Zone ' . $i]['Points']; PT FIECARE RUTA if($Zones[$Route][$i]) $Rezultat[$Group][$Athlete]['Rezultat'][$Route+1] += $TotalZone[$Group][$Route + 1]['Zone ' . $i]['Points']; } if(count($Rezultat[$Group][$Athlete]['Rezultat'])) $Rezultat[$Group][$Athlete]['Total'] = array_sum($Rezultat[$Group][$Athlete]['Rezultat']); $Rezultat[$Group][$Athlete]['Attempts'] += $Attempts; } $Rezultat[$Group][$Athlete]['Attempts'] = $Rezultat[$Group][$Athlete]['Attempts']; } uasort($Rezultat[$Group],"cmp"); //var_dump($Rezultat[$Group]); $Rank = 1; $First = true; $OldVal = -1; $OffSet = 0; foreach($Rezultat[$Group] as $Key => $Athlete){ $Val = $Athlete['Total'] .'-'.$Athlete['Attempts']; if(($Val != $OldVal) && !$First){ $Rank += $OffSet; $OffSet = 1; }else{ $OffSet ++; } $Rankings[$Group][$Key] = $Rank; $OldVal = $Val; $First = false; } } } foreach($SqlResult as $Key => $Value) { $Athlete = $Value['athleteid']; $Group = $Value['groupid']; if(!count($Rezultat[$Group][$Athlete]['Rezultat'])) $Rankings[$Group][$Athlete] = ''; $Sql = "update c1iwc.StartOrder set ResultPoints = ?, ResultAttempts = ?, Rank = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult1 = $DataObj->DbObj->query($Sql, Array($Rezultat[$Group][$Athlete]['Rezultat'] ? implode(",", $Rezultat[$Group][$Athlete]['Rezultat']) : '', $Rezultat[$Group][$Athlete]['Attempts'], $Rankings[$Group][$Athlete],$Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult1)) { return Array(false, 'Mysql Error E02: ' . $SqlResult1->getMessage(), false); } } return Array(true, '', true); } function UpdateQualifiedFlag(&$DataObj, $Competition, $Stage, $Round, $Athlete, $Value) { $Sql = "update c1iwc.StartOrder set Qualified = ? where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Value, $Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return Array(true, '', true); } else return Array(false, 'No changes made', false); } function RemoveAthlete(&$DataObj, $Competition, $Stage, $Round, $Athlete) { $Sql = "delete from c1iwc.StartOrder where CompetitionId = ? and StageId = ? and RoundId = ? and AthleteId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return Array(true, '', true); } else return Array(false, 'No changes made', false); } function AddAthlete(&$DataObj, $Competition, $Stage, $Round, $Athlete) { $Sql = "select * from c1iwc.athletes_to_competitions where AthleteId = ? and CompStageId = ?"; $SqlResult = $DataObj->DbObj->getRow($Sql, Array($Athlete, $Stage)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if(count($SqlResult) == 0) { $Sql = "insert into c1iwc.athletes_to_competitions (Id, AthleteId, CompStageId) values (NULL,?,?)"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Athlete, $Stage)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } } $Sql = "insert into c1iwc.StartOrder (CompetitionId, StageId, RoundId, AthleteId, Tshirt, StartOrder, GroupId) values (?,?,?,?,0,0,0)"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Round, $Athlete)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return Array(true, '', true); } else return Array(false, 'Couldn\'t add the athlete', false); } function RemoveOldResults($DataObj, $Competition, $Stage) { $Sql = "delete from c1iwc.Results where CompetitionId = ? and StageId = ?"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Competition, $Stage)); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } if ($DataObj->DbObj->affectedRows() > 0) { return Array(true, '', true); } else return Array(false, 'No changes made', false); } function InsertNewResult($DataObj, $Competition, $Stage, $Athlete, $AthleteFirstName, $AthleteLastName, $Rank, $UIAAPoints) { $Sql = "insert into c1iwc.Results (CompetitionId, StageId, AthleteId, FirstName, LastName, Rank, UIAAPoints) values (?, ?, ?, ?, ?, ?, ?)"; $SqlResult = $DataObj->DbObj->query($Sql, Array($Competition, $Stage, $Athlete, $AthleteFirstName, $AthleteLastName, $Rank, $UIAAPoints)); //var_dump($DataObj->DbObj->last_query); if (DB::isError($SqlResult)) { return Array(false, 'Mysql Error: ' . $SqlResult->getMessage(), false); } else return Array(true,'',true); } function GetResultsForStages($DataObj, $Stages) { $Sql = "set NAMES utf8"; $SqlResult = $DataObj->DbObj->query($Sql); $Sql = "select c1iwc.Results.*, Countries.countries_iso_code_3 as Country, users.FederationName from c1iwc.Results left join c1iwc.Athletes on c1iwc.Results.AthleteId = c1iwc.Athletes.Id left join c1iwc.Countries on c1iwc.Athletes.Country = c1iwc.Countries.countries_id left join users on c1iwc.Athletes.FederationId = users.UserId where StageId in ('" . @implode("','", $Stages) . "')"; $SqlResult = $DataObj->DbObj->getAll($Sql); // var_dump($DataObj->DbObj->last_query); foreach($SqlResult as $Key => $Value) { $Result[$Value['athleteid']][$Value['competitionid']] = Array($Value['firstname'], $Value['lastname'], $Value['rank'], $Value['uiaapoints'], $Value['country'], $Value['federationname']); $TotalResult[$Value['athleteid']] += $Value['uiaapoints']; } if(is_array($TotalResult)) arsort($TotalResult, SORT_NUMERIC); else $TotalResult = array(); return Array(true, '', Array($Result, $TotalResult)); } function GetQualificationResults($DataObj, $Competition, $Stage, $Round) { $Sql = "select A.FirstName, A.LastName, SO.StartOrder, C.countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.GroupId, SO.Result,SO.LockedGrouping, SO.ResultPoints, SO.ResultAttempts, SO.Qualified, SO.Rank, SO.Tshirt, SO.Timer, RoundId from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? order by Rank asc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); // var_dump($DataObj->DbObj->last_query); $HasOneGroup = true; foreach($SqlResult as $Key => $Value) { $Result[$Value['athleteid']][$Value['groupid']] = $Value['result']; $Attempts[$Value['athleteid']][$Value['groupid']] = $Value['resultattempts']; $Points[$Value['athleteid']][$Value['groupid']] = $Value['resultpoints']; $Ranks[$Value['athleteid']][$Value['groupid']] = $Value['rank']; $ArrayGroups[] = $Value['groupid']; } $ArrayGroups = @array_unique($ArrayGroups); return array(true, '', $Result, $ArrayGroups, $Ranks, $Attempts, $Points); } function GetSemiFinalResults($DataObj, $Competition, $Stage, $Round) { $Sql = "select A.FirstName, A.LastName, SO.StartOrder, C.countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.GroupId, SO.Result,SO.LockedGrouping, SO.ResultPoints, SO.ResultAttempts, SO.Qualified, SO.Rank, SO.Tshirt, SO.Timer, RoundId from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? order by Rank asc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); // var_dump($DataObj->DbObj->last_query); foreach($SqlResult as $Key => $Value) { $Result[$Value['athleteid']] = $Value['result']; $Ranks[$Value['athleteid']] = $Value['rank']; } return Array(true, '', $Result, $Rank); } function GetFinalResults($DataObj, $Competition, $Stage, $Round) { $Sql = "select A.FirstName, A.LastName, SO.StartOrder, C.countries_name, AC.AthleteId, CS.CompetitionId, AC.CompStageId, SO.GroupId, SO.Result,SO.LockedGrouping, SO.ResultPoints, SO.ResultAttempts, SO.Qualified, SO.Rank, SO.Tshirt, SO.Timer, RoundId from c1iwc.StartOrder SO inner join c1iwc.Athletes A on SO.AthleteId = A.Id inner join c1iwc.athletes_to_competitions AC on A.Id = AC.AthleteId inner join c1iwc.competitions_stages CS on AC.CompStageId = CS.Id and AC.CompStageId = SO.StageId inner join c1iwc.Countries C on A.Country = C.countries_id where CS.CompetitionId = ? and AC.CompStageId = ? and RoundId = ? order by Rank asc"; $SqlResult = $DataObj->DbObj->getAll($Sql, Array($Competition, $Stage, $Round)); // var_dump($DataObj->DbObj->last_query); foreach($SqlResult as $Key => $Value) { $Result[$Value['athleteid']] = $Value['result']; $Result1[$Value['groupid']][] = $Value['athleteid']; $Ranks[$Value['athleteid']] = $Value['rank']; $Qualified[$Value['athleteid']] = $Value['qualified']; } return Array(true, '', $Result, $Result1,$Ranks, $Qualified); } } function cmp($a, $b){ if($a['Total'] == $b['Total'] && $a['Attempts'] == $b['Attempts']) return 0; if($a['Total'] == $b['Total'] && ($a['Attempts'] > $b['Attempts'])) return 1; if($a['Total'] == $b['Total'] && ($a['Attempts'] < $b['Attempts'])) return -1; if($a['Total'] > $b['Total']) return -1; if($a['Total'] < $b['Total']) return 1; } ?>