PHP / OOP帮助 - 逻辑和设计问题

时间:2010-06-23 09:10:05

标签: php model-view-controller oop

我正在试图弄清楚如何最好地做到这一点,我不认为它应该太难,但我正是这样做的!在我的网站中,每次成员提供一些数据时,我都会将其存储在该成员的条目中。这个想法是,每个月连续提交数据的成员将在某个阶段获得奖励。但是有一些参数:在上次访问后21天内返回网站的成员不会将此计入新条目,而只是与前一个条目相同的另一个提交。同样,如果成员在最后一个输入日期后超过49天返回到站点,则条目号将不会连续但将增加2,表示条目之间的中断。这就是我想出的,允许在正确的时间范围内填写数据的成员之间做出区分 - 希望这一切都有意义!

对于我的代码/设计问题,任何人都可以帮助我在这里改进我​​的代码以及如何最好地在时间框架中添加检查?这是我的模型,我试图管理条目,以便它返回正确的条目(即一个新的 - 由最后一个增加一个或两个,或者从当前时期增加一个)

任何指针都会非常感激!

//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();

}**

1 个答案:

答案 0 :(得分:2)

为什么不编写一个使用时间戳计算时差的函数。

然后,您只需比较差异并相应地执行正确的功能。

因此...

 <?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


    }


    ?>