Widget to Show a Specific User's Events

Team WPMU,

Currently, BP Group Calendar has two widgets: Events for Logged in Users (User's Group Events) and Single Group Events.

User's Group Events shows the events of any logged in user who sees that widget -- their own events, so each user sees something different. I can see how this would be very helpful inside of the BP Network.

I'm looking for a widget that works outside of BP an in the user's individual websites. I would like them to have a widget that tells the world -- these are MY events. The purpose here is to show any visitors to their site that they have some events and that perhaps, someone would like to go, too.

Is this possible? And, if not, can that capability be added to this plugin?

Thanks,
Rob

    Patrick

    Hi there @jetmac

    I hope you're having a great day!

    I believe that should be possible currently with a bit of custom tweaking to the existing widgets.

    That is because they both work just fine on subsites of the same multisite install, as long as both BuddyPress and Group Calendar plugins are network-activated.

    So, getting a widget to display only those events for a specified user should not be that difficult for a competent developer.

    However, I am not as competent a developer as I'd like to be, lol.

    So I've asked for some insight from our 2nd-level support team on this isea.

    Hoang Ngo

    Hi @jetmac,

    I hope you are well today.

    Here is a code for a widget, which base on the plugin BuddyPress Group Calendar, this will let you decide which user's group event to display instead of logged in user.

    Please add this code to your theme functions.php or use mu-plugins

    class BP_Group_Calendar_Widget_User_Groups_Defined extends WP_Widget
    {
    
        function BP_Group_Calendar_Widget_User_Groups_Defined()
        {
            $widget_ops = array(
                'classname' => 'bp_group_calendar_user_groups_defined',
                'description' => __('Displays upcoming group events for an user\'s groups.', 'groupcalendar')
            );
            $this->WP_Widget('bp_group_calendar_user_groups_defined', __('User\'s Group Events Ext', 'groupcalendar'), $widget_ops);
        }
    
        function widget($args, $instance)
        {
            global $wpdb, $current_user, $bp;
            //only show widget to logged in users
    
            //get the groups the user is part of
            $results = groups_get_user_groups($instance['user_id']);
    
            //don't show widget if user doesn't have any groups
            if ($results['total'] == 0) {
                return;
            }
    
            extract($args);
    
            echo $before_widget;
            $title = $instance['title'];
            if (!empty($title)) {
                echo $before_title . apply_filters('widget_title', $title) . $after_title;
            };
    
            $group_ids = implode(',', $results['groups']);
    
            $events = $wpdb->get_results("SELECT gc.id, gc.user_id, gc.event_title, gc.event_time, gp.name, gc.group_id FROM " . $wpdb->base_prefix . "bp_groups_calendars gc JOIN " . $wpdb->base_prefix . "bp_groups gp ON gc.group_id=gp.id WHERE gc.event_time >= '" . current_time('mysql') . "' AND gp.id IN ($group_ids) ORDER BY gc.event_time ASC LIMIT " . (int)$instance['num_events']);
    
            if ($events) {
    
                echo '<ul class="events-list">';
                //loop through events
                $events_list = '';
                foreach ($events as $event) {
                    $class = ($event->user_id == $current_user->ID) ? ' class="my_event"' : '';
                    $events_list .= "\n<li" . $class . ">";
                    $group = groups_get_group(array('group_id' => $event->group_id));
                    $url = bp_get_group_permalink($group) . 'calendar/event/' . $event->id . '/';
                    $events_list .= stripslashes($event->name) . '<br /><a href="' . $url . '" title="' . __('View Event', 'groupcalendar') . '">' . bgc_date_display($event->event_time) . ': ' . stripslashes($event->event_title) . '</a>';
                    $events_list .= "</li>";
                }
                echo $events_list;
                echo "\n</ul>";
    
            } else {
                ?>
                <div class="widget-error">
                    <?php _e('There are no upcoming events for your groups.', 'groupcalendar') ?>
                </div>
            <?php } ?>
    
            <?php echo $after_widget; ?>
        <?php
        }
    
        function update($new_instance, $old_instance)
        {
            $instance = $old_instance;
            $instance['title'] = strip_tags($new_instance['title']);
            $instance['num_events'] = strip_tags($new_instance['num_events']);
            $instance['user_id'] = $new_instance['user_id'];
            return $instance;
        }
    
        function form($instance)
        {
            $instance = wp_parse_args((array)$instance, array(
                'title' => __('Your Upcoming Group Events', 'groupcalendar'),
                'num_events' => 10,
                'user_id' => 0
            ));
            $title = strip_tags($instance['title']);
            $num_events = strip_tags($instance['num_events']);
            $user_id = $instance['user_id']
            ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'groupcalendar') ?> <input
                        class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                        name="<?php echo $this->get_field_name('title'); ?>" type="text"
                        value="<?php echo esc_attr($title); ?>"/></label></p>
            <p><label
                    for="<?php echo $this->get_field_id('user_id'); ?>"><?php _e('User:', 'groupcalendar') ?>
                    <?php  wp_dropdown_users(array(
                        'name' => $this->get_field_name('user_id'),
                        'id' => $this->get_field_id('user_id'),
                        'selected' => $user_id
                    )) ?>
            <p><label
                    for="<?php echo $this->get_field_id('num_events'); ?>"><?php _e('Number of Events:', 'groupcalendar') ?>
                    <input class="widefat" id="<?php echo $this->get_field_id('num_events'); ?>"
                           name="<?php echo $this->get_field_name('num_events'); ?>" type="text"
                           value="<?php echo esc_attr($num_events); ?>" style="width: 30%"/></label></p>
        <?php
        }
    }
    
    add_action('widgets_init', create_function('', 'return register_widget("BP_Group_Calendar_Widget_User_Groups_Defined");'));

    Best regards,
    Hoang Ngo