File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.50: download - view: text, annotated - select for diffs
Fri Dec 12 15:19:29 2003 UTC (20 years, 4 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
One more statement which I've just missed.

    1: # The LearningOnline Network with CAPA
    2: # Edit Handler for RAT Maps
    3: #
    4: # $Id: lonratedt.pm,v 1.50 2003/12/12 15:19:29 sakharuk 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);
  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: 	    return(&mt('Map has resource parameters. Use advanced editor.'),1);
  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:     } else {
  419:         unless ($adv==2) {
  420:            $output.='<input type=submit name=forcesmp value="'.&mt('Simple Edit').'">';
  421:         }
  422: 	$output.='<input type=submit name=forceadv value="'.&mt('Advanced Edit').'">';
  423:     }
  424:     return $output.'</form><hr>';
  425: }
  426: 
  427: # ----------------------------------------------------------- Paste into target
  428: # modifies @order, @resources
  429: 
  430: sub pastetarget {
  431:     my ($after,@which)=@_;
  432:     my @insertorder=();
  433:     foreach (@which) {
  434:         if (defined($_)) {
  435: 	    my ($name,$url)=split(/\=/,$_);
  436:             $name=&Apache::lonnet::unescape($name);
  437:             $url=&Apache::lonnet::unescape($url);
  438:             if ($url) {
  439: 	       my $idx=$#resources+1;
  440:                $insertorder[$#insertorder+1]=$idx;
  441:                my $ext='false';
  442:                if ($url=~/^http\:\/\//) { $ext='true'; }
  443:                $url=~s/\:/\&colon;/g;
  444:                $name=~s/\:/\&colon;/g;
  445:                $resources[$idx]=$name.':'.$url.':'.$ext.':normal:res';
  446: 	   }
  447:         }
  448:     }
  449:     my @oldorder=splice(@order,$after);
  450:     @order=(@order,@insertorder,@oldorder);
  451: }
  452: 
  453: # ------------------------------------------------ Get start and finish correct
  454: # modifies @resources
  455: 
  456: sub startfinish {
  457: # Remove all start and finish
  458:     foreach (@order) {
  459: 	my ($name,$url,$ext)=split(/\:/,$resources[$_]);
  460:         if ($url=~/http\&colon\:\/\//) { $ext='true'; }
  461:         $resources[$_]=$name.':'.$url.':'.$ext.':normal:res';
  462:     }
  463: # Garbage collection
  464:     my $stillchange=1;
  465:      while (($#order>1) && ($stillchange)) {
  466:        $stillchange=0;
  467:        for (my $i=0;$i<=$#order;$i++) {
  468:  	  my ($name,$url,$ext)=split(/\:/,$resources[$order[$i]]);
  469:           unless ($url) {
  470: # Take out empty resource
  471:               for (my $j=$i+1;$j<=$#order;$j++) {
  472:                   $order[$j-1]=$order[$j];
  473: 	      }
  474:               $#order--;
  475: 	      $stillchange=1;
  476:               last;
  477:           }
  478:        }
  479:     }
  480: # Put in a start resource
  481:    my ($name,$url,$ext)=split(/\:/,$resources[$order[0]]);
  482:    $resources[$order[0]]=$name.':'.$url.':'.$ext.':start:res';
  483: # Make sure this has at least start and finish
  484:    if ($#order==0) {
  485:        $resources[$#resources+1]='::false';
  486:        $order[1]=$#resources;
  487:    }
  488: # Make the last one a finish resource
  489:    ($name,$url,$ext)=split(/\:/,$resources[$order[$#order]]);
  490:    $resources[$order[$#order]]=$name.':'.$url.':'.$ext.':finish:res';
  491: }
  492: 
  493: # ------------------------------------------------------------------- Store map
  494: 
  495: sub storemap {
  496:     my $realfn=shift;
  497:     my $fn=$realfn;
  498: # unless this is forced to work from the original file, use a temporary file
  499: # instead
  500:     unless (shift) {
  501:        $fn=$realfn.'.tmp';
  502:        unless (-e $fn) {
  503: 	  copy($realfn,$fn);
  504:        }
  505:     }
  506: # store data either into tmp or real file
  507:     &startfinish();
  508:     my $output='graphdef<:>no';
  509:     my $k=1;
  510:     for (my $i=0; $i<=$#order; $i++) {
  511:         if (defined($resources[$order[$i]])) {
  512: 	    $output.='<&>objcont<:>'.$order[$i].'<:>'.$resources[$order[$i]];
  513:         }
  514:         if (defined($order[$i+1])) {
  515: 	    if (defined($resources[$order[$i+1]])) {
  516:                $output.='<&>objlinks<:>'.$k.'<:>'.
  517: 		   $order[$i].':'.$order[$i+1].':0';
  518: 	       $k++;
  519:             }
  520:         }
  521:     }
  522:     $output=~s/http\&colon\;\/\///g;
  523:     $ENV{'form.output'}=$output;
  524:     return 
  525:      &Apache::lonratsrv::loadmap($fn,&Apache::lonratsrv::savemap($fn,''));
  526: }
  527: 
  528: sub editscript {
  529:     my $mode=shift;
  530:     return(<<ENDSCRIPT);
  531: var srch;
  532: var srchflag=-1; // 1 means currently open
  533:                  // 0 means closed (but has been open)
  534:                  // -1 means never yet opened/defined
  535: var srchmode='';
  536: 
  537: var idx;
  538: var idxflag=-1; // 1 means currently open
  539:                  // 0 means closed (but has been open)
  540:                  // -1 means never yet opened/defined
  541: var idxmode='';
  542: 
  543: // ------------------------------------------------------ Clears indexer window
  544: function idxclear() {
  545:   idx.document.clear();
  546: }
  547: 
  548: // ------------------------------------------------------- Clears search window
  549: function srchclear() {
  550:   srch.document.clear();
  551: }
  552: 
  553: // ------------------------------------------------------ Closes indexer window
  554: function idxclose() {
  555:   if (idx && !idx.closed) {
  556:     idxflag=0;
  557:     idx.close();
  558:   }
  559: }
  560: 
  561: // ------------------------------------------------------- Closes search window
  562: function srchclose() {
  563:   if (srch && !srch.closed) {
  564:     srchflag=0;
  565:     srch.close();
  566:   }
  567: }
  568: 
  569: // -------------------------------------------------------- Open indexer window
  570: function idxopen(mode) {
  571:    var options="scrollbars=1,resizable=1,menubar=0";
  572:    idxmode=mode;
  573:    idxflag=1;
  574:    idx=open("/res/?launch=1&mode=$mode&catalogmode="+mode,"idxout",options);
  575:    idx.focus();
  576: }
  577: 
  578: // --------------------------------------------------------- Open search window
  579: function srchopen(mode) {
  580:    var options="scrollbars=1,resizable=1,menubar=0";
  581:    srchmode=mode;
  582:    srchflag=1;
  583:    srch=open("/adm/searchcat?launch=1&mode=$mode&catalogmode="+mode,"srchout",options);
  584:    srch.focus();
  585: }
  586: // ----------------------------------------------------- launch indexer browser
  587: function groupsearch() {
  588:    srchcheck('groupsearch');
  589: }
  590: 
  591: function groupimport() {
  592:    idxcheck('groupimport');
  593: }
  594: // ------------------------------------------------------- Do srch status check
  595: function srchcheck(mode) {
  596:    if (!srch || srch.closed || srchmode!=mode) {
  597:       srchopen(mode);
  598:    }
  599:    srch.focus();
  600: }
  601: 
  602: // -------------------------------------------------------- Do idx status check
  603: function idxcheck(mode) {
  604:    if (!idx || idx.closed || idxmode!=mode) {
  605:       idxopen(mode);
  606:    }
  607:    idx.focus();
  608: }
  609: 
  610: 
  611:     var editbrowser;
  612:     function openbrowser(formname,elementname,only,omit) {
  613:         var url = '/res/?';
  614:         if (editbrowser == null) {
  615:             url += 'launch=1&';
  616:         }
  617:         url += 'catalogmode=interactive&';
  618:         url += 'mode=edit&';
  619:         url += 'form=' + formname + '&';
  620:         if (only != null) {
  621:             url += 'only=' + only + '&';
  622:         } 
  623:         if (omit != null) {
  624:             url += 'omit=' + omit + '&';
  625:         }
  626:         url += 'element=' + elementname + '';
  627:         var title = 'Browser';
  628:         var options = 'scrollbars=1,resizable=1,menubar=0';
  629:         options += ',width=700,height=600';
  630:         editbrowser = open(url,title,options,'1');
  631:         editbrowser.focus();
  632:     }
  633: ENDSCRIPT
  634: }
  635: # ------------------------------------------------------- Simple edit processor
  636: 
  637: sub smpedt {
  638:    my ($r,$url,$errtext)=@_;
  639:    my $buttons=&buttons(2);
  640:    my $tmpfn=&Apache::lonnet::filelocation('',$url).'.tmp';
  641:    my $targetmsg='';
  642:    if ($ENV{'form.save'}) {
  643:        $targetmsg='<b>Saving ...</b><br>';
  644:        copy($tmpfn,&Apache::lonnet::filelocation('',$url));
  645:        unlink($tmpfn);
  646:        my ($errtext,$fatal)=
  647:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  648:    }
  649:    if ($ENV{'form.revert'}) {
  650:        $targetmsg='<b>Reverting ...</b><br>';
  651:        unlink($tmpfn);
  652:        my ($errtext,$fatal)=
  653:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  654:    }
  655:    if (-e $tmpfn) {
  656:       $targetmsg=
  657:         '<b><font color="red">'.&mt('You are working with an unsaved version of your map.').'</font></b><br>';
  658:       my ($errtext,$fatal)=&mapread($tmpfn,'');
  659:    }
  660: # ---------------------------------------------------------- Process form input
  661: 
  662:    my @importselect=();
  663:    my @targetselect=();
  664:    undef @importselect;
  665:    undef @targetselect;
  666:    if (defined($ENV{'form.importsel'})) {
  667:        if (ref($ENV{'form.importsel'})) {
  668: 	   @importselect=sort(@{$ENV{'form.importsel'}});
  669:        } else {
  670:            @importselect=($ENV{'form.importsel'});
  671:        }
  672:    }
  673:    if (defined($ENV{'form.target'})) {
  674:        if (ref($ENV{'form.target'})) {
  675: 	   @targetselect=sort(@{$ENV{'form.target'}});
  676:        } else {
  677:            @targetselect=($ENV{'form.target'});
  678:        }
  679:    }
  680: # ============================================================ Process commands
  681: 
  682:    my $targetdetail=$ENV{'form.targetdetail'};
  683:    my $importdetail=$ENV{'form.curimpdetail'};
  684: 
  685: # ---------------------------------------------------- Importing from groupsort
  686:    if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
  687: 
  688:        $importdetail='';
  689:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  690: 
  691:        my $lastsel;
  692: 
  693:        if (defined($importselect[-1])) {
  694: 	   $lastsel=$importselect[-1];
  695:        } else {
  696:            $lastsel=$#curimport;
  697:        }
  698: 
  699:        for (my $i=0;$i<=$lastsel;$i++) {
  700:            my ($name,$url)=split(/\=/,$curimport[$i]);
  701:            if ($url) {
  702:               $importdetail.='&'.$name.'='.$url;
  703: 	   }
  704:        }
  705: 
  706:       $importdetail.='&'.$ENV{'form.importdetail'};
  707: 
  708:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  709:            my ($name,$url)=split(/\=/,$curimport[$i]);
  710:            if ($url) {
  711:               $importdetail.='&'.$name.'='.$url;
  712: 	  }
  713:        }
  714:        $importdetail=~s/\&+/\&/g;
  715:        $importdetail=~s/^\&//;
  716: 
  717: # ------------------------------------------------------------------- Clear all
  718:    } elsif ($ENV{'form.clear'}) {
  719:        $importdetail='';
  720: # ------------------------------------------------------------ Discard selected
  721:    } elsif ($ENV{'form.discard'}) {
  722:        $importdetail='';
  723:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  724:        foreach (@importselect) {
  725: 	   $curimport[$_]='';
  726:        }
  727:        for (my $i=0;$i<=$#curimport;$i++) {
  728:            my ($name,$url)=split(/\=/,$curimport[$i]);
  729:            if ($url) {
  730:               $importdetail.='&'.$name.'='.$url;
  731: 	   }
  732:        }
  733: # --------------------------------------------------------- Loading another map
  734:    } elsif ($ENV{'form.loadmap'}) {
  735:        $importdetail='';
  736:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  737: 
  738:        my $lastsel;
  739: 
  740:        if (defined($importselect[-1])) {
  741: 	   $lastsel=$importselect[-1];
  742:        } else {
  743:            $lastsel=$#curimport;
  744:        }
  745: 
  746:        for (my $i=0;$i<=$lastsel;$i++) {
  747:            my ($name,$url)=split(/\=/,$curimport[$i]);
  748:            if ($url) {
  749:               $importdetail.='&'.$name.'='.$url;
  750: 	   }
  751:        }
  752: 
  753:        foreach (
  754:     &Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
  755: 	   my ($name,$url)=split(/\:/,$_);
  756:            if ($url) {
  757:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  758: 		 	         &Apache::lonnet::escape($url);
  759: 	  }
  760:        }
  761: 
  762:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  763:            my ($name,$url)=split(/\=/,$curimport[$i]);
  764:            if ($url) {
  765:               $importdetail.='&'.$name.'='.$url;
  766: 	  }
  767:        }
  768:        $importdetail=~s/\&+/\&/g;
  769:        $importdetail=~s/^\&//;
  770: 
  771: # ------------------------------------------------ Groupimport/search to target
  772:    } elsif ($ENV{'form.importdetail'}) {
  773:        my $lastsel;
  774:        if (defined($targetselect[-1])) {
  775: 	   $lastsel=$targetselect[-1];
  776:        } else {
  777:            $lastsel=$#order+1;
  778:        }
  779:        &pastetarget($lastsel,split(/\&/,$ENV{'form.importdetail'}));
  780:        &storemap(&Apache::lonnet::filelocation('',$url));
  781: # ------------------------------------------------------------------------- Cut
  782:    } elsif (($ENV{'form.cut'}) || ($ENV{'form.copy'})) {
  783:        $importdetail='';
  784:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  785: 
  786:        my $lastsel;
  787: 
  788:        if (defined($importselect[-1])) {
  789: 	   $lastsel=$importselect[-1];
  790:        } else {
  791:            $lastsel=$#curimport;
  792:        }
  793: 
  794:        for (my $i=0;$i<=$lastsel;$i++) {
  795:            my ($name,$url)=split(/\=/,$curimport[$i]);
  796:            if ($url) {
  797:               $importdetail.='&'.$name.'='.$url;
  798: 	   }
  799:        }
  800: 
  801:        foreach (@targetselect) {
  802: 	   my ($name,$url)=split(/\:/,$resources[$order[$_-1]]);
  803:            if ($url) {
  804:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  805: 		 	         &Apache::lonnet::escape($url);
  806: 	  }
  807:        }
  808: 
  809:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  810:            my ($name,$url)=split(/\=/,$curimport[$i]);
  811:            if ($url) {
  812:               $importdetail.='&'.$name.'='.$url;
  813: 	  }
  814:        }
  815:        $importdetail=~s/\&+/\&/g;
  816:        $importdetail=~s/^\&//;
  817: 
  818:        if ($ENV{'form.cut'}) {
  819:            my @neworder=();
  820:            for (my $i=0;$i<=$#order;$i++) {
  821:                my $include=1;
  822:                foreach (@targetselect) {
  823: 		   if ($_-1==$i) { $include=0; }
  824:                }
  825:                if ($include) { $neworder[$#neworder+1]=$order[$i]; }
  826:            }
  827:            @order=@neworder;
  828:            &storemap(&Apache::lonnet::filelocation('',$url));      
  829:        }
  830: 
  831: # ----------------------------------------------------------------------- Paste
  832:    } elsif ($ENV{'form.paste'}) {
  833:        my $lastsel;
  834:        if (defined($targetselect[-1])) {
  835: 	   $lastsel=$targetselect[-1];
  836:        } else {
  837:            $lastsel=$#order+1;
  838:        }
  839:        my @newsequence;
  840:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  841:        foreach (@importselect) {
  842:           $newsequence[$#newsequence+1]=$curimport[$_];
  843:        }
  844:        &pastetarget($lastsel,@newsequence);
  845:        &storemap(&Apache::lonnet::filelocation('',$url));
  846: # -------------------------------------------------------------------- Move up 
  847:    } elsif ($ENV{'form.moveup'}) {
  848:        foreach (sort @targetselect) {
  849:            if ($_-1>0) {
  850:               my $movethis=$order[$_-1];
  851:               $order[$_-1]=$order[$_-2];
  852:               $order[$_-2]=$movethis;
  853: 	  }
  854:        }
  855:        &storemap(&Apache::lonnet::filelocation('',$url));
  856: # ------------------------------------------------------------------ Move down
  857:    } elsif ($ENV{'form.movedown'}) {
  858:        foreach (reverse sort @targetselect) {
  859:            if ($_-1<$#order) {
  860:               my $movethis=$order[$_-1];
  861:               $order[$_-1]=$order[$_];
  862:               $order[$_]=$movethis;
  863: 	   }
  864:        }
  865:        &storemap(&Apache::lonnet::filelocation('',$url));
  866: # --------------------------------------------------------------------- Rename
  867:    } elsif ($ENV{'form.renameres'}) {
  868:        my $residx=$Apache::lonratedt::order[$ENV{'form.renameidx'}-1];
  869:        my ($name,@resrest)=split(/\:/,$Apache::lonratedt::resources[$residx]);
  870:        $name=$ENV{'form.renametitle'};
  871:        $name=~s/\:/\&colon\;/g;
  872:        $Apache::lonratedt::resources[$residx]=$name.':'.join(':',@resrest);
  873:        &storemap(&Apache::lonnet::filelocation('',$url));
  874:    }
  875: # ------------------------------------------------------------ Assemble windows
  876:    
  877:    my $idx=-1;
  878:    $importdetail='&'.$importdetail;
  879:    $importdetail=~s/^\&+/\&/;
  880:    my $importwindow=
  881:        '<option value="-1"> ---- '.&mt('Import and Paste Area').' ---- </option>'.
  882:      join("\n",map {
  883:        $idx++;
  884:        if ($_) { 
  885:           my ($name,$url)=split(/\=/,$_);
  886:           unless ($name) { $name=(split(/\//,$url))[-1]; }
  887:           unless ($name) { $name='EMPTY'; }
  888:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
  889:                                     '</option>';
  890:       }
  891:    } split(/\&/,$importdetail));
  892: 
  893:    $idx=0;
  894:    $targetdetail='';
  895:    my $targetwindow=       
  896:        '<option value="0"> ------- '.&mt('Target Edit Map').' ------- </option>'.
  897:      join("\n",map { 
  898:        my ($name,$url)=split(/\:/,$resources[$_]);
  899:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
  900:        unless ($name) { $name='EMPTY'; }
  901:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
  902: 	                  &Apache::lonnet::escape($url);
  903:        $idx++;
  904:        $name=~s/\&colon;/\:/g;
  905:        '<option value="'.$idx.'">'.$name.'</option>';
  906:    } @order);
  907: 
  908: # ----------------------------------------------------- Start simple RAT screen
  909:    my $editscript=&editscript('simple');
  910:    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
  911:    my %lt=&Apache::lonlocal::texthash(
  912: 				      'sa' => 'Save',
  913: 				      'nt' => 'New Title',
  914: 				      'se' => 'Search',
  915: 				      'im' => 'Import',
  916: 				      'vi' => 'View',
  917: 				      'lm' => 'Load Map',
  918: 				      'ds' => 'Discard Selected',
  919: 				      'ca' => 'Clear All',
  920: 				      'ta' => 'Temporary Assembly Workspace',
  921: 				      'rv' => 'Revert to Last Saved',
  922:                                       'sa' => 'Save',
  923:                                       'mu' => 'Move Up',
  924:                                       'md' => 'Move Down',
  925:                                       're' => 'Rename',
  926:                                       'as' => 'after selected',
  927:                                       'cs' => 'Cut selected',
  928:                                       'ps' => 'Copy selected',
  929:                                       'pas' => 'Paste after selected',
  930: 				      );
  931:    $r->print(<<ENDSMPHEAD);
  932: <html>
  933: <head>
  934: <script>
  935: 
  936:    $editscript
  937: 
  938:    function openview(entry) {
  939:        var url=unescape((entry.split('='))[1]);
  940:        var parts=new Array;
  941:        parts=url.split('&colon;');
  942:        url=parts.join(':');
  943:        if (url) { open(url,'cat'); }
  944:    }
  945: 
  946:    function viewtarget() {
  947:        openview((document.forms.simpleedit.targetdetail.value.split('&'))
  948:                 [document.forms.simpleedit.target.selectedIndex]);
  949:    }
  950: 
  951:    function viewimport() {
  952:        openview((document.forms.simpleedit.curimpdetail.value.split('&'))
  953:                 [document.forms.simpleedit.importsel.selectedIndex]);
  954:    }
  955: 
  956:    function renametarget() {
  957:        var selidx=document.forms.simpleedit.target.selectedIndex;
  958:        var entry=(document.forms.simpleedit.targetdetail.value.split('&'))
  959:                 [selidx];
  960:        var oldname=unescape((entry.split('='))[0]);
  961:        var nameparts=oldname.split('&colon;');
  962:        oldname=unescape(nameparts.join(':'));
  963:        nameparts=oldname.split('&#34;');
  964:        oldname=unescape(nameparts.join('"'));
  965:        nameparts=oldname.split('&#39;');
  966:        oldname=unescape(nameparts.join("'"));
  967:        newtitle=prompt('$lt{'nt'}',oldname);
  968:        if (newtitle) {
  969:            document.forms.simpleedit.renameres.value=1;
  970:            document.forms.simpleedit.renameidx.value=selidx;
  971:            document.forms.simpleedit.renametitle.value=newtitle;
  972: 	   document.forms.simpleedit.submit();
  973:        }
  974:    }
  975: 
  976: </script>
  977: </head>                 
  978: $bodytag
  979: $buttons
  980: <font color=red>$errtext</font>
  981: <form name=simpleedit method=post>
  982: <input type=hidden name=forcesmp value=1>
  983: <input type=hidden name=renameres value=0>
  984: <input type=hidden name=renametitle value=''>
  985: <input type=hidden name=renameidx value=0>
  986: <table>
  987:     <tr><th width="40%">$lt{'ta'}</th>
  988: <th>&nbsp;</th>
  989: <th width="40%">File: $url</th></tr>
  990: <tr><td bgcolor="#FFFFCC">
  991: <input type=button onClick="javascript:groupsearch()" value="$lt{'se'}">
  992: <input type=button onClick="javascript:groupimport();" value="$lt{'im'}">
  993: $lt{'as'}
  994: <hr>
  995: <input type=text size=20 name=importmap>
  996: <input type=button 
  997: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
  998: value="Select Map"><input type=submit name=loadmap value="$lt{'lm'}"><hr>
  999: <input type=submit name="discard" value="$lt{'ds'}">
 1000: <input type=submit name="clear" value="$lt{'ca'}">
 1001: <input type=button onClick="javascript:viewimport()" value="$lt{'vi'}">
 1002: 
 1003:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
 1004: 
 1005: <input type=button onClick=
 1006: "javascript:impfortarget.value=1;groupsearch()" value="$lt{'se'}">
 1007: <input type=button onClick=
 1008: "javascript:impfortarget.value=1;groupimport();" value="$lt{'im'}">
 1009: $lt{'as'}
 1010: <hr>
 1011: <input type=submit name="moveup" value="$lt{'mu'}">
 1012: <input type=submit name="movedown" value="$lt{'md'}">
 1013: <input type=button onClick="javascript:renametarget()" value="$lt{'re'}">
 1014: <hr>$targetmsg
 1015: <input type=submit name="revert" value="$lt{'rv'}">
 1016: <input type=submit name="save" value="$lt{'sa'}">
 1017: <input type=button onClick="javascript:viewtarget()" value="$lt{'vi'}">
 1018: </td></tr>
 1019: 
 1020: <tr><td bgcolor="#FFFFCC"><select name="importsel" size=10 multiple>
 1021: $importwindow
 1022: </select>
 1023: </td>
 1024: <td bgcolor="#FFFFAA" align="center">
 1025: $lt{'cs'}<br>
 1026: <input type=submit name=cut value='<<<'><p>
 1027: <hr>
 1028: $lt{'ps'}<br>
 1029: <input type=submit name=copy value='<--'><p>
 1030: <hr>
 1031: $lt{'pas'}<br>
 1032: <input type=submit name=paste value='-->'>
 1033: </td>
 1034: <td bgcolor="#FFFFCC"><select name="target" size=10 multiple>
 1035: $targetwindow
 1036: </select>
 1037: </table>
 1038: <input type=hidden name=importdetail value="">
 1039: <input type=hidden name=curimpdetail value="$importdetail">
 1040: <input type=hidden name=targetdetail value="$targetdetail">
 1041: <input type=hidden name=impfortarget value="0">
 1042: </form>
 1043: </body></html>
 1044: ENDSMPHEAD
 1045: }
 1046: 
 1047: # ----------------------------------------------------------------- No such dir
 1048: sub nodir {
 1049:    my ($r,$dir)=@_;
 1050:    $dir=~s/^\/home\/\w+\/public\_html//;
 1051:    $r->print(<<ENDNODIR);
 1052: <html>
 1053: <body bgcolor='#FFFFFF'>
 1054: <h1>No such directory: $dir</h1>
 1055: </body>
 1056: </html>
 1057: ENDNODIR
 1058: }
 1059: 
 1060: # ---------------------------------------------------------------- View Handler
 1061: 
 1062: sub viewmap {
 1063:     my ($r,$url,$adv,$errtext)=@_;
 1064:     $r->print('<html>'.
 1065:      &Apache::loncommon::bodytag('Edit Content of a Map').
 1066:      &buttons($adv));
 1067:     if ($errtext) {
 1068: 	$r->print($errtext.'<hr>');
 1069:     }
 1070:     my $idx=0;
 1071:     $r->print('<h1>'.$url.'</h1>');
 1072:     if ($adv) {
 1073: 	$r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
 1074:     }
 1075:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
 1076: 	if (defined($_)) {
 1077:             $idx++;
 1078: 	    my ($title,$url)=split(/\:/,$_);
 1079:             $title=~s/\&colon\;/\:/g;
 1080:             $url=~s/\&colon\;/\:/g;
 1081:             unless ($title) { $title=(split(/\//,$url))[-1] };
 1082:             unless ($title) { $title='<i>Empty</i>'; }
 1083:             if ($url) {
 1084: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
 1085:             }
 1086:             $r->print(&Apache::lonratsrv::qtescape($title));
 1087:             if ($url) { $r->print('</a>'); }
 1088: 	    $r->print('<br>');
 1089:         }
 1090:     }
 1091:     $r->print('</body></html>');
 1092: }
 1093: 
 1094: # ================================================================ Main Handler
 1095: 
 1096: sub handler {
 1097:   my $r=shift;
 1098:   &Apache::loncommon::content_type($r,'text/html');
 1099:   $r->send_http_header;
 1100: 
 1101:   return OK if $r->header_only;
 1102:   my $target = $ENV{'form.grade_target'};
 1103:   if ($target eq 'meta') {
 1104:       &Apache::loncommon::content_type($r,'text/html');
 1105:       $r->send_http_header;
 1106:       return OK;
 1107:   }
 1108:   
 1109:   my $url=$r->uri;
 1110:   my $fn=&Apache::lonnet::filelocation('',$url);
 1111: 
 1112:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
 1113:   unless (-e $dir) {
 1114:       &nodir($r,$dir);
 1115:       return OK;
 1116:   }
 1117: 
 1118: # ------------------------------------------- Determine which tools can be used
 1119:   my $adv=0;
 1120: 
 1121:   unless ($ENV{'form.forcesmp'}) {
 1122:      if ($ENV{'form.forceadv'}) {
 1123:         $adv=1;
 1124:      } elsif (my $fh=Apache::File->new($fn)) {
 1125: 	 my $allmap=join('',<$fh>);
 1126:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
 1127:      }
 1128:   }
 1129: 
 1130:   my $errtext='';
 1131:   my $fatal=0;
 1132: 
 1133: # -------------------------------------------------------------------- Load map
 1134:   ($errtext,$fatal)=&mapread($fn,$errtext);
 1135: 
 1136:   if ($fatal==1) { $adv=1; }
 1137: 
 1138: # ----------------------------------- adv==1 now means "graphical MUST be used"
 1139: 
 1140:   if ($ENV{'form.forceadv'}) {
 1141:       &ratedt($r,$url);
 1142:   } elsif ($ENV{'form.forcesmp'}) {
 1143:       &smpedt($r,$url,$errtext);
 1144:   } else {
 1145:       &viewmap($r,$url,$adv,$errtext);
 1146:   }
 1147:   return OK;
 1148: }
 1149: 
 1150: 1;
 1151: __END__
 1152: 
 1153: 
 1154: 
 1155: 
 1156: 
 1157: 
 1158: 

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