I tried adding new votes by adding new variables of the type 'vote', but this didn't really work (since some of the necessary data wasn't writable).

Eventually I ended up writing a new action for casting a vote, which makes sense to me. Patch will be attached when I get an issue number for this.

Comments

itangalo’s picture

StatusFileSize
new2.13 KB

Patch attached!

(With the action you can set *any* voting value you like, even +100 for a vote that normally only accepts +/- 1. This is intentional – it is actually the very feature I'm after.)

itangalo’s picture

Status: Active » Needs review
Energyblazar’s picture

Wow !!! will be using it and post back the results shortly ...

Energyblazar’s picture

Hello can u tell me how do i apply this patch on a live site ?

itangalo’s picture

@Energyblazar: To properly apply the patch you will need Git or some equivalent tool on your live site. If you haven't got that, you could always 'apply' it manually by just entering in the new lines in the module code. The patch file tells which files and at which rows the lines should be added.

(I'm not sure it's a good idea to apply this on a live site, though, but that's your choice.)

andypost’s picture

Suppose this functionality should have reverse action to remove vote

beatnikdude’s picture

@Itangalo I can verify that this patch works for me and I am able to use Rules to cast new votes.

@Energyblazar Applying patches

andypost’s picture

Status: Needs review » Needs work

This patch needs re-roll (remove trailing white-spaces) and some work to include actions to remove vote

beatnikdude’s picture

I don't think the patch needs to be held up for the additional functionality of removing votes. Not a bad addition, but I think the likely use case is to prepopulate votes, it is for me.

timb’s picture

I just used this patch to prepopulate a five star field with votes before allowing anon voting. Works great.

Energyblazar’s picture

how did u do it timb ?

timb’s picture

StatusFileSize
new74.16 KB

@Energyblazar: I found the easiest way to create a new vote on a node: is to load a node that has already been voted on and then use it's vote values to populate the values on a new vote, except for maybe changing out the score. confusing? ... maybe a pic would help

Energyblazar’s picture

heya timb thanks...

but still i am not able to figure out what ya have done even after watching screenshot of yours...

I tried to go to Rules > Add a New Rule , no where in any of the chosen option could i find these "Entity to vote on", "Voting User","Voting Value", "Vote Tag" stuff.

Can you please explain a little more in detail.

P.S: I am fairly new to this drupal and rules thingy...

Thank you...

timb’s picture

yo e.b.: it sounds like you may not have the module patched properly. Try the instructions in comment #7 to manually use the patch command. In my install it appears as: Add new rule => Cast a vote

Energyblazar’s picture

Heya Timb...

Been two days and still i am clueless why i cant get the option "Cast A Vote" When i click on Add New Rules.

Please help me enlighten myself...I did add the patch manually.... here it is...thanks again

function voting_rules_rules_condition_info() {
  return array(
    'voting_rules_condition_check_vote_value' => array(
      'label' => t('Check the value of the vote'),
      'named parameter' => TRUE,
      'parameter' => array(
        'vote' => array(
      		'label' => t('Vote'),
      		'description' => t('The vote.'),
          'type' => 'vote',
        ),
        'operator' => array(
          'type' => 'text',
          'label' => t('Operator'),
          'description' => t('The comparison operator.'),
          'optional' => TRUE,
          'default value' => '==',
          'options list' => 'voting_rules_condition_operator_options',
          'restriction' => 'input',
        ),
        'value' => array(
          'type' => 'text',
          'label' => t('Data value'),
          'description' => t('The value to compare the data with.'),
        ),
      ),
      'group' => t('Voting API'),
    ),
    'voting_rules_condition_check_results' => array(
      'label' => t('Evaluate the results of a vote'),
      'named parameter' => TRUE,
      'parameter' => array(
        'vote_results' => array(
      		'label' => t('Vote results'),
      		'description' => t('The vote results.'),
          'type' => 'vote_results',
        ),
        'function' => array(
          'type' => 'text',
          'label' => t('Function'),
          'description' => t('The function used to compute the value.'),
          'default value' => 'sum',
          'options list' => 'voting_rules_condition_function_options',
          'restriction' => 'input',
        ),
        'operator' => array(
          'type' => 'text',
          'label' => t('Operator'),
          'description' => t('The comparison operator.'),
          'default value' => '==',
          'options list' => 'voting_rules_condition_operator_options',
          'restriction' => 'input',
        ),
        'value' => array(
          'type' => 'text',
          'label' => t('Data value'),
          'description' => t('The value to compare the data with.'),
        ),
      ),
      'group' => t('Voting API'),
    ),
  );
}

 /**
 * Implements hook_rules_action_info(). 
 */
function voting_rules_rules_action_info() {
  $actions = array();
  
  // Allows Rules to create a new, arbitrary vote.
  $actions['voting_rules_cast_a_vote'] = array(
    'group' => t('Voting API'),
    'label' => t('Cast a vote'),
    'parameter' => array(
      'entity' => array(
        'type' => 'entity',
        'label' => 'Entity to vote on',
      ),
      'account' => array(
        'type' => 'user',
        'label' => 'Voting user',
      ),
      // @TODO: Add the 'value_type' parameter, now defaulting to percent.
      'vote value' => array(
        'type' => 'integer',
        'label' => 'Voting value',
      ),
      'vote tag' => array(
        'type' => 'text',
        'label' => 'Vote tag',
        'description' => t("Default value is 'vote', but other tags can be used by voting modules."),
      ),
    ),
  );
  
  return $actions;
}

/**
 * Options list: Operators.
 */
function voting_rules_condition_operator_options() {
  return array(
    '==' => t('equals'),
    '<' => t('is lower than'),
    '<=' => t('is lower than or equal to'),
    '>' => t('is greather than'),
    '>=' => t('is greather than or equal to'),
  );
}

/**
 * Options list: Supported calculation functions.
 */
function voting_rules_condition_function_options() {
	$functions = array();
  $votingapi_metadata = votingapi_metadata();
  
  foreach ($votingapi_metadata['functions'] as $function => $info) {
  	$functions[$function] = $info['name'];
  }
  
  return $functions;
}

/**
 * Condition: Evaluate the results of the vote.
 */
function voting_rules_condition_check_results($args, RulesCondition $condition, $op) {
  foreach ($args['vote_results'] as $vote_result) {
    if ($vote_result['function'] == $args['function']) {
      return eval('return ' . (float) $vote_result['value'] . $args['operator'] . (float) $args['value'] . ';');
    }
  }
}

/**
 * Condition: Check the value of an individual vote.
 */
function voting_rules_condition_check_vote_value($args, RulesCondition $condition, $op) {
	if (in_array($args['operator'], array_keys(voting_rules_condition_operator_options()))) {
  	return eval('return ' . (float) $args['vote']['value'] . $args['operator'] . (float) $args['value'] . ';');
  }
}

/**
 * Action callback for voting_rules_cast_a_vote, casting an arbitrary vote. 
 */
function voting_rules_cast_a_vote($entity, $account, $value, $tag) {
  // Prepare data in the form Voting API expects it.
  $vote = array(
    'entity_type' => $entity->type(),
    'entity_id' => $entity->getIdentifier(),
    'value' => $value,
    'tag' => $tag,
    'uid' => $account->uid,
  );

  // Cast the vote!
  votingapi_set_votes($vote);
  
  // @TODO: Make sure that relevant functions for recalculating voting results
  // are triggered. As it is now, Vote up/down does not show the updated vote
  // score, though it is saved in the database (and visible in eg. Views).
}
Energyblazar’s picture

Can anyone atleast give me a hint as to where i am going wrong or missing a thing or two ?

itangalo’s picture

Energyblazar: Sorry, can't really see what's going on. Sorry.

Energyblazar’s picture

Its nothing complex...

If you see comment #12 timb has posted a screenshot, which he says, he achieved this rules functionality with use of your patch..

Now i did patch it manually using instructions on comment # 7.

But i am still unable to find Add new rule => "Cast a vote" functionality in rules...

Now can u look at the code and tell me wether i have applied the patch right ?

If i have applied it right then where excatly should i be looking for "Cast a Vote" functionality in rules ?

itangalo’s picture

EnergyBlazar: I can't see any mistakes in the code, but there might still be something missing.

If the code is correct, then you should be able to see – in the list of actions – a new action "Cast a vote". To have this appear you must first clear the cache, and of course also make sure that the module is enabled. (When enabling modules, all caches are cleared by default.)

Energyblazar’s picture

yes yes yes thank u Itangalo in actions there is cast a new vote.... silly me...i looked every where not in the list of actions...THANKS AGAIN !!!

mfoda’s picture

Thanks for this patch. I'm trying to set up rules to cast a vote on one of two widgets depending on what the user selects for a boolean field in a node's comment form. After applying the patch I can see "Cast A Vote" as an option under the list of Actions. However, I'm not sure how to specify which of the two Rate widgets to vote on. I'm also getting an error when trying to input +1 as a voting value.

My current rule configuration:
Event: After Saving a New Comment
Conditions:
Content is of type X
Data comparison (checks if user chose the targeted boolean value)

Any ideas on how to show the two rating widgets in the data selector?

Thanks in advance.

Energyblazar’s picture

hmm... thats kinda complex part i guess u may need do some custom coding in rules for it.... here is some hint.. http://nodeone.se/sv/node/829

mfoda’s picture

@Energyblazer
I'm not very good with code at the moment but I'll keep this for future reference. Thanks

timb’s picture

@Energyblazer
I was able to copy a previously made vote from a set node then modify it before attaching it to a new node to guarantee certain values. See my comment #12
maybe this work in your case to load 2 different votes based on your boolean

mfoda’s picture

Can anyone who has used this with the Rate Module please provide a short description of the formatting used for the "Voting Value" field for each rate widget type.

In my case, I have a rate widget with values of type "option". In the rate widget page the values for my two options are numbered "1" and "2". When i go to the rules "cast a vote" configuration page I'm not sure what to input in the "vote value" field. I tried "1" and "+1" but neither seem to work. This could be the correct format and it's possible that it's not working because I made a mistake some where else.

Any ideas?

Energyblazar’s picture

Thanks Timb looked at ur comment #12 and was trying to figure out how to get passed-node in Entity to vote on, vote-template:0:user in voting value, Voting value as passed-score and voting tag as vote-template:0:tag ..... but am still clueless how to achieve as you have done in your screenshot ... can u guide me better..

istryker’s picture

Priority: Normal » Major

I think this issue should be top of the issue queue.

The silly thing about this issue, If user case a vote, and X condition is met, then set to previous vote and/or remove vote, then another vote has to be casted.

(Until there is a pre cast event)

web226’s picture

Thanks for this patch.
I got it working in rules by using a field to pass the default value (of 40) and then adding an offset (of 40)
In my case the ratings seem to be out of 100.
The value of 80 is passed to 'vote' which results in a 4/5 Fivestar rating on the node.
The Fivestar widget needed to be set to 'rating while viewing' on the content type.

rooby’s picture

Issue summary: View changes

+ // @TODO: Add the 'value_type' parameter, now defaulting to percent.

This also need to be fixed before committing.
It's not so useful without it.

rooby’s picture

Assigned: itangalo » rooby

It's been a while since anyone else has worked on this so assigning to me and I'll make a few additions.

noodlescainzos’s picture

StatusFileSize
new83.32 KB
new136.55 KB

Hello somebody can help me ? I installed the patch easy but when i configure my rule with a "after save a node" the vote that i configured is not made it.

My rule try to create a vote in a node referenced "node:field-usuario" (Entity reference field) when i created a specific node.

This are my screenshots

rooby’s picture

Whoops, this one totally slipped off my radar (if anyone else has this problem, show your support at #1621714: Allow to bookmark/favorite issues without abusing the Assigned field or issue tags :)).

I actually did make the changes I was talking about in #30, I just have to make a patch out of it.

noopal’s picture

Hi

When I use rules I can create an action when a vote happens, but I cant create a vote as an action at all.

Has this been applied?