MDL-19756 Centralised $label object into moodle_html_component, fixed proper display of radio buttons through select() method, and fixed other little bugs

This commit is contained in:
nicolasconnault 2009-08-11 07:05:46 +00:00
parent 8deb233dfa
commit 1ae3767a0b
2 changed files with 34 additions and 21 deletions

View File

@ -48,6 +48,10 @@ class moodle_html_component {
* @var string $style value to use for the style attribute of this HTML tag.
*/
public $style = '';
/**
* @var mixed $label The label for that component. String or html_label object
*/
public $label;
/**
* @var array class names to add to this HTML element.
*/
@ -214,7 +218,8 @@ class moodle_html_component {
} else if (!empty($text)) {
$this->label = new html_label();
$this->label->for = $for;
if (empty($for) && !empty($this->id)) {
if (empty($for)) {
$this->generate_id();
$this->label->for = $this->id;
}
$this->label->text = $text;
@ -291,10 +296,6 @@ class html_select extends moodle_html_component {
* variable that will be set if this select is submitted as part of a form.
*/
public $name;
/**
* @var mixed $label The label for that component. String or html_label object
*/
public $label;
/**
* @var string $selectedvalue the option to select initially. Should match one
* of the $options array keys. Default none.
@ -442,6 +443,7 @@ class html_select extends moodle_html_component {
$timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
}
$userdatetype = 'month';
$currentdate['month'] = $currentdate['mon'];
break;
case 'days':
for ($i=1; $i<=31; $i++) {
@ -469,7 +471,8 @@ class html_select extends moodle_html_component {
$timerselector = self::make($timeunits, $name, $currentdate[$userdatetype]);
$timerselector->label = new html_label();
$timerselector->label->text = get_string(substr($type, -1), 'form');
$timerselector->label->text = get_string(substr($type, 0, -1), 'form');
$timerselector->label->for = "menu$timerselector->name";
$timerselector->label->add_class('accesshide');
$timerselector->nothinglabel = '';
@ -745,10 +748,6 @@ class html_select_option extends moodle_html_component {
* @var boolean $selected Whether or not this option is selected
*/
public $selected = false;
/**
* @var mixed $label The label for that component. String or html_label object
*/
public $label;
public function __construct() {
$this->label = new html_label();
@ -846,10 +845,6 @@ class html_field extends moodle_html_component {
* @var string $maxlength The maxlength attribute of the field (only applies to text type)
*/
public $maxlength;
/**
* @var mixed $label The label for that component. String or html_label object
*/
public $label;
public function __construct() {
$this->label = new html_label();
@ -1152,6 +1147,10 @@ class html_table_cell extends moodle_html_component {
* @var string $scope Defines a way to associate header cells and data cells in a table
*/
public $scope = '';
/**
* @var boolean $header Whether or not this cell is a header cell
*/
public $header = false;
/**
* @see lib/moodle_html_component#prepare()

View File

@ -1649,9 +1649,14 @@ class moodle_core_renderer extends moodle_renderer_base {
}
} else if ($select->rendertype == 'checkbox') {
$currentcheckbox = 0;
foreach ($select->options as $option) {
$html .= $this->checkbox($option, $select->name);
$currentcheckbox++;
// If only two choices are available, suggest using the checkbox method instead
if (count($select->options) < 3 && !$select->multiple) {
debugging('You are using $OUTPUT->select() to render two mutually exclusive choices using checkboxes. Please use $OUTPUT->checkbox(html_select_option) instead.', DEBUG_DEVELOPER);
} else {
foreach ($select->options as $option) {
$html .= $this->checkbox($option, $select->name);
$currentcheckbox++;
}
}
}
@ -1670,6 +1675,12 @@ class moodle_core_renderer extends moodle_renderer_base {
* @return string the HTML for the <input type="radio">
*/
public function radio($option, $name='unnamed') {
static $currentradio = array();
if (empty($currentradio[$name])) {
$currentradio[$name] = 0;
}
if ($option instanceof html_select_optgroup) {
throw new coding_exception('$OUTPUT->radio($option) does not support a html_select_optgroup object as param.');
} else if (!($option instanceof html_select_option)) {
@ -1680,7 +1691,7 @@ class moodle_core_renderer extends moodle_renderer_base {
$option->label->for = $option->id;
$this->prepare_event_handlers($option);
$output = $this->output_start_tag('span', array('class' => "radiogroup $select->name rb$currentradio")) . "\n";
$output = $this->output_start_tag('span', array('class' => "radiogroup $name rb{$currentradio[$name]}")) . "\n";
$output .= $this->label($option->label);
if ($option->selected == 'selected') {
@ -1697,7 +1708,7 @@ class moodle_core_renderer extends moodle_renderer_base {
'checked' => $option->selected));
$output .= $this->output_end_tag('span');
$currentradio[$name]++;
return $output;
}
@ -2064,8 +2075,11 @@ class moodle_core_renderer extends moodle_renderer_base {
'class' => $cell->get_classes_string(),
'abbr' => $cell->abbr,
'scope' => $cell->scope);
$output .= $this->output_tag('td', $tdattributes, $cell->text) . "\n";
$tagtype = 'td';
if ($cell->header) {
$tagtype = 'th';
}
$output .= $this->output_tag($tagtype, $tdattributes, $cell->text) . "\n";
}
}
$output .= $this->output_end_tag('tr') . "\n";