File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.104: download - view: text, annotated - select for diffs
Mon Jan 29 23:51:19 2007 UTC (17 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- modfying how insertlist looks
- some code simplification

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: #
    4: # $Id: edit.pm,v 1.104 2007/01/29 23:51:19 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::edit; 
   30: 
   31: use strict;
   32: use Apache::lonnet;
   33: use HTML::Entities();
   34: use Apache::lonlocal;
   35: use lib '/home/httpd/lib/perl/';
   36: use LONCAPA;
   37:  
   38: 
   39: # Global Vars
   40: # default list of colors to use in editing
   41: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
   42: # depth of nesting of edit
   43: $Apache::edit::colordepth=0;
   44: @Apache::edit::inserttag=();
   45: # image-type responses: active background image and curdepth at definition
   46: $Apache::edit::bgimgsrc='';
   47: $Apache::edit::bgimgsrccurdepth='';
   48: 
   49: sub initialize_edit {
   50:     $Apache::edit::colordepth=0;
   51:     @Apache::edit::inserttag=();
   52: }
   53: 
   54: sub tag_start {
   55:     my ($target,$token,$description) = @_;
   56:     my $result='';
   57:     if ($target eq "edit") {
   58: 	my $tag=$token->[1];
   59: 	if (!$description) {
   60: 	    $description=&Apache::lonxml::description($token);
   61: 	    if (!$description) { $description="<$tag>"; }
   62: 	}
   63: 	$result.= &start_table($token)."<tr><td>$description</td>
   64:                       <td>Delete".
   65: 		      &deletelist($target,$token)
   66: 		      ."</td>
   67:                        <td>".
   68: 		       &insertlist($target,$token);
   69: #<td>". 
   70: #  &movebuttons($target,$token).
   71: #    "</tr><tr><td colspan=\"3\">\n";
   72: 	my @help = Apache::lonxml::helpinfo($token);
   73: 	if ($help[0]) {
   74: 	    $result .= '</td><td align="right" valign="top">' .
   75: 		Apache::loncommon::help_open_topic(@help);
   76: 	} else { $result .= "</td><td>&nbsp;"; }
   77: 	$result .= &end_row().&start_spanning_row();
   78:     }
   79:     return $result;
   80: }
   81: 
   82: sub tag_end {
   83:     my ($target,$token,$description) = @_;
   84:     my $result='';
   85:     if ($target eq 'edit') {
   86: 	$result.="</td></tr>".&end_table()."\n";
   87:     }
   88:     return $result;
   89: }
   90: 
   91: sub start_table {
   92:     my ($token)=@_;
   93:     my $tag = &Apache::lonxml::get_tag($token);
   94:     
   95:     my $color = $Apache::lonxml::insertlist{"$tag.color"};
   96:     &Apache::lonxml::debug(" $tag -- $color");
   97:     if (!defined($color)) {
   98: 	$color = $Apache::edit::colorlist[$Apache::edit::colordepth];
   99:     }
  100:     $Apache::edit::colordepth++;
  101:     push(@Apache::edit::inserttag,$token->[1]);
  102:     my $result='<div align="right">';
  103:     $result.='<table bgcolor="'.$color.'" width="97%" border="0" cellspacing="5" cellpadding="3">';
  104:     return $result;
  105: }
  106: 
  107: sub end_table {
  108:     $Apache::edit::colordepth--;
  109:     my $result='</table></div>';
  110:     $result.='<div align="left"><table><tr><td>';
  111: 
  112:     my ($tagname,$closingtag);
  113:     if (defined($Apache::edit::inserttag[-2])) {
  114: 	$tagname=$Apache::edit::inserttag[-2];
  115:     } else {$tagname='problem';}
  116:     if (defined($Apache::edit::inserttag[-1])) {
  117: 	$closingtag=$Apache::edit::inserttag[-1];
  118:     }
  119:     $result.=&innerinsertlist('edit',$tagname,$closingtag).
  120: 	"</td></tr></table></div>";
  121:     pop(@Apache::edit::inserttag);
  122:     return $result;
  123: }
  124: 
  125: sub start_spanning_row { return '<tr><td colspan="4" bgcolor="#DDDDDD">';}
  126: sub start_row          { return '<tr><td bgcolor="#DDDDDD">';            }
  127: sub end_row            { return '</td></tr>';          }
  128: 
  129: sub movebuttons {
  130:     my ($target,$token) = @_;
  131:     my $result='<input type="submit" name="moveup.'.
  132: 	$Apache::lonxml::curdepth.'" value="Move Up" />';
  133:     $result.='<input type="submit" name="movedown.'.
  134: 	$Apache::lonxml::curdepth.'" value="Move Down" />';
  135:     return $result;
  136: }
  137: 
  138: sub deletelist {
  139:     my ($target,$token) = @_;
  140:     my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
  141: <option></option>
  142: <option>Yes</option>
  143: </select>";
  144:     return $result;
  145: }
  146: 
  147: sub handle_delete {
  148:     if (!$env{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
  149:     my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  150:     my $result=0;
  151:     if ($space) {
  152: 	my $sub1="$space\:\:delete_$token->[1]";
  153: 	{
  154: 	    no strict 'refs';
  155: 	    if (defined &$sub1) {
  156: 		$result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
  157: 	    }
  158: 	}
  159:     }
  160:     if (!$result) {
  161: 	my $endtag='/'.$token->[1];
  162: 	my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser,$style);
  163: 	$$parser['-1']->get_token();
  164: 	&Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
  165: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
  166:     }
  167:     return 1;
  168: }
  169: 
  170: sub get_insert_list {
  171:     my ($tagname) = @_;
  172:     my $result='';
  173:     my @tags= ();
  174:     #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
  175:     if ($Apache::lonxml::insertlist{"$tagname.which"}) {
  176: 	push (@tags, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
  177:     }
  178:     foreach my $namespace (@Apache::lonxml::namespace) {
  179: 	if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
  180: 	    push (@tags, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
  181: 	}
  182:     }
  183:     if (@tags) {
  184: 	my %options;
  185: 	foreach my $tag (@tags) {
  186: 	    my $descrip=$Apache::lonxml::insertlist{"$tag.description"};
  187: 	    my $tagnum =$Apache::lonxml::insertlist{"$tag.num"};
  188: 	    $options{$descrip} ="<option value=\"$tagnum\">".
  189: 		$descrip."</option>\n";
  190: 	}
  191: 	foreach my $option (sort(keys(%options))) {$result.=$options{$option};}
  192: 	if ($result) { $result='<option selected="selected"></option>'.$result; }
  193:     }
  194:     return $result;
  195: }
  196: 
  197: sub insertlist {
  198:     my ($target,$token) = @_;
  199:     return &innerinsertlist($target,$token->[1]);
  200: }
  201: 
  202: sub innerinsertlist {
  203:     my ($target,$tagname,$closingtag) = @_;
  204:     my $result;
  205:     my $after='';
  206:     if ($closingtag) {
  207: 	$after='_after_'.$closingtag; 
  208:     }
  209:     if ($target eq 'edit') {
  210: 	my $optionlist= &get_insert_list($tagname);
  211: 	if ($optionlist) {
  212: 	    $result = "Insert:
  213:             <select name=\"insert$after\_$Apache::lonxml::curdepth\">
  214:                   $optionlist
  215:             </select>"
  216: 	} else {
  217: 	    $result="&nbsp;";
  218: 	}
  219:     }
  220:     return $result;
  221: }
  222: 
  223: sub handle_insert {
  224:     if ($env{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
  225:     my $tagnum = $env{"form.insert_$Apache::lonxml::curdepth"};
  226:     return &do_insert($tagnum);
  227: }
  228: 
  229: sub handle_insertafter {
  230:     my $tagname=shift;
  231:     if ($env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '') {
  232: 	return '';
  233:     }
  234:     my $tagnum =$env{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
  235:     return &do_insert($tagnum);
  236: }
  237: 
  238: sub do_insert {
  239:     my ($tagnum) = @_;
  240:     my $result;
  241: 
  242:     my $newtag = $Apache::lonxml::insertlist{"$tagnum.tag"};
  243:     my $func   = $Apache::lonxml::insertlist{"$newtag.function"};
  244:     if ($func eq 'default') {
  245: 	my $namespace;
  246: 	if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
  247: 	$result.="\n<$newtag>\n</$newtag>";
  248:     } else {
  249: 	if (defined(&$func)) {
  250: 	    {
  251: 		no strict 'refs';
  252: 		$result.=&$func();
  253: 	    }
  254: 	} else {
  255: 	    &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined. ($tagnum)");
  256: 	}
  257:     }
  258:     return $result;
  259: }
  260: 
  261: sub insert_img {
  262:     return '
  263:     <img />';
  264: }
  265: 
  266: sub insert_responseparam {
  267:     return '
  268:     <responseparam />';
  269: }
  270: 
  271: sub insert_parameter {
  272:     return '
  273:     <parameter />';
  274: }
  275: 
  276: sub insert_formularesponse {
  277:     return '
  278: <formularesponse answer="" samples="">
  279:     <responseparam description="Numerical Tolerance" type="tolerance" default="0.00001" name="tol" />
  280:     <textline size="25"/>
  281:     <hintgroup>
  282:     <startouttext /><endouttext />
  283:     </hintgroup>
  284: </formularesponse>';
  285: }
  286: 
  287: sub insert_numericalresponse {
  288:     return '
  289: <numericalresponse answer="">
  290: <responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />
  291: <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />
  292:     <textline />
  293:     <hintgroup>
  294:     <startouttext /><endouttext />
  295:     </hintgroup>
  296: </numericalresponse>';
  297: }
  298: 
  299: sub insert_customresponse {
  300:     return '
  301: <customresponse>
  302:     <answer type="loncapa/perl">
  303:     </answer>
  304:     <textline />
  305:     <hintgroup>
  306:     <startouttext /><endouttext />
  307:     </hintgroup>
  308: </customresponse>';
  309: }
  310: 
  311: sub insert_customresponse_answer {
  312:     return '
  313:     <answer type="loncapa/perl">
  314:     </answer>
  315: ';
  316: }
  317: 
  318: sub insert_stringresponse {
  319:     return '
  320: <stringresponse answer="" type="">
  321:     <textline />
  322:     <hintgroup>
  323:     <startouttext /><endouttext />
  324:     </hintgroup>
  325: </stringresponse>';
  326: }
  327: 
  328: sub insert_essayresponse {
  329:     return '
  330: <essayresponse>
  331:     <textfield></textfield>
  332: </essayresponse>';
  333: }
  334: 
  335: sub insert_imageresponse {
  336:     return '
  337: <imageresponse max="1">
  338:     <foilgroup>
  339:       <foil>
  340:       </foil>
  341:     </foilgroup>
  342:     <hintgroup>
  343:     <startouttext /><endouttext />
  344:     </hintgroup>
  345: </imageresponse>';
  346: }
  347: 
  348: sub insert_optionresponse {
  349:     return '
  350: <optionresponse max="10">
  351:     <foilgroup options="">
  352:       <foil>
  353:          <startouttext /><endouttext />
  354:       </foil>
  355:     </foilgroup>
  356:     <hintgroup>
  357:     <startouttext /><endouttext />
  358:     </hintgroup>
  359: </optionresponse>';
  360: }
  361: 
  362: sub insert_organicresponse {
  363:     return '
  364: <organicresponse>
  365:     <textline />
  366:     <hintgroup>
  367:     <startouttext /><endouttext />
  368:     </hintgroup>
  369: </organicresponse>';
  370: }
  371: 
  372: sub insert_organicstructure {
  373:     return '
  374: <organicstructure />
  375: ';
  376: }
  377: 
  378: sub insert_radiobuttonresponse {
  379:     return '
  380: <radiobuttonresponse max="10">
  381:     <foilgroup>
  382:       <foil>
  383:          <startouttext /><endouttext />
  384:       </foil>
  385:     </foilgroup>
  386:     <hintgroup>
  387:     <startouttext /><endouttext />
  388:     </hintgroup>
  389: </radiobuttonresponse>';
  390: }
  391: 
  392: sub insert_reactionresponse {
  393:     return '
  394: <reactionresponse>
  395:     <textline />
  396:     <hintgroup>
  397:     <startouttext /><endouttext />
  398:     </hintgroup>
  399: </reactionresponse>';
  400: }
  401: 
  402: sub insert_rankresponse {
  403:     return '
  404: <rankresponse max="10">
  405:     <foilgroup options="">
  406:       <foil>
  407:          <startouttext /><endouttext />
  408:       </foil>
  409:     </foilgroup>
  410:     <hintgroup>
  411:     <startouttext /><endouttext />
  412:     </hintgroup>
  413: </rankresponse>';
  414: }
  415: 
  416: sub insert_matchresponse {
  417:     return '
  418: <matchresponse max="10">
  419:     <foilgroup options="">
  420:       <itemgroup>
  421:       </itemgroup>
  422:       <foil>
  423:          <startouttext /><endouttext />
  424:       </foil>
  425:     </foilgroup>
  426:     <hintgroup>
  427:     <startouttext /><endouttext />
  428:     </hintgroup>
  429: </matchresponse>';
  430: }
  431: 
  432: sub insert_displayduedate { return '<displayduedate />'; }
  433: sub insert_displaytitle   { return '<displaytitle />'; }
  434: sub insert_hintpart {
  435:     return '
  436: <hintpart on="default">
  437:     <startouttext/><endouttext />
  438: </hintpart>';
  439: }
  440: 
  441: sub insert_hintgroup {
  442:   return '
  443: <hintgroup>
  444:     <startouttext /><endouttext />
  445: </hintgroup>';
  446: }
  447: 
  448: sub insert_numericalhint {
  449:     return '
  450: <numericalhint>
  451: </numericalhint>';
  452: }
  453: 
  454: sub insert_stringhint {
  455:     return '
  456: <stringhint>
  457: </stringhint>';
  458: }
  459: 
  460: sub insert_formulahint {
  461:     return '
  462: <formulahint>
  463: </formulahint>';
  464: }
  465: 
  466: sub insert_radiobuttonhint {
  467:     return '
  468: <radiobuttonhint>
  469: </radiobuttonhint>';
  470: }
  471: 
  472: sub insert_optionhint {
  473:     return '
  474: <optionhint>
  475: </optionhint>';
  476: }
  477: 
  478: sub insert_startouttext {
  479:     return "<startouttext /><endouttext />";
  480: }
  481: 
  482: sub insert_script {
  483:     return "\n<script type=\"loncapa/perl\"></script>";
  484: }
  485: 
  486: sub js_change_detection {
  487:     my $unsaved=&mt("There are unsaved changes");
  488:     return (<<SCRIPT);
  489: <script type="text/javascript">
  490: var clean = true;
  491: var is_submit = false;
  492: var still_ask = false;
  493: function compareForm(event_) {
  494:         if (!event_ && window.event) {
  495:           event_ = window.event;
  496:         }
  497: 	if ((!is_submit || (is_submit && still_ask)) && !clean) {
  498: 	    still_ask = false;
  499: 	    is_submit = false;
  500:             event_.returnValue = "$unsaved";
  501:             return "$unsaved";
  502:         }
  503: }
  504: function unClean() {
  505:      clean=false;
  506: }
  507: window.onbeforeunload = compareForm;
  508: </script>
  509: SCRIPT
  510: }
  511: 
  512: sub form_change_detection {
  513:     return ' onsubmit="is_submit=true;" ';
  514: }
  515: 
  516: sub element_change_detection {
  517:     return ' onchange="unClean();" ';
  518: }
  519: 
  520: sub submit_ask_anyway {
  521:     return ' onclick="still_ask=true;" ';
  522: }
  523: 
  524: sub textarea_sizes {
  525:     my ($data)=@_;
  526:     my $count=0;
  527:     my $maxlength=-1;
  528:     foreach (split ("\n", $$data)) {
  529: 	$count+=int(length($_)/79);
  530: 	$count++;
  531: 	if (length($_) > $maxlength) { $maxlength = length($_); }
  532:     }
  533:     my $rows = $count;
  534:     my $cols = $maxlength;
  535:     return ($rows,$cols);
  536: }
  537: 
  538: sub editline {
  539:     my ($tag,$data,$description,$size)=@_;
  540:     $data=&HTML::Entities::encode($data,'<>&"');
  541:     if ($description) { $description="<br />".$description."<br />"; }
  542:     my $change_code = &element_change_detection();
  543:     my $result = <<"END";
  544: $description
  545: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
  546:        value="$data" size="$size" $change_code />
  547: END
  548:     return $result;
  549: }
  550: 
  551: sub editfield {
  552:     my ($tag,$data,$description,$minwidth,$minheight,$usehtmlarea)=@_;
  553: 
  554:     my ($rows,$cols)=&textarea_sizes(\$data);
  555:     if (&Apache::lonhtmlcommon::htmlareabrowser() &&
  556: 	!&Apache::lonhtmlcommon::htmlareablocked()) {
  557: 	$rows+=7;      # make room for HTMLarea
  558: 	$minheight+=7; # make room for HTMLarea
  559:     }
  560:     if ($cols > 80) { $cols = 80; }
  561:     if ($cols < $minwidth ) { $cols = $minwidth; }
  562:     if ($rows < $minheight) { $rows = $minheight; }
  563:     if ($description) { $description="<br />".$description."<br />"; }
  564:     if ($usehtmlarea) {
  565: 	&Apache::lonhtmlcommon::add_htmlareafields('homework_edit_'.
  566: 						   $Apache::lonxml::curdepth);
  567:     }
  568:     return $description."\n".'<textarea style="width:100%" rows="'.$rows.
  569: 	'" cols="'.$cols.'" name="homework_edit_'.
  570: 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
  571: 	$Apache::lonxml::curdepth.'" '.&element_change_detection().'>'.
  572: 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
  573: 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
  574: 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
  575: }
  576: 
  577: sub modifiedfield {
  578:     my ($endtag,$parser) = @_;
  579:     my $result;
  580: #  foreach my $envkey (sort keys %env) {
  581: #    &Apache::lonxml::debug("$envkey ---- $env{$envkey}");
  582: #  }
  583: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
  584: #  &Apache::lonxml::debug($env{"form.homework_edit_$Apache::lonxml::curdepth"});
  585:     $result=$env{"form.homework_edit_$Apache::lonxml::curdepth"};
  586:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
  587:     # textareas throw away intial \n 
  588:     if ($bodytext=~/^\n/) { $result="\n".$result; }
  589:     return $result;
  590: }
  591: 
  592: # Returns a 1 if the token has been modified and you should rebuild the tag
  593: # side-effects, will modify the $token if new values are found
  594: sub get_new_args {
  595:     my ($token,$parstack,$safeeval,@args)=@_;
  596:     my $rebuild=0;
  597:     foreach my $arg (@args) {
  598: 	#just want the string that it was set to
  599: 	my $value=$token->[2]->{$arg};
  600: 	my $element=&html_element_name($arg);
  601: 	my $newvalue=$env{"form.$element"};
  602: 	&Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
  603: 	if (defined($newvalue) && $value ne $newvalue) {
  604: 	    if (ref($newvalue) eq 'ARRAY') {
  605: 		$token->[2]->{$arg}=join(',',@$newvalue);
  606: 	    } else {
  607: 		$token->[2]->{$arg}=$newvalue;
  608: 	    }
  609: 	    $rebuild=1;
  610: 	} elsif (!defined($newvalue) && defined($value)) {
  611: 	    delete($token->[2]->{$arg});
  612: 	    $rebuild=1;
  613: 	}
  614:     }
  615:     return $rebuild;
  616: }
  617: 
  618: # looks for /> on start tags
  619: sub rebuild_tag {
  620:     my ($token) = @_;
  621:     my $result;
  622:     if ($token->[0] eq 'S') {
  623: 	$result = '<'.$token->[1];
  624: 	while (my ($key,$val)= each(%{$token->[2]})) {
  625: 	    $val=~s:^\s+|\s+$::g;
  626: 	    $val=~s:"::g; #"
  627: 	    &Apache::lonxml::debug("setting :$key: to  :$val:");
  628: 	    $result.=' '.$key.'="'.$val.'"';
  629: 	}
  630: 	if ($token->[4] =~ m:/>$:) {
  631: 	    $result.=' />';
  632: 	} else {
  633: 	    $result.='>';
  634: 	}
  635:     } elsif ( $token->[0] eq 'E' ) {
  636: 	$result = '</'.$token->[1].'>';
  637:     }
  638:     return $result;
  639: }
  640: 
  641: sub html_element_name {
  642:     my ($name) = @_;
  643:     return $name.'_'.$Apache::lonxml::curdepth;
  644: }
  645: 
  646: sub hidden_arg {
  647:     my ($name,$token) = @_;
  648:     my $result;
  649:     my $arg=$token->[2]{$name};
  650:     $result='<input name="'.&html_element_name($name).
  651: 	'" type="hidden" value="'.$arg.'" />';
  652:     return $result;
  653: }
  654: 
  655: sub checked_arg {
  656:     my ($description,$name,$list,$token) = @_;
  657:     my $result;
  658:     my $optionlist="";
  659:     my $allselected=$token->[2]{$name};
  660:     $result=&mt($description);
  661:     foreach my $option (@$list) {
  662: 	my ($value,$text);
  663: 	if ( ref($option) eq 'ARRAY') {
  664: 	    $value='value="'.$$option[0].'"';
  665: 	    $text=$$option[1];
  666: 	    $option=$$option[0];
  667: 	} else {
  668: 	    $text=$option;
  669: 	    $value='value="'.$option.'"';
  670: 	}
  671: 	$result.="<nobr><label><input type='checkbox' $value name='".
  672: 	    &html_element_name($name)."'";
  673: 	foreach my $selected (split(/,/,$allselected)) {
  674: 	    if ( $selected eq $option ) {
  675: 		$result.=" checked='checked' ";
  676: 		last;
  677: 	    }
  678: 	}
  679: 	$result.=&element_change_detection()." />$text</label></nobr>\n";
  680:     }
  681:     return $result;
  682: }
  683: 
  684: sub text_arg {
  685:     my ($description,$name,$token,$size) = @_;
  686:     my $result;
  687:     if (!defined $size) { $size=20; }
  688:     my $arg=$token->[2]{$name};
  689:     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
  690: 	'" type="text" value="'.$arg.'" size="'.$size.'" '.
  691: 	&element_change_detection().'/>';
  692:     return '<nobr>'.$result.'</nobr>';
  693: }
  694: 
  695: sub select_arg {
  696:     my ($description,$name,$list,$token) = @_;
  697:     my $result;
  698:     my $optionlist="";
  699:     my $selected=$token->[2]{$name};
  700:     foreach my $option (@$list) {
  701: 	my ($text,$value);
  702: 	if ( ref($option) eq 'ARRAY') {
  703: 	    $value='value="'.&HTML::Entities::encode($$option[0]).'"';
  704: 	    $text=$$option[1];
  705: 	    $option=$$option[0];
  706: 	} else {
  707: 	    $text=$option;
  708: 	    $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
  709: 	}
  710: 	if ( $selected eq $option ) {
  711: 	    $optionlist.="<option $value selected=\"selected\">$text</option>\n";
  712: 	} else {
  713: 	    $optionlist.="<option $value >$text</option>\n";
  714: 	}
  715:     }
  716:     $result.='<nobr>'.$description.'&nbsp;<select name="'.
  717: 	&html_element_name($name).'" '.&element_change_detection().' >
  718:        '.$optionlist.'
  719:       </select></nobr>';
  720:     return $result;
  721: }
  722: 
  723: sub select_or_text_arg {
  724:     my ($description,$name,$list,$token,$size) = @_;
  725:     my $result;
  726:     my $optionlist="";
  727:     my $found=0;
  728:     my $selected=$token->[2]{$name};
  729:     foreach my $option (@$list) {
  730: 	my ($text,$value);
  731: 	if ( ref($option) eq 'ARRAY') {
  732: 	    $value='value="'.&HTML::Entities::encode($$option[0]).'"';
  733: 	    $text=$$option[1];
  734: 	    $option=$$option[0];
  735: 	} else {
  736: 	    $text=$option;
  737: 	    $value='value="'.&HTML::Entities::encode($option,'\'"&<>').'"';
  738: 	}
  739: 	if ( $selected eq $option ) {
  740: 	    $optionlist.="<option $value selected=\"selected\">$text</option>\n";
  741: 	    $found=1;
  742: 	} else {
  743: 	    $optionlist.="<option $value>$text</option>\n";
  744: 	}
  745:     }
  746:     $optionlist.="<option value=\"TYPEDINVALUE\"".
  747:  	((!$found)?' selected="selected"':'').
  748:  	">".&mt('Type-in value')."</option>\n";
  749: #
  750:     my $change_code=&element_change_detection();
  751:     my $element=&html_element_name($name);
  752:     my $selectelement='select_list_'.$element;
  753:     my $typeinelement='type_in_'.$element;
  754:     my $typeinvalue=($found?'':$selected);
  755: #
  756:     my $hiddenvalue='this.form.'.$element.'.value';
  757:     my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
  758:     my $selectedvalue='this.form.'.$selectelement.
  759: 	     '.options['.$selectedindex.'].value';
  760:     my $typedinvalue='this.form.'.$typeinelement.'.value';
  761:     my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
  762:     $description=&mt($description);
  763: #
  764:     return (<<ENDSELECTORTYPE);
  765: <nobr>
  766: $description
  767: &nbsp;<select name="$selectelement"
  768: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }" >
  769: $optionlist
  770: </select>
  771: <input type="text" size="$size" name="$typeinelement"
  772:        value="$typeinvalue" 
  773: onChange="$hiddenvalue=$typedinvalue;"
  774: onFocus="$selectedindex=$selecttypeinindex-1;" />
  775: <input type="hidden" name="$element" value="$selected" $change_code />
  776: </nobr>
  777: ENDSELECTORTYPE
  778: }
  779: 
  780: #----------------------------------------------------- image coordinates
  781: # single image coordinates, x, y 
  782: sub entercoords {
  783:     my ($idx,$idy,$mode,$width,$height) = @_;
  784:     unless ($Apache::edit::bgimgsrc) { return ''; }
  785:     if ($idx) { $idx.='_'; }
  786:     if ($idy) { $idy.='_'; }
  787:     my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
  788:     my $form    = 'lonhomework';
  789:     my $element;
  790:     if (! defined($mode) || $mode eq 'attribute') {
  791:         $element = &escape("$Apache::lonxml::curdepth");
  792:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  793:         $element = &escape('homework_edit_'.
  794:                                            $Apache::lonxml::curdepth);
  795:     }
  796:     my $id=$Apache::lonxml::curdepth;
  797:     my %data=("imagechoice.$id.type"      =>'point',
  798: 	      "imagechoice.$id.formname"  =>$form,
  799: 	      "imagechoice.$id.formx"     =>"$idx$element",
  800: 	      "imagechoice.$id.formy"     =>"$idy$element",
  801: 	      "imagechoice.$id.file"      =>$bgfile,
  802: 	      "imagechoice.$id.formcoord" =>$element);
  803:     if ($height) {
  804: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
  805: 	    $Apache::edit::bgimgsrccurdepth;
  806:     }
  807:     if ($width) {
  808: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
  809: 	    $Apache::edit::bgimgsrccurdepth;
  810:     }
  811:     &Apache::lonnet::appenv(%data);
  812:     my $text="Click Coordinates";
  813:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
  814:     return $result;
  815: }
  816: 
  817: # coordinates (x1,y1)-(x2,y2)...
  818: # mode can be either box, or polygon
  819: sub entercoord {
  820:     my ($idx,$mode,$width,$height,$type) = @_;
  821:     unless ($Apache::edit::bgimgsrc) { return ''; }
  822:     my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
  823:     my $form    = 'lonhomework';
  824:     my $element;
  825:     if (! defined($mode) || $mode eq 'attribute') {
  826:         $element = &escape("$idx\_$Apache::lonxml::curdepth");
  827:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  828:         $element = &escape('homework_edit_'.
  829:                                            $Apache::lonxml::curdepth);
  830:     }
  831:     my $id=$Apache::lonxml::curdepth;
  832:     my %data=("imagechoice.$id.type"      =>$type,
  833: 	      "imagechoice.$id.formname"  =>$form,
  834: 	      "imagechoice.$id.file"      =>$bgfile,
  835: 	      "imagechoice.$id.formcoord" =>$element);
  836:     if ($height) {
  837: 	$data{"imagechoice.$id.formheight"}=$height.'_'.
  838: 	    $Apache::edit::bgimgsrccurdepth;
  839:     }
  840:     if ($width) {
  841: 	$data{"imagechoice.$id.formwidth"}=$width.'_'.
  842: 	    $Apache::edit::bgimgsrccurdepth;
  843:     }
  844:     &Apache::lonnet::appenv(%data);
  845:     my $text="Enter Coordinates";
  846:     if ($type eq 'polygon') { $text='Create Polygon Data'; }
  847:     my $result='<a href="/adm/imagechoice?token='.$id.'" target="imagechoice">'.$text.'</a>';
  848:     return $result;
  849: }
  850: 
  851: sub deletecoorddata {
  852:     &Apache::lonnet::delenv("imagechoice\\.");
  853: }
  854: 
  855: #----------------------------------------------------- browse
  856: sub browse {
  857:     # insert a link to call up the filesystem browser (lonindexer)
  858:     my ($id, $mode, $titleid) = @_;
  859:     my $form    = 'lonhomework';
  860:     my $element;
  861:     if (! defined($mode) || $mode eq 'attribute') {
  862:         $element = &escape("$id\_$Apache::lonxml::curdepth");
  863:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  864:         $element = &escape('homework_edit_'.
  865:                                            $Apache::lonxml::curdepth);	
  866:     }
  867:     my $titleelement;
  868:     if ($titleid) {
  869: 	$titleelement=",'','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
  870:     }
  871:     my $result = <<"ENDBUTTON";
  872: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
  873: ENDBUTTON
  874:     return $result;
  875: }
  876: 
  877: #----------------------------------------------------- browse
  878: sub search {
  879:     # insert a link to call up the filesystem browser (lonindexer)
  880:     my ($id, $mode, $titleid) = @_;
  881:     my $form    = 'lonhomework';
  882:     my $element;
  883:     if (! defined($mode) || $mode eq 'attribute') {
  884:         $element = &escape("$id\_$Apache::lonxml::curdepth");
  885:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  886:         $element = &escape('homework_edit_'.
  887:                                            $Apache::lonxml::curdepth);
  888:     }
  889:     my $titleelement;
  890:     if ($titleid) {
  891: 	$titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
  892:     }
  893:     my $result = <<"ENDBUTTON";
  894: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
  895: ENDBUTTON
  896:     return $result;
  897: }
  898: 
  899: 
  900: 1;
  901: __END__
  902: 
  903: =head1 NAME
  904: 
  905: Apache::edit - edit mode helpers
  906: 
  907: =head1 SYNOPSIS
  908: 
  909: Invoked by many homework and xml related modules.
  910: 
  911:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
  912: 
  913: =head1 INTRODUCTION
  914: 
  915: This module outputs HTML syntax helpful for the rendering of edit
  916: mode interfaces.
  917: 
  918: This is part of the LearningOnline Network with CAPA project
  919: described at http://www.lon-capa.org.
  920: 
  921: =head1 HANDLER SUBROUTINE
  922: 
  923: There is no handler subroutine.
  924: 
  925: =head1 OTHER SUBROUTINES
  926: 
  927: =over 4
  928: 
  929: =item *
  930: 
  931: initialize_edit() : initialize edit (set colordepth to zero)
  932: 
  933: =item *
  934: 
  935: tag_start($target,$token,$description) : provide deletion and insertion lists
  936: for the manipulation of a start tag; return a scalar string
  937: 
  938: =item *
  939: 
  940: tag_end($target,$token,$description) : ending syntax corresponding to
  941: &tag_start. return a scalar string.
  942: 
  943: =item *
  944: 
  945: start_table($token) : start table; update colordepth; return scalar string.
  946: 
  947: =item *
  948: 
  949: end_table() : reduce color depth; end table; return scalar string
  950: 
  951: =item *
  952: 
  953: start_spanning_row() : start a new table row spanning the 'edit' environment.
  954: 
  955: =item *
  956: 
  957: start_row() : start a new table row and element. 
  958: 
  959: =item *
  960: 
  961: end_row() : end current table element and row.
  962: 
  963: =item *
  964: 
  965: movebuttons($target,$token) : move-up and move-down buttons; return scalar
  966: string
  967: 
  968: =item *
  969: 
  970: deletelist($target,$token) : provide a yes option in an HTML select element;
  971: return scalar string
  972: 
  973: =item *
  974: 
  975: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
  976: $style) : respond to a user delete request by passing relevant stack
  977: and array information to various rendering functions; return a scalar string
  978: 
  979: =item *
  980: 
  981: get_insert_list($token) : provide an insertion list based on possibilities
  982: from lonxml; return a scalar string
  983: 
  984: =item *
  985: 
  986: insertlist($target,$token) : api that uses get_insert_list;
  987: return a scalar string
  988: 
  989: =item *
  990: 
  991: handleinsert($token) : provide an insertion list based on possibilities
  992: from lonxml; return a scalar string
  993: 
  994: =item *
  995: 
  996: get_insert_list($token) : provide an insertion list based on possibilities
  997: from lonxml; return a scalar string
  998: 
  999: =item *
 1000: browse($elementname) : provide a link which will open up the filesystem
 1001: browser (lonindexer) and, once a file is selected, place the result in
 1002: the form element $elementname.
 1003: 
 1004: =item *
 1005: search($elementname) : provide a link which will open up the filesystem
 1006: searcher (lonsearchcat) and, once a file is selected, place the result in
 1007: the form element $elementname.
 1008: 
 1009: =item *
 1010: editline(tag,data,description,size): Provide a <input type="text" ../> for
 1011: single-line text entry.  This is to be used for text enclosed by tags, not
 1012: arguements/parameters associated with a tag.
 1013: 
 1014: =back
 1015: 
 1016: incomplete...
 1017: 
 1018: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>