File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.16: download - view: text, annotated - select for diffs
Tue May 14 15:19:13 2002 UTC (21 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Group import/search on target side, start reading other maps

    1: # The LearningOnline Network with CAPA
    2: # Edit Handler for RAT Maps
    3: #
    4: # $Id: lonratedt.pm,v 1.16 2002/05/14 15:19:13 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (TeX Content Handler
   29: #
   30: # 05/29/00,05/30 Gerd Kortemeyer)
   31: # 7/1,6/30 Gerd Kortemeyer
   32: 
   33: package Apache::lonratedt;
   34: 
   35: use strict;
   36: use Apache::Constants qw(:common);
   37: use Apache::lonnet;
   38: use Apache::lonratsrv;
   39: 
   40: my @order=();
   41: my @resources=();
   42: 
   43: 
   44: # Mapread read maps into global arrays @links and @resources, determines status
   45: # sets @order - pointer to resources in right order
   46: # sets @resources - array with the resources with correct idx
   47: #
   48: sub mapread {
   49:     my $fn=shift;
   50: 
   51:     my @links;
   52:     undef @links;
   53:     undef @resources;
   54:     undef @order;
   55: 
   56:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
   57:     if ($errtext) { return ($errtext,2); }
   58: 
   59: # -------------------------------------------------------------------- Read map
   60:     foreach (split(/\<\&\>/,$outtext)) {
   61: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
   62:         if ($command eq 'objcont') {
   63: 	    $resources[$number]=$content;
   64:         }
   65:         if ($command eq 'objlinks') {
   66:             $links[$number]=$content;
   67:         }
   68:     }
   69: # ------------------------------------------------------- Is this a linear map?
   70:     my @starters=();
   71:     my @endings=();
   72:     undef @starters;
   73:     undef @endings;
   74: 
   75:     foreach (@links) {
   76:         if (defined($_)) {
   77: 	    my ($start,$end,$cond)=split(/\:/,$_);
   78:             if ((defined($starters[$start])) || (defined($endings[$end]))) { 
   79: 		return
   80:                  ('Map has branchings. Use advanced editor.',1);
   81:             }
   82: 	    $starters[$start]=1;
   83: 	    $endings[$end]=1;
   84: 	    if ($cond) {
   85: 		return
   86:                  ('Map has conditions. Use advanced editor.',1);
   87:             }
   88: 	}
   89: 
   90:     }
   91:     for (my $i=0; $i<=$#resources; $i++) {
   92:         if (defined($resources[$i])) {
   93: 	    unless (($starters[$i]) || ($endings[$i])) {
   94:                 return
   95: 		 ('Map has unconnected resources. Use advanced editor.',1);
   96:             }
   97:         }
   98:     }
   99: 
  100: # -------------------------------------------------- This is a linear map, sort
  101: 
  102:     my $startidx=0;
  103:     my $endidx=0;
  104:     for (my $i=0; $i<=$#resources; $i++) {
  105:         if (defined($resources[$i])) {
  106:             my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
  107: 	    if ($type eq 'start') { $startidx=$i; }
  108:             if ($type eq 'finish') { $endidx=$i; }
  109:         }
  110:     }
  111:     my $k=0;
  112:     my $currentidx=$startidx;
  113:     $order[$k]=$currentidx;
  114:     for (my $i=0; $i<=$#resources; $i++) {
  115:         foreach (@links) {
  116: 	    my ($start,$end)=split(/\:/,$_);
  117:             if ($start==$currentidx) {
  118: 		$currentidx=$end;
  119:                 $k++;
  120:                 $order[$k]=$currentidx;
  121:                 last;
  122:             }
  123:         }
  124:         if ($currentidx==$endidx) { last; }
  125:     }
  126:     return $errtext;
  127: }
  128: 
  129: # ---------------------------------------------- Read a map as well as possible
  130: 
  131: sub attemptread {
  132:     my $fn=shift;
  133: 
  134:     my @links;
  135:     undef @links;
  136:     my @theseres;
  137:     undef @theseres;
  138: 
  139:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
  140:     if ($errtext) { return @theseres }
  141: 
  142: # -------------------------------------------------------------------- Read map
  143:     foreach (split(/\<\&\>/,$outtext)) {
  144: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
  145:         if ($command eq 'objcont') {
  146: 	    $theseres[$number]=$content;
  147:         }
  148:         if ($command eq 'objlinks') {
  149:             $links[$number]=$content;
  150:         }
  151:     }
  152: 
  153: # --------------------------------------------------------------- Sort, sort of
  154: 
  155:     my $startidx=0;
  156:     my $endidx=0;
  157:     for (my $i=0; $i<=$#theseres; $i++) {
  158:         if (defined($theseres[$i])) {
  159:             my ($title,$url,$ext,$type)=split(/\:/,$theseres[$i]);
  160: 	    if ($type eq 'start') { $startidx=$i; }
  161:             if ($type eq 'finish') { $endidx=$i; }
  162:         }
  163:     }
  164: 
  165: 
  166:     return @theseres;
  167: 
  168: }
  169: 
  170: 
  171: # --------------------------------------------------------- Build up RAT screen
  172: sub ratedt {
  173:   my ($r,$url)=@_;
  174:   $r->print(<<ENDDOCUMENT);
  175: 
  176: <html>
  177: <head>
  178: <script language="JavaScript">
  179:     var flag=0;
  180: </script>
  181: </head>
  182: <frameset rows="1,50,*" border=0>
  183: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
  184: <frame name=code src="/adm/rat/code.html">
  185: <frame name=mapout src="/adm/rat/map.html">
  186: </frameset>
  187: </html>
  188: 
  189: ENDDOCUMENT
  190: }
  191: 
  192: # ---------------------------------------------------------------- Make buttons
  193: 
  194: sub buttons {
  195:     my $adv=shift;
  196:     my $output='<form method=post>';     
  197:     if ($adv==1) {
  198: 	$output.='<input type=submit name=forceadv value="Edit">';
  199:     } else {
  200:         unless ($adv==2) {
  201:            $output.='<input type=submit name=forcesmp value="Simple Edit">';
  202:         }
  203: 	$output.='<input type=submit name=forceadv value="Advanced Edit">';
  204:     }
  205:     return $output.'</form><hr>';
  206: }
  207: 
  208: sub smpedt {
  209:    my ($r,$errtext)=@_;
  210:    my $buttons=&buttons(2);
  211: 
  212: # ---------------------------------------------------------- Process form input
  213: 
  214:    my @importselect=();
  215:    my @targetselect=();
  216:    undef @importselect;
  217:    undef @targetselect;
  218:    if (defined($ENV{'form.import'})) {
  219:        if (ref($ENV{'form.import'})) {
  220: 	   @importselect=sort($ENV->{'form.import'});
  221:        } else {
  222:            @importselect=($ENV{'form.import'});
  223:        }
  224:    }
  225:    if (defined($ENV{'form.target'})) {
  226:        if (ref($ENV{'form.target'})) {
  227: 	   @targetselect=sort($ENV->{'form.target'});
  228:        } else {
  229:            @targetselect=($ENV{'form.target'});
  230:        }
  231:    }
  232: # ============================================================ Process commands
  233: 
  234:    my $targetdetail=$ENV{'form.targetdetail'};
  235:    my $importdetail=$ENV{'form.curimpdetail'};
  236: 
  237: # ---------------------------------------------------- Importing from groupsort
  238:    if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
  239: 
  240:        $importdetail='';
  241:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  242: 
  243:        my $lastsel;
  244: 
  245:        if (defined($importselect[-1])) {
  246: 	   $lastsel=$importselect[-1];
  247:        } else {
  248:            $lastsel=$#curimport;
  249:        }
  250: 
  251:        for (my $i=0;$i<=$lastsel;$i++) {
  252:            my ($name,$url)=split(/\=/,$curimport[$i]);
  253:            if ($url) {
  254:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  255: 		 	         &Apache::lonnet::escape($url);
  256: 	   }
  257:        }
  258: 
  259:       $importdetail.='&'.$ENV{'form.importdetail'};
  260: 
  261:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
  262:            my ($name,$url)=split(/\=/,$curimport[$i]);
  263:            if ($url) {
  264:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  265: 		 	         &Apache::lonnet::escape($url);
  266: 	  }
  267:        }
  268:        $importdetail=~s/\&+/\&/g;
  269:        $importdetail=~s/^\&//;
  270: 
  271: # ------------------------------------------------------------------- Clear all
  272:    } elsif ($ENV{'form.clear'}) {
  273:        $importdetail='';
  274: # ------------------------------------------------------------ Discard selected
  275:    } elsif ($ENV{'form.discard'}) {
  276:        $importdetail='';
  277:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
  278:        foreach (@importselect) {
  279: 	   $curimport[$_]='';
  280:        }
  281:        for (my $i=0;$i<=$#curimport;$i++) {
  282:            my ($name,$url)=split(/\=/,$curimport[$i]);
  283:            if ($url) {
  284:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
  285: 		 	         &Apache::lonnet::escape($url);
  286: 	   }
  287:        }
  288: # ---------------------------
  289:    }
  290: 
  291: # ------------------------------------------------------------ Assemble windows
  292: 
  293:    my $idx=-1;
  294:    my $importwindow=join("\n",map {
  295:        $idx++;
  296:        if ($_) { 
  297:           my ($name,$url)=split(/\=/,$_);
  298:           unless ($name) { $name=(split(/\//,$url))[-1]; }
  299:           unless ($name) { $name='EMPTY'; }
  300:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
  301:                                     '</option>';
  302:       }
  303:    } split(/\&/,$importdetail));
  304: 
  305:    $idx=0;
  306:    my $targetwindow=join("\n",map { 
  307:        my ($name,$url)=split(/\:/,$resources[$_]);
  308:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
  309:        unless ($name) { $name='EMPTY'; }
  310:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
  311: 	                  &Apache::lonnet::escape($url);
  312:        $idx++;
  313:        '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
  314:    } @order);
  315: 
  316: # ----------------------------------------------------- Start simple RAT screen
  317:    $r->print(<<ENDSMPHEAD);
  318: <html>
  319: <head>
  320: <script>
  321: var srch;
  322: var srchflag=-1; // 1 means currently open
  323:                  // 0 means closed (but has been open)
  324:                  // -1 means never yet opened/defined
  325: var srchmode='';
  326: 
  327: var idx;
  328: var idxflag=-1; // 1 means currently open
  329:                  // 0 means closed (but has been open)
  330:                  // -1 means never yet opened/defined
  331: var idxmode='';
  332: 
  333: // ------------------------------------------------------ Clears indexer window
  334: function idxclear() {
  335:   idx.document.clear();
  336: }
  337: 
  338: // ------------------------------------------------------- Clears search window
  339: function srchclear() {
  340:   srch.document.clear();
  341: }
  342: 
  343: // ------------------------------------------------------ Closes indexer window
  344: function idxclose() {
  345:   if (idx && !idx.closed) {
  346:     idxflag=0;
  347:     idx.close();
  348:   }
  349: }
  350: 
  351: // ------------------------------------------------------- Closes search window
  352: function srchclose() {
  353:   if (srch && !srch.closed) {
  354:     srchflag=0;
  355:     srch.close();
  356:   }
  357: }
  358: 
  359: // -------------------------------------------------------- Open indexer window
  360: function idxopen(mode) {
  361:    var options="scrollbars=1,resizable=1,menubar=0";
  362:    idxmode=mode;
  363:    idxflag=1;
  364:    idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
  365:    idx.focus();
  366: }
  367: 
  368: // --------------------------------------------------------- Open search window
  369: function srchopen(mode) {
  370:    var options="scrollbars=1,resizable=1,menubar=0";
  371:    srchmode=mode;
  372:    srchflag=1;
  373:    srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
  374:    srch.focus();
  375: }
  376: // ----------------------------------------------------- launch indexer browser
  377: function groupsearch() {
  378:    srchcheck('groupsearch');
  379: }
  380: 
  381: function groupimport() {
  382:    idxcheck('groupimport');
  383: }
  384: // ------------------------------------------------------- Do srch status check
  385: function srchcheck(mode) {
  386:    if (!srch || srch.closed || srchmode!=mode) {
  387:       srchopen(mode);
  388:    }
  389:    srch.focus();
  390: }
  391: 
  392: // -------------------------------------------------------- Do idx status check
  393: function idxcheck(mode) {
  394:    if (!idx || idx.closed || idxmode!=mode) {
  395:       idxopen(mode);
  396:    }
  397:    idx.focus();
  398: }
  399: 
  400: 
  401:     var editbrowser;
  402:     function openbrowser(formname,elementname,only,omit) {
  403:         var url = '/res/?';
  404:         if (editbrowser == null) {
  405:             url += 'launch=1&';
  406:         }
  407:         url += 'catalogmode=interactive&';
  408:         url += 'mode=edit&';
  409:         url += 'form=' + formname + '&';
  410:         if (only != null) {
  411:             url += 'only=' + only + '&';
  412:         } 
  413:         if (omit != null) {
  414:             url += 'omit=' + omit + '&';
  415:         }
  416:         url += 'element=' + elementname + '';
  417:         var title = 'Browser';
  418:         var options = 'scrollbars=1,resizable=1,menubar=0';
  419:         options += ',width=700,height=600';
  420:         editbrowser = open(url,title,options,'1');
  421:         editbrowser.focus();
  422:     }
  423: </script>
  424: </head>                 
  425: <body bgcolor='#FFFFFF'>
  426: $buttons
  427: <font color=red>$errtext</font>
  428: <form name=simpleedit method=post>
  429: <input type=hidden name=forcesmp value=1>
  430: <table>
  431:     <tr><th width="40%">Import</th>
  432: <th>&nbsp;</th>
  433: <th width="40%">Target</th></tr>
  434: <tr><td bgcolor="#FFFFCC">
  435: <input type=button onClick="javascript:groupsearch()" value="Group Search">
  436: <input type=button onClick="javascript:groupimport();" value="Group Import">
  437: after selected
  438: <hr>
  439: <input type=text size=20 name=importmap>
  440: <input type=button 
  441: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
  442: value="Browse"><input type=submit name=loadmap value="Load Map"><hr>
  443: <input type=submit name="discard" value="Discard Selected">
  444: <input type=submit name="clear" value="Clear All">
  445: <input type=button onClick="javascript:viewimport()" value="View">
  446: 
  447:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
  448: 
  449: <input type=button onClick=
  450: "javascript:impfortarget.value=1;groupsearch()" value="Group Search">
  451: <input type=button onClick=
  452: "javascript:impfortarget.value=1;groupimport();" value="Group Import">
  453: after selected
  454: <hr><input type=button onClick="javascript:viewtarget()" value="View">
  455: </td></tr>
  456: 
  457: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
  458: $importwindow
  459: </select>
  460: </td>
  461: <td bgcolor="#FFFFAA" align="center">
  462: Cut selected<br>
  463: <input type=submit name=cut value='<<<'><p>
  464: <hr>
  465: Paste after selected<br>
  466: <input type=submit name=paste value='>>>'>
  467: </td>
  468: <td bgcolor="#FFFFCC"><select name="target" multiple>
  469: $targetwindow
  470: </select>
  471: </table>
  472: <input type=hidden name=importdetail value="">
  473: <input type=hidden name=curimpdetail value="$importdetail">
  474: <input type=hidden name=targetdetail value="$targetdetail">
  475: <input type=hidden name=impfortarget value="0">
  476: </form>
  477: </body></html>
  478: ENDSMPHEAD
  479: }
  480: 
  481: # ----------------------------------------------------------------- No such dir
  482: sub nodir {
  483:    my ($r,$dir)=@_;
  484:    $dir=~s/^\/home\/\w+\/public\_html//;
  485:    $r->print(<<ENDNODIR);
  486: <html>
  487: <body bgcolor='#FFFFFF'>
  488: <h1>No such directory: $dir</h1>
  489: </body>
  490: </html>
  491: ENDNODIR
  492: }
  493: 
  494: # ---------------------------------------------------------------- View Handler
  495: 
  496: sub viewmap {
  497:     my ($r,$adv,$errtext)=@_;
  498:     $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
  499:     if ($errtext) {
  500: 	$r->print($errtext.'<hr>');
  501:     }
  502:     foreach (@resources) {
  503: 	if (defined($_)) {
  504: 	    my ($title,$url)=split(/\:/,$_);
  505:             $title=~s/\&colon\;/\:/g;
  506:             $url=~s/\&colon\;/\:/g;
  507:             unless ($title) { $title=(split(/\//,$url))[-1] };
  508:             unless ($title) { $title='<i>Empty</i>'; }
  509:             if ($url) {
  510: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
  511:             }
  512:             $r->print(&Apache::lonratsrv::qtescape($title));
  513:             if ($url) { $r->print('</a>'); }
  514:             $r->print('<br>');
  515:         }
  516:     }
  517:     $r->print('</body></html>');
  518: }
  519: 
  520: # ================================================================ Main Handler
  521: 
  522: sub handler {
  523:   my $r=shift;
  524:   $r->content_type('text/html');
  525:   $r->send_http_header;
  526: 
  527:   return OK if $r->header_only;
  528: 
  529:   my $url=$r->uri;
  530:   my $fn=&Apache::lonnet::filelocation('',$url);
  531: 
  532:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
  533:   unless (-e $dir) {
  534:       &nodir($r,$dir);
  535:       return OK;
  536:   }
  537: 
  538: # ------------------------------------------- Determine which tools can be used
  539:   my $adv=0;
  540: 
  541:   unless ($ENV{'form.forcesmp'}) {
  542:      if ($ENV{'form.forceadv'}) {
  543:         $adv=1;
  544:      } elsif (my $fh=Apache::File->new($fn)) {
  545: 	 my $allmap=join('',<$fh>);
  546:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
  547:      }
  548:   }
  549: 
  550:   my $errtext='';
  551:   my $fatal=0;
  552: 
  553: # -------------------------------------------------------------------- Load map
  554:   ($errtext,$fatal)=&mapread($fn,$errtext);
  555: 
  556:   if ($fatal==1) { $adv=1; }
  557: 
  558: # ----------------------------------- adv==1 now means "graphical MUST be used"
  559: 
  560:   if ($ENV{'form.forceadv'}) {
  561:       &ratedt($r,$url);
  562:   } elsif ($ENV{'form.forcesmp'}) {
  563:       &smpedt($r,$errtext);
  564:   } else {
  565:       &viewmap($r,$adv,$errtext);
  566:   }
  567:   return OK;
  568: }
  569: 
  570: 1;
  571: __END__
  572: 
  573: 
  574: 
  575: 
  576: 
  577: 
  578: 

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