--- loncom/interface/lonhtmlcommon.pm 2005/11/15 20:46:40 1.118 +++ loncom/interface/lonhtmlcommon.pm 2005/11/15 21:08:47 1.119 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common html routines # -# $Id: lonhtmlcommon.pm,v 1.118 2005/11/15 20:46:40 raeburn Exp $ +# $Id: lonhtmlcommon.pm,v 1.119 2005/11/15 21:08:47 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1337,7 +1337,7 @@ sub role_select_row { if (defined($title)) { $output = &row_title($col_width,$tablecolor,$title); } - $output .= qq| + $output .= qq| \n|; foreach my $status_type (sort(keys(%{$types}))) { $output .= ' '; @@ -1500,6 +1500,194 @@ sub submit_row { return $output; } +############################################## +############################################## + +# echo_form_input +# +# Generates html markup to add form elements from the referrer page +# as hidden form elements (values encoded) in the new page. +# +# Intended to support two types of use +# (a) to allow backing up to earlier pages in a multi-page +# form submission process using a breadcrumb trail. +# +# (b) to allow the current page to be reloaded with form elements +# set on previous page to remain unchanged. An example would +# be where the a page containing a dynamically-built table of data is +# is to be redisplayed, with only the sort order of the data changed. +# +# Inputs: +# 1. Reference to array of form elements in the submitted form on +# the referrer page which are to be excluded from the echoed elements. +# +# 2. Reference to array of regular expressions, which if matched in the +# name of the form element n the referrer page will be omitted from echo. +# +# Outputs: A scalar containing the html markup for the echoed form +# elements (all as hidden elements, with values encoded). + + +sub echo_form_input { + my ($excluded,$regexps) = @_; + my $output = ''; + foreach my $key (keys(%env)) { + if ($key =~ /^form\.(.+)$/) { + my $name = $1; + my $match = 0; + if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) { + if (defined($regexps)) { + if (@{$regexps} > 0) { + foreach my $regexp (@{$regexps}) { + if ($name =~ /\Q$regexp\E/) { + $match = 1; + last; + } + } + } + } + if (!$match) { + if (ref($env{$key})) { + foreach my $value (@{$env{$key}}) { + $value = &HTML::Entities::encode($value,'<>&"'); + $output .= ''."\n"; + } + } else { + my $value = &HTML::Entities::encode($env{$key},'<>&"'); + $output .= ''."\n"; + } + } + } + } + } + return $output; +} + +############################################## +############################################## + +# set_form_elements +# +# Generates javascript to set form elements to values based on +# corresponding values for the same form elements when the page was +# previously submitted. +# +# Last submission values are read from hidden form elements in referring +# page which have the same name, i.e., generated by &echo_form_input(). +# +# Intended to be called by onload event. +# +# Input: +# Reference to hash of echoed form elements to be set. +# +# In the hash, keys are the form element names, and the values are the +# element type (selectbox, radio, checkbox or text -for textbox, textarea or +# hidden). +# +# Output: +# +# javascript function - set_form_elements() which sets form elements, +# expects an argument: formname - the name of the form according to +# the DOM, e.g., document.compose + +sub set_form_elements { + my ($elements) = @_; + my $output .= 'function setFormElements(courseForm) { +'; + foreach my $key (keys(%env)) { + if ($key =~ /^form\.(.+)$/) { + my $name = $1; + if (exists($$elements{$name})) { + my @values = &Apache::loncommon::get_env_multiple($key); + for (my $i=0; $i<@values; $i++) { + $values[$i] = &HTML::Entities::decode($values[$i],'<>&"'); + $values[$i] =~ s/([\r\n\f]+)/\\n/g; + $values[$i] =~ s/"/\\"/g; + } + if ($$elements{$name} eq 'text') { + my $numvalues = @values; + if ($numvalues > 1) { + my $valuestring = join('","',@values); + $output .= qq| + var textvalues = new Array ("$valuestring"); + var total = courseForm.$name.length; + if (total > $numvalues) { + total = $numvalues; + } + for (var i=0; i