Time limit for more than one quiz

Re: Time limit for more than one quiz

by Michael Mazilu -
Number of replies: 0

Hi there,

With the help of a colleague I have modified the moodle/mod/quiz/review.php file to allow for this feature. It would be nice if this could be implemented as a core functionality but we do not know how to do this. 

For the moment it works by checking the name of the quiz and if it contains a specific string (here 'paused quiz number') then the code activates and transfers unused time from one quiz to the next one until it reaches the last quiz in the sequence. 


// Paused quiz modification; code goes at the end of moodle/mod/quiz/review.php
$str = $attemptobj->get_quiz_name();
// Quiz naming structure:
// Module #### paused quiz number ##; question ##/##;
if (strpos($str, 'paused quiz number') !== false) {
    // load database
    global $DB;
    // calculate remaining time for subsequent quizzes
    $attemptmm = $attemptobj->get_attempt();
    $quizmm = $attemptobj ->get_quiz();
    $quiztimeremaining = $quizmm->timelimit - ($attemptmm->timefinish - $attemptmm->timestart);
    // get module number, quiz number, question number and number of questions from quiz title string
    preg_match_all('!\d+!', $str, $matches);
    // check if not last question in quiz group (defined by module number and quiz number)
    if ($matches[0][2] < $matches[0][3]) {
      // get id number for next quiz by searching database table for correct string (mdl_quiz)
      $nextquiz = 'Module '.$matches[0][0]. ' paused quiz number '.$matches[0][1].'; question '.(1+$matches[0][2]).'/'.$matches[0][3].';';
      $nextquizrecord = $DB->get_record('quiz', array('name'=>$nextquiz));
      $nextquizid = $nextquizrecord->id;
      // get user id
      $quizuserid = $attemptmm->userid;
      // create/modify the user's override record for next question in the group
      $overriderecord = new stdClass();
      $overriderecord->quiz=$nextquizid;
      $overriderecord->userid=$quizuserid;
      $overriderecord->timelimit=$quiztimeremaining;
      $norecords = $DB->count_records('quiz_overrides',array('quiz'=>$nextquizid,'userid'=>$quizuserid));
      if ($norecords ==0){
         $DB->insert_record('quiz_overrides',$overriderecord,false); 
       } else {
         $DB->set_field('quiz_overrides','timelimit',$quiztimeremaining,array('quiz'=>$nextquizid,'userid'=>$quizuserid));               
       }
    }
}


For this to work as expected, the quizzes that belong together need to be named as a sequence. For example:

  • Module 1000 paused quiz number 1; question 1/5;
  • Module 1000 paused quiz number 1; question 2/5;
  • Module 1000 paused quiz number 1; question 3/5;
  • Module 1000 paused quiz number 1; question 4/5;
  • Module 1000 paused quiz number 1; question 5/5;

Each quiz needs to allow only one attempt and the time limit in the first quiz needs to be set to the overall time limit. 

I hope this helps. I appreciate any comments and help with correct implementation. 

Thank you, Michael