<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>
--- /dev/null
+<?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);
+ }
+}
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;
/**
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;
+ }
}