Skip to content

Commit

Permalink
MDL-20204 converting html_list
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Feb 17, 2010
1 parent 4ee753e commit 5be262b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 169 deletions.
15 changes: 7 additions & 8 deletions blocks/blog_menu/block_blog_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ function get_content() {
/// 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']);
}
}

Expand All @@ -92,7 +91,7 @@ function get_content() {
!$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'));
}
}

Expand All @@ -106,7 +105,7 @@ function get_content() {
$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
Expand All @@ -115,7 +114,7 @@ function get_content() {
$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']);
}
}

Expand All @@ -131,6 +130,6 @@ function get_content() {
$this->content->footer = '';
}

$this->content->text = $OUTPUT->htmllist($menulist);
$this->content->text = html_writer::alist($menulist, array('class'=>'list'));
}
}
7 changes: 3 additions & 4 deletions blocks/blog_recent/block_blog_recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ function get_content() {
$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'];
Expand Down
6 changes: 3 additions & 3 deletions grade/import/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
126 changes: 21 additions & 105 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,27 @@ public static function select_time($type, $name, $currenttime=0, $step=5, array
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
Expand Down Expand Up @@ -1576,111 +1597,6 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
}


/**
* 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


Expand Down
41 changes: 0 additions & 41 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1542,47 +1542,6 @@ public function edit_button(moodle_url $url) {
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
*
Expand Down
8 changes: 0 additions & 8 deletions lib/simpletest/testoutputlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,6 @@ public function test_error_text_blank() {
$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
Expand Down

0 comments on commit 5be262b

Please sign in to comment.