Generate lead stats WIP
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 16 Aug 2016 20:44:24 +0000 (16:44 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 16 Aug 2016 20:45:35 +0000 (16:45 -0400)
I'm setting up an ajax model which will generate the lead stats.
Daily is working so far.
Working on Weekly stats.

models/admin/ajax/statCollector.php [new file with mode: 0644]
setup/validActions.php

diff --git a/models/admin/ajax/statCollector.php b/models/admin/ajax/statCollector.php
new file mode 100644 (file)
index 0000000..260ae38
--- /dev/null
@@ -0,0 +1,198 @@
+<?php
+
+/**
+ * Gaslight Media Members Leads Database
+ * CSV Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @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 '<pre>$results: ' . print_r( $results, true ) . '</pre>';
+            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 '<pre>$last_stat_date: ' . print_r( $last_stat_date, true ) . '</pre>';
+        $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 '<pre>$results: ' . print_r( $results, 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 ) ) {
+                    $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 '<pre>$results: ' . print_r( $results, true ) . '</pre>';
+        }
+    }
+}
index ccba4e7..8d96b85 100644 (file)
@@ -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,