2

How can I add points directly to the User Points database? I have a quiz site and would like to use the User Points module to store quiz scores, instead of creating something from scratch. I have created a taxonomy "Quiz" for such points.

Every quiz ends by showing the score made (between 0 to 100 depending on how many answers they got right). I would like to go on and store this in the userpoint database.

I am using the following code but I'm getting 2 error messages:

Notice: Undefined variable: user in ...
Notice: Trying to get property of non-object in ....

The code I am using is the following one:

$params = array (
  'uid' => $user->uid,
  'description' => 'Totals points from quizes',
  'points' => $percentage,
  'tid' =>127,
);

userpoints_userpointsapi($params);

2 Answers 2

3

Userpoints has an API.

See especially userpoints_userpointsapi() Something like this: userpoints_userpointsapi(array('uid'=>$some_user, 'points'=>9000));

You might also be interested in the Quiz module which supports Userpoints.

2
  • Thanks. I found the following code which is working fine except am getting the following 2 errors. (I editted main code to include this errors + code.
    – Albert
    Commented Mar 12, 2012 at 0:18
  • Well, looks like you have no defined $user object, you need to define the uid who should receive the points somehow.
    – Berdir
    Commented Mar 19, 2012 at 0:43
0

If you're using the 7.x-5.x version of the Quiz module, you can take advantage of the integration with the Rules module that this new Quiz release comes with (which was not in the 7.x-4.x version version), which doesn't require any custom code at all.

To do so, have a look at the delivered rule, which looks like so:

{ "rules_send_results_at_the_end_of_a_quiz" : {
    "LABEL" : "Send quiz results at the end of a quiz",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "TAGS" : [ "quiz" ],
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : { "quiz_result_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "quiz-result-unchanged:is-evaluated" ], "value" : "0" } },
      { "data_is" : { "data" : [ "quiz-result:is-evaluated" ], "value" : "1" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "quiz-result:uid:mail" ],
          "subject" : "[site:name]: Your results for [quiz-result:nid:title]",
          "message" : "[quiz-result:uid:name],\r\n\r\nYou finished the quiz [quiz-result:nid:title] on [quiz-result:time-start]\r\n\r\nYour score: [quiz-result:score]%\r\n\r\nYou can access the result here: [site:url]node\/[quiz-result:nid:nid]\/quiz-results\/[quiz-result:result-id]",
          "language" : [ "" ]
        }
      },
      { "mail" : {
          "to" : [ "quiz-result:nid:author:mail" ],
          "subject" : "[site:name]: [quiz-result:uid:name]\u0027s results for [quiz-result:nid:title]",
          "message" : "[quiz-result:nid:author:name],\r\n\r\n[quiz-result:uid:name] finished the quiz [quiz-result:nid:title] on [quiz-result:time-start]\r\n\r\nTheir score: [quiz-result:score]%\r\n\r\nYou can access the result here: [site:url]node\/[quiz-result:nid:nid]\/quiz\/results\/[quiz-result:result-id]\/view",
          "language" : [ "" ]
        }
      }
    ]
  }
}

After you enable Rules (and Rules UI, a submodule of it), just clone the above rule (using the Rules UI) and replace both Rules Actions with a Rules Action to grant an amount of User Points that is equal to [quiz-result:score] (note that this token is also shown in the Rules example above (within the Rules Action).

If you're not familiar with how to use Rules to grant user points, then have a look at the answer to "How to grant userpoints only for modifying nodes last changed at least 20 days ago?" (which also includes a Rules example to make that happen).

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.