Ardışık veri girişlerini kontrol etmek için mantığı ile mücadele

0 Cevap

Ben bu sabah kendimi buldum zor biraz durumun üstesinden nasıl anlamaya çalışıyorum. Ben kullanıcıların aylık girdileri (bilgi yakalama şeyler) hakkında ayrıntılı bilgi depolamak benim veritabanında bir tablo girişleri var - Ben her giriş sayısı (değil ID) ayda bir kez geçti artırmak istiyoruz. Fikir ardışık aylık girdileri tespit etmek ve birbirlerine yakın olan girdileri göz ardı edebilmek için "bir numara" alanını kullanmaktır.

Bir kullanıcı yeni bir giriş başlatmak için siteyi ziyaret ettiğinde, o zaman ben bu yeni için "sayı" arttırmayı o daha 21 gün önce (geçerli bir ay olarak nitelendirir ki) olup olmadığını görmek için tamamlanan son giriş tarihini kontrol entry. Sorun tüm az 21 gün arayla (ve böylece tüm aynı sayıda) girişlerin bir dizi ile sonuna kadar, ama topluca fazla 21 gün span olabilir! Ben bu işlemek için bazı mantık bulmak gerekiyor - Herkes herhangi bir fikir var mı?

Ben yaşıyorum nasıl bu verilerin depolandığı ve sorun bir örnek, aşağıda görülebilir.

+------+--------+------------+------------+----------------------------+
| id   | number | initiated  | updated    | last_category_reached      |
+------+--------+------------+------------+----------------------------+
|    4 |      1 | 1277914181 | 1277914320 | complete                   |
|  105 |      2 | 1282639343 | 1283444717 | complete                   |
|  397 |      3 | 1284999429 | 1285001298 | complete                   |
|  404 |      3 | 1287478550 | 1287478631 | complete                   |
|  636 |      3 | 1287479243 | 1287479377 | complete                   |
|  649 |      3 | 1287581361 | 1287581466 | complete                   |
|  652 |      3 | 1287585123 | 1287585365 | complete                   |
|  656 |      3 | 1290185205 | 1290424128 | complete                   |
| 1105 |      3 | 1292421193 | 1292426686 | complete                   |
| 1106 |      3 | 1292426769 | 1292426870 | complete                   |
+------+--------+------------+------------+----------------------------+

Benim php mantığı altında ...

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

        // If part one, user profile is calling the update (passing the next stage as a param)
        if ($stage === 'user/profile/2?s=p_prof&p=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
            else
            {
                // Check if time difference between last visit and current time is less than 49 days and more than 21 days
                if (($last_entry->initiated > time() - 4233600) && ($last_entry->initiated < time() - 1814400))
                {
                    // Within allowed timeframe, ok to increment by one as a new entry
                    $num = $last_entry->number + 1;
                }
                // More than 49 days since last visit
                elseif (($last_entry->initiated < time() - 4233600)) 
                {
                    // Increment by two to break consecutive entries
                    $num = $last_entry->number + 2;
                }
                // Entry is within the last 21 days - if user never finished stages, use last entry created instead of creating a new one
                else
                {
                    // If they are back at the start having completed a full entry the last time, ok to create a new entry - otherwise use the one created the last time
                    if ($last_entry->last_category_reached !== 'complete')
                        $entry = $last_entry;

                    $num = $last_entry->number;
                }

            }

            // 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->user_id = $this->id;
            $entry->save();
        }
        // If it's been more than 49 days since last part completion of an entry, user won't be given option to finish the entry, so no need for time check here
        elseif ($stage !== NULL)
        {
            // This must be a continuation of a form, not the beginning of a new one
            // 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
     * @return 
     */
    public function last_entry()
    {
            return $this
                    ->limit(1)
                    ->data_entries
                    ->current();
    }

0 Cevap