Skip to content

Commit

Permalink
MDL-21198 make factory methods ahve to be static; adding "real" const…
Browse files Browse the repository at this point in the history
…rutors for components; fixed regression in detection of missing image alt text in actions; api improvements; plus some other fixes
  • Loading branch information
skodak committed Dec 28, 2009
1 parent a309d7d commit 8fa1636
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 33 deletions.
88 changes: 72 additions & 16 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class html_component {
*/
protected $actions = array();

/**
* Compoment constructor.
* @param array $options image attributes such as title, id, alt, style, class
*/
public function __construct(array $options = null) {
// not implemented in this class because we want to set only public properties of this component
renderer_base::apply_component_options($this, $options);
}

/**
* Ensure some class names are an array.
* @param mixed $classes either an array of class names or a space-separated
Expand Down Expand Up @@ -218,6 +227,14 @@ class labelled_html_component extends html_component {
*/
public $label;

/**
* Compoment constructor.
* @param array $options image attributes such as title, id, alt, style, class
*/
public function __construct(array $options = null) {
parent::__construct($options);
}

/**
* Adds a descriptive label to the component.
*
Expand Down Expand Up @@ -509,7 +526,7 @@ public static function make_time_selector($type, $name, $currenttime=0, $step=5)
* @param int $step minute spacing
* @return array Instantiated date/time selectors
*/
public function make_time_selectors($selectors, $currenttime=0, $step=5) {
public static function make_time_selectors($selectors, $currenttime=0, $step=5) {
$selects = array();
foreach ($selectors as $type => $name) {
$selects[] = html_select::make_time_selector($type, $name, $currenttime, $step);
Expand All @@ -526,7 +543,7 @@ public function make_time_selectors($selectors, $currenttime=0, $step=5) {
* @param string $selected The option that is initially selected
* @return html_select A menu initialised as a popup form.
*/
public function make_popup_form($baseurl, $name, $options, $formid, $selected=null) {
public static function make_popup_form($baseurl, $name, $options, $formid, $selected=null) {
global $CFG;

$selectedurl = null;
Expand Down Expand Up @@ -811,7 +828,7 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
* @param string $alt
* @return html_select_option A component ready for $OUTPUT->checkbox()
*/
public function make_checkbox($value, $checked, $label, $alt='') {
public static function make_checkbox($value, $checked, $label, $alt='') {
$checkbox = new html_select_option();
$checkbox->value = $value;
$checkbox->selected = $checked;
Expand Down Expand Up @@ -1175,7 +1192,7 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
* @param array $cells
* @return html_table_row
*/
public function make($cells=array()) {
public static function make($cells=array()) {
$row = new html_table_row();
foreach ($cells as $celltext) {
if (!($celltext instanceof html_table_cell)) {
Expand Down Expand Up @@ -1292,7 +1309,7 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
* @param string $text The text of the link
* @return html_link The link component
*/
public function make($url, $text) {
public static function make($url, $text) {
$link = new html_link();
$link->url = $url;
$link->text = $text;
Expand Down Expand Up @@ -1349,10 +1366,6 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
* @since Moodle 2.0
*/
class html_image extends labelled_html_component {
/**
* @var string $alt A descriptive text
*/
public $alt = HTML_ATTR_EMPTY;
/**
* @var string $src The path to the image being used
*/
Expand All @@ -1366,6 +1379,29 @@ class html_image extends labelled_html_component {
*/
public $height;

/**
* New image constructor.
*
* @param moodle_url|string $url url of the image
* @param array $options image attributes such as title, id, alt, widht, height
*/
public function __construct($url = null, array $options = null) {
parent::__construct($options);

if (is_null($url)) {
// to be filled later

} else if ($url instanceof moodle_url) {
$this->src = clone($url);

} else if (is_string($url)) {
$this->src = new moodle_url($url);

} else {
throw new coding_style_exception('Image can be constructed only from moodle_url or string url.');
}
}

/**
* @see lib/html_component#prepare()
* @return void
Expand All @@ -1377,17 +1413,20 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {

// no general class here, use custom class instead or img element directly in css selectors
parent::prepare($output, $page, $target);

if ($this->alt === '') {
// needs to be set for accessibility reasons
$this->alt = HTML_ATTR_EMPTY;
}
}

/**
* Shortcut for initialising a html_image.
*
* @param mixed $url The URL to the image (string or moodle_url)
*/
public function make($url) {
$image = new html_image();
$image->src = $url;
return $image;
public static function make($url) {
return new html_image($url);
}
}

Expand Down Expand Up @@ -1512,9 +1551,12 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
parent::prepare($output, $page, $target);
}

public static function make_button($url, $options, $label='OK', $method='post') {
public static function make_button($url, $params, $label=null, $method='post') {
if ($label === null) {
$label = get_string('ok');
}
$form = new html_form();
$form->url = new moodle_url($url, $options);
$form->url = new moodle_url($url, $params);
$form->button->text = $label;
$form->method = $method;
return $form;
Expand Down Expand Up @@ -1807,7 +1849,7 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
* @param mixed $baseurl If this is a string then it is the url which will be appended with $pagevar, an equals sign and the page number.
* If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
*/
public function make($totalcount, $page, $perpage, $baseurl) {
public static function make($totalcount, $page, $perpage, $baseurl) {
$pagingbar = new moodle_paging_bar();
$pagingbar->totalcount = $totalcount;
$pagingbar->page = $page;
Expand Down Expand Up @@ -1858,6 +1900,20 @@ class user_picture extends html_image {
*/
public $url;

/**
* User picture constructor.
*
* @param object $user user record with at least id, picture, imagealt, firstname and lastname set.
* @param array $options such as link, size, link, ...
*/
public function __construct(stdClass $user = null, array $options = null) {
parent::__construct(null, $options);

if ($user) {
$this->user = $user;
}
}

/**
* @see lib/html_component#prepare()
* @return void
Expand Down
22 changes: 5 additions & 17 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ protected function prepare_legacy_width_and_height(html_component $component) {
* @param array $options
* @return void
*/
protected function apply_component_options(html_component $component, array $options = null) {
public static function apply_component_options(html_component $component, array $options = null) {
$options = (array)$options;
foreach ($options as $key => $value) {
if ($key === 'class' or $key === 'classes') {
Expand Down Expand Up @@ -1172,21 +1172,12 @@ public function spacer(array $options = null) {
public function image($image_or_url, array $options = null) {
if ($image_or_url === false) {
return false;
}

} else if ($image_or_url instanceof html_image) {
if ($image_or_url instanceof html_image) {
$image = clone($image_or_url);

} else {
if ($image_or_url instanceof moodle_url) {
$image = new html_image();
$image->src = clone($image_or_url);
} else {
// must be a string
$image = new html_image();
$image->src = new moodle_url($image_or_url);
}

$this->apply_component_options($image, $options);
$image = new html_image($image_or_url, $options);
}

$image->prepare($this, $this->page, $this->target);
Expand Down Expand Up @@ -1247,11 +1238,8 @@ public function user_picture(stdClass $user_or_userpicture, array $options = nul
if ($user_or_userpicture instanceof user_picture) {
// we need a clone because prepare() should not be called repeatedly
$userpic = clone($user_or_userpicture);

} else {
$userpic = new user_picture();
$userpic->user = $user_or_userpicture;
$this->apply_component_options($userpic, $options);
$userpic = new user_picture($user_or_userpicture, $options);
}

$userpic->prepare($this, $this->page, $this->target);
Expand Down

0 comments on commit 8fa1636

Please sign in to comment.