# HG changeset patch # User Matti Hamalainen # Date 1416339341 -7200 # Node ID fd84eb4d8b7471abb648f304e70b69a66d50ba71 # Parent 5ef230336e67f2cbf090e14d89d2fb600b8d0fa5 Refactor compo results code for modularity. diff -r 5ef230336e67 -r fd84eb4d8b74 msite.inc.php --- a/msite.inc.php Tue Nov 18 20:29:59 2014 +0200 +++ b/msite.inc.php Tue Nov 18 21:35:41 2014 +0200 @@ -503,7 +503,7 @@ // // Points .. // - $sql = "SELECT entries.* FROM entries"; + $sql = "SELECT entries.*,entries.evalue AS votesum FROM entries"; $extra = "ORDER BY entries.evalue DESC"; break; @@ -511,7 +511,7 @@ // // Ascending // - $sql = "SELECT entries.* FROM entries"; + $sql = "SELECT entries.*,entries.evalue AS votesum FROM entries"; $extra = "ORDER BY entries.evalue ASC"; break; } @@ -523,6 +523,50 @@ } +function stGetCompoResults() +{ + $voteKeyMode = stGetSetting("voteKeyMode"); + $out = array(); + $sql = "SELECT * FROM compos WHERE visible<>0 ORDER BY name DESC"; + if (($res = stExecSQL($sql)) === false) + return $out; + + // 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"]." ". + "AND (entries.flags & ".EFLAG_DISQUALIFIED.")=0"; + + if (($nentries = stFetchSQLColumn($sql)) !== FALSE && $nentries > 0) + { + // Get voting results by mode + $sql = stGetCompoResultsSQL($voteKeyMode, $compo); + + $out[$compo["id"]] = $compo; + $out[$compo["id"]]["results"] = array(); + $prev = FALSE; + $index = 0; + + foreach (stExecSQL($sql) as $entry) + { + if ($entry["votesum"] !== $prev) + $index++; + + $entry["position"] = $index; + $out[$compo["id"]]["results"][] = $entry; + + $prev = $entry["votesum"]; + } + } + } + + return $out; +} + + function stGetCompoResultLine($html, $entry, $points, $showAuthor) { $name = stStrChopPad($entry["name"], 30); @@ -542,91 +586,47 @@ function stGetCompoResultsASCIIStr($html) { - $sql = "SELECT * FROM compos WHERE visible<>0 ORDER BY name DESC"; - if (($res = stExecSQL($sql)) === false) - return ""; - - $voteKeyMode = stGetSetting("voteKeyMode"); $out = ""; - - // For each compo that has been set visible - foreach ($res as $compo) + foreach (stGetCompoResults() as $compo) { - // Check if there are any entries for it - $sql = - "SELECT COUNT(*) FROM entries ". - "WHERE compo_id=".$compo["id"]." ". - "AND (entries.flags & ".EFLAG_DISQUALIFIED.")=0"; - - if (($nentries = stFetchSQLColumn($sql)) !== FALSE && $nentries > 0) + // Output compo title / header + if ($html) { - // Get voting results by mode - $sql = stGetCompoResultsSQL($voteKeyMode, $compo); - - // Output compo title / header - if ($html) - { - $out .= - "
\n".
-          " ".chentities($compo["name"])." \n".
-          str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
-      }
-      else
-      {
-        $out .=
-          " ".$compo["name"]."\n".
-          str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
-      }
+      $out .=
+        "
\n".
+        " ".chentities($compo["name"])." \n".
+        str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
+    }
+    else
+    {
+      $out .=
+        " ".$compo["name"]."\n".
+        str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
+    }
 
-      // List results for this compo
-      $prev = FALSE;
-      $index = 0;
-      foreach (stExecSQL($sql) as $entry)
-      {
-        // Act based on compo type
-        switch ($compo["ctype"])
-        {
-          case COMPO_NORMAL:
-            if ($entry["votesum"] !== $prev)
-            {
-              $index++;
-              $out .= sprintf("%3d%s.", $index, stGetNumberSuffix($index));
-            }
-            else
-              $out .= "  -''-";
-
-            $out .= stGetCompoResultLine($html, $entry, $entry["votesum"], TRUE);
+    // List results for this compo
+    $prev = FALSE;
+    foreach ($compo["results"] as $entry)
+    {
+      // Act based on compo type
+      if ($entry["position"] !== $prev)
+        $out .= sprintf("%3d%s.", $entry["position"], stGetNumberSuffix($entry["position"]));
+      else
+        $out .= "  -''-";
 
-            $prev = $entry["votesum"];
-            break;
-            
-          case COMPO_POINTS:
-          case COMPO_ASSIGN:
-            if ($entry["evalue"] !== $prev)
-            {
-              $index++;
-              $out .= sprintf("%3d%s.", $index, stGetNumberSuffix($index));
-            }
-            else
-              $out .= "  -''-";
+      $out .= stGetCompoResultLine($html, $entry,
+        ($compo["ctype"] != COMPO_ASSIGN) ? $entry["votesum"] : FALSE,
+        ($compo["ctype"] != COMPO_NORMAL) ? $compo["show_authors"] : TRUE);
 
-            $out .= stGetCompoResultLine($html, $entry,
-              ($compo["ctype"] == COMPO_POINTS) ? $entry["evalue"] : FALSE,
-              $compo["show_authors"]);
+      $prev = $entry["position"];
+    }
+    $out .= "\n\n".($html ? "
\n" : ""); + } - $prev = $entry["evalue"]; - break; - } - } - $out .= "\n\n".($html ? "
\n" : ""); - } - } - return $out; } - function stNormalizeListSlideOrder($list_id) { }