How to programatically change a user image field to match the user's latest completed goal? 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?
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-&gt;goal_img['und'][0]['fid'];
    $img_title = $goal-&gt;goal_img['und'][0]['filename'];

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

    user_save($user, $edit);
}