I have updated my Hesk hack based on a request from a reader. You can view the new instructions or download the new files here: http://wiki.cornempire.net/hesk/hesk252

For those unaware, my previous post on this matter was here: http://cornempire.net/2010/07/31/hesk-enhancements-for-version-2-2/

2.5 introduced a lot of changes. One of the big ones that affected my hack was the ability to directly link to a category type. This is now built into the application, so I modified my hack to take advantage of this.

One of my long time goals has been to be able to easily display tournament brackets on the web. It seems to be a difficult problem to solve with lots of questions out there, and few answers. There are a few solutions, but they have various issues and downfalls.

I decided to give it another try myself. And this is what I have come up with: A bracket builder, a saved file with raw data, and a bracket viewer that can read the files. If all that sounds like too much, the bracket builder can just spit out the HTML code to display the tree.

I’ve outlined how it works, uploaded an example and have provided a download so you can integrate it with your own applications.

Recap

So, I’ve gone through adding a calendar to your webpage to display to your users, and then creating your application with Google so that you can do queries against your calendar. Now we get to the fun part, writing the application! I should mention that this is written in PHP, because I don’t know if I mentioned that specifically yet.

The Plan

The plan is to create a PHP script that will display a list of calendars people can book items on, and then allow them to book off time on one of these calendars. We will also include some restrictions, like not booking over other events, not booking in the past, only allowing bookings so far into the future, and not allowing bookings during ‘closed’ times. You can add more restrictions as you see fit.

In my example, the script is hosted behind some password protection and user accounts. Depending on the nature of your application, that may or may not be required. For this example, I’m going to assume it is an open system.

The API

Google provides a series of classes that you can use to interface with their API. However, I could not figure out how to use them with refresh tokens. If you intend to use a lot of the functions of the API, it might be worth while for you to get it working. However, for me, it was easier to make raw calls to the API since I’m only using 2 of the API functions. If you want to download all of the Google code for PHP, you can find the directions here.

Getting Started

The Google API is based on REST principles, and uses JSON to encode data. To know exactly what to send to the API, I played around with the Google API explorer. To make a long story short, I’ll include the code I used for my calls.

The Functions Needed

As we are not using the Google code, we will need to create some functions to do our bidding. We need a function to manage our authentication tokens, to submit post requests (to add events), to submit get requests (to check for previous bookings), and to craft our JSON request for the POST.

You will need to add your values for client_id, refresh_token and client_secret that you established in Part 2 of this tutorial. This function is pretty stand alone. The only thing you might want to add to your finished product is better token management. I generate a new token every time. It would be best to check the token before regenerating, as tokens tend to be good for 3600 seconds.

This function creates the body of the our post request. What you include depends on what you are storing in the event. I only need the start and end time, and a title for the event. Check out the API explorer to see what you can do.

The date is YYYY-MM-DD format, and times are passed to the function in HH:mm format. I then pad the string with the seconds and timezone.

Odds and Ends

The rest of my functions depend on these odds and ends I set up before I start doing stuff, so I’ll cover them here.

This script expects a URL parameter to tell it which calendar to load. I first set it if nothing is specified. Then I set up the calendars. I’m using this for tennis court bookings, hence my naming conventions. The keys here are:

The array index/cid – This is the url parameter you will pass to the page to load the correct calendar.

name – The nice name you will display to users.

id – This is your calendar id from Google that you set up in Part 1.

starttime/endtime – The limits in booking times specified in HH:mm:ss

advance – This is the amount of time in the future that you allow bookings. PHPs strtotime syntax is used for defining the future time.

This function accepts the date, starttime, endtime and calendar id. It does a check to see if any event exists for this time on the calendar already. It uses the get request function to get it’s result. If the result is more than 5 characters return true, telling us that the time is used, but if it is less then 5, then there are no events, so return false.

This code prints out a list of calendars from our array, and links to them.

Then it prints out a message if one is set.

Then we use the Google iframe code from Part 1 to display a calendar.

Then we create a form to accept input from our users. Using a nice date/time picker would make things easier on the user. The forms posts back to itself, but you could separate much of this code into different files.

PHP Code To Handle Booking

So now that we have all of the pieces in place, we can create the code that will accept the input from the users.

This code checks to see if a form was submitted, and then completes a series of checks to make sure that it is a valid request. If they all pass, then the event is added to the calendar. You may want to print a message to let the user know that the event was added.

At this point, you have a fully working calendar booking system. You can experiment with the API to add additional features.

Hopefully this tutorial series was of some use to you. If you have any questions, feel free to post them below.

UPDATE (Feb. 1, 2013):

By popular demand, I’ve included a complete example of putting all of these pieces together. I’ve stripped out all of the authentication and calendar codes and keys. The first two steps of this tutorial will teach you how to get all of those.

Background

This was by far the hardest part of the whole exercise. I had worked with version 1 of the Google API for PHP a few years back. This allowed you to code your username and password into your script, and it would handle authentication for your application. Now that we are on to version 3 of the API, that method is no longer available. Instead OAuth2 is used for authentication and token management.

I downloaded code samples, and went about building my application, however, I quickly realized that the OAuth2 code samples are designed to allow you to interact with a visitors calendar. In the case of taking online bookings, I need to work with a single calendar, namely my calendar, not theirs.

After a lot of trial and error, and then reading, I realized that it could be done, and relied on what is called a ‘Refresh Token’ in OAuth2. This token allows you to get a new valid authentication token when the initial grant from your end user expires. Since the refresh token doesn’t expire, you can always use it to get a new authentication token, and therefore people can continue to use your application after you have initially configured it. I spent a while trying to implement it myself with no success, but then I came across this page: http://www.ericnagel.com/how-to-tips/google-affiliate-network-api.html This explains in some detail how to configure the application and token. It is written for the Google Affiliate Network API, but I made a few tweaks to make it work for Calendar. I will now take you through the steps of setting up your application with Google, and generating your Refresh Token.

Create Your Application

Log into your Google Account, and then visit https://code.google.com/apis/console/. This will take you to a page that invites you to create a project with the Google API. Click on Create project….

You are now asked to activate the services you wish to use. Click the button next to Calendar API to enable the calendar. You will be redirected to a page with a Terms of Service. Read and accept this.

Now click on API Access. Here we will configure the IDs needed for your application to authenticate with Google. Click on Create an OAuth2 client ID…. You will be offered to create Branding Information. You should add your project/product name. The rest won’t be necessary as you will not be asking users directly for access to their resources, but you can complete it if you like.

Before running this script, you will need to enter your Client ID ($cClientID) and Client Secret ($cClientSecret) as we found on the API page with Google. Once these values are added, run this script from the command line like this: php oauth-setup.php. You should see output like this:

thomas@thomas-desktop:~/code$ php oauth-setup.php
Go to
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=##########################&redirect_uri=###############&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar
and enter the given value into this script under $cAuthCode

Visit the website, grant permission to access your resources, and then copy the code on this page. This is your auth code, and is normally good for 3600 seconds or so.

Enter this code into the oauth-setup.php script in the $cAuthCode variable. Then run the script again: php oauth-setup.php. You should see output like this:

Now, copy down the Refresh Token and save it for later. You will need it to make subsequent requests to Google to get a valid Auth Code for a transaction.

Stay tuned for Part 3 of the tutorial, which will use the above information to make calendar requests to Google. And allow us to create a web application that uses Google Calendar as a backend for a scheduling application.

Hello everyone,
I’ve been saying that I was going to post this for a long time, ever since my presentation at Fusion 2010: http://www.desire2learn.com/fusion/ and now I finally have.

I have written up two pages on code I developed for use with PHP and D2L’s web services. I have been using it at my work for the last year or so, and I finally polished it up a little, and created some installation and usage instructions. I’m not finished yet, but it is a start.

If you use D2L and want to extend it through Web Services (you need to purchase the web services package from D2L, and have it installed first), I encourage you to read over these pages:

If you have any comments, please post them below. Feel free to email me with any questions or problems you run into while working with the API or the testing framework. I’m eager to improve the code for everyone.

For those upgrading, you will need to reapply the hack to the new files. The differences were pretty minor between 2.1 and 2.2. I’ll outline the differences here:

The line numbers are different for the files

The function print_add_ticket_start that you need to add needs a few additional lines to bring in the global stylesheets. This was not required in 2.1

I also made a small tweak to this function so that there is no category selected by default, allowing you to select the default category via the dropdown box. Previously, this would have been unselectable.

I’m working on a screencast of applying this hack for those who are more visual learners. So stay tuned for that.

To take a look back at my previous post, and learn more about Hesk, click here.

If you are doing any PHP command line scripting, you may have wondered how you can add a bit of simple colouring to your scripts so it is easier for you, or your clients to view the output.

Using some Bash codes, you can colour the output to the terminal (assuming you are running the scripts in Bash or some other compatible terminal). Depending on the font colours and effects you want, you can use any of this code in your script. Just place it at the top, and include the variables in your output:

/*
* Font Colours
*/
$BLACK=" 33[0;30m"; //Can not be used on backgrounds
$DARKGRAY=" 33[1;30m"; //Can be used on backgrounds
$RED=" 33[0;31m"; //Can not be used on backgrounds
$LIGHTRED=" 33[1;31m"; //Can be used on backgrounds
$GREEN=" 33[0;32m"; //Can not be used on backgrounds
$LIGHTGREEN=" 33[1;32m"; //Can be used on backgrounds
$BROWN=" 33[0;33m"; //Can not be used on backgrounds
$YELLOW=" 33[1;33m"; //Can be used on backgrounds
$BLUE=" 33[0;34m"; //Can not be used on backgrounds
$LIGHTBLUE=" 33[1;34m"; //Can be used on backgrounds
$PURPLE=" 33[0;35m"; //Can not be used on backgrounds
$LIGHTPURPLE=" 33[1;35m"; //Can be used on backgrounds
$CYAN=" 33[0;36m"; //Can not be used on backgrounds
$LIGHTCYAN=" 33[1;36m"; //Can be used on backgrounds
$LIGHTGRAY=" 33[0;37m"; //Can not be used on backgrounds
$WHITE=" 33[1;37m"; //Can be used on backgrounds
/*
* Bolded colours that can be used on backgrounds
* Duplicates of the colours above with BOLD in the name
*/
$BOLDBLACK=" 33[1;30m";
$BOLDRED=" 33[1;31m";
$BOLDGREEN=" 33[1;32m";
$BOLDBROWN=" 33[1;33m";
$BOLDBLUE=" 33[1;34m";
$BOLDPURPLE=" 33[1;35m";
$BOLDCYAN=" 33[1;36m";
$BOLDGRAY=" 33[1;37m";
/*
* Background Colours
*/
$BLACKBG=" 33[0;40m";
$REDBG=" 33[0;41m";
$GREENBG=" 33[0;42m";
$BROWNBG=" 33[0;43m";
$BLUEBG=" 33[0;44m";
$PURPLEBG=" 33[0;45m";
$CYANBG=" 33[0;46m";
$LIGHTGRAYBG=" 33[0;47m";
/*
* Font Effects
*/
$UNDERLINE=" 33[4;30m";
$BLINK=" 33[5;30m"; //Doesn't seem to work.
$INVERSE=" 33[7;30m";
$INVISIBLE=" 33[8;30m"; //Pretty pointless
/*
* Turn it back to the default
*/
$DEFAULT=" 33[0m";

To test this out, you can place this in a php script and execute it:

echo "<-- Font Colours -->
";
echo "$BLACK This is BLACK
";
echo "$DARKGRAY This is DARKGRAY
";
echo "$RED This is RED
";
echo "$LIGHTRED This is LIGHTRED
";
echo "$GREEN This is GREEN
";
echo "$LIGHTGREEN This is LIGHTGREEN
";
echo "$BROWN This is BROWN
";
echo "$YELLOW This is YELLOW
";
echo "$BLUE This is BLUE
";
echo "$LIGHTBLUE This is LIGHTBLUE
";
echo "$PURPLE This is PURPLE
";
echo "$LIGHTPURPLE This is LIGHTPURPLE
";
echo "$CYAN This is CYAN
";
echo "$LIGHTCYAN This is LIGHTCYAN
";
echo "$LIGHTGRAY This is LIGHTGRAY
";
echo "$WHITE This is WHITE
";
echo "<-- Backgrounds -->
";
echo "$BLACKBG This is BLACKBG
";
echo "$REDBG This is REDBG
";
echo "$GREENBG This is GREENBG
";
echo "$BROWNBG This is BROWNBG
";
echo "$BLUEBG This is BLUEBG
";
echo "$PURPLEBG This is PURPLEBG
";
echo "$CYANBG This is CYANBG
";
echo "$LIGHTGRAYBG This is LIGHTGRAYBG
";
echo "<-- Font Effects -->
";
echo "$UNDERLINE This is UNDERLINE{$DEFAULT}
";
echo "$BLINK This is BLINK //Doesn't seem to work.
";
echo "$INVERSE This is INVERSE{$DEFAULT}
";
echo "$INVISIBLE This is INVISIBLE {$DEFAULT}//Invisible - Pretty pointless
";
echo "$DEFAULT Back to the default.
";
echo "<-- A Few Examples -->
";
echo "{$REDBG}{$YELLOW}This is a red background, with yellow font that {$UNDERLINE}{$YELLOW}is underlined{$DEFAULT}{$REDBG}{$YELLOW} for a portion. {$DEFAULT}
";
echo "{$CYANBG}{$LIGHTRED}This is red text on a cyan background. {$DEFAULT}
";