1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\XoopsObject;
14: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: class PagePage_rating extends XoopsObject
28: {
29: 30: 31:
32: public function __construct()
33: {
34: $this->initVar('rating_id', XOBJ_DTYPE_INT, 0, false, 10);
35: $this->initVar('rating_content_id', XOBJ_DTYPE_INT, null, false, 10);
36: $this->initVar('rating_uid', XOBJ_DTYPE_INT, null, false, 10);
37: $this->initVar('rating_rating', XOBJ_DTYPE_OTHER, null, false, 3);
38: $this->initVar('rating_ip', XOBJ_DTYPE_TXTBOX, null, false);
39: $this->initVar('rating_date', XOBJ_DTYPE_INT, time(), false, 10);
40: }
41: }
42:
43: class PagePage_ratingHandler extends XoopsPersistableObjectHandler
44: {
45: 46: 47:
48: public function __construct(Connection $db = null)
49: {
50: parent::__construct($db, 'page_rating', 'pagepage_rating', 'rating_id', 'rating_contentid');
51: }
52:
53: public function getVotebyUser($content_id)
54: {
55: $helper = Page::getInstance();
56: $uid = $helper->getUserId();
57: $ip = $helper->xoops()->getEnv('REMOTE_ADDR');
58:
59:
60: $criteria = new CriteriaCompo();
61: $criteria->add(new Criteria('rating_content_id', $content_id));
62:
63: $criteria2 = new CriteriaCompo();
64: $criteria2->add(new Criteria('rating_uid', $uid), 'OR');
65: $criteria2->add(new Criteria('rating_ip', $ip), 'OR');
66: $criteria->add($criteria2, 'AND');
67:
68: $res = parent::getAll($criteria, null, true, false);
69: if (count($res) > 0) {
70: return $res[0]->getVar('rating_rating');
71: }
72: return -1;
73: }
74:
75: public function hasVoted($content_id)
76: {
77: $helper = Page::getInstance();
78: $uid = $helper->getUserId();
79: $ip = $helper->xoops()->getEnv('REMOTE_ADDR');
80:
81: $criteria = new CriteriaCompo();
82: $criteria->add(new Criteria('rating_content_id', $content_id));
83:
84: $criteria2 = new CriteriaCompo();
85: $criteria2->add(new Criteria('rating_uid', $uid), 'OR');
86: $criteria2->add(new Criteria('rating_ip', $ip), 'OR');
87: $criteria->add($criteria2, 'AND');
88: return parent::getCount($criteria);
89: }
90:
91: public function getStats($content_id)
92: {
93: $criteria = new CriteriaCompo();
94: $criteria->add(new Criteria('rating_content_id', $content_id));
95:
96: $i = 0;
97: $total = 0;
98: $obj = parent::getAll($criteria);
99: foreach ($obj as $k => $v) {
100: ++$i;
101: $total += $v->getVar('rating_rating');
102: }
103: return array('voters' => $i, 'average' => $total/$i);
104: }
105: }
106: