From c00552b3b048bc407ef349912e8918688e33ca1d Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 23 Apr 2020 09:22:27 +0800 Subject: [PATCH] MDL-68454 table: Move get_context from dynamic to flexible --- lib/table/classes/dynamic.php | 10 ---------- lib/tablelib.php | 26 ++++++++++++++++++++++---- user/classes/table/participants.php | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/table/classes/dynamic.php b/lib/table/classes/dynamic.php index 1ec81b0ba51..fa07700e339 100644 --- a/lib/table/classes/dynamic.php +++ b/lib/table/classes/dynamic.php @@ -30,7 +30,6 @@ namespace core_table; defined('MOODLE_INTERNAL') || die(); use moodle_url; -use context; use core_table\local\filter\filterset; /** @@ -61,13 +60,4 @@ interface dynamic { * @return filterset */ public function get_filterset(): ?filterset; - - /** - * Get the context of the current table. - * - * Note: This function should not be called until after the filterset has been provided. - * - * @return context - */ - public function get_context(): ?context; } diff --git a/lib/tablelib.php b/lib/tablelib.php index 7e669a26517..0c1c486aa57 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -831,9 +831,9 @@ class flexible_table { * @return string contents of cell in column 'fullname', for this row. */ function col_fullname($row) { - global $PAGE, $COURSE; + global $COURSE; - $name = fullname($row, has_capability('moodle/site:viewfullnames', $PAGE->context)); + $name = fullname($row, has_capability('moodle/site:viewfullnames', $this->get_context())); if ($this->download) { return $name; } @@ -1211,7 +1211,7 @@ class flexible_table { * This function is not part of the public api. */ function print_headers() { - global $CFG, $OUTPUT, $PAGE; + global $CFG, $OUTPUT; echo html_writer::start_tag('thead'); echo html_writer::start_tag('tr'); @@ -1233,7 +1233,7 @@ class flexible_table { case 'fullname': // Check the full name display for sortable fields. - if (has_capability('moodle/site:viewfullnames', $PAGE->context)) { + if (has_capability('moodle/site:viewfullnames', $this->get_context())) { $nameformat = $CFG->alternativefullnameformat; } else { $nameformat = $CFG->fullnamedisplay; @@ -1693,6 +1693,24 @@ class flexible_table { return false; } + + /** + * Get the context for the table. + * + * Note: This function _must_ be overridden by dynamic tables to ensure that the context is correctly determined + * from the filterset parameters. + * + * @return context + */ + public function get_context(): context { + global $PAGE; + + if (is_a($this, \core_table\dynamic::class)) { + throw new coding_exception('The get_context function must be defined for a dynamic table'); + } + + return $PAGE->context; + } } diff --git a/user/classes/table/participants.php b/user/classes/table/participants.php index cc9dc7f29d3..23fe81747c2 100644 --- a/user/classes/table/participants.php +++ b/user/classes/table/participants.php @@ -557,7 +557,7 @@ class participants extends \table_sql implements dynamic_table { * * @return context */ - public function get_context(): ?context { + public function get_context(): context { return $this->context; }