views:

72

answers:

0

Hi, I'm going round in circles trying to work out how best to do this, i don't think it should be too difficult but i'm making it that way! In my site, each time a member provides some data, I store it against an Entry for that member. The idea is that members who submit data consecutively from month to month will be rewarded at some stage. There are some parameters though: a member who returns to the site within 21 days of their last visit would not have this counted as new Entry, but just another submission for the same entry period as the previous one. Likewise, if the member returns to the site more than 49 days after the last Entry date, then the Entry number will not be consecutive but will be incremented by two, showing a break between the entries. This is what I've come up with to allow distinctions to be made between those members who fill in the data within the correct timeframes - hope it all makes sense!

And to my code/design issue, can anyone help me to improve my code here and how best to add in the timeframes check? This is my model, which i'm trying to get to manage the entries, so that it returns the correct entry (i.e a new one - incremented by the last one by one or two, or an already existing one from the current period).

Any pointers would be really appreciated!

//example call from a controller after successful form submission - $this->entry is then passed around for use within that session
$this->entry = $this->pet->update_entry('pet/profile/2');


public function update_entry($stage = NULL)
{
    //get last number entered for this pet
    $last_entry = $this->last_entry();

    //if part one, pet profile is calling the update (passing the next stage as a param)
    if ($stage === 'pet/profile/2')
    {
        //only at this stage do we ever create a new entry
        $entry = ORM::factory('data_entry');

        //if no previous sessions, start from 1
        if ($last_entry === FALSE)
            $num = 1; 
        //here we need to check the time period elapsed since the last submission, still to be ironed out   
        //for now just increment each time, but this may change 
        else
            $num = $last_entry->number + 1;

        //save the rest of the data for a new entry
        $entry->number = $num;
        $entry->initiated = time();
        $entry->updated = time();
        $entry->last_category_reached = $stage;
        $entry->pet_id = $this->id;
        $entry->save();
    }
    elseif ($stage !== NULL)
    {
        //echo $stage; correctly prints out stage
        //this must be a continuation of a form, not the beginning of a new one
        //timeframe checks to be added here
        //just update the stage reached and save
        $last_entry->last_category_reached = $stage;
        $last_entry->updated = time();
        $last_entry->save();
        //assign to $entry for return
        $entry = $last_entry;
    }

    return $entry;
}

/**
 * 
 * Returns the the last data entry session
 */
public function last_entry()
{
        return $this
                    ->limit(1)
                    ->data_entries
                    ->current();

}**