From: Steve Sutton Date: Wed, 24 Aug 2016 19:07:15 +0000 (-0400) Subject: Updating weekly monthly stats X-Git-Tag: v1.1.0^2~6^2~5 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=3e8631ca52d4528ff2f1575019de470818c7774b;p=WP-Plugins%2Fglm-member-db-leads.git Updating weekly monthly stats The weekly stats and monthly stats are now being created and added into the db. --- diff --git a/models/admin/ajax/statCollector.php b/models/admin/ajax/statCollector.php index 28846f0..9da44bb 100644 --- a/models/admin/ajax/statCollector.php +++ b/models/admin/ajax/statCollector.php @@ -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 '
$results: ' . print_r( $results, true ) . '
'; 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 '
$last_stat_date: ' . print_r( $last_stat_date, true ) . '
'; + 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 '
$sql: ' . print_r( $sql, true ) . '
'; - //echo '
$num_rows: ' . print_r( $this->wpdb->num_rows, true ) . '
'; - echo '
$time: ' . print_r( current_time( 'Y-m-d' ), true ) . '
'; if ( $results ) { - //echo '
$results: ' . print_r( $results, true ) . '
'; foreach ( $results as $stat ) { if ( preg_match( '%(\d{4})(\d{2})%', $stat['weekly'], $matches ) ) { - echo '
$matches: ' . print_r( $matches, true ) . '
'; $date = date( "Y-m-d", strtotime( $matches[1]."W".$matches[2]."0" ) ); - echo '
$date: ' . print_r( $date, true ) . '
'; } 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 '
$results: ' . print_r( $results, true ) . '
'; + 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' + ) + ); + } + } } } }