0

I am trying to create a rule that will display a message to the user when (s)he logs in for the first time that day. Reset has to be per day at 12:00am.

Notice: not the first time in 24 hours but the first time that day.

So if user logs in 8/30/2016 11:59 and then 8/31/2016 12:01am then both times (s)he will see message.

I tried to achieve this using event 'User has logged in' and condition: data comparison and comparing last login date with current date and time and offset 1 day. But it's not workings. It might be possible to do this with the custom php?

Thanks God Bless.

------------------------------------------Update------------------------------------------

9/1/2016

I need help creating the following rule:

Trigger: User logs in

Condition: User hasn't logged in this today(since 12:00AM)

Action: Display message to user

Am I doing something wrong, or can this be achieved with custom Php? My attempt is described in first update. Can't seem to make this work. The message should only appear every day.

Day = 12:00:00AM - 11:59:59PM

2 Answers 2

1

There's a few things to mention here.

  • Using the [account:last-login] token won't work here; Drupal's login process updates the user account login timestamp before User logs in event.
  • Using the [account:last-access] token will work however because the User logs in event triggers before Drupal updates the access timestamp.
  • PHP's strtotime() has a special keyword for the significant time you're looking for; strtotime('midnight') produces the timestamp of when the current day started.

Putting that together, you can create your own custom rules condition via hook_rules_condition_info() and using it compare against the [account:last-access] token in the User logs in event to produce the effect you're looking for:

/**
 * implementation of hook_rules_condition_info()
 */
function MYMODULE_rules_condition_info() {
  return array(
    'MYMODULE_condition_before_midnight' => array(
      'label' => t('Before Midnight'),
      'arguments' => array(
        'last_login' => array('type' => 'date', 'label' => t('The date to compare.')),
      ),
      'module' => 'MYMODULE',
      'group' => t('MYMODULE'),
    ),
  );
}

/**
 * Condition MYMODULE_condition_before_midnight
 */
function MYMODULE_condition_before_midnight($last_login) {
  return ($last_login < strtotime('midnight'));
}
10
  • Yes, a conditional callback return either TRUE/FALSE so that rules knows to execute the actions.
    – Shawn Conn
    Commented Sep 2, 2016 at 1:10
  • Could you give me a rule I could import? Because it won't work me just placing this into condition.. Please paste an Export of this rule. Thanks
    – Sammy7
    Commented Sep 2, 2016 at 13:48
  • Rules doesn't export custom conditions. You can use the existing rule that you have. This is custom code that needs to placed into a module so a that any rule can make use the condition.
    – Shawn Conn
    Commented Sep 2, 2016 at 14:48
  • This answer assumes custom rules integration, a custom module (e.g. mymodule.info/mymodule.module) with the MYMODULE prefix changed to whatever you've named your module. Using rules with Execute custom PHP code is a different thing.
    – Shawn Conn
    Commented Sep 3, 2016 at 18:35
  • I've been using this for about two weeks now and ran into a little problem. Some user don't even log out. So if they get on yesterday and again today, code won't get activated if their browser kept them logged in. Would I be able to just change your code from using last_login to last_access and would it work still? Only once per day no matter how much times they actually access. But at least they won't have to log out the day before. Thanks.
    – Sammy7
    Commented Sep 25, 2016 at 4:27
1

No problems, here's a basic tutorial. The code goes in your .module file and the functions must have MYMODULE renamed to whatever you name your module.

3
  • Thank you I'm starting to understand you more now. But I still don't get how I can get rules to connect with the new module I just created. Isn't it supposed to be triggered through condition? How do I like my rule condition to this module? Thanks again
    – Sammy7
    Commented Sep 7, 2016 at 15:49
  • please help me out
    – Sammy7
    Commented Sep 8, 2016 at 13:42
  • 1
    Continue this in chat
    – Shawn Conn
    Commented Sep 8, 2016 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.