Okay
  Public Ticket #2176576
User insights
Closed

Comments

  •  3
    Brewstar started the conversation

    Hi,

    I have bought a plugin called 'User insights' and it gives all information about my users.

    There is a way to show extra information about my users (from other plugins) by creating custom fields. I would like to hook it up with the Event Calender, for instance: how many times a user attended an event.

    I couldn't find anything/ meta keys (yet) in mysql database.... 

    Can you help me out?


    Thanks!

    Jochem



  •  784
    Zhivko replied

    Hi,

    The attendance data in mysql is OpenSSL encrypted and can't be read directly.


    I've written a small function that obtains attendance data for each users:

    function calendar_get_users_attendances() {
        $users_attendances = array();
        $attended_events = \Stachethemes\Stec\Events::get_events(array(
                    'meta_query' => array(
                        'key'     => 'attendance',
                        'value'   => '',
                        'compare' => '!='
                    )
        ));     foreach ($attended_events as $event) {
            $attendees = $event->get_attendance(true);
            foreach ($attendees as $attendee) {             if (is_nan($attendee->get_userid())) {
                    continue;
                }             $user_attend_dates = array();             foreach ($attendee->get_status() as $date => $status) {
                    if (1 === $status) {
                        $user_attend_dates[] = $date;
                    }
                }             $users_attendances[$attendee->get_userid()][$event->get_id()] = array(
                    'event_id'     => $event->get_id(),
                    'event_title'  => $event->get_title(),
                    'count'        => count($user_attend_dates),
                    'attend_dates' => implode(', ', $user_attend_dates)
                );
            }
        }     return $users_attendances;
    }


    Example:

    $users_attendances = calendar_get_users_attendances();
    print_r($users_attendances);

    will return something like this:

    Array
    (
        [1] => Array
            (
                [4505] => Array
                    (
                        [event_id] => 4505
                        [event_title] => Concert
                        [count] => 3
                        [attend_dates] => 2019-10-13, 2019-10-20, 2019-10-27
                    )         ) )


    Here's example how to process this data:

        $users_attendances = calendar_get_users_attendances();
        $html = array();
        foreach ($users_attendances as $userid => $attended_events) {
            $html[] = sprintf('User ID: %d', $userid);
            foreach ($attended_events as $event) {
                if ($event['attend_dates']) {
                    $html[] = sprintf('Event name: %s', $event['event_title']);
                    $html[] = sprintf('Attend dates: %s', $event['attend_dates']);
                    $html[] = sprintf('Attends count: %d', $event['count']);
                }
            }
            $html[] = '<hr>';
        }
        echo implode('<br>', $html);

    will return:

    User ID: 1
    Event name: Concert
    Attend dates: 2019-10-13, 2019-10-20, 2019-10-27
    Attends count: 3
    ... ... ... etc...

    I hope this helps you out.


    Stachethemes Developer

  •  3
    Brewstar replied

    Thank you so much! Only one more question... How do I place this code into/ get this output on my Wordpress site? I would like to know more about coding I just never find the time ;-)

    High regards,

    Jochem

  •  3
    Brewstar replied

    Ok, think I did it: used a code snippet for the function and inserted the php code (via php plugin) on a page! Is there a way to show them in a different order: user ID starting from low to high? And get the First and Last name in there too?

  •  784
    Zhivko replied

    Try this  example:

        $users_attendances = calendar_get_users_attendances();
    
        ksort($users_attendances); // Sort array by key value
        $html              = array();
        foreach ($users_attendances as $userid => $attended_events) {         $usermeta = get_user_meta($userid);
            $html[] = sprintf('User ID: %d', $userid);
            $html[] = sprintf('User Name: %s %s', $usermeta['first_name'][0], $usermeta['last_name'][0]);
            foreach ($attended_events as $event) {
                if ($event['attend_dates']) {
                    $html[] = sprintf('Event name: %s', $event['event_title']);
                    $html[] = sprintf('Attend dates: %s', $event['attend_dates']);
                    $html[] = sprintf('Attends count: %d', $event['count']);
                }
            }
            $html[] = '<hr>';
        }
        echo implode('<br>', $html);

    Stachethemes Developer

  •  3
    Brewstar replied

    Great! Thank you so much!

  •  3
    Brewstar replied

    And how can I show all events and the members (by first and last name) attending these events? Plus a total count.

    High regards,

    Jochem

  •  784
    Zhivko replied
        $events = \Stachethemes\Stec\Events::get_events();
        $html_ar = array();
        foreach ($events as $event) {
            $html_ar[] = sprintf('Event title: %s', $event->get_title());
            $attendees = $event->get_parsed_attendees();         foreach ($attendees as $attendee) {
                $html_ar[] = sprintf('Attendee name: %s', $attendee->name);
            }         $html_ar[] = sprintf('Total attendees: %d', count($attendees));         $html_ar[] = '<hr>';
        }
        
        echo implode('<br>', $html_ar);

    Stachethemes Developer