From 2e236f4653ec7010c2d7948779bb2c16b04f8a98 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Mon, 16 Sep 2024 16:41:08 +0200 Subject: [PATCH] modifying permissions to allow simple user to see stats --- functions.php | 1 + includes/permissions.php | 8 + includes/statistics-page.php | 6 +- resources/js/stats-dashboard.js | 208 ++++++++++-------------- template-parts/statistics/dashboard.php | 59 +++++-- 5 files changed, 139 insertions(+), 143 deletions(-) create mode 100644 includes/permissions.php diff --git a/functions.php b/functions.php index a584dce..afea1f2 100644 --- a/functions.php +++ b/functions.php @@ -10,6 +10,7 @@ require_once(__DIR__ . '/includes/logos.php'); require_once(__DIR__ . '/includes/api.php'); require_once(__DIR__ . '/includes/statistics-page.php'); require_once(__DIR__ . '/includes/search-find.php'); +require_once(__DIR__ . '/includes/permissions.php'); // require_once(__DIR__ . '/includes/widget.php'); // require_once( __DIR__ . '/includes/taxonomy.php'); diff --git a/includes/permissions.php b/includes/permissions.php new file mode 100644 index 0000000..ed42ac1 --- /dev/null +++ b/includes/permissions.php @@ -0,0 +1,8 @@ +add_cap('access_custom_options_page'); +// } +// add_action('init', 'add_custom_capability_to_subscriber'); diff --git a/includes/statistics-page.php b/includes/statistics-page.php index 2958257..20e8269 100644 --- a/includes/statistics-page.php +++ b/includes/statistics-page.php @@ -20,7 +20,7 @@ function register_statistics_option_page() add_menu_page( 'Statistiques des jeux', 'Statistiques des jeux', - 'manage_options', + 'read', 'session-datas-options', 'render_statistics_option_page', 'dashicons-chart-area' @@ -46,7 +46,7 @@ add_action('admin_post_nopriv_download_pdf', 'handle_download_pdf_request'); function handle_download_pdf_request() { // Vérifier les autorisations ou les conditions nécessaires - if (!current_user_can('manage_options')) { + if (!current_user_can('read')) { wp_die('Access Denied'); } @@ -100,7 +100,7 @@ add_action('admin_post_nopriv_export_csv', 'handle_export_csv_request'); function handle_export_csv_request() { // Vérifier les autorisations ou les conditions nécessaires - if (!current_user_can('manage_options')) { + if (!current_user_can('read')) { wp_die('Access Denied'); } diff --git a/resources/js/stats-dashboard.js b/resources/js/stats-dashboard.js index 575b566..710b95f 100644 --- a/resources/js/stats-dashboard.js +++ b/resources/js/stats-dashboard.js @@ -1,107 +1,7 @@ import Chart from 'chart.js/auto'; import ChartDataLabels from 'chartjs-plugin-datalabels'; -function buildDataChart(level) { - const scoreDistribution = level.querySelectorAll( - '.game_stats__scores-distribution .score-data' - ); - const scoreDistributionDataset = []; - - for (const score of scoreDistribution) { - scoreDistributionDataset.push({ - label: '# of Votes', - score: score.getAttribute('score') + 'points', - count: score.getAttribute('count'), - }); - } - - // console.log('score', scoreDistribution); - // console.log(scoreDistributionDataset); - - const colorPalettes = [ - ['#1d4ed8', '#3b82f6', '#60a5fa', '#93c5fd', '#c3dafe'], - ['#1d4ed8', '#3c67dc', '#5b81e0', '#7a9be3', '#99b5e7'], - ['#1223C2', '#030E8E', '#3D49C7', '#6D77D0'], - ['#1d4ed8', '#3b82f6', '#60a5fa', '#93c5fd', '#c3dafe'], - ['#1240a1', '#2e6ee0', '#548bf7', '#81aefd', '#a9d2ff'], - ['#0a37a8', '#2d65d4', '#5794ff', '#7dbdff', '#a3dcff'], - ['#1a3d9c', '#4271d9', '#75a7ff', '#9fc5ff', '#c6e2ff'], - ['#084ba6', '#3a79d6', '#6ba5ff', '#96c3ff', '#badbff'], - ['#1137a3', '#3267d6', '#5fa3ff', '#8fc1ff', '#b5e0ff'], - ]; - - const dataTruc = { - labels: ['Red', 'Blue', 'Yellow'], - datasets: [ - { - label: 'My First Dataset', - data: [300, 50, 100], - backgroundColor: [ - 'rgb(255, 99, 132)', - 'rgb(54, 162, 235)', - 'rgb(255, 205, 86)', - ], - hoverOffset: 4, - }, - ], - }; - const data = { - labels: ['Red', 'Blue', 'Yellow'], - datasets: [ - { - label: 'My First Dataset', - data: [300, 50, 100], - backgroundColor: [ - 'rgb(255, 99, 132)', - 'rgb(54, 162, 235)', - 'rgb(255, 205, 86)', - ], - hoverOffset: 4, - }, - ], - }; - - new Chart( - document.getElementById('graphic-score-repartition'), - { - type: 'doughnut', - options: { - animation: true, - layout: { - padding: { - right: 50, - }, - }, - plugins: { - legend: { - position: 'bottom', - }, - tooltip: { - enabled: false, - }, - }, - }, - data: { - labels: scoreDistributionDataset.map( - (scoreData) => scoreData.score - ), - datasets: [ - { - label: scoreDistributionDataset.map( - (scoreData) => scoreData.count - ), - backgroundColor: colorPalettes[4], - data: scoreDistributionDataset.map( - (scoreData) => scoreData.score - ), - hoverOffset: 14, - }, - ], - }, - } - ); -} -function buildDataChart2(level) { +function buildLevelDataCharts(level) { const scoreDistribution = level.querySelectorAll( '.game_stats__scores-distribution .score-data' ); @@ -113,15 +13,6 @@ function buildDataChart2(level) { count: score.getAttribute('count'), }); } - // const data = [ - // { team: 'Red Jaguars', score: 8 }, - // { team: 'Blue Barracudas', score: 23 }, - // { team: 'Green Monkeys', score: 3 }, - // { team: 'Orange Iguanas', score: 15 }, - // { team: 'Purple Parrots', score: 7 }, - // { team: 'Silver Snakes', score: 1 }, - // ]; - // console.log('dta', data); const colorPalettes = [ ['#1d4ed8', '#3b82f6', '#60a5fa', '#93c5fd', '#c3dafe'], ['#1d4ed8', '#3c67dc', '#5b81e0', '#7a9be3', '#99b5e7'], @@ -189,25 +80,90 @@ function buildDataChart2(level) { } ); } +function buildRatingsDataChart() { + const ratingDistribution = document.querySelectorAll( + '.rating_stats .rating-data' + ); + const ratingDistributionDataset = []; + + for (const rating of ratingDistribution) { + ratingDistributionDataset.push({ + rating: rating.getAttribute('rating'), + count: rating.getAttribute('count'), + }); + } + const colorPalettes = [ + ['#1d4ed8', '#3b82f6', '#60a5fa', '#93c5fd', '#c3dafe'], + ['#1d4ed8', '#3c67dc', '#5b81e0', '#7a9be3', '#99b5e7'], + ['#1223C2', '#030E8E', '#3D49C7', '#6D77D0'], + ['#1d4ed8', '#3b82f6', '#60a5fa', '#93c5fd', '#c3dafe'], + ['#1240a1', '#2e6ee0', '#548bf7', '#81aefd', '#a9d2ff'], + ['#0a37a8', '#2d65d4', '#5794ff', '#7dbdff', '#a3dcff'], + ['#1a3d9c', '#4271d9', '#75a7ff', '#9fc5ff', '#c6e2ff'], + ['#084ba6', '#3a79d6', '#6ba5ff', '#96c3ff', '#badbff'], + ['#1137a3', '#3267d6', '#5fa3ff', '#8fc1ff', '#b5e0ff'], + ]; + + // console.log('score', scoreDistributionDataset); + new Chart( + document.getElementById('graphic-rating-repartition'), + { + type: 'pie', + data: { + labels: ratingDistributionDataset.map( + (row) => row.rating + ), + datasets: [ + { + backgroundColor: colorPalettes[4], + label: 'Nombres de joueurs', + data: ratingDistributionDataset.map( + (row) => row.count + ), + hoverOffset: 50, + }, + ], + }, + plugins: [ChartDataLabels], + options: { + plugins: { + legend: { + display: false, + position: 'bottom', + }, + + title: { + display: false, + text: 'Custom Chart Title', + }, + datalabels: { + color: '#fff', + anchor: 'center', + font: { + size: 20, + }, + formatter: (value, context) => { + return context.chart.data.labels[ + context.dataIndex + ]; + }, + + // align: 'center', + // offset: 10, + // borderWidth: 2, + // borderColor: '#fff', + // borderRadius: 25, + }, + }, + }, + } + ); +} window.addEventListener('DOMContentLoaded', (event) => { const levels = document.querySelectorAll('.game_stats'); levels.forEach((level) => { - buildDataChart2(level); - // function updatePercentage(percentage) { - // const overlay = document.querySelector( - // '.donut-chart__overlay' - // ); - // const text = document.querySelector( - // '.donut-chart__percentage' - // ); - // overlay.style.transform = `rotate(${ - // (percentage / 100) * 360 - // }deg)`; - // text.textContent = `${percentage}%`; - // } - - // // Exemple : mettre à jour le pourcentage à 75% - // updatePercentage(75); + buildLevelDataCharts(level); }); + buildRatingsDataChart(); }); diff --git a/template-parts/statistics/dashboard.php b/template-parts/statistics/dashboard.php index 942546e..b4bada1 100644 --- a/template-parts/statistics/dashboard.php +++ b/template-parts/statistics/dashboard.php @@ -1,6 +1,6 @@ get_col(" "); ?> +

+ +
-

+ + + @@ -64,7 +69,6 @@ $level_post_ids = $wpdb->get_col("

-
@@ -113,23 +117,50 @@ $level_post_ids = $wpdb->get_col(" - - - - get_results(" +
+ get_results(" SELECT rating, COUNT(rating) as count FROM $ratings_table_name GROUP BY rating "); + $average_app_rating = $wpdb->get_var(" + SELECT AVG(rating) + FROM $ratings_table_name + "); + ?> +

Répartition des notes de l'app

+ + + + + +
+

Note moyenne

+ + + +

+ +
+ + + +
+

Répartition des notes

+ +
    +
  • " count="2"> : joueurs
  • + +
  • rating ?> : count ?> joueurs
  • + +
  • " count="2"> : joueurs
  • +
+
- // echo '
';
-	// print_r($ratings);
-	// echo '
'; - ?> -
+ \ No newline at end of file