From 78d22ad1040a91fce976b7a3711d7457c5c8e541 Mon Sep 17 00:00:00 2001 From: Paul Kevan Date: Tue, 21 May 2024 11:52:40 +0100 Subject: [PATCH] Add reports and styling --- plugins/wporg-5ftf/assets/css/admin.css | 14 ++ plugins/wporg-5ftf/includes/reports.php | 180 ++++++++++++++++++++++++ plugins/wporg-5ftf/index.php | 1 + 3 files changed, 195 insertions(+) create mode 100644 plugins/wporg-5ftf/includes/reports.php diff --git a/plugins/wporg-5ftf/assets/css/admin.css b/plugins/wporg-5ftf/assets/css/admin.css index 0645c5b..acb98c8 100755 --- a/plugins/wporg-5ftf/assets/css/admin.css +++ b/plugins/wporg-5ftf/assets/css/admin.css @@ -75,3 +75,17 @@ .contributor-list .button-link-delete .dashicons { margin-top: -2px; } + +table#wporg-5ftf-company-report, #wporg-5ftf-company-report th, #wporg-5ftf-company-report td { + border: 1px solid #000; + border-collapse: collapse; + padding: 5px; +} + + + +#wporg-5ftf-company-report td.center { + text-align: center; +} + + diff --git a/plugins/wporg-5ftf/includes/reports.php b/plugins/wporg-5ftf/includes/reports.php new file mode 100644 index 0000000..e8d52bd --- /dev/null +++ b/plugins/wporg-5ftf/includes/reports.php @@ -0,0 +1,180 @@ +id ) { + wp_enqueue_style( '5ftf-admin' ); + } + } +} + +/* + * Render results and download button. + */ +function render_company_report_page() { + + $status = sanitize_title( $_GET['status'] ); + + if ( ! in_array( $status, [ 'draft', '5ftf-deactivated', 'publish' ] ) ) { + $status = 'all'; + } + + $pledges = get_posts( [ + 'post_type' => '5ftf_pledge', + 'post_status' => $status, + 'posts_per_page' => 250, // set to 250 to avoid unexpected memory overuse. + 'orderby' => 'post_title', + 'order' => 'ASC', + ] ); + + // Add visible warning on page if we hit the upper limit of the query. + if ( 250 == count( $pledges ) ) { + echo '

WARNING: pledge limit reached, check the code query.

'; + } +?> + +

+ Total: + Status: + All + Draft + Publish + Deactivated +

+ +
+ + + + +
+ + + + + + + + + + + + + + + +ID, '5ftf_org-domain', true ); + $pledge_url = get_post_meta( $pledge->ID, '5ftf_org-url', true ); + $email = get_post_meta( $pledge->ID, '5ftf_org-pledge-email', true ); + $date_created = substr( $pledge->post_date, 0, 10 ); + $date_modified = substr( $pledge->post_modified, 0, 10 ); + + $team = XProfile\get_aggregate_contributor_data_for_pledge( $pledge->ID ); + $hours = $team['hours']; + $contributors = $team['contributors']; + + $all_contributors += $contributors; + $users = Contributor\get_pledge_contributors( $pledge->ID, 'publish' ); + $wporg_profiles = wp_list_pluck( $users, 'post_title' ); + + $usernames = implode( ', ', $wporg_profiles ); + $teams = implode( ', ', str_replace( ' Team', '', $team[ 'teams' ] ) ); + + echo ''; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + echo ' '; + + echo ''; + $export_data[] = [ $pledge->post_title, $pledge->post_status, $hours, $contributors, $usernames, $teams, $company_url, $pledge_url, $email, $date_created, $date_modified ]; + + } + echo '
CompanyStatusTotal HoursContributorsUsernamesTeam(s)URLPledge URLEmailPledge createdPledge updated
' . esc_html( $pledge->post_title ) . '' . esc_html( $pledge->post_status ) . '' . esc_html( $hours ) . '' . esc_html( $contributors ) . '' . esc_html( $usernames ) . '' . esc_html( $teams ) . '' . esc_html( $company_url ) . '' . esc_html( $pledge_url ) . '' . esc_html( $email ). '' . esc_html( $date_created ) . '' . esc_html( $date_modified ) . '
'; + echo '

Total contributors: ' . esc_html( $all_contributors ) . '

'; + + // Sets a transient to avoid double data lookup for export, might need to adjust timeout to longer. + set_transient( 'wporg_5ftf_company_report_' . $status, $export_data, 60 ); +} + +/* + * CSV export runner, grabs data lazily from a transient. + * @param $data array. + */ +function export_csv() { + + if ( + ! isset( $_POST['wporg-5ftf-cr'] ) || + ! current_user_can( 'manage_options' ) || + ! wp_verify_nonce( $_POST['_wpnonce'], '5ftf_download_company_report' ) + ) { + return; + } + + $status = $_POST['status']; + + $data = get_transient( 'wporg_5ftf_company_report_' . $status ); + + $exporter = new Export_CSV( array( + 'filename' => 'company-report-' . $status, + 'headers' => array( 'Company', 'Status', 'Hours', 'Contributors', 'Users', 'Teams', 'Company URL', 'Pledge URL', 'Email', 'Created', 'Last updated' ), + 'data' => $data, + ) ); + + $exporter->emit_file(); +} diff --git a/plugins/wporg-5ftf/index.php b/plugins/wporg-5ftf/index.php index 6cda33a..9b91fa6 100755 --- a/plugins/wporg-5ftf/index.php +++ b/plugins/wporg-5ftf/index.php @@ -33,6 +33,7 @@ function load() { require_once get_includes_path() . 'pledge.php'; require_once get_includes_path() . 'pledge-form.php'; require_once get_includes_path() . 'pledge-meta.php'; + require_once get_includes_path() . 'reports.php'; require_once get_includes_path() . 'stats.php'; require_once get_includes_path() . 'xprofile.php';