view results.inc.php @ 510:25bc2087869e

Translitterate.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Dec 2013 00:03:58 +0200
parents c63c7f98147a
children 6fe66ea0e954
line wrap: on
line source

<?
//
// FAPWeb Simple Demoparty System
// Competition results page
// (C) Copyright 2012-2013 Tecnic Software productions (TNSP)
//
$useASCII = stGetSetting("showResultsASCII");
$showResults = stGetSetting("showResults");
$showAuthors = stGetSetting("showResAuthors");
$voteKeyMode = stGetSetting("voteKeyMode");


function stKludge($str)
{
  $tmp = $str;
  foreach (array("ä" => "ae", "ö" => "oe", "Ä" => "Ae", "Ö" => "Oe") as $sfrom => $sto)
    $tmp = str_replace($sfrom, $sto, $tmp);

  return $tmp;
}

function stGetNumberSuffix($val)
{
  switch ($val)
  {
    case  1: return "st";
    case  2: return "nd";
    case  3: return "rd";
    case  4: case 5: case 6:
    case  7: case 8: case 9: return "th";
    default: return "th";
  }
}


echo "<h1>Results</h1>\n";

// Show everything for the admin session
if (stAdmSessionAuth())
{
  if (!$showResults)
    echo "<p class=\"notice\">Results hidden from normal users.</p>";
  if (!$showAuthors)
    echo "<p class=\"notice\">Entry authors hidden from normal users.</p>";

  $showResults = $showAuthors = TRUE;
}


if (!$showResults)
{
  echo "<p>Sorry, no results available! Nothing to see here, move along.</p>";
}
else
if (($res = stExecSQL("SELECT * FROM compos WHERE visible<>0 ORDER BY name DESC")) !== FALSE)
{
  // For each compo that has been set visible
  foreach ($res as $compo)
  {
    // Check if there are any entries for it
    $sql =
      "SELECT COUNT(*) FROM entries ".
      "WHERE compo_id=".$compo["id"];

    if (($nentries = stFetchSQLColumn($sql)) !== FALSE && $nentries > 0)
    {
      // Get voting results by mode
      switch ($voteKeyMode)
      {
        case VOTE_FREELY:
          $sql =
            "SELECT entries.*,SUM(votes.value) AS votesum FROM entries ".
            "LEFT JOIN votes ON votes.entry_id=entries.id ".
            "WHERE entries.compo_id=".$compo["id"];
          break;

        case VOTE_ACTIVATE:
          $sql =
            "SELECT entries.*, ".
              "(SELECT SUM(votes.value) FROM votes ".
              "LEFT JOIN votekeys ON votes.key_id=votekeys.id ".
              "WHERE votes.entry_id=entries.id AND votekeys.active<>0) ".
              "AS votesum ".
            "FROM entries ".
            "WHERE entries.compo_id=".$compo["id"];
          break;

        case VOTE_ASSIGN:
          $sql =
            "SELECT entries.*,SUM(votes.value) AS votesum FROM entries ".
            "LEFT JOIN votes ON votes.entry_id=entries.id ".
            "LEFT JOIN attendees ON votes.key_id=attendees.key_id ".
            "WHERE entries.compo_id=".$compo["id"]." ".
            "AND attendees.key_id<>0";

          $sql =
            "SELECT entries.*, ".
              "(SELECT SUM(votes.value) FROM votes ".
              "LEFT JOIN votekeys ON votes.key_id=votekeys.id ".
              "LEFT JOIN attendees ON votekeys.id=attendees.key_id ".
              "WHERE votes.entry_id=entries.id AND attendees.key_id<>0) ".
              "AS votesum ".
            "FROM entries ".
            "WHERE entries.compo_id=".$compo["id"];
          break;
      }

      $sql .= " ".
        "GROUP BY entries.id ".
        "ORDER BY votesum DESC";

      // List results
      $prev = FALSE;
      $index = 0;

      if ($useASCII)
      {
        echo
          "<pre>\n".
          "<b> ".chentities($compo["name"])." </b>\n".
          str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";

        foreach (stExecSQL($sql) as $entry)
        {
          if ($entry["votesum"] !== $prev)
          {
            $index++;
            printf("%3d%s.", $index, stGetNumberSuffix($index));
          }
          else
            echo "  -''-";

          printf("  %s  by  %s (%d pts)\n",
            chentities(stStrChop(stKludge($entry["name"]), 30)),
            chentities(stStrChop($showAuthors ? stKludge($entry["author"]) : "-", 30)),
            $entry["votesum"]);

          $prev = $entry["votesum"];
        }
        echo "\n\n</pre>\n";
      }
      else
      {
        echo
          "<h2>".chentities($compo["name"])."</h2>\n".
          "<table class=\"attendees\" style=\"width: 80%;\">\n".
          " <tr>\n".
          "  <th style=\"width: 5%;\">#</th>\n".
          "  <th>Name</th>\n".
          "  <th>Author</th>\n".
          "  <th style=\"width: 3%;\">Points</th>\n".
          " </tr>\n";

        foreach (stExecSQL($sql) as $entry)
        {
          if ($entry["votesum"] != $prev)
            $index++;

          echo
            " <tr>".
            "  <td>".$index."</td>".
            "  <td>".chentities($entry["name"])."</td>".
            "  <td>".($showAuthors ? chentities($entry["author"]) : "-")."</td>".
            "  <td>".$entry["votesum"]."</td>".
            " </tr>\n";

          $prev = $entry["votesum"];
        }

        echo "</table>\n";
      }
    }
  }
}

?>