AntonLV 1 год назад
Родитель
Сommit
2d179965db

+ 24 - 2
modules/boonex/reputation/classes/BxReputationAlertsResponse.php

@@ -52,11 +52,33 @@ class BxReputationAlertsResponse extends BxBaseModNotificationsResponse
         $iOwnerId = $oAlert->iSender;
         $iObjectOwnerId = $this->_getObjectOwnerId($oAlert->aExtras);
 
-        if(($iPoints = (int)$aHandler['points_active']) != 0)
+        $aEvent = [
+            'owner_id' => 0,
+            'type' => $oAlert->sUnit,
+            'action' => $oAlert->sAction,
+            'object_id' => $oAlert->iObject,
+            'object_owner_id' => $iObjectOwnerId,
+            'points' => 0,
+            'date' => time()
+        ];
+
+        if(($iPoints = (int)$aHandler['points_active']) != 0) {
+            $this->_oModule->_oDb->insertEvent(array_merge($aEvent, [
+                'owner_id' => $iOwnerId, 
+                'points' => $iPoints
+            ]));
+
             $this->_oModule->_oDb->insertProfile($iOwnerId, $iPoints);
+        }
+
+        if(($iPoints = (int)$aHandler['points_passive']) != 0 && $iOwnerId != $iObjectOwnerId) {
+            $this->_oModule->_oDb->insertEvent(array_merge($aEvent, [
+                'owner_id' => $iObjectOwnerId, 
+                'points' => $iPoints
+            ]));
 
-        if(($iPoints = (int)$aHandler['points_passive']) != 0 && $iOwnerId != $iObjectOwnerId)
             $this->_oModule->_oDb->insertProfile($iObjectOwnerId, $iPoints);
+        }
     }
 
     protected function _processProfileDelete($oAlert)

+ 2 - 1
modules/boonex/reputation/classes/BxReputationConfig.php

@@ -19,13 +19,14 @@ class BxReputationConfig extends BxBaseModNotificationsConfig
 
         $this->CNF = [
             // database tables
+            'TABLE_EVENTS' => $aModule['db_prefix'] . 'events',
             'TABLE_PROFILES' => $aModule['db_prefix'] . 'profiles',
 
             // database fields
             'FIELD_' => '',
 
             // some params
-            'PARAM_' => 'bx_reputation_',
+            'PARAM_LEADERBOARD_LIMIT' => 'bx_reputation_leaderboard_limit',
 
             // objects
             'OBJECT_FORM_HANDLER' => 'bx_reputation_handler',

+ 56 - 11
modules/boonex/reputation/classes/BxReputationDb.php

@@ -20,11 +20,6 @@ class BxReputationDb extends BxBaseModNotificationsDb
         $this->_oConfig = $oConfig;
     }
 
-    public function insertEvent($aParamsSet)
-    {
-        return 0;
-    }
-
     public function updateEvent($aParamsSet, $aParamsWhere)
     {
         return false;
@@ -37,7 +32,41 @@ class BxReputationDb extends BxBaseModNotificationsDb
 
     public function getEvents($aParams)
     {
-        return [];
+        $CNF = &$this->_oConfig->CNF;
+
+        $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
+        $sSelectClause = '*';
+        $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = '';
+        
+        switch($aParams['sample']) {
+            case 'stats':
+                $aMethod['name'] = 'getPairs';
+                $aMethod['params'][1] = 'owner_id';
+                $aMethod['params'][2] = 'points';
+                $aMethod['params'][3] = [];
+                
+                $sSelectClause = '`owner_id`, SUM(`points`) AS `points`';
+
+                if(!empty($aParams['days']))
+                    $sWhereClause = $this->prepareAsString('AND `date` >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? DAY))', (int)$aParams['days']);
+                
+                $sGroupClause = '`owner_id`';
+                $sOrderClause = '`points` DESC';
+                $sLimitClause = '0, ' . (int)$aParams['limit'];
+                break;
+        }
+        
+        if($sGroupClause)
+            $sGroupClause = "GROUP BY " . $sGroupClause;
+
+        if($sOrderClause)
+            $sOrderClause = "ORDER BY " . $sOrderClause;
+
+        if($sLimitClause)
+            $sLimitClause = "LIMIT " . $sLimitClause;
+
+        $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_EVENTS'] . "` WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
+        return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
     }
 
     public function insertProfile($iId, $iPoints)
@@ -57,13 +86,13 @@ class BxReputationDb extends BxBaseModNotificationsDb
         return $this->query("DELETE FROM `" . $CNF['TABLE_PROFILES'] . "` WHERE `id`=:id", ['id' => $iId]);
     }
     
-    public function getProfile($aParams = [])
+    public function getProfiles($aParams = [])
     {
         $CNF = &$this->_oConfig->CNF;
 
         $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
         $sSelectClause = '*';
-        $sWhereClause = '';
+        $sWhereClause = $sOrderClause = $sLimitClause = '';
         
         switch($aParams['sample']) {
             case 'id':
@@ -74,10 +103,26 @@ class BxReputationDb extends BxBaseModNotificationsDb
 
                 $sWhereClause = "AND `id` = :id";
                 break;
+
+            case 'stats':
+                $aMethod['name'] = 'getPairs';
+                $aMethod['params'][1] = 'id';
+                $aMethod['params'][2] = 'points';
+                $aMethod['params'][3] = [];
+
+                $sOrderClause = '`points` DESC';
+                $sLimitClause = '0, ' . (int)$aParams['limit'];
+                break;
         }
-        
-        $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_PROFILES'] . "` WHERE 1 " . $sWhereClause;
-        return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
+
+        if($sOrderClause)
+            $sOrderClause = "ORDER BY " . $sOrderClause;
+
+        if($sLimitClause)
+            $sLimitClause = "LIMIT " . $sLimitClause;
+
+        $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_PROFILES'] . "` WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
+        return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
     }
     
     public function getHandlers($aParams = []) 

+ 5 - 0
modules/boonex/reputation/classes/BxReputationModule.php

@@ -30,6 +30,11 @@ class BxReputationModule extends BxBaseModNotificationsModule
 
         return $this->_oTemplate->getBlockSummary($iProfileId);
     }
+
+    public function serviceGetBlockLeaderboard($iDays = 0)
+    {
+        return $this->_oTemplate->getBlockLeaderboard($iDays);
+    }
 }
 
 /** @} */

+ 32 - 1
modules/boonex/reputation/classes/BxReputationTemplate.php

@@ -22,7 +22,7 @@ class BxReputationTemplate extends BxBaseModNotificationsTemplate
         if(!$oProfile)
             return false;
 
-        $aProfileInfo = $this->_oDb->getProfile(['sample' => 'id', 'id' => $iProfileId]);
+        $aProfileInfo = $this->_oDb->getProfiles(['sample' => 'id', 'id' => $iProfileId]);
         $bProfileInfo = !empty($aProfileInfo) && is_array($aProfileInfo);
 
         return $this->parseHtmlByName('block_summary.html', [
@@ -31,6 +31,37 @@ class BxReputationTemplate extends BxBaseModNotificationsTemplate
             'points' => $bProfileInfo ? $aProfileInfo['points'] : 0,
         ]);
     }
+
+    public function getBlockLeaderboard($iDays = 0, $iLimit = 0)
+    {
+        $CNF = &$this->_oConfig->CNF;
+        
+        if(!$iLimit)
+            $iLimit = (int)getParam($CNF['PARAM_LEADERBOARD_LIMIT']);
+
+        $bGrowth = $iDays > 0;
+
+        if($bGrowth) 
+            $aItems = $this->_oDb->getEvents(['sample' => 'stats', 'days' => $iDays, 'limit' => $iLimit]);
+        else
+            $aItems = $this->_oDb->getProfiles(['sample' => 'stats', 'limit' => $iLimit]);
+
+        $oProfile = BxDolProfile::getInstance();
+
+        $aTmplVarsProfiles = [];
+        foreach($aItems as $iProfileId => $iPoints)
+            if($iProfileId)
+                $aTmplVarsProfiles[] = [
+                    'unit' => $oProfile->getUnit($iProfileId),
+                    'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
+                    'points' => $bGrowth ? abs($iPoints) : $iPoints
+                ];
+
+        $this->addCss(['main.css']);
+        return $this->parseHtmlByName('block_leaderboard.html', [
+            'bx_repeat:profiles' => $aTmplVarsProfiles
+        ]);
+    }
 }
 
 /** @} */

+ 8 - 1
modules/boonex/reputation/install/langs/en.xml

@@ -2,7 +2,7 @@
 <resources name="en" flag="gb" title="English">
     <string name="_bx_reputation"><![CDATA[Reputation]]></string>
 
-    <string name="_bx_reputation_option_"><![CDATA[]]></string>
+    <string name="_bx_reputation_option_leaderboard_limit"><![CDATA[Number of items in leaderboard]]></string>
 
     <string name="_bx_reputation_form_handler"><![CDATA[Handler]]></string>
     <string name="_bx_reputation_form_handler_display_edit"><![CDATA[Edit Handler]]></string>
@@ -19,9 +19,16 @@
     <string name="_bx_reputation_form_handler_input_do_cancel"><![CDATA[Cancel]]></string>
     <string name="_bx_reputation_popup_title_handler_edit"><![CDATA[Edit Handler]]></string>
 
+    <string name="_bx_reputation_page_title_sys_leaderboard"><![CDATA[Leaderboard]]></string>
+    <string name="_bx_reputation_page_title_leaderboard"><![CDATA[Leaderboard]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_week"><![CDATA[Leaderboard (7-days)]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_month"><![CDATA[Leaderboard (30-days)]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_all_time"><![CDATA[Leaderboard (all-time)]]></string>
     <string name="_bx_reputation_page_block_title_system_summary"><![CDATA[Summary]]></string>
     <string name="_bx_reputation_page_block_title_summary"><![CDATA[Summary]]></string>
 
+    <string name="_bx_reputation_menu_item_title_system_leaderboard"><![CDATA[Leaderboard]]></string>
+    <string name="_bx_reputation_menu_item_title_leaderboard"><![CDATA[Leaderboard]]></string>
     <string name="_bx_reviews_menu_item_title_manage"><![CDATA[Manage]]></string>
 
     <string name="_bx_reputation_grid_column_title_active"><![CDATA[Active]]></string>

+ 7 - 1
modules/boonex/reputation/install/sql/disable.sql

@@ -7,7 +7,13 @@ DELETE FROM `tot`, `toc`, `to` USING `sys_options_types` AS `tot` LEFT JOIN `sys
 
 -- PAGES & BLOCKS
 DELETE FROM `sys_objects_page` WHERE `module` = @sName;
-DELETE FROM `sys_pages_blocks` WHERE `module` = @sName; -- OR `object` IN('bx_reputation_home');
+DELETE FROM `sys_pages_blocks` WHERE `module` = @sName OR `object` IN('bx_reputation_leaderboard');
+
+
+-- MENU
+DELETE FROM `sys_objects_menu` WHERE `module` = @sName;
+DELETE FROM `sys_menu_sets` WHERE `module` = @sName;
+DELETE FROM `sys_menu_items` WHERE `module` = @sName; -- OR `set_name` IN('bx_reputation_submenu');
 
 
 -- GRIDS

+ 25 - 9
modules/boonex/reputation/install/sql/enable.sql

@@ -2,17 +2,27 @@ SET @sName = 'bx_reputation';
 
 
 -- SETTINGS
--- SET @iTypeOrder = (SELECT MAX(`order`) FROM `sys_options_types` WHERE `group` = 'modules');
--- INSERT INTO `sys_options_types`(`group`, `name`, `caption`, `icon`, `order`) VALUES 
--- ('modules', @sName, '_bx_reputation', 'bx_reputation@modules/boonex/ewts/|std-icon.svg', IF(ISNULL(@iTypeOrder), 1, @iTypeOrder + 1));
--- SET @iTypeId = LAST_INSERT_ID();
+SET @iTypeOrder = (SELECT MAX(`order`) FROM `sys_options_types` WHERE `group` = 'modules');
+INSERT INTO `sys_options_types`(`group`, `name`, `caption`, `icon`, `order`) VALUES 
+('modules', @sName, '_bx_reputation', 'bx_reputation@modules/boonex/ewts/|std-icon.svg', IF(ISNULL(@iTypeOrder), 1, @iTypeOrder + 1));
+SET @iTypeId = LAST_INSERT_ID();
 
--- INSERT INTO `sys_options_categories` (`type_id`, `name`, `caption`, `order`)
--- VALUES (@iTypeId, @sName, '_bx_reputation', 10);
--- SET @iCategId = LAST_INSERT_ID();
+INSERT INTO `sys_options_categories` (`type_id`, `name`, `caption`, `order`)
+VALUES (@iTypeId, @sName, '_bx_reputation', 10);
+SET @iCategId = LAST_INSERT_ID();
 
--- INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
--- ('bx_reputation_badge_persons', '', @iCategId, '_bx_reputation_option_badge_persons', 'select', 'a:3:{s:6:"module";s:13:"bx_reputation";s:6:"method";s:17:"get_options_badge";s:6:"params";a:1:{i:0;s:13:"bx_reputation";}}', '', '', 10);
+INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
+('bx_reputation_leaderboard_limit', '10', @iCategId, '_bx_reputation_option_leaderboard_limit', 'digit', '', '', '', 1);
+
+
+-- PAGE: leaderboard
+INSERT INTO `sys_objects_page`(`object`, `title_system`, `title`, `module`, `layout_id`, `visible_for_levels`, `visible_for_levels_editable`, `uri`, `url`, `meta_description`, `meta_keywords`, `meta_robots`, `cache_lifetime`, `cache_editable`, `deletable`, `override_class_name`, `override_class_file`) VALUES 
+('bx_reputation_leaderboard', '_bx_reputation_page_title_sys_leaderboard', '_bx_reputation_page_title_leaderboard', @sName, 8, 2147483647, 1, 'leaderboard', 'page.php?i=leaderboard', '', '', '', 0, 1, 0, '', '');
+
+INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`) VALUES
+('bx_reputation_leaderboard', 2, @sName, '_bx_reputation_page_block_title_leaderboard_week', 11, 2147483647, 'service', 'a:3:{s:6:"module";s:13:"bx_reputation";s:6:"method";s:21:"get_block_leaderboard";s:6:"params";a:1:{i:0;i:7;}}', 0, 1, 1),
+('bx_reputation_leaderboard', 3, @sName, '_bx_reputation_page_block_title_leaderboard_month', 11, 2147483647, 'service', 'a:3:{s:6:"module";s:13:"bx_reputation";s:6:"method";s:21:"get_block_leaderboard";s:6:"params";a:1:{i:0;i:30;}}', 0, 1, 1),
+('bx_reputation_leaderboard', 4, @sName, '_bx_reputation_page_block_title_leaderboard_all_time', 11, 2147483647, 'service', 'a:2:{s:6:"module";s:13:"bx_reputation";s:6:"method";s:21:"get_block_leaderboard";}', 0, 1, 1);
 
 
 -- PAGES: add page block on home
@@ -27,6 +37,12 @@ INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title_system`, `
 ('trigger_page_profile_view_entry', @iPBCellProfile, @sName, '_bx_reputation_page_block_title_system_summary', '_bx_reputation_page_block_title_summary', 13, 2147483647, 'service', 'a:3:{s:6:"module";s:13:"bx_reputation";s:6:"method";s:17:"get_block_summary";s:6:"params";a:1:{i:0;s:12:"{profile_id}";}}', 0, 0, 1, 0);
 
 
+-- MENU: add to site menu
+SET @iSiteMenuOrder = (SELECT `order` FROM `sys_menu_items` WHERE `set_name` = 'sys_site' AND `active` = 1 AND `order` < 9999 ORDER BY `order` DESC LIMIT 1);
+INSERT INTO `sys_menu_items` (`set_name`, `module`, `name`, `title_system`, `title`, `link`, `onclick`, `target`, `icon`, `submenu_object`, `visible_for_levels`, `active`, `copyable`, `order`) VALUES 
+('sys_site', @sName, 'leaderboard', '_bx_reputation_menu_item_title_system_leaderboard', '_bx_reputation_menu_item_title_leaderboard', 'page.php?i=leaderboard', '', '', 'star col-red3', '', 2147483647, 1, 1, IFNULL(@iSiteMenuOrder, 0) + 1);
+
+
 -- GRIDS: administration tools
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `override_class_name`, `override_class_file`) VALUES
 ('bx_reputation_manage', 'Sql', 'SELECT * FROM `bx_reputation_handlers` WHERE 1 ', 'bx_reputation_handlers', 'id', '', 'active', '', 20, NULL, 'start', '', 'type,alert_unit,alert_action', '', 'like', 'reports', '', 192, 'BxReputationGridManage', 'modules/boonex/reputation/classes/BxReputationGridManage.php');

+ 15 - 0
modules/boonex/reputation/install/sql/install.sql

@@ -15,6 +15,21 @@ CREATE TABLE IF NOT EXISTS `bx_reputation_handlers` (
   UNIQUE `alert` (`alert_unit`, `alert_action`)
 );
 
+-- TABLES: events
+CREATE TABLE IF NOT EXISTS `bx_reputation_events` (
+  `id` int(11) NOT NULL auto_increment,
+  `owner_id` int(11) NOT NULL default '0',
+  `type` varchar(64) NOT NULL default '',
+  `action` varchar(64) NOT NULL default '',
+  `object_id` int(11) NOT NULL default '0',
+  `object_owner_id` int(11) NOT NULL default '0',
+  `points` int(11) NOT NULL DEFAULT '0',
+  `date` int(11) NOT NULL default '0',
+  PRIMARY KEY (`id`),
+  KEY `owner_id` (`owner_id`),
+  KEY `object_id` (`object_id`)
+);
+
 -- TABLE: profiles
 CREATE TABLE IF NOT EXISTS `bx_reputation_profiles` (
   `id` int(11) NOT NULL DEFAULT '0',

+ 1 - 1
modules/boonex/reputation/install/sql/uninstall.sql

@@ -1,7 +1,7 @@
 SET @sName = 'bx_reputation';
 
 
-DROP TABLE IF EXISTS `bx_reputation_handlers`, `bx_reputation_profiles`;
+DROP TABLE IF EXISTS `bx_reputation_handlers`, `bx_reputation_events`, `bx_reputation_profiles`;
 
 
 -- FORMS

+ 8 - 0
modules/boonex/reputation/template/block_leaderboard.html

@@ -0,0 +1,8 @@
+<div 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">
+            <div class="bx-reputation-lp-unit flex-1">__unit__</div>
+            <div class="bx-reputation-lp-points text-xl font-semibold">__sign____points__</div>
+        </div>
+    </bx_repeat:profiles>
+</div>

+ 5 - 0
modules/boonex/reputation/template/css/main.css

@@ -0,0 +1,5 @@
+/*--- Leaderboard ---*/
+.bx-reputation-leaderboard .bx-base-pofile-unit {
+    width: auto;
+    margin: 0;
+}

+ 8 - 1
modules/boonex/russian/data/langs/bx_reputation/ru.xml

@@ -2,7 +2,7 @@
 <resources name="ru" flag="ru" title="Russian">
     <string name="_bx_reputation"><![CDATA[Репутация]]></string>
 
-    <string name="_bx_reputation_option_"><![CDATA[]]></string>
+    <string name="_bx_reputation_option_leaderboard_limit"><![CDATA[Количество элементов в таблице лидеров]]></string>
 
     <string name="_bx_reputation_form_handler"><![CDATA[Обработчик]]></string>
     <string name="_bx_reputation_form_handler_display_edit"><![CDATA[Редактировать обработчик]]></string>
@@ -19,9 +19,16 @@
     <string name="_bx_reputation_form_handler_input_do_cancel"><![CDATA[Отмена]]></string>
     <string name="_bx_reputation_popup_title_handler_edit"><![CDATA[Редактировать обработчик]]></string>
 
+    <string name="_bx_reputation_page_title_sys_leaderboard"><![CDATA[Таблица лидеров]]></string>
+    <string name="_bx_reputation_page_title_leaderboard"><![CDATA[Таблица лидеров]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_week"><![CDATA[Таблица лидеров (7 дней)]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_month"><![CDATA[Таблица лидеров (30 дней)]]></string>
+    <string name="_bx_reputation_page_block_title_leaderboard_all_time"><![CDATA[Таблица лидеров (за все время)]]></string>
     <string name="_bx_reputation_page_block_title_system_summary"><![CDATA[Сводка]]></string>
     <string name="_bx_reputation_page_block_title_summary"><![CDATA[Сводка]]></string>
 
+    <string name="_bx_reputation_menu_item_title_system_leaderboard"><![CDATA[Таблица лидеров]]></string>
+    <string name="_bx_reputation_menu_item_title_leaderboard"><![CDATA[Таблица лидеров]]></string>
     <string name="_bx_reviews_menu_item_title_manage"><![CDATA[Управление]]></string>
 
     <string name="_bx_reputation_grid_column_title_active"><![CDATA[Активные]]></string>