MDL-20204 converting html_list
authorPetr Skoda <skodak@moodle.org>
Wed, 17 Feb 2010 17:56:26 +0000 (17:56 +0000)
committerPetr Skoda <skodak@moodle.org>
Wed, 17 Feb 2010 17:56:26 +0000 (17:56 +0000)
blocks/blog_menu/block_blog_menu.php
blocks/blog_recent/block_blog_recent.php
grade/import/lib.php
lib/outputcomponents.php
lib/outputrenderers.php
lib/simpletest/testoutputlib.php

index 29aeb38..beeb6e2 100755 (executable)
@@ -75,14 +75,13 @@ class block_blog_menu extends block_base {
         /// Accessibility: markup as a list.
 
         $blogmodon = false;
-        $menulist = new html_list();
-        $menulist->add_class('list');
+        $menulist = array();
 
         if (!empty($blogheaders['strview']) && $CFG->useblogassociations) {
             if ($blogheaders['url']->compare($PAGE->url) == URL_MATCH_EXACT) {
-                $menulist->add_item(html_writer::tag('span', array('class'=>'current'), $blogheaders['strview']));
+                $menulist[] = html_writer::tag('span', array('class'=>'current'), $blogheaders['strview']);
             } else {
-                $menulist->add_item(html_writer::link($blogheaders['url'], $blogheaders['strview']));
+                $menulist[] = html_writer::link($blogheaders['url'], $blogheaders['strview']);
             }
         }
 
@@ -92,7 +91,7 @@ class block_blog_menu extends block_base {
                 !$PAGE->url->param('modid') && !$PAGE->url->param('courseid') && !$PAGE->url->param('userid') && !$PAGE->url->param('entryid')) {
                 // no
             } else {
-                $menulist->add_item(html_writer::add($CFG->wwwroot .'/blog/index.php', get_string('viewsiteentries', 'blog')));
+                $menulist[] = html_writer::add($CFG->wwwroot .'/blog/index.php', get_string('viewsiteentries', 'blog'));
             }
         }
 
@@ -106,7 +105,7 @@ class block_blog_menu extends block_base {
             $murl = new moodle_url('/blog/index.php', array('userid' => $USER->id));
             $murl->params($blogheaders['url']->params());
             $murl->param('userid', $USER->id);
-            $menulist->add_item(html_writer::link($murl, get_string('viewmyentries', 'blog')));
+            $menulist[] = html_writer::link($murl, get_string('viewmyentries', 'blog'));
         }
 
         // show "Add entry" or "Blog about this" link
@@ -115,7 +114,7 @@ class block_blog_menu extends block_base {
             $aurl = new moodle_url('/blog/edit.php', array('action' => 'add'));
             $aurl->params($blogheaders['url']->params());
             if ($PAGE->url->compare($aurl) != URL_MATCH_EXACT) {
-                $menulist->add_item(html_writer::link($aurl, $blogheaders['stradd']));
+                $menulist[] =html_writer::link($aurl, $blogheaders['stradd']);
             }
         }
 
@@ -131,6 +130,6 @@ class block_blog_menu extends block_base {
             $this->content->footer = '';
         }
 
-        $this->content->text = $OUTPUT->htmllist($menulist);
+        $this->content->text = html_writer::alist($menulist, array('class'=>'list'));
     }
 }
index 61f3ac9..99a859a 100644 (file)
@@ -90,17 +90,16 @@ class block_blog_recent extends block_base {
         $entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
 
         if (!empty($entries)) {
-            $entrieslist = new html_list();
-            $entrieslist->add_class('list');
+            $entrieslist = array();
             $viewblogurl = new moodle_url('/blog/index.php');
 
             foreach ($entries as $entryid => $entry) {
                 $viewblogurl->param('entryid', $entryid);
                 $entrylink = html_writer::link($viewblogurl, shorten_text($entry->subject));
-                $entrieslist->add_item($entrylink);
+                $entrieslist[] = $entrylink;
             }
 
-            $this->content->text .= $OUTPUT->htmllist($entrieslist);
+            $this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
             $strview = get_string('viewsiteentries', 'blog');
             if (!empty($blogheaders['strview'])) {
                 $strview = $blogheaders['strview'];
index dd04656..3ba24f7 100755 (executable)
@@ -132,12 +132,12 @@ function grade_import_commit($courseid, $importcode, $importfeedback=true, $verb
         echo $OUTPUT->notification(get_string('importsuccess', 'grades'), 'notifysuccess');
         $unenrolledusers = get_unenrolled_users_in_import($importcode, $courseid);
         if ($unenrolledusers) {
-            $list = new html_list();
+            $list = array();
             foreach ($unenrolledusers as $u) {
                 $u->fullname = fullname($u);
-                $list->add_item(get_string('usergrade', 'grades', $u));
+                $list[] = get_string('usergrade', 'grades', $u);
             }
-            echo $OUTPUT->notification(get_string('unenrolledusersinimport', 'grades', $OUTPUT->htmllist($list)), 'notifysuccess');
+            echo $OUTPUT->notification(get_string('unenrolledusersinimport', 'grades', html_writer::alist($list)), 'notifysuccess');
         }
         echo $OUTPUT->continue_button($CFG->wwwroot.'/grade/index.php?id='.$courseid);
     }
index df4b123..36f1f8f 100644 (file)
@@ -894,6 +894,27 @@ class html_writer {
         return $label.$timerselector;
     }
 
+    /**
+     * Shortcut for quick making of lists
+     * @param array $items
+     * @param string $tag ul or ol
+     * @param array $attributes
+     * @return string
+     */
+    public static function alist(array $items, array $attributes = null, $tag = 'ul') {
+        //note: 'list' is a reserved keyword ;-)
+
+        $output = '';
+
+        foreach ($items as $item) {
+            $output .= html_writer::start_tag('li') . "\n";
+            $output .= $item . "\n";
+            $output .= html_writer::end_tag('li') . "\n";
+        }
+
+        return html_writer::tag($tag, $attributes, $output);
+    }
+
     /**
      * Returns hidden input fields created from url parameters.
      * @param moodle_url $url
@@ -1576,111 +1597,6 @@ class html_table_cell extends html_component {
 }
 
 
-/**
- * Component representing a list.
- *
- * The advantage of using this object instead of a flat array is that you can load it
- * with metadata (CSS classes, event handlers etc.) which can be used by the renderers.
- *
- * @copyright 2009 Nicolas Connault
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @since     Moodle 2.0
- */
-class html_list extends html_component {
-
-    /**
-     * @var array $items An array of html_list_item or html_list objects
-     */
-    public $items = array();
-
-    /**
-     * @var string $type The type of list (ordered|unordered), definition type not yet supported
-     */
-    public $type = 'unordered';
-
-    /**
-     * @var string $text An optional descriptive text for the list. Will be output as a list item before opening the new list
-     */
-    public $text = false;
-
-    /**
-     * @see lib/html_component#prepare()
-     * @return void
-     */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
-        parent::prepare($output, $page, $target);
-    }
-
-    /**
-     * This function takes a nested array of data and maps it into this list's $items array
-     * as proper html_list_item and html_list objects, with appropriate metadata.
-     *
-     * @param array $tree A nested array (array keys are ignored);
-     * @param int $row Used in identifying the iteration level and in ul classes
-     * @return void
-     */
-    public function load_data($tree, $level=0) {
-
-        $this->add_class("list-$level");
-
-        $i = 1;
-        foreach ($tree as $key => $element) {
-            if (is_array($element)) {
-                $newhtmllist = new html_list();
-                $newhtmllist->type = $this->type;
-                $newhtmllist->load_data($element, $level + 1);
-                $newhtmllist->text = $key;
-                $this->items[] = $newhtmllist;
-            } else {
-                $listitem = new html_list_item();
-                $listitem->value = $element;
-                $listitem->add_class("list-item-$level-$i");
-                $this->items[] = $listitem;
-            }
-            $i++;
-        }
-    }
-
-    /**
-     * Adds a html_list_item or html_list to this list.
-     * If the param is a string, a html_list_item will be added.
-     * @param mixed $item String, html_list or html_list_item object
-     * @return void
-     */
-    public function add_item($item) {
-        if ($item instanceof html_list_item || $item instanceof html_list) {
-            $this->items[] = $item;
-        } else {
-            $listitem = new html_list_item();
-            $listitem->value = $item;
-            $this->items[] = $item;
-        }
-    }
-}
-
-
-/**
- * Component representing a list item.
- *
- * @copyright 2009 Nicolas Connault
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @since     Moodle 2.0
- */
-class html_list_item extends html_component {
-    /**
-     * @var string $value The value of the list item
-     */
-    public $value;
-
-    /**
-     * @see lib/html_component#prepare()
-     * @return void
-     */
-    public function prepare(renderer_base $output, moodle_page $page, $target) {
-        parent::prepare($output, $page, $target);
-    }
-}
-
 /// Complex components aggregating simpler components
 
 
index 723cc52..108de3f 100644 (file)
@@ -1542,47 +1542,6 @@ class core_renderer extends renderer_base {
         return $this->single_button($url, $string);
     }
 
-    /**
-     * Outputs a HTML nested list
-     *
-     * @param html_list $list A html_list object
-     * @return string HTML structure
-     */
-    public function htmllist($list) {
-        $list = clone($list);
-        $list->prepare($this, $this->page, $this->target);
-
-        $this->prepare_event_handlers($list);
-
-        if ($list->type == 'ordered') {
-            $tag = 'ol';
-        } else if ($list->type == 'unordered') {
-            $tag = 'ul';
-        }
-
-        $output = html_writer::start_tag($tag, array('class' => $list->get_classes_string()));
-
-        foreach ($list->items as $listitem) {
-            if ($listitem instanceof html_list) {
-                $output .= html_writer::start_tag('li', array()) . "\n";
-                $output .= $this->htmllist($listitem) . "\n";
-                $output .= html_writer::end_tag('li') . "\n";
-            } else if ($listitem instanceof html_list_item) {
-                $listitem->prepare($this, $this->page, $this->target);
-                $this->prepare_event_handlers($listitem);
-                $output .= html_writer::tag('li', array('class' => $listitem->get_classes_string()), $listitem->value) . "\n";
-            } else {
-                $output .= html_writer::tag('li', array(), $listitem) . "\n";
-            }
-        }
-
-        if ($list->text) {
-            $output = $list->text . $output;
-        }
-
-        return $output . html_writer::end_tag($tag);
-    }
-
     /**
      * Prints a simple button to close a window
      *
index e1efcd5..009b74c 100644 (file)
@@ -567,14 +567,6 @@ class core_renderer_test extends UnitTestCase {
         $this->assertEqual('', $html);
     }
 
-    public function test_html_list() {
-        $htmllist = new html_list();
-        $data = array('item1', 'item2', array('item1-1', 'item1-2'));
-        $htmllist->load_data($data);
-        $htmllist->items[2]->type = 'ordered';
-        $html = $this->renderer->htmllist($htmllist);
-    }
-
     public function test_userpicture() {
         global $CFG;
         // Set up the user with the required fields