It would be great if flagging an item could result in a userpoint gain. For example, if there was a flag called "great blog!" then flagging a blog post as a "great blog" would give the blog's author userpoints.

This would probably require an extra setting per flag type, and a call to userpoints_userpointsapi('points', $points, $uid, 'flag-'. $flag_title) when an item is flagged.

Comments

quicksketch’s picture

I'm not interested in providing userpoints hooks within Flag module. However, there is nothing preventing a userpoints-flag integration module from being written separate or part of userpoints itself. Flag module provides a hook_flag() that could call the appropriate userpoints functions to register new points upon flagging or unflagging a piece of content.

icecreamyou’s picture

I can't find any documentation on hook_flag. I need to know these things to write the module you're suggesting:

  1. How do I know whether the item is being flagged or unflagged?
  2. How do I know whether the item being flagged is a node, user, or something else?
  3. How do I get the flagged item's ID?
  4. How do I get the flag's name?

So for my hook_flag implementation I would want to do something like:

/**
 * Implementation of hook_flag().
 */
function mymodule_flag($flag_name, $flagged_item_id, $flag_type = 'node', $flag_status = 'flag') {
  //Example values.
  $points = 3;
  $my_flag_name = 'kudo';
  $my_flag_name_plural = 'kudos';

  //Pseudo-code.
  if (module_exists('userpoints')) {
    if ($flag_name == $my_flag_name_plural) {
      //If unflagging, remove userpoints.
      if ($flag_status == 'unflag') {
        $points = -$points;
      }
      if ($flag_type == 'node') {
        $node = node_load(array('nid' => $flagged_item_id));
        $uid = $node->uid;
        $account = user_load(array('uid' => $uid));
      }
      else if ($flag_type == 'user') {
        $account = user_load(array('uid' => $flagged_item_id));
        $uid = $node->uid;
      }
      else {
        return;
      }
      userpoints_userpointsapi('points', $points, $uid, 'flag-'. $flag_name) ;
      drupal_set_message(check_plain($account->name) .' was awarded '. $points .' points thanks to your '. $my_flag_name .'.');
    }
  }
}
quicksketch’s picture

The call to hook_flag() looks like this (currently line 401 of flag.inc):

module_invoke_all('flag', 'unflag', $this, $content_id, $account)

In other words, your module would implement it as:

function mymodule_flag($action, $flag, $content_id, $account) {
  // Get the type of data:
  $content_type = $flag->content_type;

  // Do things...
}

There isn't yet documentation on this hook (hey, we're still in beta ;) and it has changed in the past between betas. However, I feel that we're pretty solid on the hook flag implementation, so I don't think you need to worry about it changing. You might take a look at the flag_actions.module which is included with flag, as it implements the flag hooks to perform actions.

icecreamyou’s picture

Nice. One last question--how do I get the name of the flag? Is it something like $flag->name or $flag->title?

Thanks for your help so far.

quicksketch’s picture

Yep, the "machine name" of the flag is $flag->name. The human readable name is $flag->title. When grabbing the $flag->title though, you should use $flag->get_label('title'), which will run it through the translation system.

icecreamyou’s picture

Status: Active » Fixed

Great, thanks. I should be set to go now... I just have to install Flag and test!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.