Thursday, August 28, 2008  | Login
DotNetNuke Gold Benefactor
    

Blog Search  
  

Blogs  
  

Blog  
Mar 12

Written by: Chris Garrett
3/12/2007

We recently had a Help Desk request for a way to display different messages at different times during the day. Our response to this was that there would be a custom development fee for modifying the module and in instances such as this we generally would charge a fee, as we usually have to get in and fiddle around with the code. However, I've developed a javascript "hack" that accomplishes this task without having to touch Visual Studio, so I am releasing this without the custom development fee.

This "hack" will allow address the issue of displaying messages based on the server time. What it does not address is the difference in timezones. This is merely a jumping off point, if you would like to display a particular message when your server's time is between 8:00am and 5:00pm and a different message at any other point in the day, this is how I accomplished the task. I encourage anyone using this code to back up or download a back up copy of the module. If any part of the javascript in our module is written over or corrupted in any fashion the module WILL FAIL. With that I offer the following warning:

MODIFY YOUR VERSION OF THE SWIRL AJAX CHATROOM MODULE AT YOUR OWN RISK. WE ASSUME NO LIABILITY FOR ANY CHANGES MADE NOR DO WE ACCEPT ANY LIABILITY FOR ANY UNEXPECTED BEHAVIOR AS A RESULT OF ANY MODIFICATIONS MADE TO YOUR MODULE. USE THIS CODE AT YOUR OWN RISK. IT IS ADVISED THAT THE FOLLOWING NOT BE USED ON A PRODUCTION SERVER UNTIL IT HAS BEEN TESTED THOROUGHLY.

Assuming that warning didn't scare you out of an attempt at modification let's continue. This modification will require editing the ChatRoom.ascx file. Notepad will be sufficient enough for this change.

First, we'll add the necessary code to the Header of the chat module. Open the Module Settings and click the plus sign next to Advanced Settings. Here you'll find the standard settings available to all of modules. In the Header text box add this line of code:

<div id="TimeGreeting"> </div>

This code simply gives your custom messages a place to go. Next, let's add the javascript code to the ChatRoom.ascx file. Do a search for window.onunload=uLogout; we'll place the javascript directly after that before the end script tag. Here is the code to enter:

   function CheckTime()
   {
      var spc = strUserListGUID.indexOf(" ");
      var Time = strUserListGUID.substr(spc+1);
      var Hour = Time.substr(0,Time.indexOf(":"));
      var AMorPM = Time.substr(Time.indexOf(" ")+1);
      var NewHour;

      if (AMorPM == "PM")
      { NewHour = parseInt(Hour) + 12; }

      // -- 8:00 AM - 12:00 PM
      if (NewHour >= 8 && NewHour <= 11)
      document.getElementById('TimeGreeting').innerHTML = "Good Morning.";

      // -- Noon
      if (NewHour == 12)
         document.getElementById('TimeGreeting').innerHTML = "Lunch Time.";

      // -- 1:00 PM - 5:00 PM
      if (NewHour >= 13 && NewHour <= 16)
         document.getElementById('TimeGreeting').innerHTML = "Good Afternoon. ";

      // -- 6:00 PM - 7:00 AM
      if (((NewHour >= 17) && (NewHour <= 23)) || ((NewHour >= 1) && (NewHour <= 7)))
         document.getElementById('TimeGreeting').innerHTML = "Outside of business hours. ";
   }


Now you have the code needed to change your message, but how will the page know to execute this function. Since we've already built in the javascript the method for polling, which is what keeps your chatroom constantly checking to see if there are new messages to display, we'll just latch on to that so that once your chatroom loads your message gets displayed. Do a search for the function PollChat() line. Here you'll see:

   try{
      lastPoll=new Date();
      SWIRL.DNN.Modules.Chatroom.AJAXMethods.PollChat(intModuleID,strUserid,strUserListGUID,callback_PollChat);


Insert the following at this location:

   if (document.getElementById('TimeGreeting').innerHTML == " ")
      CheckTime();


This will keep the CheckTime function from being called every time the PollChat function is called by checking to see if your div is set to the default value. NOTE: If you change your default value from a non-breaking space you'll need to change your if statement here. Now, save your file and upload it back to your server. You're all set for your custom messages based on server time. You may need to tweak the hours in the CheckTime function to match business hours or hours of chatroom operation. As I stated at the beginning of this entry, this script does not address the timezone issues that would be associated with anyone trying to access your chatroom outside of the timezone in which your server resides. It also does not take into account different days (Saturday is the same as Wednesday to the script). But you may take the script and modify it to suit your needs.

Enjoy.

Tags:
  

© 2005-2006 Swirlhost Incorporated   Terms Of Use  Privacy Statement
Home  Support  Chat Module  DNN Hosting  Blogs  SkinLab