3

I want to use rules to deduct 10% of the transaction amount from a donor each time he donates userpoints to another user using the userpoints_donate contrib module. Currently there are no rules events or actions from the userpoints_donate contrib module

possible events: user donates points to another user

possible conditions: donation_amount <=> a particular value

possible actions: grant points to donor = (donation_amount * -0.1, i.e grant -10% of donation_amount to donor)

How can i achieve this? Thanks

0

If you see userpoints_donate.module userpoints_donation_giveform_submit function is reponsible to award points to another user

/**
 * Submit function to give another user points.
 */
function userpoints_donation_giveform_submit($form, &$form_state) {
  global $user;

  $points = $form_state['values']['amount'];
  $to = $form_state['validated_account'];

  $tid = NULL;
  if (isset($form_state['values']['tid'])) {
    $tid = $form_state['values']['tid'];
  }

  $params = array(
    'uid'    => $to->uid,
    'points' => $points,
    'operation'  => 'userpoints_donation_received',
    'entity_type' => 'user',
    'entity_id' => $user->uid,
    'description' => $form_state['values']['reason'],
    'tid'    => $tid,
  );
  userpoints_userpointsapi($params);

  $params = array(
    'uid'    => $user->uid,
    'points' => -$points,
    'operation'  => 'userpoints_donation_given',
    'entity_type' => 'user',
    'entity_id' => $to->uid,
    'description' => $form_state['values']['reason'],
    'tid'    => $tid,
  );
  userpoints_userpointsapi($params);
}

As you see above

userpoints_userpointsapi($params); is called to award $points with transaction operation userpoints_donation_received .

You can use hook_userpoints($op, $params = array()) to act upon certain operations. When other modules award points to a user, your hook will be called, among others.

$op: The operation to be acted upon. 'setting' Pass a field set and fields that would be displayed in the userpoints settings page. For example, this can be used for your program to ask the admin to set a number of points for certain actions your module performs. The function should return an array object conforming to FormsAPI structure.

'points before' Calls your module, and others, before the points are processed. You can prevent points from being awarded by returning FALSE.

'points after' Calls your module, and others, after points are processed. You can take certain actions if you so wish. Return value is ignored.

The $params variable is the original $params array as sent to userpoints_userpointsapi

  • Using "Points after" $op and in $params check for operation "userpoints_donation_received" you make a call to deduct 10 % points using..

userpoints_userpointsapi($params); API function..

<?php
function YOURMODULENAME_userpoints($op, $params) {
  if ($op == "points after") {
    if ($params['operation'] == "userpoints_donation_received") {
      $transaction_params = $params;
      $transaction_params['points'] = -( $transaction_params['points'] / 10);
      $transaction_params['operation'] = "userpoints_donation_transaction_fee";
      $transaction_params['description'] = "Transaction Fee";
      userpoints_userpointsapi($transaction_params);
    }
  }
}
  • Thanks for your detailed explanation and code. I'll try this out now and accept your answer if it works, i appreciate. – IT Pro Manager Apr 8 '14 at 6:46
  • when i try this code in a custom module, i get the code printed out on my screen: function userpoints_commission_userpoints($op, $params) { if ($op == "points after") { if ($params['operation'] == "userpoints_donation_received") { $transaction_params = $params; $transaction_params['points'] = -( $transaction_params['points'] / 10); $transaction_params['operation'] = "userpoints_donation_transaction_fee"; $transaction_params['description'] = "Transaction Fee"; userpoints_userpointsapi($transaction_params); } } } – IT Pro Manager Apr 8 '14 at 7:23
  • the code also shows up as ajax error on screen when i try to perform any userpoints transaction. apart from that, it just shows up at the top of every page – IT Pro Manager Apr 8 '14 at 7:56
  • Wrap them up with <?php tags.. – Anil Sagar Apr 8 '14 at 8:11
  • Oh, my bad! Thanks alot, the code works now, but it deducts 10% credit from both donor and receiver. how can i make the deduction to be for only donor but not receiver? (Just lke the original question reads). Thanks. – IT Pro Manager Apr 8 '14 at 10:40

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.