Update repo

This commit is contained in:
2026-04-22 12:51:16 +02:00
parent d303a55638
commit d31591e287
24 changed files with 3994 additions and 3501 deletions
+158 -158
View File
@@ -1,159 +1,159 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function statpress_infographic_page() {
global $wpdb;
$table_activities = $wpdb->prefix . 'statpress_activities';
$table_categories = $wpdb->prefix . 'statpress_categories';
$current_year = isset( $_GET['year'] ) ? intval( $_GET['year'] ) : current_time( 'Y' );
// Pobierz dostępne lata z bazy danych
$available_years = $wpdb->get_col( "SELECT DISTINCT YEAR(date) FROM $table_activities ORDER BY YEAR(date) DESC" );
if ( empty( $available_years ) ) {
$available_years = array( current_time( 'Y' ) ); // Domyślny rok, jeśli brak danych
}
// --- 1. Statystyki ogólne (wszystkie lata) ---
$overall_stats = $wpdb->get_row(
"
SELECT
SUM(distance) as total_distance,
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
SUM(total_elevation_gain) as total_elevation_gain,
COUNT(id) as total_activities
FROM $table_activities
"
);
// --- 2. Statystyki dla wybranego roku ---
$yearly_stats = $wpdb->get_row(
$wpdb->prepare(
"
SELECT
SUM(distance) as total_distance,
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
SUM(total_elevation_gain) as total_elevation_gain,
COUNT(id) as total_activities
FROM $table_activities
WHERE YEAR(date) = %d
",
$current_year
)
);
// --- 3. Dane dla wykresu kołowego (dystans per kategoria dla wybranego roku) ---
$category_distance_data = $wpdb->get_results(
$wpdb->prepare(
"
SELECT c.name as category_name, c.color, SUM(a.distance) as total_distance
FROM $table_activities a
LEFT JOIN $table_categories c ON a.category_id = c.id
WHERE YEAR(a.date) = %d
GROUP BY c.name, c.color
HAVING SUM(a.distance) > 0
ORDER BY total_distance DESC
",
$current_year
)
);
$chart_labels = array();
$chart_data = array();
$chart_colors = array();
foreach ( $category_distance_data as $data ) {
$chart_labels[] = $data->category_name;
$chart_data[] = round( (float) $data->total_distance, 2 );
$chart_colors[] = $data->color;
}
// Włączenie skryptów dla Chart.js
wp_enqueue_script( 'chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', array(), null, true );
wp_register_script( 'statpress-infographic-chart-loader', false );
wp_enqueue_script( 'statpress-infographic-chart-loader' );
wp_add_inline_script(
'statpress-infographic-chart-loader',
'
document.addEventListener("DOMContentLoaded", function() {
const ctx = document.getElementById("statpressCategoryPieChart");
if (!ctx) return;
new Chart(ctx, {
type: "pie",
data: {
labels: ' . json_encode( $chart_labels ) . ',
datasets: [{
data: ' . json_encode( $chart_data ) . ',
backgroundColor: ' . json_encode( $chart_colors ) . ',
hoverOffset: 4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "right",
},
title: {
display: true,
text: "Dystans wg kategorii w ' . esc_js( $current_year ) . '"
}
}
}
});
});
'
);
?>
<div class="wrap">
<h1>Infografika Statystyk Sportowych</h1>
<div class="tablenav top">
<div class="alignleft actions">
<form method="get" style="display: flex; gap: 5px; align-items: center;">
<input type="hidden" name="page" value="statpress-infographic">
<label for="filter-by-year" class="screen-reader-text">Filtruj według roku</label>
<select name="year" id="filter-by-year">
<?php foreach ( $available_years as $year_option ) : ?>
<option value="<?php echo esc_attr( $year_option ); ?>" <?php selected( $current_year, $year_option ); ?>><?php echo esc_html( $year_option ); ?></option>
<?php endforeach; ?>
</select>
<?php submit_button( 'Filtruj', 'secondary', 'filter_action', false ); ?>
</form>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Statystyki Ogólne (wszystkie lata)</h2></div>
<div class="inside statpress-infographic-grid">
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $overall_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $overall_stats->total_duration ); ?></p></div>
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $overall_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $overall_stats->total_activities, 0, ',', ' ' ); ?></p></div>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Statystyki dla <?php echo esc_html( $current_year ); ?></h2></div>
<div class="inside statpress-infographic-grid">
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $yearly_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $yearly_stats->total_duration ); ?></p></div>
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $yearly_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $yearly_stats->total_activities, 0, ',', ' ' ); ?></p></div>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Rozkład Dystansu wg Kategorii w <?php echo esc_html( $current_year ); ?></h2></div>
<div class="inside">
<div style="position: relative; height:40vh; width:100%; max-width: 600px; margin: 0 auto;">
<canvas id="statpressCategoryPieChart"></canvas>
</div>
</div>
</div>
</div>
<?php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function statpress_infographic_page() {
global $wpdb;
$table_activities = $wpdb->prefix . 'statpress_activities';
$table_categories = $wpdb->prefix . 'statpress_categories';
$current_year = isset( $_GET['year'] ) ? intval( $_GET['year'] ) : current_time( 'Y' );
// Pobierz dostępne lata z bazy danych
$available_years = $wpdb->get_col( "SELECT DISTINCT YEAR(date) FROM $table_activities ORDER BY YEAR(date) DESC" );
if ( empty( $available_years ) ) {
$available_years = array( current_time( 'Y' ) ); // Domyślny rok, jeśli brak danych
}
// --- 1. Statystyki ogólne (wszystkie lata) ---
$overall_stats = $wpdb->get_row(
"
SELECT
SUM(distance) as total_distance,
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
SUM(total_elevation_gain) as total_elevation_gain,
COUNT(id) as total_activities
FROM $table_activities
"
);
// --- 2. Statystyki dla wybranego roku ---
$yearly_stats = $wpdb->get_row(
$wpdb->prepare(
"
SELECT
SUM(distance) as total_distance,
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
SUM(total_elevation_gain) as total_elevation_gain,
COUNT(id) as total_activities
FROM $table_activities
WHERE YEAR(date) = %d
",
$current_year
)
);
// --- 3. Dane dla wykresu kołowego (dystans per kategoria dla wybranego roku) ---
$category_distance_data = $wpdb->get_results(
$wpdb->prepare(
"
SELECT c.name as category_name, c.color, SUM(a.distance) as total_distance
FROM $table_activities a
LEFT JOIN $table_categories c ON a.category_id = c.id
WHERE YEAR(a.date) = %d
GROUP BY c.name, c.color
HAVING SUM(a.distance) > 0
ORDER BY total_distance DESC
",
$current_year
)
);
$chart_labels = array();
$chart_data = array();
$chart_colors = array();
foreach ( $category_distance_data as $data ) {
$chart_labels[] = $data->category_name;
$chart_data[] = round( (float) $data->total_distance, 2 );
$chart_colors[] = $data->color;
}
// Włączenie skryptów dla Chart.js
wp_enqueue_script( 'chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', array(), null, true );
wp_register_script( 'statpress-infographic-chart-loader', false );
wp_enqueue_script( 'statpress-infographic-chart-loader' );
wp_add_inline_script(
'statpress-infographic-chart-loader',
'
document.addEventListener("DOMContentLoaded", function() {
const ctx = document.getElementById("statpressCategoryPieChart");
if (!ctx) return;
new Chart(ctx, {
type: "pie",
data: {
labels: ' . json_encode( $chart_labels ) . ',
datasets: [{
data: ' . json_encode( $chart_data ) . ',
backgroundColor: ' . json_encode( $chart_colors ) . ',
hoverOffset: 4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "right",
},
title: {
display: true,
text: "Dystans wg kategorii w ' . esc_js( $current_year ) . '"
}
}
}
});
});
'
);
?>
<div class="wrap">
<h1>Infografika Statystyk Sportowych</h1>
<div class="tablenav top">
<div class="alignleft actions">
<form method="get" style="display: flex; gap: 5px; align-items: center;">
<input type="hidden" name="page" value="statpress-infographic">
<label for="filter-by-year" class="screen-reader-text">Filtruj według roku</label>
<select name="year" id="filter-by-year">
<?php foreach ( $available_years as $year_option ) : ?>
<option value="<?php echo esc_attr( $year_option ); ?>" <?php selected( $current_year, $year_option ); ?>><?php echo esc_html( $year_option ); ?></option>
<?php endforeach; ?>
</select>
<?php submit_button( 'Filtruj', 'secondary', 'filter_action', false ); ?>
</form>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Statystyki Ogólne (wszystkie lata)</h2></div>
<div class="inside statpress-infographic-grid">
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $overall_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $overall_stats->total_duration ); ?></p></div>
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $overall_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $overall_stats->total_activities, 0, ',', ' ' ); ?></p></div>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Statystyki dla <?php echo esc_html( $current_year ); ?></h2></div>
<div class="inside statpress-infographic-grid">
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $yearly_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $yearly_stats->total_duration ); ?></p></div>
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $yearly_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $yearly_stats->total_activities, 0, ',', ' ' ); ?></p></div>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Rozkład Dystansu wg Kategorii w <?php echo esc_html( $current_year ); ?></h2></div>
<div class="inside">
<div style="position: relative; height:40vh; width:100%; max-width: 600px; margin: 0 auto;">
<canvas id="statpressCategoryPieChart"></canvas>
</div>
</div>
</div>
</div>
<?php
}