Support

Home Forums Event Espresso Premium Fetching all the answers for an attendee

Fetching all the answers for an attendee

Posted: January 30, 2015 at 7:28 am

Viewing 5 reply threads


tigerton

January 30, 2015 at 7:28 am

Hi!

I’ve built a custom CSV exporter plugin for EE and now I’m trying to fetch all the answers of an attendee.

I’ve found get_registrations() inside EE_answers.class.php which seems to be part of the answer.. But basically. I have fetched the attendees information manually (the basic info in esp_attendee_meta table) with a SQL query and in the looping of each attendee I now need to fetch all their answers to questions for the event.

I have at my disposal the ATT_ID, REG_ID and of course the event ID (and sometimes a Ticket ID).

I assume I can’t use the get_registrations() function unless I have an attendee object from EE so I guess I’ll first need to get the attendee object and then all registrations. Then Filter out the registrations to only get the ones for the specific Event (unless there’s a better function which gets just the registrations for the attendee and specific event).

Any help here will be very appreciated! I know this is a bit beyond the regular “How do I show my events on the frontpage” support ticket ๐Ÿ˜‰


Michael Nelson

  • Support Staff

January 30, 2015 at 1:20 pm

This article we did on using the EE models for querying will probably be helpful: http://developer.eventespresso.com/docs/model-querying/.

It sounds like you really want the **registration’s** answers, not the attendee’s. Keep in mind that a registration is a record for ONE attendee at ONE event. You said you already have the REG_ID so I think that’s all you’ll need.

But the short answer:
to get all the answers for a specific registration you could run
$answers_for_reg = EEM_Answer::instance()->get_all( array( array( 'REG_ID' => $reg_id ) ) );
That will return an array of EE_Answer objects for that particular registration.

If you want to get all the answers for a particular attendee at ALL the events they’ve ever attended, you would run this similar query:
$answers_to_all_regs_for_attendee = EEM_Answer::instance()->get_all( array( array( 'Registration.ATT_ID' => $att_id ) );


tigerton

January 30, 2015 at 2:48 pm

Hi Mike,

Thank you for that enlightening answer ๐Ÿ™‚ I did not know about this since last time i checked your in-progress site developer.* there wasn’t much there ๐Ÿ˜‰

I have no problem fetching the record for one attendee at one event! Since I already have the attendees base info. Do I need to use your helper functions to be able to fetch the information from the EE_Answer objects (like for an event object) or can I access then directly?

It’s really nice that you’re improving your documentation like this! A little feedback tho would be to more clearly link people to these articles (or just the developer. site in general) from your regular site. These kinds of articles are gold for a developer like me that needs to do custom functionality.

D.S.


Michael Nelson

  • Support Staff

January 30, 2015 at 3:18 pm

re

Do I need to use your helper functions to be able to fetch the information from the EE_Answer objects (like for an event object) or can I access then directly?

Sorry I don’t totally understand. Do you mean you’re wondering if you need to use EE_Answer::value() or EE_Answer::pretty_value() to get at the answer’s value, as opposed to using EE_Answer::get('ANS_value') or EE_Answer::get_pretty('ANS_value')? No you don’t need to. Those helper methods are just there for convenience because it’s for some IDEs to easily see what methods are available – but there’s no obligation to use them at all.

Does that answer your question?

And yeah I’ll suggest we make the developer documentation more visible.


tigerton

February 2, 2015 at 3:21 am

sort of. I’m not used to the return object being an object with protected fields etc. so I have to run functions on this to fetch the values.

But it worked out and I now have the question labels and answers per attendee per event. Now comes the next “challenge”.

Since I’m building this as a XML where there need to be a “head” column at the top of each value I also need to fetch all the possible questions (regardless of attendee specific answers/non-answers) for an event. So that I can loop through them and output each question as a column of it’s own.

Any ideas where to look (or possibly an answer straight out)? ๐Ÿ™‚
Doesn’t seem to be anything in the ee_registration class.
Do I have to go through the EE_Event class to fetch all question groups, then through EE_Question_group to get all questions and then EE_Question to fetch its label and values?

Another related question. How is this in terms of performance? It’s possible my client will want to export say 200-300 records at once and I’m afraid it will hit the server timed out limit with all these function calls and (I presume) queries.

Thank you for all the help! It’s nice to see a support-forum where the staff actually have some technical knowledge


Michael Nelson

  • Support Staff

February 2, 2015 at 12:43 pm

Since Iโ€™m building this as a XML where there need to be a โ€œheadโ€ column at the top of each value I also need to fetch all the possible questions (regardless of attendee specific answers/non-answers) for an event. So that I can loop through them and output each question as a column of itโ€™s own.

I’d suggest looking at how we do the registrations report CSV in the file core/db_classes/EE_Export.class.php in the EE_Export::report_registrations_for_event() method. You’ll see that after we fetched all the registrations for the event(s), before writing any of them out we needed to ascertain what question columns to add to the CSV report, and then afterwards we started filling out the rows of registrations.

re

How is this in terms of performance?

Creating all these objects and accessing all their properties via methods is convenient for developers but not terribly efficient. The objects do try to cache certain results from the queries they do behind-the-scenes (eg, calling $registration->attendee() will only run a database query the first time it’s called, subsequent times it will re-use the first result). But yes, if you’re going to be exporting hundreds of rows then timing out will be an issue (unless your client is on their own server in which case they can set the PHP timeout), so this is a case where you may decide to avoid using the models and model objects.
You may decide to only use part of the models system to fetch the data, but not to create the model objects. To do this, you could use the protected method EEM_Base::_get_all_wpdb_results() (by using MethodReflection; we’ve been debatting back-and-forth on whether to make that method public) to only get a lighter array of results, or you might use EEM_Base::get_col() to just get a specific column you might want.

Viewing 5 reply threads

The support post ‘Fetching all the answers for an attendee’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso