MDL-68454 table: Move get_context from dynamic to flexible
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 23 Apr 2020 01:22:27 +0000 (09:22 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 4 May 2020 03:52:19 +0000 (11:52 +0800)
lib/table/classes/dynamic.php
lib/tablelib.php
user/classes/table/participants.php

index 1ec81b0..fa07700 100644 (file)
@@ -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;
 }
index 7e669a2..0c1c486 100644 (file)
@@ -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;
+    }
 }
 
 
index cc9dc7f..23fe817 100644 (file)
@@ -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;
     }