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

Recalculate new contributor #375

Merged
merged 7 commits into from
Dec 10, 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
2 changes: 1 addition & 1 deletion includes/attendee/attendee-adder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private function import_stats( Event $event, Attendee $attendee ): void {
// phpcs:enable
}

private function check_is_new_contributor( Event $event, int $user_id ): bool {
public function check_is_new_contributor( Event $event, int $user_id ): bool {
global $wpdb, $gp_table_prefix;

// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
Expand Down
40 changes: 38 additions & 2 deletions includes/attendee/attendee-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Wporg\TranslationEvents\Attendee;

use Exception;
use Wporg\TranslationEvents\Attendee\Attendee_Adder;
use Wporg\TranslationEvents\Event\Event_Repository;
use Wporg\TranslationEvents\Translation_Events;
use DateTimeImmutable;
use DateTimeZone;

class Attendee_Repository {

Expand Down Expand Up @@ -47,8 +52,9 @@ public function update_attendee( Attendee $attendee ): void {
$wpdb->update(
"{$gp_table_prefix}event_attendees",
array(
'is_host' => $attendee->is_host() ? 1 : 0,
'is_remote' => $attendee->is_remote() ? 1 : 0,
'is_host' => $attendee->is_host() ? 1 : 0,
'is_remote' => $attendee->is_remote() ? 1 : 0,
'is_new_contributor' => $attendee->is_new_contributor() ? 1 : 0,
),
array(
'event_id' => $attendee->event_id(),
Expand Down Expand Up @@ -291,4 +297,34 @@ function ( Attendee $attendee ) {
}
);
}

/**
* Check the attendees if they are no longer new contributors and update
*/
public function recheck_new_contributor_status( int $event_id ) {
// Get all attendees marked as new contributors.
$new_contributors = array_filter(
$this->get_attendees( $event_id ),
function ( Attendee $attendee ) {
return $attendee->is_new_contributor();
}
);

if ( empty( $new_contributors ) ) {
return;
}

$now = Translation_Events::now();
$attendee_adder = new Attendee_Adder( $this );
$event = ( new Event_Repository( $now, $this ) )->get_event( $event_id );
if ( ! $event ) {
return;
}
foreach ( $new_contributors as $attendee ) {
if ( ! $attendee_adder->check_is_new_contributor( $event, $attendee->user_id() ) ) {
$attendee->mark_as_active_contributor();
$this->update_attendee( $attendee );
}
}
}
}
4 changes: 4 additions & 0 deletions includes/attendee/attendee.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function mark_as_new_contributor(): void {
$this->is_new_contributor = true;
}

public function mark_as_active_contributor(): void {
$this->is_new_contributor = false;
}

public function mark_as_remote_attendee(): void {
$this->is_remote = true;
}
Expand Down
9 changes: 9 additions & 0 deletions includes/notifications/notifications-send.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ public function __construct(
$this->event_repository = $event_repository;
$this->attendee_repository = $attendee_repository;
add_action( 'wporg_gp_translation_events_email_notifications_1h', array( $this, 'send_notifications' ), 10, 1 );
add_action( 'wporg_gp_translation_events_email_notifications_1h', array( $this, 'recalculate_new_contributor' ), 10, 1 );
add_action( 'wporg_gp_translation_events_email_notifications_24h', array( $this, 'send_notifications' ), 10, 1 );
}

public function recalculate_new_contributor( int $post_id ) {
$event = $this->event_repository->get_event( $post_id );
if ( null === $event ) {
return;
}
$this->attendee_repository->recheck_new_contributor_status( $event->id() );
}

/**
* Send notifications to the attendees of the event.
*
Expand Down
43 changes: 41 additions & 2 deletions tests/attendee/attendee-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
use Wporg\TranslationEvents\Attendee\Attendee;
use Wporg\TranslationEvents\Attendee\Attendee_Repository;
use Wporg\TranslationEvents\Tests\Stats_Factory;
use Wporg\TranslationEvents\Tests\Event_Factory;
use Wporg\TranslationEvents\Tests\Translation_Factory;

class Attendee_Repository_Test extends Base_Test {
private Attendee_Repository $repository;
private Stats_Factory $stats_factory;
private Event_Factory $event_factory;
private Translation_Factory $translation_factory;


public function setUp(): void {
parent::setUp();
$this->repository = new Attendee_Repository();
$this->stats_factory = new Stats_Factory();
$this->repository = new Attendee_Repository();
$this->stats_factory = new Stats_Factory();
$this->event_factory = new Event_Factory();
$this->translation_factory = new Translation_Factory( $this->factory );
}

public function test_add_attendee_invalid_event_id() {
Expand Down Expand Up @@ -261,6 +268,38 @@ public function test_get_attendees_not_contributing() {
$this->assertFalse( $attendees[ $user3_id ]->is_host() );
}

public function test_recheck_new_contributor_status() {
$user1_id = 42;
$user2_id = 43;

$event1_id = $this->event_factory->create_inactive_future( $this->now );

// Attendee, new contributor.
$attendee11 = new Attendee( $event1_id, $user1_id, true, true );
$this->repository->insert_attendee( $attendee11 );

// Attendee, existing contributor.
$attendee12 = new Attendee( $event1_id, $user2_id, false, false, array( 'aa' ) );
$this->stats_factory->create( $event1_id, $user2_id, 1, 'create' );
$this->repository->insert_attendee( $attendee12 );

$attendees = $this->repository->get_attendees( $event1_id );
$this->assertCount( 2, $attendees );
$this->assertEquals( $attendee11, $attendees[ $user1_id ] );
$this->assertEquals( $attendee12, $attendees[ $user2_id ] );
$this->assertTrue( $attendees[ $user1_id ]->is_new_contributor() );
$this->assertFalse( $attendees[ $user2_id ]->is_new_contributor() );

for ( $i = 1; $i < 12; $i++ ) {
$this->translation_factory->create( $user1_id );
}
$this->repository->recheck_new_contributor_status( $event1_id );
$attendees = $this->repository->get_attendees( $event1_id );

$this->assertFalse( $attendees[ $user1_id ]->is_new_contributor() );
$this->assertFalse( $attendees[ $user2_id ]->is_new_contributor() );
}

private function all_table_rows(): array {
global $wpdb, $gp_table_prefix;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
Expand Down
Loading