File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.119: download - view: text, annotated - select for diffs
Wed Dec 10 21:02:01 2008 UTC (15 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Replace <nobr> with <span class="LC_nobreak">

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

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