-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-monthly.php
More file actions
355 lines (307 loc) · 8.95 KB
/
class-monthly.php
File metadata and controls
355 lines (307 loc) · 8.95 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Badge object.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Badges;
/**
* Badge class.
*/
final class Monthly extends Badge {
/**
* The target points.
*
* @var int
*/
const TARGET_POINTS = 10;
/**
* The badge ID.
*
* @var string
*/
protected $id;
/**
* An array of instances for this object (one/month).
*
* @var array
*/
protected static $instances = [];
/**
* Contructor.
*
* @param string $id The badge ID.
*/
public function __construct( $id ) {
$this->id = $id;
}
/**
* Get an array of instances (one for each month).
*
* @return array
*/
public static function init_badges() {
if ( ! empty( self::$instances ) ) {
return self::$instances;
}
$activation_date = \progress_planner()->get_activation_date();
if ( $activation_date < new \DateTime( 'first day of November 2024' ) ) { // When badges were introduced.
$start_date = $activation_date->modify( 'first day of November 2024' );
} else {
$start_date = $activation_date->modify( 'first day of this month' );
}
// Year when plugin was released.
$end_date = ( 2024 === (int) $start_date->format( 'Y' ) && 2024 === (int) \gmdate( 'Y' ) )
? new \DateTime( 'last day of December next year' )
: new \DateTime( 'last day of December this year' );
$dates = \iterator_to_array( new \DatePeriod( $start_date, new \DateInterval( 'P1M' ), $end_date ), false );
// To make sure keys are defined only once and consistent.
$self_months = \array_keys( self::get_months() );
foreach ( $dates as $date ) {
$year = (int) $date->format( 'Y' );
$month = (int) $date->format( 'n' );
$id = 'monthly-' . $year . '-' . $self_months[ $month - 1 ];
if ( ! isset( self::$instances[ $year ] ) ) {
self::$instances[ $year ] = [];
}
self::$instances[ $year ][] = new self( $id );
}
return self::$instances;
}
/**
* Get an array of instances (one for each month).
*
* @return array
*/
public static function get_instances() {
if ( empty( self::$instances ) ) {
self::$instances = self::init_badges();
}
return self::$instances;
}
/**
* Get an array of instances (one for each month).
*
* @param int $year The year.
*
* @return array
*/
public static function get_instances_for_year( $year ) {
if ( empty( self::$instances ) ) {
self::$instances = self::init_badges();
}
return isset( self::$instances[ $year ] ) ? self::$instances[ $year ] : [];
}
/**
* Get the badge ID from a date.
*
* @param \DateTime $date The date.
*
* @return string
*/
public static function get_badge_id_from_date( $date ) {
return 'monthly-' . $date->format( 'Y' ) . '-m' . $date->format( 'n' );
}
/**
* Get the badge object from a badge ID.
*
* @param string $badge_id The badge ID.
*
* @return \Progress_Planner\Badges\Monthly|null
*/
public static function get_instance_from_id( $badge_id ) {
$year = (int) \explode( '-', \str_replace( 'monthly-', '', $badge_id ) )[0];
$month = (int) \str_replace( 'm', '', \explode( '-', \str_replace( 'monthly-', '', $badge_id ) )[1] );
$instances = self::get_instances();
if ( isset( $instances[ $year ] ) ) {
foreach ( $instances[ $year ] as $instance ) {
if ( (int) $instance->get_month() === $month ) {
return $instance;
}
}
}
return null;
}
/**
* Get an array of months.
*
* Year-specific names are supported. If no names are defined for the
* given year, the default (2025) names are returned as a fallback.
*
* @param int|null $year The year. Null returns the default names.
*
* @return array
*/
public static function get_months( $year = null ) {
/*
* Indexed months, The array keys are prefixed with an "m"
* so that they are strings and not integers.
*/
$default = [
'm1' => 'Jack January',
'm2' => 'Felix February',
'm3' => 'Mary March',
'm4' => 'Avery April',
'm5' => 'Matteo May',
'm6' => 'Jasmine June',
'm7' => 'Joey July',
'm8' => 'Abed August',
'm9' => 'Sam September',
'm10' => 'Oksana October',
'm11' => 'Noah November',
'm12' => 'Daisy December',
];
$yearly = [
2026 => [
'm1' => 'Jamie January',
'm2' => 'Freya February',
'm3' => 'Milo March',
'm4' => 'Aida April',
'm5' => 'Maeve May',
'm6' => 'Jason June',
'm7' => 'Julia July',
'm8' => 'Ava August',
'm9' => 'Sophie September',
'm10' => 'Omar October',
'm11' => 'Nathan November',
'm12' => 'Daniel December',
],
];
if ( null !== $year && isset( $yearly[ $year ] ) ) {
return $yearly[ $year ];
}
return $default;
}
/**
* The badge name.
*
* @return string
*/
public function get_name() {
return $this->id
? self::get_months( (int) $this->get_year() )[ 'm' . $this->get_month() ]
: '';
}
/**
* Get the badge description.
*
* @return string
*/
public function get_description() {
return '';
}
/**
* Get the year for the month.
*
* @return string
*/
public function get_year() {
return \explode( '-', \str_replace( 'monthly-', '', $this->id ) )[0];
}
/**
* Get the month for the badge.
*
* @return string
*/
public function get_month() {
return \str_replace( 'm', '', \explode( '-', \str_replace( 'monthly-', '', $this->id ) )[1] );
}
/**
* Progress callback.
*
* @param array $args The arguments for the progress callback.
*
* @return array
*/
public function progress_callback( $args = [] ) {
$saved_progress = $this->get_saved();
// If we have a saved value, return it.
if ( isset( $saved_progress['progress'] )
&& isset( $saved_progress['remaining'] )
&& isset( $saved_progress['points'] )
&& 100 === $saved_progress['progress']
) {
return $saved_progress;
}
$year = $this->get_year();
$month = self::get_months( (int) $year )[ 'm' . $this->get_month() ];
$month_num = (int) $this->get_month();
$start_date = \DateTime::createFromFormat( 'Y-m-d', "{$year}-{$month_num}-01" );
$end_date = \DateTime::createFromFormat( 'Y-m-d', "{$year}-{$month_num}-" . \gmdate( 't', \strtotime( $month ) ) );
// Get the activities for the month.
$activities = \progress_planner()->get_activities__query()->query_activities(
[
'category' => 'suggested_task',
'start_date' => $start_date,
'end_date' => $end_date,
],
);
$points = 0;
foreach ( $activities as $activity ) {
$points += $activity->get_points( $activity->date );
}
$return_progress = [
'progress' => (int) \max( 0, \min( 100, \floor( 100 * $points / self::TARGET_POINTS ) ) ),
'remaining' => (int) \max( 0, \min( self::TARGET_POINTS - $points, 10 ) ),
'points' => $points,
];
$this->save_progress( $return_progress );
if ( $points >= self::TARGET_POINTS || ( isset( $args['no_next_badge_points'] ) && $args['no_next_badge_points'] ) ) {
return $return_progress;
}
$points += $this->get_next_badges_excess_points();
return [
'progress' => (int) \max( 0, \min( 100, \floor( 100 * $points / self::TARGET_POINTS ) ) ),
'remaining' => (int) \max( 0, \min( self::TARGET_POINTS - $points, 10 ) ),
'points' => $points,
];
}
/**
* Get the next badge-ID.
*
* @return string
*/
public function get_next_badge_id() {
$year = $this->get_year();
$month = $this->get_month();
$month = $month < 10 ? "0$month" : $month;
$datetime = \DateTime::createFromFormat( 'Y-m-d', "{$year}-{$month}-10" );
if ( ! $datetime ) {
return '';
}
return self::get_badge_id_from_date( $datetime->modify( 'first day of next month' ) );
}
/**
* Get the next badge that has an excess of points, going forward up to 2 months.
*
* @return int
*/
public function get_next_badges_excess_points() {
$next_1_badge_points = 0;
$next_2_badge_points = 0;
$badge_1_excess_points = 0;
$badge_2_excess_points = 0;
// Get the next badge object.
$next_1_badge = self::get_instance_from_id( $this->get_next_badge_id() );
if ( $next_1_badge ) {
$next_1_badge_points = $next_1_badge->progress_callback( [ 'no_next_badge_points' => true ] )['points'];
$next_2_badge = self::get_instance_from_id( $next_1_badge->get_next_badge_id() );
if ( $next_2_badge ) {
$next_2_badge_points = $next_2_badge->progress_callback( [ 'no_next_badge_points' => true ] )['points'];
}
}
// If the $next_1_badge has more than 10 points, calculate the excess points.
if ( $next_1_badge_points > self::TARGET_POINTS ) {
$badge_1_excess_points = \max( 0, $next_1_badge_points - self::TARGET_POINTS );
}
// If the $next_2_badge has more than 10 points, calculate the excess points.
if ( $next_2_badge_points > self::TARGET_POINTS ) {
$badge_2_excess_points = \max( 0, $next_2_badge_points - self::TARGET_POINTS );
// Does the $next_1_badge need more points to reach 10?
if ( $next_1_badge_points < self::TARGET_POINTS ) {
$badge_2_excess_points = \max( 0, ( $next_1_badge_points + $badge_2_excess_points ) - self::TARGET_POINTS );
}
}
return (int) $badge_1_excess_points + (int) $badge_2_excess_points;
}
}