Adaptive question type and next page in quiz

Adaptive question type and next page in quiz

by Farhan Karmali -
Number of replies: 6
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi,

I am trying to modify the code of the adaptive question behavior , in a way that if the selected choice is correct the student should proceed to the next question (page) without having to press next again

I tried using the function process_action in the behavior file, I understood the code where it checks if the selected option is the correct answer, however I am unable to redirect the user to the next page from there. Any help . pointers would be much appreciated

Thanks

Average of ratings: -
In reply to Farhan Karmali

Re: Adaptive question type and next page in quiz

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Don't try to do this in the adaptive question behaviour. That breaks the encapsulation that is the whole point of the software design.

The place to try to do this is in mod/quiz/processattempt.php. There, you have access to the state of all the question on the current page (after the response have been processed) and if they are all right, you can adjust the variables for which page to go to next before the redirect happens.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Adaptive question type and next page in quiz

by Farhan Karmali -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Tim,

Thanks for the reply. I was of the opinion I would have to create a new behavior type to achieve this, Anyway , I will modify processattempt.php


I assume I have to pass the attemptid to  get the quba object  like this

$quba = question_engine::load_questions_usage_by_activity($attemptid);


and then loop through to check each question is correct ,


foreach ($quba->get_attempt_iterator() as $qa) {
    if ($qa->get_state()->is_correct()) {
     
    }
}


In my case there will be just one question per page .. Yet the loop get iterated 8-10 times sad

In reply to Farhan Karmali

Re: Adaptive question type and next page in quiz

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

What you assume is not quite correct. You need to read the code a bit more. https://docs.moodle.org/dev/Using_the_question_engine_from_module may also help explain. Also https://docs.moodle.org/dev/Overview_of_the_Moodle_question_engine.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Adaptive question type and next page in quiz

by Farhan Karmali -
Picture of Core developers Picture of Plugin developers Picture of Testers
Thank you

Will read more , have been through those two docs already, I guess I need to study the code , will come back for more help.
Thanks once again
In reply to Tim Hunt

Re: Adaptive question type and next page in quiz

by Farhan Karmali -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thank you for your help Tim,

I finally got it to work , but still would like to get your opinion in case there is scope for improvement.  Also posting the code incase someone wants to replicate the same thing. In processattempt.php , just after the transaction is committed , I added this code

Since I have just one question in one page (slot) it seems to work for me for now , Though I am aware the code might break in case there are more than one question on a page

if(($attemptobj->get_question_status($slots,true))=='Correct') {
     
       $page = $nextpage;

       if ($page == -1) {
          $nexturl = $attemptobj->summary_url();
       } else {
       $nexturl = $attemptobj->attempt_url(null, $page);
          if ($scrollpos !== '') {
          $nexturl->param('scrollpos', $scrollpos);
          }
       }
    }

Average of ratings: Useful (1)
In reply to Farhan Karmali

Re: Adaptive question type and next page in quiz

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

That looks about right. I am pleasantly surprised that it is such a small amount of code.

If you don't have just one question per page, then $slots would be a comma-separated list, so you would need to split it and have some sort of loop, but obviously you don't need that.