[How To] Multiple Time Slots for Open/Closed

26 views
Skip to first unread message

Stephen Gemme

unread,
Apr 4, 2014, 1:23:03 PM4/4/14
to
I went around looking how to do this for my chat and couldn't find anything. Here's my simple implementation of giving your chat the ability to have multiple "Open" periods for your chat throughout the day. 

The original chat only lets you pick one time slot for the chat to be open, but I wanted to be able to specify a few if needed. 

Open config.php and replace this:

// Defines the hour of the day the chat is opened (0 - closingHour):
$config
['openingHour'] = 6;
// Defines the hour of the day the chat is closed (openingHour - 24):
$config
['closingHour'] = 13;

With this:

// Defines the hour of the day the chat is opened (0 - closingHour):
$config
['openingHour'] = array(6,15);
// Defines the hour of the day the chat is closed (openingHour - 24):
$config
['closingHour'] = array(13,24);

Now, obviously you can put any numbers in here that you wish, it's your preference. As of PHP 5.4, it's also possible to use the [] declaration for an array, but that's up to you. 

To implement the code correctly, open AjaxChat.php and change your "isChatOpen" function to the following:

function isChatOpen() {
 
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR)
 
return true;
 
if($this->getConfig('chatClosed'))
 
return false;
 $time
= time();
 
if($this->getConfig('timeZoneOffset') !== null) {
 
// Subtract the server timezone offset and add the config timezone offset:
 $time
-= date('Z', $time);
 $time
+= $this->getConfig('timeZoneOffset');
 
}
 
// Get our elements so we don't need to call them from the config file each time we compare.
 $elements
= count($this->getConfig('openingHour'));
 $openingHours
= $this->getConfig('openingHour');
 $closingHours
= $this->getConfig('closingHour');
 
// Check the opening hours:
 
for($i=0; $i<$elements; $i++){
 
if($openingHours[$i] < $closingHours[$i])
 
{
 
// If the current time is After the opening hour and Before the closing hour, chat is open.
 
if($openingHours[$i] <= date('G', $time) && $closingHours[$i] > date('G', $time))
 
return true;
 
}
 
else
 
{
 
if($openingHours[$i] <= date('G', $time) && $closingHours[$i] > date('G', $time))
 
return true;
 
}
 
}
 
// Check the opening weekdays:
 
if(in_array(date('w', $time), $this->getConfig('openingWeekDays')))
 
return true;
 
return false;
 
}


***Note*** I also added entrance to Moderators in my chat, be sure you want this if you're copying my code directly. 

That's it! You're done! 

Frug

unread,
Apr 4, 2014, 11:07:19 AM4/4/14
to ajax...@googlegroups.com
nice

 
// If the current time is After the opening hour or Before the closing hour, chat is open.
 
if($openingHours[$i] <= date('G', $time) || $closingHours[$i] > date('G', $time))

 
return true;
 
}
 
else
 
{
 
if($openingHours[$i] <= date('G', $time) && $closingHours[$i] > date('G', $time))
 
return true;
 
}
 
}
 
// Check the opening weekdays:
 
if(in_array(date('w', $time), $this->getConfig('openingWeekDays')))
 
return true;
 
return false;
 
}

Reply all
Reply to author
Forward
0 new messages