Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
611 views
in Q2A Core by
edited by
I have a Joomla system and a requirement to add a Q+A board to it. I have selected Question2Answer for this, as it seems to be one of the best examples of its type. (there are a couple of Q+A board modules in the Joomla Extensions Directory, but they don't appear to be anything like as good as Q2A).

I have successfully followed the single-sign-on integration instructions on the Q2A site, and I want to share how I did it so that others can follow my example. I do have some questions remaining, but I'll ask those separately.

See the answer below for the solution...
Q2A version: 1.7.1
by
Good solution. I suggest you post it as an answer (and remove it from your question). That way the question shows up as answered.
by
@Scott: Thanks for the comment. I've now done as you suggested.  :)

1 Answer

+5 votes
by
 
Best answer

Assuming that your Joomla and Q2A installations are on the same server and under the same domain, the integration is actually very easy, once you've understood how to link into Joomla.

Start by following the standard integration instructions here: http://www.question2answer.org/single-sign-on.php.

For point 7, the correct data type to set here is "INT".

When you get to the final point (point 11), these are the code changes you will need to make in your qa-external-users.php file:

1. Several of the functions you have to change will need to access Joomla, and the code is similar, so write a single function for them all to share. I put it at the end of the qa-external-users.php file:

function qa_get_joomla_app() {
    if(!defined('_JEXEC')) {
        define( '_JEXEC', 1 );
        $joomlaPath = "/var/www/myJoomlaSite/";
        define('JPATH_BASE', $joomlaPath);
        require_once( JPATH_BASE.'/includes/defines.php' );
        require_once( JPATH_BASE.'/includes/framework.php' );
        // Instantiate the application.
        $app = JFactory::getApplication('site');
        // Initialise the application.
        $app->initialise();
    }
}

The only line you need to change in the function above is the one setting the path. Change it to suit your Joomla installation.

2. qa_get_login_links(). This function sets where the login, logout and register links should point to. This will vary according to your Joomla site's setup, so adjust as necessary. But note that Joomla doesn't have a logout page as standard (it posts to a form instead, which won't work for this). To resolve this, I installed a Joomla plugin called Quick Logout, which allowed me to set up a logout.html URL that I could then reference from this Q2A function.

3.qa_get_logged_in_user(). This function needs to call your qa_get_joomla_app() function (see 1 above). Your code should look something like this:

 

function qa_get_logged_in_user()
{
   qa_get_joomla_app();
    $user = JFactory::getUser();
    
    if($user) {
      if($user->guest || $user->block) { return null; }
      $level = QA_USER_LEVEL_APPROVED;
      if($user->authorise('core.edit')) {$level = QA_USER_LEVEL_EDITOR;}
      if($user->authorise('core.edit.state')) {$level = QA_USER_LEVEL_MODERATOR;}
      if($user->authorise('core.manage')) {$level = QA_USER_LEVEL_ADMIN;}
      if($user->authorise('core.admin') || $user->get('isRoot')) {$level = QA_USER_LEVEL_SUPER;}
      return [
        'userid' => $user->id,
        'publicusername' => $user->username,
        'email' => $user->email,
        'level' => $level,
      ];
    }
    return null;
}

Note that I've used core Joomla permissions to map to Q2A access levels. If you want to use different permissions, then you may change the code above as applicable.

4. qa_get_user_email(). This should like this:

{
    qa_get_joomla_app();
    $user = JFactory::getUser();
    if($user) {
      return $user->email;
    }
    return null;
}

5. qa_get_userids_from_public(). This should look like this:

{
    $output = [];
    if(count($publicusernames)) {
      qa_get_joomla_app();
      foreach($publicusernames as $username) {
        $output[$username] = JUserHelper::getUserId($username);
      }
    }
    return $output;
}

6. qa_get_public_from_userids(). And this one should look like this:

{
    $output = [];
    if(count($userids)) {
      qa_get_joomla_app();
      foreach($userids as $userID) {
        $output[$userID] = JFactory::getUser($userID)->username;
      }
    }
    return $output;
}

7. Add a menu item in Joomla that links to your Q2A installation.

And that's it. That's all there is to do. You should now be able to log into Joomla, click the link to go to your Q+A board, and you will be logged in there as well.

So what else is there left to do?
Obviously theming; you'll want to style Q2A so it looks like it's part of your Joomla site.
And searching. As things stand, Joomla's and Q2A's search functionality are not integrated. When you're on a Joomla page, a search will give you results from Joomla's database. When you're on Q2A, searching will give you results from your Q+A board. I haven't investigated fully how to integrate these in more detail, but what I've found so far makes it seem like it might be quite tricky.

by
yes, exactly. I do not know Joomla too much, but it won't be much work to get Q2A work with Joomla theme if it is good. Becuase Q2A has only few kinds of pages- question lists, question pages to be handled.
by
When the time comes, I will certainly ask the theme developer if it is easy to integrate the Q2A into the joomla theme.  

Thank you very much.
by
@sc-sfw
I did build the site using purity iii template.  I am sure it does not support full Q2A integration.

Is it possible to have the joomla website menu on top and under that have the Q2A with the snow theme.  And this to be mobile friendly (responsive)?  Can this be done?
by
@bobptz - I haven't used this template. However, please note that the integration being discussed here is not a visual integration, it is to provide single-sign-on. The two apps (Joomla and Q2A) will still  have their own themes and won't share any design elements.
...