From 3fed76e4df9d9df0b153e1e23fdd5220eb4113e0 Mon Sep 17 00:00:00 2001 From: Sergey Rozhkov Date: Sat, 28 Mar 2015 23:41:33 +0300 Subject: [PATCH] MDL-37864 lib: Help icon work correctly in table header with sorting --- lib/tablelib.php | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/tablelib.php b/lib/tablelib.php index 39afa90c2e4..75eecb98732 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -56,6 +56,11 @@ class flexible_table { var $uniqueid = NULL; var $attributes = array(); var $headers = array(); + + /** + * @var string For create header with help icon. + */ + private $helpforheaders = array(); var $columns = array(); var $column_style = array(); var $column_class = array(); @@ -424,6 +429,19 @@ class flexible_table { $this->headers = $headers; } + /** + * Must be called after {@link define_headers()}. + * Always use this function if you need to create header with sorting and help icon. + * @param int $index of header. + * @param string $identifier the keyword that defines a help page. + * @param string $component component name. + * @param string|bool $linktext true means use $title as link text, string means link text value. + */ + public function define_help_for_header($index, $identifier, $component = 'moodle', $linktext = '') { + global $OUTPUT; + $this->helpforheaders[$index] = $OUTPUT->help_icon($identifier, $component, $linktext); + } + /** * Must be called after table is defined. Use methods above first. Cannot * use functions below till after calling this method. @@ -1242,8 +1260,12 @@ class flexible_table { // Done this way for the possibility of more than two sortable full name display fields. $this->headers[$index] = ''; foreach ($requirednames as $name) { + $helpicon = ''; + if (isset($this->helpforheaders[$index])) { + $helpicon = $this->helpforheaders[$index]; + } $sortname = $this->sort_link(get_string($name), - $name, $primarysortcolumn === $name, $primarysortorder); + $name, $primarysortcolumn === $name, $primarysortorder, $helpicon); $this->headers[$index] .= $sortname . ' / '; } $this->headers[$index] = substr($this->headers[$index], 0, -3); @@ -1257,8 +1279,12 @@ class flexible_table { default: if ($this->is_sortable($column)) { + $helpicon = ''; + if (isset($this->helpforheaders[$index])) { + $helpicon = $this->helpforheaders[$index]; + } $this->headers[$index] = $this->sort_link($this->headers[$index], - $column, $primarysortcolumn == $column, $primarysortorder); + $column, $primarysortcolumn == $column, $primarysortorder, $helpicon); } } @@ -1274,7 +1300,11 @@ class flexible_table { if (is_array($this->column_style[$column])) { $attributes['style'] = $this->make_styles_string($this->column_style[$column]); } - $content = $this->headers[$index] . html_writer::tag('div', + $helpicon = ''; + if (isset($this->helpforheaders[$index]) && !$this->is_sortable($column)) { + $helpicon = $this->helpforheaders[$index]; + } + $content = $this->headers[$index] . $helpicon . html_writer::tag('div', $icon_hide, array('class' => 'commands')); } echo html_writer::tag('th', $content, $attributes); @@ -1327,14 +1357,15 @@ class flexible_table { * @param string $column the column name, may be a fake column like 'firstname' or a real one. * @param bool $isprimary whether the is column is the current primary sort column. * @param int $order SORT_ASC or SORT_DESC + * @param string $helpicon if the header contains helpicon must be used exactly this param. * @return string HTML fragment. */ - protected function sort_link($text, $column, $isprimary, $order) { + protected function sort_link($text, $column, $isprimary, $order, $helpicon = '') { return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_SORT] => $column)), $text . get_accesshide(get_string('sortby') . ' ' . $text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' . - $this->sort_icon($isprimary, $order); + $helpicon . ' ' . $this->sort_icon($isprimary, $order); } /**