studentquiz / moodle-mod_studentquiz

Moodle-Plugin
GNU General Public License v3.0
38 stars 37 forks source link

StudentQuiz: Deleted orphaned question is not working as expected after MDL-78025 is implemented #452

Closed vuvanhieu143 closed 1 year ago

vuvanhieu143 commented 1 year ago

Hi @timhunt ,

This issue is ready for peer review. I have added two separate commits: 1) To fix this issue, as I have discussed with you, we will change the status of the question to draft when we disapprove of a question in StudentQuiz. 2) To update the legacy data.

timhunt commented 1 year ago

Automated tests:

  1. CI fail looks real. Needs to be fixed.
  2. We should add an additional commit to update the moodle-plugin-ci config, to run on all supported Moodle branches (a bit like https://github.com/moodleou/moodle-qtype_crossword/commit/aedc498c56598968bb2abfec66f1112016517792)

Main code

  1. classes/local/studentquiz_question.php - change_state_visibility - the huge if statement here is not very readable. A better way to do this, both for simplicity of code and self-documentation is: Have an array like [self::STATE_DISAPPROVED => question_version_status::QUESTION_STATUS_DRAFT] which defines the logic ...
  2. Then ... well, the change_state_visibility method is not even very helpful. Why is this a separate function? I would expect the work of udpating the state of the question in the question bank to be part of the funciotn that updates the studentquiz_question state, so it is impossible for the two things to get out-of-synch. Right, we have a huge chunk of duplciate logic between changestate.php and change_question_state in externallib.php That should be fixed.
  3. And, the fact that both those places end up calling change_state_visibility twice in quick succession really shows this code is not well designed.
  4. classes/question/bank/studentquiz_bank_view.php build_query - "qv.status <> '$state'", we shoudl be usign a placeholder to insert the value into the SQL.
  5. ensure_studentquiz_question_status_is_always_ready - the coment here says "Make sure questions created/edited in StudentQuiz is always ready." but that is now incorrect.

Upgrade code:

  1. If you are only doing a single $DB->execute, you don't need a transactoin.
  2. The SQL is unnecessarily complicated. No need to have {question} and {question_versions} in the subquery (unless I am wrong). Just do UPDATE {question_versions} SET status = 'draft' WHERE questionbankentryid IN (...)
  3. Then, there is no need to have two levels on nested subqueries.
vuvanhieu143 commented 1 year ago

Hi @timhunt , Thank you for your feedback.

timhunt commented 1 year ago

Thanks. Nice, and speedy, work.