2010-06-23 3 views
2

나는 이것을하기에 가장 좋은 방법을 찾으려 고 노력하면서 원을 돌고 있습니다. 너무 어려워 야한다고 생각하지 않지만 그런 식으로 만들고 있습니다! 내 사이트에서 회원이 데이터를 제공 할 때마다 해당 회원의 항목에 대해 저장합니다. 한 달에 한 번씩 연속적으로 데이터를 제출하는 회원에게는 어떤 단계에서 보상이 주어질 것이라는 아이디어가 있습니다. 그러나 일부 매개 변수가 있습니다. 마지막 방문 후 21 일 이내에 사이트로 돌아온 회원에게는 새 항목으로 계산되지 않지만 이전 항목과 동일한 항목 기간에 대해서는 다른 항목으로 계산됩니다. 마찬가지로 회원이 마지막 입국 일로부터 49 일 이상 사이트로 돌아 오면 입장 번호는 연속되지 않고 2 점씩 증가하여 항목간에 휴식을 표시합니다. 정확한 시간 내에 데이터를 채우는 회원간에 구별이 가능하도록하기 위해 내가 생각해 왔던 것입니다. 모든 것이 의미가 있기를 바랍니다.PHP/OOP 도움말 - 논리 및 디자인 문제

내 코드/디자인 문제에 대해서는 누구나 내 코드를 개선하는 데 도움이 될 수 있으며 시간 측정에 추가하는 것이 가장 좋습니다. 이것은 내 모델입니다. 그래서 항목을 관리하여 정확한 항목을 반환합니다 (즉, 새 항목 - 마지막 항목이 하나 또는 두 개씩 증가하거나 현재 기간의 항목이 이미 증가됨) .

모든 포인터가 정말 감사하겠습니다!

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

}** 

답변

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 


    } 


    ?>