changeset 724:fd84eb4d8b74

Refactor compo results code for modularity.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 18 Nov 2014 21:35:41 +0200
parents 5ef230336e67
children 9ddc4a3c9b8a
files msite.inc.php
diffstat 1 files changed, 76 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- 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 .=
-          "<pre>\n".
-          "<b> ".chentities($compo["name"])." </b>\n".
-          str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
-      }
-      else
-      {
-        $out .=
-          " ".$compo["name"]."\n".
-          str_repeat("=", strlen($compo["name"]) + 2)."-- - .\n\n";
-      }
+      $out .=
+        "<pre>\n".
+        "<b> ".chentities($compo["name"])." </b>\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 ? "</pre>\n" : "");
+  }
 
-            $prev = $entry["evalue"];
-            break;
-        }
-      }
-      $out .= "\n\n".($html ? "</pre>\n" : "");
-    }
-  }
-  
   return $out;
 }
 
 
-
 function stNormalizeListSlideOrder($list_id)
 {
 }