/**
- * Component representing a user picture.
+ * Data structure representing a user picture.
*
* @copyright 2009 Nicolas Connault, 2010 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
}
}
+
+/**
+ * Data structure representing a help icon.
+ *
+ * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.0
+ */
+class help_icon implements renderable {
+ /**
+ * @var string $page name of help page
+ */
+ public $helppage;
+ /**
+ * @var string $title A descriptive text for title tooltip
+ */
+ public $title = '';
+ /**
+ * @var string $component Component name, the same as in get_string()
+ */
+ public $component = 'moodle';
+ /**
+ * @var string $linktext Extra descriptive text next to the icon
+ */
+ public $linktext = '';
+
+ /**
+ * Constructor: sets up the other components in case they are needed
+ * @param string $page The keyword that defines a help page
+ * @param string $title A descriptive text for accesibility only
+ * @param string $component
+ * @param bool $linktext add extra text to icon
+ * @return void
+ */
+ public function __construct($helppage, $title, $component = 'moodle') {
+ if (empty($title)) {
+ throw new coding_exception('A help_icon object requires a $text parameter');
+ }
+ if (empty($helppage)) {
+ throw new coding_exception('A help_icon object requires a $helppage parameter');
+ }
+
+ $this->helppage = $helppage;
+ $this->title = $title;
+ $this->component = $component;
+ }
+}
+
+
// ==== HTML writer and helper classes, will be probably moved elsewhere ======
if (is_array($value)) {
debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
}
-
+ if ($value instanceof moodle_url) {
+ return ' ' . $name . '="' . $value->out() . '"';
+ }
$value = trim($value);
if ($value == HTML_ATTR_EMPTY) {
return ' ' . $name . '=""';
}
-/**
- * Component representing a help icon.
- *
- * @copyright 2009 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @since Moodle 2.0
- */
-class help_icon extends html_image {
- public $page;
- /**
- * @var string $module Which module is the page defined in
- */
- /**
- * @var string $text A descriptive text
- */
- public $text;
- /**
- * @var string $page The keyword that defines a help page
- */
- public $component = 'moodle';
- /**
- * @var boolean $linktext Whether or not to show text next to the icon
- */
- public $linktext = false;
-
- /**
- * @var html_link $link A html_link object that will hold the URL info
- */
- public $link;
-
- /**
- * Constructor: sets up the other components in case they are needed
- * @param string $page The keyword that defines a help page
- * @param string $text A descriptive text
- * @param string $component
- * @param bool $linktext add extra text to icon
- * @return void
- */
- public function __construct($helppage, $text, $component='moodle', $linktext=false) {
- global $CFG;
-
- if (empty($helppage)) {
- throw new coding_exception('A help_icon object requires a $helppage parameter');
- }
-
- if (empty($text)) {
- throw new coding_exception('A help_icon object requires a $text parameter');
- }
-
- parent::__construct(null, array('class'=>'iconhelp'));
-
- $this->helppage = $helppage;
- $this->text = $text;
- $this->component = $component;
- $this->linktext = $linktext;
-
- $this->link = new html_link();
- $this->link->url = new moodle_url($CFG->wwwroot.'/help.php', array('module' => $this->component, 'file' => $this->helppage .'.html'));
- // Warn users about new window for Accessibility
- }
-
- /**
- * @see lib/html_component#prepare()
- * @return void
- */
- public function prepare(renderer_base $output, moodle_page $page, $target) {
- global $CFG;
-
- if (empty($this->link->title)) {
- $this->link->title = get_string('helpprefix2', '', trim($this->text, ". \t")) .' ('.get_string('newwindow').')';
- }
-
- if (empty($this->src)) {
- $this->src = $output->pix_url('help');
- }
-
- if ($this->linktext) {
- $this->image->alt = get_string('helpwiththis');
- } else {
- $this->image->alt = $this->text;
- }
-
- $popup = new popup_action('click', $this->link->url);
- $this->link->add_action($popup);
-
- parent::prepare($output, $page, $target);
- }
-}
-
-
/**
* This class represents how a block appears on a page.
*
* Print a help icon.
*
* @param string $page The keyword that defines a help page
- * @param string $text A descriptive text
+ * @param string $title A descriptive text for accessibility only
* @param string $component component name
- * @param bool $linktext show extra descriptive text
+ * @param string|bool $linktext true means use $title as link text, string means link text value
+ * @return string HTML fragment
+ */
+ public function help_icon($helppage, $title, $component = 'moodle', $linktext='') {
+ $icon = new help_icon($helppage, $title, $component);
+ if ($linktext === true) {
+ $icon->linktext = $title;
+ } else if (!empty($linktext)) {
+ $icon->linktext = $linktext;
+ }
+ return $this->render($icon);
+ }
- * @return string HTML fragment
+ /**
+ * Implementation of user image rendering.
+ * @param help_icon $helpicon
+ * @return string HTML fragment
*/
- public function help_icon($helppage, $text=null, $component='moodle', $linktext=false) {
- $icon = new help_icon($helppage, $text, $component, $linktext);
+ protected function render_help_icon(help_icon $helpicon) {
+ global $CFG;
- $icon->prepare($this, $this->page, $this->target);
+ // first get the help image icon
+ $src = $this->pix_url('help');
- $icon->link->text = $this->image($icon);
- if ($icon->linktext) {
- $icon->link->text .= $icon->text;
+ if (empty($helpicon->linktext)) {
+ $alt = $helpicon->title;
+ } else {
+ $alt = get_string('helpwiththis');
+ }
+
+ $attributes = array('src'=>$src, 'alt'=>$alt, 'class'=>'iconhelp');
+ $output = html_writer::empty_tag('img', $attributes);
+
+ // add the link text if given
+ if (!empty($helpicon->linktext)) {
+ // the spacing has to be done through CSS
+ $output .= $helpicon->linktext;
}
- return html_writer::tag('span', array('class' => 'helplink'), $this->link($icon->link));
+ // now create the link around it - TODO: this will be changed during the big lang cleanup in 2.0
+ $url = new moodle_url($CFG->wwwroot.'/help.php', array('module' => $helpicon->component, 'file' => $helpicon->helppage .'.html'));
+
+ // note: this title is displayed only if JS is disabled, otherwise the link will have the new ajax tooltip
+ $title = get_string('helpprefix2', '', trim($helpicon->title, ". \t"));
+
+ $attributes = array('href'=>$url, 'title'=>$title);
+ $id = html_writer::random_id('helpicon');
+ $attributes['id'] = $id;
+ $this->add_action_handler($id, new popup_action('click', $url));
+ $output = html_writer::tag('a', $attributes, $output);
+
+ // and finally span
+ return html_writer::tag('span', array('class' => 'helplink'), $output);
}
/**