views:

44

answers:

1

I'm building a webapp using the Zend Framework, and I need to model logic or keep track of some logic that has to do with tracking progress towards a goal.

Let me illustrate with a sample goal.

USER Needs to Complete All Three of the following:
A) Activity One
B) Activity Two
C) Activity Three
USER Needs to Complete One of the following:
D) Activity Four
E) Activity Five
F) Activity Six

After completing all three from the first group and one from the second group the USER has completed that goal. How would I go about modeling this in PHP so that the webapp knows the goal has been completed and how would I store the data in the database?

To be clear, there will be many different types of goals like this, but all of them will be quite similar in nature.

+3  A: 

Assuming that A, B & C and D, E, & F will always belong to a specific group within the goal, I would design it thus:

Goal::isComplete()
{
  foreach (Group)
  {
    switch (Group::type())
    {
      case "all":
        TRUE if all complete
        break

      case "any":
        TRUE if any complete
        break;
    }
  }

  if all TRUE
    return TRUE
}

Or in English...

You could then store all of your Activities within an Activities table and define the group they are a part of as a simple id reference to the Groups table. When an Activity is complete, it can be marked thus in the DB.

To check for completed goals, you simply need to look for each Group that is required for the Goal. Each group would either be "all" or "any" (or other such options, like "min-2"), and this would tell the script what to check for with the Activity completions. Each Group can then return TRUE or FALSE depending upon its Activities. Assuming that all Groups are required, the Goal would then easily be identified as Complete or not.

The database can look like:

Activities
- id
- group_id
- name
- completed
- [details about activitiy]

Groups
- id
- goal_id
- type  (ENUM: 'any', 'all')
- completed
- [details about group]

Goals
- id
- completed
- [details about goal]

The Completed values within Groups and Goals would need to be actively updated whenever Activities is updated, or left out and their values always dynamically worked out.

Does this make sense and do what you are requiring?

Valorin
Thank you. This has set me in the right direction.
nvoyageur