From: Mike G. v. a. <we...@ma...> - 2005-07-23 21:12:15
|
Log Message: ----------- Adding experimental modules which support Preflight Added Files: ----------- webwork-modperl/lib/WeBWorK/HTML: DropdownList.pm OptionList.pm Revision Data ------------- --- /dev/null +++ lib/WeBWorK/HTML/OptionList.pm @@ -0,0 +1,106 @@ +########################################################################= ######## +# WeBWorK Online Homework Delivery System +# Copyright =A9 2000-2003 The WeBWorK Project, http://openwebwork.sf.net= / +# $CVSHeader:=20 +#=20 +# This program is free software; you can redistribute it and/or modify i= t under +# the terms of either: (a) the GNU General Public License as published b= y the +# Free Software Foundation; either version 2, or (at your option) any la= ter +# version, or (b) the "Artistic License" which comes with this package. +#=20 +# This program is distributed in the hope that it will be useful, but WI= THOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or = FITNESS +# FOR A PARTICULAR PURPOSE. See either the GNU General Public License o= r the +# Artistic License for more details. +########################################################################= ######## + +package WeBWorK::HTML::OptionList; +use base qw(Exporter); + +=3Dhead1 NAME + +WeBWorK::HTML::ScrollingRecordList - HTML widget for a textfield with a = dropdown list + +=3Dcut + +use strict; +use warnings; +use Carp; + +our @EXPORT =3D (); +our @EXPORT_OK =3D qw( + optionList +); + + + +sub optionList { + my ($options, @Records) =3D @_; +=09 + my %options =3D (%$options); + # %options must contain: + # name - name of option list -- use $r->param("$name") + # request - the WeBWorK::Request object for the current request + # may contain: + # default - default selection from pop_up list + # size - number of rows shown in option list + # multiple - are multiple selections allowed? +=09 + croak "name not found in options" unless exists $options{name}; + croak "request not found in options" unless exists $options{request}; + my $name =3D $options{name}; + my $r =3D $options{request}; +=09 + my $default =3D $options{default}; + my $size =3D $options{size}; + $size =3D 1 unless defined $size; + my $multiple =3D $options{multiple}; + $multiple =3D 0 unless defined $multiple; + + my $value =3D $r->param($name) || ""; +=09 + my @values =3D ref $options{values} eq "ARRAY" ? @{ $options{values} } = : (); + my %labels =3D ref $options{labels} eq "HASH" ? %{ $options{labels} } := map { $_ =3D> $_ } @values; +=09 + # if someone just sends in the labels parameter, use all of them as val= ues + @values =3D keys %labels if (%labels and not @values); + + + map { $size =3D 4 + length if (length) > $size } @values; + + my %textfield_options =3D ( + name =3D> $name, + value =3D> $value, + size =3D> $size, # we need to calculate this to be the same as the p= opup_menu + ); +=09 + my %popup_options =3D ( + -name =3D> $name, + -values =3D> \@values, + -labels =3D> \%labels, + -default =3D> $default || $r->param($name) || 0, + );=09 + + return CGI::span({-class=3D>"OptionList"}, + CGI::table({cellpadding =3D> 0, cellspacing =3D> 0, border =3D> 0},=20 + CGI::Tr({}, CGI::td({}, CGI::textfield({%textfield_options}))), + CGI::Tr({}, CGI::td({}, CGI::popup_menu({%popup_options}))), + ) + ); + + return CGI::span({-class=3D>"OptionList"}, + CGI::textfield({ + name =3D> $name, + value =3D> $value, + size =3D> $size, # we need to calculate this to be the same as the p= opup_menu + }), CGI::br(), + CGI::popup_menu( + -name =3D> $name, + -values =3D> \@values, + -labels =3D> \%labels, + -default =3D> $r->param($name), + ), + ); +} + +1; --- /dev/null +++ lib/WeBWorK/HTML/DropdownList.pm @@ -0,0 +1,180 @@ +########################################################################= ######## +# WeBWorK Online Homework Delivery System +# Copyright =A9 2000-2003 The WeBWorK Project, http://openwebwork.sf.net= / +# $CVSHeader:=20 +#=20 +# This program is free software; you can redistribute it and/or modify i= t under +# the terms of either: (a) the GNU General Public License as published b= y the +# Free Software Foundation; either version 2, or (at your option) any la= ter +# version, or (b) the "Artistic License" which comes with this package. +#=20 +# This program is distributed in the hope that it will be useful, but WI= THOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or = FITNESS +# FOR A PARTICULAR PURPOSE. See either the GNU General Public License o= r the +# Artistic License for more details. +########################################################################= ######## + +package WeBWorK::HTML::DropdownRecordList; +use base qw(Exporter); + +=3Dhead1 NAME + +WeBWorK::HTML::DropdownList - HTML widget for a scrolling list of +records. + +=3Dcut + +use strict; +use warnings; +use Carp; +use WeBWorK::Utils::FormatRecords qw/getFormatsForClass formatRecords/; +use WeBWorK::Utils::SortRecords qw/getSortsForClass sortRecords/; +use WeBWorK::Utils::FilterRecords qw/getFiltersForClass filterRecords/; + +our @EXPORT =3D (); +our @EXPORT_OK =3D qw( + dropdownRecordList +); + + + +sub dropdownRecordList { + my ($options, @Records) =3D @_; +=09 + my %options =3D (default_filters=3D>[],default_sort=3D>"",default_forma= t=3D>"",%$options); + # %options must contain: + # name - name of scrolling list -- use $r->param("$name") + # request - the WeBWorK::Request object for the current request + # may contain: + # default_sort - name of sort to use by default + # default_format - name of format to use by default + # default_filter - listref, names of filters to apply by default (unim= pl.) + # allowed_filters - hashref, mapping field name to list of allowed val= ues (unimpl.) + # size - number of rows shown in scrolling list + # multiple - are multiple selections allowed? +=09 + croak "name not found in options" unless exists $options{name}; + croak "request not found in options" unless exists $options{request}; + my $name =3D $options{name}; + my $r =3D $options{request}; +=09 + my $default_sort =3D $options{default_sort} || ""; + my $default_format =3D $options{default_format} || ""; + + my @default_filters =3D @{$options{default_filters}} ; + + my $size =3D $options{size}; + my $multiple =3D $options{multiple}; +=09 + my $sorts =3D []; + my $sort_labels =3D {}; + my $selected_sort =3D ""; +=09 + my $formats =3D []; + my $format_labels =3D {}; + my $selected_format =3D ""; +=09 + my $filters =3D []; + my $filter_labels =3D {}; + my @selected_filters=3D (); +=09 + my @ids =3D (); + my %labels =3D (); +=09 + my @selected_records =3D $r->param("$name"); + + if (@Records) { + my $class =3D ref $Records[0]; + + ($filters, $filter_labels) =3D getFiltersForClass(@Records); + if (defined $r->param("$name!filter")){ + @selected_filters =3D $r->param("$name!filter"); + @selected_filters =3D ("all") unless @selected_filters; + } + else { + @selected_filters =3D @default_filters; + } +=09 + ($sorts, $sort_labels) =3D getSortsForClass($class); + $selected_sort =3D $r->param("$name!sort") + || $default_sort + || (@$sorts ? $sorts->[0] : ""); + =09 + ($formats, $format_labels) =3D getFormatsForClass($class); + $selected_format =3D $r->param("$name!format") + || $default_format + || (@$formats ? $formats->[0] : ""); + =09 + @Records =3D filterRecords({filter=3D>\@selected_filters},@Records); + =09 + @Records =3D sortRecords({preset=3D>$selected_sort}, @Records); + =09 + # generate IDs from keyfields + my @keyfields =3D $class->KEYFIELDS; + foreach my $Record (@Records) { + push @ids, join("!", map { $Record->$_ } @keyfields); + } + =09 + # generate labels hash + @labels{@ids} =3D @Records; + %labels =3D formatRecords({preset=3D>$selected_format}, %labels); + } +=09 + my %sort_popup_options =3D ( + -name =3D> "$name!sort", + -values =3D> $sorts, + -default =3D> $selected_sort, + -labels =3D> $sort_labels, + ); +=09 + my %format_popup_options =3D ( + -name =3D> "$name!format", + -values =3D> $formats, + -default =3D> $selected_format, + -labels =3D> $format_labels, + ); + + my %filter_options =3D ( + -name =3D> "$name!filter", + -values =3D> $filters, + -default =3D> \@selected_filters, + -labels =3D> $filter_labels, + -size =3D> 3, + -multiple =3D> 1, + ); + + my %list_options =3D ( + -class=3D>"ScrollingRecordList", + -name =3D> "$name", + -values =3D> \@ids, + -default =3D> \@selected_records, + -labels =3D> \%labels, + ); + $list_options{-size} =3D $size if $size; + $list_options{-multiple} =3D $multiple if $multiple; +=09 + my $value =3D $r->param($name) || ""; +=09 + map { $size =3D 4 + length if length > $size } values %{ $options{value= s} }; + + my %textfield_options =3D ( + name =3D> $name, + value =3D> $value, + size =3D> $size, # we need to calculate this to be the same as the p= opup_menu + );=09 +=09 + return CGI::div({-class=3D>"ScrollingRecordList"}, + CGI::textfield(%textfield_options), + CGI::scrolling_list(%list_options) + ); +=09 + return CGI::div({-class=3D>"ScrollingRecordList"}, + "Sort: ", CGI::popup_menu(%sort_popup_options), CGI::br(), + "Format: ", CGI::popup_menu(%format_popup_options), CGI::br(), + "Filter: ", CGI::scrolling_list(%filter_options), CGI::br(), + CGI::submit("$name!refresh", "Change Display Settings"), CGI::br(), + CGI::scrolling_list(%list_options) + ); +} + +1; |