2

I created several goals with the Goals module. Every goal has an associated image.

My users have an image field 'Last goal' and I'd like to change it to the image associated with their latest completed goal. As this doesn't seem possible with the Rules module, I'd like to create a custom module. I found in the Goals API that I can use hook_goals_completed_goal($goal_id, $uid), but I'm stuck there.

How can I grab the user's 'Last goal' field and change it to goal_img?

1

I finally found the answer thanks to the Devel module and the user_save API page! After investigating a random goal with dpm(goals_load(GOAL_ID));, I wrote the following code:

 function top_goals_completed_goal($goal_id, $uid) {
    $user = user_load($uid);
    $goal = goals_load($goal_id);
    $fid = $goal->goal_img['und'][0]['fid'];
    $img_title = $goal->goal_img['und'][0]['filename'];

    $edit = array();
    $edit['field_last_goal']['und']['0']['fid'] = $fid;

    user_save($user, $edit);
}
  • Interesting, thanks for sharing! BTW, are you aware Goals has a new maintainer now? – Pierre.Vriens May 25 '16 at 6:37
  • @Pierre.Vriens: You're welcome Pierre. Thanks for letting me know you're not the maintainer anymore. I assumed you were still on the project, since you've been editing my questions, voting and commenting on them all around. ;-) I asked quite a lot about Goals in the past, but frankly, I've now abandoned the module, because I couldn't find any features that I couldn't achieve by using Rules. Hopefully, some more pros of the Goals module will be highlighted on the project's page. – Jeroen May 25 '16 at 13:19
  • Hey @Jeroen , in your prior comment it says "* ... you're not the maintainer anymore ... *". Just so that you know: I am the new maintainer ... – Pierre.Vriens Jul 1 '16 at 17:19
0

I have not used the goals module, but it looks interesting.

I realise that you have tackled this using user badges already, but it sounds like it might not be easiest to tackle this by adding the image to the goal.

As the project page mentions user points and user badges, I would suggest that rather than adding the required image to a goal directly, you install the user badges module, create a 'badge' to match each of the goals you have setup, and attach the image to the badge instead. You could then use the goal completion action in rules to award the user badge to the user.

  • I've abandoned the idea of using the User Badges module. Instead, I've created a 'Last goal' image field for every user and now I'd like to write a custom module that changes this field to the goal's image field when that goal is completed. That seems to be the easiest way. Any ideas how I can do this? – Jeroen Feb 3 '14 at 18: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.