CI =& get_instance();
}
function GetByDate($school_id = NULL, $date = NULL){
if($school_id == NULL){ $school_id = $this->session->userdata('school_id'); }
if($date == NULL){ $date = date("Y-m-d"); }
$day_num = date('w', strtotime($date));
$query_str = "SELECT * FROM bookings WHERE school_id='$school_id' AND (date='$date' OR day_num=$day_num)";
#echo $query_str;
$query = $this->db->query($query_str);
$result = $query->result_array();
#print_r($result);
return $result;
}
function TableAddColumn($td){
$this->table_headings .= $td;
}
function TableAddRow($data){
$this->table_rows[] = $data;
}
function Table(){
$table = '
' . $this->table_headings . '
';
/* foreach($this->table_rows as $row){
$table .= '
' . $row . '
';
} */
return $table;
}
function BookingCell($data, $key, $rooms, $users, $room_id, $url){
// Check if there is a booking
if(isset($data[$key])){
// There's a booking for this ID, set var
$booking = $data[$key];
if($booking->date == NULL){
// If no date set, then it's a static/timetable/recurring booking
$cell['class'] = 'static';
$cell['body']= '';
} else {
// Date is set, it's a once off staff booking
$cell['class'] = 'staff';
$cell['body'] = '';
}
// Username info
if(isset($users[$booking->user_id])){
$username = $users[$booking->user_id]->username;
$displayname = trim($users[$booking->user_id]->displayname);
if(strlen($displayname) < 2){ $displayname = $username; }
$cell['body'] .= ''.$displayname.'';
$user = 1;
}
// Any notes?
if($booking->notes){
if(isset($user)){ $cell['body'] .= ' '; }
$cell['body'] .= ''.character_limiter($booking->notes, 15).'';
}
// Edit if admin?
/* if($this->userauth->CheckAuthLevel(ADMINISTRATOR, $this->authlevel)){
$edit_url = site_url('bookings/edit/'.$booking->booking_id);
$cell['body'] .= ' ';
$edit = 1;
} */
// Cancel if user is an Admin, Room owner, or Booking owner
$user_id = $this->session->userdata('user_id');
if(
($this->userauth->CheckAuthLevel(ADMINISTRATOR, $this->authlevel)) OR
($user_id == $booking->user_id) OR
( ($user_id == $rooms[$room_id]->user_id) && ($booking->date != NULL) )
){
$cancel_msg = 'Are you sure you want to cancel this booking?';
if($user_id != $booking->user_id){
$cancel_msg = 'Are you sure you want to cancel this booking?\n\n(**) Please take caution, it is not your own booking!!';
}
$cancel_url = site_url('bookings/cancel/'.$booking->booking_id);
if(!isset($edit)){ $cell['body'] .= ' '; }
$cell['body'] .= '';
}
} else {
// No bookings
$book_url = site_url('bookings/book/'.$url);
$cell['class'] = 'free';
$cell['body'] = 'Book';
if($this->userauth->CheckAuthLevel(ADMINISTRATOR, $this->authlevel)){
$cell['body'] .= '';
}
}
#$cell['width'] =
#return sprintf('
%s
', $cell['class'], $cell['body']);
return $this->load->view('bookings/table/bookingcell', $cell, True);
}
function html($school_id = NULL, $display = NULL, $cols = NULL, $date = NULL, $room_id = NULL, $school, $uri = NULL){
if($school_id == NULL){ $school_id = $this->session->userdata('school_id'); }
// Format the date to Ymd
if($date == NULL){
$date = Now();
$date_ymd = date("Y-m-d", $date);
} else {
$date_ymd = date("Y-m-d", $date);
}
// Today's weekday number
$day_num = date('w', $date);
$day_num = ($day_num == 0 ? 7 : $day_num);
// Get info on the current week
$this_week = $this->WeekObj($date, $school_id);
// Init HTML + Jscript variable
$html = '';
$jscript = '';
// Put users into array with their ID as the key
foreach($school['users'] as $user){
$users[$user->user_id] = $user;
}
// Get rooms
$rooms = $this->Rooms($school_id);
if($rooms == False){
$html .= $this->load->view('msgbox/error', 'There are no rooms available. Please see your administrator.', True);
return $html;
}
// Find out which columns to display and which view type we use
$style = $this->BookingStyle($school_id);
if(!$style OR ($style['cols'] == NULL OR $style['display'] == NULL) ){
$html = $this->load->view('msgbox/error', 'No booking style has been configured. Please contact your administrator.', True);
return $html;
}
$cols = $style['cols'];
$display = $style['display'];
// Select a default room if none given (first room)
if($room_id == NULL){
$room_c = current($rooms);
$room_id = $room_c->room_id;
unset($room_c);
}
// Load the appropriate select box depending on view style
switch($display){
case 'room':
$html .= $this->load->view('bookings/select_room', array('rooms' => $rooms, 'room_id' => $room_id, 'chosen_date' => $date_ymd ), True);
break;
case 'day':
$html .= $this->load->view('bookings/select_date', array('chosen_date' => $date), True);
break;
default:
$html .= $this->load->view('msgbox/error', 'Application error: No display type set.', True);
return $html;
break;
}
// Date/room selecter bar
#$selects['date'] = $this->load->view('bookings/select_date', array('chosen_date' => $date), True);
#$selects['room'] = $this->load->view('bookings/select_room', array('rooms' => $rooms), True);
#$html .= $this->load->view('bookings/selects', $selects, True);
// Return error if nothing available
/*if(!$this_week){
$html = $this->load->view('msgbox/error', 'Could not get details of current week - probably no week configured for this date.', True);
#return $html;
}*/
// Get dates for each weekday
if($display == 'room'){
$this_date = strtotime("-1 day", strtotime($this_week->date));
foreach($school['days_list'] as $d_day_num => $d_day_name){
$weekdates[$d_day_num] = date("Y-m-d", strtotime("+1 day", $this_date));
$this_date = strtotime("+1 day", $this_date);
}
}
// Do we have any info on this week name?
if($this_week){
$week_bar['style'] = sprintf('padding:3px;font-weight:bold;background:#%s;color:#%s', $this_week->bgcol, $this_week->fgcol);
// Change the week bar depending on view type
switch($display){
case 'room':
$week_bar['back_date'] = date("Y-m-d", strtotime("last Week", $date));
$week_bar['back_text'] = '<< Previous week';
$week_bar['back_link'] = sprintf('bookings/index/date/%s/room/%s/direction/back', $week_bar['back_date'], $room_id);
$week_bar['next_date'] = date("Y-m-d", strtotime("next Week", $date));
$week_bar['next_text'] = 'Next week >>';
$week_bar['next_link'] = sprintf('bookings/index/date/%s/room/%s/direction/next', $week_bar['next_date'], $room_id);
$week_bar['longdate'] = 'Week commencing '.date("l jS F Y", strtotime($this_week->date));
break;
case 'day':
$week_bar['longdate'] = date("l jS F Y", $date);
$week_bar['back_date'] = date("Y-m-d", strtotime("yesterday", $date));
$week_bar['back_link'] = sprintf('bookings/index/date/%s/direction/back', $week_bar['back_date']);
$week_bar['next_date'] = date("Y-m-d", strtotime("tomorrow", $date));
$week_bar['next_link'] = sprintf('bookings/index/date/%s/direction/next', $week_bar['next_date']);
if(date("Y-m-d") == date("Y-m-d", $date)){
$week_bar['back_text'] = '<< Yesterday';
$week_bar['next_text'] = 'Tomorrow >> ';
} else {
$week_bar['back_text'] = '<< Back';
$week_bar['next_text'] = 'Next >> ';
}
break;
}
$week_bar['week_name'] = $this_week->name;
$html .= $this->CI->load->view('bookings/week_bar', $week_bar, True);
} else {
$html .= $this->load->view('msgbox/error', 'A configuration error prevented the timetable from loading: no week configured.
Please contact your administrator.', True);
#return $html;
$err = true;
}
// See if our selected date is in a holiday
$query_str = "SELECT * "
."FROM holidays "
."WHERE date_start <= '$date_ymd' "
."AND date_end >= '$date_ymd' "
."LIMIT 1";
$query = $this->db->query($query_str);
if($query->num_rows() == 1){
// The date selected IS in a holiday - give them a nice message saying so.
$holiday = $query->row();
$msg = sprintf(
'The date you selected is during a holiday priod (%s, %s - %s).',
$holiday->name,
date("d/m/Y", strtotime($holiday->date_start)),
date("d/m/Y", strtotime($holiday->date_end))
);
$html .= $this->load->view('msgbox/warning', $msg, True);
// Let them choose the date afterwards/before
$next_date = date("Y-m-d", strtotime("+1 day", strtotime($holiday->date_end)));
$prev_date = date("Y-m-d", strtotime("-1 day", strtotime($holiday->date_start)));
switch($uri['direction']){
case 'forward':
default:
$uri['date'] = $next_date;
$html .= '
';
break;
}
#return $html;
$err = true;
}
// Get periods
$query_str = "SELECT * FROM periods WHERE school_id='$school_id' AND bookable=1 ORDER BY time_start asc";
$query = $this->db->query($query_str);
if($query->num_rows() > 0){
$result = $query->result();
foreach($result as $period){
// Check which days this period is for
if($style['display'] == 'day'){
$school['days_bitmask']->reverse_mask($period->days);
if($school['days_bitmask']->bit_isset($day_num)){
$periods[$period->period_id] = $period;
}
} else {
$periods[$period->period_id] = $period;
}
#$days[$day_num] = $school['days_list'][$day_num];
#$days_available[$day_num] = $school['days_list'][$day_num];
}
} else {
$html .= $this->load->view('msgbox/error', 'There are no periods available. Please see your administrator.', True);
#return $html;
$err = true;
}
// If this array isn't set, we don't have any periods configured for *this day*
// If there were no periods at all, user would have been told before reaching this stage.
if(!isset($periods)){
$html .= $this->load->view('msgbox/warning', 'There are no periods configured for this week day. Please choose another date.', True);
return $html;
}
if( isset($err) && $err == true){
return $html;
}
$count['periods'] = count($periods);
$count['rooms'] = count($rooms);
$count['days'] = count($school['days_list']);
#$col_width = sprintf('%d%%', (round($period_count/10) * 100) / $period_count );
$col_width = sprintf('%s%%', round(100/($count[$cols]+1)));
// Open form
$html .= '