When you go to Edit an existing quiz with some results the quiz results are recalculated incorrectly. You don't need to make any changes, just re-saving the settings triggers the bug.I've isolated the bug to the

quiz_update_max_score_properties

function. It looks like the original query wasn't translated correctly into the new Drupal 7 style. I worked around this by using translating the D6 db_query to the D7 db_query api. I have a patch if anyone is interested.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ppc.coder’s picture

Here is the patch.

UPDATE: I found something wrong with this patch that causes an error when questions are being modified on a quiz. Working on an update.

SteveK’s picture

getting this same issue. would love a fix. looking into it as well...

SteveK’s picture

I haven't received any errors when applying this patch and re-saving existing or adding new questions to the quiz. Can you elaborate where you notice the code failing?

Sabareesh’s picture

I found out the patch in http://drupal.org/files/quiz-quiz.pages_.inc_.patch, This offers some kind of solution to this issue. Looking forward to officially fix this at the earliest.

SteveK’s picture

Sabareesh.KF, it doesn't look like that patch worked. re-saves all entries incorrectly.

ppc.coder’s picture

I've attached my patch that basically rewrites the db queries to do the calculation without the D7 dynamic queries which weren't ported over correctly. The cases that I missed in my first patch were in when a quiz was brand new and had no quiz results to update. I added a simple check return from the function in that situation.

Beanjammin’s picture

Using the patch from #6 I run into a PDO exception when saving a quiz node edit:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'score' cannot be null: UPDATE {quiz_node_results} r SET r.score = ROUND( 100 * ( SELECT SUM(a.points_awarded) FROM {quiz_node_results_answers} a WHERE a.result_id = r.result_id ) / ( SELECT max_score FROM {quiz_node_properties} qnp WHERE qnp.vid = r.vid ) ) WHERE r.vid IN(:quiz_vids_0); Array ( [:quiz_vids_0] => 491 ) in quiz_update_max_score_properties() (line 2567 of /path/to/quiz/quiz.module).
Beanjammin’s picture

To follow up on my post in #7, it turns out there was a problem in my database and when working with a properly set up quiz the quiz node saves without error when edited. On mysql the query could be rewritten to avoid the NULL problem:

UPDATE {quiz_node_results} r SET r.score = IFNULL(ROUND( 100 * ( SELECT SUM(a.points_awarded) FROM {quiz_node_results_answers} a WHERE a.result_id = r.result_id ) / ( SELECT max_score FROM {quiz_node_properties} qnp WHERE qnp.vid = r.vid ) ), 0) WHERE r.vid IN(:quiz_vids_0);

I don't know how portable IFNULL() is, so it may not be practical.

Beanjammin’s picture

There is still an issue with results calculations if a question is removed from the quiz after a user has taken the quiz. The simplest illustration of this is that a user with a 100% result before a question is removed will have a result of greater than 100% after a question is removed.

zeezhao’s picture

It is also an issue when you have "long answer" or "short answer" type questions in a quiz, and you edit it to score them.

Sabareesh’s picture

 $expression = array(':points_awarded' => $points_awarded, ':max_score' => ($max_score > 0) ? $max_score : 1);
  db_update('quiz_node_results')
    ->expression('score', 'ROUND(100 * :points_awarded / :max_score)', $expression)
    ->condition('vid', $quizzes_to_update, 'IN')
    ->execute();

The actual problem starts here, quiz.module line 2552. I think this is a redundant query. I commented the execute() and tried in my local instance, it worked fine wit out any errors.
I am not sure how this has to be done in a proper way, waiting to get suggestion form you all.

falcon’s picture

Status: Active » Fixed

I've commited a fix, but haven't done much testing.

zeezhao’s picture

Status: Fixed » Active

Still incorrect for existing quiz with "long answer" or "short answer" type questions,since those quizzes are edited to score the questions. I am using alpha version quiz-7.x-4.0-alpha1.

Unless will always be wrong for old quizzes? Please confirm. Thanks.

falcon’s picture

Status: Active » Fixed

New version doesn't fix existing quizzes, but I have commited another very important fix for this issue now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

zeezhao’s picture

Status: Closed (fixed) » Active

I tried latest dev version, and it still incorrect for quiz with "long answer" or "short answer" type questions, since those quizzes are edited to score the questions.

  • Commit 38b5922 on 7.x-4.x, 7.x-5.x by falcon:
    #1314072 reported by ppc.coder: Fixed the way max score is recalculated...
  • Commit 7437c26 on 7.x-4.x, 7.x-5.x by falcon:
    Fixed #1314072: Auto revisioning system wasn't working. It created...

  • Commit 38b5922 on 7.x-4.x, 7.x-5.x, quiz-pages by falcon:
    #1314072 reported by ppc.coder: Fixed the way max score is recalculated...
  • Commit 7437c26 on 7.x-4.x, 7.x-5.x, quiz-pages by falcon:
    Fixed #1314072: Auto revisioning system wasn't working. It created...
ezraw’s picture

Issue summary: View changes

Could not reproduce this in 7.x-5.0-alpha-1. Tested by editing a quiz with a long answer and removing a long answer.

ezraw’s picture

Status: Active » Postponed (maintainer needs more info)

If this is still happening with 7.x-4.x please provide steps to reproduce.

  • Commit 38b5922 on 7.x-4.x, 7.x-5.x, quiz-pages, 2269219 by falcon:
    #1314072 reported by ppc.coder: Fixed the way max score is recalculated...
  • Commit 7437c26 on 7.x-4.x, 7.x-5.x, quiz-pages, 2269219 by falcon:
    Fixed #1314072: Auto revisioning system wasn't working. It created...

  • Commit 38b5922 on 7.x-4.x, 7.x-5.x, 2269219 by falcon:
    #1314072 reported by ppc.coder: Fixed the way max score is recalculated...
  • Commit 7437c26 on 7.x-4.x, 7.x-5.x, 2269219 by falcon:
    Fixed #1314072: Auto revisioning system wasn't working. It created...

  • falcon committed 38b5922 on 7.x-6.x
    #1314072 reported by ppc.coder: Fixed the way max score is recalculated...
  • falcon committed 7437c26 on 7.x-6.x
    Fixed #1314072: Auto revisioning system wasn't working. It created...
falcon’s picture

Status: Postponed (maintainer needs more info) » Fixed

Seems to be fixed. Please re-open if the problem still exists.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.