2

Once a month I would like to reset a field on all users accounts to 0.

At the moment I have a horrible way of doing it. Each time a user logs in the data is scheduled to be changed only for that user. This is done through a rule (using the Rules module) which whenever a user logs in runs 'Schedule component evaluation' with a data selector of site:current-user and a date of the first of the month. The component that the rule calls 'set a data value' of 0. This is obviously not an efficient way of handling this. However it does work.

Ideally I would like to have a rule that runs occasionally, I could use when cron is run. This rule would schedule setting the data value for all users of the site. The problem is that I don't seem to have a data selector of 'all users' to pass to the component.

Am I missing something really obvious here?

1
  • Hi Gisle. No it doesn't have to be rules based, but simple is always good with me ;) I have made a few small custom modules, but haven't set data with them so far. Could you give a few more ideas as to how to set data values within a hook_cron function? Commented Apr 3, 2014 at 14:26

2 Answers 2

0

You can make a small module for this to write some code using hook_cron().

Inside hook function you can write query to collect all the user ID & update one field value in loop.

Then use Elsia Cron to schedule your made cron function.

1
0

If you don't have "... a data selector of 'all users' ..." in your rule (as in your question), then a possible solution is to make one available in an appropriately triggered Rules Event, as further detailed below.

Step 1: Trigger a monthly rule

Create a rule using the Rules Once per Day module to actually trigger a rule once a month (not day!). And add a Rules Condition "Day of the month" (which comes with this module also). Further configure this Rules Condition with an actual "*Day of month** (an integer between 1 and 31) to actually fit your needs. E.g.: if you specify 1 as your Day of month, this rule will be triggered on the 1st of every month.

As an example, have a look at this simplistic rule (in Rules export format):

{ "rules_monthly_email_reminder_to_admin_account" : {
    "LABEL" : "Monthly eMail reminder to admin account",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_onceperday", "rules" ],
    "ON" : { "once_per_day" : [] },
    "IF" : [
      { "rules_onceperday_condition_dayofmonth" : { "comparison" : "==", "monthday" : "1" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "site:mail" ],
          "subject" : "Time for the monthly operations on the 1st of every month",
          "message" : "It\u0027s the 1st day of the month again, time for the monthly operations ...",
          "language" : [ "" ]
        }
      }
    ]
  }
}

All this rule does, is to send an eMail to the site admin, on the 1st of every month, with a subject like "Time for the monthly operations on the 1st of every month" (and some eMail body that goes with it).

If you have the Rules UI enabled, and after you also enabled the Rules Ones per Day module, you should be able to just import the above rule in your own site.

Step 2: Create a view of all users to be processed

Create a view (using the Views module) of those users for which you want to "reset a field to 0" (as in your question).

Step 3: Make the monthly rule process all users

After you created such view, you can process the results of a VBO enabled view via Rules, by replacing the Rules Action in the rule from Step 1 like so:

  • Load the VBO list of the users in your View.
  • Add a loop to perform a Rules Action to "reset a field to 0" (as in your question), for each user contained in your Views result.

For more details, have a look at the tutorial "How to process the results of a VBO enabled view via Rules?".

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.