|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\Modules; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A database driven model. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Vote extends ActiveRecordModelExtender |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string $tableName name of the database table. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $tableName = "ramverk1_votes"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Columns in the table. |
|
18
|
|
|
* |
|
19
|
|
|
* @var integer $id primary key auto incremented. |
|
20
|
|
|
*/ |
|
21
|
|
|
public $id; |
|
22
|
|
|
public $user; # question/answer/comment |
|
23
|
|
|
public $parentId; # All posts have different ids |
|
24
|
|
|
public $parentType; # post or comment |
|
25
|
|
|
public $upVote; |
|
26
|
|
|
public $downVote; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Return a vote |
|
31
|
|
|
* @param string $sql |
|
32
|
|
|
* @param array $params |
|
33
|
|
|
* |
|
34
|
|
|
* @return object |
|
35
|
|
|
*/ |
|
36
|
9 |
|
public function getVote($sql, $params) |
|
37
|
|
|
{ |
|
38
|
9 |
|
$likes = $this->findAllWhere("$sql", $params); |
|
39
|
|
|
|
|
40
|
|
|
// Return users of the ones who have upvoted. |
|
41
|
|
|
$score = array_reduce($likes, function ($carry, $item) { |
|
42
|
9 |
|
$carry += $item->upVote - $item->downVote; |
|
43
|
9 |
|
return $carry; |
|
44
|
9 |
|
}); |
|
45
|
|
|
|
|
46
|
9 |
|
$this->likes = $likes; |
|
|
|
|
|
|
47
|
9 |
|
$this->score = $score; |
|
|
|
|
|
|
48
|
9 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Checks if vote already exists, then either create new or update |
|
53
|
|
|
* @param array $params |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function saveVote($user, $parentId, $parentType, $voteType = "like") |
|
58
|
|
|
{ |
|
59
|
2 |
|
$vote = $this->findAllWhere("parentId = ? AND parentType = ? AND user = ?", [$parentId, $parentType, $user])[0]; |
|
60
|
|
|
|
|
61
|
2 |
|
if ($vote != null) { |
|
62
|
2 |
|
$vote->setDb($this->db); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
if ($vote == null) { |
|
66
|
|
|
$vote = $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$vote->user = $user; |
|
70
|
2 |
|
$vote->parentId = $parentId; |
|
71
|
2 |
|
$vote->parentType = $parentType; |
|
72
|
2 |
|
$vote->upVote = 1; |
|
73
|
2 |
|
$vote->downVote = null; |
|
74
|
|
|
|
|
75
|
2 |
|
if ($voteType == "downVote") { |
|
76
|
1 |
|
$vote->upVote = null; |
|
77
|
1 |
|
$vote->downVote = 1; |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
2 |
|
$vote->save(); |
|
83
|
2 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Control if user has already liked or not |
|
88
|
|
|
* @param string $user |
|
89
|
|
|
* @param array $params |
|
|
|
|
|
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
1 |
View Code Duplication |
public function like($user, $parentId, $parentType) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
1 |
|
$likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
|
96
|
|
|
// Return users of the ones who have upvoted. |
|
97
|
|
|
$users = array_map(function ($like) { |
|
98
|
1 |
|
return $like->upVote != null ? $like->user : ""; |
|
99
|
1 |
|
}, $likes); |
|
100
|
|
|
|
|
101
|
1 |
|
if (in_array($user, $users)) { |
|
102
|
1 |
|
return false; |
|
103
|
|
|
} |
|
104
|
1 |
|
$this->saveVote($user, $parentId, $parentType); |
|
105
|
1 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Control if user has already liked or not |
|
110
|
|
|
* @param string $user |
|
111
|
|
|
* @param array $params |
|
|
|
|
|
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
1 |
View Code Duplication |
public function dislike($user, $parentId, $parentType) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
1 |
|
$likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]); |
|
118
|
|
|
// Return users of the ones who have downvoted. |
|
119
|
1 |
|
$users = array_map(function ($like) { |
|
120
|
1 |
|
return $like->downVote != null ? $like->user : ""; |
|
121
|
1 |
|
}, $likes); |
|
122
|
|
|
|
|
123
|
1 |
|
if (in_array($user, $users)) { |
|
124
|
1 |
|
return false; |
|
125
|
|
|
} |
|
126
|
1 |
|
$this->saveVote($user, $parentId, $parentType, "downVote"); |
|
127
|
1 |
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: