PluginProbe ʕ •ᴥ•ʔ
FAPI Member / trunk
FAPI Member vtrunk
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Repository / MemberActivityRepository.php
fapi-member / src / Repository Last commit date
EmailRepository.php 1 year ago LevelOrderRepository.php 1 year ago LevelRepository.php 1 year ago MemberActivityRepository.php 1 year ago MembershipChangeRepository.php 1 year ago MembershipHistoryRepository.php 1 year ago MembershipRepository.php 1 year ago PageRepository.php 1 year ago Repository.php 2 years ago SettingsRepository.php 1 year ago UserRepository.php 3 weeks ago
MemberActivityRepository.php
178 lines
1 <?php
2
3
4
5 namespace FapiMember\Repository;
6
7 use DateTimeImmutable;
8 use FapiMember\Model\Enums\Format;
9 use FapiMember\Model\MembershipChange;
10 use FapiMember\Utils\DateTimeHelper;
11 use wpdb;
12
13 class MemberActivityRepository extends Repository
14 {
15 private wpdb $wpdb;
16 private string $tableName;
17
18 public function __construct()
19 {
20 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
21 global $wpdb;
22
23 $this->wpdb = $wpdb;
24 $this->tableName = $this->wpdb->prefix . 'fm_member_activity';
25 }
26
27 public function tableExists(): bool
28 {
29 return $this->wpdb->get_var("SHOW TABLES LIKE '$this->tableName'") == $this->tableName;
30 }
31
32 public function createTableIfNeeded(): void
33 {
34 $charset_collate = $this->wpdb->get_charset_collate();
35
36 $sql = "
37 CREATE TABLE IF NOT EXISTS $this->tableName (
38 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
39 user_id BIGINT(20) UNSIGNED NOT NULL,
40 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
41 PRIMARY KEY (id),
42 INDEX idx_user_id (user_id),
43 INDEX idx_timestamp (timestamp)
44 );
45 ) $charset_collate;";
46
47 dbDelta($sql);
48 }
49
50 public function addActivity(int $userId): void
51 {
52 $this->wpdb->insert(
53 $this->tableName,
54 [
55 'user_id' => $userId,
56 'timestamp' => DateTimeHelper::getNow()->format(Format::DATE_TIME_BASIC),
57 ],
58 );
59 }
60
61 public function updateActivity(int $userId): void
62 {
63 $sql = $this->wpdb->prepare("
64 UPDATE $this->tableName
65 SET timestamp = %s
66 WHERE user_id = %d
67 AND DATE(timestamp) = %s",
68 DateTimeHelper::getNow()->format(Format::DATE_TIME_BASIC),
69 $userId,
70 DateTimeHelper::getNow()->format(Format::DATE)
71 );
72
73 $this->wpdb->query($sql);
74 }
75
76 /** @return array<MembershipChange>*/
77 public function findLastActivity(int $userId): DateTimeImmutable|null
78 {
79 $query = $this->wpdb->prepare("
80 SELECT * FROM $this->tableName
81 WHERE user_id = %d
82 ORDER BY timestamp DESC, id DESC
83 LIMIT 1;
84 ", $userId);
85
86 $result = $this->wpdb->get_row($query, ARRAY_A);
87
88 if ($result === null) {
89 return null;
90 }
91
92 return DateTimeHelper::createOrNull($result['timestamp'], Format::DATE_TIME_BASIC);
93 }
94
95 public function getOneForPeriod(int $userId, DateTimeImmutable $dateFrom, DateTimeImmutable $dateTo)
96 {
97 $query = $this->wpdb->prepare("
98 SELECT * FROM $this->tableName
99 WHERE user_id = %d
100 AND timestamp >= %s
101 AND timestamp <= %s
102 GROUP BY user_id
103 ;
104 ",
105 [
106 $userId,
107 $dateFrom->format(Format::DATE_TIME_BASIC),
108 $dateTo->format(Format::DATE_TIME_BASIC),
109 ],
110 );
111
112 $results = $this->wpdb->get_results($query, ARRAY_A);
113
114 return $results;
115 }
116
117 public function getAllForPeriod(DateTimeImmutable $dateFrom, DateTimeImmutable $dateTo)
118 {
119 $query = $this->wpdb->prepare("
120 SELECT * FROM $this->tableName
121 WHERE timestamp >= %s
122 AND timestamp <= %s
123 GROUP BY user_id
124 ;
125 ",
126 [
127 $dateFrom->format(Format::DATE_TIME_BASIC),
128 $dateTo->format(Format::DATE_TIME_BASIC),
129 ],
130 );
131
132 $results = $this->wpdb->get_results($query, ARRAY_A);
133
134 return $results;
135 }
136
137 /** @return array<MembershipChange>*/
138 private function toEntities(array|null $results, bool $allowMultiple = false): array
139 {
140 if ($results === null) {
141 return [];
142 }
143
144 $entities = [];
145
146 foreach ($results as $result) {
147 $change = new MembershipChange($result);
148
149 if ($allowMultiple) {
150 $entities[] = $change;
151 continue;
152 }
153 $entities[$change->getLevelId() . '-' . $change->getUserId()] = $change;
154 }
155
156 return $entities;
157 }
158
159 public function getAll()
160 {
161 $query = $this->wpdb->prepare("
162 SELECT * FROM $this->tableName;
163 ");
164
165 $result = $this->wpdb->get_results($query, ARRAY_A);
166
167 return $result;
168 }
169
170 public function dropTable()
171 {
172 $sql = "DROP TABLE IF EXISTS $this->tableName";
173
174 $this->wpdb->query($sql);
175 }
176
177 }
178