Page 1 of 2

How to allow only one user at a time.

Posted: Wed Feb 24, 2010 10:19 am
by rizjav_86
I am developing a School System in PHP. I am facing a very tough time, hope you guys can help me !

Lets consider the following situation:

Student1 has a grade="" in English and Math.
Teacher1 opens Student1's profile in her browser to enter the grade for English class.
At the same time Teacher2 opens Student1's profile in her browser to enter the grade for Math class.

Now initially both Teachers will see English="" and Math="", but let say Teacher1 enters "A" in English and hits SAVE. now database has English="A" and Math="".

BUT at this time Teacher2's browser is still showing English="" and Math="" and now if Teacher2 enters Math="B" and hits SAVE then it will change English="" i.e. it will overwrite Teacher1's entry.

Question: How can I dynamically upgrade the screens of all users? or How can I allow only one Teacher to enter grades at a time?

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 11:03 am
by Salaria
Hi,

Use AJAX to update pages on some time gap say 5 sec. Also use Lock Table so that data will not get updated with empty fields.

You can also provide access to only single or multiple subjects updates those they teach.

Hope this will give you some idea to handle this situation.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 12:24 pm
by rizjav_86
Salaria wrote:Hi,

Use AJAX to update pages on some time gap say 5 sec. Also use Lock Table so that data will not get updated with empty fields.

You can also provide access to only single or multiple subjects updates those they teach.

Hope this will give you some idea to handle this situation.
Thanks for the reply !

I gave that English and Math example just to describe the situation but in reality I have pages with lots of information showing all newly enrolled students with ALL of their information and more than one people are allowed to access them and modify them.

refreshing after every 5 seconds increases the server load (there are thousands of students and hundreds of teachers, this system will cover like 12-13 schools...).

Basically what I need is that if there are 100 input fields on a page and if a user is editing them then show the changes made by that user to all logged in users asynchronously (like Google wave ?)...

I tried to use variables to lock/unlock pages with "application scope" but then it locks the entire page...my boss needs me to show changes made by one user to all the other users asynchronously or to disable only the field in which a user is changing data.

If I use lock/unlock and a user comes in to enter data, but after let say 5 minutes the user goes for lunch or something, then it will keep the page locked for other users until the first user does not "logs out" !

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 12:35 pm
by AbraCadaver
You said asynchronously, so AJAX. You also mentioned Google Wave, so AJAX. This is your only option as I see it.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 1:28 pm
by rizjav_86
AbraCadaver wrote:You said asynchronously, so AJAX. You also mentioned Google Wave, so AJAX. This is your only option as I see it.
Thanks for the reply. I have never done this type of AJAX programming which updates ALL user's browsers simultaneously. Could you please show me a sample code or any tutorial link?

I mean how to do this: If User1 hits save in his browser, then User2's browser should also reflect the new changes.

I know the idea is to keep on checking database for any changes, and then reflect the changes in the browser. But I have never done this thing, any tutorial links would be awesome.

Thanks again !

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 1:56 pm
by AbraCadaver
rizjav_86 wrote:
AbraCadaver wrote:You said asynchronously, so AJAX. You also mentioned Google Wave, so AJAX. This is your only option as I see it.
Thanks for the reply. I have never done this type of AJAX programming which updates ALL user's browsers simultaneously. Could you please show me a sample code or any tutorial link?

I mean how to do this: If User1 hits save in his browser, then User2's browser should also reflect the new changes.

I know the idea is to keep on checking database for any changes, and then reflect the changes in the browser. But I have never done this thing, any tutorial links would be awesome.

Thanks again !
For AJAX just search on xmlhttprequest or AJAX PHP. With this approach the client browser would use AJAX to fetch the DB info for the record every several seconds or so making sure it is somewhat up to date.

The other options are as you have stated, locking the record for a certain amount of time. If someone else tries to access it within say 5 minutes of the lock it won't let them edit, however if it has been more than the time then expire the old lock and add a new one. If the original person comes back 10 minutes later and tries to save, then it will notice the new lock and generate an error.

Another option would be to store the old data in the session when the edit page is loaded. Then when you submit the new data, check to make sure that the data in the database is the same as the old data in the session and then insert it. If not, then generate an error and redisplay the edit page.

Just thoughts...

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 2:16 pm
by Kurby
Wouldn't you only allow certain cells to be edited by certain users? A math teacher shouldn't be updating a science grade. Also, wouldn't the grade be done on INSERTS? I guess it would depend on your database schema but I would assume you have some sort of students_grades relational table. That why you only insert one grade at a time and don't have to worry about overwriting things.

I think its a better option than reloading everybody every 5 seconds.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 2:18 pm
by Kurby
Can you give an idea of your database structure? Are teachers related 1 to 1 with a subject/department? Are students and grades 1 to 1 with a subject....

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 3:36 pm
by rizjav_86
I think I gave a wrong example of the English and Math teacher. Each teacher is allowed to view/modify the grades of only her subjects.

The problem comes when the people in "admissions office" are entering data. Let say 3 people in admissions office get a list of students who will be coming back to school in 2010 Summer. And each of them starts entering the information, now if I don't asynchronously displays the changes to ALL users then there will be high chances of data overwriting.

@AbraCadaver: I know about XMLHTTPRequest, but it's good only for one user, e.g. if both of us have a same page open in browser then you will not gonna see my changes until I hit SUBMIT. I want to update ALL USERS as soon as anyone of them makes any change.

@Kurby: Yes you are right, each teacher only enters grades for her class. I gave a wrong example.

@Kurby: The database is huge it has 1-1, 1-M, M-1, M-M all relations, there are about 100 tables... The problem comes in when multiple users are allowed to enter information on the same page e.g. people in admission's office, registrar's office etc.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 3:53 pm
by AbraCadaver
It's nearly impossible to do what you are trying to do without extensive AJAX. The page would need to update a temporary table on each onchange() event of each field and that page would also have to check the temporary table constantly for other changes. So a user is sitting there watching all the values they just changed change to something else. :crazy: I won't elaborate as this requiremnet is not sound.

Use the locking approach and don't let users edit a record that is being edited by someone else.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 4:11 pm
by Kurby
Its an interesting dilemma. From your original question: "How can I dynamically upgrade the screens of all users?". My answer: you can't do it easily and shouldn't need to.

Your other question: How can I allow only one Teacher to enter grades at a time?
This one I think you can do. You can have some sort of "checkedOut" field for each record. Set it to 1 when their profile is accessed to somebody and set it to 0 when the page is closed or after a timed period. Seems a little messy to me, but if you deem it necessary.

Another option:
When a user access a page of info, store that information somewhere. Just before submitting, requery the database to see if any changes have been made. If none, make an update.

I think part of the problem is an error in the business practices. There shouldn't be multiple people entering information for the same person at the same time. If people are modifying separate parts of a persons info, perhaps those parts should be segmented.

Hope this helps.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 4:15 pm
by Kurby
Perhaps you would be interested in how Google Wave does its concurrent changes by multiple users.

http://www.justusleeg.com/2009/05/29/ho ... wave-work/

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 4:20 pm
by AbraCadaver
Now that I re-read your first post I see that you are worried about a field being "emptied" if someone saves data with that field blank. This is no problem as you just don't specify that field and it won't be updated. As long as you don't specify a value for a field, it won't get updated:

Code: Select all

UPDATE `grades` SET `english`='A' WHERE `id`=1
UPDATE `grades` SET `math`='B' WHERE `id`=1
Now for row id=1, math is B and english is still A.

Re: How to allow only one user at a time.

Posted: Wed Feb 24, 2010 7:12 pm
by rizjav_86
Thanks guys for all the thoughtful responses, I will go with the lock/unlock technique.

Re: How to allow only one user at a time.

Posted: Thu Feb 25, 2010 1:25 am
by kaisellgren
Here's a simple solution that works:

1) Display the form and store the current value in a session.
2) Upon submission, lock the table, check if the value has changed.
3) If the value has changed, abort and display a message stating that the value has been changed and redisplay the form.
4) Otherwise, just update it.

Pretty much like this forum does -- if you post a message and someone has replied in between the last read and your post time, it will show a message "there's a new post, want to post anyway?".