diff --git a/moje-statystyki.php b/moje-statystyki.php index 2b148f4..511111b 100644 --- a/moje-statystyki.php +++ b/moje-statystyki.php @@ -21,6 +21,7 @@ function mystat_activate() { $table_activities = $wpdb->prefix . 'mystat_activities'; $table_event_types = $wpdb->prefix . 'mystat_event_types'; $table_equipment = $wpdb->prefix . 'mystat_equipment'; + $table_goals = $wpdb->prefix . 'mystat_goals'; // SQL dla Kategorii $sql_cat = "CREATE TABLE $table_categories ( @@ -45,6 +46,19 @@ function mystat_activate() { PRIMARY KEY (id) ) $charset_collate;"; + // SQL dla Celów + $sql_goals = "CREATE TABLE $table_goals ( + id mediumint(9) NOT NULL AUTO_INCREMENT, + name varchar(255) NOT NULL, + goal_type varchar(20) NOT NULL, -- 'distance', 'duration_sec', 'count' + target_value decimal(10,2) NOT NULL, + year smallint(4) NOT NULL, + month tinyint(2) UNSIGNED DEFAULT NULL, + category_id mediumint(9) DEFAULT NULL, + PRIMARY KEY (id), + KEY category_id (category_id) + ) $charset_collate;"; + // SQL dla Aktywności $sql_act = "CREATE TABLE $table_activities ( id bigint(20) NOT NULL AUTO_INCREMENT, @@ -73,6 +87,7 @@ function mystat_activate() { ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + dbDelta( $sql_goals ); dbDelta( $sql_equipment ); dbDelta( $sql_cat ); dbDelta( $sql_event_types ); @@ -206,6 +221,25 @@ function mystat_add_admin_menu() { 'mystat_equipment_page' ); + $mystat_plugin_hooks[] = add_submenu_page( + 'moje-statystyki', + 'Użycie Sprzętu', + 'Użycie Sprzętu', + 'manage_options', + 'mystat-equipment-usage', + 'mystat_equipment_usage_page' + ); + + $mystat_plugin_hooks[] = add_submenu_page( + 'moje-statystyki', + 'Cele', + 'Cele', + 'manage_options', + 'mystat-goals', + 'mystat_goals_page' + ); + + $mystat_plugin_hooks[] = add_submenu_page( null, // Ukryta strona, nie pojawia się w menu 'Szczegóły Treningu', // Tytuł strony @@ -444,6 +478,247 @@ function mystat_equipment_page() { prefix . 'mystat_activities'; + $table_equipment = $wpdb->prefix . 'mystat_equipment'; + + $sql = " + SELECT + eq.id, + eq.name, + SUM(a.distance) as total_distance, + SUM(TIME_TO_SEC(a.duration)) as total_seconds, + COUNT(a.id) as activity_count + FROM + $table_equipment eq + LEFT JOIN + $table_activities a ON eq.id = a.equipment_id + GROUP BY + eq.id, eq.name + ORDER BY + total_distance DESC + "; + + $equipment_usage = $wpdb->get_results($sql); + ?> +
+

Użycie Sprzętu

+

Tutaj znajdziesz podsumowanie wykorzystania Twojego sprzętu we wszystkich zarejestrowanych aktywnościach.

+ + + + + + + + + + + + + + + + + + + + +
Nazwa SprzętuCałkowity DystansCałkowity CzasLiczba Aktywności
name); ?>total_distance ? number_format($item->total_distance, 2, ',', ' ') . ' km' : 'Brak danych'; ?>total_seconds ? sprintf('%d godz. %d min.', floor($item->total_seconds / 3600), floor(($item->total_seconds % 3600) / 60)) : 'Brak danych'; ?>activity_count; ?>
+
+ prefix . 'mystat_activities'; + + $sql_select = ''; + switch ($goal->goal_type) { + case 'distance': + $sql_select = 'SUM(distance)'; + break; + case 'duration_sec': + $sql_select = 'SUM(TIME_TO_SEC(duration))'; + break; + case 'count': + $sql_select = 'COUNT(id)'; + break; + default: + return ['current_value' => 0, 'percentage' => 0]; + } + + $where_clauses = []; + $where_clauses[] = $wpdb->prepare('YEAR(date) = %d', $goal->year); + + if (!empty($goal->month)) { + $where_clauses[] = $wpdb->prepare('MONTH(date) = %d', $goal->month); + } + if (!empty($goal->category_id)) { + $where_clauses[] = $wpdb->prepare('category_id = %d', $goal->category_id); + } + + $sql = "SELECT {$sql_select} FROM {$table_activities} WHERE " . implode(' AND ', $where_clauses); + + $current_value = (float) $wpdb->get_var($sql); + $percentage = ($goal->target_value > 0) ? ($current_value / $goal->target_value) * 100 : 0; + + return [ + 'current_value' => $current_value, + 'percentage' => $percentage, + ]; +} + +function mystat_goals_page() { + global $wpdb; + $table_goals = $wpdb->prefix . 'mystat_goals'; + $table_categories = $wpdb->prefix . 'mystat_categories'; + $message = ''; + $notice_class = ''; + + // Handle POST requests (add/update) + if ( isset( $_POST['submit'] ) && check_admin_referer( 'mystat_manage_goal' ) ) { + $goal_id = isset( $_POST['goal_id'] ) ? intval( $_POST['goal_id'] ) : 0; + $data = [ + 'name' => sanitize_text_field($_POST['goal_name']), + 'goal_type' => sanitize_text_field($_POST['goal_type']), + 'target_value' => floatval(str_replace(',', '.', $_POST['target_value'])), + 'year' => intval($_POST['year']), + 'month' => $_POST['month'] === 'all' ? null : intval($_POST['month']), + 'category_id' => $_POST['category_id'] === 'all' ? null : intval($_POST['category_id']), + ]; + + if ( !empty($data['name']) && !empty($data['goal_type']) && $data['target_value'] > 0 && $data['year'] > 2000 ) { + if ( $goal_id > 0 ) { // Update + $wpdb->update( $table_goals, $data, [ 'id' => $goal_id ] ); + $message = 'Cel zaktualizowany.'; + $notice_class = 'notice-success'; + } else { // Insert + $wpdb->insert( $table_goals, $data ); + $message = 'Cel dodany.'; + $notice_class = 'notice-success'; + } + } else { + $message = 'Wypełnij poprawnie wszystkie wymagane pola (Nazwa, Typ, Cel, Rok).'; + $notice_class = 'notice-error'; + } + } + + // Handle GET requests (delete) + if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && $_GET['action'] === 'delete' ) { + if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_goal_' . $_GET['id'] ) ) { + $wpdb->delete( $table_goals, [ 'id' => intval( $_GET['id'] ) ] ); + $message = 'Cel usunięty.'; + $notice_class = 'notice-success'; + } + } + + // Prepare for form (for editing) + $item_to_edit = null; + if ( isset( $_GET['action'], $_GET['id'] ) && $_GET['action'] === 'edit' ) { + $item_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_goals WHERE id = %d", intval( $_GET['id'] ) ) ); + } + + $goals = $wpdb->get_results( "SELECT g.*, c.name as category_name FROM $table_goals g LEFT JOIN $table_categories c ON g.category_id = c.id ORDER BY g.year DESC, g.name ASC" ); + $categories = $wpdb->get_results( "SELECT * FROM $table_categories ORDER BY name ASC" ); + ?> +
+

Zarządzaj Celami

+ +

+ + +
+
+
+
+

+
+ + +
+
+

Dla czasu podaj wartość w godzinach (np. 100.5).

+
+
+
+ +
+
+
+
+
+
+ + + + + + + + goal_type === 'duration_sec'; + if ($is_duration) { + $goal->target_value = $goal->target_value * 3600; // Convert hours back to seconds for calculation + } + $progress = mystat_get_goal_progress($goal); + $percentage = min(100, $progress['percentage']); + + $target_formatted = ''; + $current_formatted = ''; + if ($is_duration) { + $target_formatted = round($goal->target_value / 3600) . ' godz.'; + $current_formatted = sprintf('%d godz. %d min.', floor($progress['current_value'] / 3600), floor(($progress['current_value'] % 3600) / 60)); + } elseif ($goal->goal_type === 'distance') { + $target_formatted = number_format($goal->target_value, 0, ',', ' ') . ' km'; + $current_formatted = number_format($progress['current_value'], 2, ',', ' ') . ' km'; + } else { // count + $target_formatted = (int)$goal->target_value; + $current_formatted = (int)$progress['current_value']; + } + ?> + + + + + + + + +
CelPostępAkcje
Brak zdefiniowanych celów.
+ name); ?>
+ + year); ?> + month) echo ' / ' . date_i18n('F', mktime(0,0,0,$goal->month,10)); ?> + category_name) echo ' / ' . esc_html($goal->category_name); ?> + +
+
+
+
+ z (%) +
+ Edytuj | + Usuń +
+
+
+
+
+ +
@@ -547,6 +822,13 @@ function mystat_yearly_summary_page() { $available_years = [current_time('Y')]; // Domyślny rok, jeśli brak danych } + // --- GOALS SECTION --- + $table_goals = $wpdb->prefix . 'mystat_goals'; + $goals_for_year = $wpdb->get_results($wpdb->prepare( + "SELECT * FROM {$table_goals} WHERE year = %d ORDER BY name ASC", + $current_year + )); + // Zapytanie SQL do grupowania danych miesięcznie $sql = $wpdb->prepare(" SELECT @@ -727,6 +1009,48 @@ function mystat_yearly_summary_page() {
+ +
+

Cele na

+
+ +
+
+ + +

Wykresy dla

@@ -2005,7 +2329,6 @@ function mystat_render_history_table() {