diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 9835863de02..c1921cbc6ec 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -157,6 +157,29 @@ } } + if ($quiz->delay1 or $quiz->delay2) { + //quiz enforced time delay + if ($attempts = quiz_get_user_attempts($quiz->id, $USER->id)) { + $numattempts = count($attempts); + } else { + $numattempts = 0; + } + $timenow = time(); + $lastattempt_obj = get_record_select('quiz_attempts', "quiz = $quiz->id AND attempt = $numattempts AND userid = $USER->id", 'timefinish'); + if ($lastattempt_obj) { + $lastattempt = $lastattempt_obj->timefinish; + } + if ($numattempts == 1 && $quiz->delay1) { + if ($timenow - $quiz->delay1 < $lastattempt) { + error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id); + } + } else if($numattempts > 1 && $quiz->delay2) { + if ($timenow - $quiz->delay2 < $lastattempt) { + error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id); + } + } + } + /// Load attempt or create a new attempt if there is no unfinished one if ($isteacher and $forcenew) { // teacher wants a new preview diff --git a/mod/quiz/defaults.php b/mod/quiz/defaults.php index d0818df5b01..1e6797a2272 100644 --- a/mod/quiz/defaults.php +++ b/mod/quiz/defaults.php @@ -35,7 +35,8 @@ 'quiz_fix_timelimit' => 0, 'quiz_fix_adaptive' => 0, 'quiz_fix_penaltyscheme' => 0, - + 'quiz_fix_delay1' => 0, + 'quiz_fix_delay2' => 0, 'attemptuniqueid' => 0 ); diff --git a/mod/quiz/mod.html b/mod/quiz/mod.html index e0689fe2c55..1e9f7ca7d24 100644 --- a/mod/quiz/mod.html +++ b/mod/quiz/mod.html @@ -67,6 +67,15 @@ if (empty($form->timedue)) { $form->timedue = ""; } + //enforced time delay between quiz attempts add-on + //delay1: time delay between first and second attempt + //delay2: time delay between second and additional quiz attempt + if(!isset($form->delay1)) { + $form->delay1 = 0; + } + if(!isset($form->delay2)) { + $form->delay2 = 0; + } $fix = 0; // This will later be set to 1 if some of the variables have been fixed by the admin. @@ -75,7 +84,7 @@ $yesnooptions = array(); $yesnooptions[0] = get_string("no"); $yesnooptions[1] = get_string("yes"); - + $attemptoptions = array(); $attemptoptions[0] = get_string("attemptsunlimited", "quiz"); $attemptoptions[1] = "1 ".strtolower(get_string("attempt", "quiz")); @@ -83,6 +92,21 @@ $attemptoptions[$i] = "$i ".strtolower(get_string("attempts", "quiz")); } + //enforced time delay between quiz attempts add-on + $timedelayoptions = array(); + $timedelayoptions[0] = get_string('none'); + $timedelayoptions[1800] = get_string('numminutes', '', 30); + $timedelayoptions[3600] = get_string('numminutes', '', 60); + for($i=2; $i<=23; $i++) { + $seconds = $i*3600; + $timedelayoptions[$seconds] = get_string('numhours', '', $i); + } + $timedelayoptions[86400] = get_string('numhours', '', 24); + for($i=2; $i<=7; $i++) { + $seconds = $i*86400; + $timedelayoptions[$seconds] = get_string('numdays', '', $i); + } + ?>