MDL-76614 quiz: move quiz_attempt_nav_panel to classes

This commit is contained in:
Tim Hunt 2022-12-19 20:21:37 +00:00
parent adbd0daddb
commit ccbb5da784
6 changed files with 67 additions and 38 deletions

View file

@ -22,6 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
use mod_quiz\output\navigation_panel_attempt;
use mod_quiz\output\renderer;
require_once(__DIR__ . '/../../config.php'); require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/locallib.php');
@ -82,6 +85,7 @@ if ($attemptobj->is_finished()) {
// Check the access rules. // Check the access rules.
$accessmanager = $attemptobj->get_access_manager(time()); $accessmanager = $attemptobj->get_access_manager(time());
$accessmanager->setup_attempt_page($PAGE); $accessmanager->setup_attempt_page($PAGE);
/** @var renderer $output */
$output = $PAGE->get_renderer('mod_quiz'); $output = $PAGE->get_renderer('mod_quiz');
$messages = $accessmanager->prevent_access(); $messages = $accessmanager->prevent_access();
if (!$attemptobj->is_preview_user() && $messages) { if (!$attemptobj->is_preview_user() && $messages) {
@ -121,7 +125,7 @@ $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_
\core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts. \core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts.
// Arrange for the navigation to be displayed in the first region on the page. // Arrange for the navigation to be displayed in the first region on the page.
$navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page); $navbc = $attemptobj->get_navigation_panel($output, navigation_panel_attempt::class, $page);
$regions = $PAGE->blocks->get_regions(); $regions = $PAGE->blocks->get_regions();
$PAGE->blocks->add_fake_block($navbc, reset($regions)); $PAGE->blocks->add_fake_block($navbc, reset($regions));

View file

@ -30,8 +30,6 @@ defined('MOODLE_INTERNAL') || die();
use mod_quiz\access_manager; use mod_quiz\access_manager;
use mod_quiz\output\links_to_other_attempts; use mod_quiz\output\links_to_other_attempts;
use mod_quiz\output\navigation_panel_base; use mod_quiz\output\navigation_panel_base;
use mod_quiz\output\navigation_question_button;
use mod_quiz\output\navigation_section_heading;
use mod_quiz\output\renderer; use mod_quiz\output\renderer;
use mod_quiz\question\bank\qbank_helper; use mod_quiz\question\bank\qbank_helper;
use mod_quiz\question\display_options; use mod_quiz\question\display_options;
@ -1872,7 +1870,7 @@ class quiz_attempt {
* Get the navigation panel object for this attempt. * Get the navigation panel object for this attempt.
* *
* @param renderer $output the quiz renderer to use to output things. * @param renderer $output the quiz renderer to use to output things.
* @param string $panelclass The type of panel, quiz_attempt_nav_panel or quiz_review_nav_panel * @param string $panelclass The type of panel, navigation_panel_attempt::class or quiz_review_nav_panel
* @param int $page the current page number. * @param int $page the current page number.
* @param bool $showall whether we are showing the whole quiz on one page. (Used by review.php.) * @param bool $showall whether we are showing the whole quiz on one page. (Used by review.php.)
* @return block_contents the requested object. * @return block_contents the requested object.
@ -2714,39 +2712,6 @@ class quiz_attempt {
} }
/**
* Specialisation of {@see navigation_panel_base} for the attempt quiz page.
*
* @copyright 2008 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class quiz_attempt_nav_panel extends navigation_panel_base {
public function get_question_url($slot) {
if ($this->attemptobj->can_navigate_to($slot)) {
return $this->attemptobj->attempt_url($slot, -1, $this->page);
} else {
return null;
}
}
public function render_before_button_bits(renderer $output) {
return html_writer::tag('div', get_string('navnojswarning', 'quiz'),
array('id' => 'quiznojswarning'));
}
public function render_end_bits(renderer $output) {
if ($this->page == -1) {
// Don't link from the summary page to itself.
return '';
}
return html_writer::link($this->attemptobj->summary_url(),
get_string('endtest', 'quiz'), array('class' => 'endtestlink aalink')) .
$this->render_restart_preview_link($output);
}
}
/** /**
* Specialisation of {@see navigation_panel_base} for the review quiz page. * Specialisation of {@see navigation_panel_base} for the review quiz page.
* *

View file

@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\output;
use html_writer;
/**
* Specialisation of {@see navigation_panel_base} for the attempt quiz page.
*
* This class is not currently renderable or templatable, but it probably should be in the future,
* which is why it is already in the output namespace.
*
* @package mod_quiz
* @category output
* @copyright 2008 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class navigation_panel_attempt extends navigation_panel_base {
public function get_question_url($slot) {
if ($this->attemptobj->can_navigate_to($slot)) {
return $this->attemptobj->attempt_url($slot, -1, $this->page);
} else {
return null;
}
}
public function render_before_button_bits(renderer $output) {
return html_writer::tag('div', get_string('navnojswarning', 'quiz'),
array('id' => 'quiznojswarning'));
}
public function render_end_bits(renderer $output) {
if ($this->page == -1) {
// Don't link from the summary page to itself.
return '';
}
return html_writer::link($this->attemptobj->summary_url(),
get_string('endtest', 'quiz'), array('class' => 'endtestlink aalink')) .
$this->render_restart_preview_link($output);
}
}

View file

@ -62,4 +62,5 @@ $renamedclasses = [
'quiz_nav_question_button' => 'mod_quiz\output\navigation_question_button', 'quiz_nav_question_button' => 'mod_quiz\output\navigation_question_button',
'quiz_nav_section_heading' => 'mod_quiz\output\navigation_section_heading', 'quiz_nav_section_heading' => 'mod_quiz\output\navigation_section_heading',
'quiz_nav_panel_base' => 'mod_quiz\output\navigation_panel_base', 'quiz_nav_panel_base' => 'mod_quiz\output\navigation_panel_base',
'quiz_attempt_nav_panel' => 'mod_quiz\output\navigation_panel_attempt',
]; ];

View file

@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
use mod_quiz\output\navigation_panel_attempt;
use mod_quiz\output\renderer;
require_once(__DIR__ . '/../../config.php'); require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/locallib.php');
@ -60,6 +62,7 @@ if ($attemptobj->is_preview_user()) {
// Check access. // Check access.
$accessmanager = $attemptobj->get_access_manager(time()); $accessmanager = $attemptobj->get_access_manager(time());
$accessmanager->setup_attempt_page($PAGE); $accessmanager->setup_attempt_page($PAGE);
/** @var renderer $output */
$output = $PAGE->get_renderer('mod_quiz'); $output = $PAGE->get_renderer('mod_quiz');
$messages = $accessmanager->prevent_access(); $messages = $accessmanager->prevent_access();
if (!$attemptobj->is_preview_user() && $messages) { if (!$attemptobj->is_preview_user() && $messages) {
@ -87,7 +90,7 @@ if (empty($attemptobj->get_quiz()->showblocks)) {
$PAGE->blocks->show_only_fake_blocks(); $PAGE->blocks->show_only_fake_blocks();
} }
$navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', -1); $navbc = $attemptobj->get_navigation_panel($output, navigation_panel_attempt::class, -1);
$regions = $PAGE->blocks->get_regions(); $regions = $PAGE->blocks->get_regions();
$PAGE->blocks->add_fake_block($navbc, reset($regions)); $PAGE->blocks->add_fake_block($navbc, reset($regions));

View file

@ -46,6 +46,7 @@ This files describes API changes in the quiz code.
- quiz_nav_question_button => mod_quiz\output\navigation_question_button - quiz_nav_question_button => mod_quiz\output\navigation_question_button
- quiz_nav_section_heading => mod_quiz\output\navigation_section_heading - quiz_nav_section_heading => mod_quiz\output\navigation_section_heading
- quiz_nav_panel_base => mod_quiz\output\navigation_panel_base - quiz_nav_panel_base => mod_quiz\output\navigation_panel_base
- quiz_attempt_nav_panel => mod_quiz\output\navigation_panel_attempt
* The following classes have been deprecated: * The following classes have been deprecated:
- mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts - mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts