2

I'm using Drupal 7 and the Quiz module (version 7.x.5.x).

I'd like to grant User Points to a user whenever a user answers correctly. I have to use the Rules module and I wonder how I can create a Rule for it, like what's the Rules Event, the Rules Condition(s), etc...).

Note that I have set the percentage of passing at 75%.

I prefer not to code anything or use the Quiz userpoints module because I had some troubles in the past with this Quiz userpoints module.

Update: I tried the 2nd rule included in revision 2 of the answer below. But it only grants the points whenever users take a test, no matter whether they passed it or failed it.

  • I had some troubles in the past with the quiz userpoints module * – 13junemadrid Dec 1 '16 at 16:50
0

If using Quiz 7.x-5.x

Assuming you're using the 7.x-5.x version of the Quiz module, you can take advantage of integration with the Rules module that this new release comes with.

To do so, you can start from 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 apply these changes:

  • Add these extra Rules Conditions:
    • entity is of type (to make the data about the Quiz node available, such as pass rate).
    • verify if the user taking the Quiz actually passed.
  • Add another Rules Action to grant an amount of User Points that you may want to be related to [quiz-result:score] (note that this token is also shown in the Rules example above, within the Rules Action), e.g. an amount that equals the score of the Quiz. 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).

After you added this extra Rules Condition and Rules Action, your rule should look similar to this rule:

{ "rules_send_results_at_the_end_of_a_quiz" : {
    "LABEL" : "Send quiz results at the end of a quiz",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "quiz" ],
    "REQUIRES" : [ "rules", "userpoints_rules", "entity" ],
    "ON" : { "quiz_result_update" : [] },
    "IF" : [
      { "entity_is_of_type" : { "entity" : [ "quiz-result:nid" ], "type" : "node" } },
      { "data_is" : { "data" : [ "quiz-result-unchanged:is-evaluated" ], "value" : "0" } },
      { "data_is" : { "data" : [ "quiz-result:is-evaluated" ], "value" : "1" } },
      { "NOT data_is" : {
          "data" : [ "quiz-result:score" ],
          "op" : "\u003C",
          "value" : [ "quiz-result:nid:quiz:pass-rate" ]
        }
      }
    ],
    "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" : [ "" ]
        }
      },
      { "drupal_message" : { "message" : "Congratulations, you passed this quiz with a score of [quiz-result:score] % ..." } },
      { "userpoints_action_grant_points" : {
          "user" : [ "quiz-result:uid" ],
          "points" : [ "quiz-result:score" ],
          "tid" : "0",
          "entity" : [ "quiz-result" ],
          "description" : "Points earned because of your results in the quiz.",
          "operation" : "Insert",
          "display" : "1",
          "moderate" : "default"
        }
      }
    ]
  }
}

Just use the Rules UI to import the above rule and you should (more or less) be ready to go. Possibly the only remaining tuning you want to do in this rule is like so:

  • Review the various settings of the Rules Action to grant User Points (eg the amount (= quiz-result:score in the rule above), the description and the category for the points.
  • Optionally also review the messages being shown when a user passes a quiz. Using the above rule, you'll get a messages similar to this example:

    Congratulations, you passed this quiz with a score of 80 % ...

    That message is also followed by a related message produced by the User Points module, which looks like in this example:

    You just earned 80 points and now have 29.512 points in the General category.

Attention: be aware that the above rule will grant user points over and over again, since there is no validation (Rules Condition) if the user taking the quiz already passed the quiz before. If you also want to get that resolve (= only grant such points once), then refer to my answer to "How to check if user has passed the quiz already before granting user points?".

If using Quiz 7.x-4.x

For the 7.x-4.x version of the Quiz module, you may want to try the patch from fago (aka the Rules maestro ...), attached to issue # 1300420, which has a description like so (note the last phrase in it ...):

... adds basic rules integration to quiz. It includes an event when users have finished taking a quiz, a condition to check whether a user has passed a certain quiz and an action to clear quiz results for a user.

Patch works for me.

| improve this answer | |
  • I used this Rule but it doesn't give points when the quiz is passed, it grants when the user completes a quiz without checking if the answers are correctly answered. – 13junemadrid Dec 2 '16 at 9:13
  • I edited my post – 13junemadrid Dec 6 '16 at 15:29
  • @13junemadrid please check my updated answer (again), especially the update I applied to the rule I included. Just import that rule in your own environment and QA-test it. – Pierre.Vriens Sep 10 '17 at 14:24

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.