--- sites/all/modules/userpoints/userpoints_views.module	Mon Feb 11 09:00:45 2008
+++ sites/all/modules/userpoints/userpoints_views.module	Mon Feb 11 09:38:49 2008
@@ -42,11 +42,11 @@
           '#description' => 'title|destination<br/>$uid is a placeholder.',
         ),
         'help' => t('Displays a link to something, based on the node author.'),
         'field' => 'uid',
         'handler' => 'views_handler_field_userpoints_link_uid',
-      ),
+      ),
     ),
     'sorts' => array(
       'points' => array(
         'name' => t('Node: Author Points'),
         'help' => t("Sort by the node's author current number of userpoints"),
@@ -221,10 +221,19 @@
       ),
       'operation' => array(
         'name' => t('Userpoints: Transaction Operation'),
         'sortable' => TRUE,
         'help' => t('Displays the operation which caused the transaction.'),
+      ),
+      'points_sum' => array(
+        'name' => t('Userpoints: Points This Month'), 
+        'help' => t('Displays the accumulated points for a given user this month'),
+        'notafield' => true,
+        'sortable' => false,
+        'option' => 'string',
+        'handler' => 'userpoints_views_views_points_handler_thismonth',
+        'query_handler' => 'userpoints_views_views_points_query_handler_thismonth'
       ),
     ),
     'sorts' => array(
       'points' => array(
       'name' => t('Userpoints: Transaction Points'),
@@ -235,11 +244,42 @@
         'handler' => 'views_handler_sort_date',
         'option' => views_handler_sort_date_options(),
         'help' => t("Sort by the transaction time"),
       ),
     ),
-    'filters' => array(
+    'filters' => array(
+      'month' => array(
+        'field' => 'time_stamp',
+        'name' => t('Userpoints: Month'),
+        'operator' => 'views_handler_operator_gtlt',
+        'list' => array(
+          '',
+          'January',
+          'February',
+          'March',
+          'April',
+          'May',
+          'June',
+          'July',
+          'August',
+          'September',
+          'October',
+          'November',
+          'December'
+        ),
+        'list-type' => 'select',
+        'handler' => 'userpoints_views_handler_filter_month',
+        'type' => 'MONTHNAME',
+        'help' => t('Filter by month. Use the option to select the date field to filter on.'),
+      ),
+      'userpoints_thismonth' => array(
+        'field' => 'time_stamp',
+        'name' => t('Userpoints: This Month'),
+        'operator' => 'views_handler_operator_gtlt',
+        'handler' => 'userpoints_views_handler_filter_month',
+        'help' => t('Display user points acquired this month')
+      )
     ),
   );
   
   return $tables;
 }
@@ -456,5 +496,80 @@
   if (!isset($stati)) {
     $stati = userpoints_txn_status();
   }
   return $stati[$value];
 }
+
+/**
+ * userpoints_views_views_points_handler_thismonth
+ *
+ * Handler function to handle the calculation of the userpoints data for the current month
+ *
+ * @since v1.0.0
+ * @access  public
+ *  @param  mixed $fieldinfo The array from the $tables data for this particular field. It will contain the information from this very list.
+ * @param mixed $fielddata The information on the field from the database.
+ * @param string/int $value The actual value retrieved from the database.
+ * @param obj $data The entire record from the database.
+ * @return  string The string to display.
+ */
+function userpoints_views_views_points_handler_thismonth($fieldinfo, $fielddata, $value, $data)
+{
+  $intPointsThisMonth = db_result(db_query("select sum(userpoints_txn.points) from userpoints_txn inner join userpoints on userpoints.uid = userpoints_txn.uid 
+                              where userpoints.uid = %d AND userpoints_txn.uid = %d AND MONTH(FROM_UNIXTIME(userpoints_txn.time_stamp)) = MONTH(NOW()) AND 
+                              YEAR(FROM_UNIXTIME(userpoints_txn.time_stamp)) = YEAR(NOW())", $data->usernode_users_uid, $data->usernode_users_uid));
+
+  return (int)$intPointsThisMonth;
+  
+} // End Function
+
+function userpoints_views_views_points_query_handler_thismonth($fielddata, $fieldinfo, &$query)
+{
+  return $query->add_where[0];
+
+} // End Function
+
+/**
+ * Custom views filter for year, month, day queries
+ *
+ * @param $filter[operator] is =, >=, >, <=, <
+ * @param $filter[value] is the month number
+ */
+function userpoints_views_handler_filter_month($op, $filter, $filterinfo, &$query) 
+{
+  switch($op)
+  {
+    case 'handler':
+      switch(trim($filter['value'])) 
+      {
+        // No filter value was entered, filter by current month
+        case '':
+          //$query->add_table('userpoints_txn', true);
+          $query->add_table("userpoints_txn", false, 1, array('left' => array('table' => 'userpoints_txn', 'field' => 'uid'), 'right' => array('field' => 'uid')));
+          $query->add_field('userpoints_txn.time_stamp', null, 'userpoints_txn_time_stamp');
+          $query->add_where('MONTH(FROM_UNIXTIME(userpoints_txn.time_stamp)) = MONTH(NOW())');
+          $query->add_where('YEAR(FROM_UNIXTIME(userpoints_txn.time_stamp)) = YEAR(NOW())');
+          break;
+        
+        // 'now' was entered as filter value, filter by current month
+        case 'now':
+          //$query->add_table('userpoints_txn', true);
+          $query->add_table("userpoints_txn", false, 1, array('left' => array('table' => 'userpoints_txn', 'field' => 'uid'), 'right' => array('field' => 'uid')));
+          $query->add_field('userpoints_txn.time_stamp', null, 'userpoints_txn_time_stamp');
+          $query->add_where('MONTH(FROM_UNIXTIME(userpoints_txn.time_stamp)) = MONTH(NOW())');
+          $query->add_where('YEAR(FROM_UNIXTIME(userpoints_txn.time_stamp)) = YEAR(NOW())');
+          
+          break;
+          
+        default:
+          //$query->add_table('userpoints_txn', true);
+          $query->add_table("userpoints_txn", false, 1, array('left' => array('table' => 'userpoints_txn', 'field' => 'uid'), 'right' => array('field' => 'uid')));
+          $query->add_field('userpoints_txn.time_stamp', null, 'userpoints_txn_time_stamp');
+          $query->add_where("MONTH(FROM_UNIXTIME(userpoints_txn.time_stamp)) %s %d", $filter['operator'], $filter['value']);
+          $query->add_where('YEAR(FROM_UNIXTIME(userpoints_txn.time_stamp)) = YEAR(NOW())');
+          break;
+      }
+  }
+
+} // End Function
+
+?>
