MDL-76614 quiz: move mod_quiz_links_to_other_attempts => classes\output

This commit is contained in:
Tim Hunt 2022-12-19 12:29:56 +00:00
parent 581a3bc7e9
commit aceae3a4fa
5 changed files with 63 additions and 16 deletions

View File

@ -28,6 +28,7 @@
defined('MOODLE_INTERNAL') || die();
use mod_quiz\access_manager;
use mod_quiz\output\links_to_other_attempts;
use mod_quiz\question\bank\qbank_helper;
use mod_quiz\question\display_options;
@ -1889,14 +1890,14 @@ class quiz_attempt {
*
* The $url passed in must contain an attempt parameter.
*
* The {@link mod_quiz_links_to_other_attempts} object returned contains an
* The {@see links_to_other_attempts} object returned contains an
* array with keys that are the attempt number, 1, 2, 3.
* The array values are either a {@link moodle_url} with the attempt parameter
* updated to point to the attempt id of the other attempt, or null corresponding
* to the current attempt number.
*
* @param moodle_url $url a URL.
* @return mod_quiz_links_to_other_attempts|bool containing array int => null|moodle_url.
* @return links_to_other_attempts|bool containing array int => null|moodle_url.
* False if none.
*/
public function links_to_other_attempts(moodle_url $url) {
@ -1905,7 +1906,7 @@ class quiz_attempt {
return false;
}
$links = new mod_quiz_links_to_other_attempts();
$links = new links_to_other_attempts();
foreach ($attempts as $at) {
if ($at->id == $this->attempt->id) {
$links->links[$at->attempt] = null;
@ -1921,15 +1922,15 @@ class quiz_attempt {
*
* The $url passed in must contain a slot parameter.
*
* The {@link mod_quiz_links_to_other_attempts} object returned contains an
* The {@see links_to_other_attempts} object returned contains an
* array with keys that are the redo number, 1, 2, 3.
* The array values are either a {@link moodle_url} with the slot parameter
* The array values are either a {@see moodle_url} with the slot parameter
* updated to point to the slot that has that redo of this question; or null
* corresponding to the redo identified by $slot.
*
* @param int $slot identifies a question in this attempt.
* @param moodle_url $baseurl the base URL to modify to generate each link.
* @return mod_quiz_links_to_other_attempts|null containing array int => null|moodle_url,
* @return links_to_other_attempts|null containing array int => null|moodle_url,
* or null if the question in this slot has not been redone.
*/
public function links_to_other_redos($slot, moodle_url $baseurl) {
@ -1940,7 +1941,7 @@ class quiz_attempt {
return null;
}
$links = new mod_quiz_links_to_other_attempts();
$links = new links_to_other_attempts();
$index = 1;
foreach ($qas as $qa) {
if ($qa->get_slot() == $slot) {

View File

@ -0,0 +1,38 @@
<?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 renderable;
/**
* Represents the list of links to other attempts
*
* @package mod_quiz
* @category output
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class links_to_other_attempts implements renderable {
/**
* @var array The list of links. string attempt number => one of three things:
* - null if this is the current attempt, and so should not be linked. (Just the number is output.)
* - moodle_url if this is a different attempt. (Output as a link to the URL with the number as link text.)
* - a renderable, in which case the results of rendering the renderable is output.
* (This is used by {@see quiz_attempt::links_to_other_redos()}.)
*/
public $links = [];
}

View File

@ -56,4 +56,5 @@ $renamedclasses = [
'quiz_override_form' => 'mod_quiz\form\edit_override_form',
'quiz_access_rule_base' => 'mod_quiz\local\access_rule_base',
'quiz_add_random_form' => 'mod_quiz\form\add_random_form',
'mod_quiz_links_to_other_attempts' => 'mod_quiz\output\links_to_other_attempts',
];

View File

@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die();
use mod_quiz\access_manager;
use mod_quiz\form\preflight_check_form;
use mod_quiz\output\links_to_other_attempts;
use mod_quiz\question\display_options;
@ -410,12 +411,13 @@ class mod_quiz_renderer extends plugin_renderer_base {
}
/**
* outputs the link the other attempts.
* Renders a list of links the other attempts.
*
* @param mod_quiz_links_to_other_attempts $links
* @param links_to_other_attempts $links
* @return string HTML fragment.
*/
protected function render_mod_quiz_links_to_other_attempts(
mod_quiz_links_to_other_attempts $links) {
protected function render_links_to_other_attempts(
links_to_other_attempts $links) {
$attemptlinks = array();
foreach ($links->links as $attempt => $url) {
if (!$url) {
@ -1416,15 +1418,19 @@ class mod_quiz_renderer extends plugin_renderer_base {
array('id' => 'connection-error', 'style' => 'display: none;', 'role' => 'alert')) .
html_writer::tag('div', $ok, array('id' => 'connection-ok', 'style' => 'display: none;', 'role' => 'alert'));
}
}
class mod_quiz_links_to_other_attempts implements renderable {
/**
* @var array string attempt number => url, or null for the current attempt.
* url may be either a moodle_url, or a renderable.
* Deprecated version of render_links_to_other_attempts.
*
* @param links_to_other_attempts $links
* @return string HTML fragment.
* @deprecated since Moodle 4.2. Please use render_links_to_other_attempts instead.
* @todo MDL-76612 Final deprecation in Moodle 4.6
*/
public $links = array();
protected function render_mod_quiz_links_to_other_attempts(links_to_other_attempts $links) {
return $this->render_links_to_other_attempts($links);
}
}

View File

@ -40,6 +40,7 @@ This files describes API changes in the quiz code.
- quiz_override_form => mod_quiz\form\edit_override_form
- quiz_access_rule_base => mod_quiz\local\access_rule_base
- quiz_add_random_form => mod_quiz\form\add_random_form
- mod_quiz_links_to_other_attempts => mod_quiz\output\links_to_other_attempts
* The following classes have been deprecated:
- mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts