moodle-qtype_formulas icon indicating copy to clipboard operation
moodle-qtype_formulas copied to clipboard

Added blank space after `{_0:` breaks single-choice questions

Open FloMiLe opened this issue 8 months ago • 8 comments

Description of bug / unexpected behavior

I cannot say for sure that the following weird observation was caused by updating Formulas to v6 or whether that was just a coincidence. But after updating, questions with single-choice inputs were not displayed correctly, see the question in questions-Grössenumrechnung Geschwindigkeit-20250504-2226.xml.zip.

All questions like this one used to work properly. But now, the source code contains a blank space after the colon in {_0:. After removing it, the dropdown menu gets displayed again.

Normally, I would first suspect me having accidentally edited such a question with an html-aware editor. But I usually don't use such an editor, plus I find (and now fix again) these misplaced blank spaces all over my question bank, even in questions that I recently used and therefore know worked.

So, again, this coincided with updating Formulas, but I don't know whether a plugin update could actually change strings in the database.

FloMiLe avatar May 04 '25 20:05 FloMiLe

That's a very strange thing. Outside of unit tests, there are only three places in the code where records in the qtype_formulas_answers table are updated. One is in upgrade.php and concerns an old upgrade (> 12 years ago). The other two are in the save_question_options()function which, obviously, is there to save new or edited questions. We do not generally touch existing questions or parts, neither during an upgrade nor during normal operation.

At the same time, it is an extremely strange coincidence if that happened just now when you upgraded to version 6. So, you're saying that all your Formulas questions with something like {_0:foo} in them do now have a space after the colon?

Can you please check the following to see whether you find some sort of pattern?

  • Did it happen to all Formulas questions or only some of them? Only those that you opened for editing?
  • Is it only for radio box multi-choice questions or also for dropdown? Or only dropdown?
  • Is it happening in all parts or only e. g. the first?
  • Is it happening to all occurrences inside a part's text or only the first one?
  • When you create a new question with a multi-choice field, is it saved correctly?
  • What happens when you edit one of the wrong questions and try to remove the space?
  • What happens when you duplicate a question with a (spaceless) multi-choice answer field?

Looking at the XML file you attached, I see that the first occurrence of {_0:strQuantities:MCE} has been modified, but the later {_3:strUnits:MCE} has not.

I will definitely look into it, because – as Agent Gibbs says in Rule 39 – there is no such thing as a coincidence.

PhilippImhof avatar May 05 '25 06:05 PhilippImhof

FWIW, I have done the following:

  1. Set up a new, clean instance of Moodle 4.5.4.
  2. Install the qbehaviour_adaptivemultipart plugin.
  3. Install the qtype_formulas plugins in version 5.3.6.
  4. Create a question (questions-MCE-20250505-0709.xml.txt, remove TXT suffix). The question works as desired.
  5. Upgrade qtype_formulas to 6.0.1.
  6. Go to question bank and preview the question. It still works.
  7. Edit the question, save it and preview again. It sill works.

So there is currently nothing I can do, but I'd love to know more.

PhilippImhof avatar May 05 '25 06:05 PhilippImhof

@FloMiLe Have you had any luck in finding out what caused the issue in your case?

PhilippImhof avatar May 06 '25 06:05 PhilippImhof

@PhilippImhof, I'm sorry I couldn't reply earlier. Here is what I can say so far:

  • Did it happen to all Formulas questions or only some of them? Only those that you opened for editing?
    • No, it did not happen to all Formulas questions. But it definitely happened to many questions even though they were not opened for editing, and were fine when previewed recently.
  • Is it only for radio box multi-choice questions or also for dropdown? Or only dropdown?
    • I pretty much only use dropdown.
  • Is it happening in all parts or only e. g. the first?
    • So far, I have only seen it in the first part. But there are also questions, where the dropdown input {_0: was not affected.
  • Is it happening to all occurrences inside a part's text or only the first one?
    • When I observed it, there was only one occurrence in the first part anyway.
  • When you create a new question with a multi-choice field, is it saved correctly?
    • Yes, it is.
  • What happens when you edit one of the wrong questions and try to remove the space?
    • That fixes the question.
  • What happens when you duplicate a question with a (spaceless) multi-choice answer field?
    • The question remains working, i.e. no space is added.

So I guess this issue should be closed as the symptoms are ambiguous and the causal chain obscure. Thanks for looking into and contemplating this, though!

FloMiLe avatar May 06 '25 15:05 FloMiLe

Thanks for your feedback, @FloMiLe. I will therefore close the issue. Please do not hesitate to re-open it, if anything points to the Formulas question being the culprit.

PhilippImhof avatar May 06 '25 15:05 PhilippImhof

@FloMiLe I am re-opening this, because there is still something very suspicious: the parts' texts are stored in a dedicated table named qtype_formulas_answers. There is virtually no other component in Moodle and no other plugin that should ever touch this table. So I have some more questions:

  • Was the "Last modified" date of the problematic questions updated? If yes, did it happen at roughly the same time for all of them? Does the time correlate with the update or some other bulk action, e. g. an import or a restore?
  • Has there been a new version for these questions or were they changed "in place"?

PhilippImhof avatar May 07 '25 05:05 PhilippImhof

@PhilippImhof Please excuse my high latency. During school weeks I am unfortunately too busy...

But I have checked and can confirm that the question that I monitor and have deliberately not repaired yet had been changed in place, i.e. the version and the modified date are still the same. To be clear, the date (from 2024) lies before my updating Formulas, and the question was correctly working briefly before the update but not after it.

FloMiLe avatar May 14 '25 13:05 FloMiLe

No worries, I can absolutely relate that, approaching the end of the school year at our school…

With those findings, its less likely for the problem to be caused by the Formulas question. As written above, there is basically only one place where an UPDATE query involving the qtype_formulas_answers table is sent to the DB, and that is in questiontype.php::save_question_options().

That function, in turn is only called by two other methods: either during a question import in the importprocess() function or from the save_question() method of the question_type class, i. e. the qtype_formulas' parent class, via questiontype.php::save_question()'s call of its parent method.

Now the thing is that both importprocess() and questiontypebase.php::save_question() will set the timemodified field to time(), so it is virtually impossible for the Formulas question plugin to have changed a question part without updating the modified time. (Also, AFAICS, both functions will create a new question version.)

I still do not understand how the modification could have happened, especially because – as outlined above – no other component in Moodle is supposed to ever touch that table. So I wonder whether you had some script or query running on the database? I would really love to understand what caused the issue. So far, I have no explanation. I cannot prove it was not the plugin, but at the same time, there is no evidence pointing to it either.

PhilippImhof avatar May 14 '25 18:05 PhilippImhof

@FloMiLe Did you have any ideas as to what else could have caused the problem, e. g. some other database operation?

PhilippImhof avatar Jun 04 '25 15:06 PhilippImhof

@FloMiLe If it is OK for you, I will close this issue now, because we do not have any new information and there have been no other reports of a similar problem from other users.

PhilippImhof avatar Jun 21 '25 14:06 PhilippImhof

Yes, please do. I haven't found any more clues.

By the way, I'm sorry I wasn't able to find the time yet to test the update for the decimal comma.

FloMiLe avatar Jun 22 '25 11:06 FloMiLe

No worries at all; I mean, you don't owe us anything at all anyway. Just have a look at it if you have the time, but don't feel obliged to do so.

PhilippImhof avatar Jun 22 '25 13:06 PhilippImhof

@FloMiLe You might be interested in #288

It does, unfortunately, not clear up why these spaces suddenly appeared in your questions. But the fix makes sure that such rogue spaces will not affect rendering of multiple-choice questions anymore.

Now, what I wonder: Are we actually sure that those spaces did "suddenly appear", or is it possible that they have been there before and nobody cared, because the questions just worked?

PhilippImhof avatar Nov 14 '25 07:11 PhilippImhof

I guess that could be possible. Auto-correct often does insert spaces after a colon and it might not have been necessary to take care of them prior to the update...

FloMiLe avatar Nov 14 '25 10:11 FloMiLe

I am happy to see that we have (at least probably) solved the mystery. As it is virtually impossible that something has just introduced those spaces to questions that had already been in the DB, I am almost certain that this is what must have happened.

PhilippImhof avatar Nov 14 '25 16:11 PhilippImhof