VotedataHandler::getLastVotes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Gestion des votes sur les produits
27
 */
28
29
/**
30
 * Class VotedataHandler
31
 */
32
class VotedataHandler extends OledrionPersistableObjectHandler
33
{
34
    /**
35
     * VotedataHandler constructor.
36
     * @param \XoopsDatabase|null $db
37
     */
38
    public function __construct(\XoopsDatabase $db = null)
39
    {
40
        if (null === $db) {
41
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
42
        }
43
        //                                Table                   Classe                             Id
44
        parent::__construct($db, 'oledrion_votedata', Votedata::class, 'vote_ratingid');
45
    }
46
47
    /**
48
     * Renvoie le nombre total de votes pour un produit ainsi que la sommes des votes
49
     *
50
     * @param int $product_id Identifiant du produit
51
     * @param int $totalVotes Variable passée par référence et devant contenir le nombre total de votes du produit
52
     * @param int $sumRating  Variable passée par référence et devant contenir le cumul des votes
53
     * @return int Rien
54
     */
55
    public function getCountRecordSumRating($product_id, &$totalVotes, &$sumRating)
56
    {
57
        $sql    = 'SELECT count( * ) AS cpt, sum( vote_rating ) AS sum_rating FROM ' . $this->table . ' WHERE vote_product_id = ' . (int)$product_id;
58
        $result = $this->db->query($sql);
59
        if (!$result) {
60
            return 0;
61
        }
62
63
        $myrow      = $this->db->fetchArray($result);
64
        $totalVotes = $myrow['cpt'];
65
        $sumRating  = $myrow['sum_rating'];
66
    }
67
68
    /**
69
     * Returns the (x) last votes
70
     *
71
     * @param int $start Starting position
72
     * @param int $limit count of items to return
73
     * @return array   Array of votedata objects
74
     */
75
    public function getLastVotes($start = 0, $limit = 0)
76
    {
77
        $tbl_datas = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tbl_datas is dead and can be removed.
Loading history...
78
        $criteria  = new \Criteria('vote_ratingid', 0, '<>');
79
        $criteria->setLimit($limit);
80
        $criteria->setStart($start);
81
        $criteria->setSort('vote_ratingtimestamp');
82
        $criteria->setOrder('DESC');
83
        $tbl_datas = $this->getObjects($criteria, true);
84
85
        return $tbl_datas;
86
    }
87
88
    /**
89
     * Suppression des votes d'un produit
90
     *
91
     * @param int $vote_product_id L'identifiant du produit
92
     * @return bool résultat de la suppression
93
     */
94
    public function deleteProductRatings($vote_product_id)
95
    {
96
        $criteria = new \Criteria('vote_product_id', $vote_product_id, '=');
97
98
        return $this->deleteAll($criteria);
99
    }
100
101
    /**
102
     * Indique si un utilisateur a déjà voté pour un produit
103
     *
104
     * @param int $vote_uid        L'identifiant de l'utilisateur
105
     * @param int $vote_product_id Le numéro du produit
106
     * @return bool True s'il a déjà voté sinon False
107
     */
108
    public function hasUserAlreadyVoted($vote_uid, $vote_product_id)
109
    {
110
        if (0 == $vote_uid) {
111
            $vote_uid = Oledrion\Utility::getCurrentUserID();
112
        }
113
        $criteria = new \CriteriaCompo();
114
        $criteria->add(new \Criteria('vote_product_id', $vote_product_id, '='));
115
        $criteria->add(new \Criteria('vote_uid', $vote_uid, '='));
116
        $count = $this->getCount($criteria);
117
118
        return $count > 0;
119
    }
120
121
    /**
122
     * Indique si un utilisateur anonyme a déjà voté (d'après son adresse IP)
123
     *
124
     * @param  string $ip              L'adresse IP
125
     * @param int     $vote_product_id Ld'identifiant du produit
126
     * @return bool
127
     */
128
    public function hasAnonymousAlreadyVoted($ip = '', $vote_product_id = 0)
129
    {
130
        if ('' === $ip) {
131
            $ip = Oledrion\Utility::IP();
132
        }
133
        $anonwaitdays = 1;
134
        $yesterday    = (time() - (86400 * $anonwaitdays));
135
        $criteria     = new \CriteriaCompo();
136
        $criteria->add(new \Criteria('vote_product_id', $vote_product_id, '='));
137
        $criteria->add(new \Criteria('vote_uid', 0, '='));
138
        $criteria->add(new \Criteria('vote_ratinghostname', $ip, '='));
139
        $criteria->add(new \Criteria('vote_ratingtimestamp', $yesterday, '>'));
140
        $count = $this->getCount($criteria);
141
142
        return $count > 0;
143
    }
144
145
    /**
146
     * Crée un vote pour un produit
147
     *
148
     * @param int $vote_product_id L'identifiant du produit
149
     * @param int $vote_uid        L'identifiant de l'utilisateur
150
     * @param int $vote_rating     Le vote
151
     * @return bool résultat de la création du vote
152
     */
153
    public function createRating($vote_product_id, $vote_uid, $vote_rating)
154
    {
155
        $product = $this->create(true);
156
        $product->setVar('vote_product_id', $vote_product_id);
157
        $product->setVar('vote_uid', $vote_uid);
158
        $product->setVar('vote_rating', $vote_rating);
159
        $product->setVar('vote_ratinghostname', Oledrion\Utility::IP());
160
        $product->setVar('vote_ratingtimestamp', time());
161
162
        return $this->insert($product);
163
    }
164
}
165