I have a semi-complicated stored proceedure that I need to create. I'm not exactly a DBA here but no one else in my company is better than I am. The basic idea is I have a group of tasks. The user completes these tasks and gets a flag for each task completed. To be considered "complete" the user has to have all of a group of given tasks finished. The lynch pin is that some of those tasks my be wrappers for other groups of tasks.
For example, we have the following possible tasks:
Task 1
Task 2
Task 3
Task 4 -> (1, 2, 3)
Task 5 -> (1, 2)
Task 6
Task 7 -> (5, 6)
Once a user has completed tasks 1 and 2, they have implicitly completed task 5. Once they finish task 3, they'll have implicitly completed task 4.
If another user finish tasks 1, 2 and 6, they will have implicitly finished 1, 2, 5, 6, 7.
In reverse, if an assignment required only Task 7, it would by extension be requiring tasks 6 and 5, which would required 1 and 2.
I have five basic tables: userCompletedTask, assignmentRequiredTask, compoundTask, userTaskAssignment and task. I omit the user and assignment table as it's superfluous.
userCompletedTask:
userID (Foreign Key on user)
taskID (Foreign Key on task)
usertTaskAssignment:
userID (Foreign key on user)
assignmentID (Foreign key on assignment)
assignmentRequiredTask:
assignmentID (Foreign key on assignment)
taskID (Foreign key on task)
task:
taskID (primary key)
compound (boolean flag. If 1, it is compound)
compoundTask:
parentID (foreign key on task)
childID (foreign key on task)
A user gets assigned a userTaskAssignment which requires task 4 to be completed. I want to build a stored proceedure that would check the userCompletedTasks against the assignmentRequiredTasks, checking if there is appropriate compoundTasks.
The pseudo code would like this:
collection tasksCompleted = user->getTasksCompleted
collection tasksRequired = new collection
foreach task in assignment->getRequiredTasks
if(task.isCompound)
tasksRequired->addAll(getCompountTasks(task))
else
tasksRequired->add(task)
if tasksCompleted->containsAll(tasksRequired)
return true
else
return false
I just don't know MySQL/SQL's internals well enough to translate this into a stored proceedure. Last resort is I pull the code into the application but this would really fit at the data level. Any help would be greatly appreciated.
EDIT
As was pointed out below, compoundTask entries could themselves be compound tasks. Thus you would need a recursive drill to get a list of all the non-compound tasks that would need to be finished. I expanded the example above to exemplify this.