PHP / MySQL search schedule with templates and overrides

I am looking for some tips / help on a rather sophisticated search algorithm. Any articles on relevant methods, etc. Would be highly appreciated.

Background

I am creating an application that, in a nutshell, allows users to set their “availability” for any given day. The user first sets up a common accessibility template that allows them to say:

Monday - AM   
Tuesday - PM  
Wednesday - All Day  
Thursday - None  
Friday - All Day

Thus, this user is usually available on Monday AM, Tuesday, etc.

Scheme:

id  
user_id  
day_of_week  (1-7)(Monday to Sunday)
availability

They can then manually override specific dates, for example:

2013-03-03 - am  
2013-03-04 - pm  
2013-03-05 - all_day

Scheme:

id
user_id
date
availability

All of this works well - I have a calendar that integrates the template and redefines and allows users to change their availability, etc.

Problem

Admin , . , Admin , .

, , :

2013-03-03 - pm
2013-03-04 - pm
2013-03-05 - pm

, Templated Availability Overrides, . , , , , , , .

, , , , ..

. , , , . , , , " ".

, :

user_id
body

body , :

user_id: 2
body: monday_am tuesday_pm wednesday_pm thursday_am friday_allday 2013-03-03_all_day 2013-03-03_pm

. , -, 19 2013 - 20 2013 - PM, .

-, 19 - , tuesday_allday 20-. :

tuesday_allday wednesday_pm 2013-03-19_allday 2013-03-20_pm

"" , .

, , , - , .

+5
2

, . , ( am pm), . , ( , , ).

, , , . ( , SQL )

select statement, . , SELECT SQL @ . , , , ( ). (, , , , , , , , , , , , ... @, () , ).

: ( SQL CREATE TABLE) DaysOfWeek. - TimeFrames , ( AM 00:00:00 11:59:59, PM StartTime 12:00:00 EndTime 23:59:59) - - , (. SQL , ) - - ( ) - , . , (, , , ...) : , , /, ... (, , ). , 12 , , , , StackOverflow , .

, . , ( , , "", , , , ).

SELECT (. view... , , , /, WHERE, ... WHERE... , ):

SELECT *
FROM Users Us
JOIN Availabilities Av
ON Us.User_ID=Av.User_ID
JOIN Dates Da
ON Av.Date_ID=Da.Date_ID
JOIN AvailableTimes Avt
ON Av.Av_ID=Avt.Av_ID
WHERE Da.Date='2014-01-03' -- whatever date 
-- alternately: WHERE Da.DayOWeek_ID=3 -- which would be Wednesday
-- WHERE Da.Date BETWEEN() -- whatever date range...
-- etc...

DaysOfWeek ( ):

INSERT INTO DaysOfWeek(DayOWeek_ID,Name,Description)
VALUES (1,'Sunday', 'First Day of the Week'),(1,'Monday', 'Second Day of the Week')...(7,'Saturday', 'Last Day of the Week'),(8,'AllWeek','The entire week'),(9,'Weekdays', 'Monday through Friday'),(10,'Weekends','Saturday & Sunday')

:

INSERT INTO Templates(Time_ID,User_ID,DayOWeek_ID)
VALUES (1,1,9)-- this would show the first user is available for the first time frame every weekday as their default... 
,(1,2,2) -- this would show the first user available on Tuesdays for the second time frame

:

CREATE  TABLE `test`.`Users` (

User_ID INT NOT NULL AUTO_INCREMENT, UserName VARCHAR (45) NULL, (User_ID));

CREATE  TABLE `test`.`Templates` (
  `Template_ID` INT NOT NULL AUTO_INCREMENT ,
  `Time_ID` INT NULL ,
  `User_ID` INT NULL ,
  `DayOWeek_ID` INT NULL ,
  PRIMARY KEY (`Template_ID`) )
`COMMENT = 'This table holds the template data for general expected availability of a user/agent/person (so the person would use this to set their general availability)'`;

CREATE  TABLE `test`.`Availabilities` (
  `Av_ID` INT NOT NULL AUTO_INCREMENT ,
  `User_ID` INT NULL ,
  `Date_ID` INT NULL ,
  PRIMARY KEY (`Av_ID`) )
COMMENT = 'This table holds a users actual availability for a particular date.\nIf the use is not available for a date then this table has no entry for that user for that date.\n(btw, this suggests the possiblity of an alternate table that could utilize all other structures except the templates called Engagements which would record when a user is actually busy... in order to use this table & the other table together would need to always join to AvailableTimes as a date would actually be in both tables but associated with different time frames).';

CREATE  TABLE `test`.`Dates` (
  `Date_ID` INT NOT NULL AUTO_INCREMENT ,
  `DayOWeek_ID` INT NULL ,
  `Date` DATE NULL ,
  PRIMARY KEY (`Date_ID`) )
COMMENT = 'This table is utilized to hold actual dates whith which users/agents can be associated.\nThe important thing to note here is: this may end up holding every day of every year... this suggests a need to archive this data (and everything associated with it for performance reasons as this database is utilized).\nOne more important detail... this is more efficient than associating actual dates directly with each user/agent with an availability on that date... this way the date is only recorded once, the other approach records this date with the user for each availability.';

 CREATE  TABLE `test`.`AvailableTimes` (
  `AvTime_ID` INT NOT NULL AUTO_INCREMENT ,
  `Av_ID` INT NULL ,
  `Time_ID` INT NULL ,
  PRIMARY KEY (`AvTime_ID`) )
COMMENT = 'This table records the time frames that a user is available on a particular date.\nThis allows the time frames to be flexible without affecting the structure of the DB.\n(e.g. if you only keep track of AM & PM at the beginning of the use of the DB but later decide to keep track on an hourly basis you simply add the hourly time frames & start populating them, no changes to the DB schema need to be made)';

CREATE  TABLE `test`.`TimeFrames` (
  `Time_ID` INT NOT NULL AUTO_INCREMENT ,
  `StartTime` TIME NOT NULL ,
  `EndTime` TIME NOT NULL ,
  `Name` VARCHAR(45) NOT NULL ,
  `Desc` VARCHAR(128) NULL ,
  PRIMARY KEY (`Time_ID`) ,
  UNIQUE INDEX `Name_UNIQUE` (`Name` ASC) )
COMMENT = 'Utilize this table to record the times that are being tracked.\nThis allows the flexibility of having multiple time frames on the same day.\nIt also provides the flexibility to change the time frames being tracked without changing the DB structure.';

CREATE  TABLE `test`.`DaysOfWeek` (
  `DaysOWeek_ID` INT NOT NULL AUTO_INCREMENT ,
  `Name` VARCHAR(45) NOT NULL ,
  `Description` VARCHAR(128) NULL ,
  PRIMARY KEY (`DaysOWeek_ID`) ,
  UNIQUE INDEX `Name_UNIQUE` (`Name` ASC) )
COMMENT = 'This table is a lookup table to hold the days of the week.\nI personally would recommend adding a row for:\nWeekends, All Week, & WeekDays \nThis will often be used in conjunction with the templates and will allow less entries in that table to be made with those 3 entries in this table.';
+1

, :

  • , ... .
  • pm, am both .
  • db .
  • user/date/meridian ( am pm). pm, am both.
  • php, switch .
  • .
  • / user/date/meridian / .

, , . , , , . , , .

0

All Articles