MDL-76614 quiz: move quiz_nav_panel_base to classes folder

This commit is contained in:
Tim Hunt
2023-01-06 14:35:50 +00:00
parent 600b17db21
commit 74c6823bac
5 changed files with 177 additions and 148 deletions
+5 -145
View File
@@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
use mod_quiz\access_manager;
use mod_quiz\output\links_to_other_attempts;
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;
@@ -2714,154 +2715,13 @@ class quiz_attempt {
/**
* Represents the navigation panel, and builds a {@link block_contents} to allow
* it to be output.
* 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
*/
abstract class quiz_nav_panel_base {
/** @var quiz_attempt */
protected $attemptobj;
/** @var question_display_options */
protected $options;
/** @var integer */
protected $page;
/** @var boolean */
protected $showall;
public function __construct(quiz_attempt $attemptobj,
question_display_options $options, $page, $showall) {
$this->attemptobj = $attemptobj;
$this->options = $options;
$this->page = $page;
$this->showall = $showall;
}
/**
* Get the buttons and section headings to go in the quiz navigation block.
*
* @return renderable[] the buttons, possibly interleaved with section headings.
*/
public function get_question_buttons() {
$buttons = array();
foreach ($this->attemptobj->get_slots() as $slot) {
$heading = $this->attemptobj->get_heading_before_slot($slot);
if (!is_null($heading)) {
$sections = $this->attemptobj->get_quizobj()->get_sections();
if (!(empty($heading) && count($sections) == 1)) {
$buttons[] = new navigation_section_heading(format_string($heading));
}
}
$qa = $this->attemptobj->get_question_attempt($slot);
$showcorrectness = $this->options->correctness && $qa->has_marks();
$button = new navigation_question_button();
$button->id = 'quiznavbutton' . $slot;
$button->number = $this->attemptobj->get_question_number($slot);
$button->stateclass = $qa->get_state_class($showcorrectness);
$button->navmethod = $this->attemptobj->get_navigation_method();
if (!$showcorrectness && $button->stateclass === 'notanswered') {
$button->stateclass = 'complete';
}
$button->statestring = $this->get_state_string($qa, $showcorrectness);
$button->page = $this->attemptobj->get_question_page($slot);
$button->currentpage = $this->showall || $button->page == $this->page;
$button->flagged = $qa->is_flagged();
$button->url = $this->get_question_url($slot);
if ($this->attemptobj->is_blocked_by_previous_question($slot)) {
$button->url = null;
$button->stateclass = 'blocked';
$button->statestring = get_string('questiondependsonprevious', 'quiz');
}
$buttons[] = $button;
}
return $buttons;
}
protected function get_state_string(question_attempt $qa, $showcorrectness) {
if ($qa->get_question(false)->length > 0) {
return $qa->get_state_string($showcorrectness);
}
// Special case handling for 'information' items.
if ($qa->get_state() == question_state::$todo) {
return get_string('notyetviewed', 'quiz');
} else {
return get_string('viewed', 'quiz');
}
}
/**
* Hook for subclasses to override.
*
* @param renderer $output the quiz renderer to use.
* @return string HTML to output.
*/
public function render_before_button_bits(renderer $output) {
return '';
}
abstract public function render_end_bits(renderer $output);
/**
* Render the restart preview button.
*
* @param renderer $output the quiz renderer to use.
* @return string HTML to output.
*/
protected function render_restart_preview_link($output) {
if (!$this->attemptobj->is_own_preview()) {
return '';
}
return $output->restart_preview_button(new moodle_url(
$this->attemptobj->start_attempt_url(), array('forcenew' => true)));
}
protected abstract function get_question_url($slot);
public function user_picture() {
global $DB;
if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_NONE) {
return null;
}
$user = $DB->get_record('user', array('id' => $this->attemptobj->get_userid()));
$userpicture = new user_picture($user);
$userpicture->courseid = $this->attemptobj->get_courseid();
if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_LARGE) {
$userpicture->size = true;
}
return $userpicture;
}
/**
* Return 'allquestionsononepage' as CSS class name when $showall is set,
* otherwise, return 'multipages' as CSS class name.
*
* @return string, CSS class name
*/
public function get_button_container_class() {
// Quiz navigation is set on 'Show all questions on one page'.
if ($this->showall) {
return 'allquestionsononepage';
}
// Quiz navigation is set on 'Show one page at a time'.
return 'multipages';
}
}
/**
* Specialisation of {@link quiz_nav_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 quiz_nav_panel_base {
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);
@@ -2888,13 +2748,13 @@ class quiz_attempt_nav_panel extends quiz_nav_panel_base {
/**
* Specialisation of {@link quiz_nav_panel_base} for the review quiz page.
* Specialisation of {@see navigation_panel_base} for the review 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_review_nav_panel extends quiz_nav_panel_base {
class quiz_review_nav_panel extends navigation_panel_base {
public function get_question_url($slot) {
return $this->attemptobj->review_url($slot, -1, $this->showall, $this->page);
}
@@ -0,0 +1,168 @@
<?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 moodle_url;
use question_attempt;
use question_display_options;
use question_state;
use quiz_attempt;
use renderable;
use user_picture;
/**
* Represents the navigation panel, and builds a {@link block_contents} to allow it to be output.
*
* 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
*/
abstract class navigation_panel_base {
/** @var quiz_attempt */
protected $attemptobj;
/** @var question_display_options */
protected $options;
/** @var integer */
protected $page;
/** @var boolean */
protected $showall;
public function __construct(quiz_attempt $attemptobj,
question_display_options $options, $page, $showall) {
$this->attemptobj = $attemptobj;
$this->options = $options;
$this->page = $page;
$this->showall = $showall;
}
/**
* Get the buttons and section headings to go in the quiz navigation block.
*
* @return renderable[] the buttons, possibly interleaved with section headings.
*/
public function get_question_buttons() {
$buttons = array();
foreach ($this->attemptobj->get_slots() as $slot) {
$heading = $this->attemptobj->get_heading_before_slot($slot);
if (!is_null($heading)) {
$sections = $this->attemptobj->get_quizobj()->get_sections();
if (!(empty($heading) && count($sections) == 1)) {
$buttons[] = new navigation_section_heading(format_string($heading));
}
}
$qa = $this->attemptobj->get_question_attempt($slot);
$showcorrectness = $this->options->correctness && $qa->has_marks();
$button = new navigation_question_button();
$button->id = 'quiznavbutton' . $slot;
$button->number = $this->attemptobj->get_question_number($slot);
$button->stateclass = $qa->get_state_class($showcorrectness);
$button->navmethod = $this->attemptobj->get_navigation_method();
if (!$showcorrectness && $button->stateclass === 'notanswered') {
$button->stateclass = 'complete';
}
$button->statestring = $this->get_state_string($qa, $showcorrectness);
$button->page = $this->attemptobj->get_question_page($slot);
$button->currentpage = $this->showall || $button->page == $this->page;
$button->flagged = $qa->is_flagged();
$button->url = $this->get_question_url($slot);
if ($this->attemptobj->is_blocked_by_previous_question($slot)) {
$button->url = null;
$button->stateclass = 'blocked';
$button->statestring = get_string('questiondependsonprevious', 'quiz');
}
$buttons[] = $button;
}
return $buttons;
}
protected function get_state_string(question_attempt $qa, $showcorrectness) {
if ($qa->get_question(false)->length > 0) {
return $qa->get_state_string($showcorrectness);
}
// Special case handling for 'information' items.
if ($qa->get_state() == question_state::$todo) {
return get_string('notyetviewed', 'quiz');
} else {
return get_string('viewed', 'quiz');
}
}
/**
* Hook for subclasses to override.
*
* @param renderer $output the quiz renderer to use.
* @return string HTML to output.
*/
public function render_before_button_bits(renderer $output) {
return '';
}
abstract public function render_end_bits(renderer $output);
/**
* Render the restart preview button.
*
* @param renderer $output the quiz renderer to use.
* @return string HTML to output.
*/
protected function render_restart_preview_link($output) {
if (!$this->attemptobj->is_own_preview()) {
return '';
}
return $output->restart_preview_button(new moodle_url(
$this->attemptobj->start_attempt_url(), array('forcenew' => true)));
}
protected abstract function get_question_url($slot);
public function user_picture() {
global $DB;
if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_NONE) {
return null;
}
$user = $DB->get_record('user', array('id' => $this->attemptobj->get_userid()));
$userpicture = new user_picture($user);
$userpicture->courseid = $this->attemptobj->get_courseid();
if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_LARGE) {
$userpicture->size = true;
}
return $userpicture;
}
/**
* Return 'allquestionsononepage' as CSS class name when $showall is set,
* otherwise, return 'multipages' as CSS class name.
*
* @return string, CSS class name
*/
public function get_button_container_class() {
// Quiz navigation is set on 'Show all questions on one page'.
if ($this->showall) {
return 'allquestionsononepage';
}
// Quiz navigation is set on 'Show one page at a time'.
return 'multipages';
}
}
+2 -3
View File
@@ -32,7 +32,6 @@ use popup_action;
use question_display_options;
use quiz;
use quiz_attempt;
use quiz_nav_panel_base;
use renderable;
use single_button;
use stdClass;
@@ -325,9 +324,9 @@ class renderer extends plugin_renderer_base {
/**
* Outputs the navigation block panel
*
* @param quiz_nav_panel_base $panel instance of quiz_nav_panel_base
* @param navigation_panel_base $panel
*/
public function navigation_panel(quiz_nav_panel_base $panel) {
public function navigation_panel(navigation_panel_base $panel) {
$output = '';
$userpicture = $panel->user_picture();
+1
View File
@@ -61,4 +61,5 @@ $renamedclasses = [
'mod_quiz_renderer' => 'mod_quiz\output\renderer',
'quiz_nav_question_button' => 'mod_quiz\output\navigation_question_button',
'quiz_nav_section_heading' => 'mod_quiz\output\navigation_section_heading',
'quiz_nav_panel_base' => 'mod_quiz\output\navigation_panel_base',
];
+1
View File
@@ -45,6 +45,7 @@ This files describes API changes in the quiz code.
- mod_quiz_renderer => mod_quiz\output\renderer
- quiz_nav_question_button => mod_quiz\output\navigation_question_button
- quiz_nav_section_heading => mod_quiz\output\navigation_section_heading
- quiz_nav_panel_base => mod_quiz\output\navigation_panel_base
* The following classes have been deprecated:
- mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts