Back to PhocaGalleryRateImage class

Method updateVoteStatistics

public static
updateVoteStatistics
(mixed $imgid)

Method updateVoteStatistics - Source code

public static function updateVoteStatistics($imgid)
{
    $db = Factory::getDBO();
    // Get AVG and COUNT
    $query = 'SELECT COUNT(vs.id) AS count, AVG(vs.rating) AS average' . ' FROM #__phocagallery_img_votes AS vs' . ' WHERE vs.imgid = ' . (int) $imgid;
    //		.' AND vs.published = 1';
    $db->setQuery($query, 0, 1);
    $votesStatistics = $db->loadObject();
    // if no count, set the average to 0
    if ($votesStatistics->count == 0) {
        $votesStatistics->count = (int) 0;
        $votesStatistics->average = (float) 0;
    }
    if (isset($votesStatistics->count) && isset($votesStatistics->average)) {
        // Insert or update
        $query = 'SELECT vs.id AS id' . ' FROM #__phocagallery_img_votes_statistics AS vs' . ' WHERE vs.imgid = ' . (int) $imgid . ' ORDER BY vs.id';
        $db->setQuery($query, 0, 1);
        $votesStatisticsId = $db->loadObject();
        // Yes, there is id (UPDATE) x No, there isn't (INSERT)
        if (!empty($votesStatisticsId->id)) {
            $query = 'UPDATE #__phocagallery_img_votes_statistics' . ' SET count = ' . (int) $votesStatistics->count . ' , average = ' . (float) $votesStatistics->average . ' WHERE imgid = ' . (int) $imgid;
            $db->setQuery($query);
            $db->execute();
        } else {
            $query = 'INSERT into #__phocagallery_img_votes_statistics' . ' (id, imgid, count, average)' . ' VALUES (null, ' . (int) $imgid . ' , ' . (int) $votesStatistics->count . ' , ' . (float) $votesStatistics->average . ')';
            $db->setQuery($query);
            $db->execute();
        }
    } else {
        return false;
    }
    return true;
}