<commit_before><?php


/**
 * Skeleton subclass for performing query and update operations on the 'category_kuser' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package Core
 * @subpackage model
 */
class categoryKuserPeer extends BasecategoryKuserPeer {
	
	/**
	 * 
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param $con
	 * 
	 * @return categoryKuser
	 */
	public static function retrieveByCategoryIdAndKuserId($categoryId, $kuserId, $con = null)
	{
		$criteria = new Criteria();

		$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);

		return categoryKuserPeer::doSelectOne($criteria, $con);
	}

	/**
	 *
	 * @param int $kuserId
	 * @param $con
	 *
	 * @return array Array of categoryKuser
	 */
	public static function retrieveByKuserId($kuserId, $con = null)
	{
		$criteria = new Criteria();
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);

		return categoryKuserPeer::doSelect($criteria, $con);
	}
	
	/**
	 * 
	 * @param int $kuserId
	 * @return bool - no need to fetch the objects
	 */
	public static function isCategroyKuserExistsForKuser($kuserId)
	{
		$criteria = new Criteria();

		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);
		
		$categoryKuser = categoryKuserPeer::doSelectOne($criteria);
		
		if($categoryKuser)
			return true;
			
		return false;
	}


	/**
	 *  this function return categoryUser if the user has explicit or implicit (by group) required permissions on the category
	 *
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param array $requiredPermissions
	 * @param bool $supportGroups
	 * @param null $con
	 * @return categoryKuser|null
	 */
	public static function retrievePermittedKuserInCategory($categoryId, $kuserId = null, $requiredPermissions = null, $supportGroups = true, $con = null){
		$category = categoryPeer::retrieveByPK($categoryId);
		if(!$category)
			return null;

		if($category->getInheritedParentId())
			$categoryId = $category->getInheritedParentId();

		if(is_null($kuserId))
			$kuserId = kCurrentContext::getCurrentKsKuserId();

		if(is_null($requiredPermissions))
			$requiredPermissions = array(PermissionName::CATEGORY_VIEW);

		$categoryKuser = self::retrieveByCategoryIdAndActiveKuserId($categoryId, $kuserId, $requiredPermissions, $con);
		if (!is_null($categoryKuser)){
			return $categoryKuser;
		}

		//check if kuserId has permission in category by a junction group
		if($supportGroups)
		{
			$kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuserId);
			if (count($kgroupIds) == 0)
				return null;

			$criteria = new Criteria();
			$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
			$criteria->add(categoryKuserPeer::KUSER_ID, $kgroupIds, Criteria::IN);
			$criteria->add(categoryKuserPeer::STATUS, CategoryKuserStatus::ACTIVE);
			$categoryKusers = categoryKuserPeer::doSelect($criteria, $con);
			if(!$categoryKusers)
				return null;
			foreach( $categoryKusers as $categoryKuser){
				foreach($requiredPermissions as $requiredPermission){
					if($categoryKuser->hasPermission($requiredPermission)){
						return $categoryKuser;
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * 
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param array $requiredPermissions
	 * @param $con
	 * 
	 * @return categoryKuser
	 */
	public static function retrieveByCategoryIdAndActiveKuserId($categoryId, $kuserId, $requiredPermissions, $con = null)
	{
		$criteria = new Criteria();
		$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);
		$criteria->add(categoryKuserPeer::STATUS, CategoryKuserStatus::ACTIVE);<commit_msg>PLAT-9211: Always return group with highest permission level when user is assigned to multiple groups<commit_after><?php


/**
 * Skeleton subclass for performing query and update operations on the 'category_kuser' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package Core
 * @subpackage model
 */
class categoryKuserPeer extends BasecategoryKuserPeer {
	
	/**
	 * 
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param $con
	 * 
	 * @return categoryKuser
	 */
	public static function retrieveByCategoryIdAndKuserId($categoryId, $kuserId, $con = null)
	{
		$criteria = new Criteria();

		$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);

		return categoryKuserPeer::doSelectOne($criteria, $con);
	}

	/**
	 *
	 * @param int $kuserId
	 * @param $con
	 *
	 * @return array Array of categoryKuser
	 */
	public static function retrieveByKuserId($kuserId, $con = null)
	{
		$criteria = new Criteria();
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);

		return categoryKuserPeer::doSelect($criteria, $con);
	}
	
	/**
	 * 
	 * @param int $kuserId
	 * @return bool - no need to fetch the objects
	 */
	public static function isCategroyKuserExistsForKuser($kuserId)
	{
		$criteria = new Criteria();

		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);
		
		$categoryKuser = categoryKuserPeer::doSelectOne($criteria);
		
		if($categoryKuser)
			return true;
			
		return false;
	}


	/**
	 *  this function return categoryUser if the user has explicit or implicit (by group) required permissions on the category
	 *
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param array $requiredPermissions
	 * @param bool $supportGroups
	 * @param null $con
	 * @return categoryKuser|null
	 */
	public static function retrievePermittedKuserInCategory($categoryId, $kuserId = null, $requiredPermissions = null, $supportGroups = true, $con = null){
		$category = categoryPeer::retrieveByPK($categoryId);
		if(!$category)
			return null;

		if($category->getInheritedParentId())
			$categoryId = $category->getInheritedParentId();

		if(is_null($kuserId))
			$kuserId = kCurrentContext::getCurrentKsKuserId();

		if(is_null($requiredPermissions))
			$requiredPermissions = array(PermissionName::CATEGORY_VIEW);

		$categoryKuser = self::retrieveByCategoryIdAndActiveKuserId($categoryId, $kuserId, $requiredPermissions, $con);
		if (!is_null($categoryKuser)){
			return $categoryKuser;
		}
		
		$permittedCategoryKuser = null;
		//check if kuserId has permission in category by a junction group
		if($supportGroups)
		{
			$kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuserId);
			if (count($kgroupIds) == 0)
				return null;

			$criteria = new Criteria();
			$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
			$criteria->add(categoryKuserPeer::KUSER_ID, $kgroupIds, Criteria::IN);
			$criteria->add(categoryKuserPeer::STATUS, CategoryKuserStatus::ACTIVE);
			$categoryKusers = categoryKuserPeer::doSelect($criteria, $con);
			if(!$categoryKusers)
				return null;
			
			foreach( $categoryKusers as $categoryKuser)
			{
				/* @var $categoryKuser categoryKuser */
				foreach($requiredPermissions as $requiredPermission)
				{
					if($categoryKuser->hasPermission($requiredPermission))
					{
						if(!$permittedCategoryKuser || $categoryKuser->getPermissionLevel() < $permittedCategoryKuser->getPermissionLevel())
							$permittedCategoryKuser = $categoryKuser;
					}
				}
			}
		}
		return $permittedCategoryKuser;
	}
	
	/**
	 * 
	 * @param int $categoryId
	 * @param int $kuserId
	 * @param array $requiredPermissions
	 * @param $con
	 * 
	 * @return categoryKuser
	 */
	public static function retrieveByCategoryIdAndActiveKuserId($categoryId, $kuserId, $requiredPermissions, $con = null)
	{
		$criteria = new Criteria();
		$criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
		$criteria->add(categoryKuserPeer::KUSER_ID, $kuserId);
		$criteria->add(categoryKuserPeer::STATUS, CategoryKuserStatus::ACTIVE);
{'fraction_non_alphanumeric': 0.12066608674040015, 'fraction_numerical': 0.000745619485522555, 'mean_word_length': 4.7240398293029875, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 31, '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': '7232607', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2508, 'n_words': 575}