Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contributors report, and allow data to be downloaded. #267

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions plugins/wporg-5ftf/includes/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
add_action( 'admin_menu', __NAMESPACE__ . '\add_admin_pages' );
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'admin_init', __NAMESPACE__ . '\export_csv' );
add_action( 'admin_init', __NAMESPACE__ . '\export_contributors_csv' );

/**
* Register admin page.
Expand All @@ -30,6 +31,15 @@ function add_admin_pages() {
'5ftf_company_report',
__NAMESPACE__ . '\render_company_report_page'
);

add_submenu_page(
'edit.php?post_type=5ftf_pledge',
'Contributor Report',
'Contributor Report',
'manage_options',
'5ftf_contributor_report',
__NAMESPACE__ . '\render_contributor_report_page'
);
}

/**
Expand Down Expand Up @@ -150,6 +160,91 @@ function render_company_report_page() {
set_transient( 'wporg_5ftf_company_report_' . $status, $export_data, 60 );
}

/**
* Render results and download button.
*/
function render_contributor_report_page() {

$status = sanitize_title( $_GET['status'] ?? '' );
$contributor_limit = 1500;

if ( ! in_array( $status, array( 'pending', 'trash', 'publish' ) ) ) {
$status = 'all';
}

$contributors = get_posts( array(
'post_type' => '5ftf_contributor',
'post_status' => $status,
'posts_per_page' => $contributor_limit, // set 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 ( count( $contributors ) === $contributor_limit ) {
echo '<p>WARNING: Contributor limit reached, check the code query.</p>';
}

$all_contributor_data = XProfile\get_all_xprofile_contributors_indexed();
?>
<p>
<b>Total:</b><?php echo count( $contributors ); ?>
<b>Status:</b>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report">All</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=pending">Pending</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=publish">Publish</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=trash">Trash</a>
</p>

<form action="#" method="post">
<input type="hidden" name="wporg-5ftf-contr" value="1">
<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>">
<input type="submit" value="Export">
<?php wp_nonce_field( '5ftf_download_contributor_report' ); ?>
</form>
<table id="wporg-5ftf-company-report">
<tr>
<th>User id</th>
<th>Username</th>
<th>Company</th>
<th>Hours</th>
<th>Teams</th>
<th>Full Name</th>
<th>Email</th>
<th>Last login</th>
<th>Status</th>
</tr>
<?php
$export_data = array();
foreach ( $contributors as $c ) {
$pledge_company = get_post( $c->post_parent );
$pledge_company_title = get_the_title( $pledge_company ) ?? 'unattached';
$user_id = get_post_meta( $c->ID, 'wporg_user_id', true );
$xprofile = $all_contributor_data[ $user_id ] ?? [
'team_names' => [],
'hours_per_week' => 0,
];
$xprofile_teams = $xprofile['team_names'] ?? [];
$user = get_user_by( 'ID', $user_id );
$last_login = get_user_meta( $user_id, 'last_logged_in', true );
$teams = str_replace( ' Team', '', implode( ',', $xprofile_teams ) );
echo '<tr>';
echo '<td>' . absint( $user_id ) . '</td>';
echo '<td>' . esc_html( $c->post_title ) . '</td>';
echo '<td>' . esc_html( $pledge_company_title ) . '</td>';
echo '<td>' . esc_html( $xprofile['hours_per_week'] ) . '</td>';
echo '<td>' . esc_html( $teams ) . '</td>';
echo '<td>' . esc_html( $user->display_name ) . '</td>';
echo '<td>' . esc_html( $user->user_email ) . '</td>';
echo '<td>' . esc_html( $last_login ) . '</td>';
echo '<td>' . esc_html( $c->post_status ) . '</td>';
echo '</tr>';
$export_data[] = array( $user_id, $c->post_title, $pledge_company_title, $xprofile['hours_per_week'], $teams, $user->display_name, $user->user_email, $last_login, $c->post_status );
}
echo '</table>';

set_transient( 'wporg_5ftf_contributor_report_' . $status, $export_data, 2 * MINUTE_IN_SECONDS );
}
/**
* CSV export runner, grabs data lazily from a transient.
*/
Expand All @@ -175,3 +270,29 @@ function export_csv() {

$exporter->emit_file();
}

/**
* Export contributors as a CSV, also from transient.
*/
function export_contributors_csv() {

if (
! isset( $_POST['wporg-5ftf-contr'] ) ||
! current_user_can( 'manage_options' ) ||
! wp_verify_nonce( $_POST['_wpnonce'], '5ftf_download_contributor_report' )
) {
return;
}

$status = $_POST['status'];

$data = get_transient( 'wporg_5ftf_contributor_report_' . $status );

$exporter = new Export_CSV( array(
'filename' => 'contributor-report-' . $status,
'headers' => array( 'User id', 'Username', 'Company', 'Hours', 'Teams', 'Full Name', 'Email', 'Last Login', 'Status' ),
'data' => $data,
) );

$exporter->emit_file();
}
20 changes: 20 additions & 0 deletions plugins/wporg-5ftf/includes/xprofile.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ function get_all_xprofile_contributor_hours_teams(): array {
return $users;
}

/**
*
* Reconfigures xprofile data to be in indexed array.
*
* @return array
*/
function get_all_xprofile_contributors_indexed(): array {
$all_data = get_all_xprofile_contributor_hours_teams();

$newdata = array();
foreach ( $all_data as $contributor ) {
$newdata[ $contributor->user_id ] = [
'hours_per_week' => $contributor->hours_per_week,
'team_names' => $contributor->team_names,
];
}

return $newdata;
}

/**
* Pull relevant data from profiles.wordpress.org.
*
Expand Down
Loading