Created
September 3, 2013 09:03
-
-
Save ar-juan/6421411 to your computer and use it in GitHub Desktop.
Saves rating (1-5) of 'id' in MySQL database
input: id, stars
output: html success or fail, new total votes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Cache-Control: no-cache"); | |
header("Pragma: nocache"); | |
include("../phpsqlajax_dbinfo.php"); | |
$units=5.0; // number of units possible | |
$id_sent = $_GET['id']; | |
$vote_sent = $_GET['stars']; | |
$ip =$_SERVER['REMOTE_ADDR'] ; | |
try { | |
$dbConnection = new PDO("mysql:dbname=$database;host=$host;charset=utf8", $username, $password); | |
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// Yay! | |
} | |
catch(PDOException $e) | |
{ | |
echo $e->getMessage(); | |
} | |
// statement | |
$insert_id_date = $dbConnection->prepare(" | |
INSERT INTO ratings_view (id, date) | |
VALUES (:id_sent, now()) | |
"); | |
$insert_id_date->bindValue(':id_sent', $id_sent); | |
try { | |
// Making sure the marker exists to maintain consistency and check if a vote exists | |
$select_id = $dbConnection->prepare("SELECT | |
ratings_view.id as id | |
FROM ratings_view | |
RIGHT JOIN markers | |
ON markers.id = ratings_view.id | |
WHERE ratings_view.id = :id_sent"); | |
$select_id->bindValue(':id_sent', $id_sent); | |
$select_id->execute(); | |
$result_select_id = $select_id->fetch(PDO::FETCH_ASSOC); | |
// If there is no vote cast for this markerid yet | |
if (!$result_select_id) { | |
// Start by inserting the markerid and the date | |
$insert_id_date->execute(); | |
} | |
} catch (Exception $e) { | |
die("marker does not exist."); //normal users will never see this | |
} // end of try | |
if ($vote_sent > $units || $vote_sent < 1.0) die("sorry, vote seems to be invalid."); // kill the script because a wrong vote has been given. Impossible for normal users and makes sure votes are within range | |
//connecting to the database to get some information | |
$get_votes_value_ips = $dbConnection->prepare(" | |
SELECT total_votes, | |
total_value, | |
used_ips | |
FROM ratings_view | |
WHERE id = :id_sent"); | |
$get_votes_value_ips->bindValue(':id_sent', $id_sent); | |
$get_votes_value_ips->execute(); | |
$result_get_votes_value_ips = $get_votes_value_ips->fetch(PDO::FETCH_ASSOC); | |
$checkIP = unserialize($result_get_votes_value_ips['used_ips']); // unserialize all used ips | |
$total_votes = $result_get_votes_value_ips['total_votes']; //how many votes total | |
$current_total_value = $result_get_votes_value_ips['total_value']; //total value of all ratings added together and stored | |
$new_total_value = $vote_sent + $current_total_value; //add together the current total value and the newest vote | |
//$tense = ($total_votes == 1) ? "vote" : "votes"; //plural form votes/vote --> unused | |
// checking to see if the first vote has been tallied | |
// or increment the current number of votes | |
($new_total_value==0 ? $added=0 : $added=$total_votes+1); // is this is a double check? votes of zero are already ignored? | |
// ip cookie check when voting | |
// if a cookie entry of this marker + view is not yet set (i.e. there is not yet voted for this entry) | |
if(!isset($_COOKIE['rating_view_'.$id_sent])) { | |
// then check also if the user's ip address is used before to vote on this marker | |
$get_used_ip = $dbConnection->prepare(" | |
SELECT used_ips | |
FROM ratings_view | |
WHERE used_ips LIKE '%".$ip."%' | |
AND id = :id_sent"); | |
//$get_used_ip->bindValue(':ip', $ip); //if it works, try it with :ip | |
$get_used_ip->bindValue(':id_sent', $id_sent); | |
$get_used_ip->execute(); | |
// if there is a reply (= a row), it means the ip has been found | |
$hasvoted = ($get_used_ip->fetch()); | |
//$hasvoted = $result_get_used_ip->rowCount(); | |
} | |
else { | |
// i.e. there is a cookie entry found for this marker + view --> has voted for sure | |
$hasvoted = 1; | |
} | |
if (!$hasvoted) { // if the user has not voted, vote normally | |
// prepare ip array | |
// if checkIP is an array (i.e. it has already entries) then push in another value | |
((is_array($checkIP)) ? array_push($checkIP, $ip) : $checkIP=array($ip)); | |
$insertip = serialize($checkIP); | |
try { | |
$update_rating = $dbConnection->prepare(" | |
UPDATE ratings_view | |
SET total_votes = :added, | |
total_value = :new_total_value, | |
used_ips = :insertip | |
WHERE id = :id_sent"); | |
$update_rating->bindValue(':added', $added); | |
$update_rating->bindValue(':new_total_value', $new_total_value); | |
$update_rating->bindValue(':insertip', $insertip); | |
$update_rating->bindValue(':id_sent', $id_sent); | |
$update_rating->execute(); | |
// check if the row has been affected | |
$result_update_rating = $update_rating->rowCount(); | |
// if UPDATE query is succesful (1 row = affected) set cookie (30 day validity) | |
if ($result_update_rating) setcookie("rating_view_" . $id_sent , 1 , time() + 3600*24*30); | |
} catch (Exception $e) { | |
throw $e; | |
} | |
} //end of if(!hasvoted) | |
// todo: return array with number of votes | |
// if saving the rating was succesful, the script will get here, otherwise will die earlier. So no need to check again for rowCount() | |
// if the user hadn't voted before | |
if (!$hasvoted) { | |
$return_value_html = ' <img src="images/green_check.png" width="16 height="16">'; | |
$return_total_votes_html = $added; // return also the new total amount of votes | |
} else { // the user had already voted (ip exists / cookie exists) | |
$return_value_html = '<span class="invalid" style="font-size:0.7em;"> <img src="images/red_cross.png" width="16" height="16"><br>Already voted for this item</span>'; | |
$return_total_votes_html = $total_votes; // total votes stay the same as it was before the vote | |
} | |
$return_html = array('outcome' => $return_value_html, 'total_votes' => $return_total_votes_html); | |
echo json_encode($return_html); // return as json array (use dataType "json" in ajax to read corectly) | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment