-
Notifications
You must be signed in to change notification settings - Fork 22
/
class.gengamificationdao.php
223 lines (187 loc) · 6.86 KB
/
class.gengamificationdao.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
<?php
/*
*
* CHANGES:
*
* 2014-10-21:
*
* Fixed logUserEvent() eventId error.
*
*/
// Data Access Object for gengamification persistent data layer
class gengamificationDAO implements gengamificationDAOint {
public $conn = null;
/**
*
* PDO methods
*
*/
public function getConnection() {
if (!$this->conn) {
try {
$this->conn = new PDO('mysql:host='.BD_SERVER.';dbname='.BD_NAME.';charset=utf8', BD_USER, BD_PASSWD);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
exit($e->getMessage());
}
}
return $this->conn;
}
public function execute($sql, $params = array()) {
$conn = $this->getConnection();
/** @var PDOStatement $stmt */
$stmt = $conn->prepare($sql);
$stmt->execute($params);
return $stmt;
}
public function query($sql, $params = array()) {
$conn = $this->getConnection();
if (empty($params)) {
/** @var $stmt PDOStatement */
$stmt = $conn->query($sql);
} else $stmt = $this->execute($sql, $params);
return $stmt;
}
public function toArray($query, $params = false)
{
$a = array();
$q = $this->query($query, $params);
while ($r = $q->fetch(PDO::FETCH_ASSOC)) $a[] = $r;
return $a;
}
/**
*
* Interface methods
*
*/
public function getUserAlerts($userId, $resetAlerts = false) {
$sql = 'SELECT id_user, id_badge, id_level FROM t_gengamification_alerts WHERE id_user = :uid';
$params = array(
':uid' => $userId
);
$r = $this->toArray($sql, $params);
if (!empty($r) && $resetAlerts) {
$sql = 'DELETE FROM t_gengamification_alerts WHERE id_user = :uid';
$params = array(
':uid' => $userId
);
$this->execute($sql, $params);
}
return $r;
}
public function getUserBadges($userId) {
$sql = 'SELECT id_badge, badgescounter FROM t_gengamification_badges WHERE id_user = :uid';
$params = array(
':uid' => $userId
);
return $this->toArray($sql, $params);
}
public function getUserEvents($userId) {
$sql = 'SELECT id_user, id_event, eventcounter FROM t_gengamification_events WHERE id_user = :uid';
$params = array(
':uid' => $userId
);
return $this->toArray($sql, $params);
}
public function getUserLog($userId) {
$sql = 'SELECT id_user, id_event, eventdate, points, id_badge, id_level FROM t_gengamification_log WHERE id_user = :uid ORDER BY eventdate DESC';
$params = array(
':uid' => $userId
);
return $this->toArray($sql, $params);
}
public function getUserEvent($userId, $eventId) {
$sql = 'SELECT id_user, id_event, eventcounter, pointscounter FROM t_gengamification_events WHERE id_user = :uid AND id_event = :eid LIMIT 1';
$params = array(
':uid' => $userId,
':eid' => $eventId
);
$r = $this->toArray($sql, $params);
return $r[0];
}
public function getUserScores($userId) {
$sql = 'SELECT id_user, points, id_level FROM t_gengamification_scores WHERE id_user = :uid LIMIT 1';
$params = array(
':uid' => $userId
);
$r = $this->toArray($sql, $params);
return $r[0];
}
public function grantBadgeToUser($userId, $badgeId) {
$sql = 'INSERT INTO t_gengamification_badges (id_user, id_badge, badgescounter, grantdate) VALUES (:uid, :bid, 1, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE badgescounter = badgescounter + 1';
$params = array(
':uid' => $userId,
':bid' => $badgeId
);
$this->execute($sql, $params);
return true;
}
public function grantLevelToUser($userId, $levelId) {
$sql = 'UPDATE t_gengamification_scores SET id_level = :lid WHERE id_user = :uid LIMIT 1';
$params = array(
':uid' => $userId,
':lid' => $levelId
);
$this->execute($sql, $params);
return true;
}
public function grantPointsToUser($userId, $points) {
$sql = 'INSERT INTO t_gengamification_scores (id_user, points, id_level) VALUES (:uid, :p, :firstlevel) ON DUPLICATE KEY UPDATE points = points + :p';
$params = array(
':uid' => $userId,
':p' => $points,
':firstlevel' => 1
);
$this->execute($sql, $params);
return true;
}
public function logUserEvent($userId, $eventId, $points = null, $badgeId = null, $levelId = null) {
$sql = 'INSERT INTO t_gengamification_log (id_user, id_event, eventdate, points, id_badge, id_level) VALUES (:uid, :eid, UTC_TIMESTAMP(), :p, :bid, :lid)';
$params = array(
':uid' => $userId,
':eid' => $eventId,
':p' => $points,
':bid' => $badgeId,
':lid' => $levelId,
);
$this->execute($sql, $params);
return true;
}
public function increaseEventCounter($userId, $eventId) {
$sql = 'INSERT INTO t_gengamification_events (id_user, id_event, eventcounter) VALUES (:uid, :eid, 1) ON DUPLICATE KEY UPDATE eventcounter = eventcounter + 1';
$params = array(
':uid' => $userId,
':eid' => $eventId
);
$this->execute($sql, $params);
return true;
}
public function increaseEventPoints($userId, $eventId, $points) {
$sql = 'UPDATE t_gengamification_events SET pointscounter = pointscounter + :c WHERE id_user = :uid AND id_event = :eid LIMIT 1';
$params = array(
':c' => $points,
':uid' => $userId,
':eid' => $eventId
);
$this->execute($sql, $params);
return true;
}
public function saveBadgeAlert($userId, $badgeId) {
$sql = 'INSERT INTO t_gengamification_alerts (id_user, id_badge, id_level) VALUES (:uid, :bid, NULL)';
$params = array(
':uid' => $userId,
':bid' => $badgeId
);
$this->execute($sql, $params);
return true;
}
public function saveLevelAlert($userId, $levelId) {
$sql = 'INSERT INTO t_gengamification_alerts (id_user, id_badge, id_level) VALUES (:uid, NULL, :lid)';
$params = array(
':uid' => $userId,
':lid' => $levelId
);
$this->execute($sql, $params);
return true;
}
}