Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
in Q2A Core by
Basically I am adding Gravatar support to my the question and answer view and I need to be able to extract the userid of the author of the post (be it a question, answer or comment) so I can call the following function to get their email address:

qa_db_single_select($qa_db, qa_db_user_account_selectspec($userid, true));

Does anyone know a simple way of doing this?

1 Answer

0 votes
Best answer
Currently the raw userid of the author of the post is not passed through to the theme layer. But it is available in qa_post_html_fields(...) in qa-app-format.php, as $post['userid'], so you could do something with it there.

But a better way would be to modify qa_db_posts_basic_selectspec(...) in qa-db-selects.php to extract the author's email address as part of the main query. That will save you having to run lots of individual queries to get the email address of the author for each post. Try adding this:

$selectspec['columns']['email']='BINARY ^users.email';

... after ...

$selectspec['columns']['handle']='BINARY ^users.handle';

Having done that you should be able to access $post['email'] directly from qa_post_html_fields(...) and can use that to add the gravatar HTML as a prefix or suffix to $fields['who']['data'] after it is assigned by the call to qa_who_to_html(...)
Ok I added $selectspec['columns']['email']='BINARY ^users.email'; to qa_db_posts_basic_selectspec(...) function which I copied to my qa-theme.php.

I am having a little difficulty understanding the latter part of your solution as I am unsure which functions I need to modify in order to output the following HTML:

<img src="'.$post['email'], 52, "mm", "g", false).'" class="gravatar" alt="Gravatar" width="52" height="52" />'

before the HTML for:

<span class="qa-q-view-what">asked</span>


<span class="qa-a-item-what">answered</span>

Much appreciated for your help so far. Thanks :-)
First of all you'll need to modify the actual qa-db-selects.php file to change the qa_db_posts_basic_selectspec(...) function - putting your new version in qa-theme.php won't work because this isn't a function in the theme class.

Second if you want it before the 'what' span, you're best off doing this in two stages. First, pass the email through to the theme by adding this line somewhere towards the end of qa_post_html_fields(...), again by modifying the original qa-app-format.php:


Then, do something like this in your qa-theme.php file:


    class qa_html_theme extends qa_html_theme_base
        function post_meta($post, $class, $prefix=null)
            if (strlen(@$post['email']))
                $prefix="YOUR GRAVATAR HTML WHICH USES $post['email']".$prefix;
            qa_html_theme_base::post_meta($post, $class, $prefix);


This is basically prepending the HTML to show a Gravatar to $prefix (if the email is not empty) then calling through to the standard post_meta(...) theme function, which outputs the $prefix before the rest of the meta info.
Thank you very much, worked perfectly. I now understand where I was going wrong. Perhaps this is something Q&A team may wish to consider adding as a feature in the future?
Yes indeed, avatars (via Gravatar or otherwise) are certainly on the roadmap.
Sounds good. You could have radio buttons in the users profile to upload an Avatar or use Gravatar instead. I am very impressed with this project, I look forward to contributing to it in the future.