Skip to content

Commit

Permalink
MDL-21235 html_writer now supports select form elements
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 16, 2010
1 parent a685593 commit 3ff163c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 32 deletions.
17 changes: 4 additions & 13 deletions admin/report/capability/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,15 @@

// Print the settings form.
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
echo '<form method="get" action="." id="settingsform">';
echo '<form method="get" action="." id="settingsform"><div>';
echo $OUTPUT->heading(get_string('reportsettings', 'report_capability'));
echo '<p id="intro">', get_string('intro', 'report_capability') , '</p>';
echo '<p><label for="menucapability"> ' . get_string('capabilitylabel', 'report_capability') . '</label></p>';
$select = html_select::make($capabilitychoices, 'capability', $capability);
$select->nothingvalue = '';
$select->listbox = true;
$select->tabindex = 0;
echo $OUTPUT->select($select);
echo html_writer::input_select($capabilitychoices, 'capability', $capability, array(''=>'choose'), array('size'=>10));
echo '<p><label for="menuroles"> ' . get_string('roleslabel', 'report_capability') . '</label></p>';
$select = html_select::make($rolechoices, 'roles[]', $selectedroleids, false);
$select->nothingvalue = '';
$select->listbox = true;
$select->multiple = true;
$select->tabindex = 0;
echo $OUTPUT->select($select);
echo html_writer::input_select($rolechoices, 'roles[]', $selectedroleids, false, array('size'=>10, 'multiple'=>'multiple'));
echo '<p><input type="submit" id="settingssubmit" value="' . get_string('getreport', 'report_capability') . '" /></p>';
echo '</form>';
echo '</div></form>';
echo $OUTPUT->box_end();

// If we have a capability, generate the report.
Expand Down
59 changes: 59 additions & 0 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,65 @@ public static function link($url, $text, array $attributes = null) {
$attributes['href'] = $url;
return self::tag('a', $attributes, $text);
}

/**
* Generates a simple select form field
* @param array $options associative array value=>label
* @param string $name name of select element
* @param string|array $selected value or arary of values depending on multiple attribute
* @param array|bool $nothing, add nothing selected option, or false of not added
* @param array $attributes - html select element attributes
* @return string HRML fragment
*/
public static function input_select(array $options, $name, $selected = '', $nothing = array(''=>'choose'), array $attributes = null) {
$attributes = (array)$attributes;
if (is_array($nothing)) {
foreach ($nothing as $k=>$v) {
if ($v === 'choose') {
$nothing[$k] = get_string('choosedots');
}
}
$options = $nothing + $options; // keep keys, do not override
}

// we may accept more values if multiple attribute specified
$selected = (array)$selected;
foreach ($selected as $k=>$v) {
$selected[$k] = (string)$v;
}

if (!isset($attributes['id'])) {
$id = 'menu'.$name;
// name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
$id = str_replace('[', '', $id);
$id = str_replace(']', '', $id);
$attributes['id'] = $id;
}

if (!isset($attributes['class'])) {
$class = 'menu'.$name;
// name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
$class = str_replace('[', '', $class);
$class = str_replace(']', '', $class);
$attributes['class'] = $class;
}
$attributes['class'] = 'select ' . $attributes['class']; /// Add 'select' selector always

$attributes['name'] = $name;

$output = '';
foreach ($options as $value=>$label) {
$ias = array();
$value = (string)$value; //TODO: add support for opt groups as nested arrays
if (in_array($value, $selected, true)) {
$ias['selected'] = 'selected';
}
$ias['value'] = $value;
$output .= self::tag('option', $ias, $label);
}

return self::tag('select', $attributes, $output);
}
}


Expand Down
2 changes: 1 addition & 1 deletion mod/glossary/formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
//Sort it
asort($formats);

echo $OUTPUT->select(html_select::make($formats,'popupformatname',$displayformat->popupformatname));
echo html_writer::input_select($formats, 'popupformatname', $displayformat->popupformatname, false);
?>
</td>
<td width="60%">
Expand Down
2 changes: 1 addition & 1 deletion tag/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
$timemodified = format_time(time() - $tag->timemodified);
$checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
$text = '<input type="text" name="newname['.$tag->id.']" />';
$tagtype = $OUTPUT->select(html_select::make($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false));
$tagtype = html_writer::input_select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false);

//if the tag if flagged, highlight it
if ($tag->flag > 0) {
Expand Down
14 changes: 2 additions & 12 deletions user/addnote.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,7 @@
$contents = optional_param('contents', array(), PARAM_RAW); // array of user notes
$states = optional_param('states', array(), PARAM_ALPHA); // array of notes states

$url = new moodle_url('/user/addnote.php', array('id'=>$id));
if ($users !== 0) {
$url->param('userid', $users);
}
if ($contents !== 0) {
$url->param('contents', $contents);
}
if ($states !== 0) {
$url->param('states', $states);
}
$PAGE->set_url($url);
$PAGE->set_url('/user/addnote.php', array('id'=>$id));

if (! $course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourseid');
Expand Down Expand Up @@ -119,7 +109,7 @@
if(!$user = $DB->get_record('user', array('id'=>$v))) {
continue;
}
$checkbox = $OUTPUT->select(html_select::make($state_names, 'states[' . $k . ']', empty($states[$k]) ? NOTES_STATE_PUBLIC : $states[$k], false));
$checkbox = html_writer::input_select($state_names, 'states[' . $k . ']', empty($states[$k]) ? NOTES_STATE_PUBLIC : $states[$k], false);
$table->data[] = array(
'<input type="hidden" name="userid['.$k.']" value="'.$v.'" />'. fullname($user, true),
'<textarea name="contents['. $k . ']" rows="2" cols="40">' . strip_tags(@$contents[$k]) . '</textarea>',
Expand Down
4 changes: 2 additions & 2 deletions user/extendenrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@
unset($userbasemenu[2]);
}

$checkbox = $OUTPUT->select(html_select::make($periodmenu, "extendperiod[{$m[1]}]", "0", $nochange));
$checkbox2 = $OUTPUT->select(html_select::make($userbasemenu, "extendbase[{$m[1]}]", "2", false));
$checkbox = html_writer::input_select($periodmenu, "extendperiod[{$m[1]}]", "0", array('0'=>$nochange));
$checkbox2 = html_writer::input_select($userbasemenu, "extendbase[{$m[1]}]", "2", false);
$table->data[] = array(
fullname($user, true),
$timestart,
Expand Down
2 changes: 1 addition & 1 deletion user/groupaddnote.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

echo '<p>' . $strpublishstate;
echo $OUTPUT->help_icon('status', $strpublishstate, 'notes');
echo $OUTPUT->select(html_select::make($state_names, 'state', empty($state) ? NOTES_STATE_PUBLIC : $state, false));
echo html_writer::input_select($state_names, 'state', empty($state) ? NOTES_STATE_PUBLIC : $state, false);
echo '</p>';

echo '<input type="submit" value="' . get_string('savechanges'). '" /></div></form>';
Expand Down
4 changes: 2 additions & 2 deletions user/groupextendenrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@
echo $OUTPUT->table($table);
echo '<div style="width:100%;text-align:center;"><strong>';
echo get_string('extendperiod') . ' ';
echo $OUTPUT->select(html_select::make($periodmenu, 'extendperiod'));
echo html_writer::input_select($periodmenu, 'extendperiod');
echo ' ' . get_string('startingfrom') . ' ';
echo $OUTPUT->select(html_select::make($basemenu, 'extendbase', '2', false));
echo html_writer::input_select($basemenu, 'extendbase', '2', false);
echo '</strong><br />';
echo '<input type="submit" value="'.get_string('savechanges').'" />';
echo '</div></form>';
Expand Down

0 comments on commit 3ff163c

Please sign in to comment.