From c0e030bb5068085ef52cc8bd0c91f025fb78045e Mon Sep 17 00:00:00 2001
From: Arun <arun@etaner.(none)>
Date: Mon, 8 Oct 2007 16:01:26 +0200
Subject: NEW FEATURE: send out reminder email

if a player hasn't moved for a week a reminder email can be send out.
only one per day is possible, tracking in the db
---
 create_database.sql | 26 ++++++++++++++++++++---
 db.php              | 24 +++++++++++++++++++++
 index.php           | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 106 insertions(+), 5 deletions(-)

diff --git a/create_database.sql b/create_database.sql
index c5de955..d552be5 100644
--- a/create_database.sql
+++ b/create_database.sql
@@ -336,14 +336,34 @@ CREATE TABLE `Recovery` (
 -- Dumping data for table `Recovery`
 --
 
-
-
-
 /*!40000 ALTER TABLE `Recovery` DISABLE KEYS */;
 LOCK TABLES `Recovery` WRITE;
 UNLOCK TABLES;
 /*!40000 ALTER TABLE `Recovery` ENABLE KEYS */;
 
+/*!40000 ALTER TABLE `Reminder` DISABLE KEYS */;
+LOCK TABLES `Reminder` WRITE;
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `Reminder` ENABLE KEYS */;
+
+DROP TABLE IF EXISTS `Reminder`;
+CREATE TABLE `Reminder` (
+  `id` int(11) NOT NULL auto_increment,
+  `user_id` int(11) NOT NULL default '0',
+  `game_id` int(11) NOT NULL default '0',
+  `create_date` timestamp NOT NULL default '0000-00-00 00:00:00',
+  UNIQUE KEY `id` (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+--
+-- Dumping data for table `Reminder`
+--
+
+/*!40000 ALTER TABLE `Reminder` DISABLE KEYS */;
+LOCK TABLES `Reminder` WRITE;
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `Reminder` ENABLE KEYS */;
+
 
 
 
diff --git a/db.php b/db.php
index a6b9c3b..646f004 100644
--- a/db.php
+++ b/db.php
@@ -916,4 +916,28 @@ function DB_format_gameid($gameid)
   return $session.".".$r[0];
 }
 
+function DB_get_reminder($user,$gameid)
+{
+  $queryresult = mysql_query("SELECT COUNT(*) FROM Reminder ".
+			     "  WHERE user_id=$user ".
+			     "  AND game_id=$gameid ".
+			     "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
+			     "  GROUP BY user_id " );
+  
+  $r = mysql_fetch_array($queryresult,MYSQL_NUM);
+  if($r)
+    return $r[0];
+  else
+    return 0;
+}
+
+function DB_set_reminder($user,$gameid)
+{
+  mysql_query("INSERT INTO Reminder ".
+	      "  VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
+              ", NULL) ");
+  return 0;
+}
+
+
 ?>
\ No newline at end of file
diff --git a/index.php b/index.php
index c565607..f2fa3d6 100644
--- a/index.php
+++ b/index.php
@@ -214,6 +214,58 @@ else if(myisset("cancle","me"))
     else
       echo "<p>You need to wait longer before you can cancle a game...</p>\n";
   }
+/* send out a reminder */
+else if(myisset("remind","me"))
+  {
+    $me = $_REQUEST["me"];
+    
+    /* test for valid ID */
+    $myid = DB_get_userid_by_hash($me);
+    if(!$myid)
+      {
+	echo "Can't find you in the database, please check the url.<br />\n";
+	echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
+	output_footer();
+	DB_close();
+	exit();
+      }
+    
+    DB_update_user_timestamp($myid);
+    
+    /* get some information from the DB */
+    $gameid   = DB_get_gameid_by_hash($me);
+    $myname   = DB_get_name_by_hash($me);
+
+    /* check if game really is old enough */
+    $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
+    $r = mysql_fetch_array($result,MYSQL_NUM);
+    if( (time()-strtotime($r[0]) > 60*60*24*7)  && ($r[2]!='gameover') ) /* = 1 week */
+      {
+	$name = DB_get_name_by_userid($r[1]);
+	$To = DB_get_email_by_userid($r[1]);
+	$userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
+	
+	$message = "Hello $name, \n\n".
+	  "It's your turn in game ".DB_format_gameid($gameid)." \n".
+	  "Actually everyone else is waiting for you for more than a week now ;)\n\n".
+	  "Please visit this link now to continue: \n".
+	  " ".$host."?me=".$userhash."\n\n" ;
+	
+	if(DB_get_reminder($r[1],$gameid)>0)
+	  {
+	    echo "<p>An email has already been sent out.</p>\n";
+	  }
+	else
+	  {
+	    DB_set_reminder($r[1],$gameid);
+	    mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
+	    
+	    echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).": an email has been sent out.<br /><br /></p>";
+	  }
+      }
+    else
+      echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
+  }
 /* handle request from one specific player for one game,
  * (the hash is set on a per game base) */
 else if(myisset("me"))
@@ -1761,12 +1813,17 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
 			   else
 			     {
 			       $name = DB_get_name_by_userid($r[3]);
+			       $gameid = $r[1];
+			       if(DB_get_reminder($r[3],$gameid)==0)
+				 if(time()-strtotime($r[2]) > 60*60*24*7)
+				   echo "".
+				     "<a href=\"$host?remind=1&amp;me=".$r[0]."\">Send a reminder.</a>";
 			       echo "(it's $name's turn)\n";
 			     };
 			 }
 		       if(time()-strtotime($r[2]) > 60*60*24*30)
-			 echo " The game has been running for over a month.".
-			   " Do you want to cancel it? <a href=\"$host?cancle=1&amp;me=".$r[0]."\">yes</a>".
+			 echo "".
+			   "<a href=\"$host?cancle=1&amp;me=".$r[0]."\">Cancel?</a>".
 			   " (clicking here is final and can't be restored)";
 
 		     }
-- 
cgit v1.2.3-18-g5258