File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.54: download - view: text, annotated - select for diffs
Fri Apr 23 22:34:15 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
The 0th step toward bug #883.

    1: # The LearningOnline Network with CAPA
    2: # Edit Handler for RAT Maps
    3: #
    4: # $Id: lonratedt.pm,v 1.54 2004/04/23 22:34:15 www 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: # (TeX Content Handler
   29: #
   30: # 05/29/00,05/30 Gerd Kortemeyer)
   31: # 7/1,6/30 Gerd Kortemeyer
   32: 
   33: package Apache::lonratedt;
   34: 
   35: =pod
   36: 
   37: =head1 NAME
   38: 
   39: Apache::lonratedt: advanced resource assembly tool
   40: 
   41: =head1 SYNOPSIS
   42: 
   43: lonratedt provides the routines and the handler for the Advanced
   44:     Resource Assembly Tool (RAT), and ties the various pieces together
   45:     with Javascript.
   46: 
   47: =head1 OVERVIEW
   48: 
   49: =head2 Map Representation
   50: 
   51: =begin latex
   52: 
   53: %
   54: \begin{figure}
   55: \begin{center}\includegraphics[%
   56:   width=0.55\paperwidth,bb = 0 0 200 100, draft, type=eps]{Map_Example}\end{center}
   57: 
   58: 
   59: \caption{\label{Map_In_Advanced_Editor}Example of a Map in the Advanced Editor}
   60: \end{figure}
   61: %
   62: \begin{figure}
   63: \begin{lyxcode}
   64: <map>
   65: 
   66: ~~<resource~id=\char`\"{}1\char`\"{}
   67: 
   68: ~~~~src=\char`\"{}/res/msu/korte/phy231welcome.html\char`\"{}
   69: 
   70: ~~~~type=\char`\"{}start\char`\"{}
   71: 
   72: ~~~~title=\char`\"{}Start\char`\"{}>
   73: 
   74: ~~~~</resource>
   75: 
   76: ~~<resource~id=\char`\"{}2\char`\"{}
   77: 
   78: ~~~~src=\char`\"{}\char`\"{}~type=\char`\"{}finish\char`\"{}
   79: 
   80: ~~~~title=\char`\"{}Finish\char`\"{}>
   81: 
   82: ~~~~</resource>
   83: 
   84: ~~<resource~id=\char`\"{}6\char`\"{}
   85: 
   86: ~~~~src=\char`\"{}/res/msu/korte/tests/units.problem\char`\"{}
   87: 
   88: ~~~~type=\char`\"{}mandatory\char`\"{}
   89: 
   90: ~~~~title=\char`\"{}Physical~Units~Test\char`\"{}>
   91: 
   92: ~~~~</resource>
   93: 
   94: ~~<resource~id=\char`\"{}9\char`\"{}
   95: 
   96: ~~~~src=\char`\"{}/res/msu/korte/chapters/onedim.sequence\char`\"{}
   97: 
   98: ~~~~title=\char`\"{}Motion~in~One~Dimension\char`\"{}>
   99: 
  100: ~~~~</resource>
  101: 
  102: ~~<resource~id=\char`\"{}11\char`\"{}
  103: 
  104: ~~~~src=\char`\"{}/res/msu/bauer/bridges/units.sequence\char`\"{}
  105: 
  106: ~~~~title=\char`\"{}Physical~Units~Refresher\char`\"{}>
  107: 
  108: ~~~~</resource>
  109: 
  110: ~~<condition~id=\char`\"{}19\char`\"{}
  111: 
  112: ~~~~type=\char`\"{}stop\char`\"{}
  113: 
  114: ~~~~value=\char`\"{}user.assessments{[}this./res/msu/korte/tests/units.problem{]}.status=solved\char`\"{}>
  115: 
  116: ~~~~</condition>
  117: 
  118: ~~<link~from=\char`\"{}1\char`\"{}~to=\char`\"{}6\char`\"{}></link>
  119: 
  120: ~~<link~from=\char`\"{}6\char`\"{}~to=\char`\"{}9\char`\"{}~condition=\char`\"{}19\char`\"{}></link>
  121: 
  122: ~~<link~from=\char`\"{}6\char`\"{}~to=\char`\"{}11\char`\"{}></link>
  123: 
  124: ~~<link~from=\char`\"{}11\char`\"{}~to=\char`\"{}6\char`\"{}></link>
  125: 
  126: ~~</map>
  127: \end{lyxcode}
  128: 
  129: \caption{\label{XML}XML for Map in Figure \ref{Map_In_Advanced_Editor}}
  130: \end{figure}
  131: 
  132: =end latex
  133: 
  134: Fig. "XML for Map in Figure" shows the XML representation of the
  135: resource map shown in Fig. "Example of a Map in the Advanced Editor",
  136: which is the format in which maps are stored. In the figure, however,
  137: additional graphical map layout information generated by the Advanced
  138: Resource Assembly Tool is not displayed. This graphical information is
  139: optional to re-generate the same graphical layout when the map is
  140: brought up again in the Resource Assembly Tool, and is not needed for
  141: any other system functionality.
  142: 
  143: Maps can be generated by tools other than the Resource Assembly
  144: Tool. In particular, an author might have some other representation of
  145: a course sequence, which can be converted into a map using scripts. If
  146: this map then were to be brought up in the Resource Assembly Tool, the
  147: Tool would automatically generate a graphical layout for it. Each
  148: entry of the map (resources, conditions and links) is stored in a
  149: separate tag.
  150: 
  151: Resources and conditionsX<conditions> have to have unique ID
  152: numbers. These numbers are automatically generated by the Resource
  153: Assembly Tool when the entry is first created, or added to the entries
  154: when a map generated outside the Resource Assembly Tool is first
  155: retrieved. They can also be assigned by custom scripts or added in by
  156: hand.
  157: 
  158: In the XML example, entry 1 is the start resource of the map. When
  159: this map is accessed, the source (src) URL of this tag will be the
  160: first resource rendered. Entry 2 is the finish resource of this
  161: map. This resource will be the last resource in the sequence of
  162: resources. Entry 6 is a problem resource with the given URL and title,
  163: as well as the priority "mandatory". Entry 19 is a condition, which is
  164: used by the link between entries 6, the problem, and 9, a
  165: sequence. I<The final syntax for conditions has not yet been
  166: determined.>
  167: 
  168: =cut
  169: 
  170: use strict;
  171: use Apache::Constants qw(:common);
  172: use Apache::lonnet;
  173: use Apache::lonratsrv;
  174: use Apache::lonsequence;
  175: use Apache::loncommon;
  176: use Apache::lonlocal;
  177: use File::Copy;
  178: 
  179: use vars qw(@order @resources @resparms);
  180: 
  181: 
  182: # Mapread read maps into global arrays @links and @resources, determines status
  183: # sets @order - pointer to resources in right order
  184: # sets @resources - array with the resources with correct idx
  185: #
  186: sub mapread {
  187:     my $fn=shift;
  188: 
  189:     my @links;
  190:     undef @links;
  191:     undef @resources;
  192:     undef @order;
  193:     @resources=('');
  194:     @order=();
  195: 
  196:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
  197:     if ($errtext) { return ($errtext,2); }
  198: 
  199: # -------------------------------------------------------------------- Read map
  200:     foreach (split(/\<\&\>/,$outtext)) {
  201: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
  202:         if ($command eq 'objcont') {
  203: 	    $resources[$number]=$content;
  204:         }
  205:         if ($command eq 'objlinks') {
  206:             $links[$number]=$content;
  207:         }
  208:         if ($command eq 'objparms') {
  209: 	    $resparms[$number]=$content;
  210:         }
  211:     }
  212: # ------------------------------------------------------- Is this a linear map?
  213:     my @starters=();
  214:     my @endings=();
  215:     undef @starters;
  216:     undef @endings;
  217: 
  218:     foreach (@links) {
  219:         if (defined($_)) {
  220: 	    my ($start,$end,$cond)=split(/\:/,$_);
  221:             if ((defined($starters[$start])) || (defined($endings[$end]))) { 
  222: 		return
  223:                  (&mt('Map has branchings. Use advanced editor.'),1);
  224:             }
  225: 	    $starters[$start]=1;
  226: 	    $endings[$end]=1;
  227: 	    if ($cond) {
  228: 		return
  229:                  (&mt('Map has conditions. Use advanced editor.'),1);
  230:             }
  231: 	}
  232: 
  233:     }
  234:     for (my $i=1; $i<=$#resources; $i++) {
  235:         if (defined($resources[$i])) {
  236: 	    unless (($starters[$i]) || ($endings[$i])) {
  237:                 return
  238: 		 (&mt('Map has unconnected resources. Use advanced editor.'),1);
  239:             }
  240:         }
  241:     }
  242: # ---------------------------------------------- Did we just read an empty map?
  243:     if ($#resources<1) {
  244:         undef $resources[0];
  245: 	$resources[1]=':::start';
  246:         $resources[2]=':::finish';
  247:     }
  248: # -------------------------------------------------- This is a linear map, sort
  249: 
  250:     my $startidx=0;
  251:     my $endidx=0;
  252:     for (my $i=0; $i<=$#resources; $i++) {
  253:         if (defined($resources[$i])) {
  254:             my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
  255: 	    if ($type eq 'start') { $startidx=$i; }
  256:             if ($type eq 'finish') { $endidx=$i; }
  257:         }
  258:     }
  259:     my $k=0;
  260:     my $currentidx=$startidx;
  261:     $order[$k]=$currentidx;
  262:     for (my $i=0; $i<=$#resources; $i++) {
  263:         foreach (@links) {
  264: 	    my ($start,$end)=split(/\:/,$_);
  265:             if ($start==$currentidx) {
  266: 		$currentidx=$end;
  267:                 $k++;
  268:                 $order[$k]=$currentidx;
  269:                 last;
  270:             }
  271:         }
  272:         if ($currentidx==$endidx) { last; }
  273:     }
  274:     return $errtext;
  275: }
  276: 
  277: # ---------------------------------------------- Read a map as well as possible
  278: # Also used by the sequence handler
  279: # Call lonsequence::attemptread to read from resource space
  280: #
  281: sub attemptread {
  282:     my $fn=shift;
  283: 
  284:     my @links;
  285:     undef @links;
  286:     my @theseres;
  287:     undef @theseres;
  288: 
  289:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
  290:     if ($errtext) { return @theseres }
  291: 
  292: # -------------------------------------------------------------------- Read map
  293:     foreach (split(/\<\&\>/,$outtext)) {
  294: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
  295:         if ($command eq 'objcont') {
  296: 	    $theseres[$number]=$content;
  297:         }
  298:         if ($command eq 'objlinks') {
  299:             $links[$number]=$content;
  300:         }
  301:     }
  302: 
  303: # --------------------------------------------------------------- Sort, sort of
  304: 
  305:     my @objsort=();
  306:     undef @objsort;
  307: 
  308:     my @data1=();
  309:     my @data2=();
  310:     undef @data1;
  311:     undef @data2;
  312: 
  313:     my $k;
  314:     my $kj;
  315:     my $j;
  316:     my $ij;
  317: 
  318:    for ($k=1;$k<=$#theseres;$k++) {
  319:       if (defined($theseres[$k])) {
  320:          $objsort[$#objsort+1]=$k;
  321:       }
  322:    }
  323: 
  324:    for ($k=1;$k<=$#links;$k++) {
  325:      if (defined($links[$k])) {
  326:       @data1=split(/\:/,$links[$k]);
  327:       $kj=-1;
  328:       for (my $j=0;$j<=$#objsort;$j++) {
  329:          if ((split(/\:/,$objsort[$j]))[0]==$data1[0]) {
  330:             $kj=$j;
  331:          }
  332:       }
  333:       if ($kj!=-1) { $objsort[$kj].=':'.$data1[1]; }
  334:      }
  335:    }
  336:     for ($k=0;$k<=$#objsort;$k++) {
  337:       for ($j=0;$j<=$#objsort;$j++) {
  338:         if ($k!=$j) {
  339:           @data1=split(/\:/,$objsort[$k]);
  340:           @data2=split(/\:/,$objsort[$j]);
  341:           my $dol=$#data1+1;
  342:           my $dtl=$#data2+1;
  343:           if ($dol+$dtl<1000) {
  344:            for ($kj=1;$kj<$dol;$kj++) {
  345:              if ($data1[$kj]==$data2[0]) {
  346:                 for ($ij=1;$ij<$dtl;$ij++) {
  347:                    $data1[$#data1+1]=$data2[$ij];
  348:                 }
  349:              }
  350:            }
  351:            for ($kj=1;$kj<$dtl;$kj++) {
  352:              if ($data2[$kj]==$data1[0]) {
  353:                  for ($ij=1;$ij<$dol;$ij++) {
  354:                     $data2[$#data2+1]=$data1[$ij];
  355:                  }
  356:              }
  357:            }
  358:            $objsort[$k]=join(':',@data1);
  359:            $objsort[$j]=join(':',@data2);
  360:           }
  361:          }
  362:       } 
  363:   }
  364: # ---------------------------------------------------------------- Now sort out
  365: 
  366:     @objsort=sort {
  367:       my @data1=split(/\:/,$a);
  368:       my @data2=split(/\:/,$b);
  369:       my $rvalue=0;
  370:       my $k;
  371:       for ($k=1;$k<=$#data1;$k++) {
  372:          if ($data1[$k]==$data2[0]) { $rvalue--; }
  373:       }
  374:       for ($k=1;$k<=$#data2;$k++) {
  375:          if ($data2[$k]==$data1[0]) { $rvalue++; }
  376:       }
  377:       if ($rvalue==0) { $rvalue=$#data2-$#data1; }
  378:       $rvalue;
  379:     } @objsort;
  380: 
  381:     my @outres=();
  382:     undef @outres;
  383: 
  384:     for ($k=0;$k<=$#objsort;$k++) {
  385: 	$outres[$k]=$theseres[(split(/\:/,$objsort[$k]))[0]];
  386:     }
  387:     return @outres;
  388: }
  389: 
  390: # --------------------------------------------------------- Build up RAT screen
  391: sub ratedt {
  392:   my ($r,$url)=@_;
  393:   $r->print(<<ENDDOCUMENT);
  394: 
  395: <html>
  396: <head>
  397: <script language="JavaScript">
  398:     var flag=0;
  399: </script>
  400: </head>
  401: <frameset rows="1,50,*" border=0>
  402: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
  403: <frame name=code src="/adm/rat/code.html">
  404: <frame name=mapout src="/adm/rat/map.html">
  405: </frameset>
  406: </html>
  407: 
  408: ENDDOCUMENT
  409: }
  410: 
  411: # ---------------------------------------------------------------- Make buttons
  412: 
  413: sub buttons {
  414:     my $adv=shift;
  415:     my $output='<form method=post>';     
  416:     if ($adv==1) {
  417: 	$output.='<input type=submit name=forceadv value="'.&mt('Edit').'">'.
  418: 	    &Apache::loncommon::help_open_topic('Sequence_Advanced_Editor_Creation');;
  419:     } else {
  420:         unless ($adv==2) {
  421:            $output.='<input type=submit name=forcesmp value="'.&mt('Simple Edit').'">'.
  422: 	    &Apache::loncommon::help_open_topic('Sequence_Simple_Editor_Creation');
  423:         }
  424: 	$output.='<input type=submit name=forceadv value="'.&mt('Advanced Edit').'">'.
  425: 	    &Apache::loncommon::help_open_topic('Sequence_Advanced_Editor_Creation');
  426:     }
  427:     return $output.'</form><hr>';
  428: }
  429: 
  430: # ----------------------------------------------------------- Paste into target
  431: # modifies @order, @resources
  432: 
  433: sub pastetarget {
  434:     my ($after,@which)=@_;
  435:     my @insertorder=();
  436:     foreach (@which) {
  437:         if (defined($_)) {
  438: 	    my ($name,$url)=split(/\=/,$_);
  439:             $name=&Apache::lonnet::unescape($name);
  440:             $url=&Apache::lonnet::unescape($url);
  441:             if ($url) {
  442: 	       my $idx=$#resources+1;
  443:                $insertorder[$#insertorder+1]=$idx;
  444:                my $ext='false';
  445:                if ($url=~/^http\:\/\//) { $ext='true'; }
  446:                $url=~s/\:/\&colon;/g;
  447:                $name=~s/\:/\&colon;/g;
  448:                $resources[$idx]=$name.':'.$url.':'.$ext.':normal:res';
  449: 	   }
  450:         }
  451:     }
  452:     my @oldorder=splice(@order,$after);
  453:     @order=(@order,@insertorder,@oldorder);
  454: }
  455: 
  456: # ------------------------------------------------ Get start and finish correct
  457: # modifies @resources
  458: 
  459: sub startfinish {
  460: # Remove all start and finish
  461:     foreach (@order) {
  462: 	my ($name,$url,$ext)=split(/\:/,$resources[$_]);
  463:         if ($url=~/http\&colon\:\/\//) { $ext='true'; }
  464:         $resources[$_]=$name.':'.$url.':'.$ext.':normal:res';
  465:     }
  466: # Garbage collection
  467:     my $stillchange=1;
  468:      while (($#order>1) && ($stillchange)) {
  469:        $stillchange=0;
  470:        for (my $i=0;$i<=$#order;$i++) {
  471:  	  my ($name,$url,$ext)=split(/\:/,$resources[$order[$i]]);
  472:           unless ($url) {
  473: # Take out empty resource
  474:               for (my $j=$i+1;$j<=$#order;$j++) {
  475:                   $order[$j-1]=$order[$j];
  476: 	      }
  477:               $#order--;
  478: 	      $stillchange=1;
  479:               last;
  480:           }
  481:        }
  482:     }
  483: # Put in a start resource
  484:    my ($name,$url,$ext)=split(/\:/,$resources[$order[0]]);
  485:    $resources[$order[0]]=$name.':'.$url.':'.$ext.':start:res';
  486: # Make sure this has at least start and finish
  487:    if ($#order==0) {
  488:        $resources[$#resources+1]='::false';
  489:        $order[1]=$#resources;
  490:    }
  491: # Make the last one a finish resource
  492:    ($name,$url,$ext)=split(/\:/,$resources[$order[$#order]]);
  493:    $resources[$order[$#order]]=$name.':'.$url.':'.$ext.':finish:res';
  494: }
  495: 
  496: # ------------------------------------------------------------------- Store map
  497: 
  498: sub storemap {
  499:     my $realfn=shift;
  500:     my $fn=$realfn;
  501: # unless this is forced to work from the original file, use a temporary file
  502: # instead
  503:     unless (shift) {
  504:        $fn=$realfn.'.tmp';
  505:        unless (-e $fn) {
  506: 	  copy($realfn,$fn);
  507:        }
  508:     }
  509: # store data either into tmp or real file
  510:     &startfinish();
  511:     my $output='graphdef<:>no';
  512:     my $k=1;
  513:     for (my $i=0; $i<=$#order; $i++) {
  514:         if (defined($resources[$order[$i]])) {
  515: 	    $output.='<&>objcont<:>'.$order[$i].'<:>'.$resources[$order[$i]];
  516:         }
  517: 	if (defined($resparms[$order[$i]])) {
  518: 	    $output.='<&>objparms<:>'.$order[$i].'<:>'.$resparms[$order[$i]];
  519: 	}
  520:         if (defined($order[$i+1])) {
  521: 	    if (defined($resources[$order[$i+1]])) {
  522:                $output.='<&>objlinks<:>'.$k.'<:>'.
  523: 		   $order[$i].':'.$order[$i+1].':0';
  524: 	       $k++;
  525:             }
  526:         }
  527:     }
  528:     $output=~s/http\&colon\;\/\///g;
  529:     $ENV{'form.output'}=$output;
  530:     return 
  531:      &Apache::lonratsrv::loadmap($fn,&Apache::lonratsrv::savemap($fn,''));
  532: }
  533: 
  534: sub editscript {
  535:     my $mode=shift;
  536:     my $resurl=&Apache::loncommon::lastresurl();
  537:     return(<<ENDSCRIPT);
  538: var srch;
  539: var srchflag=-1; // 1 means currently open
  540:                  // 0 means closed (but has been open)
  541:                  // -1 means never yet opened/defined
  542: var srchmode='';
  543: 
  544: var idx;
  545: var idxflag=-1; // 1 means currently open
  546:                  // 0 means closed (but has been open)
  547:                  // -1 means never yet opened/defined
  548: var idxmode='';
  549: 
  550: // ------------------------------------------------------ Clears indexer window
  551: function idxclear() {
  552:   idx.document.clear();
  553: }
  554: 
  555: // ------------------------------------------------------- Clears search window
  556: function srchclear() {
  557:   srch.document.clear();
  558: }
  559: 
  560: // ------------------------------------------------------ Closes indexer window
  561: function idxclose() {
  562:   if (idx && !idx.closed) {
  563:     idxflag=0;
  564:     idx.close();
  565:   }
  566: }
  567: 
  568: // ------------------------------------------------------- Closes search window
  569: function srchclose() {
  570:   if (srch && !srch.closed) {
  571:     srchflag=0;
  572:     srch.close();
  573:   }
  574: }
  575: 
  576: // -------------------------------------------------------- Open indexer window
  577: function idxopen(mode) {
  578:    var options="scrollbars=1,resizable=1,menubar=0";
  579:    idxmode=mode;
  580:    idxflag=1;
  581:    idx=open("$resurl/?launch=1&mode=$mode&catalogmode="+mode,"idxout",options);
  582:    idx.focus();
  583: }
  584: 
  585: // --------------------------------------------------------- Open search window
  586: function srchopen(mode) {
  587:    var options="scrollbars=1,resizable=1,menubar=0";
  588:    srchmode=mode;
  589:    srchflag=1;
  590:    srch=open("/adm/searchcat?launch=1&mode=$mode&catalogmode="+mode,"srchout",options);
  591:    srch.focus();
  592: }
  593: // ----------------------------------------------------- launch indexer browser
  594: function groupsearch() {
  595:    srchcheck('groupsearch');
  596: }
  597: 
  598: function groupimport() {
  599:    idxcheck('groupimport');
  600: }
  601: // ------------------------------------------------------- Do srch status check
  602: function srchcheck(mode) {
  603:    if (!srch || srch.closed || srchmode!=mode) {
  604:       srchopen(mode);
  605:    }
  606:    srch.focus();
  607: }
  608: 
  609: // -------------------------------------------------------- Do idx status check
  610: function idxcheck(mode) {
  611:    if (!idx || idx.closed || idxmode!=mode) {
  612:       idxopen(mode);
  613:    }
  614:    idx.focus();
  615: }
  616: 
  617: 
  618:     var editbrowser;
  619:     function openbrowser(formname,elementname,only,omit) {
  620:         var url = '$resurl/?';
  621:         if (editbrowser == null) {
  622:             url += 'launch=1&';
  623:         }
  624:         url += 'catalogmode=interactive&';
  625:         url += 'mode=edit&';
  626:         url += 'form=' + formname + '&';
  627:         if (only != null) {
  628:             url += 'only=' + only + '&';
  629:         } 
  630:         if (omit != null) {
  631:             url += 'omit=' + omit + '&';
  632:         }
  633:         url += 'element=' + elementname + '';
  634:         var title = 'Browser';
  635:         var options = 'scrollbars=1,resizable=1,menubar=0';
  636:         options += ',width=700,height=600';
  637:         editbrowser = open(url,title,options,'1');
  638:         editbrowser.focus();
  639:     }
  640: ENDSCRIPT
  641: }
  642: # ------------------------------------------------------- Simple edit processor
  643: 
  644: sub smpedt {
  645:    my ($r,$url,$errtext)=@_;
  646:    my $buttons=&buttons(2);
  647:    my $tmpfn=&Apache::lonnet::filelocation('',$url).'.tmp';
  648:    my $targetmsg='';
  649:    if ($ENV{'form.save'}) {
  650:        $targetmsg='<b>Saving ...</b><br>';
  651:        copy($tmpfn,&Apache::lonnet::filelocation('',$url));
  652:        unlink($tmpfn);
  653:        my ($errtext,$fatal)=
  654:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  655:    }
  656:    if ($ENV{'form.revert'}) {
  657:        $targetmsg='<b>Reverting ...</b><br>';
  658:        unlink($tmpfn);
  659:        my ($errtext,$fatal)=
  660:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  661:    }
  662:    if (-e $tmpfn) {
  663:       $targetmsg=
  664:         '<b><font color="red">'.&mt('You are working with an unsaved version of your map.').'</font></b><br>';
  665:       my ($errtext,$fatal)=&mapread($tmpfn,'');
  666:    }
  667: # ---------------------------------------------------------- Process form input
  668: 
  669:    my @importselect=();
  670:    my @targetselect=();
  671:    undef @importselect;
  672:    undef @targetselect;
  673:    if (defined($ENV{'form.importsel'})) {
  674:        if (ref($ENV{'form.importsel'})) {
  675: 	   @importselect=sort(@{$ENV{'form.importsel'}});
  676:        } else {
  677:            @importselect=($ENV{'form.importsel'});
  678:        }
  679:    }
  680:    if (defined($ENV{'form.target'})) {
  681:        if (ref($ENV{'form.target'})) {
  682: 	   @targetselect=sort(@{$ENV{'form.target'}});
  683:        } else {
  684:            @targetselect=($ENV{'form.target'});
  685:        }
  686:    }
  687: # ============================================================ Process commands
  688: 
  689:    my $targetdetail=$ENV{'form.targetdetail'};
  690:    my $importdetail=$ENV{'form.curimpdetail'};
  691: 
  692: # ---------------------------------------------------- Importing from groupsort
  693:    if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
  694: 
  695:        $importdetail='';
  696:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  697: 
  698:        my $lastsel;
  699: 
  700:        if (defined($importselect[-1])) {
  701: 	   $lastsel=$importselect[-1];
  702:        } else {
  703:            $lastsel=$#curimport;
  704:        }
  705: 
  706:        for (my $i=0;$i<=$lastsel;$i++) {
  707:            my ($name,$url)=split(/\=/,$curimport[$i]);
  708:            if ($url) {
  709:               $importdetail.='&'.$name.'='.$url;
  710: 	   }
  711:        }
  712: 
  713:       $importdetail.='&'.$ENV{'form.importdetail'};
  714: 
  715:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  716:            my ($name,$url)=split(/\=/,$curimport[$i]);
  717:            if ($url) {
  718:               $importdetail.='&'.$name.'='.$url;
  719: 	  }
  720:        }
  721:        $importdetail=~s/\&+/\&/g;
  722:        $importdetail=~s/^\&//;
  723: 
  724: # ------------------------------------------------------------------- Clear all
  725:    } elsif ($ENV{'form.clear'}) {
  726:        $importdetail='';
  727: # ------------------------------------------------------------ Discard selected
  728:    } elsif ($ENV{'form.discard'}) {
  729:        $importdetail='';
  730:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  731:        foreach (@importselect) {
  732: 	   $curimport[$_]='';
  733:        }
  734:        for (my $i=0;$i<=$#curimport;$i++) {
  735:            my ($name,$url)=split(/\=/,$curimport[$i]);
  736:            if ($url) {
  737:               $importdetail.='&'.$name.'='.$url;
  738: 	   }
  739:        }
  740: # --------------------------------------------------------- Loading another map
  741:    } elsif ($ENV{'form.loadmap'}) {
  742:        $importdetail='';
  743:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  744: 
  745:        my $lastsel;
  746: 
  747:        if (defined($importselect[-1])) {
  748: 	   $lastsel=$importselect[-1];
  749:        } else {
  750:            $lastsel=$#curimport;
  751:        }
  752: 
  753:        for (my $i=0;$i<=$lastsel;$i++) {
  754:            my ($name,$url)=split(/\=/,$curimport[$i]);
  755:            if ($url) {
  756:               $importdetail.='&'.$name.'='.$url;
  757: 	   }
  758:        }
  759: 
  760:        foreach (
  761:     &Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
  762: 	   my ($name,$url)=split(/\:/,$_);
  763:            if ($url) {
  764:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  765: 		 	         &Apache::lonnet::escape($url);
  766: 	  }
  767:        }
  768: 
  769:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  770:            my ($name,$url)=split(/\=/,$curimport[$i]);
  771:            if ($url) {
  772:               $importdetail.='&'.$name.'='.$url;
  773: 	  }
  774:        }
  775:        $importdetail=~s/\&+/\&/g;
  776:        $importdetail=~s/^\&//;
  777: 
  778: # ------------------------------------------------ Groupimport/search to target
  779:    } elsif ($ENV{'form.importdetail'}) {
  780:        my $lastsel;
  781:        if (defined($targetselect[-1])) {
  782: 	   $lastsel=$targetselect[-1];
  783:        } else {
  784:            $lastsel=$#order+1;
  785:        }
  786:        &pastetarget($lastsel,split(/\&/,$ENV{'form.importdetail'}));
  787:        &storemap(&Apache::lonnet::filelocation('',$url));
  788: # ------------------------------------------------------------------------- Cut
  789:    } elsif (($ENV{'form.cut'}) || ($ENV{'form.copy'})) {
  790:        $importdetail='';
  791:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  792: 
  793:        my $lastsel;
  794: 
  795:        if (defined($importselect[-1])) {
  796: 	   $lastsel=$importselect[-1];
  797:        } else {
  798:            $lastsel=$#curimport;
  799:        }
  800: 
  801:        for (my $i=0;$i<=$lastsel;$i++) {
  802:            my ($name,$url)=split(/\=/,$curimport[$i]);
  803:            if ($url) {
  804:               $importdetail.='&'.$name.'='.$url;
  805: 	   }
  806:        }
  807: 
  808:        foreach (@targetselect) {
  809: 	   my ($name,$url)=split(/\:/,$resources[$order[$_-1]]);
  810:            if ($url) {
  811:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  812: 		 	         &Apache::lonnet::escape($url);
  813: 	  }
  814:        }
  815: 
  816:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  817:            my ($name,$url)=split(/\=/,$curimport[$i]);
  818:            if ($url) {
  819:               $importdetail.='&'.$name.'='.$url;
  820: 	  }
  821:        }
  822:        $importdetail=~s/\&+/\&/g;
  823:        $importdetail=~s/^\&//;
  824: 
  825:        if ($ENV{'form.cut'}) {
  826:            my @neworder=();
  827:            for (my $i=0;$i<=$#order;$i++) {
  828:                my $include=1;
  829:                foreach (@targetselect) {
  830: 		   if ($_-1==$i) { $include=0; }
  831:                }
  832:                if ($include) { $neworder[$#neworder+1]=$order[$i]; }
  833:            }
  834:            @order=@neworder;
  835:            &storemap(&Apache::lonnet::filelocation('',$url));      
  836:        }
  837: 
  838: # ----------------------------------------------------------------------- Paste
  839:    } elsif ($ENV{'form.paste'}) {
  840:        my $lastsel;
  841:        if (defined($targetselect[-1])) {
  842: 	   $lastsel=$targetselect[-1];
  843:        } else {
  844:            $lastsel=$#order+1;
  845:        }
  846:        my @newsequence;
  847:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  848:        foreach (@importselect) {
  849:           $newsequence[$#newsequence+1]=$curimport[$_];
  850:        }
  851:        &pastetarget($lastsel,@newsequence);
  852:        &storemap(&Apache::lonnet::filelocation('',$url));
  853: # -------------------------------------------------------------------- Move up 
  854:    } elsif ($ENV{'form.moveup'}) {
  855:        foreach (sort @targetselect) {
  856:            if ($_-1>0) {
  857:               my $movethis=$order[$_-1];
  858:               $order[$_-1]=$order[$_-2];
  859:               $order[$_-2]=$movethis;
  860: 	  }
  861:        }
  862:        &storemap(&Apache::lonnet::filelocation('',$url));
  863: # ------------------------------------------------------------------ Move down
  864:    } elsif ($ENV{'form.movedown'}) {
  865:        foreach (reverse sort @targetselect) {
  866:            if ($_-1<$#order) {
  867:               my $movethis=$order[$_-1];
  868:               $order[$_-1]=$order[$_];
  869:               $order[$_]=$movethis;
  870: 	   }
  871:        }
  872:        &storemap(&Apache::lonnet::filelocation('',$url));
  873: # --------------------------------------------------------------------- Rename
  874:    } elsif ($ENV{'form.renameres'}) {
  875:        my $residx=$Apache::lonratedt::order[$ENV{'form.renameidx'}-1];
  876:        my ($name,@resrest)=split(/\:/,$Apache::lonratedt::resources[$residx]);
  877:        $name=$ENV{'form.renametitle'};
  878:        $name=~s/\:/\&colon\;/g;
  879:        $Apache::lonratedt::resources[$residx]=$name.':'.join(':',@resrest);
  880:        &storemap(&Apache::lonnet::filelocation('',$url));
  881:    }
  882: # ------------------------------------------------------------ Assemble windows
  883:    
  884:    my $idx=-1;
  885:    $importdetail='&'.$importdetail;
  886:    $importdetail=~s/^\&+/\&/;
  887:    my $importwindow=
  888:        '<option value="-1"> ---- '.&mt('Import and Paste Area').' ---- </option>'.
  889:      join("\n",map {
  890:        $idx++;
  891:        if ($_) { 
  892:           my ($name,$url)=split(/\=/,$_);
  893:           unless ($name) { $name=(split(/\//,$url))[-1]; }
  894:           unless ($name) { $name='EMPTY'; }
  895:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
  896:                                     '</option>';
  897:       }
  898:    } split(/\&/,$importdetail));
  899: 
  900:    $idx=0;
  901:    $targetdetail='';
  902:    my $targetwindow=       
  903:        '<option value="0"> ------- '.&mt('Target Edit Map').' ------- </option>'.
  904:      join("\n",map { 
  905:        my ($name,$url)=split(/\:/,$resources[$_]);
  906:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
  907:        unless ($name) { $name='EMPTY'; }
  908:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
  909: 	                  &Apache::lonnet::escape($url);
  910:        $idx++;
  911:        $name=~s/\&colon;/\:/g;
  912:        '<option value="'.$idx.'">'.$name.'</option>';
  913:    } @order);
  914: 
  915: # ----------------------------------------------------- Start simple RAT screen
  916:    my $editscript=&editscript('simple');
  917:    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1).
  918:        &Apache::loncommon::help_open_topic('Sequence_Simple_Editor_Creation').
  919:        &Apache::loncommon::help_open_faq(6).
  920:        &Apache::loncommon::help_open_bug('RAT');
  921:    my %lt=&Apache::lonlocal::texthash(
  922: 				      'sa' => 'Save',
  923: 				      'nt' => 'New Title',
  924: 				      'se' => 'Search',
  925: 				      'im' => 'Import',
  926: 				      'vi' => 'View',
  927: 				      'lm' => 'Load Map',
  928: 				      'ds' => 'Discard Selected',
  929: 				      'ca' => 'Clear All',
  930: 				      'ta' => 'Temporary Assembly Workspace',
  931: 				      'rv' => 'Revert to Last Saved',
  932:                                       'sa' => 'Save',
  933:                                       'mu' => 'Move Up',
  934:                                       'md' => 'Move Down',
  935:                                       're' => 'Rename',
  936:                                       'as' => 'after selected',
  937:                                       'cs' => 'Cut selected',
  938:                                       'ps' => 'Copy selected',
  939:                                       'pas' => 'Paste after selected',
  940: 				      );
  941:    $r->print(<<ENDSMPHEAD);
  942: <html>
  943: <head>
  944: <script>
  945: 
  946:    $editscript
  947: 
  948:    function openview(entry) {
  949:        var url=unescape((entry.split('='))[1]);
  950:        var parts=new Array;
  951:        parts=url.split('&colon;');
  952:        url=parts.join(':');
  953:        if (url) { open(url,'cat'); }
  954:    }
  955: 
  956:    function viewtarget() {
  957:        openview((document.forms.simpleedit.targetdetail.value.split('&'))
  958:                 [document.forms.simpleedit.target.selectedIndex]);
  959:    }
  960: 
  961:    function viewimport() {
  962:        openview((document.forms.simpleedit.curimpdetail.value.split('&'))
  963:                 [document.forms.simpleedit.importsel.selectedIndex]);
  964:    }
  965: 
  966:    function renametarget() {
  967:        var selidx=document.forms.simpleedit.target.selectedIndex;
  968:        var entry=(document.forms.simpleedit.targetdetail.value.split('&'))
  969:                 [selidx];
  970:        var oldname=unescape((entry.split('='))[0]);
  971:        var nameparts=oldname.split('&colon;');
  972:        oldname=unescape(nameparts.join(':'));
  973:        nameparts=oldname.split('&#34;');
  974:        oldname=unescape(nameparts.join('"'));
  975:        nameparts=oldname.split('&#39;');
  976:        oldname=unescape(nameparts.join("'"));
  977:        newtitle=prompt('$lt{'nt'}',oldname);
  978:        if (newtitle) {
  979:            document.forms.simpleedit.renameres.value=1;
  980:            document.forms.simpleedit.renameidx.value=selidx;
  981:            document.forms.simpleedit.renametitle.value=newtitle;
  982: 	   document.forms.simpleedit.submit();
  983:        }
  984:    }
  985: 
  986: </script>
  987: </head>                 
  988: $bodytag
  989: $buttons
  990: <font color=red>$errtext</font>
  991: <form name=simpleedit method=post>
  992: <input type=hidden name=forcesmp value=1>
  993: <input type=hidden name=renameres value=0>
  994: <input type=hidden name=renametitle value=''>
  995: <input type=hidden name=renameidx value=0>
  996: <table>
  997:     <tr><th width="40%">$lt{'ta'}</th>
  998: <th>&nbsp;</th>
  999: <th width="40%">File: $url</th></tr>
 1000: <tr><td bgcolor="#FFFFCC">
 1001: <input type=button onClick="javascript:groupsearch()" value="$lt{'se'}">
 1002: <input type=button onClick="javascript:groupimport();" value="$lt{'im'}">
 1003: $lt{'as'}
 1004: <hr>
 1005: <input type=text size=20 name=importmap>
 1006: <input type=button 
 1007: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
 1008: value="Select Map"><input type=submit name=loadmap value="$lt{'lm'}"><hr>
 1009: <input type=submit name="discard" value="$lt{'ds'}">
 1010: <input type=submit name="clear" value="$lt{'ca'}">
 1011: <input type=button onClick="javascript:viewimport()" value="$lt{'vi'}">
 1012: 
 1013:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
 1014: 
 1015: <input type=button onClick=
 1016: "javascript:impfortarget.value=1;groupsearch()" value="$lt{'se'}">
 1017: <input type=button onClick=
 1018: "javascript:impfortarget.value=1;groupimport();" value="$lt{'im'}">
 1019: $lt{'as'}
 1020: <hr>
 1021: <input type=submit name="moveup" value="$lt{'mu'}">
 1022: <input type=submit name="movedown" value="$lt{'md'}">
 1023: <input type=button onClick="javascript:renametarget()" value="$lt{'re'}">
 1024: <hr>$targetmsg
 1025: <input type=submit name="revert" value="$lt{'rv'}">
 1026: <input type=submit name="save" value="$lt{'sa'}">
 1027: <input type=button onClick="javascript:viewtarget()" value="$lt{'vi'}">
 1028: </td></tr>
 1029: 
 1030: <tr><td bgcolor="#FFFFCC"><select name="importsel" size=10 multiple>
 1031: $importwindow
 1032: </select>
 1033: </td>
 1034: <td bgcolor="#FFFFAA" align="center">
 1035: $lt{'cs'}<br>
 1036: <input type=submit name=cut value='<<<'><p>
 1037: <hr>
 1038: $lt{'ps'}<br>
 1039: <input type=submit name=copy value='<--'><p>
 1040: <hr>
 1041: $lt{'pas'}<br>
 1042: <input type=submit name=paste value='-->'>
 1043: </td>
 1044: <td bgcolor="#FFFFCC"><select name="target" size=10 multiple>
 1045: $targetwindow
 1046: </select>
 1047: </table>
 1048: <input type=hidden name=importdetail value="">
 1049: <input type=hidden name=curimpdetail value="$importdetail">
 1050: <input type=hidden name=targetdetail value="$targetdetail">
 1051: <input type=hidden name=impfortarget value="0">
 1052: </form>
 1053: </body></html>
 1054: ENDSMPHEAD
 1055: }
 1056: 
 1057: # ----------------------------------------------------------------- No such dir
 1058: sub nodir {
 1059:    my ($r,$dir)=@_;
 1060:    $dir=~s/^\/home\/\w+\/public\_html//;
 1061:    $r->print(<<ENDNODIR);
 1062: <html>
 1063: <body bgcolor='#FFFFFF'>
 1064: <h1>No such directory: $dir</h1>
 1065: </body>
 1066: </html>
 1067: ENDNODIR
 1068: }
 1069: 
 1070: # ---------------------------------------------------------------- View Handler
 1071: 
 1072: sub viewmap {
 1073:     my ($r,$url,$adv,$errtext)=@_;
 1074:     $r->print('<html>'.
 1075:      &Apache::loncommon::bodytag('Edit Content of a Map').
 1076: 	      &Apache::loncommon::help_open_faq(6).
 1077: 	      &Apache::loncommon::help_open_bug('RAT').
 1078: 	      &buttons($adv));
 1079:     if ($errtext) {
 1080: 	$r->print($errtext.'<hr />');
 1081:     }
 1082:     my $idx=0;
 1083:     $r->print('<h1>'.$url.'</h1>');
 1084:     if ($adv) {
 1085: 	$r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
 1086:     }
 1087:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
 1088: 	if (defined($_)) {
 1089:             $idx++;
 1090: 	    my ($title,$url)=split(/\:/,$_);
 1091:             $title=~s/\&colon\;/\:/g;
 1092:             $url=~s/\&colon\;/\:/g;
 1093:             unless ($title) { $title=(split(/\//,$url))[-1] };
 1094:             unless ($title) { $title='<i>Empty</i>'; }
 1095:             if ($url) {
 1096: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
 1097:             }
 1098:             $r->print(&Apache::lonratsrv::qtescape($title));
 1099:             if ($url) { $r->print('</a>'); }
 1100: 	    $r->print('<br>');
 1101:         }
 1102:     }
 1103:     $r->print('</body></html>');
 1104: }
 1105: 
 1106: # ================================================================ Main Handler
 1107: 
 1108: sub handler {
 1109:   my $r=shift;
 1110:   &Apache::loncommon::content_type($r,'text/html');
 1111:   $r->send_http_header;
 1112: 
 1113:   return OK if $r->header_only;
 1114:   my $target = $ENV{'form.grade_target'};
 1115:   if ($target eq 'meta') {
 1116:       &Apache::loncommon::content_type($r,'text/html');
 1117:       $r->send_http_header;
 1118:       return OK;
 1119:   }
 1120:   
 1121:   my $url=$r->uri;
 1122:   my $fn=&Apache::lonnet::filelocation('',$url);
 1123: 
 1124:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
 1125:   unless (-e $dir) {
 1126:       &nodir($r,$dir);
 1127:       return OK;
 1128:   }
 1129: 
 1130: # ------------------------------------------- Determine which tools can be used
 1131:   my $adv=0;
 1132: 
 1133:   unless ($ENV{'form.forcesmp'}) {
 1134:      if ($ENV{'form.forceadv'}) {
 1135:         $adv=1;
 1136:      } elsif (my $fh=Apache::File->new($fn)) {
 1137: 	 my $allmap=join('',<$fh>);
 1138:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
 1139:      }
 1140:   }
 1141: 
 1142:   my $errtext='';
 1143:   my $fatal=0;
 1144: 
 1145: # -------------------------------------------------------------------- Load map
 1146:   ($errtext,$fatal)=&mapread($fn,$errtext);
 1147: 
 1148:   if ($fatal==1) { $adv=1; }
 1149: 
 1150: # ----------------------------------- adv==1 now means "graphical MUST be used"
 1151: 
 1152:   if ($ENV{'form.forceadv'}) {
 1153:       &ratedt($r,$url);
 1154:   } elsif ($ENV{'form.forcesmp'}) {
 1155:       &smpedt($r,$url,$errtext);
 1156:   } else {
 1157:       &viewmap($r,$url,$adv,$errtext);
 1158:   }
 1159:   return OK;
 1160: }
 1161: 
 1162: 1;
 1163: __END__
 1164: 
 1165: 
 1166: 
 1167: 
 1168: 
 1169: 
 1170: 

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