Main Page | Directories | File List | File Members

include/trackers/votes.php

Go to the documentation of this file.
00001 <?php
00002 # This file is part of the Savane project
00003 # <http://gna.org/projects/savane/>
00004 #
00005 # $Id: votes.php 5187 2005-12-01 16:22:29Z yeupou $
00006 #
00007 #  Copyright 2005      (c) Mathieu Roy <yeupou--gnu.org>
00008 #
00009 # The Savane project is free software; you can redistribute it and/or
00010 # modify it under the terms of the GNU General Public License
00011 # as published by the Free Software Foundation; either version 2
00012 # of the License, or (at your option) any later version.
00013 #
00014 # The Savane project is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 #
00019 # You should have received a copy of the GNU General Public License
00020 # along with the Savane project; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 # Count the remaining votes of a given user
00024 function trackers_votes_user_remains_count ($user_id)
00025 {
00026   $total = 100;
00027   
00028   $sql = "SELECT howmuch FROM user_votes WHERE user_id='$user_id'";
00029   $result = db_query($sql);
00030 
00031   if (db_numrows($result)) 
00032     {
00033       while ($row = db_fetch_array($result)) 
00034         {
00035           $total = $total - $row['howmuch'];
00036         }
00037     }
00038   
00039     
00040   # Total < 0 does not make sense
00041   if ($total < 0) 
00042     {
00043       fb(_("You appear to have less than 0 votes remaining. There's a bug somewhere, please contact the administrators"), 1);
00044     }
00045 
00046   return $total;
00047 }
00048 
00049 # Count the number of vote of a given user of a given item
00050 function trackers_votes_user_giventoitem_count ($user_id, $tracker, $item_id)
00051 {
00052   $total = 0;
00053   
00054   $sql = "SELECT howmuch FROM user_votes WHERE user_id='$user_id' AND tracker='$tracker' AND item_id='$item_id' LIMIT 1";
00055   $result = db_query($sql);
00056 
00057   if (db_numrows($result)) 
00058     {
00059       $total = db_result($result, 0, 'howmuch');
00060     }
00061    
00062   return $total;
00063 }
00064 
00065 
00066 # Update the database: add / update votes
00067 function trackers_votes_update ($item_id, $group_id=0, $new_vote, $tracker=0)
00068 {
00069   # Vote must be simple integer
00070   if (!ctype_digit($new_vote))
00071     {
00072       fb(_("Vote provided is not a simple integer, it has been ignored"), 1);
00073       return false;
00074     }
00075 
00076   # If the tracker is undefined, use the constant
00077   if (!$tracker)
00078     {
00079       $tracker = ARTIFACT;
00080     }
00081 
00082   # If group_id is not known, we guess it
00083   if (!$group_id)
00084     {
00085       $res_getgroupid = db_query("SELECT group_id FROM ".$tracker." WHERE bug_id='$item_id'");
00086       $group_id = db_result($res_getgroupid, 0, 'group_id');
00087     }
00088   
00089   # If the user already voted for this item:
00090   #   - if he voted 0, we must simply remove the vote
00091   #   - if he voted something else, we must add or remove the diff
00092   
00093   # Vote = 0
00094   if ($new_vote < 1) 
00095     {
00096       $registered_vote = trackers_votes_user_giventoitem_count(user_getid(), $tracker, $item_id);
00097       if ($registered_vote)
00098         {
00099           db_query("DELETE FROM user_votes WHERE user_id='".user_getid()."' AND tracker='".$tracker."' AND item_id='$item_id' LIMIT 1");
00100           $res_get = db_query("SELECT vote FROM ".$tracker." WHERE bug_id='$item_id' AND group_id='$group_id'");
00101           $real_new_vote = db_result($res_get, 0, 'vote') - $registered_vote;
00102           db_query("UPDATE ".$tracker." SET vote='$real_new_vote' WHERE bug_id='$item_id' AND group_id='$group_id'");
00103           
00104           fb(_("Vote erased"));
00105         }
00106       return false; 
00107     }
00108   else
00109     {
00110       # Vote > 0 
00111 
00112       # Check the diff between the registered vote and the new vote
00113       $registered_vote = trackers_votes_user_giventoitem_count(user_getid(), $tracker, $item_id);
00114       $diff_vote = $new_vote - $registered_vote;
00115 
00116       # If new vote equal to the current vote, nothing to do
00117       if (!$diff_vote)
00118         {
00119           return true;
00120         }
00121 
00122       # Check whether the user have not specified more votes than he actually
00123       # got available
00124       $remains = trackers_votes_user_remains_count(user_getid());
00125       if ($remains < $diff_vote)
00126         {
00127           # If so, set the diff_vote and new_vote as the maximum possible
00128           $diff_vote = $remains;
00129           $new_vote = $diff_vote + $registered_vote;
00130         }
00131 
00132       # If the vote is new, we do a SQL INSERT, otherwise a SQL UPDATE
00133       # in the user_votes table
00134       if (!$registered_vote)
00135         {
00136           $sql = "INSERT INTO user_votes ".
00137             "(user_id,tracker,item_id,howmuch) ".
00138             "VALUES ('".user_getid()."','".$tracker."','$item_id','$new_vote')";          
00139 
00140           # Add in CC
00141           unset($bah); # workaround for stupid function that require a 
00142                        # variable to be passed as final argument.
00143           trackers_add_cc($item_id,
00144                           $group_id,
00145                           user_getname(),
00146                           "Voted in favor of this item", # hum, picky to deal
00147                           $bah);                        # with, i18n-wise
00148         }
00149       else
00150         {
00151           $sql = "UPDATE user_votes SET howmuch='$new_vote' WHERE ".
00152             "user_id='".user_getid()."' AND tracker='".$tracker."' AND item_id='$item_id'";       
00153         }
00154       $res_insert = db_query($sql);
00155       if (db_affected_rows($res_insert) < 1)
00156         { 
00157           # In case of problem, kept unmodified the item proper info
00158           fb(_("Unable to record the vote, please report to admins"), 1); 
00159           return false;
00160         }
00161       
00162       # Add the new vote to the item proper info table
00163       $res_get = db_query("SELECT vote FROM ".$tracker." WHERE bug_id='$item_id' AND group_id='$group_id'");
00164       $real_new_vote = db_result($res_get, 0, 'vote') + $diff_vote;
00165       $res_update = db_query("UPDATE ".$tracker." SET vote='$real_new_vote' WHERE bug_id='$item_id' AND group_id='$group_id'");
00166       if (db_affected_rows($res_update) < 1)
00167         { 
00168           # In case of problem, kept unmodified the item proper info
00169           fb(_("Unable to finally record the vote, please report to admins"), 1); 
00170           return false;
00171         }
00172       
00173       # If we arrive here, everything went properly
00174       if ($diff_vote > 0)
00175         { $diff_vote = "+$diff_vote"; }
00176       fb(_("Vote recorded")." ($diff_vote)");
00177       return true;
00178     }
00179 }
00180 
00181 ?>

Generated on Sun Feb 26 13:23:04 2006 for Savane PHP Frontend Developer Reference by  doxygen 1.4.4