From: Steve Sutton Date: Tue, 16 Aug 2016 20:44:24 +0000 (-0400) Subject: Generate lead stats WIP X-Git-Tag: v1.1.0^2~6^2~7 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=1beb21f208691422d47efed7e5f68e2b13cccfd5;p=WP-Plugins%2Fglm-member-db-leads.git Generate lead stats WIP I'm setting up an ajax model which will generate the lead stats. Daily is working so far. Working on Weekly stats. --- diff --git a/models/admin/ajax/statCollector.php b/models/admin/ajax/statCollector.php new file mode 100644 index 0000000..260ae38 --- /dev/null +++ b/models/admin/ajax/statCollector.php @@ -0,0 +1,198 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/** + * Steve Note... + * + * You can get to this using the following URL. + * + * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=statCollector + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +// Load Members data abstract + +/** + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_statCollector +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + const STAT_START_DATE = '2016-01-01'; + + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + /* + * Run constructor for the data class + * + * Note, the third parameter is a flag that indicates to the + * data class that it should flag a group of fields as 'view_only'. + */ + //parent::__construct(false, false, true); + + + } + public function modelAction($actionData = false) + { + // generate the stat entries + $this->dailyStats(); + $this->weeklyStats(); + $this->monthlyStats(); + echo 'done'; + exit; + } + + public function getLastStatDate( $type ) + { + $date = ''; + $date = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT stat_date + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_stats + WHERE stat_type = %s + ORDER BY stat_date DESC + LIMIT 1 + OFFSET 0", + $type + ) + ); + if ( !$date ) { + $date = self::STAT_START_DATE; + } + return $date; + } + + public function dailyStats() + { + $last_stat_date = $this->getLastStatDate( 'daily' ); + $results = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT count(id) as leads_count,date_submitted as daily + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry + WHERE date_submitted < %s + AND date_submitted > %s + GROUP BY daily", + current_time( 'mysql' ), + $last_stat_date + ), + 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', + array( + 'stat_type' => 'daily', + 'stat_date' => $stat['daily'], + 'leads_count' => $stat['leads_count'] + ), + array( + '%s', + '%s', + '%d' + ) + ); + } + } + } + + public function weeklyStats() + { + $last_stat_date = $this->getLastStatDate( 'weekly' ); + echo '
$last_stat_date: ' . print_r( $last_stat_date, true ) . '
'; + $results = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT count(id) as leads_count,DATE_FORMAT(date_submitted, '%Y%U') as weekly + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry + WHERE date_submitted < %s + AND date_submitted > %s + GROUP BY weekly", + current_time( 'mysql' ), + $last_stat_date + ), + ARRAY_A + ); + echo '
$results: ' . print_r( $results, true ) . '
'; + if ( $results ) { + echo '
$results: ' . print_r( $results, true ) . '
'; + /* + foreach ( $results as $stat ) { + if ( preg_match( '%/d{4}/d{2}%', $stat['weekly'], $matches ) ) { + $date = new DateTime( $matches[1], $matches[2] ); + } else { + return false; + } + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_stats', + array( + 'stat_type' => 'weekly', + 'stat_date' => $date->format( 'P-m-d' ), + 'leads_count' => $stat['leads_count'] + ), + array( + '%s', + '%s', + '%d' + ) + ); + } + */ + } + } + + public function monthlyStats() + { + $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 ); + if ( $results ) { + echo '
$results: ' . print_r( $results, true ) . '
'; + } + } +} diff --git a/setup/validActions.php b/setup/validActions.php index ccba4e7..8d96b85 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -34,6 +34,7 @@ $glmMembersLeadsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( 'csvExport' => GLM_MEMBERS_LEADS_PLUGIN_SLUG, + 'statCollector' => GLM_MEMBERS_LEADS_PLUGIN_SLUG, ), 'leads' => array( 'index' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,