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.

3
  • 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
    Commented Aug 30, 2012 at 18:44
  • Can rules check for cck field status in conditions?
    – saadlulu
    Commented Aug 31, 2012 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
    Commented Aug 31, 2012 at 11:36

1 Answer 1

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

5
  • Every time I call that function I get an error. "Fatal error: Call to undefined function userpoints_userpointsapi()"
    – saadlulu
    Commented Aug 30, 2012 at 12:31
  • Strange. Did you enabled the module? Commented Aug 30, 2012 at 12:36
  • Yes, Its enabled.
    – saadlulu
    Commented Aug 30, 2012 at 12:38
  • Weird, I can access that function from my template but not from my custom module. how could this be happening?
    – saadlulu
    Commented Aug 30, 2012 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
    Commented Aug 30, 2012 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.