Few words about my idea
Working on some web based, education project. Creating question-answer system for this project. There is some logical difficulties that I can’t figure out:
There are teachers and students between users. Every student selects some course during signup.
I have 5 tables: users, courses, lessons, questions, answers.
There might be 2 types of questions:
-
Userbased. For ex, when teacher wants to send questions to specific users and vise-versa (users may post question to teachers too). -
Course-lessoncombination based. For ex, teacher wants to post questions to all course listeners based on lesson, they are currently in.
What I’ve done
I’ve created questions from-to (qft) table
CREATE TABLE `questions from-to` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`qid` int(11) unsigned NOT NULL,
`from_uid` int(11) unsigned NOT NULL,
`to_uid` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `qft.from_uid_users.id` (`from_uid`),
KEY `qft.to_uid_users.id` (`to_uid`),
KEY `qft.qid_qe.id` (`qid`),
CONSTRAINT `qft.from_uid_users.id` FOREIGN KEY (`from_uid`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `qft.qid_qe.id` FOREIGN KEY (`qid`) REFERENCES `questions_and_exercises` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `qft.to_uid_users.id` FOREIGN KEY (`to_uid`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Which looks like that

qid– is question id, connects this table withquestions, based
onidfrom_uid– from user id (question sender id), connects this table withusers, based
onidto_uid– to user id (question receiver id), connects this table withusers, based
onid
So I tried to create some universal solution for both:
-
students and teacher so they can post questions to each others. ok
-
teacher can post to some users specific questions. ok
BUT there is a problem
The problem is
What if, teacher wants to post question for all course listeners? I already have user-course relationship table, but, currently I need to add one-by-one user id’s into questions from-to table so they can receive questions. I have no idea, how can I design this table for both situations: for "mass questioning" based on course-lesson combination and for "user-user" type question posting (teacher sends questions to specific students and vise-versa)
Any suggestions?
One possibility would be to have a special code in
to_uidthat your application knows to interpret as “show to all users.” When it encounters ato_uidof -1 (or whatever you chose) then it does a select for all students listening to that course, and displays the question just as if their IDs had all been separately listed into_uid. Depending on how your application is organized, you should only have to implement this special check in a small number of places.