I am currently creating an online registration web app that will allow students to register for classes online. The problem that I am now encountering is creating the time conflict algorithm.
As registration goes, every student should be able to register for at least 5 courses, but these courses cannot be all at the same time. So each time a student registers for a class at say 12:00pm - 1:00pm, the next class shouldn't be at the same time, it should be before or after this currently registered class. I have no idea on how to successfully implement the algorithm to get this done. Thank you very much in advance.
Have a CourseEnrollment with user_id, course_id, start_time, end_time. Now when the student registers, via Ajax use Eloquent's hasBetween to check that the current user has a course in that time or not. You can also, with course_id check for course capacity saved in a separate table or compute it on the fry. Check conflict only on complete data entry by the user to reduce the no. of API requests. Also you may have a "check availability" button next to each row, and unless all are checked true, dont allow the user to submit the form. On submit, verify the data and access again and create those entries.
Cheers.
So technically you don't need laravel for this. You could hold all the time slots in an object in JS and do something like the following (not complete code, more like pseudo code)
const slots = {
8to9_available: 'true',
9to10_available: 'true',
//etc...
}
Then we you click on the enroll button, you have a JS function that just checks against that array....
function checkAvailability(slot) //slot should be formatted 8to9available
{
if(slots[slot]) //objects can be accessed as arrays, and this is checking if it's value is true
{
// make an ajax request to register
}else{
// show error message
}
}
This would keep this logic on the front end, perhaps in aVue or a React component and eliminate a DB hit.
j
stuff ;)
I recommend joy014's way otherwise you could have a look at this http://pastebin.com/9gf77Vun which does something similar as a pure php function.
There are bottlenecks in both approaches the database can only keep up so many connections per MB of RAM the php approach is not at all ACID compliant. But since I don't know how your systems scales :) this was curiosity on my side :) there are many different ways to solve this :)