MDL-20204 new pix_icon renderable widget
authorPetr Skoda <skodak@moodle.org>
Thu, 11 Feb 2010 14:59:00 +0000 (14:59 +0000)
committerPetr Skoda <skodak@moodle.org>
Thu, 11 Feb 2010 14:59:00 +0000 (14:59 +0000)
blog/external_blogs.php
blog/locallib.php
course/pending.php
course/report/outline/index.php
grade/lib.php
lib/deprecatedlib.php
lib/navigationlib.php
lib/outputcomponents.php
lib/outputrenderers.php
mod/quiz/locallib.php
question/editlib.php

index 63f44c2..50ca55c 100644 (file)
@@ -73,9 +73,9 @@ if (!empty($blogs)) {
 
     foreach ($blogs as $blog) {
         if ($blog->failedlastsync) {
-            $validicon = $OUTPUT->image('i/cross_red_big', array('alt'=>get_string('feedisinvalid', 'blog'), 'title'=>get_string('feedisinvalid', 'blog')));
+            $validicon = $OUTPUT->pix_icon('i/cross_red_big', get_string('feedisinvalid', 'blog'));
         } else {
-            $validicon = $OUTPUT->image('i/tick_green_big', array('alt'=>get_string('feedisvalid', 'blog'), 'title'=>get_string('feedisvalid', 'blog')));
+            $validicon = $OUTPUT->pix_icon('i/tick_green_big', get_string('feedisvalid', 'blog'));
         }
 
         $editurl = new moodle_url('/blog/external_blog_edit.php', array('id' => $blog->id));
index 32022ef..c02c9e4 100644 (file)
@@ -494,10 +494,10 @@ class blog_entry {
             $icon     = substr(mimeinfo_from_type("icon", $mimetype), 0, -4);
             $type     = mimeinfo_from_type("type", $mimetype);
 
-            $image = $OUTPUT->image("/f/$icon", array('alt'=>$filename, 'class'=>'icon'));
+            $image = $OUTPUT->pix_icon("/f/$icon", $filename, 'moodle', array('class'=>'icon'));
 
             if ($return == "html") {
-                $output .= html_writer::link($ffurl, $OUTPUT->image($image));
+                $output .= html_writer::link($ffurl, $image);
                 $output .= html_writer::link($ffurl, $filename);
 
             } else if ($return == "text") {
@@ -505,8 +505,7 @@ class blog_entry {
 
             } else {
                 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {    // Image attachments don't get printed as links
-                    $image = $OUTPUT->image($ffurl, array('alt'=>$filename));
-                    $imagereturn .= "<br />" . $OUTPUT->image($image);
+                    $imagereturn .= "<br />" . $OUTPUT->pix_icon($ffurl, $filename);
                 } else {
                     $imagereturn .= html_writer::link($ffurl, $image);
                     $imagereturn .= filter_text(html_writer::link($ffurl, $filename));
index 38d03bd..b2c3eda 100644 (file)
@@ -106,7 +106,7 @@ if (empty($pending)) {
     $strrequireskey = get_string('requireskey');
 
     // Loop over requested courses.
-    $keyicon = $OUTPUT->image('i/key', array('alt'=>$strrequireskey, 'class'=>'icon'));
+    $keyicon = $OUTPUT->pix_icon('i/key', $strrequireskey, 'moodle', array('class'=>'icon'));
 
     foreach ($pending as $course) {
         $course = new course_request($course);
index 70fd47e..5d74e1a 100644 (file)
             $activitycell = new html_table_cell();
             $activitycell->add_class('activity');
 
-            $activityicon = $OUTPUT->image($OUTPUT->pix_url('icon', $cm->modname), array('alt'=>$modulename, 'class'=>'icon'));
+            $activityicon = $OUTPUT->pix_icon('icon', $modulename, $cm->modname, array('class'=>'icon'));
 
             $attributes = array();
             if (!$cm->visible) {
index c88ddbb..af2e5bb 100644 (file)
@@ -1542,7 +1542,7 @@ class grade_structure {
             $strparamobj->itemname = $element['object']->grade_item->itemname;
             $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj);
 
-            $action = $OUTPUT->image('t/unlock_gray', array('alt'=>$strnonunlockable, 'title'=>$strnonunlockable, 'class'=>'iconsmall'));
+            $action = $OUTPUT->pix_icon('t/unlock_gray', $strnonunlockable);
 
         } else if ($element['object']->is_locked()) {
             $type = 'unlock';
index 8e8d114..6dd2354 100644 (file)
@@ -2653,7 +2653,7 @@ function print_user_picture($user, $courseid, $picture=NULL, $size=0, $return=fa
  *
  */
 function print_png() {
-    throw new coding_exception('print_png() has been deprecated since Moodle 2.0. Please use $OUTPUT->image() instead.');
+    throw new coding_exception('print_png() has been deprecated since Moodle 2.0. Please use $OUTPUT->pix_icon() instead.');
 }
 
 
index 9d79e13..dadd5a8 100644 (file)
@@ -412,7 +412,7 @@ class navigation_node {
         }
 
         if ($this->icon!==null) {
-            $icon = $OUTPUT->image($this->icon, array('alt'=>'', 'class'=>'icon'));
+            $icon = $OUTPUT->pix_icon($this->icon, '', 'moodle', array('class'=>'icon'));
             $content = $icon.$content; // use CSS for spacing of icons
         } else if ($this->helpbutton!==null) {
             $content = sprintf('%s<span class="clearhelpbutton">%s</span>',trim($this->helpbutton),$content);
index 02262b7..9e2a6a6 100644 (file)
@@ -180,6 +180,40 @@ class help_icon implements renderable {
 }
 
 
+/**
+ * Data structure representing an icon.
+ *
+ * @copyright 2010 Petr Skoda
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.0
+ */
+class pix_icon implements renderable {
+    var $pix;
+    var $component;
+    var $attributes = array();
+
+    /**
+     * Constructor
+     * @param string $pix short icon name
+     * @param string $component component name
+     * @param array $attributes html attributes
+     */
+    public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
+        $this->icon       = $pix;
+        $this->compondent = $component;
+        $this->attributes = (array)$attributes;
+
+        $this->attributes['alt'] = $alt;
+        if (empty($this->attributes['class'])) {
+            $this->attributes['class'] = 'smallicon';
+        }
+        if (!isset($this->attributes['title'])) {
+            $this->attributes['title'] = $this->attributes['alt'];
+        }
+    }
+}
+
+
 /**
  * Data structure representing a simple form with only one button.
  *
index 1f659ac..a63edbc 100644 (file)
@@ -1225,10 +1225,9 @@ class core_renderer extends renderer_base {
     public function doc_link($path, $text) {
         global $CFG;
 
-        $options = array('class'=>'iconhelp', 'alt'=>$text);
-        $url = new moodle_url(get_docs_url($path));
+        $icon = $this->pix_icon('docs', $text, 'moodle', array('class'=>'iconhelp'));
 
-        $icon = $this->image('docs', $options);
+        $url = new moodle_url(get_docs_url($path));
 
         $link = new html_link($url, $icon.$text);
 
@@ -1239,6 +1238,30 @@ class core_renderer extends renderer_base {
         return $this->link($link);
     }
 
+    /**
+     * Render icon
+     * @param string $pix short pix name
+     * @param string $alt mandatory alt attribute
+     * @param strin $component standard compoennt name like 'moodle', 'mod_form', etc.
+     * @param array $attributes htm lattributes
+     * @return string HTML fragment
+     */
+    public function pix_icon($pix, $alt, $component='moodle', array $attributes = null) {
+        $icon = new pix_icon($pix, $alt, $component, $attributes);
+        return $this->render($icon);
+    }
+
+    /**
+     * Render icon
+     * @param pix_icon $icon
+     * @return string HTML fragment
+     */
+    public function render_icon(pix_icon $icon) {
+        $attributes = $icon->attributes;
+        $attributes['src'] = $this->pix_url($icon->pix, $icon->component);
+        return html_writer::empty_tag('img', $atrributes);
+    }
+
     /**
      * Given a moodle_action_icon object, outputs an image linking to an action (URL or AJAX).
      *
index 591b263..3a102ce 100644 (file)
@@ -823,7 +823,7 @@ function quiz_question_preview_button($quiz, $question, $label = false) {
     }
 
     // Build the icon.
-    $image = $OUTPUT->image($OUTPUT->pix_url('t/preview'), array('class'=>'iconsmall', 'alt'=>$strpreviewquestion));
+    $image = $OUTPUT->pix_icon('t/preview', $strpreviewquestion);
 
     $link = new html_link($CFG->wwwroot."/question/preview.php?id=$question->id&quizid=$quiz->id", $image, array('title' => $strpreviewquestion));
     parse_str(QUESTION_PREVIEW_POPUP_OPTIONS, $options);
index 5628955..1f2ff4e 100644 (file)
@@ -626,7 +626,7 @@ class question_bank_preview_action_column extends question_bank_action_column_ba
         global $OUTPUT;
         if (question_has_capability_on($question, 'use')) {
             // Build the icon.
-            $image = $OUTPUT->image($OUTPUT->pix_url('t/preview'), array('class'=>'iconsmall', 'alt'=>$this->strpreview));
+            $image = $OUTPUT->pix_icon('t/preview', $this->strpreview);
 
             $link = new html_link($this->qbank->preview_question_url($question->id), $image, array('title' => $this->strpreview));
             parse_str(QUESTION_PREVIEW_POPUP_OPTIONS, $options);