-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActivityBar.php
237 lines (189 loc) · 6.78 KB
/
ActivityBar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types=1);
/**
* @package Activity Bar mod
* @version 2.0.1
* @author Michel Mendiola <suki@missallsunday.com>
* @copyright Copyright (c), Michel Mendiola
* @license http://www.mozilla.org/MPL/ MPL 2.0
*/
if (!defined('SMF'))
die('No direct access...');
class ActivityBar
{
protected const CACHE_TIME = 900;
protected const MAX_POSTS = 500;
protected const MAX_WIDTH = 139;
protected const DAYS = 30;
protected const DEFAULT_COLOR = 'blue';
public string $name = __CLASS__;
protected array $activity = [];
protected string $fieldLabel = '';
protected int $fieldPlacement = 0;
public function __construct()
{
$this->fieldPlacement = (int) $this->setting('placement', 0);
$this->fieldLabel = (string) $this->setting('label', $this->text('standardLabel'));
}
public function addGeneralSettings(array &$config_vars): void
{
global $txt;
loadLanguage($this->name);
$config_vars[] = $this->text('modName');
$config_vars[] = array('check', $this->name .'_master', 'subtext' => $this->text('master_sub'));
$config_vars[] = array('check', $this->name .'_show_in_posts', 'subtext' => $this->text('show_in_posts_sub'));
$config_vars[] = array('check', $this->name .'_show_in_profile', 'subtext' => $this->text('show_in_profile_sub'));
$config_vars[] = array('text', $this->name .'_label', 'subtext' => $this->text('label_sub'));
$config_vars[] = array('int', $this->name .'_timeframe', 'subtext' => $this->text('timeframe_sub'));
$config_vars[] = array('int', $this->name .'_max_posts', 'subtext' => $this->text('max_posts_sub'));
$config_vars[] = array('int', $this->name .'_max_width', 'subtext' => $this->text('max_width_sub'));
$config_vars[] = array('check', $this->name .'_colors', 'subtext' => $this->text('colors_sub'));
// Option to select the placement.
$config_vars[] = ['select', $this->name .'_placement',
[
$txt['custom_profile_placement_standard'],
$txt['custom_profile_placement_icons'],
$txt['custom_profile_placement_above_signature'],
$txt['custom_profile_placement_below_signature'],
$txt['custom_profile_placement_below_avatar'],
$txt['custom_profile_placement_above_member'],
$txt['custom_profile_placement_bottom_poster'],
$txt['custom_profile_placement_before_member'],
$txt['custom_profile_placement_after_member'],
],
'subtext' => $this->text('placement_sub'),
'multiple' => false,
];
$config_vars[] = '';
}
public function addMemberContext(array &$data, $user, $display_custom_fields)
{
$user = (int) $user;
// Mod is disabled, or we aren't loading any custom profile field, don't bother.
if(!$this->setting('master') || empty($display_custom_fields))
return;
loadTemplate($this->name);
// Get this user's activity.
$activity = $this->getActivity($user);
// Append the data.
$data['custom_fields'][] = array(
'title' => $activity['label'],
'col_name' => $activity['placement'],
'value' => template_activity_display($activity),
'placement' => $activity['placement'],
);
unset($activity);
}
public function addDisplayContext(&$output, &$message): void
{
global $context;
// Mod is disabled or the user does not want to show this field on users posts.
if(!$this->setting('master') ||
$this->setting('show_in_posts') ||
empty($output['custom_fields']))
return;
// This is going to be awkward... need to know the placement to properly unset our field...
$placement = $context['cust_profile_fields_placement'][$this->fieldPlacement];
foreach ($output['custom_fields'][$placement] as $key => $value)
if ($value['title'] === $this->fieldLabel)
unset($output['custom_fields'][$placement][$key]);
}
public function addProfile($memID, $area): void
{
global $context;
// Mod is disabled, or we aren't in summary page and show in profile is disabled
if(!$this->setting('master') ||
($area !== 'summary' && $this->setting('show_in_profile')))
return;
// Get this user's activity.
$activity = $this->getActivity((int) $memID);
loadTemplate($this->name);
$context['custom_fields'][] = array(
'name' => $this->fieldLabel,
'placement' => $this->fieldPlacement,
'output_html' => template_activity_display($activity),
'show_reg' => false,
);
}
public function create(int $user = 0): array
{
global $smcFunc;
// Meh...
if (empty($user))
return [];
if (($this->activity[$user] = cache_get_data($this->name .'_' . $user,
self::CACHE_TIME)) == null)
{
// Make sure everything is set. If something is missing, use a default value.
$maxWidth = (int) $this->setting('max_width', self::MAX_WIDTH);
$maxPosts = (int) $this->setting('max_posts', self::MAX_POSTS);
$days = (int) $this->setting('timeframe', self::DAYS);
// Calculate the starting date.
$startingDate = time() - ($days * 86400);
// Get all posts since the starting date.
$request = $smcFunc['db_query']('', '
SELECT poster_time, id_member
FROM {db_prefix}messages
WHERE poster_time > {int:startingdate} AND id_member = {int:user}',
array(
'startingdate' => $startingDate,
'user' => $user,
)
);
// Count the posts.
$posts = (int) $smcFunc['db_num_rows']($request);
$smcFunc['db_free_result']($request);
// Calculate everything.
$numPosts = min($posts / $maxPosts, 1);
$percentage = round(($numPosts * 100), 2);
$barWidth = $maxWidth * $numPosts;
// Store the result in an array.
$this->activity[$user] = [
'maxWidth' => $maxWidth,
'width' => $barWidth,
'percentage' => $percentage,
'post' => $numPosts,
'realPost' => $posts,
'color' => $this->setColor($percentage),
'label' => $this->fieldLabel,
'overallText' => $this->fieldPlacement === 0 ? '' : ($this->fieldLabel . ' ' . $percentage . '%'),
'title' => $this->fieldLabel . ' ' . $percentage . '%',
'placement' => $this->fieldPlacement
];
cache_put_data($this->name .'_' . $user, $this->activity[$user], self::CACHE_TIME);
}
// There you go. Anything else?
return $this->activity[$user];
}
public function setColor(float $percentage): string
{
$color = self::DEFAULT_COLOR;
// Which color should we use?
if ($this->setting('colors'))
{
if ($percentage <= 33)
$color = 'green';
elseif ($percentage >= 34 && $percentage <= 66)
$color = 'yellow';
else
$color = 'red';
}
return $color;
}
public function getActivity(int $user = 0): array
{
// Let create() to handle the checks.
$this->create($user);
return $user ? $this->activity[$user] : $this->activity;
}
protected function text(string $textLabel = ''): string
{
global $txt;
return $txt[$this->name . '_' . $textLabel] ?? '';
}
protected function setting(string $settingLabel, $defaultValue = false)
{
global $modSettings;
return $modSettings[$this->name . '_' . $settingLabel] ?? $defaultValue;
}
}