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

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 Sep 2 '16 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 Sep 2 '16 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 Sep 2 '16 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 Sep 3 '16 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 Sep 25 '16 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 Sep 7 '16 at 15:49
  • please help me out – Sammy7 Sep 8 '16 at 13:42
  • 1
    Continue this in chat – Shawn Conn Sep 8 '16 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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