public function saveRating($data, $docId, $criteriaArray = array(), $commentId = 0) { $user = JFactory::getUser(); $created = JFactory::getDate()->toSql(); $ratingScore = JUDownloadFrontHelperRating::calculateRatingScore($data, $docId, $criteriaArray); $dataRating = array('doc_id' => $docId, 'user_id' => $user->id, 'score' => $ratingScore, 'created' => $created); JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_judownload/tables'); $ratingTable = JTable::getInstance('Rating', 'JUDownloadTable'); $ratingTable->bind($dataRating); if (!$ratingTable->check()) { $this->setError($ratingTable->getError()); return false; } $ratingStore = $ratingTable->store(); if ($ratingStore) { if ($commentId) { $db = JFactory::getDbo(); $query = "UPDATE #__judownload_comments SET rating_id = " . $ratingTable->id . " WHERE id = " . $commentId; $db->setQuery($query); $db->execute(); } } else { $this->setError($ratingTable->getError()); return false; } if ($ratingStore && count($criteriaArray) > 0) { foreach ($criteriaArray AS $criteria) { JUDownloadMultiRating::insertCriteriaValue($ratingTable->id, $criteria->id, $criteria->value); } } if ($commentId == 0 || $data['approved'] == 1) { $documentItem = JUDownloadHelper::getDocumentById($docId); $totalVoteTimes = 0; $params = JUDownloadHelper::getParams(null, $docId); $onlyCalculateLastRating = $params->get('only_calculate_last_rating', 0); if (!$user->get('guest')) { $totalVoteTimes = JUDownloadFrontHelperRating::getTotalDocumentVotesOfUser($user->id, $docId); } if ($onlyCalculateLastRating && ($totalVoteTimes > 0)) { $total_votes = $documentItem->total_votes; $ratingLatest = $this->getLatestRating($docId, $user->id); $rating = (($documentItem->rating * $total_votes) + $ratingScore - $ratingLatest) / $total_votes; } else { $total_votes = $documentItem->total_votes + 1; $rating = (($documentItem->rating * $documentItem->total_votes) + $ratingScore) / $total_votes; } $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->update('#__judownload_documents'); $query->set('rating = ' . $rating); $query->set('total_votes = ' . $total_votes); $query->where('id = ' . $docId); $db->setQuery($query); $db->execute(); } $session = JFactory::getSession(); $timeNow = JFactory::getDate()->toSql(); $timeNowStamp = strtotime($timeNow); $inputCookie = JFactory::getApplication()->input->cookie; $config = JFactory::getConfig(); $cookie_domain = $config->get('cookie_domain', ''); $cookie_path = $config->get('cookie_path', '/'); $inputCookie->set('judl-document-rated-' . $docId, $timeNowStamp, time() + 864000, $cookie_path, $cookie_domain); $session->set('judl-document-rated-' . $docId, $timeNowStamp); $logData = array( 'user_id' => $user->id, 'event' => 'document.rate', 'item_id' => $docId, 'doc_id' => $docId, 'value' => $ratingScore, 'reference' => $ratingTable->id ); JUDownloadFrontHelperLog::addLog($logData); return true; }
public static function canRateDocument($documentId) { $documentObject = JUDownloadHelper::getDocumentById($documentId); if (!is_object($documentObject)) { return false; } $params = JUDownloadHelper::getParams(null, $documentId); if (!$params->get('enable_document_rate', 1)) { return false; } $userCanViewDocument = JUDownloadFrontHelperPermission::userCanDoDocument($documentId, true); if (!$userCanViewDocument) { return false; } $ratingField = new JUDownloadFieldCore_rating(); if (!$ratingField->canView()) { return false; } $user = JFactory::getUser(); $criteriaGroupId = JUDownloadFrontHelperCriteria::getCriteriaGroupIdByCategoryId($documentObject->cat_id); if ($criteriaGroupId == 0 || !JUDownloadHelper::hasMultiRating()) { $assetName = 'com_judownload.category.' . $documentObject->cat_id; if ($user->authorise('judl.single.rate', $assetName) || (JUDownloadFrontHelperPermission::canDownloadDocument($documentId) && $params->get('can_download_can_rate', 0))) { if ($user->authorise('judl.single.rate.many_times', $assetName)) { return true; } else { if ($user->get('guest')) { $session = JFactory::getSession(); if (!$session->has('judl-document-rated-' . $documentId)) { return true; } } else { $totalVoteTimes = JUDownloadFrontHelperRating::getTotalDocumentVotesOfUser($user->id, $documentId); if ($totalVoteTimes == 0) { return true; } } } } } else { $assetName = 'com_judownload.criteriagroup.' . $criteriaGroupId; if ($user->authorise('judl.criteria.rate', $assetName) || (JUDownloadFrontHelperPermission::canDownloadDocument($documentId) && $params->get('can_download_can_rate', 0))) { if ($user->authorise('judl.criteria.rate.many_times', $assetName)) { return true; } else { if ($user->get('guest')) { $session = JFactory::getSession(); if (!$session->has('judl-document-rated-' . $documentId)) { return true; } } else { $totalVoteTimes = JUDownloadFrontHelperRating::getTotalDocumentVotesOfUser($user->id, $documentId); if ($totalVoteTimes == 0) { return true; } } } } } return false; }