File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.56: download - view: text, annotated - select for diffs
Sat Apr 24 15:37:30 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
Routines to selectively get and set certain parameters

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

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