Automatically set rewarded trophies as special trophy
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 2 Aug 2018 18:33:45 +0000 (20:33 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 2 Aug 2018 18:33:56 +0000 (20:33 +0200)
If there is enough space for an additional special trophy and the user has not earned the trophy yet (Heads Up: You can get the same trophy more than once!), the trophy will be automatically set as special.
See #2630

wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php

index 9bce8a8b809a63b628b867c1fe5a2327f1d71591..ad87909eec55b909a9980b1fea22bdba99e78805 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\data\user\trophy;
 use wcf\data\user\UserAction;
 use wcf\data\AbstractDatabaseObjectAction;
+use wcf\data\user\UserProfileAction;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\IllegalLinkException;
@@ -49,6 +50,26 @@ class UserTrophyAction extends AbstractDatabaseObjectAction {
                                ]
                        ]); 
                        $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([WCF::getUser()->userID])[WCF::getUser()->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_merge(array_map(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);