function checkRounds()
{
    #----------------------------------------------------------------------
    global $competitionCondition, $competitionDescription;
    echo "<hr /><p>Checking <b> rounds for {$competitionDescription}</b>... (wait for the result message box at the end)</p>\n";
    #--- Get the number of competitors per round
    $roundRows = dbQuery("\n    SELECT   count(result.id) nbPersons, result.competitionId, competition.year, competition.month, competition.day, result.eventId, result.roundId, round.cellName, result.formatId,\n             CASE result.formatId WHEN '2' THEN BIT_AND( IF( result.value2,1,0)) WHEN '3' THEN BIT_AND( IF( result.value3,1,0)) WHEN 'm' THEN BIT_AND( IF( result.value3,1,0)) WHEN 'a' THEN BIT_AND( IF( result.value5 <> 0,1,0)) ELSE 1 END isNotCombined\n    FROM     Results result, Competitions competition, Rounds round\n    WHERE    competition.id = competitionId\n      {$competitionCondition}\n      AND    (( eventId <> '333mbf' ) OR (( competition.year = 2009 ) AND ( competition.month > 1 )) OR ( competition.year > 2009 ))\n      AND    result.roundId <> 'b'\n      AND    result.roundId = round.id\n    GROUP BY competitionId, eventId, roundId\n    ORDER BY year desc, month desc, day desc, competitionId, eventId, round.rank\n  ");
    #--- Get the number of competitors per event
    $eventRows = dbQuery("\n    SELECT   count(distinct result.personId) nbPersons, competitionId, eventId\n    FROM     Results result, Competitions competition\n    WHERE    competition.id = competitionId\n      {$competitionCondition}\n      AND    (( eventId <> '333mbf' ) OR (( competition.year = 2009 ) AND ( competition.month > 1 )) OR ( competition.year > 2009 ))\n      AND    result.roundId <> 'b'\n    GROUP BY competitionId, eventId\n    ORDER BY year desc, month desc, day desc, competitionId, eventId\n  ");
    #--- Begin the form
    echo "<form action='check_rounds_ACTION.php' method='post'>\n";
    $prevEvent = '';
    $wrongs = 0;
    foreach ($roundRows as $i => $roundRow) {
        list($nbPersons, $competitionId, $year, $month, $day, $eventId, $roundId, $roundCellName, $formatId, $isNotCombined) = $roundRow;
        $event = "{$competitionId}|{$eventId}";
        #--- First round
        if ($event != $prevEvent) {
            $nbRounds = 1;
            $eventRow = array_shift($eventRows);
            list($nbTotalPersons, $eventCompetitionId, $eventEventId) = $eventRow;
            assert($eventCompetitionId == $competitionId);
            assert($eventEventId == $eventId);
            #--- Checks round names
            if ($prevEvent) {
                list($prevCompetitionId, $prevEventId) = explode('|', $prevEvent);
                $wrongs += checkRoundNames($roundInfos, $prevCompetitionId, $prevEventId);
            }
            $roundInfos = array();
            #--- Checks for qualification round
            $isThisRoundQuals = ($roundId == '0' or $roundId == 'h');
            if ($nbTotalPersons != $nbPersons and !$isThisRoundQuals) {
                echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}_{$roundId}'>{$competitionId} - {$eventId} - {$roundId}</a></p>";
                #--- Peek at next roundId
                if ($i + 1 < count($roundRows)) {
                    #--- Should be true.
                    $nextRoundId = $roundRows[$i + 1]['roundId'];
                    showQualifications($competitionId, $eventId, $roundId, $nextRoundId);
                }
                echo "<p>Not all persons that competed in {$eventId} are in {$roundCellName}. It should thus be indicated as Qualification round<p/>";
                addQuals($competitionId, $eventId);
                echo "<br /><hr />";
                $isThisRoundQuals = true;
                $wrongs++;
            }
            if ($nbTotalPersons == $nbPersons and $isThisRoundQuals) {
                echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}_{$roundId}'>{$competitionId} - {$eventId} - {$roundId}</a></p>";
                #--- Peek at next roundId
                if ($i + 1 < count($roundRows)) {
                    #--- Is not always true.
                    if ($roundRows[$i + 1]['competitionId'] == $competitionId && $roundRows[$i + 1]['eventId'] == $eventId) {
                        #--- Idem
                        $nextRoundId = $roundRows[$i + 1]['roundId'];
                        showQualifications($competitionId, $eventId, $roundId, $nextRoundId);
                    }
                }
                echo "<p>All persons that competed in {$eventId} are in {$roundCellName}. It should thus not be indicated as Qualification round</p>";
                removeQuals($competitionId, $eventId);
                echo "<br /><hr />";
                $isThisRoundQuals = false;
                $wrongs++;
            }
        } else {
            $isThisRoundQuals = false;
            # Article 9m, since April 9, 2008
            if (mktime(0, 0, 0, $month, $day, $year) >= mktime(0, 0, 0, 4, 9, 2008)) {
                if ($nbRounds > 1 and $nbTotalPersons < 8 or $nbRounds > 2 and $nbTotalPersons < 16 or $nbRounds > 3 and $nbTotalPersons < 100 or $nbRounds > 4) {
                    echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}_{$roundId}'>{$competitionId} - {$eventId} - {$roundId}</a></p>";
                    echo "<p>There are {$nbRounds} rounds for event {$eventId}, but only {$nbTotalPersons} competitors in total</p>";
                    removeRound($competitionId, $eventId, $nbRounds);
                    echo "<br /><hr />";
                    $wrongs++;
                }
            }
            # Article 9m/n/o, since July 20, 2006 until April 8, 2008
            if (mktime(0, 0, 0, $month, $day, $year) >= mktime(0, 0, 0, 7, 20, 2006) and mktime(0, 0, 0, $month, $day, $year) < mktime(0, 0, 0, 4, 9, 2008)) {
                if ($nbRounds > 2 and $nbTotalPersons < 16 or $nbRounds > 3 and $nbTotalPersons < 100 or $nbRounds > 4) {
                    echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}_{$roundId}'>{$competitionId} - {$eventId} - {$roundId}</a></p>";
                    echo "<p>There are {$nbRounds} rounds for event {$eventId}, but only {$nbTotalPersons} competitors in total</p>";
                    removeRound($competitionId, $eventId, $nbRounds);
                    echo "<br /><hr />";
                    $wrongs++;
                }
            }
            $nbQualPersons = $isPrevRoundQuals ? getQualifications($competitionId, $eventId, $prevRoundId, $roundId) : $nbPersons;
            # Article 9p1, since April 14, 2010
            if (mktime(0, 0, 0, $month, $day, $year) >= mktime(0, 0, 0, 4, 14, 2010)) {
                if ($nbQualPersons > 3 * $prevNbPersons / 4) {
                    echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}'>{$competitionId} - {$eventId}</a></p>";
                    showQualifications($competitionId, $eventId, $prevRoundId, $roundId);
                    echo "<p>From round {$prevRoundCellName} with {$prevNbPersons} competitors, {$nbQualPersons} were qualified to round {$roundCellName} which is more than 75%</p>";
                    echo "<br /><hr />";
                    $wrongs++;
                }
            }
            # Article 9p, since July 20, 2006 until April 13, 2010
            if (mktime(0, 0, 0, $month, $day, $year) >= mktime(0, 0, 0, 7, 20, 2006) and mktime(0, 0, 0, $month, $day, $year) < mktime(0, 0, 0, 4, 14, 2010)) {
                if ($nbQualPersons >= $prevNbPersons) {
                    echo "<p style='margin-top:2em; margin-bottom:0'><a href='https://www.worldcubeassociation.org/results/c.php?i={$competitionId}&allResults=1#e{$eventId}'>{$competitionId} - {$eventId}</a></p>";
                    showQualifications($competitionId, $eventId, $prevRoundId, $roundId);
                    echo "<p>From round {$prevRoundCellName} to round {$roundCellName}, at least one competitor must not proceed</p>";
                    echo "<br /><hr />";
                    $wrongs++;
                }
            }
        }
        $nbRounds += 1;
        $prevNbPersons = $nbPersons;
        $prevEvent = $event;
        $prevRoundId = $roundId;
        $prevRoundCellName = $roundCellName;
        $isPrevRoundQuals = $isThisRoundQuals;
        $roundInfos[] = array($roundId, $roundCellName, $formatId, $isNotCombined);
    }
    #--- Tell the result.
    $date = wcaDate();
    noticeBox2(!$wrongs, "We didn't find any mistake.<br />{$date}", "<p>Darn! We disagree: {$wrongs} incorrect rounds found<br /><br />{$date}</p>" . "<p>Choose the changes you agree with above, then click the 'Execute...' button below. It will result in something like the following.</p>" . "<pre>I'm doing this:\n" . "UPDATE Results SET pos=111 WHERE id=11111\n" . "UPDATE Results SET pos=222 WHERE id=22222\n" . "UPDATE Results SET pos=333 WHERE id=33333\n" . "</pre>");
    #--- If differences were found, offer to fix them.
    if ($wrongs) {
        echo "<center><input type='submit' value='Execute the agreed changes!' /></center>\n";
    }
    #--- Finish the form.
    echo "</form>\n";
}
function checkRounds () {
#----------------------------------------------------------------------
  global $competitionCondition, $competitionDescription;

  echo "<hr /><p>Checking <b> rounds for $competitionDescription</b>... (wait for the result message box at the end)</p>\n";

  #--- Get the number of competitors per round
  $roundRows = dbQuery("
    SELECT   count(result.id) nbPersons, result.competitionId, competition.year, competition.month, competition.day, result.eventId, result.roundId, round.cellName, result.formatId,
             CASE result.formatId WHEN '2' THEN BIT_AND( IF( result.value2,1,0)) WHEN '3' THEN BIT_AND( IF( result.value3,1,0)) WHEN 'm' THEN BIT_AND( IF( result.value3,1,0)) WHEN 'a' THEN BIT_AND( IF( result.value5 <> 0,1,0)) ELSE 1 END isNotCombined
    FROM     Results result, Competitions competition, Rounds round
    WHERE    competition.id = competitionId
      $competitionCondition
      AND    (( eventId <> '333mbf' ) OR (( competition.year = 2009 ) AND ( competition.month > 1 )) OR ( competition.year > 2009 ))
      AND    result.roundId <> 'b'
      AND    result.roundId = round.id
    GROUP BY competitionId, eventId, roundId
    ORDER BY year desc, month desc, day desc, competitionId, eventId, round.rank
  ");

  #--- Get the number of competitors per event
  $eventRows = dbQuery("
    SELECT   count(distinct result.personId) nbPersons, competitionId, eventId
    FROM     Results result, Competitions competition
    WHERE    competition.id = competitionId
      $competitionCondition
      AND    (( eventId <> '333mbf' ) OR (( competition.year = 2009 ) AND ( competition.month > 1 )) OR ( competition.year > 2009 ))
      AND    result.roundId <> 'b'
    GROUP BY competitionId, eventId
    ORDER BY year desc, month desc, day desc, competitionId, eventId
  ");

  #--- Begin the form
  echo "<form action='check_rounds_ACTION.php' method='post'>\n";

  $prevEvent = '';
  $wrongs = 0;
  foreach( $roundRows as $i => $roundRow ){
    list( $nbPersons, $competitionId, $year, $month, $day, $eventId, $roundId, $roundCellName, $formatId, $isNotCombined ) = $roundRow;
    $event = "$competitionId|$eventId";
    $competitionDate = mktime( 0, 0, 0, $month, $day, $year );

    $subsequentRoundCount = 0;
    while(true) {
      $nextRoundIndex = $i + $subsequentRoundCount + 1;
      if($nextRoundIndex >= count($roundRows)) {
        break;
      }
      $nextRoundRow = $roundRows[$nextRoundIndex];
      if($nextRoundRow['competitionId'] != $competitionId || $nextRoundRow['eventId'] != $eventId) {
        break;
      }
      $subsequentRoundCount++;
    }

    # Expanded Article 9m, since April 18, 2016
    if (mktime( 0, 0, 0, 4, 18, 2016 ) <= $competitionDate) {
      if($nbPersons <= 7) {
        # https://www.worldcubeassociation.org/regulations/#9m3: Rounds with 7 or fewer competitors must not have subsequent rounds.
        $maxAllowedSubsequentRoundCount = 0;
      } else if($nbPersons <= 15) {
        # https://www.worldcubeassociation.org/regulations/#9m2: Rounds with 15 or fewer competitors must have at most one subsequent round.
        $maxAllowedSubsequentRoundCount = 1;
      } else if($nbPersons <= 99) {
        # https://www.worldcubeassociation.org/regulations/#9m1: Rounds with 99 or fewer competitors must have at most two subsequent rounds.
        $maxAllowedSubsequentRoundCount = 2;
      } else {
        # https://www.worldcubeassociation.org/regulations/#9m: Events must have at most four rounds.
        $maxAllowedSubsequentRoundCount = 3;
      }

      if($subsequentRoundCount > $maxAllowedSubsequentRoundCount) {
        echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId - $roundId</a></p>";
        echo "<p>There were $nbPersons competitors in this round, and $subsequentRoundCount subsequent round(s), which is more than the allowed $maxAllowedSubsequentRoundCount subsequent round(s).</p>";
        echo "<br /><hr />";
        $wrongs++;
      }
    }

    #--- First round
    if ( $event != $prevEvent ) {
      $nbRounds = 1;
      $eventRow = array_shift( $eventRows );
      list( $nbTotalPersons, $eventCompetitionId, $eventEventId ) = $eventRow;
      assert( $eventCompetitionId == $competitionId );
      assert( $eventEventId == $eventId );

      #--- Checks round names
      if( $prevEvent ){
        list( $prevCompetitionId, $prevEventId ) = explode('|', $prevEvent);
        $wrongs += checkRoundNames ( $roundInfos, $prevCompetitionId, $prevEventId );
      }

      $roundInfos = array();

      #--- Checks for qualification round
      $isThisRoundQuals = (( $roundId == '0' or $roundId == 'h' ));

      if (( $nbTotalPersons != $nbPersons ) and ( ! $isThisRoundQuals )) {
        echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId - $roundId</a></p>";

        #--- Peek at next roundId
        if($subsequentRoundCount > 0) {
          $nextRoundId = $roundRows[$i+1]['roundId'];
          showQualifications( $competitionId, $eventId, $roundId, $nextRoundId );
        }

        echo "<p>Not all persons that competed in $eventId are in $roundCellName. It should thus be indicated as Qualification round<p/>";
        addQuals( $competitionId, $eventId );
        echo "<br /><hr />";
        $isThisRoundQuals = true;
        $wrongs++;
      }

      if (( $nbTotalPersons == $nbPersons ) and ( $isThisRoundQuals )) {
        echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId - $roundId</a></p>";

        #--- Peek at next roundId
        if($subsequentRoundCount > 0) {
          $nextRoundId = $roundRows[$i+1]['roundId'];
          showQualifications( $competitionId, $eventId, $roundId, $nextRoundId );
        }

        echo "<p>All persons that competed in $eventId are in $roundCellName. It should thus not be indicated as Qualification round</p>";
        removeQuals( $competitionId, $eventId );
        echo "<br /><hr />";
        $isThisRoundQuals = false;
        $wrongs++;
      }
    }

    # Following rounds
    else {
      $isThisRoundQuals = false;

      # Article 9m, since April 9, 2008 until April 17, 2016
      if (mktime( 0, 0, 0, 4, 9, 2008 ) <= $competitionDate and $competitionDate <= mktime( 0, 0, 0, 4, 17, 2016 )) {
        if ((( $nbRounds > 1 ) and ( $nbTotalPersons < 8 )) or (( $nbRounds > 2 ) and ( $nbTotalPersons < 16 )) or (( $nbRounds > 3 ) and ( $nbTotalPersons < 100 )) or ( $nbRounds > 4 )) {
          echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId - $roundId</a></p>";
          echo "<p>There are $nbRounds rounds for event $eventId, but only $nbTotalPersons competitors in total</p>";
          removeRound( $competitionId, $eventId, $nbRounds );
          echo "<br /><hr />";
          $wrongs++;
        }
      }

      # Article 9m/n/o, since July 20, 2006 until April 8, 2008
      if ( mktime( 0, 0, 0, 7, 20, 2006 ) <= $competitionDate and $competitionDate <= mktime( 0, 0, 0, 4, 8, 2008 )) {
        if ((( $nbRounds > 2 ) and ( $nbTotalPersons < 16 )) or (( $nbRounds > 3 ) and ( $nbTotalPersons < 100 )) or ( $nbRounds > 4 )) {
          echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId - $roundId</a></p>";
          echo "<p>There are $nbRounds rounds for event $eventId, but only $nbTotalPersons competitors in total</p>";
          removeRound( $competitionId, $eventId, $nbRounds );
          echo "<br /><hr />";
          $wrongs++;
        }
      }

      $nbQualPersons = $isPrevRoundQuals ? getQualifications( $competitionId, $eventId, $prevRoundId, $roundId ) : $nbPersons;

      # Article 9p1, since April 14, 2010
      if ( mktime( 0, 0, 0, 4, 14, 2010 ) <= $competitionDate ) {
        if ( $nbQualPersons > ( 3*$prevNbPersons/4 )) {
          echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId</a></p>";
          showQualifications( $competitionId, $eventId, $prevRoundId, $roundId );
          echo "<p>From round $prevRoundCellName with $prevNbPersons competitors, $nbQualPersons were qualified to round $roundCellName which is more than 75%</p>";
          echo "<br /><hr />";
          $wrongs++;
        }
      }

      # Article 9p, since July 20, 2006 until April 13, 2010
      if (mktime( 0, 0, 0, 7, 20, 2006 ) <= $competitionDate and $competitionDate <= mktime( 0, 0, 0, 4, 13, 2010 )) {
        if ( $nbQualPersons >= $prevNbPersons ) {
          echo "<p style='margin-top:2em; margin-bottom:0'><a href='/competitions/$competitionId/results/all#e{$eventId}_$roundId'>$competitionId - $eventId</a></p>";
          showQualifications( $competitionId, $eventId, $prevRoundId, $roundId );
          echo "<p>From round $prevRoundCellName to round $roundCellName, at least one competitor must not proceed</p>";
          echo "<br /><hr />";
          $wrongs++;
        }
      }
    }
    $nbRounds += 1;
    $prevNbPersons = $nbPersons;
    $prevEvent = $event;
    $prevRoundId = $roundId;
    $prevRoundCellName = $roundCellName;
    $isPrevRoundQuals = $isThisRoundQuals;
    $roundInfos[] = array( $roundId, $roundCellName, $formatId, $isNotCombined );

  }

  #--- Tell the result.
  $date = wcaDate();
  noticeBox2(
    ! ( $wrongs ),
    "We didn't find any mistake.<br />$date",
    "<p>Darn! We disagree: $wrongs incorrect rounds found<br /><br />$date</p>"
    ."<p>Choose the changes you agree with above, then click the 'Execute...' button below. It will result in something like the following.</p>"
    ."<pre>I'm doing this:\n"
    ."UPDATE Results SET pos=111 WHERE id=11111\n"
    ."UPDATE Results SET pos=222 WHERE id=22222\n"
    ."UPDATE Results SET pos=333 WHERE id=33333\n"
    ."</pre>"
  );

  #--- If differences were found, offer to fix them.
  if( $wrongs )
    echo "<center><input type='submit' value='Execute the agreed changes!' /></center>\n";

  #--- Finish the form.
  echo "</form>\n";
}