CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Smart groups based on relative date of activity
Pages: [1]

Author Topic: Smart groups based on relative date of activity  (Read 2319 times)

AlanT

  • Guest
Smart groups based on relative date of activity
February 27, 2010, 11:31:32 am
Hello all,

I'm new to CiviCRM, and am working to set it up before moving forward.  One thing that has me stumped is in setting up smart groups, especially regarding dates which will change with each use.

Here's what I'm looking to do, and I have not found anything in the documentation or in the forums here that describes it.

I want a smart group that is based on someone having received an earlier email, but not within the previous 3 days.  This smart group will be used to identify all those who are scheduled to receive the next mailing in our sequence.  Obviously, there will be another criteria that the current email has not been sent.

When I go in to define a smart group search, the only date values I can select are absolute values, not relative ones, which is useless for a smart group, where the date range required changes on a daily basis.

Am I missing something, or are relative date searches not possible at this time?

Thanks.

- Alan

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart groups based on relative date of activity
February 27, 2010, 04:02:52 pm

relative dates are not yet supported in advanced search / smart groups. This would be an awesome addition to CiviCRM

would be great if you can work on it and provide a patch. Alternatively you can sponsor a developer and/or core team to work on this. I suspect this is a 20-40 hour project

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

AlanT

  • Guest
Re: Smart groups based on relative date of activity
March 01, 2010, 10:53:34 am
Thanks for the confirmation.  At this time, I'm not able to work on creating a patch, nor am I in a position to fund the development of this feature. 

I may not understand everything that's going on behind the scenes (I haven't yet looked at the code), but it seems that adding relative dates would be an easy addition, not a 20-40 hour project.  Just add a "date adjustment" field to the search criteria, with an option for the base date to be "today", as determined when the search is performed.  Even with testing, a developer familiar with the code should be able to do it in less than an hour.

Looks like, for the time being, we'll have to manually adjust the search parameters for every step in our mailing sequence every time we process our list.


Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart groups based on relative date of activity
March 01, 2010, 11:30:40 am

if you do think its a one hour project, please do jump in and make the needed changes. Please submit your changes as a patch. do remember to take care of the various search scenarios and component searches also.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

AlanT

  • Guest
Re: Smart groups based on relative date of activity
March 01, 2010, 12:07:36 pm
As I mentioned, I haven't yet looked at the code, so have no idea how it is structured.  Sounds like the code wasn't very well designed.  Expected to find things modularized in a Drupal-ish way, where this sort of modification could be done in a few functions. 

Creating a patch will require that I:
  • learn how CiviCRM is structured and laid out
  • learn how your patches need to be formatted
  • learn how your website accepts new patches
and possibly a few other things, which will require more time than I have available for this project.

Maybe some other time.

daveshaw

  • I’m new here
  • *
  • Posts: 1
  • Karma: 0
  • CiviCRM version: 3.1.4
  • CMS version: Drupal 6.19
  • MySQL version: 5.0.41
  • PHP version: 5.2.5
Re: Smart groups based on relative date of activity
January 31, 2011, 03:59:49 pm
I havent tested this for knock-on effects but i have been able to build a smart group with relative dates for Contribution 'Recieve Date' using the Search Builder.

I did this by modifying what is an acceptable 'Date' input type.
I added a few lines to

civicrm/CRM/Utils/Type.php

in "public static function escape" i added a new check to the case 'Date' case 'Timestamp' which looks for human readable dates then converts them into the proper yyyymmdd date format for return.

Old Code:


       case 'Date':
        case 'Timestamp':
            // a null date or timestamp is valid
            if ( strlen( trim( $data ) ) == 0 ) {
                return trim( $data );
            }
           
            if ( ( preg_match('/^\d{8}$/', $data) ||
                   preg_match('/^\d{14}$/', $data) ) &&
                 CRM_Utils_Rule::mysqlDate($data) ) {
                return $data;
            }
            break;
           
           
New Code checking for relative human readable dates:

case 'Date':
        case 'Timestamp':
            // a null date or timestamp is valid
            if ( strlen( trim( $data ) ) == 0 ) {
                return trim( $data );
            }
           
            if ( ( preg_match('/^\d{8}$/', $data) ||
                   preg_match('/^\d{14}$/', $data) ) &&
                 CRM_Utils_Rule::mysqlDate($data) ) {
                return $data;
            }
            if (strtotime($data)) {
                $timestamp = strtotime($data);
           $yyyymmdd = date('Ymd',$timestamp);
                return $yyyymmdd;
            }
            break;
           
           


So with the above code you can now enter anything php's string to time function can handle into Search Builder when looking up >= or <= on date fields.

For example try entering yesterday,last year, -2 year, -3 week, etc.
For more examples Google "php strtotime manual"


Enjoy!

David Shaw

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Re: Smart groups based on relative date of activity
March 08, 2011, 04:09:41 pm
Is there any chance of something like the above making it into the mainline code?  I'm another user who needs relative date searching for membership and sponsor reports and smart groups.

My main goal at the moment is being able to create a smart group for sending reminder mail message to our sponsors who haven't contributed in the past year.  Being able to search for something like "-13 months" - "-12 months" for the most recent contribution would be exactly what I would need.

For a patch would you want not only the Types.php change but also modifications to the date selector that currently displays a calendar? Or would this be something that would most likely only be available via. the search builder?
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart groups based on relative date of activity
March 09, 2011, 08:32:38 am

This has been coming up more often. I suspect we will have a Make It Happen for the 4.1 release to include "flexible" date searching in all our searches

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Re: Smart groups based on relative date of activity
March 09, 2011, 09:03:36 am
lobo,

That would be wonderful!  We have a handful of different projects that are on my plate that would all be a whole lot simpler that way.  The sponsor notifications is the biggest one, I think for now I am going to just put the patch in place and use the search builder so I can get something up and running quickly.

Thanks!
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

jimyhuang

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 3
Re: Smart groups based on relative date of activity
April 26, 2011, 12:02:07 pm
After the testing, I think this is not the solution for smart group. Because the saved query still have static datetime.
I suppose solution of relative date for smart groups need to use STR_TO_DATE function of mysql
( http://stackoverflow.com/questions/3818978/how-to-specify-a-relative-date-in-mysql )

I didn't have time to implement that, but seems that the much more trivial way to apply to smart group.


Quote from: daveshaw on January 31, 2011, 03:59:49 pm
I havent tested this for knock-on effects but i have been able to build a smart group with relative dates for Contribution 'Recieve Date' using the Search Builder.
....
....
David Shaw

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart groups based on relative date of activity
April 26, 2011, 12:10:22 pm

there is a Make It Happen: http://civicrm.org/mih for this feature:

Relative date filters for all search forms

http://civicrm.org/mih#reldate

This would be a really great enhancement for CiviCRM, so please do consider contributing

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Smart groups based on relative date of activity

This forum was archived on 2017-11-26.