<commit_before><?php
namespace wcf\data\user\trophy;
use wcf\data\trophy\Trophy;
use wcf\data\trophy\TrophyCache;
use wcf\data\user\User;
use wcf\data\user\UserProfile;
use wcf\data\DatabaseObject;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\event\EventHandler;
use wcf\system\exception\SystemException;
use wcf\system\WCF;
use wcf\util\StringUtil;

/**
 * Represents a user trophy.
 *
 * @author	<NAME>
 * @copyright	2001-2017 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Data\User\Trophy
 * @since	3.1
 *
 * @property-read	integer		$userTrophyID			unique id of the user trophy
 * @property-read	integer		$trophyID			trophy id
 * @property-read	integer		$userID				user id
 * @property-read	integer		$time				the time when the trophy was rewarded
 * @property-read	string		$description			the custom trophy description
 * @property-read	string		$useCustomDescription		`1`, iif the trophy use a custom description
 */
class UserTrophy extends DatabaseObject {
	/**
	 * The description text replacements. 
	 * @var string[]
	 */
	private $replacements; 
	
	/**
	 * Returns the trophy for the user trophy. 
	 * 
	 * @return Trophy
	 */
	public function getTrophy() {
		return TrophyCache::getInstance()->getTrophyByID($this->trophyID);
	}
	
	/**
	 * Returns the user profile for the user trophy.
	 *
	 * @return UserProfile
	 */
	public function getUserProfile() {
		return UserProfileRuntimeCache::getInstance()->getObject($this->userID);
	}
	
	/**
	 * Returns the parsed description.
	 * 
	 * @return string
	 */
	public function getDescription() {
		if (!$this->useCustomDescription) {
			return $this->getTrophy()->getDescription();
		}
		
		return nl2br(StringUtil::encodeHTML(strtr($this->description, $this->getReplacements())));
	}
	
	/**
	 * Returns true, if the given user can see this user trophy.
	 *
	 * @param 	User 	$user
	 * @return 	bool
	 * @throws 	SystemException
	 */
	public function canSee(User $user = null) {
		if ($user === null) {
			$user = WCF::getUser();
		}
		
		if (!$user->userID) {
			$userProfile = new UserProfile(new User(null, []));
		} else {
			$userProfile = UserProfileRuntimeCache::getInstance()->getObject($user->userID);
		}
		
		if (!$userProfile->getPermission('user.profile.trophy.canSeeTrophies')) {
			return false;
		}
		
		if ($this->getTrophy()->isDisabled()) {
			return false;
		}
		
		// @TODO check user option canViewTrophies
		return true;
	}
	
	/**
	 * Returns an array with replacements for the trophy. 
	 * 
	 * @return string[]
	 */
	protected function getReplacements() {
		if ($this->replacements == null) {
			$replacements = [
				'{$username}' => $this->getUserProfile()->username
			];
			
			$parameters = ['replacements' => $replacements];
			
			EventHandler::getInstance()->fireAction($this, 'getDescription', $parameters);
			
			$this->replacements = $parameters['replacements'];
		}
		
		return $this->replacements; 
	}
}<commit_msg>Improve event name for getReplacements<commit_after><?php
namespace wcf\data\user\trophy;
use wcf\data\trophy\Trophy;
use wcf\data\trophy\TrophyCache;
use wcf\data\user\User;
use wcf\data\user\UserProfile;
use wcf\data\DatabaseObject;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\event\EventHandler;
use wcf\system\exception\SystemException;
use wcf\system\WCF;
use wcf\util\StringUtil;

/**
 * Represents a user trophy.
 *
 * @author	<NAME>
 * @copyright	2001-2017 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Data\User\Trophy
 * @since	3.1
 *
 * @property-read	integer		$userTrophyID			unique id of the user trophy
 * @property-read	integer		$trophyID			trophy id
 * @property-read	integer		$userID				user id
 * @property-read	integer		$time				the time when the trophy was rewarded
 * @property-read	string		$description			the custom trophy description
 * @property-read	string		$useCustomDescription		`1`, iif the trophy use a custom description
 */
class UserTrophy extends DatabaseObject {
	/**
	 * The description text replacements. 
	 * @var string[]
	 */
	private $replacements; 
	
	/**
	 * Returns the trophy for the user trophy. 
	 * 
	 * @return Trophy
	 */
	public function getTrophy() {
		return TrophyCache::getInstance()->getTrophyByID($this->trophyID);
	}
	
	/**
	 * Returns the user profile for the user trophy.
	 *
	 * @return UserProfile
	 */
	public function getUserProfile() {
		return UserProfileRuntimeCache::getInstance()->getObject($this->userID);
	}
	
	/**
	 * Returns the parsed description.
	 * 
	 * @return string
	 */
	public function getDescription() {
		if (!$this->useCustomDescription) {
			return $this->getTrophy()->getDescription();
		}
		
		return nl2br(StringUtil::encodeHTML(strtr($this->description, $this->getReplacements())));
	}
	
	/**
	 * Returns true, if the given user can see this user trophy.
	 *
	 * @param 	User 	$user
	 * @return 	bool
	 * @throws 	SystemException
	 */
	public function canSee(User $user = null) {
		if ($user === null) {
			$user = WCF::getUser();
		}
		
		if (!$user->userID) {
			$userProfile = new UserProfile(new User(null, []));
		} else {
			$userProfile = UserProfileRuntimeCache::getInstance()->getObject($user->userID);
		}
		
		if (!$userProfile->getPermission('user.profile.trophy.canSeeTrophies')) {
			return false;
		}
		
		if ($this->getTrophy()->isDisabled()) {
			return false;
		}
		
		// @TODO check user option canViewTrophies
		return true;
	}
	
	/**
	 * Returns an array with replacements for the trophy. 
	 * 
	 * @return string[]
	 */
	protected function getReplacements() {
		if ($this->replacements == null) {
			$replacements = [
				'{$username}' => $this->getUserProfile()->username
			];
			
			$parameters = ['replacements' => $replacements];
			
			EventHandler::getInstance()->fireAction($this, 'getReplacements', $parameters);
			
			$this->replacements = $parameters['replacements'];
		}
		
		return $this->replacements; 
	}
}
{'fraction_non_alphanumeric': 0.1369321922317314, 'fraction_numerical': 0.003949967083607637, 'mean_word_length': 3.7145073700543056, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 53, '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': '2014556', 'n_tokens_mistral': 2236, 'n_tokens_neox': 1915, 'n_words': 462}