From e1cc88403aec6f449028f48543ecf6f4cb26c78b Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 4 Aug 2009 15:15:11 +0000 Subject: [PATCH] MDL-19756 Migrated print_heading_with_help --- lib/deprecatedlib.php | 38 ++++++++++++++++++++++++++++++++ lib/outputlib.php | 23 +++++++++++++++++++ lib/simpletest/testoutputlib.php | 24 ++++++++++++++++++++ lib/weblib.php | 24 -------------------- 4 files changed, 85 insertions(+), 24 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 0200a40f20..63c40e2516 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3505,3 +3505,41 @@ function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $retur } + +/** + * Centered heading with attached help button (same title text) + * and optional icon attached + * + * @deprecated since Moodle 2.0 + * + * @param string $text The text to be displayed + * @param string $helppage The help page to link to + * @param string $module The module whose help should be linked to + * @param string $icon Image to display if needed + * @param bool $return If set to true output is returned rather than echoed, default false + * @return string|void String if return=true nothing otherwise + */ +function print_heading_with_help($text, $helppage, $module='moodle', $icon=false, $return=false) { + + // debugging('print_heading_with_help() has been deprecated. Please change your code to use $OUTPUT->textfield($field).'); + + global $OUTPUT; + + $helpicon = new help_icon(); + $helpicon->page = $helppage; + $helpicon->text = $text; + $helpicon->module = $module; + + // Extract the src from $icon if it exists + if (preg_match('/src="([^"]*)"/', $icon, $matches)) { + $icon = $matches[1]; + } + + $output = $OUTPUT->heading_with_help($helpicon, $icon); + + if ($return) { + return $output; + } else { + echo $output; + } +} diff --git a/lib/outputlib.php b/lib/outputlib.php index 324fba299a..0ffa1c2fb9 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -2583,6 +2583,22 @@ class moodle_core_renderer extends moodle_renderer_base { return $this->link($icon->link); } + /* + * Centered heading with attached help button (same title text) + * and optional icon attached + * @param help_icon $helpicon A help_icon object + * @param mixed $image An image URL or a html_image object + * @return string HTML fragment + */ + public function heading_with_help($helpicon, $image=false) { + if (!($image instanceof html_image) && !empty($image)) { + $htmlimage = new html_image(); + $htmlimage->src = $image; + $image = $htmlimage; + } + return $this->container($this->image($image) . $this->heading($helpicon->text, 2, 'main help') . $this->help_icon($helpicon), 'heading-with-help'); + } + /** * Print a help icon. * @@ -2679,6 +2695,10 @@ class moodle_core_renderer extends moodle_renderer_base { * @return string HTML fragment */ public function image($image) { + if ($image === false) { + return false; + } + $image->prepare(); $this->prepare_event_handlers($image); @@ -4626,6 +4646,9 @@ class help_icon extends moodle_html_component { } $this->image->add_class('iconhelp'); } else if (empty($this->image->src)) { + if (!($this->image instanceof html_image)) { + $this->image = new html_image(); + } $this->image->src = $OUTPUT->old_icon_url('help'); } diff --git a/lib/simpletest/testoutputlib.php b/lib/simpletest/testoutputlib.php index 978625a573..5c3acbfce5 100644 --- a/lib/simpletest/testoutputlib.php +++ b/lib/simpletest/testoutputlib.php @@ -1206,4 +1206,28 @@ class moodle_core_renderer_test extends UnitTestCase { $html = $this->renderer->user_picture($userpic); $this->assert(new ContainsTagWithAttributes('a', array('title' => 'Test User', 'href' => $CFG->wwwroot.'/user/view.php?id=1&course=1')), $html); } + + public function test_heading_with_help() { + $originalicon = new help_icon(); + $originalicon->page = 'myhelppage'; + $originalicon->text = 'Cool help text'; + + $helpicon = clone($originalicon); + $html = $this->renderer->heading_with_help($helpicon); + $this->assert(new ContainsTagWithAttribute('div', 'class', 'heading-with-help'), $html); + $this->assert(new ContainsTagWithAttribute('span', 'class', 'helplink'), $html); + $this->assert(new ContainsTagWithAttribute('h2', 'class', 'main help'), $html); + $this->assert(new ContainsTagWithAttributes('img', array('class' => 'iconhelp image', 'src' => $this->renderer->old_icon_url('help'))), $html); + $this->assert(new ContainsTagWithContents('h2', 'Cool help text'), $html); + + $helpicon = clone($originalicon); + $helpicon->image = false; + + $html = $this->renderer->heading_with_help($helpicon); + $this->assert(new ContainsTagWithAttribute('div', 'class', 'heading-with-help'), $html); + $this->assert(new ContainsTagWithAttribute('span', 'class', 'helplink'), $html); + $this->assert(new ContainsTagWithAttribute('h2', 'class', 'main help'), $html); + $this->assert(new ContainsTagWithAttributes('img', array('class' => 'iconhelp image', 'src' => $this->renderer->old_icon_url('help'))), $html); + $this->assert(new ContainsTagWithContents('h2', 'Cool help text'), $html); + } } diff --git a/lib/weblib.php b/lib/weblib.php index 3df29fd626..42e0776b67 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2255,30 +2255,6 @@ function build_navigation($extranavlinks, $cm = null) { return(array('newnav' => true, 'navlinks' => $navigation)); } -/** - * Centered heading with attached help button (same title text) - * and optional icon attached - * - * @param string $text The text to be displayed - * @param string $helppage The help page to link to - * @param string $module The module whose help should be linked to - * @param string $icon Image to display if needed - * @param bool $return If set to true output is returned rather than echoed, default false - * @return string|void String if return=true nothing otherwise - */ -function print_heading_with_help($text, $helppage, $module='moodle', $icon='', $return=false) { - $output = '
'; - $output .= '

'.$icon.$text.'

'; - $output .= helpbutton($helppage, $text, $module, true, false, '', true); - $output .= '
'; - - if ($return) { - return $output; - } else { - echo $output; - } -} - /** * Print (or return) a collapisble region, that has a caption that can * be clicked to expand or collapse the region. -- 2.39.5