diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index 2df8276c1ba..3f9480aac62 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -1402,6 +1402,26 @@ class quiz_attempt { return $this->quizobj->start_attempt_url($page); } + /** + * Generates the title of the attempt page. + * + * @param int $page the page number (starting with 0) in the attempt. + * @return string + */ + public function attempt_page_title(int $page) : string { + if ($this->get_num_pages() > 1) { + $a = new stdClass(); + $a->name = $this->get_quiz_name(); + $a->currentpage = $page + 1; + $a->totalpages = $this->get_num_pages(); + $title = get_string('attempttitlepaged', 'quiz', $a); + } else { + $title = get_string('attempttitle', 'quiz', $this->get_quiz_name()); + } + + return $title; + } + /** * @param int $slot if speified, the slot number of a specific question to link to. * @param int $page if specified, a particular page to link to. If not givem deduced @@ -1416,6 +1436,15 @@ class quiz_attempt { return $this->page_and_question_url('attempt', $slot, $page, false, $thispage); } + /** + * Generates the title of the summary page. + * + * @return string + */ + public function summary_page_title() : string { + return get_string('attemptsummarytitle', 'quiz', $this->get_quiz_name()); + } + /** * @return string the URL of this quiz's summary page. */ @@ -1430,6 +1459,27 @@ class quiz_attempt { return new moodle_url('/mod/quiz/processattempt.php'); } + /** + * Generates the title of the review page. + * + * @param int $page the page number (starting with 0) in the attempt. + * @param bool $showall whether the review page contains the entire attempt on one page. + * @return string + */ + public function review_page_title(int $page, bool $showall = false) : string { + if (!$showall && $this->get_num_pages() > 1) { + $a = new stdClass(); + $a->name = $this->get_quiz_name(); + $a->currentpage = $page + 1; + $a->totalpages = $this->get_num_pages(); + $title = get_string('attemptreviewtitlepaged', 'quiz', $a); + } else { + $title = get_string('attemptreviewtitle', 'quiz', $this->get_quiz_name()); + } + + return $title; + } + /** * @param int $slot indicates which question to link to. * @param int $page if specified, the URL of this particular page of the attempt, otherwise diff --git a/mod/quiz/lang/en/quiz.php b/mod/quiz/lang/en/quiz.php index f900c774f86..bdba3b6efb2 100644 --- a/mod/quiz/lang/en/quiz.php +++ b/mod/quiz/lang/en/quiz.php @@ -98,6 +98,8 @@ $string['attemptincomplete'] = 'That attempt (by {$a}) is not yet completed.'; $string['attemptlast'] = 'Last attempt'; $string['attemptnumber'] = 'Attempt'; $string['attemptquiznow'] = 'Attempt quiz now'; +$string['attemptreviewtitle'] = '{$a}: Attempt review'; +$string['attemptreviewtitlepaged'] = '{$a->name}: Attempt review (page {$a->currentpage} of {$a->totalpages})'; $string['attempts'] = 'Attempts'; $string['attempts_help'] = 'The total number of attempts allowed (not the number of extra attempts).'; $string['attemptsallowed'] = 'Attempts allowed'; @@ -110,7 +112,10 @@ $string['attemptsnumyourgroups'] = 'Attempts: {$a->total} ({$a->group} from your $string['attemptsonly'] = 'Show only students with attempts'; $string['attemptstate'] = 'State'; $string['attemptstillinprogress'] = 'Attempt still in progress'; +$string['attemptsummarytitle'] = '{$a}: Attempt summary'; $string['attemptsunlimited'] = 'Unlimited attempts'; +$string['attempttitle'] = '{$a}'; +$string['attempttitlepaged'] = '{$a->name} (page {$a->currentpage} of {$a->totalpages})'; $string['autosaveperiod'] = 'Auto-save delay'; $string['autosaveperiod_desc'] = 'Responses can be saved automatically during quiz attempts. The responses are saved whenever one is changed, and then after this delay. There is a trade-off: a shorter delay increases the server load, but reduces the chance that students lose their work. If you are going to make this delay much shorter, you should change the value gradually and monitor the server load. If the load gets too high, make the delay longer again. Setting the delay to 0 turns off auto-saving.'; $string['back'] = 'Back to preview question'; diff --git a/mod/quiz/tests/attempt_test.php b/mod/quiz/tests/attempt_test.php index ebadab88aa0..96e41b45a5e 100644 --- a/mod/quiz/tests/attempt_test.php +++ b/mod/quiz/tests/attempt_test.php @@ -216,6 +216,67 @@ class mod_quiz_attempt_testcase extends advanced_testcase { $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(11, -1, false, 0)); } + /** + * Tests attempt page titles when all questions are on a single page. + */ + public function test_attempt_titles_single() { + $attempt = $this->create_quiz_and_attempt_with_layout('1,2,0'); + + // Attempt page. + $this->assertEquals('Quiz 1', $attempt->attempt_page_title(0)); + + // Summary page. + $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title()); + + // Review page. + $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0)); + } + + /** + * Tests attempt page titles when questions are on multiple pages, but are reviewed on a single page. + */ + public function test_attempt_titles_multiple_single() { + $attempt = $this->create_quiz_and_attempt_with_layout('1,2,0,3,4,0,5,6,0'); + + // Attempt page. + $this->assertEquals('Quiz 1 (page 1 of 3)', $attempt->attempt_page_title(0)); + $this->assertEquals('Quiz 1 (page 2 of 3)', $attempt->attempt_page_title(1)); + $this->assertEquals('Quiz 1 (page 3 of 3)', $attempt->attempt_page_title(2)); + + // Summary page. + $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title()); + + // Review page. + $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0, true)); + } + + /** + * Tests attempt page titles when questions are on multiple pages, and they are reviewed on multiple pages as well. + */ + public function test_attempt_titles_multiple_multiple() { + $attempt = $this->create_quiz_and_attempt_with_layout( + '1,2,3,4,5,6,7,8,9,10,0,11,12,13,14,15,16,17,18,19,20,0,' . + '21,22,23,24,25,26,27,28,29,30,0,31,32,33,34,35,36,37,38,39,40,0,' . + '41,42,43,44,45,46,47,48,49,50,0,51,52,53,54,55,56,57,58,59,60,0'); + + // Attempt page. + $this->assertEquals('Quiz 1 (page 1 of 6)', $attempt->attempt_page_title(0)); + $this->assertEquals('Quiz 1 (page 2 of 6)', $attempt->attempt_page_title(1)); + $this->assertEquals('Quiz 1 (page 6 of 6)', $attempt->attempt_page_title(5)); + + // Summary page. + $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title()); + + // Review page. + $this->assertEquals('Quiz 1: Attempt review (page 1 of 6)', $attempt->review_page_title(0)); + $this->assertEquals('Quiz 1: Attempt review (page 2 of 6)', $attempt->review_page_title(1)); + $this->assertEquals('Quiz 1: Attempt review (page 6 of 6)', $attempt->review_page_title(5)); + + // When all questions are shown. + $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0, true)); + $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(1, true)); + } + public function test_is_participant() { global $USER; $this->resetAfterTest();