Having a smaller "number of questions" set for a quiz than the number of questions that are assigned to a quiz with a condition of "always" results in bad math.

From http://drupal.org/node/85983:

#8
Somehow.. and I'll need to try and remember how I did this... I got the following error:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1 query: SELECT question_nid FROM quiz_questions WHERE quiz_nid = 7 AND question_status = 0 ORDER BY RAND() LIMIT 0, -1 in /Applications/MAMP/htdocs/drupal-4-7/includes/database.mysql.inc on line 121.

I believe I had created a quiz, taken it twice, then added a new question to it, then taken it again. My quiz progress indicator also said -100%

#15
I'm trying to remember what I did. :(

At first, my quiz only had 1 question.

Then I gave it two.

OH!

I wonder if I added another question and didn't increment the # of questions on the quiz in the quiz settings.

Let me test.

#16
Yep. that was it. So don't let that hold up this patch -- kind of an edge case. Eventually we'll want to increment this somehow so that doesn't happen, though.

And grumble grumble at the title change. ;) But fine. Once we get something here we can use, I guess we probably should look at naming this module more descriptively then if that's come up that many times.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

webchick’s picture

The point of the # of questions was so you could potentially create a large question bank of 200 questions or so, and say that each quiz should only be 10 questions in length.

Seems like it would be pretty easy to solve this with a validation function on the add questions form:

If # of questions either always or random > max number of questions specified on quiz, increase the # of max questions.

seanbfuller’s picture

Would it be valid to say if the number of always questions > the "number of questions" value, increase the "number of questions" value to match the number of "always" questions and let the user know what we did?

This could potentially cut out the random questions, but I think that might OK.

nicholasThompson’s picture

imho - number of questions in a quiz should be as follows:
Only always questions, num questions = num selected as always
Always AND Random, min num questions = num of always + 1, max num questions = num always + num random - 1 (allows for a single random change)?
Make sense? Or am I talking crap?

seanbfuller’s picture

Status: Active » Needs review
FileSize
6.91 KB

Here is a first attempt at fixing this. This patch does the following:

  • Removes the handling of quiz_questions form from the quiz_questions form.
  • Renamed quiz_questions_execute to quiz_questions_submit so that it once again handles the submission of this page.
  • Adds a new function for getting a count of the questions of a particular type ("always", "random, etc.) for a particular quiz.
  • Adds logic to increase or decrease the number of questions based on the questions.
  • Uses Nicholas' suggestions above to add one extra if there are any set to "random."
  • Adds and indicator of how many questions the quiz currently is set to display.
  • Updates hook_validate to set an error if the number of questions for a quiz is outside of the range.
  • Valid range for a quiz is the number of questions set to "always" (+1 if there are any set to "random") to the total number of questions assigned to the quiz (always + random).

It would be great if drupal would send the user to the "add questions" page after initially creating a quiz, but my brain is too tired to make that happen right now. Otherwise this seems to do the job pretty well.

nicholasThompson’s picture

All good, except...

to the total number of questions assigned to the quiz (always + random)

So if I set 10 always questions + 10 random questions and then set my quiz to 20 questions (which is valid according to your formula) then I'll actually never get any random questions, they'll all be "always" as there are no excess questions to swap with.

In the case of always & random, should the max not be "Always Count + Random Count - 1" so we get at least 1 position to swap random questions in?

seanbfuller’s picture

If you only have one random question defined, it would never show up. ie- With 10 "always" questions and 1 "random" question, 11 -1 = 10.

I guess I could check for random > 1 before subtracting one from the high range...

    // If we have random number, add one to the low range.
    if($anum_random > 0){
      $anum_always++;
    }
    // If we have more than one random number, lower the high range by one.
    if($anum_random > 1){
      $anum_total--;
    }

I was trying to avoid getting into a lotof tedious and un-intuitive logic, but that doesn't actually seem too
bad.

On the other hand, is it bad to force users to choose one less than the total number of questions? At the very least, it seems that the rules governing the range is complex enough now that I should update the help description of the "number of questions" field.

seanbfuller’s picture

FileSize
7.12 KB

Here's a new version that just adds the rule that I posted above to the validation hook.

I did not update the help text on the number of fields form element because I was having a hard time describing the rules in a meaningful way without using a whole paragraph. I'm going to create a new issue to update the help text and add a note in there concerning the min and max values for this field.

If someone wants to give this the thumbs up, I'll start on the rest of the issues.

seanbfuller’s picture

Status: Needs review » Reviewed & tested by the community

Nicholas gave me a thumbs up to commit this over IM.

seanbfuller’s picture

Status: Reviewed & tested by the community » Fixed

Committed

Anonymous’s picture

Status: Fixed » Closed (fixed)

  • Commit b4ffaf4 on 4.7.x-1.x, 5.x-1.x, 5.x-2.x, 6.x-2.x, 6.x-3.x, 6.x-4.x, 6.x-5.x, 6.x-6.x, 7.x-4.x, master, 7.x-5.x by seanbfuller:
    #88688 Interface change and fixes to address issues regarding the number...

  • Commit b4ffaf4 on 4.7.x-1.x, 5.x-1.x, 5.x-2.x, 6.x-2.x, 6.x-3.x, 6.x-4.x, 6.x-5.x, 6.x-6.x, 7.x-4.x, master, 7.x-5.x, quiz-pages by seanbfuller:
    #88688 Interface change and fixes to address issues regarding the number...

  • Commit b4ffaf4 on 4.7.x-1.x, 5.x-1.x, 5.x-2.x, 6.x-2.x, 6.x-3.x, 6.x-4.x, 6.x-5.x, 6.x-6.x, 7.x-4.x, master, 7.x-5.x, quiz-pages, 2269219 by seanbfuller:
    #88688 Interface change and fixes to address issues regarding the number...

  • Commit b4ffaf4 on 4.7.x-1.x, 5.x-1.x, 5.x-2.x, 6.x-2.x, 6.x-3.x, 6.x-4.x, 6.x-5.x, 6.x-6.x, 7.x-4.x, master, 7.x-5.x, 2269219 by seanbfuller:
    #88688 Interface change and fixes to address issues regarding the number...