Add automatically assign trophies cronjob
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 27 Jul 2017 14:35:41 +0000 (16:35 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 27 Jul 2017 14:35:59 +0000 (16:35 +0200)
See #2315

com.woltlab.wcf/cronjob.xml
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Trophy/List.js
wcfsetup/install/files/lib/data/trophy/Trophy.class.php
wcfsetup/install/files/lib/system/cronjob/AssignTrophiesCronjob.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/trophy/condition/TrophyConditionHandler.class.php

index 7480fa58d6e1a5f859d2c779bfcb47ae83fdb5e0..ed91399e9d36bb2530a0844c4a0e64fe5da2de2e 100644 (file)
                        <canbeedited>1</canbeedited>
                        <canbedisabled>1</canbedisabled>
                </cronjob>
+               
+               <cronjob name="com.woltlab.wcf.assignTrophies">
+                       <classname>wcf\system\cronjob\AssignTrophiesCronjob</classname>
+                       <description>Assign trophies based on rules</description>
+                       <description language="de">Ordnet Trophäen aufgrund der Regeln zu</description>
+                       <startminute>*/15</startminute>
+                       <starthour>*</starthour>
+                       <startdom>*</startdom>
+                       <startmonth>*</startmonth>
+                       <startdow>*</startdow>
+                       <canbeedited>1</canbeedited>
+                       <canbedisabled>1</canbedisabled>
+               </cronjob>
        </import>
        
        <delete>
index c521c7a9d2c4834a7dee41f92763d7af7533050c..0e5a1abc0ecff772bc35be6275d99eb0c4b2ed4b 100644 (file)
@@ -16,8 +16,6 @@ define(['Ajax', 'Core', 'Dictionary', 'Dom/Util', 'Ui/Dialog', 'WoltLabSuite/Cor
        UiUserTrophyList.prototype = {
                /**
                 * Initializes the user trophy list.
-                *
-                * @param       {object}        options         list of initialization options
                 */
                init: function() {
                        this._cache = new Dictionary();
index 771aadf39d8ec465b450a8e8038ed2c840cae531..27a6281f79d86e6f149287f06e8f66b99e58a85e 100644 (file)
@@ -23,7 +23,7 @@ use wcf\util\StringUtil;
  *
  * @property-read      integer         $trophyID                       unique id for the trophy
  * @property-read      string          $title                          the trophy title
- * @property-read      integer         $description                    the trophy description
+ * @property-read      string          $description                    the trophy description
  * @property-read      integer         $categoryID                     the categoryID of the trophy
  * @property-read      integer         $type                           the trophy type
  * @property-read      string          $iconFile                       the file location of the icon
diff --git a/wcfsetup/install/files/lib/system/cronjob/AssignTrophiesCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/AssignTrophiesCronjob.class.php
new file mode 100644 (file)
index 0000000..db5391a
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+namespace wcf\system\cronjob;
+use wcf\data\cronjob\Cronjob;
+use wcf\system\trophy\condition\TrophyConditionHandler;
+
+/**
+ * Assigns automatically trophies.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2017 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Cronjob
+ * @since      3.1
+ */
+class AssignTrophiesCronjob extends AbstractCronjob {
+       /**
+        * @inheritDoc
+        */
+       public function execute(Cronjob $cronjob) {
+               parent::execute($cronjob);
+               
+               TrophyConditionHandler::getInstance()->assignTrophies(50);
+       }
+}
index b9d65916cb0c4a5e3432543f139e7db96105e3c5..df9e940cacd55ea88d75419da7de4ea3cec21ba4 100644 (file)
@@ -2,6 +2,11 @@
 namespace wcf\system\trophy\condition;
 use wcf\data\object\type\ObjectType;
 use wcf\data\object\type\ObjectTypeCache;
+use wcf\data\trophy\Trophy;
+use wcf\data\trophy\TrophyList;
+use wcf\data\user\trophy\UserTrophyAction;
+use wcf\data\user\User;
+use wcf\data\user\UserList;
 use wcf\system\SingletonFactory;
 
 /**
@@ -51,4 +56,78 @@ class TrophyConditionHandler extends SingletonFactory {
        public function getGroupedObjectTypes() {
                return $this->groupedObjectTypes;
        }
+       
+       /**
+        * Assign trophies based on rules. 
+        * 
+        * @param       integer         $maxAssigns
+        */
+       public function assignTrophies($maxAssigns = 500) {
+               $trophyList = new TrophyList();
+               $trophyList->getConditionBuilder()->add('awardAutomatically = ?', [1]);
+               $trophyList->getConditionBuilder()->add('isDisabled = ?', [0]);
+               $trophyList->readObjects();
+               
+               $i = 0;
+               foreach ($trophyList as $trophy) {
+                       $users = $this->getUsers($trophy);
+                       
+                       foreach ($users as $user) {
+                               (new UserTrophyAction([], 'create', [
+                                       'data' => [
+                                               'trophyID' => $trophy->trophyID,
+                                               'userID' => $user->userID,
+                                               'time' => TIME_NOW
+                                       ]
+                               ]))->executeAction();
+                               
+                               if (++$i >= $maxAssigns) return;
+                       }
+               }
+       }
+       
+       /**
+        * Returns the users who fulfill all conditions of the given user group
+        * assignment.
+        *
+        * @param       Trophy          $trophy
+        * @return      User[]
+        */
+       private function getUsers(Trophy $trophy) {
+               $userList = new UserList();
+               
+               $conditions = $trophy->getConditions();
+               foreach ($conditions as $condition) {
+                       $condition->getObjectType()->getProcessor()->addUserCondition($condition, $userList);
+               }
+               $userList->readObjects();
+               
+               return $userList->getObjects();
+       }
+       
+       /**
+        * Returns the outstanding trophy assignment count. 
+        * 
+        * @return      integer
+        */
+       public function getOutstandingTrophyAssignmentCount() {
+               $trophyList = new TrophyList();
+               $trophyList->getConditionBuilder()->add('awardAutomatically = ?', [1]);
+               $trophyList->getConditionBuilder()->add('isDisabled = ?', [0]);
+               $trophyList->readObjects();
+               
+               $outstandingCount = 0;
+               foreach ($trophyList as $trophy) {
+                       $userList = new UserList();
+                       
+                       $conditions = $trophy->getConditions();
+                       foreach ($conditions as $condition) {
+                               $condition->getObjectType()->getProcessor()->addUserCondition($condition, $userList);
+                       }
+                       
+                       $outstandingCount += $userList->countObjects();
+               }
+               
+               return $outstandingCount; 
+       }
 }