PHP/OOP Help - logic and design issue
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 timef开发者_高级运维rames 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();
}**
Why don't you write a function that calculates the time difference using timestamps.
Then, you will simply compare the difference and execute the right functions accordingly.
So...
<?php
class time_checker{
function difference($last_entry){
$now = time();
$difference = $now - $last_entry;
return difference;
}
}
//Let's use the class just before the entry is stored
$day = 24 * 60 * 60; //Number of seconds in a day because we are using timestamps
$last_entry = get_last_entry($user_id);// Have a function that gets the last entry
$time_check = new time_checker;
$time_since_last_entry = $time_check->difference($last_entry);
$period_1 = 21 * $day; //21 day period
$period_2 = 49 * $day; //49 day period
if($time_since_last_entry < $period_1 ){
//This is what we'll do if there have been less than 21 days since last entry
}
if($time_since_last_entry > $period_2 ){
//This is what we'll do if there have been more than 49 days since last entry
}
?>
精彩评论