fixed typo in comment
[e-DoKo.git] / include / db.php
index 2a927f5773019ab99923b1ce57137e953110d6e1..cf39d85a72e4b7fdc670158a5a2d3221cd43f014 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-/* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
+/* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arun Persaud <arun@nubati.net>
  *
  *   This file is part of e-DoKo.
  *
@@ -30,7 +30,7 @@ if(!isset($HOST))
 
 function DB_open()
 {
-  $version_needed = 2;
+  $version_needed = 3;
 
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
@@ -570,13 +570,31 @@ function DB_get_user_timezone($userid)
     return "Europe/London";
 }
 
-function DB_insert_comment($comment,$playid,$userid)
+function DB_insert_comment($comment,$playid,$gameid,$userid)
 {
-  DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid, ".DB_quote_smart($comment).")");
+  DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid,$gameid, ".DB_quote_smart($comment).")");
 
   return;
 }
 
+function DB_get_pre_comment($gameid)
+{
+  $r = DB_query_array_all("SELECT comment, User.fullname FROM Comment".
+                         " LEFT JOIN User ON User.id=user_id".
+                         " WHERE play_id=-1".
+                         " AND game_id=$gameid ");
+  return $r;
+}
+
+function DB_get_pre_comment_call($gameid)
+{
+  $r = DB_query_array_all("SELECT comment, User.fullname FROM Comment".
+                         " LEFT JOIN User ON User.id=user_id".
+                         " WHERE play_id=-2".
+                         " AND game_id=$gameid ");
+  return $r;
+}
+
 function DB_insert_note($comment,$gameid,$userid)
 {
   DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");
@@ -754,6 +772,18 @@ function DB_get_party_by_gameid_and_userid($gameid,$userid)
     return NULL;
 }
 
+function DB_get_party_by_session_and_userid($session,$userid)
+{
+  /* used in score table by index. gameids are sorted by date, so we need to sort here too! */
+  $r = DB_query_array_all("SELECT party FROM Hand".
+                         " LEFT JOIN Game ON Game.id = Hand.game_id".
+                         " WHERE Game.session=".DB_quote_smart($session).
+                         "  AND user_id=".DB_quote_smart($userid)."".
+                         "  AND Game.status='gameover' ".
+                         " ORDER BY Game.create_date ASC");
+  return $r;
+}
+
 function DB_set_party_by_hash($hash,$party)
 {
   DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
@@ -772,6 +802,7 @@ function DB_get_PREF($myid)
   $PREF['vacation_start']      =  NULL;
   $PREF['vacation_stop']       =  NULL;
   $PREF['vacation_comment']    =  '';
+  $PREF['language']            =  'en';
 
   /* get all preferences */
   $r = DB_query('SELECT pref_key, value FROM User_Prefs'.
@@ -782,7 +813,7 @@ function DB_get_PREF($myid)
        {
        case 'cardset':
          /* licence only valid until then */
-         if($pref[1]=='altenburg' && (time()-strtotime( '2009-12-31 23:59:59')<0) )
+         if($pref[1]=='altenburg' && (time()-strtotime( '2012-12-31 23:59:59')<0) )
            $PREF['cardset']='altenburg';
          break;
 
@@ -825,8 +856,14 @@ function DB_get_PREF($myid)
          if($pref[1])
            $PREF['vacation_comment'] = $pref[1];
          break;
+
+       case 'language':
+         if($pref[1])
+           $PREF['language'] = $pref[1];
+         break;
        }
     }
+  $_SESSION['language'] =  $PREF['language'];
   return $PREF;
 }
 
@@ -929,16 +966,17 @@ function DB_get_card_name($card)
 
 function DB_get_current_playid($gameid)
 {
+  /* return playid or -1 for pre-game phase */
   $trick = DB_get_max_trickid($gameid);
 
-  if(!$trick) return NULL;
+  if(!$trick) return -1;
 
   $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
 
   if($r)
     return $r[0];
 
-  return "";
+  return -1;
 }
 
 function DB_get_call_by_hash($hash)
@@ -1025,48 +1063,24 @@ function DB_is_session_active($session)
     return -1;
 }
 
-function DB_get_score_by_gameid($gameid)
-{
-  /* returns the points of a game from the point of the re parth (<0 if they lost) */
-  $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
-                         "  WHERE game_id=$gameid ".
-                         "  GROUP BY party ");
-  $re     = 0;
-  $contra = 0;
-
-  while($r = DB_fetch_array($queryresult) )
-    {
-      if($r[1] == "re")
-       $re += $r[0];
-      else if ($r[1] == "contra")
-       $contra += $r[0];
-    };
-
-  return ($re - $contra);
-}
-
 function DB_get_gameids_of_finished_games_by_session($session)
 {
   $ids = array ();
 
   if($session==0) /* return all games */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           " WHERE status='gameover' ".
-                           " ORDER BY create_date ASC");
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 " WHERE status='gameover' ".
+                                 " GROUP BY Game.id");
   else   /* return games in a session */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           "  WHERE session=$session ".
-                           "   AND status='gameover' ".
-                           " ORDER BY create_date ASC");
-
-  $i=0;
-  while($r = DB_fetch_array($queryresult) )
-    {
-      $ids[$i] = $r[0];
-      $i++;
-    }
-
-  return $ids;
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 "  WHERE session=$session ".
+                                 "   AND status='gameover' ".
+                                 " GROUP BY Game.id".
+                                 " ORDER BY Game.create_date ASC");
+
+  return $queryresult;
 }
 
 function DB_get_card_value_by_cardid($id)