Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-82639

Quiz: Creating/updating random questions does not trigger events

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • 4.4.1
    • Quiz
    • MOODLE_404_STABLE
    • MDL-82639-main
    • Hide
      1. Create a course with a quiz
      2. Add a random question to the quiz
      3. Browse to "Site administration" > "Reports" > "Logs"
      4. Press the "Get these logs" button
      5. Verify you see the "Slot created" event and it says something like:

        The user with id '2' created a new slot using question references with id '1', slot number '1', question context '30', and filter condition '{"filter":{"category":

        Unknown macro: {"name"}

        }},"cmid":2,"courseid":2,"jointype":2,"qpage":0,"qperpage":100,"sortdata":[],"cat":"2,30","tabname":"questions"}' on page '1' of the quiz with course module id '2'.

      6. Play around with changing filter conditions of the random question and verify a "Slot filter condition updated" event is created and it says something like (NB: there is a pre-existing bug with the random question edit form that prevents the "
        Also show questions from subcategories" checkbox populating correctly, see MDL-80539):

        The user with id '2' updated a slot using question references with id '1' and slot number '1' on page '1' of the quiz with course module id '2' to use question context '32' and filter condition '{"filter":{"category":{"name":"category","jointype":1,"values":[1],"filteroptions":{"includesubcategories":false}}},"cmid":2,"courseid":2,"jointype":2,"qpage":0,"qperpage":100,"sortdata":[],"cat":"4,30","tabname":"questions"}'.

      7.  Make a regular question
      8. Check the logs and verify you see a "Slot created" event with a message like:

        The user with id '2' created a new slot with id '4', slot number '2', and question bank entry id '2' (version 'Always latest') on page '2' of the quiz with course module id '2'.

      Show
      Create a course with a quiz Add a random question to the quiz Browse to "Site administration" > "Reports" > "Logs" Press the "Get these logs" button Verify you see the "Slot created" event and it says something like: The user with id '2' created a new slot using question references with id '1', slot number '1', question context '30', and filter condition '{"filter":{"category": Unknown macro: {"name"} }},"cmid":2,"courseid":2,"jointype":2,"qpage":0,"qperpage":100,"sortdata":[],"cat":"2,30","tabname":"questions"}' on page '1' of the quiz with course module id '2'. Play around with changing filter conditions of the random question and verify a "Slot filter condition updated" event is created and it says something like ( NB : there is a pre-existing bug with the random question edit form that prevents the " Also show questions from subcategories" checkbox populating correctly, see MDL-80539 ): The user with id '2' updated a slot using question references with id '1' and slot number '1' on page '1' of the quiz with course module id '2' to use question context '32' and filter condition '{"filter":{"category":{"name":"category","jointype":1,"values": [1] ,"filteroptions":{"includesubcategories":false}}},"cmid":2,"courseid":2,"jointype":2,"qpage":0,"qperpage":100,"sortdata":[],"cat":"4,30","tabname":"questions"}'.  Make a regular question Check the logs and verify you see a "Slot created" event with a message like: The user with id '2' created a new slot with id '4', slot number '2', and question bank entry id '2' (version 'Always latest') on page '2' of the quiz with course module id '2'.

      Background

      Creating/updating random questions used to be handled by calling qtype_random's save_question method. This in turn called save_question from the question type base class, which would fire the relevant events.

      Creating a question

      In 3.9 quiz_add_random_questions would call save_question (see here). However since 4.0 the slot_random class is used. Which does not call save_question. We get the slot created event but not the question created event.

      Updating a question

      In 3.9 mod/quiz/editrandom.php called save_question (see here). From 4.0 onwards, editrandom.php no longer calls save_question and the event is no longer fired.

      Interesting observations

      Changes to the base question type class

      It appears that the question_type base class exclusively fires question_created events from 4.0 onwards too (this makes sense due to question versioning implying a new question every time a question is saved) - but random questions are different as they don't have versions. So it still makes sense that the question_updated event should fire in the case of a random question being updated.

      Original code still exists

      Weirdly, the original save_question code still exists: https://github.com/moodle/moodle/blob/2e1a632302c4d3a47b2dfc1c790d106d38a03c73/question/type/random/questiontype.php#L195

      And even more weirdly, it's still got a unit test (which passes): https://github.com/moodle/moodle/blob/674497a12c200a7b2c7fdd559a5b692c26dc8be1/question/type/random/tests/question_type_test.php#L77

      Solutions

      It looks like there are two approaches here:

      1. Deprecate qtype_random's save_question and fire appropriate events in the relevant places as identified above.
      2. Rewrite save_question so it handles saving, updating, and firing appropriate events (I'm not sure if we should call parent::save_question - probably not?) and update other parts of the code to call save_question.

            cameron1729 cameron1729
            cameron1729 cameron1729
            Trisha Milan Trisha Milan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.