Problem:
Calendar where user can make booking some type of events for specified hours (depends of availability).
If client signup for specified hour, then this timeslot is marked as booked.

P.S. Any solution, advice or help will be appreciate.

Comments

kenorb’s picture

StatusFileSize
new56.14 KB
new1.09 KB
new810 bytes

First of all we need Calendar and some custom view of day/week/month views.
Copy your tpl.php files from calendar/theme dir into your theme and modify them as you wish.
If you will have problem that template files will not work, look at this: http://drupal.org/node/342614#comment-1148317

In attachment you have example patch for calendar-day.tpl.php theme file (and whole file).
Example with fixed hours slots even there are not any events.
See attachment for demo.
You can change your time range in following line:

    <?php for ($h = 9; $h<=21; $h++): ?> // e.g. FROM 9 to 21
kenorb’s picture

This version example theme for each half an hour (see pic).
Make sure that your grouping settings is set to 'Half hour' in your calendar view.

kenorb’s picture

StatusFileSize
new1.99 KB
new62.2 KB

Lets now do some booking theme.

All you need is to change following lines:

    define('AVAIL_SLOTS', 3); // CHANGE here to set limit if you have one or many slots available in the same time
    define('EVENT_TIME', 3); // for HOW LONG each event should be booked (please put number of half hours, 2 = hour, 3 = hour and half, etc.)
    $slot_booked = t('Booked').'<br>';
    $slot_free = l(t('Book now'),'aa').'<br>'; // CHANGE your link from 'aa' to somewhere where user can book specified event

Now it's configured that there are 3 available slots in one time and each even is 1,5h long (so the real end time of the event will be ignored).

Note that in theme file you can't check permission of specified time slot, if you will not have permission to access specified dates, on different roles there can be different free time slots. But you can easily modify the theme to check if user can create some content and then you can generate message that booking is not available or something.

See screenshot for live example.

P.S. For PHP 4.x you need to change following section into:

    foreach ($booked as $key => $time_left) { // check booking slots
	if (--$booked[$key]<1) { // decrease half an hour and check if it's finished
	    unset($booked[$key]); // if yes, free one slot
	}
    }
kenorb’s picture

Status: Active » Fixed
StatusFileSize
new1.29 KB

Now we need to develop relation between Booking link and creation datetime field.
Make some changes to the above theme:

    $slot_booked = t('Already booked');
    $slot_free = t('Book my event');
    $module_link = 'node/add/my_content_type'; // Change this to pass datetime values to the specified content type

And change content generation to followed:

    /*
     * Set content
     */
    for ($slot=0; $slot<(AVAIL_SLOTS); $slot++) { // now check which slot is...
	if (array_key_exists($slot,$booked) && $booked[$slot]>0) { // ...booked
	    $content .= "<div class='slot_booked'>$slot_booked</div>";
	} else {
	    $link = l($slot_free, $module_link.'/'.$rows['date'].' '.$hh);
	    $content .= "<div class='slot_free'>$link</div>"; // ...and which is free
	}
    }

Now when you click, you will be redirected to page: node/add/my_content_type/2008-12-08 17:20:00

In the form_alter() create something like:

function party_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
	case 'my_content_type_node_form':
	    $fieldname = 'field_my_field';
	    $form[$fieldname][0]['#default_value']['value'] = _get_last_path_arg();
	    $form[$fieldname][0]['#default_value']['value2'] = party_add_time(_get_last_path_arg(),array('hour' => 1, 'minute' => 30)); // here change your period how long even will take time
	    // here inject javascript to hide your datetime field, like: $('#edit-field-my-datetime-0-value-wrapper')[0].parentNode.parentNode.setAttribute('style','display:none'); // hide dates
	break;
    }
}

Above function you will find in attachment.

So basically:
user choosing Booking slot from day view (you can modify your week and month themes as well), then it's going to custom booking form with filled datetime fields and that's it.

Note: of course you can make some your own validation to make sure that user will not book some date when he is not allow to do that.

UPDATE:
After some changes into Drupal website I can't edit my attachment files, so there is small patch for party_add_time() function:
Replace following lines:

    $new_datetime = gmdate('Y-m-d H:i:s',mktime($arr['hour'], $arr['minute'], $arr['second'], $arr['month'], $arr['mday'], $arr['year']));
    return is_array($datetime) ? party_convert_date_to_arr($new_datetime) : $new_datetime;

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

robotjox’s picture

just curious - this sounds awesome! Did you manage to create a fully working booking thingy using calendar with all of the above?

kenorb’s picture

Status: Closed (fixed) » Fixed
StatusFileSize
new11.02 KB

In attachment you will find my first version of module.
I can't promise that it will work at it should, but you can fully customize it (read README.txt)
Definitely it will save some time for people who tried make similar thing.
If somebody is interested with development of this module, I'm interested as well.

karens’s picture

Status: Fixed » Active

I'm going to reopen this. I have a client interested in something similar so we should explore ways to make this easier to do.

kenorb’s picture

Component: Documentation » Code
Category: support » feature

I agree.
I think the first thing is good to implement in Calendar module some settings that will make grid of hours even there are no any events in those slots (like static slots). Option located close to hour and half-hour settings.

BruisedGhost’s picture

I managed to fix the parsing of the clock output from a 24 hr clock to a 12 hr clock.

If you change the following times.

$booked = array();
    for ($h = 10; $h<=16; $h++) {
	for ($half = 0; $half<= (int)($view->style_options['groupby_times'] === 'half'); $half++) { // half-hour style supported if enabled
	    $hh = !$half ? $h.':00:00' : $h.':30:00'; // add minutes and second to the hour
            $test = date("g:i a", strtotime($hh));
	    $hour = array_key_exists($hh,$rows['items']) ? $rows['items'][$hh] : array('hour' => substr($test,0,strlen($hh)-0), 'ampm' => ''); // prepare hour time slot
	    $content = '';

it should fix the problem. but you may need to play with the CSS to get the look of the ampm to be correct.

karens’s picture

You don't have to make any code changes to get static slots whether or not there is data, if you set the slot values using the 'custom' option it will do that already.

MrDruPaul’s picture

This is something that I would be interested in using as well. Subscribing.

redndahead’s picture

kenorb are you planning on starting a drupal project with this? I am needing this at this moment and could provide a few patches to help get it started.

kenorb’s picture

redndahead:
you can attach some patches, in my free time I'll create drupal project for this.
At the moment there are some things hardcoded into the code (and I couldn't find time to do that), so some settings have to be moved into the settings page.
In the first time I didn't know that lot of the people will be interested with this functionality.

kenorb’s picture

Status: Active » Needs work

Ok, anyway, I've created new project:
http://drupal.org/project/booking_timeslots

There are pending issues TODO:
http://drupal.org/project/issues/booking_timeslots

If somebody is interested in development of this module, you're welcome.

dragondad’s picture

I found your half hour day view is typically what I am looking for, unfortunately, I only installed the drupal 5, could someone here suggest the work around of the hour view for drupal 5 calendar, or any suggestion.
Thanks.

kenorb’s picture

Status: Needs work » Fixed

dragondad: Please create new issue for that.
booking_timeslots is done

There is Settings Page so it's configurable.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

GBain22’s picture

When I go to choose my date or datetime field from my specific content type, the field list is not populated - but those fields definately exist in my custom content type - anyone experienced this?

kenorb’s picture