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 #__phocadownload_file_votes_statistics AS vs' .' WHERE vs.fileid = '.(int) $fileid; $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 #__phocadownload_file_votes_statistics' .' SET count = ' .(int)$votesStatistics->count .' , average = ' .(float)$votesStatistics->average .' WHERE fileid = '.(int) $fileid; $db->setQuery($query); if (!$db->execute()) { throw new Exception('Database Error Voting 1', 500); return false; } } else { $query = 'INSERT into #__phocadownload_file_votes_statistics' .' (id, fileid, count, average)' .' VALUES (null, '.(int)$fileid .' , '.(int)$votesStatistics->count .' , '.(float)$votesStatistics->average .')'; $db->setQuery($query); if (!$db->execute()) { throw new Exception('Database Error Voting 2', 500); return false; } } } else { return false; } return true; } function getVotesStatisticsFile($id) { $db = JFactory::getDBO(); $query = 'SELECT vs.count AS count, vs.average AS average' .' FROM #__phocadownload_file_votes_statistics AS vs' .' WHERE vs.fileid = '.(int) $id; $db->setQuery($query, 0, 1); $votesStatistics = $db->loadObject(); return $votesStatistics; } function checkUserVoteFile($fileid, $userid) { $db = JFactory::getDBO(); $query = 'SELECT v.id AS id' .' FROM #__phocadownload_file_votes AS v' .' WHERE v.fileid = '. (int)$fileid .' AND v.userid = '. (int)$userid; $db->setQuery($query, 0, 1); $checkUserVote = $db->loadObject(); if ($checkUserVote) { return true; } return false; } public function renderRateFile($id, $displayRating, $small = 1, $refresh = false) { $user = JFactory::getUser(); $neededAccessLevels = PhocaDownloadAccess::getNeededAccessLevels(); $access = PhocaDownloadAccess::isAccess($user->getAuthorisedViewLevels(), $neededAccessLevels); if ($small == 1) { $smallO = '-small'; $ratio = 18; } else { $smallO = ''; $ratio = 22; } $o = ''; //.$rating['urlvote'].$amp.'controller=detail&task=rate&rating=1 //$amp = PhocaDownloadUtils::setQuestionmarkOrAmp($rating['urlvote']); $href = 'javascript:void(0);'; if ((int)$displayRating != 1) { return ''; } else { $rating['alreadyratedfile'] = self::checkUserVoteFile( (int)$id, (int)$user->id ); $rating['notregisteredfile'] = true; //$rating['usernamefile'] = ''; if ($access > 0) { $rating['notregisteredfile'] = false; $rating['usernamefile'] = $user->name; } $rating['votescountfile'] = 0; $rating['votesaveragefile'] = 0; $rating['voteswidthfile'] = 0; $votesStatistics = self::getVotesStatisticsFile((int)$id); if (!empty($votesStatistics->count)) { $rating['votescountfile'] = $votesStatistics->count; } if (!empty($votesStatistics->average)) { $rating['votesaveragefile'] = $votesStatistics->average; if ($rating['votesaveragefile'] > 0) { $rating['votesaveragefile'] = round(((float)$rating['votesaveragefile'] / 0.5)) * 0.5; $rating['voteswidthfile'] = $ratio * $rating['votesaveragefile']; } else { $rating['votesaveragefile'] = (int)0;// not float displaying } } // Leave message for already voted images //$vote = JFactory::getApplication()->input->get('vote', 0, '', 'int'); $voteMsg = JText::_('COM_PHOCADOWNLOAD_RATING_ALREADY_RATED_FILE'); //if ($vote == 1) { // $voteMsg = JText::_('COM_PHOCADOWNLOAD_ALREADY_RATED_FILE_THANKS'); //} $rating['votestextimg'] = 'VOTE'; if ((int)$rating['votescountfile'] > 1) { $rating['votestextimg'] = 'VOTES'; } $o .= '
' . JText::_('COM_PHOCADOWNLOAD_RATING'). ': ' . $rating['votesaveragefile'] .' / ' .$rating['votescountfile'] . ' ' . JText::_('COM_PHOCADOWNLOAD_'.$rating['votestextimg']). '  
'; if ($rating['alreadyratedfile']) { $o .= '
'; $or ='
'.JText::_('COM_PHOCADOWNLOAD_RATING_ALREADY_RATED_FILE').'
'; } else if ($rating['notregisteredfile']) { $o .= '
'; $or ='
'.JText::_('COM_PHOCADOWNLOAD_ONLY_REGISTERED_LOGGED_RATE_FILE').'
'; } else { $o .= '
'; $or ='
'; } } if ($refresh == true) { return $o;//we are in Ajax, return only content of pdvoting div } else { return '
'.$o.'
' .$or ;//not in ajax, return the contend in div } } public function renderRateFileJS($small = 1) { $document = JFactory::getDocument(); $url = 'index.php?option=com_phocadownload&view=ratingfilea&task=rate&format=json&'.JSession::getFormToken().'=1'; $urlRefresh = 'index.php?option=com_phocadownload&view=ratingfilea&task=refreshrate&small='.$small.'&format=json&'.JSession::getFormToken().'=1'; $imgLoadingUrl = JURI::base(). 'components/com_phocadownload/assets/images/icon-loading2.gif'; $imgLoadingHTML = ''; $js = ''; $document->addCustomTag($js); } } ?>