From 173da3666c71f98ff74c895e278e23438bf7d99a Mon Sep 17 00:00:00 2001 From: danghieu1407 Date: Tue, 11 Jul 2023 14:18:54 +0700 Subject: [PATCH] MDL-78556 tablelib: flexible_table should support caption tag --- lib/tablelib.php | 39 +++++++++++++++++- lib/tests/tablelib_test.php | 79 +++++++++++++++++++++++++++++-------- lib/upgrade.txt | 4 ++ 3 files changed, 104 insertions(+), 18 deletions(-) diff --git a/lib/tablelib.php b/lib/tablelib.php index 7aab424dc6b..48dfcbd7548 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -170,6 +170,16 @@ class flexible_table { /** @var $resetting bool Whether the table preferences is resetting. */ protected $resetting; + /** + * @var string $caption The caption of table + */ + public $caption; + + /** + * @var array $captionattributes The caption attributes of table + */ + public $captionattributes; + /** * @var filterset The currently applied filerset * This is required for dynamic tables, but can be used by other tables too if desired. @@ -1839,8 +1849,35 @@ class flexible_table { // Start of main data table echo html_writer::start_tag('div', array('class' => 'no-overflow')); - echo html_writer::start_tag('table', $this->attributes); + echo html_writer::start_tag('table', $this->attributes) . $this->render_caption(); + } + /** + * This function set caption for table. + * + * @param string $caption Caption of table. + * @param array|null $captionattributes Caption attributes of table. + */ + public function set_caption(string $caption, ?array $captionattributes): void { + $this->caption = $caption; + $this->captionattributes = $captionattributes; + } + + /** + * This function renders a table caption. + * + * @return string $output Caption of table. + */ + public function render_caption(): string { + if ($this->caption === null) { + return ''; + } + + return html_writer::tag( + 'caption', + $this->caption, + $this->captionattributes, + ); } /** diff --git a/lib/tests/tablelib_test.php b/lib/tests/tablelib_test.php index d0710f99138..b7328a56a43 100644 --- a/lib/tests/tablelib_test.php +++ b/lib/tests/tablelib_test.php @@ -67,17 +67,21 @@ class tablelib_test extends \advanced_testcase { /** * Create a table with properties as passed in params, add data and output html. * - * @param string[] $columns - * @param string[] $headers - * @param bool $sortable - * @param bool $collapsible - * @param string[] $suppress - * @param string[] $nosorting - * @param (array|object)[] $data - * @param int $pagesize + * @param string[] $columns The columns of the table. + * @param string[] $headers The header of the table. + * @param bool $sortable Sorting of the table. + * @param bool $collapsible Is table collapsible. + * @param string[] $suppress Suppress columns. + * @param string[] $nosorting No sorting. + * @param (array|object)[] $data The data of the table. + * @param int $pagesize Page size of the table + * @param string $caption Caption of the table. + * @param array $captionattribute The attribute of the caption. */ - protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data, $pagesize) { - $table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting); + protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data, + $pagesize, $caption = '', $captionattribute = []) { + $table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, + $caption, $captionattribute); $table->pagesize($pagesize, count($data)); foreach ($data as $row) { $table->add_data_keyed($row); @@ -88,15 +92,18 @@ class tablelib_test extends \advanced_testcase { /** * Create a table with properties as passed in params. * - * @param string[] $columns - * @param string[] $headers - * @param bool $sortable - * @param bool $collapsible - * @param string[] $suppress - * @param string[] $nosorting + * @param string[] $columns The columns of the table. + * @param string[] $headers The header of the table. + * @param bool $sortable Sorting of the table. + * @param bool $collapsible Is table collapsible. + * @param string[] $suppress Suppress columns. + * @param string[] $nosorting No sorting. + * @param string $caption Caption of the table. + * @param array $captionattribute The attribute of the caption. * @return flexible_table */ - protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting) { + protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, + $caption = '', $captionattribute = '') { $table = new flexible_table('tablelib_test'); $table->define_columns($columns); @@ -112,6 +119,9 @@ class tablelib_test extends \advanced_testcase { foreach ($nosorting as $column) { $table->no_sorting($column); } + if ($caption) { + $table->set_caption($caption, $captionattribute); + } $table->setup(); return $table; @@ -784,4 +794,39 @@ class tablelib_test extends \advanced_testcase { ]; } + /** + * Data test for set and render caption for table. + * + * @covers ::set_caption_for_table + * @covers ::render_caption_for_table + */ + public function test_set_and_render_caption_for_table(): void { + $data = $this->generate_data(10, 2); + $columns = $this->generate_columns(2); + $headers = $this->generate_headers(2); + $caption = 'Caption for table'; + $captionattribute = ['class' => 'inline']; + $this->run_table_test( + $columns, + $headers, + // Sortable. + true, + // Collapsible. + false, + // Suppress columns. + [], + // No sorting. + [], + // Data. + $data, + // Page size. + 10, + // Caption for table. + $caption, + // Caption attribute. + $captionattribute, + ); + $this->expectOutputRegex('/' . '' . $caption . '<\/caption>' . '/'); + } + } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 3bdc64d97bb..1420adbe24d 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -2,6 +2,10 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. === 4.1.5 === +* Added a new render of caption for the table in render_caption. It can be used by + set_caption($caption, $captionattributes). + e.g. $caption = 'Caption for table' + e.g. $captionattributes = ['class' => 'inline']; * Added new \admin_setting::is_forceable() method to determine whether the setting can be overridden or not. Therefore, whether the settings can be overriden or not will depend on the value of implemented \admin_setting::is_forceable() method, even if we define the settings in config.php.