# The LearningOnline Network with CAPA
# Full matching style response
#
# $Id: matchresponse.pm,v 1.3 2003/02/20 09:30:42 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
# 2/21 Guy
package Apache::matchresponse;
use strict;
use HTML::Entities();
use Math::Random();
BEGIN {
&Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
}
sub start_matchresponse {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
#when in a matchresponse use these
&Apache::lonxml::register('Apache::matchresponse',
('foilgroup','foil','conceptgroup','item',
'itemgroup'));
push (@Apache::lonxml::namespace,'matchresponse');
my $id = &Apache::response::start_response($parstack,$safeeval);
%Apache::hint::match=();
if ($target eq 'meta') {
$result=&Apache::response::meta_package_write('matchresponse');
} elsif ($target eq 'edit' ) {
$result.=&Apache::edit::start_table($token).
'
 ".&Apache::edit::end_row()
.&Apache::edit::start_spanning_row();
$result.=
&Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
&Apache::edit::select_arg('Randomize Foil Order','randomize',
['yes','no'],$token).
&Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
} elsif ($target eq 'modified') {
my $constructtag=&Apache::edit::get_new_args($token,$parstack,
$safeeval,'max',
'randomize');
if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
}
return $result;
}
sub end_matchresponse {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
&Apache::response::end_response;
pop @Apache::lonxml::namespace;
&Apache::lonxml::deregister('Apache::matchresponse',
('foilgroup','foil','conceptgroup'));
return $result;
}
%Apache::response::itemgroup=();
sub start_itemgroup {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
%Apache::response::itemgroup=();
if ($target eq 'edit') {
$result=&Apache::edit::tag_start($target,$token);
$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
['yes','no'],$token);
$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
}
return $result;
}
sub end_itemgroup {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
my @names=@{ $Apache::response::itemgroup{'names'} };
my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
if ($randomize ne 'no' ) {
@names=&whichorder($#names+1,$randomize,0,
\%Apache::response::itemgroup)
}
my %letter_name_map;
my %name_letter_map;
my @alphabet=('A'..'Z');
my $i=0;
foreach my $name (@names) {
$letter_name_map{$alphabet[$i]}=$name;
$name_letter_map{$name}=$alphabet[$i];
$i++;
}
$Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
$Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
if ($target eq 'web') {
$result.='
';
my $i=0;
foreach my $name (@names) {
$result.='
'.$alphabet[$i].'
'.
$Apache::response::itemgroup{$name.'.text'}.
'
';
$i++;
}
$result.='
';
} elsif ($target eq 'edit') { $result=&Apache::edit::end_table(); }
return $result;
}
sub start_item {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result='';
if ($target eq 'web' ) {
&Apache::lonxml::startredirection;
} elsif ($target eq 'edit') {
my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
$safeeval,'-2');
$result=&Apache::edit::tag_start($target,$token,"Item");
$result.=&Apache::edit::text_arg('Name:','name',$token);
if ($randomize ne 'no') {
$result.=&Apache::edit::select_arg('Location:','location',
['random','top','bottom'],
$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,'name',
'location');
if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
}
return $result;
}
sub end_item {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $text ='';
my $result = '';
if ($target eq 'web') {
$text=&Apache::lonxml::endredirection;
}
if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
$target eq 'edit') {
my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
my $location=&Apache::lonxml::get_param('location',$parstack,
$safeeval);
&Apache::lonxml::debug("Got a name of :$name:");
if (!$name) { $name=$Apache::lonxml::curdepth; }
&Apache::lonxml::debug("Using a name of :$name:");
push @{ $Apache::response::itemgroup{'names'} }, $name;
$Apache::response::itemgroup{"$name.text"} = $text;
$Apache::response::itemgroup{"$name.location"} = $location;
}
if ($target eq 'edit') {
$result.= &Apache::edit::tag_end($target,$token,'');
}
return $result;
}
sub insert_item {
return '
';
}
%Apache::response::foilgroup=();
sub start_foilgroup {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
%Apache::response::foilgroup=();
$Apache::matchresponse::conceptgroup=0;
&Apache::response::setrandomnumber();
if ($target eq 'edit') {
$result.=&Apache::edit::start_table($token)
."