Wheel_of_Fortune_Plugin/admin/stats-page.php

540 lines
25 KiB
PHP

<?php
/**
* Stran s statistiko za Kolo Sreče
*/
// Prepreči neposreden dostop
if (!defined('ABSPATH')) {
exit;
}
// Preveri, če ima uporabnik pravice za dostop
if (!current_user_can('manage_options')) {
return;
}
// Pridobi podatke o uporabnikih
global $wpdb;
$users_table = $wpdb->prefix . 'users';
$spins_table = $wpdb->prefix . 'wheel_spins';
$log_table = $wpdb->prefix . 'wheel_log';
$prizes_table = $wpdb->prefix . 'wheel_prizes';
$wheels_table = $wpdb->prefix . 'wof_wheels';
// Pridobi vsa kolesa
$wheels = $wpdb->get_results("SELECT * FROM $wheels_table ORDER BY id ASC", ARRAY_A);
// Izberi kolo (privzeto prvo)
$selected_wheel_id = isset($_GET['wheel_id']) ? intval($_GET['wheel_id']) : 1;
$selected_wheel = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wheels_table WHERE id = %d", $selected_wheel_id), ARRAY_A);
if (!$selected_wheel) {
$selected_wheel_id = 1;
$selected_wheel = $wpdb->get_row("SELECT * FROM $wheels_table WHERE id = 1", ARRAY_A);
}
// Iskanje uporabnikov
$search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
$search_condition = '';
if (!empty($search)) {
$search_condition = $wpdb->prepare(
"AND (u.user_login LIKE %s OR u.user_email LIKE %s OR u.display_name LIKE %s)",
"%{$search}%",
"%{$search}%",
"%{$search}%"
);
}
// Pridobi uporabnike z spin-i za izbrano kolo
$users_with_spins = $wpdb->get_results(
$wpdb->prepare(
"SELECT u.ID, u.user_email, u.display_name,
COALESCE(s.spins_available, 0) as spins_available,
COUNT(l.id) as total_spins,
MAX(l.spin_date) as last_spin_date
FROM {$users_table} u
LEFT JOIN {$spins_table} s ON u.ID = s.user_id AND s.wheel_id = %d
LEFT JOIN {$log_table} l ON u.ID = l.user_id AND l.wheel_id = %d
WHERE 1=1 {$search_condition}
GROUP BY u.ID
HAVING total_spins > 0 OR spins_available > 0
ORDER BY total_spins DESC",
$selected_wheel_id, $selected_wheel_id
),
ARRAY_A
);
// Označi nagrado kot unovčeno
if (isset($_POST['mark_redeemed']) && isset($_POST['prize_id'])) {
check_admin_referer('mark_prize_redeemed_nonce', 'mark_prize_redeemed_nonce');
$prize_log_id = intval($_POST['prize_id']);
$wpdb->update(
$log_table,
array('redeemed' => 1),
array('id' => $prize_log_id)
);
echo '<div class="notice notice-success is-dismissible"><p>' .
__('Nagrada je bila označena kot unovčena.', 'wheel-of-fortune') .
'</p></div>';
}
// Ponastavi spine na 0 za vse uporabnike na izbranem kolesu
if (isset($_POST['reset_all_spins_wheel']) && isset($_POST['wheel_id'])) {
check_admin_referer('reset_spins_wheel_nonce', 'reset_spins_wheel_nonce');
$wheel_id = intval($_POST['wheel_id']);
// Preveri, če kolo obstaja
$wheel_exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM $wheels_table WHERE id = %d", $wheel_id));
if ($wheel_exists) {
// Ponastavi spine na 0 za vse uporabnike na tem kolesu
$result = $wpdb->update(
$spins_table,
array('spins_available' => 0),
array('wheel_id' => $wheel_id),
array('%d'),
array('%d')
);
if ($result !== false) {
echo '<div class="notice notice-success is-dismissible"><p>' .
sprintf(__('Vsi spini za kolo ID %d so bili uspešno ponastavljeni na 0.', 'wheel-of-fortune'), $wheel_id) .
'</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' .
__('Prišlo je do napake pri ponastavitvi spinov.', 'wheel-of-fortune') .
'</p></div>';
}
} else {
echo '<div class="notice notice-error is-dismissible"><p>' .
__('Izbrano kolo ne obstaja.', 'wheel-of-fortune') .
'</p></div>';
}
}
// Izberi uporabnika za podrobnosti
$selected_user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
// Pridobi podrobnosti o nagradah uporabnika za izbrano kolo, če je izbran
$user_prizes = array();
if ($selected_user_id > 0) {
$user_prizes = $wpdb->get_results(
$wpdb->prepare(
"SELECT l.id, p.name as prize_name, p.description as prize_description,
l.spin_date, l.redeemed
FROM {$log_table} l
JOIN {$prizes_table} p ON l.prize_id = p.id
WHERE l.user_id = %d AND l.wheel_id = %d
ORDER BY l.spin_date DESC",
$selected_user_id, $selected_wheel_id
),
ARRAY_A
);
}
?>
<div class="wrap">
<h1><?php echo esc_html__('Statistika Kolesa Sreče', 'wheel-of-fortune'); ?></h1>
<!-- Gumb za ponastavitev spinov za izbrano kolo -->
<div class="notice notice-warning" style="margin: 20px 0;">
<p>
<strong><?php echo esc_html__('Ponastavitev spinov', 'wheel-of-fortune'); ?>:</strong>
<?php echo esc_html__('To dejanje bo ponastavilo vse spine na 0 za vse uporabnike na izbranem kolesu. To dejanje ni mogoče razveljaviti.', 'wheel-of-fortune'); ?>
<form method="post" style="display: inline-block; margin-left: 10px;">
<?php wp_nonce_field('reset_spins_wheel_nonce', 'reset_spins_wheel_nonce'); ?>
<input type="hidden" name="wheel_id" value="<?php echo esc_attr($selected_wheel_id); ?>">
<button type="submit" name="reset_all_spins_wheel" class="button button-secondary"
onclick="return confirm('<?php echo esc_js(__('Ste prepričani, da želite ponastaviti vse spine na 0 za vse uporabnike na tem kolesu? To dejanje ni mogoče razveljaviti.', 'wheel-of-fortune')); ?>')">
<?php echo esc_html__('Ponastavi vse spine na 0 za to kolo', 'wheel-of-fortune'); ?>
</button>
</form>
</p>
</div>
<!-- Izbira kolesa -->
<div class="tablenav top">
<div class="alignleft actions">
<form method="get" style="display: inline-block; margin-right: 20px;">
<input type="hidden" name="page" value="wof-stats">
<label for="wheel-select"><?php echo esc_html__('Izberi kolo:', 'wheel-of-fortune'); ?></label>
<select name="wheel_id" id="wheel-select" onchange="this.form.submit()">
<?php foreach ($wheels as $wheel): ?>
<option value="<?php echo esc_attr($wheel['id']); ?>" <?php selected($selected_wheel_id, $wheel['id']); ?>>
<?php echo esc_html($wheel['name']); ?>
</option>
<?php endforeach; ?>
</select>
</form>
<form method="get" style="display: inline-block;">
<input type="hidden" name="page" value="wof-stats">
<input type="hidden" name="wheel_id" value="<?php echo esc_attr($selected_wheel_id); ?>">
<label for="user-search" class="screen-reader-text"><?php echo esc_html__('Iskanje uporabnikov:', 'wheel-of-fortune'); ?></label>
<input type="search" id="user-search" name="s" value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?>">
<input type="submit" class="button" value="<?php echo esc_attr__('Išči uporabnike', 'wheel-of-fortune'); ?>">
</form>
</div>
<br class="clear">
</div>
<h2><?php echo sprintf(esc_html__('Seznam uporabnikov s spini za kolo: %s', 'wheel-of-fortune'), esc_html($selected_wheel['name'])); ?></h2>
<!-- Filtriranje uporabnikov -->
<div class="user-filters" style="margin: 20px 0; padding: 15px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 5px;">
<h4><?php echo esc_html__('Filtriranje uporabnikov', 'wheel-of-fortune'); ?></h4>
<div style="display: flex; gap: 15px; align-items: center; flex-wrap: wrap;">
<div>
<label for="filter-spins"><?php echo esc_html__('Preostali spini:', 'wheel-of-fortune'); ?></label>
<select id="filter-spins" style="margin-left: 5px;">
<option value=""><?php echo esc_html__('Vsi', 'wheel-of-fortune'); ?></option>
<option value="0"><?php echo esc_html__('0 spinov (porabili)', 'wheel-of-fortune'); ?></option>
<option value="1"><?php echo esc_html__('1 spin', 'wheel-of-fortune'); ?></option>
<option value="2"><?php echo esc_html__('2 spina', 'wheel-of-fortune'); ?></option>
<option value="3"><?php echo esc_html__('3 spini', 'wheel-of-fortune'); ?></option>
<option value="4"><?php echo esc_html__('4 spini', 'wheel-of-fortune'); ?></option>
<option value="5"><?php echo esc_html__('5+ spinov', 'wheel-of-fortune'); ?></option>
</select>
</div>
<div>
<label for="filter-total-spins"><?php echo esc_html__('Skupno spinov:', 'wheel-of-fortune'); ?></label>
<select id="filter-total-spins" style="margin-left: 5px;">
<option value=""><?php echo esc_html__('Vsi', 'wheel-of-fortune'); ?></option>
<option value="0"><?php echo esc_html__('0', 'wheel-of-fortune'); ?></option>
<option value="1"><?php echo esc_html__('1', 'wheel-of-fortune'); ?></option>
<option value="2-5"><?php echo esc_html__('2-5', 'wheel-of-fortune'); ?></option>
<option value="6-10"><?php echo esc_html__('6-10', 'wheel-of-fortune'); ?></option>
<option value="11+"><?php echo esc_html__('11+', 'wheel-of-fortune'); ?></option>
</select>
</div>
<div>
<button type="button" onclick="filterUsers()" class="button button-primary"><?php echo esc_html__('Potrdi filtriranje', 'wheel-of-fortune'); ?></button>
<button type="button" onclick="clearFilters()" class="button" style="margin-left: 10px;"><?php echo esc_html__('Počisti filtre', 'wheel-of-fortune'); ?></button>
</div>
</div>
<div id="filter-results" style="margin-top: 10px; font-weight: bold; color: #0073aa;"></div>
</div>
<table class="wp-list-table widefat fixed striped" id="users-table">
<thead>
<tr>
<th scope="col"><?php echo esc_html__('ID', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('E-pošta', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Skupno št. spinov', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Preostali spini', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Zadnji spin', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Akcije', 'wheel-of-fortune'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($users_with_spins)) : ?>
<tr>
<td colspan="6"><?php echo esc_html__('Ni uporabnikov z dodeljenimi spini.', 'wheel-of-fortune'); ?></td>
</tr>
<?php else : ?>
<?php foreach ($users_with_spins as $user) : ?>
<tr<?php echo ($selected_user_id == $user['ID']) ? ' class="active"' : ''; ?>>
<td><?php echo esc_html($user['ID']); ?></td>
<td><?php echo esc_html($user['user_email']); ?></td>
<td><?php echo esc_html($user['total_spins']); ?></td>
<td><?php echo esc_html($user['spins_available']); ?></td>
<td><?php echo !empty($user['last_spin_date']) ? esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($user['last_spin_date']))) : '-'; ?></td>
<td>
<a href="<?php echo esc_url(add_query_arg('user_id', $user['ID'])); ?>" class="button"><?php echo esc_html__('Podrobnosti', 'wheel-of-fortune'); ?></a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<?php if ($selected_user_id > 0) : ?>
<?php $selected_user = get_userdata($selected_user_id); ?>
<?php if ($selected_user) : ?>
<h2><?php echo sprintf(esc_html__('Podrobnosti za uporabnika: %s', 'wheel-of-fortune'), esc_html($selected_user->display_name)); ?></h2>
<table class="form-table">
<tr>
<th scope="row"><?php echo esc_html__('ID uporabnika', 'wheel-of-fortune'); ?></th>
<td><?php echo esc_html($selected_user_id); ?></td>
</tr>
<tr>
<th scope="row"><?php echo esc_html__('E-pošta', 'wheel-of-fortune'); ?></th>
<td><?php echo esc_html($selected_user->user_email); ?></td>
</tr>
<tr>
<th scope="row"><?php echo esc_html__('Skupno število spinov', 'wheel-of-fortune'); ?></th>
<td>
<?php
$total_spins = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$log_table} WHERE user_id = %d AND wheel_id = %d",
$selected_user_id, $selected_wheel_id
));
echo esc_html($total_spins);
?>
</td>
</tr>
<tr>
<th scope="row"><?php echo esc_html__('Preostali spini', 'wheel-of-fortune'); ?></th>
<td>
<?php
$spins = $wpdb->get_var($wpdb->prepare(
"SELECT spins_available FROM {$spins_table} WHERE user_id = %d AND wheel_id = %d",
$selected_user_id, $selected_wheel_id
));
echo esc_html($spins ?: 0);
?>
</td>
</tr>
</table>
<h3><?php echo esc_html__('Zgodovina nagrad', 'wheel-of-fortune'); ?></h3>
<?php if (empty($user_prizes)) : ?>
<p><?php echo esc_html__('Ta uporabnik še ni prejel nobene nagrade.', 'wheel-of-fortune'); ?></p>
<?php else : ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th scope="col"><?php echo esc_html__('ID', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Nagrada', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Opis', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Datum', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Status', 'wheel-of-fortune'); ?></th>
<th scope="col"><?php echo esc_html__('Akcije', 'wheel-of-fortune'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($user_prizes as $prize) : ?>
<tr>
<td><?php echo esc_html($prize['id']); ?></td>
<td><?php echo esc_html($prize['prize_name']); ?></td>
<td><?php echo esc_html($prize['prize_description']); ?></td>
<td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($prize['spin_date']))); ?></td>
<td>
<?php echo $prize['redeemed'] ?
'<span class="dashicons dashicons-yes" style="color: green;"></span> ' . esc_html__('Unovčeno', 'wheel-of-fortune') :
'<span class="dashicons dashicons-no" style="color: red;"></span> ' . esc_html__('Neunovčeno', 'wheel-of-fortune');
?>
</td>
<td>
<?php if (!$prize['redeemed']) : ?>
<form method="post" style="display: inline;">
<?php wp_nonce_field('mark_prize_redeemed_nonce', 'mark_prize_redeemed_nonce'); ?>
<input type="hidden" name="prize_id" value="<?php echo esc_attr($prize['id']); ?>">
<button type="submit" name="mark_redeemed" class="button"><?php echo esc_html__('Označi kot unovčeno', 'wheel-of-fortune'); ?></button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php else : ?>
<p><?php echo esc_html__('Uporabnik ni bil najden.', 'wheel-of-fortune'); ?></p>
<?php endif; ?>
<?php endif; ?>
</div>
<script>
// Funkcija za filtriranje uporabnikov
function filterUsers() {
const filterSpins = document.getElementById('filter-spins').value;
const filterTotalSpins = document.getElementById('filter-total-spins').value;
const table = document.getElementById('users-table');
const tbody = table.getElementsByTagName('tbody')[0];
if (!tbody) {
console.error('Tabela ni najdena');
return;
}
const rows = tbody.getElementsByTagName('tr');
let visibleCount = 0;
console.log('Filtriranje uporabnikov:', { filterSpins, filterTotalSpins, totalRows: rows.length });
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const cells = row.getElementsByTagName('td');
if (cells.length < 4) {
console.log('Preskočena vrstica (premalo stolpcev):', cells.length);
continue; // Preskoči header vrstice ali vrstice brez dovolj stolpcev
}
// Debug: prikaži vsebino vseh stolpcev
console.log(`Vrstica ${i} stolpci:`, {
stolpec0: cells[0] ? cells[0].textContent.trim() : 'N/A',
stolpec1: cells[1] ? cells[1].textContent.trim() : 'N/A',
stolpec2: cells[2] ? cells[2].textContent.trim() : 'N/A',
stolpec3: cells[3] ? cells[3].textContent.trim() : 'N/A',
stolpec4: cells[4] ? cells[4].textContent.trim() : 'N/A',
stolpec5: cells[5] ? cells[5].textContent.trim() : 'N/A'
});
const currentSpins = parseInt(cells[3].textContent.trim()) || 0; // Preostali spini (4. stolpec)
const totalSpins = parseInt(cells[2].textContent.trim()) || 0; // Skupno št. spinov (3. stolpec)
console.log(`Vrstica ${i}: currentSpins=${currentSpins}, totalSpins=${totalSpins}`);
let showRow = true;
// Filtriranje po preostalih spinih
if (filterSpins !== '') {
if (filterSpins === '0') {
showRow = showRow && (currentSpins === 0);
} else if (filterSpins === '1') {
showRow = showRow && (currentSpins === 1);
} else if (filterSpins === '2') {
showRow = showRow && (currentSpins === 2);
} else if (filterSpins === '3') {
showRow = showRow && (currentSpins === 3);
} else if (filterSpins === '4') {
showRow = showRow && (currentSpins === 4);
} else if (filterSpins === '5') {
showRow = showRow && (currentSpins >= 5);
}
}
// Filtriranje po skupnem številu spinov
if (filterTotalSpins !== '') {
if (filterTotalSpins === '0') {
showRow = showRow && (totalSpins === 0);
} else if (filterTotalSpins === '1') {
showRow = showRow && (totalSpins === 1);
} else if (filterTotalSpins === '2-5') {
showRow = showRow && (totalSpins >= 2 && totalSpins <= 5);
} else if (filterTotalSpins === '6-10') {
showRow = showRow && (totalSpins >= 6 && totalSpins <= 10);
} else if (filterTotalSpins === '11+') {
showRow = showRow && (totalSpins >= 11);
}
}
// Prikaži/skrij vrstico
if (showRow) {
row.style.display = '';
visibleCount++;
console.log(`Prikazana vrstica ${i}`);
} else {
row.style.display = 'none';
console.log(`Skrita vrstica ${i}`);
}
}
console.log(`Filtriranje končano: ${visibleCount} od ${rows.length} vrstic prikazanih`);
// Posodobi števec rezultatov
updateFilterResults(visibleCount, rows.length);
}
function clearFilters() {
document.getElementById('filter-spins').value = '';
document.getElementById('filter-total-spins').value = '';
filterUsers();
}
function updateFilterResults(visible, total) {
const resultsDiv = document.getElementById('filter-results');
if (visible === total) {
resultsDiv.innerHTML = '<?php echo esc_js(__('Prikazani vsi uporabniki:', 'wheel-of-fortune')); ?> ' + total;
} else {
resultsDiv.innerHTML = '<?php echo esc_js(__('Prikazano:', 'wheel-of-fortune')); ?> ' + visible + ' <?php echo esc_js(__('od', 'wheel-of-fortune')); ?> ' + total + ' <?php echo esc_js(__('uporabnikov', 'wheel-of-fortune')); ?>';
}
}
// Testna funkcija za preverjanje strukture tabele
function testTableStructure() {
const table = document.getElementById('users-table');
if (!table) {
console.log('Tabela ni najdena!');
return;
}
const tbody = table.getElementsByTagName('tbody')[0];
if (!tbody) {
console.log('Tbody ni najden!');
return;
}
const rows = tbody.getElementsByTagName('tr');
console.log(`Najdenih ${rows.length} vrstic v tabeli`);
if (rows.length > 0) {
const firstRow = rows[0];
const cells = firstRow.getElementsByTagName('td');
console.log(`Prva vrstica ima ${cells.length} stolpcev`);
for (let i = 0; i < cells.length; i++) {
console.log(`Stolpec ${i}: "${cells[i].textContent.trim()}"`);
}
}
}
// Inicializiraj števec rezultatov ob nalaganju strani
jQuery(document).ready(function($) {
// Gumb za ponastavitev spinov preko AJAX
$('button[name="reset_all_spins_wheel"]').on('click', function(e) {
e.preventDefault();
const button = $(this);
const originalText = button.text();
const wheelId = $('input[name="wheel_id"]').val();
if (!confirm('<?php echo esc_js(__('Ste prepričani, da želite ponastaviti vse spine na 0 za vse uporabnike na tem kolesu? To dejanje ni mogoče razveljaviti.', 'wheel-of-fortune')); ?>')) {
return;
}
button.prop('disabled', true).text('<?php echo esc_js(__('Ponastavljam spine...', 'wheel-of-fortune')); ?>');
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'wheel_reset_spins_wheel',
wheel_id: wheelId,
_ajax_nonce: '<?php echo wp_create_nonce('wheel_admin_nonce'); ?>'
},
success: function(response) {
if (response.success) {
// Prikaži uspešno sporočilo
$('<div class="notice notice-success is-dismissible"><p>' + response.data.message + '</p></div>')
.insertAfter('h1')
.delay(3000)
.fadeOut();
// Osveži stran po 2 sekundah
setTimeout(function() {
location.reload();
}, 2000);
} else {
alert(response.data.message || '<?php echo esc_js(__('Prišlo je do napake.', 'wheel-of-fortune')); ?>');
button.prop('disabled', false).text(originalText);
}
},
error: function() {
alert('<?php echo esc_js(__('Prišlo je do napake pri komunikaciji s strežnikom.', 'wheel-of-fortune')); ?>');
button.prop('disabled', false).text(originalText);
}
});
});
// Posodobi števec rezultatov ob nalaganju strani
const table = document.getElementById('users-table');
if (table) {
const rows = table.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
updateFilterResults(rows.length, rows.length);
// Testiraj strukturo tabele
testTableStructure();
}
});
</script>