<commit_before><?php

namespace wcf\data\user\trophy;

use wcf\data\AbstractDatabaseObjectAction;
use wcf\data\user\UserAction;
use wcf\data\user\UserProfile;
use wcf\data\user\UserProfileAction;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\UserInputException;
use wcf\system\user\activity\event\UserActivityEventHandler;
use wcf\system\user\notification\object\UserTrophyNotificationObject;
use wcf\system\user\notification\UserNotificationHandler;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\WCF;

/**
 * Provides user trophy actions.
 *
 * @author  <NAME>
 * @copyright   2001-2019 WoltLab GmbH
 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package WoltLabSuite\Core\Data\User\Trophy
 * @since   3.1
 *
 * @method  UserTrophyEditor[]      getObjects()
 * @method  UserTrophyEditor        getSingleObject()
 */
class UserTrophyAction extends AbstractDatabaseObjectAction
{
    /**
     * @inheritDoc
     */
    protected $permissionsDelete = ['admin.trophy.canAwardTrophy'];

    /**
     * @inheritDoc
     */
    protected $allowGuestAccess = ['getGroupedUserTrophyList'];

    /**
     * @var UserProfile
     */
    public $userProfile;

    /**
     * @inheritDoc
     */
    public function create()
    {
        /** @var UserTrophy $userTrophy */
        $userTrophy = parent::create();

        if (!$userTrophy->getTrophy()->isDisabled()) {
            $userAction = new UserAction([$userTrophy->userID], 'update', [
                'counters' => [
                    'trophyPoints' => 1,
                ],
            ]);
            $userAction->executeAction();

            // checks if the user still has space to add special trophies
            if (\count($userTrophy->getUserProfile()->getSpecialTrophies()) < $userTrophy->getUserProfile()->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
                $hasTrophy = false;
                foreach (UserTrophyList::getUserTrophies([$userTrophy->getUserProfile()->userID])[$userTrophy->getUserProfile()->userID] as $trophy) {
                    if ($trophy->trophyID == $userTrophy->trophyID && $trophy->userTrophyID !== $userTrophy->userTrophyID) {
                        $hasTrophy = true;
                        break;
                    }
                }

                if (!$hasTrophy) {
                    $userProfileAction = new UserProfileAction(
                        [$userTrophy->getUserProfile()->getDecoratedObject()],
                        'updateSpecialTrophies',
                        [
                            'trophyIDs' => \array_unique(\array_merge(\array_map(static function ($trophy) {
                                return $trophy->trophyID;
                            }, $userTrophy->getUserProfile()->getSpecialTrophies()), [$userTrophy->trophyID])),
                        ]
                    );
                    $userProfileAction->executeAction();
                }
            }
        }

        UserActivityEventHandler::getInstance()->fireEvent(
            'com.woltlab.wcf.userTrophy.recentActivityEvent.trophyReceived',
            $userTrophy->getObjectID(),
            null,
            $userTrophy->userID
        );

        UserNotificationHandler::getInstance()->fireEvent(
            'received',
            'com.woltlab.wcf.userTrophy.notification',
            new UserTrophyNotificationObject($userTrophy),
            [
                $userTrophy->userID,
            ]
        );

        return $userTrophy;
    }

    /**
     * @inheritDoc
     */
    public function validateDelete()
    {
        parent::validateDelete();

        /** @var UserTrophy $object */
        foreach ($this->objects as $object) {
            if ($object->getTrophy()->awardAutomatically) {
                throw new PermissionDeniedException();
            }
        }
    }

    /**<commit_msg>Prevent firing notifications for awarding own trophies<commit_after><?php

namespace wcf\data\user\trophy;

use wcf\data\AbstractDatabaseObjectAction;
use wcf\data\user\UserAction;
use wcf\data\user\UserProfile;
use wcf\data\user\UserProfileAction;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\UserInputException;
use wcf\system\user\activity\event\UserActivityEventHandler;
use wcf\system\user\notification\object\UserTrophyNotificationObject;
use wcf\system\user\notification\UserNotificationHandler;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\WCF;

/**
 * Provides user trophy actions.
 *
 * @author  <NAME>
 * @copyright   2001-2019 WoltLab GmbH
 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package WoltLabSuite\Core\Data\User\Trophy
 * @since   3.1
 *
 * @method  UserTrophyEditor[]      getObjects()
 * @method  UserTrophyEditor        getSingleObject()
 */
class UserTrophyAction extends AbstractDatabaseObjectAction
{
    /**
     * @inheritDoc
     */
    protected $permissionsDelete = ['admin.trophy.canAwardTrophy'];

    /**
     * @inheritDoc
     */
    protected $allowGuestAccess = ['getGroupedUserTrophyList'];

    /**
     * @var UserProfile
     */
    public $userProfile;

    /**
     * @inheritDoc
     */
    public function create()
    {
        /** @var UserTrophy $userTrophy */
        $userTrophy = parent::create();

        if (!$userTrophy->getTrophy()->isDisabled()) {
            $userAction = new UserAction([$userTrophy->userID], 'update', [
                'counters' => [
                    'trophyPoints' => 1,
                ],
            ]);
            $userAction->executeAction();

            // checks if the user still has space to add special trophies
            if (\count($userTrophy->getUserProfile()->getSpecialTrophies()) < $userTrophy->getUserProfile()->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
                $hasTrophy = false;
                foreach (UserTrophyList::getUserTrophies([$userTrophy->getUserProfile()->userID])[$userTrophy->getUserProfile()->userID] as $trophy) {
                    if ($trophy->trophyID == $userTrophy->trophyID && $trophy->userTrophyID !== $userTrophy->userTrophyID) {
                        $hasTrophy = true;
                        break;
                    }
                }

                if (!$hasTrophy) {
                    $userProfileAction = new UserProfileAction(
                        [$userTrophy->getUserProfile()->getDecoratedObject()],
                        'updateSpecialTrophies',
                        [
                            'trophyIDs' => \array_unique(\array_merge(\array_map(static function ($trophy) {
                                return $trophy->trophyID;
                            }, $userTrophy->getUserProfile()->getSpecialTrophies()), [$userTrophy->trophyID])),
                        ]
                    );
                    $userProfileAction->executeAction();
                }
            }
        }

        UserActivityEventHandler::getInstance()->fireEvent(
            'com.woltlab.wcf.userTrophy.recentActivityEvent.trophyReceived',
            $userTrophy->getObjectID(),
            null,
            $userTrophy->userID
        );

		if ($userTrophy->userID != WCF::getUser()->userID) {
			UserNotificationHandler::getInstance()->fireEvent(
				'received',
				'com.woltlab.wcf.userTrophy.notification',
				new UserTrophyNotificationObject($userTrophy),
				[
					$userTrophy->userID,
				]
			);
		}

        return $userTrophy;
    }

    /**
     * @inheritDoc
     */
    public function validateDelete()
    {
        parent::validateDelete();

        /** @var UserTrophy $object */
        foreach ($this->objects as $object) {
            if ($object->getTrophy()->awardAutomatically) {
                throw new PermissionDeniedException();
            }
        }
    }

    /**
{'fraction_non_alphanumeric': 0.12271793011727723, 'fraction_numerical': 0.002659896022246403, 'mean_word_length': 2.142857142857143, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 77, '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': '3752114', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2234, 'n_words': 343}