<commit_before><?php
/**
 * Allows user to manipulate their entry rating
 *
 * @service rating
 * @package plugins.rating
 * @subpackage api.services
 */

class RatingService extends KalturaBaseService
{
	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);
		
		if(!RatingPlugin::isAllowedPartner($this->getPartnerId()))
		{
			throw new KalturaAPIException(KalturaErrors::FEATURE_FORBIDDEN, RatingPlugin::PLUGIN_NAME);
		}
		
		if (kCurrentContext::$ks_object->isAnonymousSession() && $actionName !== 'getRatingCounts')
		{
			throw new KalturaAPIException(KalturaErrors::ANONYMOUS_ACCESS_FORBIDDEN);
		}
	}
	
	/**
	 * @action rate
	 * Action for current kuser ro rate a specific entry
	 * @param string $entryId
	 * @param int $rank
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws KalturaErrors::INVALID_RANK_VALUE
	 * @return int
	 */
	public function rateAction ($entryId, $rank)
	{
		$dbEntry = entryPeer::retrieveByPK($entryId);
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		}
		
		//Check if a kvote for current entryId and kuser already exists.
		$existingKVote = kvotePeer::doSelectByEntryIdAndPuserId($entryId, $this->getPartnerId(), kCurrentContext::$ks_uid);
		if ($existingKVote)
		{
			$existingKVote->setRank($rank);
			$existingKVote->save();
		}
		else
		{
			kvotePeer::createKvote($entryId, $this->getPartnerId(), kCurrentContext::$ks_uid, $rank, KVoteType::RANK);
		}
		
		return $rank;
	}
	
	/**
	 * @action removeRating
	 * Action to remove current kuser's rating for a specific entry
	 * @param string $entryId
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws KalturaRatingErrors::USER_RATING_FOR_ENTRY_NOT_FOUND
	 * @return bool
	 */
	public function removeRatingAction ($entryId)
	{
		$dbEntry = entryPeer::retrieveByPK($entryId);
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);<commit_msg>Update RatingService.php<commit_after><?php
/**
 * Allows user to manipulate their entry rating
 *
 * @service rating
 * @package plugins.rating
 * @subpackage api.services
 */

class RatingService extends KalturaBaseService
{
	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);
		
		if(!RatingPlugin::isAllowedPartner($this->getPartnerId()))
		{
			throw new KalturaAPIException(KalturaErrors::FEATURE_FORBIDDEN, RatingPlugin::PLUGIN_NAME);
		}
		
		if (kCurrentContext::$ks_object->isAnonymousSession() && $actionName !== 'getRatingCounts')
		{
			throw new KalturaAPIException(KalturaErrors::ANONYMOUS_ACCESS_FORBIDDEN);
		}
	}
	
	/**
	 * @action rate
	 * Action for current kuser ro rate a specific entry
	 * @param string $entryId
	 * @param int $rank
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws KalturaErrors::INVALID_RANK_VALUE
	 * @return int
	 */
	public function rateAction ($entryId, $rank)
	{
		$dbEntry = entryPeer::retrieveByPK($entryId);
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		}
		
		if ($rank <= 0 || $rank > 5)
		{
			throw new KalturaAPIException(KalturaErrors::INVALID_RANK_VALUE);
		}
		
		//Check if a kvote for current entryId and kuser already exists.
		$existingKVote = kvotePeer::doSelectByEntryIdAndPuserId($entryId, $this->getPartnerId(), kCurrentContext::$ks_uid);
		if ($existingKVote)
		{
			$existingKVote->setRank($rank);
			$existingKVote->save();
		}
		else
		{
			kvotePeer::createKvote($entryId, $this->getPartnerId(), kCurrentContext::$ks_uid, $rank, KVoteType::RANK);
		}
		
		return $rank;
	}
	
	/**
	 * @action removeRating
	 * Action to remove current kuser's rating for a specific entry
	 * @param string $entryId
	 * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
	 * @throws KalturaRatingErrors::USER_RATING_FOR_ENTRY_NOT_FOUND
	 * @return bool
	 */
	public function removeRatingAction ($entryId)
	{
		$dbEntry = entryPeer::retrieveByPK($entryId);
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
{'fraction_non_alphanumeric': 0.12313970235237638, 'fraction_numerical': 0.00048007681228996637, 'mean_word_length': 5.127941176470588, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2222279', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1393, 'n_words': 276}