File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.46: download - view: text, annotated - select for diffs
Fri Jul 25 04:14:32 2003 UTC (20 years, 9 months ago) by bowersj2
Branches: MAIN
CVS tags: HEAD
Documentation updates.

    1: # The LearningOnline Network with CAPA
    2: # Edit Handler for RAT Maps
    3: #
    4: # $Id: lonratedt.pm,v 1.46 2003/07/25 04:14:32 bowersj2 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 File::Copy;
  177: 
  178: use vars qw(@order @resources);
  179: 
  180: 
  181: # Mapread read maps into global arrays @links and @resources, determines status
  182: # sets @order - pointer to resources in right order
  183: # sets @resources - array with the resources with correct idx
  184: #
  185: sub mapread {
  186:     my $fn=shift;
  187: 
  188:     my @links;
  189:     undef @links;
  190:     undef @resources;
  191:     undef @order;
  192:     @resources=('');
  193:     @order=();
  194: 
  195:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
  196:     if ($errtext) { return ($errtext,2); }
  197: 
  198: # -------------------------------------------------------------------- Read map
  199:     foreach (split(/\<\&\>/,$outtext)) {
  200: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
  201:         if ($command eq 'objcont') {
  202: 	    $resources[$number]=$content;
  203:         }
  204:         if ($command eq 'objlinks') {
  205:             $links[$number]=$content;
  206:         }
  207:         if ($command eq 'objparms') {
  208: 	    return('Map has resource parameters. Use advanced editor.',1);
  209:         }
  210:     }
  211: # ------------------------------------------------------- Is this a linear map?
  212:     my @starters=();
  213:     my @endings=();
  214:     undef @starters;
  215:     undef @endings;
  216: 
  217:     foreach (@links) {
  218:         if (defined($_)) {
  219: 	    my ($start,$end,$cond)=split(/\:/,$_);
  220:             if ((defined($starters[$start])) || (defined($endings[$end]))) { 
  221: 		return
  222:                  ('Map has branchings. Use advanced editor.',1);
  223:             }
  224: 	    $starters[$start]=1;
  225: 	    $endings[$end]=1;
  226: 	    if ($cond) {
  227: 		return
  228:                  ('Map has conditions. Use advanced editor.',1);
  229:             }
  230: 	}
  231: 
  232:     }
  233:     for (my $i=1; $i<=$#resources; $i++) {
  234:         if (defined($resources[$i])) {
  235: 	    unless (($starters[$i]) || ($endings[$i])) {
  236:                 return
  237: 		 ('Map has unconnected resources. Use advanced editor.',1);
  238:             }
  239:         }
  240:     }
  241: # ---------------------------------------------- Did we just read an empty map?
  242:     if ($#resources<1) {
  243:         undef $resources[0];
  244: 	$resources[1]=':::start';
  245:         $resources[2]=':::finish';
  246:     }
  247: # -------------------------------------------------- This is a linear map, sort
  248: 
  249:     my $startidx=0;
  250:     my $endidx=0;
  251:     for (my $i=0; $i<=$#resources; $i++) {
  252:         if (defined($resources[$i])) {
  253:             my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
  254: 	    if ($type eq 'start') { $startidx=$i; }
  255:             if ($type eq 'finish') { $endidx=$i; }
  256:         }
  257:     }
  258:     my $k=0;
  259:     my $currentidx=$startidx;
  260:     $order[$k]=$currentidx;
  261:     for (my $i=0; $i<=$#resources; $i++) {
  262:         foreach (@links) {
  263: 	    my ($start,$end)=split(/\:/,$_);
  264:             if ($start==$currentidx) {
  265: 		$currentidx=$end;
  266:                 $k++;
  267:                 $order[$k]=$currentidx;
  268:                 last;
  269:             }
  270:         }
  271:         if ($currentidx==$endidx) { last; }
  272:     }
  273:     return $errtext;
  274: }
  275: 
  276: # ---------------------------------------------- Read a map as well as possible
  277: # Also used by the sequence handler
  278: # Call lonsequence::attemptread to read from resource space
  279: #
  280: sub attemptread {
  281:     my $fn=shift;
  282: 
  283:     my @links;
  284:     undef @links;
  285:     my @theseres;
  286:     undef @theseres;
  287: 
  288:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
  289:     if ($errtext) { return @theseres }
  290: 
  291: # -------------------------------------------------------------------- Read map
  292:     foreach (split(/\<\&\>/,$outtext)) {
  293: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
  294:         if ($command eq 'objcont') {
  295: 	    $theseres[$number]=$content;
  296:         }
  297:         if ($command eq 'objlinks') {
  298:             $links[$number]=$content;
  299:         }
  300:     }
  301: 
  302: # --------------------------------------------------------------- Sort, sort of
  303: 
  304:     my @objsort=();
  305:     undef @objsort;
  306: 
  307:     my @data1=();
  308:     my @data2=();
  309:     undef @data1;
  310:     undef @data2;
  311: 
  312:     my $k;
  313:     my $kj;
  314:     my $j;
  315:     my $ij;
  316: 
  317:    for ($k=1;$k<=$#theseres;$k++) {
  318:       if (defined($theseres[$k])) {
  319:          $objsort[$#objsort+1]=$k;
  320:       }
  321:    }
  322: 
  323:    for ($k=1;$k<=$#links;$k++) {
  324:      if (defined($links[$k])) {
  325:       @data1=split(/\:/,$links[$k]);
  326:       $kj=-1;
  327:       for (my $j=0;$j<=$#objsort;$j++) {
  328:          if ((split(/\:/,$objsort[$j]))[0]==$data1[0]) {
  329:             $kj=$j;
  330:          }
  331:       }
  332:       if ($kj!=-1) { $objsort[$kj].=':'.$data1[1]; }
  333:      }
  334:    }
  335:     for ($k=0;$k<=$#objsort;$k++) {
  336:       for ($j=0;$j<=$#objsort;$j++) {
  337:         if ($k!=$j) {
  338:           @data1=split(/\:/,$objsort[$k]);
  339:           @data2=split(/\:/,$objsort[$j]);
  340:           my $dol=$#data1+1;
  341:           my $dtl=$#data2+1;
  342:           if ($dol+$dtl<1000) {
  343:            for ($kj=1;$kj<$dol;$kj++) {
  344:              if ($data1[$kj]==$data2[0]) {
  345:                 for ($ij=1;$ij<$dtl;$ij++) {
  346:                    $data1[$#data1+1]=$data2[$ij];
  347:                 }
  348:              }
  349:            }
  350:            for ($kj=1;$kj<$dtl;$kj++) {
  351:              if ($data2[$kj]==$data1[0]) {
  352:                  for ($ij=1;$ij<$dol;$ij++) {
  353:                     $data2[$#data2+1]=$data1[$ij];
  354:                  }
  355:              }
  356:            }
  357:            $objsort[$k]=join(':',@data1);
  358:            $objsort[$j]=join(':',@data2);
  359:           }
  360:          }
  361:       } 
  362:   }
  363: # ---------------------------------------------------------------- Now sort out
  364: 
  365:     @objsort=sort {
  366:       my @data1=split(/\:/,$a);
  367:       my @data2=split(/\:/,$b);
  368:       my $rvalue=0;
  369:       my $k;
  370:       for ($k=1;$k<=$#data1;$k++) {
  371:          if ($data1[$k]==$data2[0]) { $rvalue--; }
  372:       }
  373:       for ($k=1;$k<=$#data2;$k++) {
  374:          if ($data2[$k]==$data1[0]) { $rvalue++; }
  375:       }
  376:       if ($rvalue==0) { $rvalue=$#data2-$#data1; }
  377:       $rvalue;
  378:     } @objsort;
  379: 
  380:     my @outres=();
  381:     undef @outres;
  382: 
  383:     for ($k=0;$k<=$#objsort;$k++) {
  384: 	$outres[$k]=$theseres[(split(/\:/,$objsort[$k]))[0]];
  385:     }
  386:     return @outres;
  387: }
  388: 
  389: # --------------------------------------------------------- Build up RAT screen
  390: sub ratedt {
  391:   my ($r,$url)=@_;
  392:   $r->print(<<ENDDOCUMENT);
  393: 
  394: <html>
  395: <head>
  396: <script language="JavaScript">
  397:     var flag=0;
  398: </script>
  399: </head>
  400: <frameset rows="1,50,*" border=0>
  401: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
  402: <frame name=code src="/adm/rat/code.html">
  403: <frame name=mapout src="/adm/rat/map.html">
  404: </frameset>
  405: </html>
  406: 
  407: ENDDOCUMENT
  408: }
  409: 
  410: # ---------------------------------------------------------------- Make buttons
  411: 
  412: sub buttons {
  413:     my $adv=shift;
  414:     my $output='<form method=post>';     
  415:     if ($adv==1) {
  416: 	$output.='<input type=submit name=forceadv value="Edit">';
  417:     } else {
  418:         unless ($adv==2) {
  419:            $output.='<input type=submit name=forcesmp value="Simple Edit">';
  420:         }
  421: 	$output.='<input type=submit name=forceadv value="Advanced Edit">';
  422:     }
  423:     return $output.'</form><hr>';
  424: }
  425: 
  426: # ----------------------------------------------------------- Paste into target
  427: # modifies @order, @resources
  428: 
  429: sub pastetarget {
  430:     my ($after,@which)=@_;
  431:     my @insertorder=();
  432:     foreach (@which) {
  433:         if (defined($_)) {
  434: 	    my ($name,$url)=split(/\=/,$_);
  435:             $name=&Apache::lonnet::unescape($name);
  436:             $url=&Apache::lonnet::unescape($url);
  437:             if ($url) {
  438: 	       my $idx=$#resources+1;
  439:                $insertorder[$#insertorder+1]=$idx;
  440:                my $ext='false';
  441:                if ($url=~/^http\:\/\//) { $ext='true'; }
  442:                $url=~s/\:/\&colon;/g;
  443:                $name=~s/\:/\&colon;/g;
  444:                $resources[$idx]=$name.':'.$url.':'.$ext.':normal:res';
  445: 	   }
  446:         }
  447:     }
  448:     my @oldorder=splice(@order,$after);
  449:     @order=(@order,@insertorder,@oldorder);
  450: }
  451: 
  452: # ------------------------------------------------ Get start and finish correct
  453: # modifies @resources
  454: 
  455: sub startfinish {
  456: # Remove all start and finish
  457:     foreach (@order) {
  458: 	my ($name,$url,$ext)=split(/\:/,$resources[$_]);
  459:         if ($url=~/http\&colon\:\/\//) { $ext='true'; }
  460:         $resources[$_]=$name.':'.$url.':'.$ext.':normal:res';
  461:     }
  462: # Garbage collection
  463:     my $stillchange=1;
  464:      while (($#order>1) && ($stillchange)) {
  465:        $stillchange=0;
  466:        for (my $i=0;$i<=$#order;$i++) {
  467:  	  my ($name,$url,$ext)=split(/\:/,$resources[$order[$i]]);
  468:           unless ($url) {
  469: # Take out empty resource
  470:               for (my $j=$i+1;$j<=$#order;$j++) {
  471:                   $order[$j-1]=$order[$j];
  472: 	      }
  473:               $#order--;
  474: 	      $stillchange=1;
  475:               last;
  476:           }
  477:        }
  478:     }
  479: # Put in a start resource
  480:    my ($name,$url,$ext)=split(/\:/,$resources[$order[0]]);
  481:    $resources[$order[0]]=$name.':'.$url.':'.$ext.':start:res';
  482: # Make sure this has at least start and finish
  483:    if ($#order==0) {
  484:        $resources[$#resources+1]='::false';
  485:        $order[1]=$#resources;
  486:    }
  487: # Make the last one a finish resource
  488:    ($name,$url,$ext)=split(/\:/,$resources[$order[$#order]]);
  489:    $resources[$order[$#order]]=$name.':'.$url.':'.$ext.':finish:res';
  490: }
  491: 
  492: # ------------------------------------------------------------------- Store map
  493: 
  494: sub storemap {
  495:     my $realfn=shift;
  496:     my $fn=$realfn;
  497: # unless this is forced to work from the original file, use a temporary file
  498: # instead
  499:     unless (shift) {
  500:        $fn=$realfn.'.tmp';
  501:        unless (-e $fn) {
  502: 	  copy($realfn,$fn);
  503:        }
  504:     }
  505: # store data either into tmp or real file
  506:     &startfinish();
  507:     my $output='graphdef<:>no';
  508:     my $k=1;
  509:     for (my $i=0; $i<=$#order; $i++) {
  510:         if (defined($resources[$order[$i]])) {
  511: 	    $output.='<&>objcont<:>'.$order[$i].'<:>'.$resources[$order[$i]];
  512:         }
  513:         if (defined($order[$i+1])) {
  514: 	    if (defined($resources[$order[$i+1]])) {
  515:                $output.='<&>objlinks<:>'.$k.'<:>'.
  516: 		   $order[$i].':'.$order[$i+1].':0';
  517: 	       $k++;
  518:             }
  519:         }
  520:     }
  521:     $output=~s/http\&colon\;\/\///g;
  522:     $ENV{'form.output'}=$output;
  523:     return 
  524:      &Apache::lonratsrv::loadmap($fn,&Apache::lonratsrv::savemap($fn,''));
  525: }
  526: 
  527: sub editscript {
  528:     my $mode=shift;
  529:     return(<<ENDSCRIPT);
  530: var srch;
  531: var srchflag=-1; // 1 means currently open
  532:                  // 0 means closed (but has been open)
  533:                  // -1 means never yet opened/defined
  534: var srchmode='';
  535: 
  536: var idx;
  537: var idxflag=-1; // 1 means currently open
  538:                  // 0 means closed (but has been open)
  539:                  // -1 means never yet opened/defined
  540: var idxmode='';
  541: 
  542: // ------------------------------------------------------ Clears indexer window
  543: function idxclear() {
  544:   idx.document.clear();
  545: }
  546: 
  547: // ------------------------------------------------------- Clears search window
  548: function srchclear() {
  549:   srch.document.clear();
  550: }
  551: 
  552: // ------------------------------------------------------ Closes indexer window
  553: function idxclose() {
  554:   if (idx && !idx.closed) {
  555:     idxflag=0;
  556:     idx.close();
  557:   }
  558: }
  559: 
  560: // ------------------------------------------------------- Closes search window
  561: function srchclose() {
  562:   if (srch && !srch.closed) {
  563:     srchflag=0;
  564:     srch.close();
  565:   }
  566: }
  567: 
  568: // -------------------------------------------------------- Open indexer window
  569: function idxopen(mode) {
  570:    var options="scrollbars=1,resizable=1,menubar=0";
  571:    idxmode=mode;
  572:    idxflag=1;
  573:    idx=open("/res/?launch=1&mode=$mode&catalogmode="+mode,"idxout",options);
  574:    idx.focus();
  575: }
  576: 
  577: // --------------------------------------------------------- Open search window
  578: function srchopen(mode) {
  579:    var options="scrollbars=1,resizable=1,menubar=0";
  580:    srchmode=mode;
  581:    srchflag=1;
  582:    srch=open("/adm/searchcat?launch=1&mode=$mode&catalogmode="+mode,"srchout",options);
  583:    srch.focus();
  584: }
  585: // ----------------------------------------------------- launch indexer browser
  586: function groupsearch() {
  587:    srchcheck('groupsearch');
  588: }
  589: 
  590: function groupimport() {
  591:    idxcheck('groupimport');
  592: }
  593: // ------------------------------------------------------- Do srch status check
  594: function srchcheck(mode) {
  595:    if (!srch || srch.closed || srchmode!=mode) {
  596:       srchopen(mode);
  597:    }
  598:    srch.focus();
  599: }
  600: 
  601: // -------------------------------------------------------- Do idx status check
  602: function idxcheck(mode) {
  603:    if (!idx || idx.closed || idxmode!=mode) {
  604:       idxopen(mode);
  605:    }
  606:    idx.focus();
  607: }
  608: 
  609: 
  610:     var editbrowser;
  611:     function openbrowser(formname,elementname,only,omit) {
  612:         var url = '/res/?';
  613:         if (editbrowser == null) {
  614:             url += 'launch=1&';
  615:         }
  616:         url += 'catalogmode=interactive&';
  617:         url += 'mode=edit&';
  618:         url += 'form=' + formname + '&';
  619:         if (only != null) {
  620:             url += 'only=' + only + '&';
  621:         } 
  622:         if (omit != null) {
  623:             url += 'omit=' + omit + '&';
  624:         }
  625:         url += 'element=' + elementname + '';
  626:         var title = 'Browser';
  627:         var options = 'scrollbars=1,resizable=1,menubar=0';
  628:         options += ',width=700,height=600';
  629:         editbrowser = open(url,title,options,'1');
  630:         editbrowser.focus();
  631:     }
  632: ENDSCRIPT
  633: }
  634: # ------------------------------------------------------- Simple edit processor
  635: 
  636: sub smpedt {
  637:    my ($r,$url,$errtext)=@_;
  638:    my $buttons=&buttons(2);
  639:    my $tmpfn=&Apache::lonnet::filelocation('',$url).'.tmp';
  640:    my $targetmsg='';
  641:    if ($ENV{'form.save'}) {
  642:        $targetmsg='<b>Saving ...</b><br>';
  643:        copy($tmpfn,&Apache::lonnet::filelocation('',$url));
  644:        unlink($tmpfn);
  645:        my ($errtext,$fatal)=
  646:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  647:    }
  648:    if ($ENV{'form.revert'}) {
  649:        $targetmsg='<b>Reverting ...</b><br>';
  650:        unlink($tmpfn);
  651:        my ($errtext,$fatal)=
  652:                            &mapread(&Apache::lonnet::filelocation('',$url),'');
  653:    }
  654:    if (-e $tmpfn) {
  655:       $targetmsg=
  656:         '<b><font color="red">You are working with an unsaved version of your map.</font></b><br>';
  657:       my ($errtext,$fatal)=&mapread($tmpfn,'');
  658:    }
  659: # ---------------------------------------------------------- Process form input
  660: 
  661:    my @importselect=();
  662:    my @targetselect=();
  663:    undef @importselect;
  664:    undef @targetselect;
  665:    if (defined($ENV{'form.importsel'})) {
  666:        if (ref($ENV{'form.importsel'})) {
  667: 	   @importselect=sort(@{$ENV{'form.importsel'}});
  668:        } else {
  669:            @importselect=($ENV{'form.importsel'});
  670:        }
  671:    }
  672:    if (defined($ENV{'form.target'})) {
  673:        if (ref($ENV{'form.target'})) {
  674: 	   @targetselect=sort(@{$ENV{'form.target'}});
  675:        } else {
  676:            @targetselect=($ENV{'form.target'});
  677:        }
  678:    }
  679: # ============================================================ Process commands
  680: 
  681:    my $targetdetail=$ENV{'form.targetdetail'};
  682:    my $importdetail=$ENV{'form.curimpdetail'};
  683: 
  684: # ---------------------------------------------------- Importing from groupsort
  685:    if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
  686: 
  687:        $importdetail='';
  688:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  689: 
  690:        my $lastsel;
  691: 
  692:        if (defined($importselect[-1])) {
  693: 	   $lastsel=$importselect[-1];
  694:        } else {
  695:            $lastsel=$#curimport;
  696:        }
  697: 
  698:        for (my $i=0;$i<=$lastsel;$i++) {
  699:            my ($name,$url)=split(/\=/,$curimport[$i]);
  700:            if ($url) {
  701:               $importdetail.='&'.$name.'='.$url;
  702: 	   }
  703:        }
  704: 
  705:       $importdetail.='&'.$ENV{'form.importdetail'};
  706: 
  707:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  708:            my ($name,$url)=split(/\=/,$curimport[$i]);
  709:            if ($url) {
  710:               $importdetail.='&'.$name.'='.$url;
  711: 	  }
  712:        }
  713:        $importdetail=~s/\&+/\&/g;
  714:        $importdetail=~s/^\&//;
  715: 
  716: # ------------------------------------------------------------------- Clear all
  717:    } elsif ($ENV{'form.clear'}) {
  718:        $importdetail='';
  719: # ------------------------------------------------------------ Discard selected
  720:    } elsif ($ENV{'form.discard'}) {
  721:        $importdetail='';
  722:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  723:        foreach (@importselect) {
  724: 	   $curimport[$_]='';
  725:        }
  726:        for (my $i=0;$i<=$#curimport;$i++) {
  727:            my ($name,$url)=split(/\=/,$curimport[$i]);
  728:            if ($url) {
  729:               $importdetail.='&'.$name.'='.$url;
  730: 	   }
  731:        }
  732: # --------------------------------------------------------- Loading another map
  733:    } elsif ($ENV{'form.loadmap'}) {
  734:        $importdetail='';
  735:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  736: 
  737:        my $lastsel;
  738: 
  739:        if (defined($importselect[-1])) {
  740: 	   $lastsel=$importselect[-1];
  741:        } else {
  742:            $lastsel=$#curimport;
  743:        }
  744: 
  745:        for (my $i=0;$i<=$lastsel;$i++) {
  746:            my ($name,$url)=split(/\=/,$curimport[$i]);
  747:            if ($url) {
  748:               $importdetail.='&'.$name.'='.$url;
  749: 	   }
  750:        }
  751: 
  752:        foreach (
  753:     &Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
  754: 	   my ($name,$url)=split(/\:/,$_);
  755:            if ($url) {
  756:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  757: 		 	         &Apache::lonnet::escape($url);
  758: 	  }
  759:        }
  760: 
  761:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  762:            my ($name,$url)=split(/\=/,$curimport[$i]);
  763:            if ($url) {
  764:               $importdetail.='&'.$name.'='.$url;
  765: 	  }
  766:        }
  767:        $importdetail=~s/\&+/\&/g;
  768:        $importdetail=~s/^\&//;
  769: 
  770: # ------------------------------------------------ Groupimport/search to target
  771:    } elsif ($ENV{'form.importdetail'}) {
  772:        my $lastsel;
  773:        if (defined($targetselect[-1])) {
  774: 	   $lastsel=$targetselect[-1];
  775:        } else {
  776:            $lastsel=$#order+1;
  777:        }
  778:        &pastetarget($lastsel,split(/\&/,$ENV{'form.importdetail'}));
  779:        &storemap(&Apache::lonnet::filelocation('',$url));
  780: # ------------------------------------------------------------------------- Cut
  781:    } elsif (($ENV{'form.cut'}) || ($ENV{'form.copy'})) {
  782:        $importdetail='';
  783:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  784: 
  785:        my $lastsel;
  786: 
  787:        if (defined($importselect[-1])) {
  788: 	   $lastsel=$importselect[-1];
  789:        } else {
  790:            $lastsel=$#curimport;
  791:        }
  792: 
  793:        for (my $i=0;$i<=$lastsel;$i++) {
  794:            my ($name,$url)=split(/\=/,$curimport[$i]);
  795:            if ($url) {
  796:               $importdetail.='&'.$name.'='.$url;
  797: 	   }
  798:        }
  799: 
  800:        foreach (@targetselect) {
  801: 	   my ($name,$url)=split(/\:/,$resources[$order[$_-1]]);
  802:            if ($url) {
  803:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  804: 		 	         &Apache::lonnet::escape($url);
  805: 	  }
  806:        }
  807: 
  808:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  809:            my ($name,$url)=split(/\=/,$curimport[$i]);
  810:            if ($url) {
  811:               $importdetail.='&'.$name.'='.$url;
  812: 	  }
  813:        }
  814:        $importdetail=~s/\&+/\&/g;
  815:        $importdetail=~s/^\&//;
  816: 
  817:        if ($ENV{'form.cut'}) {
  818:            my @neworder=();
  819:            for (my $i=0;$i<=$#order;$i++) {
  820:                my $include=1;
  821:                foreach (@targetselect) {
  822: 		   if ($_-1==$i) { $include=0; }
  823:                }
  824:                if ($include) { $neworder[$#neworder+1]=$order[$i]; }
  825:            }
  826:            @order=@neworder;
  827:            &storemap(&Apache::lonnet::filelocation('',$url));      
  828:        }
  829: 
  830: # ----------------------------------------------------------------------- Paste
  831:    } elsif ($ENV{'form.paste'}) {
  832:        my $lastsel;
  833:        if (defined($targetselect[-1])) {
  834: 	   $lastsel=$targetselect[-1];
  835:        } else {
  836:            $lastsel=$#order+1;
  837:        }
  838:        my @newsequence;
  839:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  840:        foreach (@importselect) {
  841:           $newsequence[$#newsequence+1]=$curimport[$_];
  842:        }
  843:        &pastetarget($lastsel,@newsequence);
  844:        &storemap(&Apache::lonnet::filelocation('',$url));
  845: # -------------------------------------------------------------------- Move up 
  846:    } elsif ($ENV{'form.moveup'}) {
  847:        foreach (sort @targetselect) {
  848:            if ($_-1>0) {
  849:               my $movethis=$order[$_-1];
  850:               $order[$_-1]=$order[$_-2];
  851:               $order[$_-2]=$movethis;
  852: 	  }
  853:        }
  854:        &storemap(&Apache::lonnet::filelocation('',$url));
  855: # ------------------------------------------------------------------ Move down
  856:    } elsif ($ENV{'form.movedown'}) {
  857:        foreach (reverse sort @targetselect) {
  858:            if ($_-1<$#order) {
  859:               my $movethis=$order[$_-1];
  860:               $order[$_-1]=$order[$_];
  861:               $order[$_]=$movethis;
  862: 	   }
  863:        }
  864:        &storemap(&Apache::lonnet::filelocation('',$url));
  865: # --------------------------------------------------------------------- Rename
  866:    } elsif ($ENV{'form.renameres'}) {
  867:        my $residx=$Apache::lonratedt::order[$ENV{'form.renameidx'}-1];
  868:        my ($name,@resrest)=split(/\:/,$Apache::lonratedt::resources[$residx]);
  869:        $name=$ENV{'form.renametitle'};
  870:        $name=~s/\:/\&colon\;/g;
  871:        $Apache::lonratedt::resources[$residx]=$name.':'.join(':',@resrest);
  872:        &storemap(&Apache::lonnet::filelocation('',$url));
  873:    }
  874: # ------------------------------------------------------------ Assemble windows
  875:    
  876:    my $idx=-1;
  877:    $importdetail='&'.$importdetail;
  878:    $importdetail=~s/^\&+/\&/;
  879:    my $importwindow=
  880:        '<option value="-1"> ---- Import and Paste Area ---- </option>'.
  881:      join("\n",map {
  882:        $idx++;
  883:        if ($_) { 
  884:           my ($name,$url)=split(/\=/,$_);
  885:           unless ($name) { $name=(split(/\//,$url))[-1]; }
  886:           unless ($name) { $name='EMPTY'; }
  887:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
  888:                                     '</option>';
  889:       }
  890:    } split(/\&/,$importdetail));
  891: 
  892:    $idx=0;
  893:    $targetdetail='';
  894:    my $targetwindow=       
  895:        '<option value="0"> ------- Target Edit Map ------- </option>'.
  896:      join("\n",map { 
  897:        my ($name,$url)=split(/\:/,$resources[$_]);
  898:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
  899:        unless ($name) { $name='EMPTY'; }
  900:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
  901: 	                  &Apache::lonnet::escape($url);
  902:        $idx++;
  903:        $name=~s/\&colon;/\:/g;
  904:        '<option value="'.$idx.'">'.$name.'</option>';
  905:    } @order);
  906: 
  907: # ----------------------------------------------------- Start simple RAT screen
  908:    my $editscript=&editscript('simple');
  909:    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
  910:    $r->print(<<ENDSMPHEAD);
  911: <html>
  912: <head>
  913: <script>
  914: 
  915:    $editscript
  916: 
  917:    function openview(entry) {
  918:        var url=unescape((entry.split('='))[1]);
  919:        var parts=new Array;
  920:        parts=url.split('&colon;');
  921:        url=parts.join(':');
  922:        if (url) { open(url,'cat'); }
  923:    }
  924: 
  925:    function viewtarget() {
  926:        openview((document.forms.simpleedit.targetdetail.value.split('&'))
  927:                 [document.forms.simpleedit.target.selectedIndex]);
  928:    }
  929: 
  930:    function viewimport() {
  931:        openview((document.forms.simpleedit.curimpdetail.value.split('&'))
  932:                 [document.forms.simpleedit.importsel.selectedIndex]);
  933:    }
  934: 
  935:    function renametarget() {
  936:        var selidx=document.forms.simpleedit.target.selectedIndex;
  937:        var entry=(document.forms.simpleedit.targetdetail.value.split('&'))
  938:                 [selidx];
  939:        var oldname=unescape((entry.split('='))[0]);
  940:        var nameparts=oldname.split('&colon;');
  941:        oldname=unescape(nameparts.join(':'));
  942:        nameparts=oldname.split('&#34;');
  943:        oldname=unescape(nameparts.join('"'));
  944:        nameparts=oldname.split('&#39;');
  945:        oldname=unescape(nameparts.join("'"));
  946:        newtitle=prompt('New Title',oldname);
  947:        if (newtitle) {
  948:            document.forms.simpleedit.renameres.value=1;
  949:            document.forms.simpleedit.renameidx.value=selidx;
  950:            document.forms.simpleedit.renametitle.value=newtitle;
  951: 	   document.forms.simpleedit.submit();
  952:        }
  953:    }
  954: 
  955: </script>
  956: </head>                 
  957: $bodytag
  958: $buttons
  959: <font color=red>$errtext</font>
  960: <form name=simpleedit method=post>
  961: <input type=hidden name=forcesmp value=1>
  962: <input type=hidden name=renameres value=0>
  963: <input type=hidden name=renametitle value=''>
  964: <input type=hidden name=renameidx value=0>
  965: <table>
  966:     <tr><th width="40%">Temporary Assembly Workspace</th>
  967: <th>&nbsp;</th>
  968: <th width="40%">File: $url</th></tr>
  969: <tr><td bgcolor="#FFFFCC">
  970: <input type=button onClick="javascript:groupsearch()" value="Search">
  971: <input type=button onClick="javascript:groupimport();" value="Import">
  972: after selected
  973: <hr>
  974: <input type=text size=20 name=importmap>
  975: <input type=button 
  976: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
  977: value="Select Map"><input type=submit name=loadmap value="Load Map"><hr>
  978: <input type=submit name="discard" value="Discard Selected">
  979: <input type=submit name="clear" value="Clear All">
  980: <input type=button onClick="javascript:viewimport()" value="View">
  981: 
  982:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
  983: 
  984: <input type=button onClick=
  985: "javascript:impfortarget.value=1;groupsearch()" value="Search">
  986: <input type=button onClick=
  987: "javascript:impfortarget.value=1;groupimport();" value="Import">
  988: after selected
  989: <hr>
  990: <input type=submit name="moveup" value="Move Up">
  991: <input type=submit name="movedown" value="Move Down">
  992: <input type=button onClick="javascript:renametarget()" value="Rename">
  993: <hr>$targetmsg
  994: <input type=submit name="revert" value="Revert to Last Saved">
  995: <input type=submit name="save" value="Save">
  996: <input type=button onClick="javascript:viewtarget()" value="View">
  997: </td></tr>
  998: 
  999: <tr><td bgcolor="#FFFFCC"><select name="importsel" size=10 multiple>
 1000: $importwindow
 1001: </select>
 1002: </td>
 1003: <td bgcolor="#FFFFAA" align="center">
 1004: Cut selected<br>
 1005: <input type=submit name=cut value='<<<'><p>
 1006: <hr>
 1007: Copy selected<br>
 1008: <input type=submit name=copy value='<--'><p>
 1009: <hr>
 1010: Paste after selected<br>
 1011: <input type=submit name=paste value='-->'>
 1012: </td>
 1013: <td bgcolor="#FFFFCC"><select name="target" size=10 multiple>
 1014: $targetwindow
 1015: </select>
 1016: </table>
 1017: <input type=hidden name=importdetail value="">
 1018: <input type=hidden name=curimpdetail value="$importdetail">
 1019: <input type=hidden name=targetdetail value="$targetdetail">
 1020: <input type=hidden name=impfortarget value="0">
 1021: </form>
 1022: </body></html>
 1023: ENDSMPHEAD
 1024: }
 1025: 
 1026: # ----------------------------------------------------------------- No such dir
 1027: sub nodir {
 1028:    my ($r,$dir)=@_;
 1029:    $dir=~s/^\/home\/\w+\/public\_html//;
 1030:    $r->print(<<ENDNODIR);
 1031: <html>
 1032: <body bgcolor='#FFFFFF'>
 1033: <h1>No such directory: $dir</h1>
 1034: </body>
 1035: </html>
 1036: ENDNODIR
 1037: }
 1038: 
 1039: # ---------------------------------------------------------------- View Handler
 1040: 
 1041: sub viewmap {
 1042:     my ($r,$url,$adv,$errtext)=@_;
 1043:     $r->print('<html>'.
 1044:      &Apache::loncommon::bodytag('Edit Content of a Map').
 1045:      &buttons($adv));
 1046:     if ($errtext) {
 1047: 	$r->print($errtext.'<hr>');
 1048:     }
 1049:     my $idx=0;
 1050:     $r->print('<h1>'.$url.'</h1>');
 1051:     if ($adv) {
 1052: 	$r->print('<p><b><font color="red">Map contents are not shown in order.</font></b></p><br />');
 1053:     }
 1054:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
 1055: 	if (defined($_)) {
 1056:             $idx++;
 1057: 	    my ($title,$url)=split(/\:/,$_);
 1058:             $title=~s/\&colon\;/\:/g;
 1059:             $url=~s/\&colon\;/\:/g;
 1060:             unless ($title) { $title=(split(/\//,$url))[-1] };
 1061:             unless ($title) { $title='<i>Empty</i>'; }
 1062:             if ($url) {
 1063: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
 1064:             }
 1065:             $r->print(&Apache::lonratsrv::qtescape($title));
 1066:             if ($url) { $r->print('</a>'); }
 1067: 	    $r->print('<br>');
 1068:         }
 1069:     }
 1070:     $r->print('</body></html>');
 1071: }
 1072: 
 1073: # ================================================================ Main Handler
 1074: 
 1075: sub handler {
 1076:   my $r=shift;
 1077:   $r->content_type('text/html');
 1078:   $r->send_http_header;
 1079: 
 1080:   return OK if $r->header_only;
 1081: 
 1082:   my $url=$r->uri;
 1083:   my $fn=&Apache::lonnet::filelocation('',$url);
 1084: 
 1085:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
 1086:   unless (-e $dir) {
 1087:       &nodir($r,$dir);
 1088:       return OK;
 1089:   }
 1090: 
 1091: # ------------------------------------------- Determine which tools can be used
 1092:   my $adv=0;
 1093: 
 1094:   unless ($ENV{'form.forcesmp'}) {
 1095:      if ($ENV{'form.forceadv'}) {
 1096:         $adv=1;
 1097:      } elsif (my $fh=Apache::File->new($fn)) {
 1098: 	 my $allmap=join('',<$fh>);
 1099:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
 1100:      }
 1101:   }
 1102: 
 1103:   my $errtext='';
 1104:   my $fatal=0;
 1105: 
 1106: # -------------------------------------------------------------------- Load map
 1107:   ($errtext,$fatal)=&mapread($fn,$errtext);
 1108: 
 1109:   if ($fatal==1) { $adv=1; }
 1110: 
 1111: # ----------------------------------- adv==1 now means "graphical MUST be used"
 1112: 
 1113:   if ($ENV{'form.forceadv'}) {
 1114:       &ratedt($r,$url);
 1115:   } elsif ($ENV{'form.forcesmp'}) {
 1116:       &smpedt($r,$url,$errtext);
 1117:   } else {
 1118:       &viewmap($r,$url,$adv,$errtext);
 1119:   }
 1120:   return OK;
 1121: }
 1122: 
 1123: 1;
 1124: __END__
 1125: 
 1126: 
 1127: 
 1128: 
 1129: 
 1130: 
 1131: 

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