--- loncom/homework/edit.pm 2001/12/21 16:58:25 1.26 +++ loncom/homework/edit.pm 2003/06/04 23:04:34 1.53 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # edit mode helpers # -# $Id: edit.pm,v 1.26 2001/12/21 16:58:25 harris41 Exp $ +# $Id: edit.pm,v 1.53 2003/06/04 23:04:34 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -26,19 +26,27 @@ # http://www.lon-capa.org/ # # 3/20 Guy +# 01/10/02 Matthew +# 03/06/02 Matthew package Apache::edit; use strict; -use Apache::lonnet; +use Apache::lonnet(); +use HTML::Entities(); # Global Vars # default list of colors to use in editing @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f'); # depth of nesting of edit $Apache::edit::colordepth=0; +@Apache::edit::inserttag=(); +# image-type responses: active background image and curdepth at definition +$Apache::edit::bgimgsrc=''; +$Apache::edit::bgimgsrccurdepth=''; sub initialize_edit { $Apache::edit::colordepth=0; + @Apache::edit::inserttag=(); } sub tag_start { @@ -55,10 +63,8 @@ sub tag_start { &deletelist($target,$token) ." ". - &insertlist($target,$token). - " -\n"; -#". + &insertlist($target,$token).&end_row().&start_spanning_row(); +#". # &movebuttons($target,$token). # "\n"; } @@ -95,16 +101,34 @@ sub start_table { $color = $Apache::edit::colorlist[$Apache::edit::colordepth]; } $Apache::edit::colordepth++; - my $result=""; + push(@Apache::edit::inserttag,$token->[1]); + my $result='
'; + $result.='
'; return $result; } sub end_table { $Apache::edit::colordepth--; - my $result="
"; + my $result=''; + $result.="
"; + + my ($tagname,$closingtag); + if (defined($Apache::edit::inserttag[-2])) { + $tagname=$Apache::edit::inserttag[-2]; + } else {$tagname='problem';} + if (defined($Apache::edit::inserttag[-1])) { + $closingtag=$Apache::edit::inserttag[-1]; + } + $result.=&innerinsertlist('edit',$tagname,$closingtag). + "
"; + pop(@Apache::edit::inserttag); return $result; } +sub start_spanning_row { return '';} +sub start_row { return ''; } +sub end_row { return ''; } + sub movebuttons { my ($target,$token) = @_; my $result='[1]"); &Apache::lonxml::end_tag($tagstack,$parstack,$token); @@ -147,22 +171,25 @@ sub handle_delete { } sub get_insert_list { - my ($token) = @_; + my ($tagname) = @_; my $result=''; my @tagnums= (); #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist)))); - if ($Apache::lonxml::insertlist{"$token->[1].which"}) { - push (@tagnums, @{ $Apache::lonxml::insertlist{"$token->[1].which"} }); + if ($Apache::lonxml::insertlist{"$tagname.which"}) { + push (@tagnums, @{ $Apache::lonxml::insertlist{"$tagname.which"} }); } foreach my $namespace (@Apache::lonxml::namespace) { - if ($Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"}) { - push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$token->[1].which"} }); + if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) { + push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} }); } } if (@tagnums) { + my %options; foreach my $tagnum (@tagnums) { - $result.='\n"; + my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"}; + $options{$descrip} ="\n"; } + foreach my $option (sort(keys(%options))) { $result.=$options{$option}; } if ($result) { $result=''.$result; } } return $result; @@ -170,12 +197,21 @@ sub get_insert_list { sub insertlist { my ($target,$token) = @_; + return &innerinsertlist($target,$token->[1]); +} + +sub innerinsertlist { + my ($target,$tagname,$closingtag) = @_; my $result; + my $after=''; + if ($closingtag) { + $after='_after_'.$closingtag; + } if ($target eq 'edit') { - my $optionlist= &get_insert_list($token); + my $optionlist= &get_insert_list($tagname); if ($optionlist) { $result = "Insert: - $optionlist " } else { @@ -209,6 +245,32 @@ sub handle_insert { return $result; } +sub handle_insertafter { + my $tagname=shift; + if ($ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '') + { return ''; } + my $result; + my $tagnum =$ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"}; + my $func=$Apache::lonxml::insertlist{"$tagnum.function"}; + if ($func eq 'default') { + my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"}; + my $namespace; + if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); } + $result.="\n<$newtag>\n"; + } else { + if (defined(&$func)) { + { + no strict 'refs'; + $result.=&$func(); + } + } else { + my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"}; + &Apache::lonxml::error("Unable to insert (after) tag $newtag, $func was not defined. ($tagname $tagnum)"); + } + } + return $result; +} + sub insert_responseparam { return ' '; @@ -241,6 +303,13 @@ sub insert_stringresponse { '; } +sub insert_essayresponse { + return ' + + +'; +} + sub insert_optionresponse { return ' @@ -261,6 +330,28 @@ sub insert_radiobuttonresponse { '; } +sub insert_rankresponse { + return ' + + + + + +'; +} + +sub insert_matchresponse { + return ' + + + + + + + +'; +} + sub insert_displayduedate { return ''; } sub insert_displaytitle { return ''; } sub insert_hintpart { @@ -277,6 +368,30 @@ sub insert_numericalhint { '; } +sub insert_stringhint { + return ' + +'; +} + +sub insert_formulahint { + return ' + +'; +} + +sub insert_radiobuttonhint { + return ' + +'; +} + +sub insert_optionhint { + return ' + +'; +} + sub insert_startouttext { return "\n"; } @@ -289,14 +404,28 @@ sub textarea_sizes { my ($data)=@_; my $count=0; my $maxlength=-1; - foreach (split ("\n", $$data)) { $count++; - if (length($_) > $maxlength) { $maxlength = length($_); } - } + foreach (split ("\n", $$data)) { + $count+=int(length($_)/79); + $count++; + if (length($_) > $maxlength) { $maxlength = length($_); } + } my $rows = $count; my $cols = $maxlength; return ($rows,$cols); } +sub editline { + my ($tag,$data,$description,$size)=@_; + $data=&HTML::Entities::encode($data); + if ($description) { $description="
".$description."
"; } + my $result = <<"END"; +$description + +END + return $result; +} + sub editfield { my ($tag,$data,$description,$minwidth,$minheight)=@_; @@ -305,7 +434,9 @@ sub editfield { if ($cols < $minwidth ) { $cols = $minwidth; } if ($rows < $minheight) { $rows = $minheight; } if ($description) { $description="
".$description."
"; } - return "$description\n   \n"; + return $description."\n".'   '."\n"; } sub modifiedfield { @@ -317,13 +448,6 @@ sub modifiedfield { # &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth"); # &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"}); $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"}; - if (defined $token) { - if (defined $token->[4]) { - $result=$token->[4].$result; - } else { - $result=$result.$token->[2]; - } - } return $result; } @@ -335,9 +459,10 @@ sub get_new_args { foreach my $arg (@args) { #just want the string that it was set to my $value=$token->[2]->{$arg}; - my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"}; + my $element=&html_element_name($arg); + my $newvalue=$ENV{"form.$element"}; &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:"); - if ($value ne $newvalue) { + if (defined($newvalue) && $value ne $newvalue) { $token->[2]->{$arg}=$newvalue; $rebuild=1; } @@ -368,58 +493,184 @@ sub rebuild_tag { return $result; } +sub html_element_name { + my ($name) = @_; + return $name.'_'.$Apache::lonxml::curdepth; +} + +sub hidden_arg { + my ($name,$token) = @_; + my $result; + my $arg=$token->[2]{$name}; + $result=''; + return $result; +} + sub text_arg { my ($description,$name,$token,$size) = @_; my $result; if (!defined $size) { $size=20; } my $arg=$token->[2]{$name}; - $result=$description.''; return $result; } sub select_arg { - my ($description,$name,$list,$token) = @_; - my $result; - my $optionlist=""; - my $selected=$token->[2]{$name}; - foreach my $option (@$list) { - if ( $selected eq $option ) { - $optionlist.="\n"; - } else { - $optionlist.="\n"; + my ($description,$name,$list,$token) = @_; + my $result; + my $optionlist=""; + my $selected=$token->[2]{$name}; + foreach my $option (@$list) { + my $value; + if ( ref($option) eq 'ARRAY') { + $value='value="'.$$option[0].'"'; + $option=$$option[1]; + } else { + $value='value="'.$option.'"'; + } + if ( $selected eq $option ) { + $optionlist.="\n"; + } else { + $optionlist.="\n"; + } } - } - $result.=$description.' '.$optionlist.' - '; - return $result; + '; + return $result; } sub select_or_text_arg { - my ($description,$name,$list,$token,$size) = @_; - my $result; - my $optionlist=""; - my $found=0; - my $selected=$token->[2]{$name}; - foreach my $option (@$list) { - if ( $selected eq $option ) { - $optionlist.="\n"; - $found=1; + my ($description,$name,$list,$token,$size) = @_; + my $result; + my $optionlist=""; + my $found=0; + my $selected=$token->[2]{$name}; + foreach my $option (@$list) { + my $value; + if ( ref($option) eq 'ARRAY') { + $value='value="'.$$option[0].'"'; + $option=$$option[1]; + } else { + $value='value="'.$option.'"'; + } + if ( $selected eq $option ) { + $optionlist.="\n"; + $found=1; + } else { + $optionlist.="\n"; + } + } + $optionlist.="\n"; + if (($found) || (!$selected)) { + $result.=$description.' '; } else { - $optionlist.="\n"; + $result.=&text_arg($description,$name,$token,$size); } - } - $optionlist.="\n"; - if ($found) { - $result.=$description.''; - } else { - $result.=&text_arg($description,$name,$token,$size); - } - return $result; + return $result; +} + +#----------------------------------------------------- image coordinates +# single image coordinates, x, y +sub entercoords { + my ($idx,,$idy,$mode,$width,$height) = @_; + unless ($Apache::edit::bgimgsrc) { return ''; } + if ($idx) { $idx.='_'; } + if ($idy) { $idy.='_'; } + my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc); + my $form = 'lonhomework'; + my $element; + if (! defined($mode) || $mode eq 'attribute') { + $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth"); + } elsif ($mode eq 'textnode') { # for data between ... + $element = &Apache::lonnet::escape('homework_edit_'. + $Apache::lonxml::curdepth); + } + my $formheight=''; + if ($height) { + $formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth; + } + my $formwidth=''; + if ($width) { + $formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth; + } + my $result = <<"ENDBUTTON"; +Click Coordinates +ENDBUTTON + return $result; +} + +# coordinate pair (x1,y1)-(x2,y2) +sub entercoordpair { + my ($id,$mode,$width,$height) = @_; + unless ($Apache::edit::bgimgsrc) { return ''; } + my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc); + my $form = 'lonhomework'; + my $element; + if (! defined($mode) || $mode eq 'attribute') { + $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth"); + } elsif ($mode eq 'textnode') { # for data between ... + $element = &Apache::lonnet::escape('homework_edit_'. + $Apache::lonxml::curdepth); + } + my $formheight=''; + if ($height) { + $formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth; + } + my $formwidth=''; + if ($width) { + $formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth; + } + my $result = <<"ENDBUTTON"; +Click Coordinate Pair +ENDBUTTON + return $result; +} +#----------------------------------------------------- browse +sub browse { + # insert a link to call up the filesystem browser (lonindexer) + my ($id, $mode) = @_; + my $form = 'lonhomework'; + my $element; + if (! defined($mode) || $mode eq 'attribute') { + $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth"); + } elsif ($mode eq 'textnode') { # for data between ... + $element = &Apache::lonnet::escape('homework_edit_'. + $Apache::lonxml::curdepth); + } + my $result = <<"ENDBUTTON"; +Browse +ENDBUTTON + return $result; +} + +#----------------------------------------------------- browse +sub search { + # insert a link to call up the filesystem browser (lonindexer) + my ($id, $mode) = @_; + my $form = 'lonhomework'; + my $element; + if (! defined($mode) || $mode eq 'attribute') { + $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth"); + } elsif ($mode eq 'textnode') { # for data between ... + $element = &Apache::lonnet::escape('homework_edit_'. + $Apache::lonxml::curdepth); + } + my $result = <<"ENDBUTTON"; +Search +ENDBUTTON + return $result; } + + 1; __END__ @@ -473,6 +724,18 @@ end_table() : reduce color depth; end ta =item * +start_spanning_row() : start a new table row spanning the 'edit' environment. + +=item * + +start_row() : start a new table row and element. + +=item * + +end_row() : end current table element and row. + +=item * + movebuttons($target,$token) : move-up and move-down buttons; return scalar string @@ -507,6 +770,21 @@ from lonxml; return a scalar string get_insert_list($token) : provide an insertion list based on possibilities from lonxml; return a scalar string +=item * +browse($elementname) : provide a link which will open up the filesystem +browser (lonindexer) and, once a file is selected, place the result in +the form element $elementname. + +=item * +search($elementname) : provide a link which will open up the filesystem +searcher (lonsearchcat) and, once a file is selected, place the result in +the form element $elementname. + +=item * +editline(tag,data,description,size): Provide a for +single-line text entry. This is to be used for text enclosed by tags, not +arguements/parameters associated with a tag. + =back incomplete...