[system] / branches / rel-2-2-dev / webwork2 / lib / WeBWorK / HTML / ScrollingRecordList.pm Repository:
ViewVC logotype

View of /branches/rel-2-2-dev/webwork2/lib/WeBWorK/HTML/ScrollingRecordList.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3972 - (download) (as text) (annotate)
Wed Jan 25 23:12:05 2006 UTC (7 years, 3 months ago) by sh002i
File size: 5347 byte(s)
update copyright date range -- 2000-2006. this is probably overkill,
since there are some files that were created after 2000 and some files
that were last modified before 2006.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2006 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork2/lib/WeBWorK/HTML/ScrollingRecordList.pm,v 1.7 2004/12/18 16:12:19 gage Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 package WeBWorK::HTML::ScrollingRecordList;
   18 use base qw(Exporter);
   19 
   20 =head1 NAME
   21 
   22 WeBWorK::HTML::ScrollingRecordList - HTML widget for a scrolling list of
   23 records.
   24 
   25 =cut
   26 
   27 use strict;
   28 use warnings;
   29 use Carp;
   30 use WeBWorK::Utils::FormatRecords qw/getFormatsForClass formatRecords/;
   31 use WeBWorK::Utils::SortRecords qw/getSortsForClass sortRecords/;
   32 use WeBWorK::Utils::FilterRecords qw/getFiltersForClass filterRecords/;
   33 
   34 our @EXPORT    = ();
   35 our @EXPORT_OK = qw(
   36   scrollingRecordList
   37 );
   38 
   39 
   40 
   41 sub scrollingRecordList {
   42   my ($options, @Records) = @_;
   43 
   44   my %options = (default_filters=>[],default_sort=>"",default_format=>"",%$options);
   45   # %options must contain:
   46   #  name - name of scrolling list -- use $r->param("$name")
   47   #  request - the WeBWorK::Request object for the current request
   48   # may contain:
   49   #  default_sort - name of sort to use by default
   50   #  default_format - name of format to use by default
   51   #  default_filter - listref, names of filters to apply by default (unimpl.)
   52   #  allowed_filters - hashref, mapping field name to list of allowed values (unimpl.)
   53   #  size - number of rows shown in scrolling list
   54   #  multiple - are multiple selections allowed?
   55 
   56   croak "name not found in options" unless exists $options{name};
   57   croak "request not found in options" unless exists $options{request};
   58   my $name = $options{name};
   59   my $r = $options{request};
   60 
   61   my $default_sort = $options{default_sort} || "";
   62   my $default_format = $options{default_format} || "";
   63 
   64   my @default_filters = @{$options{default_filters}} ;
   65 
   66   my $size = $options{size};
   67   my $multiple = $options{multiple};
   68 
   69   my $sorts = [];
   70   my $sort_labels = {};
   71   my $selected_sort = "";
   72 
   73   my $formats = [];
   74   my $format_labels = {};
   75   my $selected_format = "";
   76 
   77   my $filters = [];
   78   my $filter_labels = {};
   79   my @selected_filters= ();
   80 
   81   my @ids = ();
   82   my %labels = ();
   83 
   84   my $refresh_button_name    = defined($options{refresh_button_name}) ? $options{refresh_button_name}:"Change Display Settings";
   85 
   86   my @selected_records = $r->param("$name");
   87 
   88   if (@Records) {
   89     my $class = ref $Records[0];
   90 
   91     ($filters, $filter_labels) = getFiltersForClass(@Records);
   92     if (defined $r->param("$name!filter")){
   93       @selected_filters = $r->param("$name!filter");
   94       @selected_filters = ("all") unless @selected_filters;
   95     }
   96     else {
   97       @selected_filters = @default_filters;
   98     }
   99 
  100     ($sorts, $sort_labels) = getSortsForClass($class);
  101     $selected_sort = $r->param("$name!sort")
  102       || $default_sort
  103       || (@$sorts ? $sorts->[0] : "");
  104 
  105     ($formats, $format_labels) = getFormatsForClass($class);
  106     $selected_format = $r->param("$name!format")
  107       || $default_format
  108       || (@$formats ? $formats->[0] : "");
  109 
  110     @Records = filterRecords({filter=>\@selected_filters},@Records);
  111 
  112     @Records = sortRecords({preset=>$selected_sort}, @Records);
  113 
  114     # generate IDs from keyfields
  115     my @keyfields = $class->KEYFIELDS;
  116     foreach my $Record (@Records) {
  117       push @ids, join("!", map { $Record->$_ } @keyfields);
  118     }
  119 
  120     # generate labels hash
  121     @labels{@ids} = @Records;
  122     %labels = formatRecords({preset=>$selected_format}, %labels);
  123   }
  124 
  125   my %sort_popup_options = (
  126     -name => "$name!sort",
  127     -values => $sorts,
  128     -default => $selected_sort,
  129     -labels => $sort_labels,
  130   );
  131 
  132   my %format_popup_options = (
  133     -name => "$name!format",
  134     -values => $formats,
  135     -default => $selected_format,
  136     -labels => $format_labels,
  137   );
  138 
  139   my %filter_options = (
  140     -name => "$name!filter",
  141     -values => $filters,
  142     -default => \@selected_filters,
  143     -labels => $filter_labels,
  144     -size => 5,
  145     -multiple => 1,
  146   );
  147 
  148   my %list_options = (
  149     -class=>"ScrollingRecordList",
  150     -name => "$name",
  151     -values => \@ids,
  152     -default => \@selected_records,
  153     -labels => \%labels,
  154   );
  155   $list_options{-size} = $size if $size;
  156   $list_options{-multiple} = $multiple if $multiple;
  157 
  158   return CGI::div({-class=>"ScrollingRecordList"},
  159          CGI::table({-border=>0, -cellspacing=>0, -cellpadding=>0},
  160         CGI::Tr({valign=>"top"},[
  161        CGI::td({-align=>"right"},"Sort: ").
  162        CGI::td(CGI::popup_menu(%sort_popup_options)),
  163 
  164        CGI::td({-align=>"right"},"Format: ").
  165        CGI::td(CGI::popup_menu(%format_popup_options)),
  166 
  167        CGI::td({-align=>"right"},"Filter: ").
  168        CGI::td(CGI::scrolling_list(%filter_options)),
  169      ]),
  170          ),
  171          CGI::submit("$name!refresh", $refresh_button_name), CGI::br(),
  172          CGI::scrolling_list(%list_options),
  173        );
  174 }
  175 
  176 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9