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

Register an user to attend or un-attend to an event #60

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 1 addition & 5 deletions assets/css/translation-events.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
width: 10%;
vertical-align: top;
}
.translation-event-form #event-title,
.translation-event-form #event-title,
.translation-event-form #event-description {
width: 30%;
}
Expand Down Expand Up @@ -40,10 +40,6 @@
float: left;
padding: 1em;
}
button#join-event {
width: 100%;
display: block;
}
.event-details-stats {
clear: both;
margin: 1em;
Expand Down
51 changes: 46 additions & 5 deletions includes/class-wporg-gp-translation-events-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @package wporg-gp-translation-events
*/
class WPORG_GP_Translation_Events_Route extends GP_Route {

public const USER_META_KEY_ATTENDING = 'translation-events-attending';

/**
* WPORG_GP_Translation_Events_Route constructor.
Expand Down Expand Up @@ -106,15 +106,19 @@ public function events_edit( $event_id ) {
* @return void
*/
public function events_details( $event_slug ) {
$user = wp_get_current_user();
$event = get_page_by_path( $event_slug, OBJECT, 'event' );
if ( ! $event ) {
$this->die_with_404();
}

$event_title = $event->post_title;
$event_description = $event->post_content;
$event_start = get_post_meta( $event->ID, '_event_start', true ) ?? '';
$event_end = get_post_meta( $event->ID, '_event_end', true ) ?? '';
$event_id = $event->ID;
$event_title = $event->post_title;
$event_description = $event->post_content;
$event_start = get_post_meta( $event->ID, '_event_start', true ) ?? '';
$event_end = get_post_meta( $event->ID, '_event_end', true ) ?? '';
amieiro marked this conversation as resolved.
Show resolved Hide resolved
$attending_event_ids = get_user_meta( $user->ID, self::USER_META_KEY_ATTENDING, true ) ?: array();
$user_is_attending = isset( $attending_event_ids[ $event_id ] );

$stats_calculator = new WPORG_GP_Translation_Events_Stats_Calculator();
try {
Expand All @@ -127,6 +131,43 @@ public function events_details( $event_slug ) {
$this->tmpl( 'event', get_defined_vars() );
}

/**
* Toggle whether the current user is attending an event.
* If the user is not currently marked as attending, they will be marked as attending.
* If the user is currently marked as attending, they will be marked as not attending.
*
* @param int $event_id
*/
public function events_attend( int $event_id ) {
$user = wp_get_current_user();
if ( ! $user ) {
$this->die_with_error( 'Only logged-in users can attend events', 403 );
}

$event = get_post( $event_id );
if ( ! $event ) {
$this->die_with_404();
}

$event_ids = get_user_meta( $user->ID, self::USER_META_KEY_ATTENDING, true ) ?? [];
if ( ! $event_ids ) {
$event_ids = [];
}

if ( ! isset( $event_ids[ $event_id ] ) ) {
// Not yet attending, mark as attending.
$event_ids[ $event_id ] = true;
} else {
// Currently attending, mark as not attending.
unset( $event_ids[ $event_id ] );
}

update_user_meta( $user->ID, self::USER_META_KEY_ATTENDING, $event_ids );

wp_safe_redirect( gp_url( "/events/$event->post_name" ) );
exit;
}

/**
* Convert date time stored in UTC to a date time in a time zone.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ function ( $event ) use ( $at ) {
private function select_events_user_is_registered_for( array $events, int $user_id ): array {
return array_filter(
$events,
function ( WPORG_GP_Translation_Events_Event $event ) {
// TODO.
return true;
function ( $event ) use ( $attending_event_ids ) {
return isset( $attending_event_ids[ $event->ID ] );
}
);
}
Expand Down
8 changes: 7 additions & 1 deletion templates/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
</div>
<?php if ( is_user_logged_in() ) : ?>
<div class="event-details-join">
<button class="button is-primary" id="join-event">Attend Event</button>
<form class="event-details-attend" method="post" action="<?php echo esc_url( gp_url( "/events/attend/$event_id" ) )?>">
<?php if ( ! $user_is_attending ): ?>
<input type="submit" class="button is-primary" value="Attend Event"/>
<?php else: ?>
<input type="submit" class="button is-secondary" value="You're attending"/>
<?php endif ?>
</form>
</div>
<?php endif; ?>
</div>
Expand Down
1 change: 1 addition & 0 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ function() {
GP::$router->add( '/events?', array( 'WPORG_GP_Translation_Events_Route', 'events_list' ), 'get' );
GP::$router->add( '/events/new', array( 'WPORG_GP_Translation_Events_Route', 'events_create' ), 'get' );
GP::$router->add( '/events/edit/(\d+)', array( 'WPORG_GP_Translation_Events_Route', 'events_edit' ), 'get' );
GP::$router->add( '/events/attend/(\d+)', array( 'WPORG_GP_Translation_Events_Route', 'events_attend' ), 'post' );
GP::$router->add( '/events/([a-z0-9_-]+)', array( 'WPORG_GP_Translation_Events_Route', 'events_details' ), 'get' );

require_once __DIR__ . '/includes/class-wporg-gp-translation-events-event.php';
Expand Down