I really got some problems getting to understand the scheduling process. I have made some trigger based rules so that is something I have some understanding off.
What I want to do is to schedule on daily basis at midnight (or any time actually) to fill a nodequeue with a random node of a certain type.
I got as far as getting the random node with some php code
$sql = "SELECT nid FROM {node} ".
"WHERE status=1 AND type='image' ORDER BY RAND() LIMIT 1";
$res = db_query($sql);
$item = db_fetch_object($res);
Next thing is to put the node in the nodequeue. I can use the function which is there for it but it is disabled.
If I got those action rules working the next thing is how to schedule it in such a way these actions will run every night (or any time but only once daily or weekly etc.) So how do youput everthing together in a working thing.
I have been searching for some examples which would make it clear on how to do this but seem not to find anything. Somewhere I miss something to understand exactly how the rules and scheduler work.
If someone could give me an example on how to do this in my opinion easy task I would be very gratefull
Comments
Comment #1
alexgreyhead commentedEdit: on second thoughts, I have re-worded this post and added it as a separate issue - #563996: [Meta] Improve Rules UI UX, rather than effectively hijacking your thread. Sorry! /al
Comment #2
klausiHave a look at the scheduling tutorials:
http://drupal.org/node/298482
Scheduling loops: http://drupal.org/node/520016
Comment #3
erald commentedI have looked at those examples but those give me no answer nor a clue on how to do put the things described together.
I am not even able to get the rules to fill the nodequeu working since the action is simply grayed out. Let alone I am able to understand how to do the scheduling.
Those tutorials do not give me an answer on that. They work perfectly for scheduling something after an event but I want to have an action which is triggered on basis of time (every 24 hours the nodequeu has to be filled with a random node)
As said in #1 a good description of the general use of scheduling and rules would be very helpfull and I do agree very much with alexharries to make the interface easier to understand.
Comment #4
klausi1) Create a rule set (without arguments)
2) Add a rule with an action to execute your php code
3) Add a schedule action to schedule this rule set again with the date set to +24 hours
4) go to the scheduling page and manually schedule the rule set at the date of your choice. it will re-schedule itself.
5) If you found this useful write it up in a new tutorial.
Comment #5
erald commented1) is no problem, I created a rules set
2) the code is selecting a node randomly. Now I have the problem how do I load the selected node to the nodequeu. I was thinking to pass the nid and use the action add to nodequeu. However that action is not available. I tried to use then first load node and next the add to nodequeu. But I do not understand how to pass the selected node from my php code to the nodequeu. I could write some more code to something like that but why should I do so if the action is there. I just like to know how I get the nid to the next action.
3) I added a schedule action to the rule set with the date field set to +30 sec. but no rescheduling took place if the I scheduled the rule set in 4)
4) That what is done. But as said no reschedule.
5) I will certainly write up exactly how I did this if I got it working
thanks for the help so far, hope you can give me some further hints to get this working.
Comment #6
klausi3) should work (remember: run cron!)
for the node: try to use your randomly calculated node ID in the load content by ID action. This action makes a node variable available in the rule and then you should be able to add the nodequeue action.
Comment #7
erald commented3) my mistake that is working.
What I do not understand is how to get the node ID to the load content action.
How do I return the node ID from my php code to use it to load the content of that node. For me that is the missing link. Been trying several things and done a lot of reading but somehow this is completely unclear to me.
My rule set is
php code
load content by id
add to nodequeus
schedule
Comment #8
Anonymous (not verified) commentedI understand your frustration. I had the same struggles with understanding how to use the Rules Scheduler and gave up. Then I found a different way to do it, not using scheduler at all.
On our FinalCutters site there's a daily rotation of a user's photo. At the beginning of the month, I build a nodequeue for the rest of that month's photos. Then I have a rule that fires at 1200 daily to publish the top node in the queue, set a 'publish date' CCK field then remove it from the queue and send the user/admin an email to say it's changed.
So in the rule, the event is set as "Cron maintenance tasks are performed", then I added a new condition set to "Execute custom PHP code".
Our cron runs every 15 mins and this is the code that only runs the rule at the 1200 midday cron:
Works great! I'm no PHP guru but you can see that this only returns true and runs the rule if the cron is between 1200 and 1205, so it would be easy enough to modify to your own specific needs.
As an aside, would be great to have this "time selection" with a UI and built into Rules. If I ever get any time, I might look into doing that.
Martin
Comment #9
erald commentedHi Martin,
Thanks for your reaction. However what you describe is a way to trigger something and setting a true or false condition to do so. I have made several php code snippets this way to trigger certain actions.
My problem is here that I have an action (the php code) which selects a random node and I want to use that node to be added to the nodequeues. My problem is not so much triggering the actions but how make the node id available for the next action (load the node by id) and then placing it into the nodequeue.
I am not a PHP guru either and can not find anywhere what variable I have to return so it will be available for the next action. (and how I define it there) It is probably something very simple but just missing it.
Just returning the array or string does not work. If someone could tell me how to do this with the snippet I gave above I would be happy. Someone who is familiar with Drupal and PHP probably finds it so simple that they even do not give it a thought. But I am missing the logic somehow.
Comment #10
klausiJust put your PHP snippet into the node ID field of the load content action, where you print out the node ID with something like $item->nid.
Comment #11
erald commentedHi Klausi,
Thanks for the suggestion.
That sounds simple but pity enough that does not work since the field is not big enough to hold the code.
So my rule contained following actions
1. execute custom PHP code
2. load content by ID
3. Add to nodequeues.
4. schedule the ruleset.
the PHP code was:
The last line is the one I have been changing to get the selected node to action #2 but there I am missing how to do that. My guess is that if you have the possibility to use custom PHP code in an action you should be able to transfer some results to a next action.
But might be I am just missing something here and have to add the node in the nodequeues with custom code
So I tried following:
This works like a charm so have now made a ruleset with;
1. execute custom PHP code (the above code)
2. schedule the ruleset (+1 day)
Manually activated the schedule with +1 minute. ran the cron after a few minutes and my nodequeu is filled with a new node and a new schedule is made to run the job.
Think though there should be somehow the possibility to transfer data to a next action instead of doing it all in PHP code.
Anyway I got a working solution, thanks all for the tips.
Comment #12
klausiSo I consider this one fixed.
Comment #13
erald commentedWell I fixed my problem. But am still wondering if you actually can execute PHP code, get a node ID (or as a matter of fact anything) transfer this to a following action and use that variable there.
Anyway I will put everything together and make some tutorial later this weekend.
Comment #15
lipinponmala007 commented@ klausi
I am in between developing a simple module which sends a mails twice (1/day) if a database table say x1, contains no data. It will be good if i can run the modules code with Rules. I am not that good programmer so i couldn't understant that documentations i saw #4. but how can we do
'2) Add a rule with an action to execute your php code'? that's my question please help me Thanks in advance.
Comment #16
landing commentedsub