I am getting a PHP error when a quiz is being filled out:

Division by zero in modules/quiz/question_types/short_answer/short_answer.classes.inc on line 344.

We have a "personality style" quiz setup that anonymous users can complete. And have short-answers to gather user information. When you set the "maximum possible grade" to 0 (we don't care about grading these questions) you get the division by zero error. If you set it to 1, the error goes away. It doesn't matter for our purposes...but I thought I would post it.

Comments

mbutcher’s picture

Hmm... only multichoice has built-in support for personality quizzes. I guess we should figure out a way to handle this for short and long answer question types, which could legitimately be used for personality quizzes.

sivaji_ganesh_jojodae’s picture

Title: warning: Division by zero » warning: Division by zero in long_answer and short_answer module when the max score is 0
Status: Active » Fixed
StatusFileSize
new2.25 KB
new2.28 KB

attached patches will fix it. Committed to 3.x as well as 4.x.

turadg’s picture

Status: Fixed » Needs work

I think we can make a more reliable fix. Here is the isCorrect() function for long_answer now:

public function isCorrect() {
$possible = _quiz_question_get_instance($this->question)->getMaximumScore();
$actual = $this->score;
// To prevent Division by zero warning
$possible = ($possible == 0) ? 1 : $possible;
return (($actual / $possible) * 100 > 50);
}

1) Why should $possible ever be less than one? If there's no good case, then we should enforce that $possible>=1 throughout the object lifetime and avoid this hack.

2) If possible is allowed to be zero, what does that mean? I would think it means that getting 0 out of 0 would be counted as isCorrect(), but this function will count it as incorrect.

3) Where does that 50% threshold come from? A long answer is correct if over 50% of possible points are earned? That seems like something a user would want to control. Shouldn't it go into the long answer admin section?

falcon’s picture

This issue doesn't seem relevant anymore. ($possible == $actual is the new logic for all of our current question types.)

falcon’s picture

Status: Needs work » Closed (fixed)