function computeCompetitionEvents ( $eventSpecs ) {
#----------------------------------------------------------------------
  global $chosenCompetitionId, $chosenAllResults;

  $events = "";
  foreach( getEventSpecsEventIds( $eventSpecs ) as $eventId ){
    $url = $chosenAllResults ? "#$eventId" : "competition.php?competitionId=$chosenCompetitionId&allResults=1#$eventId";
    $events .= '[{' . eventCellName( $eventId ) . '}{' . $url . '}]';
  }
  return $events;
}
function checkEvents()
{
    #----------------------------------------------------------------------
    global $competitionCondition, $competitionDescription;
    echo "<hr /><p>Checking <b> events for {$competitionDescription}</b>... (wait for the result message box at the end)</p>\n";
    #--- Get events from Results and Competitions
    $eventsResults = dbQuery("SELECT r.competitionId, r.eventId, c.eventSpecs FROM Results r, Competitions c WHERE c.id = r.competitionId AND r.eventId != '333mbo' {$competitionCondition} GROUP BY r.competitionId, r.eventId");
    # Grossness to handle the fact that the competition id in the Competitions table
    # is the "id" column, not the "competitionId" column.
    $competitionConditionForCompetitionsTable = str_replace("competitionId", "id", $competitionCondition);
    $eventsCompetition = dbQuery("SELECT id, eventSpecs FROM Competitions WHERE 1 {$competitionConditionForCompetitionsTable}");
    #--- Group events by competitions.
    foreach ($eventsResults as $eventResults) {
        extract($eventResults);
        $arrayEventsResults[$competitionId][] = $eventId;
    }
    foreach ($eventsCompetition as $eventCompetition) {
        extract($eventCompetition);
        $arrayEventsCompetition[$id] = getEventSpecsEventIds($eventSpecs);
    }
    $ok = true;
    #--- Compare events.
    if ($arrayEventsResults) {
        foreach (array_keys($arrayEventsResults) as $competitionId) {
            # Sort tables to compare them.
            sort($arrayEventsResults[$competitionId], SORT_STRING);
            sort($arrayEventsCompetition[$competitionId], SORT_STRING);
            if ($arrayEventsResults[$competitionId] != $arrayEventsCompetition[$competitionId]) {
                $ok = false;
                echo "<p>Update competition {$competitionId}.<br />\n";
                $intersect = array_intersect($arrayEventsResults[$competitionId], $arrayEventsCompetition[$competitionId]);
                $resultsOnly = array_diff($arrayEventsResults[$competitionId], $arrayEventsCompetition[$competitionId]);
                $competitionOnly = array_diff($arrayEventsCompetition[$competitionId], $arrayEventsResults[$competitionId]);
                echo "  Old events list: " . implode(' ', $intersect) . " <b style='color:#F00'>" . implode(' ', $competitionOnly) . "</b><br />\n";
                echo "  New events list: " . implode(' ', $intersect) . " <b style='color:#3C3'>" . implode(' ', $resultsOnly) . "</b><br />\n";
                dbCommand("UPDATE Competitions SET eventSpecs='" . implode(' ', $arrayEventsResults[$competitionId]) . "' WHERE id='{$competitionId}'");
            }
        }
    }
    noticeBox2($ok, 'No mistakes found in the database', 'Some errors were fixed, you *should* check what has been updated');
}
function showPreregList()
{
    #----------------------------------------------------------------------
    global $chosenCompetitionId;
    if (getBooleanParam('isPreregSubmit')) {
        savePreregForm();
    }
    $eventId = getNormalParam('eventId');
    if ($eventId) {
        showPsychSheet($eventId);
        return;
    }
    echo "<h1>Registered competitors</h1><br />";
    #--- Get the data.
    $preregs = dbQuery("SELECT * FROM Preregs WHERE competitionId = '{$chosenCompetitionId}' AND status='a' ORDER BY countryId, name");
    $competition = getFullCompetitionInfos($chosenCompetitionId);
    #--- Get all events of the competition.
    $eventList = getEventSpecsEventIds($competition['eventSpecs']);
    $headerEvent = "";
    $headerEventLink = "";
    foreach ($eventList as $event) {
        $headerEvent .= "|{$event}";
        $headerEventLink .= "|<a href='c.php?list=1&competitionId={$chosenCompetitionId}&eventId={$event}'>{$event}</a>";
    }
    for ($i = 3; $i < 3 + count($eventList); $i++) {
        $tableStyle[$i] = 'class="c"';
    }
    $tableStyle[3 + count($eventList)] = 'class="f"';
    tableBegin('results', 4 + count($eventList));
    $countPerson = 0;
    $countCountry = 0;
    foreach ($preregs as $prereg) {
        extract($prereg);
        if (!($countPerson % 20)) {
            if ($countPerson) {
                tableHeader(explode('|', "#|Person|Citizen of{$headerEvent}|"), $tableStyle);
            } else {
                if (isset($standAlone)) {
                    tableHeader(explode('|', "#|Person|Citizen of{$headerEvent}|"), $tableStyle);
                } else {
                    tableHeader(explode('|', "#|Person|Citizen of{$headerEventLink}|"), $tableStyle);
                }
            }
        }
        $countPerson += 1;
        #--- Compute the row.
        $row = array($countPerson);
        if ($personId) {
            if (preg_match('/competition_registration.php/', $_SERVER['PHP_SELF'])) {
                $row[] = "<a target='_blank' class='p' href='p.php?i={$personId}'>{$name}</a>";
            } else {
                $row[] = personLink($personId, $name);
            }
        } else {
            $row[] = $name;
        }
        $row[] = $countryId;
        if (!isset($listCountries[$countryId])) {
            $listCountries[$countryId] = 1;
            $countCountry += 1;
        }
        $personEvents = 0;
        $eventIdsList = array_flip(explode(' ', $eventIds));
        foreach ($eventList as $event) {
            if (isset($eventIdsList[$event])) {
                $row[] = 'X';
                $countEvents[$event] = isset($countEvents[$event]) ? $countEvents[$event] + 1 : 1;
                $personEvents += 1;
            } else {
                $row[] = '-';
            }
        }
        $row[] = $personEvents;
        tableRow($row);
    }
    $row = array('', 'Total', $countCountry);
    foreach ($eventList as $event) {
        if (isset($countEvents[$event])) {
            $row[] = $countEvents[$event];
        } else {
            $row[] = 0;
        }
    }
    $row[] = '';
    tableHeader($row, $tableStyle);
    tableEnd();
}