A better less intensive script:
(untested still - Revision 2 – Editted for more appropriate date updating, suits collision limits greater than 1 now)
// Haven't researched if this is possible too generate an array of events
// however I can generate the events in the section if required, using my own code.
var myEvents = connector.getEvents('select *RequiredFields* FROM *TableName* where sectionId=' + event.section_id + ' order by start_date);
// a variable to let us know if we have slotted in the event to an available timeslot
var hasInserted = false;
// a variable too hold the length of an event in seconds
var eventLength = dateDiff(event.start_date, event.end_date);
for(i; i<myEvents.length; i++)
{
// Editted, I was thinking only of my use case, start_date will work better with
// more than 1 set for the collision limit
var myEventStartDate = new Date(myEvents[i].start_date);
var eventEndDate = new Date(event.end_date)
while(myEventStartDate < eventEndDate)
{
// Hasn't inserted, so increment date to current events start Date.
if(hasInserted == false)
{
event.start_date = new Date(myEventStartDate);
event.end_date = new Date(myEventStartDate);
event.end_date.addSeconds(eventLength);
}
// Get a variable to hold the current events in the current section
var currentEvents = scheduler.getEvents(event.start_date, event.end_date, event.section_id);
// Set the inserted variable to true as this is a valid position for the variable.
if(currentEvents.length < COLISSION_LIMIT)
{
hasInserted = true;
}
else
{
var lowestCurrentDate = new Date(myEvents[i].end_date);
// Loop through the events in the current block and get the lowest end_date for the event to slot after.
for(j; j<currentEvents.length; i++)
{
// Get the current loops event's end date.
var currentEndDate = new Date(currentEvents[j].end_date);
// Set the end date the lowest end date, unless it is the currently editted event.
if(currentEndDate<lowestCurrentDate && currentEvents[j].id != event.id)
{
lowestCurrentDate = new Date(currentEndDate);
}
}
// Increment the start date for the next stage of the outer loop.
myEventStartDate = new Date(lowestCurrentDate);
}
}
}
if(hasInserted == false)
{
var highestCurrentDate = new Date(myEvents[0].end_date);
// Loop through the events in the current block and get the latest end date for the event to slot after
for(k; k<myEvents.length; k++)
{
var currentEndDate = new Date(myEvents[k].end_date);
if(currentEndDate>highestCurrentDate)
{
highestCurrentDate = new Date(currentEndDate);
}
}
// Editted to cast variables as a date variable
event.start_date = new Date(highestCurrentDate);
event.end_date = new Date(highestCurrentDate);
event.end_date.addSeconds(eventLength);
}
scheduler.setCurrentview(new Date(event.start_date));
It isn’t too much code, and would only need too be called when the event is copied/pasted.
Kind regards
Greg