MDL-21235 support for optgroups in select

This commit is contained in:
Petr Skoda 2010-01-16 19:48:01 +00:00
parent d776d59ee2
commit 6770330d17
5 changed files with 50 additions and 28 deletions

View file

@ -130,6 +130,8 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
$hostarray[$CFG->mnet_localhost_id] = $SITE->fullname; $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname;
asort($hostarray); asort($hostarray);
$dropdown = array();
foreach($hostarray as $hostid => $name) { foreach($hostarray as $hostid => $name) {
$courses = array(); $courses = array();
$sites = array(); $sites = array();
@ -161,7 +163,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
} }
asort($courses); asort($courses);
$dropdown[$name] = $sites + $courses; $dropdown[] = array($name=>($sites + $courses));
} }
@ -255,9 +257,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n"; echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) { if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
$cid = empty($course->id)? '1' : $course->id; $cid = empty($course->id)? '1' : $course->id;
$select = html_select::make($dropdown, "host_course", $hostid.'/'.$cid); echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid);
$select->nested = true;
echo $OUTPUT->select($select);
} else { } else {
$courses = array(); $courses = array();
$courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : ''); $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : '');

View file

@ -64,11 +64,11 @@
if (empty($modinfo->instances[$module->name])) { if (empty($modinfo->instances[$module->name])) {
continue; continue;
} }
$agroup = get_string('modulenameplural', $module->name); $instances = array();
$instanceoptions[$agroup] = array();
foreach ($modinfo->instances[$module->name] as $cm) { foreach ($modinfo->instances[$module->name] as $cm) {
$instanceoptions[$agroup][$cm->id] = format_string($cm->name); $instances[$cm->id] = format_string($cm->name);
} }
$instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
} }
$timeoptions = array(); $timeoptions = array();
@ -117,9 +117,7 @@
echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n". echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
'<input type="hidden" name="id" value="'.$course->id.'" />'."\n"; '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n"; echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
$select = html_select::make($instanceoptions, 'instanceid', $instanceid); echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
$select->nested = true;
echo $OUTPUT->select($select);
echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n"; echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
echo html_writer::select($timeoptions,'timefrom',$timefrom); echo html_writer::select($timeoptions,'timefrom',$timefrom);
echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n"; echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";

View file

@ -431,7 +431,11 @@ class html_writer {
/** /**
* Generates a simple select form field * Generates a simple select form field
* @param array $options associative array value=>label * @param array $options associative array value=>label ex.:
* array(1=>'One, 2=>Two)
* it is also possible to specify optgroup as complex label array ex.:
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
* @param string $name name of select element * @param string $name name of select element
* @param string|array $selected value or arary of values depending on multiple attribute * @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|bool $nothing, add nothing selected option, or false of not added
@ -480,17 +484,37 @@ class html_writer {
$output = ''; $output = '';
foreach ($options as $value=>$label) { foreach ($options as $value=>$label) {
$ias = array(); if (is_array($label)) {
$value = (string)$value; //TODO: add support for opt groups as nested arrays // ignore key, it just has to be unique
if (in_array($value, $selected, true)) { $output .= self::select_optgroup(key($label), current($label), $selected);
$ias['selected'] = 'selected'; } else {
$output .= self::select_option($label, $value, $selected);
} }
$ias['value'] = $value;
$output .= self::tag('option', $ias, $label);
} }
return self::tag('select', $attributes, $output); return self::tag('select', $attributes, $output);
} }
private static function select_option($label, $value, array $selected) {
$attributes = array();
$value = (string)$value;
if (in_array($value, $selected, true)) {
$attributes['selected'] = 'selected';
}
$attributes['value'] = $value;
return self::tag('option', $attributes, $label);
}
private static function select_optgroup($groupname, $options, array $selected) {
if (empty($options)) {
return '';
}
$attributes = array('label'=>$groupname);
$output = '';
foreach ($options as $value=>$label) {
$output .= self::select_option($label, $value, $selected);
}
return self::tag('optgroup', $attributes, $output);
}
} }

View file

@ -2351,14 +2351,16 @@ function question_category_select_menu($contexts, $top = false, $currentcat = 0,
global $OUTPUT; global $OUTPUT;
$categoriesarray = question_category_options($contexts, $top, $currentcat, false, $nochildrenof); $categoriesarray = question_category_options($contexts, $top, $currentcat, false, $nochildrenof);
if ($selected) { if ($selected) {
$nothing = ''; $choose = '';
} else { } else {
$nothing = 'choosedots'; $choose = 'choosedots';
} }
$select = html_select::make($categoriesarray, 'category', $selected); $options = array();
$select->nothingvalue = $nothing; foreach($categoriesarray as $group=>$opts) {
$select->nested = true; $options[] = array($group=>$opts);
echo $OUTPUT->select($select); }
echo html_writer::select($options, 'category', $selected, $choose);
} }
/** /**

View file

@ -987,9 +987,7 @@ class assignment_base {
$options[0] = get_string('nooutcome', 'grades'); $options[0] = get_string('nooutcome', 'grades');
echo $options[$outcome->grades[$submission->userid]->grade]; echo $options[$outcome->grades[$submission->userid]->grade];
} else { } else {
$select = html_select::make($options, 'outcome_'.$n.'['.$userid.']', $outcome->grades[$submission->userid]->grade, get_string('nooutcome', 'grades')); echo html_writer::select($options, 'outcome_'.$n.'['.$userid.']', $outcome->grades[$submission->userid]->grade, get_string('nooutcome', 'grades'), array('id' => 'menuoutcome_'.$n));
$select->id = 'menuoutcome_'.$n;
echo $OUTPUT->select($select);
} }
echo '</div>'; echo '</div>';
echo '<div class="clearer"></div>'; echo '<div class="clearer"></div>';
@ -1008,7 +1006,7 @@ class assignment_base {
echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />'; echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
} else { } else {
echo '<div class="format">'; echo '<div class="format">';
echo $OUTPUT->select(html_select::make(format_text_menu(), "format", $submission->format, false)); echo html_writer::select(format_text_menu(), "format", $submission->format, false);
echo $OUTPUT->help_icon("textformat", get_string("helpformatting")); echo $OUTPUT->help_icon("textformat", get_string("helpformatting"));
echo '</div>'; echo '</div>';
} }