--- loncom/homework/matchresponse.pm 2011/10/07 22:39:59 1.83 +++ loncom/homework/matchresponse.pm 2014/02/27 14:41:34 1.90 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Full matching style response # -# $Id: matchresponse.pm,v 1.83 2011/10/07 22:39:59 raeburn Exp $ +# $Id: matchresponse.pm,v 1.90 2014/02/27 14:41:34 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,6 +34,7 @@ use Apache::optionresponse(); use Apache::lonlocal; use Apache::lonnet; use Apache::lonxml; +use POSIX qw(ceil); BEGIN { &Apache::lonxml::register('Apache::matchresponse',('matchresponse')); @@ -103,20 +104,25 @@ sub start_itemgroup { if ($target eq 'edit') { $result=&Apache::edit::tag_start($target,$token); $result.=&Apache::edit::select_arg('Randomize Order:','randomize', - ['yes','no'],$token).' 'x 3; + ['yes','no'],$token); $result.=&Apache::edit::select_arg('Items Display Location:', 'location', ['top','bottom','left','right'], - $token).' 'x 3; + $token); $result.=&Apache::edit::select_arg('Items Display Direction:', 'direction', ['vertical','horizontal'], $token); + $result.=&Apache::edit::select_arg('Items Columns:', + 'columns', + [['','default'],'1','2','3','4'], + $token); $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row(); } elsif ($target eq 'modified') { my $constructtag=&Apache::edit::get_new_args($token,$parstack, $safeeval,'randomize', - 'location','direction'); + 'location','direction', + 'columns'); if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); } } elsif ($target eq 'web' or $target eq 'tex') { $Apache::matchresponse::itemtable{'location'}= @@ -131,7 +137,11 @@ sub end_itemgroup { my $result; if ($target eq 'edit') { $result=&Apache::edit::end_table(); } - if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; } + if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') { + if(!@{ $Apache::response::itemgroup{'names'} }) { return; } + } else { + return; + } my @names=@{ $Apache::response::itemgroup{'names'} }; my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval); if ($randomize ne 'no' ) { @@ -154,19 +164,51 @@ sub end_itemgroup { $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map; $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map; my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval); + my $columns=&Apache::lonxml::get_param('columns',$parstack,$safeeval); + + unless ($columns =~ /^\d+$/) { + undef($columns); + } if ($target eq 'web') { my $table='
'; # extra space to match what latex does. - my $i=0; - if ($direction eq 'horizontal') { $table .='';} - foreach my $name (@names) { - if ($direction ne 'horizontal') { $table.=''; } - $table.=''; - if ($direction ne 'horizontal') { $table.=''; } - $i++; - } - if ($direction eq 'horizontal') { $table .='';} + if ((!$columns) || ($columns < 0)) { + if ($direction eq 'horizontal') { + if (@names > 0) { + $columns = scalar(@names); + } else { + $columns = 1; + } + } else { + $columns = 1; + } + } + my $rows=ceil(scalar(@names)/$columns); + my $endloop = $columns*$rows; + for (my $i=0; $i<$endloop; $i++) { + my $label = ' '; + my $item = ' '; + my $index; + if ($direction eq 'horizontal') { + $index = $i; + } else { + $index = ($i % $columns)*$rows+int($i/$columns); + } + if ($index < scalar(@names)) { + $label = $alphabet[$index]; + $item = $Apache::response::itemgroup{$names[$index].'.text'}; + } + if ($i % $columns == 0) { + $table.=''; + } + $table.= ''; + if ($columns > 1) { + $table .= ''; + } + if ( ! (($i+1) % $columns) ) { + $table.=''; + } + } $table.='
'.$alphabet[$i].''. - $Apache::response::itemgroup{$name.'.text'}.'
'.$label.''.$item.' 
'; $Apache::matchresponse::itemtable{'display'}=$table; $Apache::lonxml::post_evaluate=0; @@ -220,7 +262,7 @@ sub start_item { $result=&Apache::edit::tag_start($target,$token,"Item"); $result.=&Apache::edit::text_arg('Name:','name',$token); if ($randomize ne 'no') { - $result.=' 'x 3 . + $result.= &Apache::edit::select_arg('Location:','location', ['random','top','bottom'], $token); @@ -345,16 +387,22 @@ sub whichfoils { sub displayanswers { my ($max,$randomize,@opt)=@_; - if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; } + if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') { + if (!@{ $Apache::response::foilgroup{'names'} }) { return; } + } else { + return; + } my @names = @{ $Apache::response::foilgroup{'names'} }; my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max), $randomize, &Apache::response::showallfoils(), \%Apache::response::foilgroup); my %name_letter_map; - if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) { - %name_letter_map= - %{ $Apache::response::itemgroup{'name_letter_map'} }; + if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') { + if (%{$Apache::response::itemgroup{'name_letter_map'}}) { + %name_letter_map= + %{ $Apache::response::itemgroup{'name_letter_map'} }; + } } my $result; if ($Apache::lonhomework::type eq 'exam') { @@ -391,9 +439,11 @@ sub grade_response { my %answerhash; my ($temp,$right,$wrong,$ignored)=(1,0,0,0); my %letter_name_map; - if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) { - %letter_name_map= - %{ $Apache::response::itemgroup{'letter_name_map'} }; + if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') { + if (%{$Apache::response::itemgroup{'letter_name_map'}}) { + %letter_name_map= + %{ $Apache::response::itemgroup{'letter_name_map'} }; + } } my @items; my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} }); @@ -450,7 +500,7 @@ sub grade_response { } } my $part=$Apache::inputtags::part; - my $nonlenient=&Apache::optionresponse::is_nonlenient($part); + my $nonlenient=&Apache::optionresponse::grading_is_nonlenient($part); my $id = $Apache::inputtags::response['-1']; my $responsestr=&Apache::lonnet::hash2str(%responsehash); my $itemstr =&Apache::lonnet::array2str(@items); @@ -530,7 +580,7 @@ sub format_prior_answer { my $output; foreach my $name (@{ $foil_order }) { - my $item=shift(@items); + my $item = &HTML::Entities::encode(shift(@items),'<>&"'); $output .= ''.$item.''; } return if (!defined($output)); @@ -564,14 +614,18 @@ sub displayfoils { my $part=$Apache::inputtags::part; my $solved=$Apache::lonhomework::history{"resource.$part.solved"}; my %letter_name_map; - if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) { - %letter_name_map= - %{ $Apache::response::itemgroup{'letter_name_map'} }; + if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') { + if (%{$Apache::response::itemgroup{'letter_name_map'}}) { + %letter_name_map= + %{ $Apache::response::itemgroup{'letter_name_map'} }; + } } my %name_letter_map; - if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) { - %name_letter_map= - %{ $Apache::response::itemgroup{'name_letter_map'} }; + if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') { + if (%{$Apache::response::itemgroup{'name_letter_map'}}) { + %name_letter_map= + %{ $Apache::response::itemgroup{'name_letter_map'} }; + } } if ( &Apache::response::show_answer() && ($target ne 'tex')) { foreach my $name (@whichfoils) { @@ -598,9 +652,19 @@ sub displayfoils { $newvariation = 1; } } - unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred') && (defined($env{'form.grade_symb'}))) || $newvariation) { - $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"}; + unless ($newvariation) { + if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) || + (($env{'form.grade_username'} eq $env{'user.name'}) && + ($env{'form.grade_domain'} eq $env{'user.domain'}))) { + $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"}; + } else { + unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || + ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) { + $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"}; + } + } } + my %lastresponse=&Apache::lonnet::str2hash($lastresponse); my @alphabet=('A'..'Z'); my @used_letters=sort(keys(%letter_name_map)); @@ -835,8 +899,10 @@ sub start_foil { if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; } $result.=&Apache::edit::text_arg('Name:','name',$token); my @names; - if (defined(@{ $Apache::response::itemgroup{'names'} })) { - @names=@{ $Apache::response::itemgroup{'names'} }; + if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') { + if (@{ $Apache::response::itemgroup{'names'} }) { + @names=@{ $Apache::response::itemgroup{'names'} }; + } } $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15'); my $randomize=&Apache::lonxml::get_param('randomize',$parstack,