Updating weekly monthly stats
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 24 Aug 2016 19:07:15 +0000 (15:07 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 24 Aug 2016 19:07:15 +0000 (15:07 -0400)
The weekly stats and monthly stats are now being created and added into
the db.

models/admin/ajax/statCollector.php

index 28846f0..9da44bb 100644 (file)
@@ -78,9 +78,9 @@ class GlmMembersAdmin_ajax_statCollector
     public function modelAction( $actionData = false )
     {
         // generate the stat entries
-        //$this->dailyStats();
+        $this->dailyStats();
         $this->weeklyStats();
-        //$this->monthlyStats();
+        $this->monthlyStats();
         echo 'done';
         exit;
     }
@@ -121,7 +121,6 @@ class GlmMembersAdmin_ajax_statCollector
             ARRAY_A
         );
         if ( $results ) {
-            echo '<pre>$results: ' . print_r( $results, true ) . '</pre>';
             foreach ( $results as $stat ) {
                 $this->wpdb->insert(
                     GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_stats',
@@ -143,7 +142,11 @@ class GlmMembersAdmin_ajax_statCollector
     public function weeklyStats()
     {
         $last_stat_date = $this->getLastStatDate( 'weekly' );
-        echo '<pre>$last_stat_date: ' . print_r( $last_stat_date, true ) . '</pre>';
+        if ( $last_stat_date != self::STAT_START_DATE ) {
+            $stat_date = new DateTime( $last_stat_date );
+            $stat_date->modify( '+ 2 week' );
+            $last_stat_date = $stat_date->format( 'Y-m-d' );
+        }
         $results = $this->wpdb->get_results(
             $this->wpdb->prepare(
                 "SELECT count(id) as leads_count,DATE_FORMAT(date_submitted, '%%Y%%U') as weekly
@@ -156,16 +159,10 @@ class GlmMembersAdmin_ajax_statCollector
             ),
             ARRAY_A
         );
-        //echo '<pre>$sql: ' . print_r( $sql, true ) . '</pre>';
-        //echo '<pre>$num_rows: ' . print_r( $this->wpdb->num_rows, true ) . '</pre>';
-        echo '<pre>$time: ' . print_r( current_time( 'Y-m-d' ), true ) . '</pre>';
         if ( $results ) {
-            //echo '<pre>$results: ' . print_r( $results, true ) . '</pre>';
             foreach ( $results as $stat ) {
                 if ( preg_match( '%(\d{4})(\d{2})%', $stat['weekly'], $matches ) ) {
-                    echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
                     $date = date( "Y-m-d", strtotime( $matches[1]."W".$matches[2]."0" ) );
-                    echo '<pre>$date: ' . print_r( $date, true ) . '</pre>';
                 } else {
                     return false;
                 }
@@ -190,13 +187,52 @@ class GlmMembersAdmin_ajax_statCollector
 
     public function monthlyStats()
     {
+        $last_stat_date = $this->getLastStatDate( 'monthly' );
+        if ( $last_stat_date != self::STAT_START_DATE ) {
+            $stat_date = new DateTime( $last_stat_date );
+            $stat_date->modify( '+ 2 month' );
+            $last_stat_date = $stat_date->format( 'Y-m-d' );
+        }
         $sql = "
           SELECT count(id) as leads_count,DATE_FORMAT(date_submitted, '%Y%m') as monthly
             FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry
         GROUP BY monthly";
-        $results = $this->wpdb->get_results( $sql );
+        //$results = $this->wpdb->get_results( $sql );
+        $results = $this->wpdb->get_results(
+            $this->wpdb->prepare(
+                "SELECT count(id) as leads_count,DATE_FORMAT(date_submitted, '%%Y%%m') as monthly
+                   FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry
+                  WHERE date_submitted < %s
+                    AND date_submitted > %s
+               GROUP BY monthly",
+                current_time( 'Y-m-d' ),
+                $last_stat_date
+            ),
+            ARRAY_A
+        );
         if ( $results ) {
-            echo '<pre>$results: ' . print_r( $results, true ) . '</pre>';
+            foreach ( $results as $stat ) {
+                if ( preg_match( '%(\d{4})(\d{2})%', $stat['monthly'], $matches ) ) {
+                    $date = date( "Y-m-d", strtotime( $matches[1]."-".$matches[2]."-01" ) );
+                } else {
+                    return false;
+                }
+                if ( $date ) {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_stats',
+                        array(
+                            'stat_type'   => 'monthly',
+                            'stat_date'   => $date,
+                            'leads_count' => $stat['leads_count']
+                        ),
+                        array(
+                            '%s',
+                            '%s',
+                            '%d'
+                        )
+                    );
+                }
+            }
         }
     }
 }