0

I have a function I want to execute when a user finishes a quiz.

function wilson_random_task_quiz_finished($quiz, $score, $session_data) {
  // ...
}

When a user finishes a quiz, I think the hook should be invoked from the following code of the Quiz module.

/**
 * Actions to take at the end of a quiz
 *
 * @param $quiz
 *  The quiz node
 * @param $rid
 *  Result id
 * @param $score
 *  Score as a number
 */
function quiz_end_actions($quiz, $score, $session_data) {
  // Call hook_quiz_finished().
  module_invoke_all('quiz_finished', $quiz, $score, $session_data);

  if (!empty($quiz->aid)) {
    actions_do($quiz->aid, $quiz, $score, $session_data);
  }

  return $score;
}

My hook is not invoked. What am I missing?

  • 3
    is your module called "wilson_random_task"? Are you sure that the quiz_end_actions function is being called? – 2pha May 3 '16 at 8:35
  • I think in your 2nd code your function needs your module name. Ex:function [module name]_quiz_end_actions($quiz, $score, $session_data) – No Sssweat May 3 '16 at 8:41
  • @NoSssweat That is a function of the Quiz module. – kiamlaluno May 3 '16 at 8:49
  • @2pha yes that is the name of my module. How can I be sure? I end the quiz so I expect it to be called. Are there any way to debug? – Jimmie Johansson May 3 '16 at 8:50
  • I usually use the devel module and it's dpm() function to quickly debug. Just temporarily add a dpm call inside the quiz_end_actions function to see if it's being fired. – 2pha May 4 '16 at 2:20
1

2pha is correct: quiz_end_actions() is not necessarily called, contrary to what the code workflow could seem. In fact, quiz_take_quiz() contains the following code.

// Remove session variables, save $rid
$session_data = $_SESSION['quiz_' . $quiz->nid];
unset($_SESSION['quiz_' . $quiz->nid]);
// NOTE: End actions might redirect the user somewhere. Code below this line might not get executed...
quiz_end_actions($quiz, $score, $session_data);

As the comment says, end actions could redirect the user to somewhere else, and the code after it would not be executed.

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.