Skip to content

Commit

Permalink
MDL-21235 support for optgroups in select
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 16, 2010
1 parent d776d59 commit 6770330
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
8 changes: 4 additions & 4 deletions course/report/log/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
$hostarray[$CFG->mnet_localhost_id] = $SITE->fullname;
asort($hostarray);

$dropdown = array();

foreach($hostarray as $hostid => $name) {
$courses = array();
$sites = array();
Expand Down Expand Up @@ -161,7 +163,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
}

asort($courses);
$dropdown[$name] = $sites + $courses;
$dropdown[] = array($name=>($sites + $courses));
}


Expand Down Expand Up @@ -255,9 +257,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
$cid = empty($course->id)? '1' : $course->id;
$select = html_select::make($dropdown, "host_course", $hostid.'/'.$cid);
$select->nested = true;
echo $OUTPUT->select($select);
echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid);
} else {
$courses = array();
$courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : '');
Expand Down
10 changes: 4 additions & 6 deletions course/report/participation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
if (empty($modinfo->instances[$module->name])) {
continue;
}
$agroup = get_string('modulenameplural', $module->name);
$instanceoptions[$agroup] = array();
$instances = array();
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();
Expand Down Expand Up @@ -117,9 +117,7 @@
echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
'<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
$select = html_select::make($instanceoptions, 'instanceid', $instanceid);
$select->nested = true;
echo $OUTPUT->select($select);
echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
echo html_writer::select($timeoptions,'timefrom',$timefrom);
echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
Expand Down
40 changes: 32 additions & 8 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ public static function link($url, $text, array $attributes = null) {

/**
* 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|array $selected value or arary of values depending on multiple attribute
* @param array|bool $nothing, add nothing selected option, or false of not added
Expand Down Expand Up @@ -480,17 +484,37 @@ public static function select(array $options, $name, $selected = '', $nothing =

$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';
if (is_array($label)) {
// ignore key, it just has to be unique
$output .= self::select_optgroup(key($label), current($label), $selected);
} else {
$output .= self::select_option($label, $value, $selected);
}
$ias['value'] = $value;
$output .= self::tag('option', $ias, $label);
}

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);
}
}


Expand Down
14 changes: 8 additions & 6 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2351,14 +2351,16 @@ function question_category_select_menu($contexts, $top = false, $currentcat = 0,
global $OUTPUT;
$categoriesarray = question_category_options($contexts, $top, $currentcat, false, $nochildrenof);
if ($selected) {
$nothing = '';
$choose = '';
} else {
$nothing = 'choosedots';
$choose = 'choosedots';
}
$select = html_select::make($categoriesarray, 'category', $selected);
$select->nothingvalue = $nothing;
$select->nested = true;
echo $OUTPUT->select($select);
$options = array();
foreach($categoriesarray as $group=>$opts) {
$options[] = array($group=>$opts);
}

echo html_writer::select($options, 'category', $selected, $choose);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,7 @@ function display_submission($extra_javascript = '') {
$options[0] = get_string('nooutcome', 'grades');
echo $options[$outcome->grades[$submission->userid]->grade];
} else {
$select = html_select::make($options, 'outcome_'.$n.'['.$userid.']', $outcome->grades[$submission->userid]->grade, get_string('nooutcome', 'grades'));
$select->id = 'menuoutcome_'.$n;
echo $OUTPUT->select($select);
echo html_writer::select($options, 'outcome_'.$n.'['.$userid.']', $outcome->grades[$submission->userid]->grade, get_string('nooutcome', 'grades'), array('id' => 'menuoutcome_'.$n));
}
echo '</div>';
echo '<div class="clearer"></div>';
Expand All @@ -1008,7 +1006,7 @@ function display_submission($extra_javascript = '') {
echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
} else {
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 '</div>';
}
Expand Down

0 comments on commit 6770330

Please sign in to comment.