Added extension: vanilla-voting-0.2beta-0.2beta.zip
[vanilla-miry.git] / extensions / VanillaVoting / library / Class.CommentVote.php
blob05ff6557135087b8234ec638db862c09a469581c
1 <?php
2 class CommentVote {
4 var $Name;
5 var $Context;
6 var $CategoryID;
7 var $SessionPostBackKey;
8 var $PositiveVote;
9 var $NegativeVote;
11 function CommentVote(&$Context) {
12 $this->Name = 'CommentVote';
13 $this->Context = &$Context;
14 $this->CategoryID = explode(",", $this->Context->Configuration['VANILLAVOTING_CATEGORYID']);
15 $this->SessionPostBackKey = $this->Context->Session->GetVariable('SessionPostBackKey', 'string');
16 $this->PositiveVote = 1;
17 $this->NegativeVote = -1;
20 function GetCommentVote (&$sql) {
21 $sql->AddSelect('Vote', 'm','TotalCommentVotes');
22 $sql->AddJoin('UserCommentVote', 'UserCommentVote', 'CommentID', 'm', 'CommentID', 'left join', ' and UserCommentVote.'.$this->Context->DatabaseColumns['UserCommentVote']['UserID'].' = '.$this->Context->Session->UserID);
23 $sql->AddSelect('Vote', 'UserCommentVote');
26 function AddCommentVote ($CommentID, $Vote, $CurrentTotalVotes) {
27 $sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
28 $sql->SetMainTable('UserCommentVote', 'UserCommentVote');
29 $sql->AddFieldNameValue('UserID', $this->Context->Session->UserID);
30 $sql->AddFieldNameValue('CommentID', $CommentID);
31 $sql->AddFieldNameValue('Vote', $Vote);
32 $sql->AddFieldNameValue('DateCreated', MysqlDateTime());
33 $this->Context->Database->Insert($sql, $this->Name, 'AddCommentVote', 'Failed to add Comment vote');
34 $this->UpdateCommentTotalVotes ($CommentID, $Vote, $CurrentTotalVotes);
36 function UpdateCommentTotalVotes ($CommentID, $Vote, $CurrentTotalVotes) {
37 $sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
38 $sql->SetMainTable('Comment', 'Comment');
39 $sql->AddFieldNameValue('Vote', $CurrentTotalVotes+$Vote);
40 $sql->AddWhere('Comment', 'CommentID', '', $CommentID, '=');
41 $this->Context->Database->Update($sql, $this->Name, 'UpdateCommentTotalVotes', 'Failed to update Comment total votes');
44 function Render(&$CommentList, &$DataSet) {
46 $verdict = '';
48 if ($DataSet['Vote'] < 0) {
49 $verdict = 'VotedNegative';
50 $CommentList .= '<span class="BelowViewingThreshold"><a href="'.GetRequestUri().'" onClick="ShowComment('.$DataSet['CommentID'].');return false;">'.$this->Context->GetDefinition('ShowComment').'</a></span>';
52 } else if ($DataSet['Vote'] > 0) {
53 $verdict = 'VotedPositive';
56 $CommentList .= '<span class="CommentVoteCount '.$verdict.'">'.$DataSet['TotalCommentVotes'].'</span><span class="CommentVoteCountLabel"> '.$this->Context->GetDefinition('Votes').'</span>';
57 $CommentList .= '<span class="AddCommentVote">';
58 if ($this->Context->Session->UserID > 0) {
59 if (is_null($DataSet['Vote'])) {
60 $CommentList .= '<span class="Vote"><input class="VotePositive" type="button" onClick="AddCommentVote(\''.$this->Context->Configuration["VANILLAVOTING_PATH"].'library/AddVote.php\', '.$DataSet['CommentID'].', '.$this->PositiveVote.', '.$DataSet['TotalCommentVotes'].', \''.$this->SessionPostBackKey.'\');" /><input type="button" class="VoteNegative" onClick="AddCommentVote(\''.$this->Context->Configuration["VANILLAVOTING_PATH"].'library/AddVote.php\', '.$DataSet['CommentID'].', '.$this->NegativeVote.', '.$DataSet['TotalCommentVotes'].', \''.$this->SessionPostBackKey.'\');" /></span>';
61 } else if ($DataSet['Vote'] < 0) {
62 $CommentList .= '<span class="VotedNegative"><input class="VotePositive" type="button" disabled="disabled" /><input type="button" class="VoteNegative" disabled="disabled"/></span>';
63 } else if ($DataSet['Vote'] > 0) {
64 $CommentList .= '<span class="VotedPositive"><input class="VotePositive" type="button" disabled="disabled" /><input type="button" class="VoteNegative" disabled="disabled"/></span>';
67 else {
68 $CommentList .= '<a href="'.AppendUrlParameters($this->Context->Configuration['SIGNIN_URL'], 'ReturnUrl='.GetRequestUri()).'">'.$this->Context->GetDefinition('Vote').'</a>';
70 $CommentList .= '</span>';