1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-19 20:21:51 +02:00

Rating and Comments. Like and Dislike buttons are now functional.

This commit is contained in:
CaMer0n 2012-06-16 05:04:22 +00:00
parent e0390c3c1c
commit 3c37643365
12 changed files with 318 additions and 84 deletions

View File

@ -103,7 +103,9 @@ CREATE TABLE comments (
comment_ip varchar(45) NOT NULL default '',
comment_type varchar(20) NOT NULL default '0',
comment_lock tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (comment_id)
PRIMARY KEY (comment_id),
KEY comment_blocked (comment_blocked),
KEY comment_author_id (comment_author_id)
) ENGINE=MyISAM;
# --------------------------------------------------------
@ -427,6 +429,8 @@ CREATE TABLE rate (
rate_rating int(10) unsigned NOT NULL default '0',
rate_votes int(10) unsigned NOT NULL default '0',
rate_voters text NOT NULL,
rate_up int(10) unsigned NOT NULL default '0',
rate_down int(10) unsigned NOT NULL default '0',
PRIMARY KEY (rate_id)
) ENGINE=MyISAM;
# --------------------------------------------------------

View File

@ -98,6 +98,7 @@ class comment_shortcodes extends e_shortcode
}
function sc_comment_avatar($parm)
{
$height = e107::getPref("im_height");
@ -109,18 +110,18 @@ class comment_shortcodes extends e_shortcode
if (vartrue($this->var['user_image']))
{
$img = $tp->thumbUrl(e_MEDIA."avatars/".$this->var['user_image'],"aw=".$width."&ah=".$height);
$text = "<div class='comments-avatar'><img class='comment-avatar' src='".$img."' alt='' /></div>";
$text = "<img class='comment-avatar' src='".$img."' alt='' />";
}
else
{
$img = $tp->thumbUrl(e_IMAGE."generic/blank_avatar.jpg","aw=".$width."&ah=".$height);
$text = "<div class='comments-avatar'><img class='comment-avatar' src='".$img."' alt='' /></div>";
$text = "<img class='comment-avatar' src='".$img."' alt='' />";
}
}
else
{
$img = $tp->thumbUrl(e_IMAGE."generic/blank_avatar.jpg","aw=".$width."&ah=".$height);
$text = "<div class='comments-avatar'><img class='comment-avatar' src='".$img."' alt='' /></div>";
$text = "<img class='comment-avatar' src='".$img."' alt='' />";
}
return $text;
@ -215,9 +216,21 @@ class comment_shortcodes extends e_shortcode
}
function sc_rate_input($parm)
function sc_comment_rate($parm)
{
return $this->var['rate'];
if($this->var['comment_blocked'] > 0 || $this->var['rating_enabled'] == false)
{
return;
}
$curVal = array(
'up' => $this->var['rate_up'],
'down' => $this->var['rate_down'],
'total' => $this->var['rate_votes']
);
return e107::getRate()->renderLike("comments",$this->var['comment_id'],$curVal);
}

View File

@ -246,24 +246,7 @@ $(document).ready(function()
});
$(".e-ajax").click(function(){
var id = $(this).attr("href");
var src = $(this).attr("data-src");
if(src == null) // old way - href='myscript.php#id-to-target
{
var tmp = src.split('#');
id = tmp[1];
src = tmp[0];
}
var effect = $(this).attr("data-effect");
$(id).load(src + " ",function() {
// $(id).effect("slide");
});
});
$(".e-shake" ).effect("shake",{times: 10, distance: 2},20);

View File

@ -399,6 +399,63 @@ $(document).ready(function()
// Text-area AutoGrow
$("textarea.e-autoheight").elastic();
$(".e-ajax").click(function(){
var id = $(this).attr("href");
var src = $(this).attr("data-src");
if(src == null) // old way - href='myscript.php#id-to-target
{
var tmp = src.split('#');
id = tmp[1];
src = tmp[0];
}
var effect = $(this).attr("data-effect");
$(id).load(src + " ",function() {
// $(id).effect("slide");
});
});
$(".e-rate-thumb").click(function(){
var src = $(this).attr("href");
var tmp = src.split('#');
id = tmp[1];
src = tmp[0];
$.ajax({
type: "POST",
url: src,
data: { ajax_used: 1, mode: 'thumb' },
dataType: "html",
success: function(html) {
if(html == '')
{
return false;
}
var tmp = html.split('|');
up= tmp[0];
down = tmp[1];
$('#'+id +'-up').text(up);
$('#'+id +'-down').text(down);
// alert('Thanks for liking');
}
});
return false;
});
})

View File

@ -3,6 +3,6 @@
$(document).ready(function()
{
$(":input").tipsy({gravity: 'w',fade: true});
$(".e-tip").tipsy({gravity: 'sw',fade: true});
});

View File

@ -202,6 +202,7 @@ class comment
}
//add the rating select box/result ?
/*
$rate = "";
if ($rating == TRUE && !(ANON == TRUE && USER == FALSE))
{
@ -215,7 +216,7 @@ class comment
} //end rating area
*/
// -------------------------------------------------------------
@ -287,38 +288,28 @@ class comment
function render_comment($row, $table, $action, $id, $width, $subject, $addrating = FALSE)
{
//addrating : boolean, to show rating system in rendered comment
global $sc_style, $rater, $gen;
global $sc_style, $gen;
$tp = e107::getParser();
$sql = e107::getDb();
$pref = e107::getPref();
global $NEWIMAGE, $USERNAME, $RATING, $datestamp;
global $thisaction,$thistable,$thisid,$e107;
$tp = e107::getParser();
$sql = e107::getDb();
$pref = e107::getPref();
if (vartrue($pref['comments_disabled']))
{
return;
}
$comrow = $row;
$thistable = $table;
$thisid = $id;
$thisaction = $action;
if ($addrating === TRUE)
{
require_once(e_HANDLER."rate_class.php");
if (!$rater || !is_object($rater))
{
$rater = new rater;
}
}
global $NEWIMAGE, $USERNAME, $RATING, $datestamp;
global $thisaction,$thistable,$thisid,$e107;
$comrow = $row;
$thistable = $table;
$thisid = $id;
$thisaction = $action;
//FIXME - new level handler, currently commented to avoid parse errors
//require_once (e_HANDLER."level_handler.php");
if (!$width)
{
$width = 0;
@ -339,10 +330,12 @@ class comment
$gen = new convert;
}
$row['rating_enabled'] = true; // Toggles rating shortcode. //TODO add pref
e107::getScBatch('comment')->setParserVars($row);
$COMMENT_TEMPLATE = $this->template;
if ($pref['nested_comments'])
if (vartrue($pref['nested_comments']))
{
$width2 = 100 - $width;
$total_width = "95%";
@ -399,24 +392,24 @@ class comment
define("IMAGE_rank_admin_image", (isset($pref['rank_admin_image']) && $pref['rank_admin_image'] && file_exists(THEME."forum/".$pref['rank_admin_image']) ? "<img src='".THEME_ABS."forum/".$pref['rank_admin_image']."' alt='' />" : "<img src='".e_PLUGIN_ABS."forum/images/lite/admin.png' alt='' />"));
}
$RATING = ($addrating == TRUE && $comrow['user_id'] ? $rater->composerating($thistable, $thisid, FALSE, $comrow['user_id']) : "");
// $RATING = ($addrating == TRUE && $comrow['user_id'] ? $rater->composerating($thistable, $thisid, FALSE, $comrow['user_id']) : "");
$comment_shortcodes = e107::getScBatch('comment');
// $text = $tp->parseTemplate($COMMENT_TEMPLATE['ITEM_START'], TRUE, $comment_shortcodes);
$text = $tp->parseTemplate($renderstyle, TRUE, $comment_shortcodes);
//FIXME - dramatically increases the number of queries performed.
if ($action == "comment" && $pref['nested_comments'])
if ($action == "comment" && vartrue($pref['nested_comments']))
{
$type = $this->getCommentType($thistable);
$sub_query = "
SELECT c.*, u.*, ue.*
SELECT c.*, u.*, ue.*, r.*
FROM #comments AS c
LEFT JOIN #user AS u ON c.comment_author_id = u.user_id
LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id
LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments'
WHERE comment_item_id='".intval($thisid)."' AND comment_type='".$tp->toDB($type, true)."' AND comment_pid='".intval($comrow['comment_id'])."'
AND (c.comment_blocked = 0 OR (c.comment_blocked > 0 AND c.comment_author_id = ".intval(USERID)."))
@ -778,21 +771,32 @@ class comment
$sql = e107::getDb();
$type = $this->getCommentType($table);
$query = $pref['nested_comments'] ?
"SELECT c.*, u.*, ue.* FROM #comments AS c
if(vartrue($pref['nested_comments']))
{
$query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c
LEFT JOIN #user AS u ON c.comment_author_id = u.user_id
LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id
LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id
LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments'
WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."' AND c.comment_pid='0'
AND (c.comment_blocked = 0 OR (c.comment_blocked > 0 AND c.comment_author_id = ".intval(USERID)."))
ORDER BY c.comment_datestamp"
:
"SELECT c.*, u.*, ue.* FROM #comments AS c
ORDER BY c.comment_datestamp";
}
else
{
$query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c
LEFT JOIN #user AS u ON c.comment_author_id = u.user_id
LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id
WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."'
LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id
LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments'
";
$query .= "WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."'
AND (c.comment_blocked = 0 OR (c.comment_blocked > 0 AND c.comment_author_id = ".intval(USERID)."))
ORDER BY c.comment_datestamp";
}
$text = "";
$comment = '';

View File

@ -232,6 +232,125 @@ class rater {
return $this->enterrating($array,true);
}
/**
* @param $table: table without prefix that the like is for
* @param $itemid: item id within that table for the item to be liked
* @param $curval: optional array of current values for 'up' and 'down'
* @param $perc: optional percentage mode. Displays percentages instead of totals.
*/
function renderLike($table,$itemid,$curVal=false,$perc=false)
{
$id = "rate-".$table."-".$itemid; // "-up or -down is appended to the ID by jquery as both value will need updating.
if($curVal == false)
{
$curVal = $this->getLikes($table,$itemid);
}
$p = ($perc) ? "%" : "";
$upImg = "<img class='e-tip' src='".e_IMAGE."rate/like_16.png' alt='' title='Like' />";
$upDown = "<img class='e-tip' src='".e_IMAGE."rate/dislike_16.png' alt='' title='Dislike' />";
$text = "<span id='{$id}-up'>".intval($curVal['up'])."{$p}</span>
<a class='e-rate-thumb e-rate-up' href='".e_BASE."rate.php?table={$table}&id={$itemid}&type=up#{$id}'>{$upImg}</a>
<span id='{$id}-down'>".intval($curVal['down'])."{$p}</span>
<a class='e-rate-thumb e-rate-down' href='".e_BASE."rate.php?table={$table}&id={$itemid}&type=down#{$id}'>{$upDown}</a>";
return $text;
}
protected function getLikes($table,$itemid,$perc=false)
{
$sql = e107::getDb();
if($sql->db_Select("rate","*","rate_table = '{$table}' AND rate_itemid = '{$itemid}' LIMIT 1"))
{
$row = $sql->db_Fetch();
if($perc == true) // Percentage Mode
{
$up = round(($row['rate_up'] / $row['rate_votes']) * 100) . "%";
$down = round(($row['rate_down'] / $row['rate_votes']) * 100) . "%";
return array('up'=>$up,'down'=>$down,'total'=> $row['rate_votes']);
}
else // Counts mode.
{
$up = $row['rate_up'];
$down = $row['rate_down'];
return array('up'=>$up,'down'=>$down,'total'=>$row['rate_votes']);
}
}
return ($perc == false) ? array('up'=>0,'down'=>0,'total'=>0) : array('up'=>'0%','down'=>'0%','total'=>'0%');
}
function submitLike($table,$itemid,$type,$perc=false)
{
$sql = e107::getDb();
if($sql->db_Select("rate","*","rate_table = '{$table}' AND rate_itemid = '{$itemid}' LIMIT 1"))
{
$row = $sql->db_Fetch();
if(preg_match("/\.". USERID."\./",$row['rate_voters'])) // already voted.
{
return false;
}
$newvoters = $row['rate_voters'].".".USERID.".";
$totalVotes = $row['rate_votes'] + 1;
$totalDown = $row['rate_down'] + (($type == 'down') ? 1 : 0);
$totalUp = $row['rate_up'] + (($type == 'up') ? 1 : 0);
$qry = ($type == 'up') ? "rate_up = {$totalUp} " : "rate_down = {$totalDown}";
$qry .= ", rate_voters = '{$newvoters}', rate_votes = {$totalVotes} ";
$qry .= " WHERE rate_table = '{$table}' AND rate_itemid = '{$itemid}' LIMIT 1";
if($sql->db_Update("rate",$qry))
{
if($perc == true) // Percentage Mode
{
$up = round(($totalUp /$totalVotes) * 100) . "%";
$down = round(($totalDown /$totalVotes) * 100) . "%";
}
else // Counts mode.
{
$up = $totalUp;
$down = $totalDown;
}
return $up."|".$down;
}
}
else
{
$insert = array(
// "rate_id" => 0, // let it increment
"rate_table" => $table,
"rate_itemid" => $itemid,
"rate_rating" => 0,
"rate_votes" => 1,
"rate_voters" => ".".USERID.".",
"rate_up" => ($type == 'up') ? 1 : 0,
"rate_down" => ($type == 'down') ? 1 : 0
);
if($sql->db_Insert("rate", $insert))
{
if($perc == true) // Percentage Mode
{
return ($type == 'up') ? "100%|0%" : "0%|100%";
}
else
{
return ($type == 'up') ? "1|0" : "0|1";
}
}
}
}
function enterrating($rateindex,$ajax = false)
@ -294,11 +413,32 @@ class rater {
}
}
elseif($sql->db_Insert("rate", " 0, '$table', '$itemid', '$rate', '1', '.".$voter.".' "))
else
{
$stat = ($rate /1)/2;
$statR = round($stat,1);
return RATELAN_3."|".$this->renderVotes(1,$statR); ; // Thank you for your vote.
$insert = array(
// "rate_id" => 0,
"rate_table" => $table,
"rate_itemid" => $itemid,
"rate_rating" => $rate,
"rate_votes" => 1,
"rate_voters" => ".".$voter.".",
"rate_up" => 0,
"rate_down" => 0
);
if($sql->db_Insert("rate", $insert))
// if($sql->db_Insert("rate", " 0, '$table', '$itemid', '$rate', '1', '.".$voter.".' "))
{
$stat = ($rate /1)/2;
$statR = round($stat,1);
return RATELAN_3."|".$this->renderVotes(1,$statR); ; // Thank you for your vote.
}
elseif(getperms('0'))
{
return "Rating Failed ";
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -65,7 +65,7 @@ $sc_style['REPLY']['post'] = "</span>";
$sc_style['COMMENTEDIT']['pre'] = '<span class="comment-edit">';
$sc_style['COMMENTEDIT']['post'] = '</span>';
$sc_style['COMMENT_AVATAR']['pre'] = '<div class="center">';
$sc_style['COMMENT_AVATAR']['pre'] = '<div class="comment-avatar center">';
$sc_style['COMMENT_AVATAR']['post'] = '</div>';
$sc_style['SUBJECT_INPUT']['pre'] = ""; //COMLAN_324
@ -74,17 +74,17 @@ $sc_style['SUBJECT_INPUT']['post'] = "";
$sc_style['AUTHOR_INPUT']['pre'] = ""; // COMLAN_16
$sc_style['AUTHOR_INPUT']['post'] = "";
$sc_style['RATE_INPUT']['pre'] = "".COMLAN_327."";
$sc_style['RATE_INPUT']['post'] = "";
$sc_style['COMMENT_INPUT']['pre'] = "";// COMLAN_8
$sc_style['COMMENT_INPUT']['post'] = "";
$sc_style['COMMENT_BUTTON']['pre'] = "<div id='commentformbutton'>";
$sc_style['COMMENT_BUTTON']['post'] = "</div>";
$sc_style['USER_AVATAR']['pre'] = '<div class="center">';
$sc_style['USER_AVATAR']['post'] = '</div>';
$sc_style['COMMENT_RATE']['pre'] = '<div class="comment-rate">';
$sc_style['COMMENT_RATE']['post'] = '</div>';
$sc_style['USER_AVATAR']['pre'] = '<div class="comment-avatar center">';
$sc_style['USER_AVATAR']['post'] = '</div>';
@ -120,11 +120,13 @@ $COMMENT_TEMPLATE['ITEM'] = '
<span class="comment-box-date">{TIMEDATE}</span>
<span class="comment-status">{COMMENT_STATUS}</span>
<div class="comments-user-badge-bar">
<div class="comment-user-badge-bar">
{COMMENT_RATE}
{REPLY}
{COMMENTEDIT}
</div>
<div class="clear_b H5"><!-- --></div>

View File

@ -141,7 +141,7 @@ div.bbcode { margin: 15px 0px; clear: both; } /* layout control via bbcodes */
.comment-box-username { padding-left:0px; font-size:1.2em; padding-right: 10px }
.comments-avatar { padding:10px 10px 0px 10px; }
div.comment-avatar { margin-top:5px; padding:10px 10px 0px 10px; }
img.comment-avatar { max-width:128px; -webkit-box-shadow:#CCCCCC 0px 0px 10px; -moz-box-shadow:#CCCCCC 0px 0px 10px; box-shadow:#CCCCCC 0px 0px 10px; -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; }
@ -151,7 +151,7 @@ img.comment-avatar { max-width:128px; -webkit-box-shadow:#CCCCCC 0px 0px
.comment-box-date { }
.comments-user-badge-bar { float:right; margin-left:20px; }
.comment-user-badge-bar { float:right; margin-left:20px; }
.comment-replyxxx { position:relative; bottom:0px; right:-10px; padding:2px 5px; font-size:0.85em; background-color:#EBFAFF; border:1px solid #C6F1FF; border-bottom:0px none; border-right:0px none; -webkit-border-top-left-radius: 5px; -moz-border-radius-topleft: 5px; border-top-left-radius: 5px; }
@ -166,3 +166,9 @@ a.comment-edit { }
.comment-reply { float:right ; margin-right:5px;}
.comment-moderate { text-align:center }
.comment-rate { text-align:right; float:right; width:100px }
.e-rate-up img { opacity: 0.5 }
.e-rate-down img { opacity: 0.5 }

View File

@ -42,11 +42,36 @@ if(!e_AJAX_REQUEST) // Legacy method.
exit;
}
else // Ajax Used.
{
$table = $tp->toDB($_POST['table']);
$itemid = intval($_POST['id']);
$rate = intval($_POST['score']) * 2;
echo e107::getRate()->submitVote($table,$itemid,$rate);
{
if($_POST['mode'] == 'thumb')
{
if(vartrue($_GET['type']) !='up' && vartrue($_GET['type']) !='down')
{
exit;
}
$table = $tp->toDB($_GET['table']);
$itemid = intval($_GET['id']);
$type = $_GET['type'];
if($result = e107::getRate()->submitLike($table,$itemid,$type))
{
echo $result;
}
else // already liked/disliked
{
exit;
}
}
elseif($_POST['table'])
{
$table = $tp->toDB($_POST['table']);
$itemid = intval($_POST['id']);
$rate = intval($_POST['score']) * 2;
echo e107::getRate()->submitVote($table,$itemid,$rate);
}
exit;
}