Sfoglia il codice sorgente

Ticket #4844 - Reputation module. Positions in Growth lists.

AntonLV 1 anno fa
parent
commit
dfa210f401

+ 37 - 0
modules/boonex/reputation/classes/BxReputationDb.php

@@ -102,6 +102,43 @@ class BxReputationDb extends BxBaseModNotificationsDb
         return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
     }
 
+    public function getEventsStats($aParams)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $aBindings = [];
+
+        $sWhereSubClause = '';
+        if(isset($aParams['context_id']) && $aParams['context_id'] !== false) {
+            $aBindings['context_id'] = $aParams['context_id'];
+
+            $sWhereSubClause .= ' AND `context_id` = :context_id';
+        }
+
+        if(!empty($aParams['days'])) {
+            $aBindings['days'] = (int)$aParams['days'];
+
+            $sWhereSubClause .= ' AND `date` >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :days DAY))';
+        }
+
+        $sGroupSubClause = 'GROUP BY `owner_id`';
+
+        $sTableClause = '(SELECT `owner_id` AS `profile_id`, SUM(`points`) AS `points` FROM `' . $CNF['TABLE_EVENTS'] . '` WHERE 1 ' . $sWhereSubClause . ' ' . $sGroupSubClause . ')';
+
+        $sSelectPosition = 'SELECT COUNT(DISTINCT `tre2`.`points`) FROM ' . $sTableClause . ' AS `tre2` WHERE `tre2`.`points` >= `tre`.`points`';
+
+        $sWhereClause = '';
+        if(isset($aParams['username']) && $aParams['username'] !== false) {
+            $aIds = $this->_getProfileIdByTerm($aParams['username']);
+            $sWhereClause .= ' AND `tre`.`profile_id` IN (' . $this->implode_escape($aIds) . ')';
+        }
+
+        $sOrderClause = 'ORDER BY `tre`.`points` DESC';
+        $sLimitClause = 'LIMIT 0, ' . (int)$aParams['limit'];
+
+        return $this->getAllWithKey('SELECT *, (' . $sSelectPosition . ') AS `position` FROM ' . $sTableClause . ' AS `tre` WHERE 1 ' . $sWhereClause . ' ' . $sOrderClause . ' ' . $sLimitClause, 'profile_id', $aBindings);
+    }
+
     public function getLevels($aParams = []) 
     {
         $CNF = &$this->_oConfig->CNF;

+ 5 - 12
modules/boonex/reputation/classes/BxReputationTemplate.php

@@ -254,9 +254,8 @@ class BxReputationTemplate extends BxBaseModNotificationsTemplate
         $bFilters = $aParams['filters'] ?? false;
 
         if($bGrowth) 
-            $aItems = $this->_oDb->getEvents([
-                'sample' => 'stats', 
-                'context_id' => $iContextId ?: false,
+            $aItems = $this->_oDb->getEventsStats([
+                'context_id' => $iContextId,
                 'days' => $iDays,
                 'username' => $sUsername,
                 'limit' => $iLimit
@@ -272,18 +271,12 @@ class BxReputationTemplate extends BxBaseModNotificationsTemplate
         $aTmplVarsProfiles = [];
         foreach($aItems as $iProfileId => $aItem)
             if(($iPoints = (int)$aItem['points']) != 0 && ($iProfileId = abs($iProfileId)) && ($oProfile = BxDolProfile::getInstance($iProfileId)) !== false)
-                $aTmplVarsProfiles[] = array_merge($this->_bIsApi ? ['position' => $aItem['position'] ?? 0] : [
-                    'bx_if:show_position' => [
-                        'condition' => !$bGrowth,
-                        'content' => [
-                            'position' => $aItem['position'] ?? 0,
-                        ]
-                    ]
-                ], [
+                $aTmplVarsProfiles[] = [
+                    'position' => $aItem['position'] ?? 0,
                     'unit' => !$this->_bIsApi ? $oProfile->getUnit($iProfileId) : BxDolProfile::getData($oProfile),
                     'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
                     'points' => $bGrowth ? abs($iPoints) : $iPoints
-                ]);
+                ];
 
         if($this->_bIsApi)
             return array_merge([

+ 1 - 3
modules/boonex/reputation/template/block_leaderboard.html

@@ -1,9 +1,7 @@
 <div id="__html_id__" class="bx-reputation-leaderboard flex flex-col items-center">
     <bx_repeat:profiles>
         <div class="bx-reputation-l-profile flex flex-row justify-between items-center w-full mb-4">
-            <bx_if:show_position>
-                <div class="bx-reputation-lp-position flex-0 pr-4 text-xl font-semibold">__position__</div>
-            </bx_if:show_position>
+            <div class="bx-reputation-lp-position flex-0 pr-4 text-xl font-semibold">__position__</div>
             <div class="bx-reputation-lp-unit flex-1">__unit__</div>
             <div class="bx-reputation-lp-points text-xl font-semibold">__sign____points__</div>
         </div>