You Are Here: Home » How-To » Programming

How To Create Chatrooms In PHP AJAX-Chat Based On Question2Answer Categories?

By on February 1st, 2014 
Advertisement

If you have visited StackOverflow or any of its many Question & Answer sites then you may have noticed that the users can chat with each other on various chatrooms that are basically categories of questions on the site. We will try to achieve something similar in our AJAX-Chat and Question2Answer integration as well.

For this tutorial to function properly you have to first make sure that you have a few categories setup or added to your Question2Answer Q2A site else this tutorial is not going to work as desired. Lets see how to add new categories to Question2Answer first.

How to add new categories to Question2Answer?

Step 1: Log into your Question2Answer site as the root or admin user and navigate to the Categories section:

YOUR_QA_SITE_URL/admin/categories

Step 2: Now click on Add Categories button and enter various details such as Category Name, a small description of your categories and a parent category and click Add Category again to save your changes.

How To Add Question2Answer Categories As Chatrooms To AJAX-Chat?

Ajax-Chat provides with you a custom function that you can use to add new Chatrooms to it programmatically. The function is called getCustomChannels. This function provides users with a data source for the various categories to be used as ChatRooms in AJAX-Chat.

AJAX-Chat Q2A Integration

AJAX-Chat Q2A Integration

All you need to do is replace the getCustomChannels function and its contents in the CustomAJAXChat.php file located inside the class directory in lib directory of the AJAX-Chat root folder with the following:

function getCustomChannels() {
// List containing the custom channels:
$channels = array();
//Q2A Categories list
$q2a_categories=qa_db_select_with_pending( qa_db_category_nav_selectspec(false, false, false, true) );
foreach ($q2a_categories as $catID => $catDet) {
$channels[$catID] = $catDet["title"];
}
// Channel array structure should be:
// ChannelName => ChannelID
return array_flip($channels);
}

function getCustomChannelsId() {
    // List containing the custom channels:
    $channels = array();
    $q2a_categories=qa_db_select_with_pending( qa_db_category_nav_selectspec(false, false, false, true) );
    foreach ($q2a_categories as $catID => $catDet) {
        $channels[] = $catID;
    }
    // Channel array structure should be:
    // ChannelName => ChannelID
    return $channels;
}

In the above snippet what we basically do is replace the dummy Chat rooms with the the New chat rooms which are nothing but categories of your Question2Answer installation. With this your users will now be able to chat on various aspects and their interests making the chat rooms more focused and the chats more meaningful.

Please note, if you are trying to add all the users from your database of Q2A to AJAX-Chat preloaded, then you need to replace the getCustomUsers function and its content with the following code:

function &getCustomUsers() {
// List containing the registered chat users:
$users = null;
$channelIDs = $this->getCustomChannelsId();
require(AJAX_CHAT_PATH.'lib/data/users.php');
$userscount = count($users);
//users from q2a
$usersfromq2a = qa_db_select_with_pending(qa_db_top_users_selectspec(0));
foreach ($usersfromq2a as $key => $value) {
$users[$key] = array();
$users[$key]['userRole'] = AJAX_CHAT_USER;
$users[$key]['userName'] = 'user';
$users[$key]['password'] = time().mt_rand();
$users[$key]['channels'] = $channelIDs;
}
return $users;
}

Advertisement



Related Posts by Tags: , , ,





How To Create Chatrooms In PHP AJAX-Chat Based On Question2Answer Categories? was originally published on Digitizor.com on February 1, 2014 - 8:47 pm (Indian Standard Time)