Adaptive question type and next page in quiz

Adaptive question type and next page in quiz

Farhan Karmali -
回帖数:6
Core developers的头像 Plugin developers的头像 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

回复Farhan Karmali

Re: Adaptive question type and next page in quiz

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 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.

回复Tim Hunt

Re: Adaptive question type and next page in quiz

Farhan Karmali -
Core developers的头像 Plugin developers的头像 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 伤心

回复Farhan Karmali

Re: Adaptive question type and next page in quiz

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 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.

回复Tim Hunt

Re: Adaptive question type and next page in quiz

Farhan Karmali -
Core developers的头像 Plugin developers的头像 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
回复Tim Hunt

Re: Adaptive question type and next page in quiz

Farhan Karmali -
Core developers的头像 Plugin developers的头像 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);
          }
       }
    }

回复Farhan Karmali

Re: Adaptive question type and next page in quiz

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 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.