4

Is there a way to programmatically deduct points from user points?

I want to deduct points from users if a certain "featured Item" field is checked when they create a new content, but don't want to deduct points from users that created content without "featured Item" checked.

  • 2
    Hint: You could also do this using the provided Rules integration. The advantage is that you can easily alter it (fo the example the amount of points or add another condition) without having to change your code. – Berdir Aug 30 '12 at 18:44
  • Can rules check for cck field status in conditions? – saadlulu Aug 31 '12 at 10:15
  • 1
    Sure. It's a bit complicated but you can find a lot of tutorials and documentation about rules, for example the excellent screencasts on nodeone.se/en/learn-the-rules-framework – Berdir Aug 31 '12 at 11:36
6

You can use userpoints_userpointsapi and pass points in negative if you want to deduct.

example

    $params = array(
      'uid' => $user->uid,
      'points' => -5,
      'operation' => 'mymodule_some_operation',
    );
    userpoints_userpointsapi($params);

For more information http://drupal.org/node/993376 http://api.worldempire.ch/api/userpoints/userpoints-7.x-1.x%21userpoints.module/function/userpoints_userpointsapi/7-1

|improve this answer|||||
  • Every time I call that function I get an error. "Fatal error: Call to undefined function userpoints_userpointsapi()" – saadlulu Aug 30 '12 at 12:31
  • Strange. Did you enabled the module? – Mathankumar Aug 30 '12 at 12:36
  • Yes, Its enabled. – saadlulu Aug 30 '12 at 12:38
  • Weird, I can access that function from my template but not from my custom module. how could this be happening? – saadlulu Aug 30 '12 at 12:43
  • 2
    @saadlulu Are you maybe calling it from outside a function? Never execute code outside a function, at that point Drupal is still bootstrapping and not every module has been loaded. – Berdir Aug 30 '12 at 18:41

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.