Skip to content

Commit

Permalink
MDL-35939 mod_quiz: new functions to generate title for attempt pages
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Aug 15, 2019
1 parent 0f07f03 commit 5dd2654
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
50 changes: 50 additions & 0 deletions mod/quiz/attemptlib.php
Expand Up @@ -1402,6 +1402,26 @@ public function start_attempt_url($slot = null, $page = -1) {
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
Expand All @@ -1416,6 +1436,15 @@ public function attempt_url($slot = null, $page = -1, $thispage = -1) {
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.
*/
Expand All @@ -1430,6 +1459,27 @@ public function processattempt_url() {
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
Expand Down
5 changes: 5 additions & 0 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -98,6 +98,8 @@
$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';
Expand All @@ -110,7 +112,10 @@
$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';
Expand Down
61 changes: 61 additions & 0 deletions mod/quiz/tests/attempt_test.php
Expand Up @@ -305,6 +305,67 @@ public function test_attempt_url() {
$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();
Expand Down

0 comments on commit 5dd2654

Please sign in to comment.