Search Brown University

Display Events on a Departmental Web Page - PHP Code

NB: The php scripts provided below are versioned. The current version is 1.2.3.  If you are using an earlier version, you are encouraged to update to 1.2.3.  Previous versions are no longer officially supported.

 

  • Version 1.2.3 fixes the direct .ics download link.  The link changed in the Bedework update.
  • Version 1.2.2 creates a separate class with better exception handling (thanks Chris Keith!)
  • Version 1.2.1 strips tabs from calendar feeds.   Tabs in event data can result in the feed showing no events.  

 

Step by Step

1. Include the files: Begin by creating a new page in your department web site. Create it just as you normally would create any other HTML page, but leave the content area blank for now. Give your file a meaningful name, but be sure that it ends in .php e.g. events.php

2. At the very top of the document before any HTML starts add the following code. This code includes the PHP script that handles the data feed, and it creates a new instance of the BedeworkFeed class. 

     include_once "/www/data/httpd/htdocs/Templates/common/calendar/1.2.3/bedeworkfeed.class.php";
    $Cal = new BedeworkFeed();
?> 

3. Set the dates: Next, the newly instantiated class needs to know what date range to use when it retrieves events. For the sake of demonstration, here's a sample of code that sets the date range to be events that occur between one week in the past and three weeks in the future.  Dates can be expressed in any format understood by php's strtotime() function.  Some examples: +1 daynow,next Monday, or 30 April 2010. Be sure to set a start date and an end date; if you do not specify dates, the class will use its default settings and return events between last Sunday and next Sunday.

    $Cal->setDate("start", "-1 week");
    $Cal->setDate("end", "+3 week");
?>

4. Apply filters: You can add one or more categories to your request to filter the feed. Adding multiple categories is permitted.  Filtering by multiple categories will return any event that matches any one of the categories you specify.  So using the code below will show all events that are categorized asAnthropology or Lectures.

    $Cal->addCategory("Anthropology");
    $Cal->addCategory("Lectures");
?>

5. Exclude Categories: In addition to adding categories, you can also exclude categories. This allows you to get a feed of department events, but exclude events of a certain type. The code below will exclude all events that are categorized as Workshops.

    $Cal->addExcludeCategory("Workshops");
?>

6. Fetch the Feed: Once your filters and categories are set, you can fetch the feed. After the feed is fetched, another call is made to get the events, and in this code snippet, all of the events are stored in the $events array.

    $Cal->getFeed();
    $events = $Cal->getEvents();
?>

7. Display events: Finally, it is possible to display the event data on your web site. A template has been made available to do this as well. Simply include the template where you'd like the list of events to be on your page. You could also create your own template and include it instead of this default one.

   include_once("/www/data/httpd/htdocs/Templates/common/calendar/1.2.3/bedework-event-list.php");
?>

8. Caching: Version 1.2 introduces caching of feeds. Caching dramatically speeds up the performance of departmental calendar sites. When your site is first visited, a feed is read from Bedework, but its data is saved locally so subsequent visits open the local saved data. The result is a significantly faster web page. Feed caches last two hours by default, and after two hours pass, cache files are created automatically the next time someone accesses your page. If you find that two hours isn't suitable for your department, you can change the life of the cache with the following code. NB: the amount of time is expressed in seconds. e.g. 60*60*5 = 60 seconds times 60 minutes 8 five hours = 18000 seconds = 5 hours. Setting this to be a high number will make your web site faster, but it will not refresh its data from Bedework as often.

    $Cal->setCacheAge(60*60*5); // five hours
?>

Tips

1. Smaller feeds load faster. Use category filters to reduce the number of events that you retrieve from Bedework, and limit the time frame to a week or less on very busy calendars.

2. Add RSS. You can easily add a link to an RSS feed and make it auto-discoverable by adding the following HTML between the and tags:

3. Display (or hide) "Private" Events. By default, "Private" events in Bedework will show up on your department page. The idea is that private events are private to your department and its audience, not strictly private to the admin. If you'd like to hide "Private" events from your department calendar, add this code snippet:

     $Cal->hidePrivateEvents();
?>

Should you ever need to reverse this preference, you can use: $Cal->showPrivateEvents(); which will restore the default setting.

4. Cache Busting. You can bypass the caching features by adding "?go-slow=true" to your URL. Also, you can set the cache age to a low number (like 1) and view the page. This will cause your cache to be refreshed immediately. Then you can set it back to a high number again so that other visitors will not have to wait for your page to reload feeds from Bedework.

A Typical Department Example

Below are two snippets of code for a typical departmental calendar view. You can copy and paste this code into your web page, and substitute the department category (here, Classics) with your own. NOTE that the filename of your webpage needs to end in .php (not .htm or .html), for example: index.php

Place this code as close as possible to the top of the document (this code fetches events, but generates no HTML):

    include_once "/www/data/httpd/htdocs/Templates/common/calendar/1.2.3/bedeworkfeed.class.php";
    $Cal = new BedeworkFeed();
    $Cal->setDate("start", "now");
    $Cal->setDate("end", "+4 week");
    $Cal->addCategory("Dept: Classics");
    $Cal->getFeed();
    $events = $Cal->getEvents();
?>

Then, place this code in the document where you want the list mark up to be generated:

    include_once("/www/data/httpd/htdocs/Templates/common/calendar/1.2.3/bedework-event-list.php");
?>

To format your events, follow the instructions in the article Display Events On A Departmental Web Page - Use JavaScript for Formatting.

Comments (0)

Add a comment