Day Ten: Adding A Threshold To the Event Calendar

In this tutorial we are going to add a threshold to the event calendar, which gives you a bit more control when displaying events.

The issue that some readers have with the current event calendar plugin we have designed is the what to do when event starts maybe at 9:00 PM and goes to 12:30 AM. Well, using the current event calendar we designed, you would get an event to show on two days on the calendar because is starts one day and really ends the next. Even though the events are really on two different days, people think of this as happening on the day that it starts. So to make is user friendly, it would be nice to gain control over this.

To prevent or even give you control over this we need add what we will call as a threshold. Basically, we are going to use a option that allows us to determine how many hours into the next day an event can go without being displayed on that day.

To accomplish a working threshold for our events we are going to need to take the ending time and convert it into seconds. The threshold we also be in seconds and then we will compare them. If the event is under our threshold, we will not include it on the calendar and if it above our threshold we will include it on the calendar.

Step One: Adding an Int and Constuct Methods to the Calendar Events Class

To have the option of control over our event threshold, I am going to add a construct and init method to our class. Here are the methods:

function __construct($options = NULL){
    if(!is_null($options)){
        $this->init($options);
    }
}

function init($options){
    foreach($options as $key => $value){
        $this->$key = $value;
    }
    $this->threshold = $this->time_in_secs($this->threshold);
}

I am also going to add a class variable that I am going to call $threshold.

This will now allow us to pass an option array directly into the class of use the init function to set our $threshold. You will also see this $this->threshold = $this->time_in_secs($this->threshold);, we will come back to that. But you need to know when you are passing the options to the class your time must be in MySQL dateTime format. You will see why later on.

Step Two: New Method to Convert MySQL dateTime to Seconds

If we are going to compare our event ending time with our threshold, seconds is the best way. Below is the method we will use to convert MySQL dateTime to seconds.

function time_in_secs($time){
        list($hr, $mins, $secs) = explode(":", $time);
        return ( $hr * 60 *60 ) + ( $mins * 60 ) + $secs;
}

Step 4: Change the Conditional Statements in Calendar Data Method

If you remember back from our post on the calendar event class, we had four conditions on how to add our event into the data array.

  • If an event started and ended on the same date.
  • If an event started and ended on different days in the same month.
  • If an event started last month and continued into this month
  • If an event started this month and continued into next month

Those are the four but we are really only interest in two: If an event started and ended on different days in the same month and If an event started last month and continued into this month. Because if an event starts and ends on the same day the threshold and if the event ends next month, the threshold is not needed.

So here is modifications to the code for the other two conditions:

//event that happens over several days in the current month
if($start_date < $end_date && $start_month == $end_month){
    for($start_date; $start_date <= $end_date; $start_date++){
        if($start_date == $end_date){
            if($ending_time > $this->threshold){
                $events[$start_date][] = $row;
            }
        }else{
            $events[$start_date][] = $row;
        }
    }
}

//event that started last month but continues into this month
if($start_month == $prev_month){
    for($start_date = 1; $start_date <= $end_date; $start_date++){
        if($start_date == $end_date){
            if($ending_time > $this->threshold){
                $events[$start_date][] = $row;
            }
        }else{
            $events[$start_date][] = $row;
        }
    }
}

We basically added and if statement to each. We just see if the end date is the date we want to add to the array and if it above our threshold. And if it is we add it our calendar.

Step Five: Pass the Options Array to our Class

We can now go our main plugin file: custom_event_calendar.php and pass the options array, which will currently only be our threshold. Here is the edited function :

function custom_event_calendar($atts){
   
    global $post;

    extract(shortcode_atts(array(
            'link_base' => get_permalink($post->ID),
            'month' => get_query_var('mon'),
            'year' => get_query_var('yr')
    ), $atts));

    if(empty($month)){
            $month = strftime('%m', current_time('timestamp'));
    }

    if(empty($year)){
            $year = strftime('%Y', current_time('timestamp'));
    }

    include('calendar.php');

    $options = array(
            'link_base' => $link_base
    );

    include('calendar_events.php');

    $options = array(
        'threshold' => "7:00:00"
    );

    $our_events = new Our_calendar_events($options);

    $events = $our_events->calendar_content($month, $year);

    $event_calendar = new Event_calendar($options);

    return $event_calendar->create($month, $year, $events);
}

You see that I have a MySQL dateTime of 7:00:00 which is 7:00 AM. So any event that ends before 7:00 AM will not be included.

Wrap up

We are all done. The only thing that would be nice would be to have an options page to control the threshold. Here are the files for download and you will also find that I added a style sheet to the calendar.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
This entry was posted in Advanced, Plugin Development. Bookmark the permalink.

4 Responses to Day Ten: Adding A Threshold To the Event Calendar

  1. Carlitos says:

    Hello!

    First of all, many many thanks for this great tutorial series. It has been most illuminating.

    I’d like to point out that the code, as it is attached to this post, has one little bug. On the last lines of the custom_event_calendar function (custom_event_calendar.php), you overwrite the contents of the $options array, as in:

    
        include('calendar.php');
    
        $options = array(
                'link_base' => $link_base
        );
    
        include('calendar_events.php');
    
        $options = array(
            'threshold' => "7:00:00"
        );
    
        $our_events = new Our_calendar_events($options);
    
        $events = $our_events->calendar_content($month, $year);
    
        $event_calendar = new Event_calendar($options);
    
        return $event_calendar->create($month, $year, $events);
    
    

    This has the negative effect of not passing $link_base to Event_calendar->create, because $options has been overwritten with only $threshold’s value. I changed this code to the following, and it resumed working as intended:

    
    
        $options = array(
                'link_base' => $link_base,
                'threshold' => "7:00:00"
        );
    
        include('calendar.php');
        include('calendar_events.php');
    
        $our_events = new Our_calendar_events($options);
        $events = $our_events->calendar_content($month, $year);
        $event_calendar = new Event_calendar($options);
    
        return $event_calendar->create($month, $year, $events);
    

    Again, thanks a million for this series!

  2. nayeem says:

    Thanks for your support, I don’t want the event in CUSTOM POST TYPE instead in the POST itself . I replace the code in custom_event_calendar.php the following code

    add_action(‘save_post’, ‘save_event_data’);

    function save_event_data(){

    if($_POST['post_type'] == “event”){

    include(EVENT_CALENDAR_DIR . “event_class.php”);

    $event = new Manage_events;

    $data = array(

    ‘post_id’ => $_POST['ID'],

    ‘start_date’ => $_POST['start_date'],

    ‘end_date’ => $_POST['end_date'],

    ‘start_time’ => $_POST['start_time'],

    ‘end_time’ => $_POST['end_time'],

    ‘event_visible’ => $_POST['event_visible']

    );

    $event->save($data);

    }

    }
    i replace the event to post in custom_event_calendar.php it working perfect, but in the save event it not stored the data in data base

    add_action(‘save_post’, ‘save_event_data’);

    function save_event_data(){

    if($post->post_type == ‘post’){

    include(EVENT_CALENDAR_DIR . “event_class.php”);

    $event = new Manage_events;

    $data = array(

    ‘post_id’ => $_POST['ID'],

    ‘start_date’ => $_POST['start_date'],

    ‘end_date’ => $_POST['end_date'],

    ‘start_time’ => $_POST['start_time'],

    ‘end_time’ => $_POST['end_time'],

    ‘event_visible’ => $_POST['event_visible']

    );

    $event->save($data);

    }

    }

    i got the custom field in the post.when i publish the content with custom date ,data is not stored in the database. Can you suggest some tricks.

  3. Michael Mallett says:

    Is there anyway of adding recurring events to this design? Or would you have to completely change the database and php script?

  4. nayeem says:

    I resolve the problem which i ask earlier. what i want in next development is one additional calender widget in sidebar . Suppose i click (eg 14 feb) in sidebar calendar, it will so all the 14 feb events in the index file.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>