Exemple #1
0
	public function multiRating()
	{
		
		JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

		
		$app        = JFactory::getApplication();
		$data       = $app->input->getArray($_POST);
		$documentId = $data['doc_id'];
		$params     = JUDownloadHelper::getParams(null, $documentId);

		
		$canRateDocument = JUDownloadFrontHelperPermission::canRateDocument($documentId);

		JUDownloadHelper::obCleanData();

		if (!$canRateDocument)
		{
			echo JText::_('COM_JUDOWNLOAD_YOU_CAN_NOT_VOTE_ON_THIS_DOCUMENT');
			exit;
		}

		if (!JUDownloadHelper::hasMultiRating())
		{
			echo JText::_('COM_JUDOWNLOAD_MULTI_RATING_HAS_BEEN_DISABLED_PLEASE_CONTACT_ADMINISTRATOR');
			exit;
		}

		$inputCookie = $app->input->cookie;

		$ratingInterval = $params->get('rating_interval', 86400);
		$user           = JFactory::getUser();
		$timeNow        = JFactory::getDate()->toSql();
		$timeNowStamp   = strtotime($timeNow);
		if ($user->get('guest'))
		{
			
			$lastTimeRated = $inputCookie->get('judl-document-rated-' . $documentId, null);
			if ($lastTimeRated != null)
			{
				if ($timeNowStamp > $lastTimeRated)
				{
					if ($timeNowStamp - $lastTimeRated < $ratingInterval)
					{
						echo JText::_('COM_JUDOWNLOAD_YOU_ARE_ALREADY_VOTED_ON_THIS_DOCUMENT');
						exit;
					}
				}
			}
		}
		else
		{
			$lastTimeRated = JUDownloadFrontHelperRating::getLastTimeVoteDocumentOfUser($user->id, $documentId);
			if (!$lastTimeRated)
			{
				$lastTimeRated = 0;
			}
			$lastTimeRated = strtotime($lastTimeRated);
			if ($lastTimeRated > 0)
			{
				if ($timeNowStamp > $lastTimeRated)
				{
					if ($timeNowStamp - $lastTimeRated < $ratingInterval)
					{
						
						echo JText::_('COM_JUDOWNLOAD_YOU_ARE_ALREADY_VOTED_ON_THIS_DOCUMENT');
						exit;
					}
				}
			}
		}

		
		$dataValid     = array();
		$mainCatId     = JUDownloadFrontHelperCategory::getMainCategoryId($documentId);
		$criteriaArray = JUDownloadFrontHelperCriteria::getCriteriasByCatId($mainCatId);
		$postCriteria  = $data['criteria'];

		if (count($criteriaArray) > 0)
		{
			foreach ($criteriaArray AS $key => $criteria)
			{
				if ($criteria->required)
				{
					if (isset($postCriteria[$criteria->id]) && $postCriteria[$criteria->id] > 0 && $postCriteria[$criteria->id] <= 10)
					{
						$criteria->value = $postCriteria[$criteria->id];
					}
					else
					{
						
						echo JText::_('Invalid Field ' . $criteria->title);
						exit;
					}
				}
				else
				{
					if (isset($postCriteria[$criteria->id]) && $postCriteria[$criteria->id] > 0 && $postCriteria[$criteria->id] <= 10)
					{
						$criteria->value = $postCriteria[$criteria->id];
					}
					else
					{
						unset($criteriaArray[$key]);
					}
				}
			}
		}
		else
		{
			echo JText::_('COM_JUDOWNLOAD_VOTING_FAILED_PLEASE_CONTACT_ADMINISTRATOR');
			exit;
		}

		$model = $this->getModel();
		JUDownloadHelper::obCleanData();
		if ($model->saveRating($dataValid, $documentId, $criteriaArray))
		{
			echo JText::_('COM_JUDOWNLOAD_THANK_YOU_FOR_VOTING');

		}
		else
		{
			echo JText::_('COM_JUDOWNLOAD_VOTING_FAILED_PLEASE_CONTACT_ADMINISTRATOR');
		}
		exit;
	}