views:

34

answers:

2

Hey Everyone,

Here is my current problem. I am working with the chat module and I'm building a module that notifies users via AJAX that they have been invited to a chat. The current table structure for the invites table looks like this:

|-------------------------------------------------------------------------|
|  CCID  |  NID  |  INVITER_UID  | INVITEE_UID  |  NOTIFIED  |  ACCEPTED  |
|-------------------------------------------------------------------------|
|  int   |  int  |     int       |     int      |  (0 or 1)  |  (0 or 1)  |
|-------------------------------------------------------------------------|

I'm using the periodical updater plug-in for JQuery to continually poll the server to check for invites. When an invite is found, I set the notified from 0 to 1. However, my problem is the periodical updater. When I first see that there is an invite, I notify the user, and set notified to 1. On the next select though, I get the same results before, as if the update didn't work. But, when I got check the database, I can see that it worked just fine. It's as if the query is querying a cache, but I can't figure it out.

My code for the periodical updater is as follows:

window.onload = function() {


var uid = $('a#chat_uid').html();

$.PeriodicalUpdater(
    '/steelylib/sites/all/modules/_chat_whos_online/ajax/ajax.php',     //url to service
    {
       method: 'get',           //send data via...
       data: {uid: uid},        //data to send
       minTimeout: '1000',      //min time before server is polled (milli-sec.)
       maxTimeout: '20000',     //max time before server is polled (milli-sec.)
       multiplyer: '1.5',       //multiply against curretn poll time every time constant     data is returned
       type: 'text',            //type of data recieved (response type)
       maxCalls: 0,             //max calls to make (0=unlimited)
       autoStop: 0              //max calls with constant data (0=unlimited/disabled)

    },
    function(data)              //callback function
    {
        alert( data ); //for now, until i get it working
    }
);

}



And my code for the ajax call is as follows:

<?php

#bootstrap Drupal, and call function, passing current user's uid.

function _create_chat_node_check_invites($uid)
{    
    cache_clear_all('chatroom_chat_list', 'cache');
    $query = "SELECT * FROM {chatroom_chat_invite} WHERE notified=0 AND invitee_uid=%d     and accepted=0";
    $query_results = db_query( $query, $uid );
    $json = '{"invites":[';
    while( $row = db_fetch_object($query_results) )
    {
      var_dump($row);
        global $base_url;
        $url = $base_url . '/content/privatechat' . $uid .'-' . $row->inviter_uid;

        $inviter = db_fetch_object( db_query( "SELECT name FROM {users} WHERE uid = %d", $row->inviter_uid ) );
        $invitee = db_fetch_object( db_query( "SELECT name FROM {users} WHERE uid = %d", $row->invitee_uid ) );

      #reset table
      $query = "UPDATE {chatroom_chat_invite} "
                ."SET notified=1 "
                ."WHERE inviter_uid=%d AND invitee_uid=%d";
      db_query( $query, $row->inviter_uid, $row->invitee_uid );

        $json .= '[';
        $json .= '"' . $url . '",';
        $json .= '"' .  ($inviter->name) . '",';
        $json .= '"' . ($invitee->name) . '"' ;
        $json .= '],';
    }
    $json = substr($json, 0, -1);
    $json .= ']}';

    return $json;
}
?>

I can't figure out what is going wrong, any help is greatly appreciated!

A: 

The browser may be cacheing the GET call. Try adding a cache-buster value to the URL (current time works well) of the AJAX request to force a fresh load each time.

Marc B
A: 

Well, it's been a while and I finally figured out my problem, but I realized that if anyone else found this page and wanted an answer, they were out of luck. So here was the problem.

Sine the handler for the AJAX call used Drupal functions, I had to bootstrap Drupal (used above, but not shown). However, when I bootstrapped Drupal, I did a full bootstrap. This caused the Chat Module to call a hook that checked for invites. I didn't realize that this was being called, because in normal use, nothing ever notified me... Anyways, the hook is meant to check for notifications and alert the user (somehow.. i do not know how) and when it checked, it set the notified flag to true.

So, by the time that my AJAX handler was being called, the invite had already been set to notified by the ChatRoom module. So, my solution was to only to a partial bootstrap. The code for the bootstrap is below. I found the script on someone else's website, but I'm not sure what it is. Anyways... here's the code:

<?php
    #bootstrap drupal to get access to drupal functions
    define('STRIP_LEN', -41); #length of path to use (strip off stuff like /sites/all/...)
    $base_url =  substr(getcwd(), 0, STRIP_LEN);
    chdir($base_url);
    global $base_url;

    $base_root = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? 'https' : 'http';

    $base_url = $base_root .= '://' . preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);

    if( $dir = trim( dirname( $_SERVER['SCRIPT_NAME'] ), '\,/' ) )
    {
        $base_path = "/$dir";
        $base_url .= $base_path;
    }
    $base_url = substr($base_url, 0, STRIP_LEN);


    require_once './includes/bootstrap.inc';
    require_once './includes/common.inc';

    drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

    require_once drupal_get_path('module', '_chat_whos_online') . '/_chat_whos_online.inc';

    #END BOOTSTRAPPING
?>
John