-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rating.php
62 lines (51 loc) · 1.41 KB
/
Rating.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class Recipe_Model_Rating extends Recipe_Model_GenericModel implements Zend_Acl_Resource_Interface
{
var $ownerUserId = null;
public function getResourceId()
{
return 'rating';
}
public function getRatings($recipe_id, $max = 5)
{
$select = $this->db->select()
->from(array('r' => 'ratings'))
->joinLeft(array('u' => 'users'), 'r.user_id = u.id', array(
'username' => 'u.name'
))
->where('recipe_id = ?', $recipe_id)
->limit($max);
return $this->db->fetchAll($select);
}
/**
* Calculate rating for recipe
*
* @param int $recipe_id
* @return int
*/
public function getAvgRating($recipe_id)
{
/* Get average rating for recipe_ip */
$select = $this->db->select()->from('ratings',array("rating" => "AVG(value)"))->where('recipe_id = ?',$recipe_id);
$avg = $this->db->fetchOne($select);
/* Round down to a sensible prescision, like 3.5 */
return round($avg,1);
}
/**
* Check if this user has rated this recipe already
*
* @param int $recipe_id
* @param int $user_id
* @return bool
*/
public function hasRated($recipe_id, $user_id)
{
$select = $this->db->select()
->from('ratings',array("numberOfRatings" => "COUNT(*)"))
->where("recipe_id = ?", $recipe_id)
->where("user_id = ?", $user_id);
if ($this->db->fetchOne($select) > 0)
return true;
return false;
}
}