-
Notifications
You must be signed in to change notification settings - Fork 28
/
go_datatable.php
416 lines (400 loc) · 10.2 KB
/
go_datatable.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
// Creates table for indivual logs.
function go_table_individual() {
global $wpdb;
$table_name = "{$wpdb->prefix}go";
$sql = "
CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
uid INT,
status INT,
post_id INT,
page_id INT,
count INT DEFAULT 0,
e_fail_count INT DEFAULT 0,
a_fail_count INT DEFAULT 0,
c_fail_count INT DEFAULT 0,
m_fail_count INT DEFAULT 0,
e_passed BOOLEAN DEFAULT 0,
a_passed BOOLEAN DEFAULT 0,
c_passed BOOLEAN DEFAULT 0,
m_passed BOOLEAN DEFAULT 0,
e_uploaded BOOLEAN DEFAULT 0,
a_uploaded BOOLEAN DEFAULT 0,
c_uploaded BOOLEAN DEFAULT 0,
m_uploaded BOOLEAN DEFAULT 0,
r_uploaded BOOLEAN DEFAULT 0,
points INT,
currency INT,
bonus_currency INT,
penalty INT,
gifted BOOLEAN DEFAULT 0,
minutes INT,
reason VARCHAR (200),
url VARCHAR (200),
timestamp VARCHAR (200),
UNIQUE KEY id (id)
);
";
require_once( ABSPATH.'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Creates a table for totals.
function go_table_totals() {
global $wpdb;
$table_name = "{$wpdb->prefix}go_totals";
$sql = "
CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
uid INT,
currency INT,
points INT,
bonus_currency INT,
penalty INT,
minutes INT,
badge_count INT,
UNIQUE KEY id (id)
);
";
require_once( ABSPATH.'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Updates the rank totals upon activation of plugin.
function go_ranks_registration() {
$ranks = get_option( 'go_ranks', false );
if ( ! $ranks || ! in_array( 'name', array_keys( $ranks ) ) ) {
$rank_prefix = get_option( 'go_level_names' );
if ( empty( $rank_prefix) ) {
$rank_prefix = 'Level';
}
$ranks = array(
'name' => array(),
'points' => array(),
'badges' => array()
);
for ( $i = 1; $i <= 20; $i++ ) {
if ( $i < 10 ) {
$ranks['name'][] = "{$rank_prefix} 0{$i}";
} else {
$ranks['name'][] = "{$rank_prefix} {$i}";
}
if ( $i == 1 ) {
$ranks['points'][0] = 0;
} else {
$ranks['points'][] = (15/2) * ( $i + 18) * ( $i - 1);
}
$ranks['badges'][] = '';
}
update_option( 'go_ranks', $ranks );
}
}
// Updates the presets for task creation upon activation of plugin.
function go_presets_registration() {
$presets = get_option( 'go_presets' );
if ( ! $presets || ! in_array( 'name', array_keys( $presets ) ) ) {
$presets = array(
'name' => array(
'Tier 1',
'Tier 2',
'Tier 3',
'Tier 4',
'Tier 5',
),
'points' => array(
array(
5, 5, 10, 30, 30
),
array(
5, 5, 20, 60, 60
),
array(
5, 5, 40, 120, 120
),
array(
5, 5, 70, 210, 210
),
array(
5, 5, 110, 330, 330
)
),
'currency' => array(
array(
0, 0, 3, 9, 9
),
array(
0, 0, 6, 18, 18
),
array(
0, 0, 12, 36, 36
),
array(
0, 0, 21, 63, 63
),
array(
0, 0, 33, 99, 99
)
)
);
update_option( 'go_presets', $presets );
}
}
function go_install_data () {
global $wpdb;
$table_name_user_meta = "{$wpdb->prefix}usermeta";
$table_name_go_totals = "{$wpdb->prefix}go_totals";
$table_name_go = "{$wpdb->prefix}go";
global $default_role;
$role = get_option( 'go_role', $default_role );
$rank_prefix = get_option( 'go_level_names' );
if ( empty( $rank_prefix ) ) {
$rank_prefix = 'Level';
}
$ranks = array(
'name' => array(),
'points' => array(),
'badges' => array()
);
for ( $i = 1; $i <= 20; $i++ ) {
if ( $i < 10 ) {
$ranks['name'][] = "{$rank_prefix} 0{$i}";
} else {
$ranks['name'][] = "{$rank_prefix} {$i}";
}
if ( $i == 1 ) {
$ranks['points'][0] = 0;
} else {
$ranks['points'][] = (15/2) * ( $i + 18) * ( $i - 1);
}
$ranks['badges'][] = '';
}
$tier_presets = array(
'name' => array(
'Tier 1',
'Tier 2',
'Tier 3',
'Tier 4',
'Tier 5'
),
'points' => array(
array( 5, 5, 10, 30, 30 ),
array( 5, 5, 20, 60, 60 ),
array( 5, 5, 40, 120, 120 ),
array( 5, 5, 70, 210, 210 ),
array( 5, 5, 110, 330, 330 )
),
'currency' => array(
array( 0, 0, 3, 9, 9 ),
array( 0, 0, 6, 18, 18 ),
array( 0, 0, 12, 36, 36 ),
array( 0, 0, 21, 63, 63 ),
array( 0, 0, 33, 99, 99 )
)
);
$period_defaults = array(
'Period 1',
'Period 2',
'Period 3',
'Period 4',
'Period 5',
'Period 6',
'Period 7'
);
$computer_defaults = array(
'Computer 01',
'Computer 02',
'Computer 03',
'Computer 04',
'Computer 05',
'Computer 06',
'Computer 07',
'Computer 08',
'Computer 09',
'Computer 10',
'Computer 11',
'Computer 12',
'Computer 13',
'Computer 14',
'Computer 15',
'Computer 16',
'Computer 17',
'Computer 18',
'Computer 19',
'Computer 20',
'Computer 21',
'Computer 22',
'Computer 23',
'Computer 24',
'Computer 25',
'Computer 26',
'Computer 27',
'Computer 28',
'Computer 29',
'Computer 30',
'Computer 31',
'Computer 32',
'Computer 33',
'Computer 34',
'Computer 35',
'Computer 36',
'Computer 37',
'Computer 38',
'Computer 39',
'Computer 40',
'Computer 41',
'Computer 42',
'Computer 43',
'Computer 44'
);
$options_array = array(
'go_tasks_name' => 'Quest',
'go_tasks_plural_name' => 'Quests' ,
'go_first_stage_name' => 'Stage 1',
'go_second_stage_name' => 'Stage 2',
'go_third_stage_name' => 'Stage 3',
'go_fourth_stage_name' => 'Stage 4',
'go_fifth_stage_name' => 'Stage 5',
'go_abandon_stage_button' => 'Abandon',
'go_second_stage_button' => 'Accept',
'go_third_stage_button' => 'Complete',
'go_fourth_stage_button' => 'Master',
'go_fifth_stage_button' => 'Repeat Mastery',
'go_store_name' => 'Store',
'go_bonus_loot_name' => 'Bonus Loot',
'go_points_name' => 'Experience',
'go_points_prefix' => '',
'go_points_suffix' => 'XP',
'go_currency_name' => 'Gold',
'go_currency_prefix' => '',
'go_currency_suffix' => 'G',
'go_bonus_currency_name' => 'Honor',
'go_bonus_currency_prefix' => '',
'go_bonus_currency_suffix' => 'HP',
'go_penalty_name' => 'Damage',
'go_penalty_prefix' => '',
'go_penalty_suffix' => 'DP',
'go_minutes_name' => 'Minutes',
'go_minutes_prefix' => '',
'go_minutes_suffix' => 'M',
'go_level_names' => 'Level',
'go_level_plural_names' => 'Levels',
'go_prestige_name' => 'Prestige',
'go_organization_name' => 'Seating Chart',
'go_class_a_name' => 'Period',
'go_class_b_name' => 'Computer',
'go_focus_name' => 'Profession',
'go_stats_name' => 'Stats',
'go_inventory_name' => 'Inventory',
'go_badges_name' => 'Badges',
'go_leaderboard_name' => 'Leaderboard',
'go_presets' => $tier_presets,
'go_admin_bar_display_switch' => 'On',
'go_admin_bar_user_redirect' => 'On',
'go_admin_bar_add_switch' => '',
'go_ranks' => $ranks,
'go_class_a' => $period_defaults,
'go_class_b' => $computer_defaults,
'go_focus_switch' => '',
'go_focus' => array( '' ),
'go_admin_email' => '',
'go_video_width' => '',
'go_video_height' => '',
'go_email_from' => 'no-reply@go.net',
'go_store_receipt_switch' => '',
'go_full_student_name_switch' => '',
'go_multiplier_switch' => '',
'go_multiplier_threshold' => 10,
'go_penalty_switch' => '',
'go_penalty_threshold' => 5,
'go_multiplier_percentage' => 10,
'go_data_reset_switch' => '',
);
foreach ( $options_array as $key => $value ) {
add_option( $key, $value );
}
$user_id_array = $wpdb->get_results(
$wpdb->prepare(
"SELECT user_id
FROM {$table_name_user_meta}
WHERE meta_key = %s AND ( meta_value LIKE %s OR meta_value LIKE %s )",
"{$wpdb->prefix}capabilities",
"%{$role}%",
'%administrator%'
)
);
for ( $index = 0; $index < count( $user_id_array ); $index++ ) {
$user_id = (int) $user_id_array[ $index ]->user_id;
$stored_points = 0;
$total_points = 0;
$total_currency = 0;
$bonus_currency = 0;
$penalty = 0;
$minutes = 0;
$status = -1;
$user_has_progress = (bool) $wpdb->get_var(
$wpdb->prepare( "SELECT uid FROM {$table_name_go} WHERE uid = %d", $user_id )
);
if ( $user_has_progress ) {
$stored_points = (int) $wpdb->get_var(
$wpdb->prepare( "SELECT sum( points ) FROM {$table_name_go} WHERE uid = %d", $user_id )
);
$total_points = ( $stored_points >= 0 ? $stored_points : 0 );
$total_currency = (int) $wpdb->get_var(
$wpdb->prepare( "SELECT sum( currency ) FROM {$table_name_go} WHERE uid = %d", $user_id )
);
}
$user_has_totals = (bool) $wpdb->get_var(
$wpdb->prepare( "SELECT uid FROM {$table_name_go_totals} WHERE uid = %d", $user_id )
);
if ( $user_has_totals && $total_points > 0 ) {
$wpdb->update(
$table_name_go_totals,
array(
'points' => $total_points,
'currency' => $total_currency
),
array(
'uid' => $user_id
),
array( '%d' )
);
} else if ( ! $user_has_totals ) {
$wpdb->insert(
$table_name_go_totals,
array(
'uid' => $user_id,
'points' => $total_points,
'currency' => $total_currency
),
array( '%d' )
);
}
go_update_ranks( $user_id, $total_points );
}
}
// Adds user id to the totals table upon user creation.
function go_user_registration ( $user_id ) {
global $wpdb;
$table_name_go_totals = "{$wpdb->prefix}go_totals";
$table_name_capabilities = "{$wpdb->prefix}capabilities";
$role = get_option( 'go_role', 'subscriber' );
$user_role = get_user_meta( $user_id, "{$table_name_capabilities}", true );
if ( array_search( 1, $user_role ) == $role || array_search( 1, $user_role ) == 'administrator' ) {
// this should update the user's rank metadata
go_update_ranks( $user_id, 0 );
// this should set the user's points to 0
$wpdb->insert( $table_name_go_totals, array( 'uid' => $user_id, 'points' => 0 ), array( '%s' ) );
}
}
// Deletes all rows related to a user in the individual and total tables upon deleting said user.
function go_user_delete( $user_id ) {
global $wpdb;
$table_name_go_totals = "{$wpdb->prefix}go_totals";
$table_name_go = "{$wpdb->prefix}go";
$wpdb->delete( $table_name_go_totals, array( 'uid' => $user_id ) );
$wpdb->delete( $table_name_go, array( 'uid' => $user_id ) );
}
function go_open_comments() {
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'comment_status' => 'open', 'ping_status' => 'open' ), array( 'post_type' => 'tasks' ) );
}
?>