Annotation of rat/lonratedt.pm, revision 1.15

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Edit Handler for RAT Maps
1.5       www         3: #
1.15    ! www         4: # $Id: lonratedt.pm,v 1.14 2002/05/13 19:38:32 www Exp $
1.5       www         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: #
1.1       www        28: # (TeX Content Handler
                     29: #
                     30: # 05/29/00,05/30 Gerd Kortemeyer)
1.4       www        31: # 7/1,6/30 Gerd Kortemeyer
1.1       www        32: 
                     33: package Apache::lonratedt;
                     34: 
                     35: use strict;
                     36: use Apache::Constants qw(:common);
1.3       www        37: use Apache::lonnet;
1.7       www        38: use Apache::lonratsrv;
1.1       www        39: 
1.10      www        40: my @order=();
1.8       www        41: my @resources=();
                     42: 
                     43: 
                     44: # Mapread read maps into global arrays @links and @resources, determines status
1.10      www        45: # sets @order - pointer to resources in right order
                     46: # sets @resources - array with the resources with correct idx
                     47: #
1.8       www        48: sub mapread {
                     49:     my $fn=shift;
                     50: 
1.10      www        51:     my @links;
1.8       www        52:     undef @links;
                     53:     undef @resources;
1.10      www        54:     undef @order;
1.8       www        55: 
                     56:     my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
                     57:     if ($errtext) { return ($errtext,2); }
                     58: 
1.9       www        59: # -------------------------------------------------------------------- Read map
1.8       www        60:     foreach (split(/\<\&\>/,$outtext)) {
1.9       www        61: 	my ($command,$number,$content)=split(/\<\:\>/,$_);
1.8       www        62:         if ($command eq 'objcont') {
1.9       www        63: 	    $resources[$number]=$content;
1.8       www        64:         }
                     65:         if ($command eq 'objlinks') {
1.9       www        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]))) { 
1.8       www        79: 		return
1.10      www        80:                  ('Map has branchings. Use advanced editor.',1);
1.8       www        81:             }
1.9       www        82: 	    $starters[$start]=1;
                     83: 	    $endings[$end]=1;
                     84: 	    if ($cond) {
1.8       www        85: 		return
1.10      www        86:                  ('Map has conditions. Use advanced editor.',1);
1.8       www        87:             }
                     88: 	}
                     89: 
                     90:     }
1.10      www        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:     }
1.8       www       126:     return $errtext;
                    127: }
                    128: 
1.3       www       129: # --------------------------------------------------------- Build up RAT screen
                    130: sub ratedt {
                    131:   my ($r,$url)=@_;
1.1       www       132:   $r->print(<<ENDDOCUMENT);
                    133: 
                    134: <html>
1.2       harris41  135: <head>
                    136: <script language="JavaScript">
                    137:     var flag=0;
                    138: </script>
                    139: </head>
1.1       www       140: <frameset rows="1,50,*" border=0>
                    141: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
                    142: <frame name=code src="/adm/rat/code.html">
                    143: <frame name=mapout src="/adm/rat/map.html">
                    144: </frameset>
                    145: </html>
                    146: 
                    147: ENDDOCUMENT
1.3       www       148: }
                    149: 
1.8       www       150: # ---------------------------------------------------------------- Make buttons
                    151: 
                    152: sub buttons {
                    153:     my $adv=shift;
                    154:     my $output='<form method=post>';     
                    155:     if ($adv==1) {
                    156: 	$output.='<input type=submit name=forceadv value="Edit">';
                    157:     } else {
                    158:         unless ($adv==2) {
                    159:            $output.='<input type=submit name=forcesmp value="Simple Edit">';
                    160:         }
                    161: 	$output.='<input type=submit name=forceadv value="Advanced Edit">';
                    162:     }
                    163:     return $output.'</form><hr>';
                    164: }
                    165: 
1.3       www       166: sub smpedt {
1.8       www       167:    my ($r,$errtext)=@_;
                    168:    my $buttons=&buttons(2);
1.12      www       169: 
                    170: # ---------------------------------------------------------- Process form input
                    171: 
                    172:    my @importselect=();
                    173:    my @targetselect=();
                    174:    undef @importselect;
                    175:    undef @targetselect;
                    176:    if (defined($ENV{'form.import'})) {
                    177:        if (ref($ENV{'form.import'})) {
1.13      www       178: 	   @importselect=sort($ENV->{'form.import'});
1.12      www       179:        } else {
                    180:            @importselect=($ENV{'form.import'});
                    181:        }
                    182:    }
                    183:    if (defined($ENV{'form.target'})) {
                    184:        if (ref($ENV{'form.target'})) {
1.13      www       185: 	   @targetselect=sort($ENV->{'form.target'});
1.12      www       186:        } else {
                    187:            @targetselect=($ENV{'form.target'});
                    188:        }
                    189:    }
1.13      www       190: # ============================================================ Process commands
1.12      www       191: 
1.14      www       192:    my $targetdetail=$ENV{'form.targetdetail'};
                    193:    my $importdetail=$ENV{'form.curimpdetail'};
1.13      www       194: 
                    195: # ---------------------------------------------------- Importing from groupsort
                    196:    if ($ENV{'form.importdetail'}) {
                    197: 
1.14      www       198:        $importdetail='';
1.13      www       199:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
                    200: 
                    201:        my $lastsel;
                    202: 
                    203:        if (defined($importselect[-1])) {
                    204: 	   $lastsel=$importselect[-1];
                    205:        } else {
                    206:            $lastsel=$#curimport;
                    207:        }
                    208: 
                    209:        for (my $i=0;$i<=$lastsel;$i++) {
                    210:            my ($name,$url)=split(/\=/,$curimport[$i]);
                    211:            if ($url) {
                    212:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
                    213: 		 	         &Apache::lonnet::escape($url);
                    214: 	   }
                    215:        }
                    216: 
                    217:       $importdetail.='&'.$ENV{'form.importdetail'};
                    218: 
                    219:        for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
                    220:            my ($name,$url)=split(/\=/,$curimport[$i]);
                    221:            if ($url) {
                    222:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
                    223: 		 	         &Apache::lonnet::escape($url);
                    224: 	  }
                    225:        }
                    226:        $importdetail=~s/\&+/\&/g;
                    227:        $importdetail=~s/^\&//;
                    228: 
1.14      www       229: # ------------------------------------------------------------------- Clear all
                    230:    } elsif ($ENV{'form.clear'}) {
                    231:        $importdetail='';
                    232: # ------------------------------------------------------------ Discard selected
                    233:    } elsif ($ENV{'form.discard'}) {
                    234:        $importdetail='';
                    235:        my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
                    236:        foreach (@importselect) {
                    237: 	   $curimport[$_]='';
                    238:        }
                    239:        for (my $i=0;$i<=$#curimport;$i++) {
                    240:            my ($name,$url)=split(/\=/,$curimport[$i]);
                    241:            if ($url) {
                    242:               $importdetail.='&'.&Apache::lonnet::escape($name).'='.
                    243: 		 	         &Apache::lonnet::escape($url);
                    244: 	   }
                    245:        }
                    246: # ---------------------------
1.13      www       247:    }
1.12      www       248: 
                    249: # ------------------------------------------------------------ Assemble windows
                    250: 
1.13      www       251:    my $idx=-1;
                    252:    my $importwindow=join("\n",map {
                    253:        $idx++;
                    254:        if ($_) { 
1.15    ! www       255:           my ($name,$url)=split(/\=/,$_);
        !           256:           unless ($name) { $name=(split(/\//,$url))[-1]; }
        !           257:           unless ($name) { $name='EMPTY'; }
1.13      www       258:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
                    259:                                     '</option>';
                    260:       }
                    261:    } split(/\&/,$importdetail));
1.12      www       262: 
1.13      www       263:    $idx=0;
1.11      www       264:    my $targetwindow=join("\n",map { 
1.13      www       265:        my ($name,$url)=split(/\:/,$resources[$_]);
1.15    ! www       266:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
        !           267:        unless ($name) { $name='EMPTY'; }
1.13      www       268:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
                    269: 	                  &Apache::lonnet::escape($url);
                    270:        $idx++;
                    271:        '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
1.11      www       272:    } @order);
                    273: 
1.8       www       274: # ----------------------------------------------------- Start simple RAT screen
1.3       www       275:    $r->print(<<ENDSMPHEAD);
                    276: <html>
1.6       www       277: <head>
                    278: <script>
                    279: var srch;
                    280: var srchflag=-1; // 1 means currently open
                    281:                  // 0 means closed (but has been open)
                    282:                  // -1 means never yet opened/defined
                    283: var srchmode='';
                    284: 
                    285: var idx;
                    286: var idxflag=-1; // 1 means currently open
                    287:                  // 0 means closed (but has been open)
                    288:                  // -1 means never yet opened/defined
                    289: var idxmode='';
                    290: 
                    291: // ------------------------------------------------------ Clears indexer window
                    292: function idxclear() {
                    293:   idx.document.clear();
                    294: }
                    295: 
                    296: // ------------------------------------------------------- Clears search window
                    297: function srchclear() {
                    298:   srch.document.clear();
                    299: }
                    300: 
                    301: // ------------------------------------------------------ Closes indexer window
                    302: function idxclose() {
                    303:   if (idx && !idx.closed) {
                    304:     idxflag=0;
                    305:     idx.close();
                    306:   }
                    307: }
                    308: 
                    309: // ------------------------------------------------------- Closes search window
                    310: function srchclose() {
                    311:   if (srch && !srch.closed) {
                    312:     srchflag=0;
                    313:     srch.close();
                    314:   }
                    315: }
                    316: 
                    317: // -------------------------------------------------------- Open indexer window
                    318: function idxopen(mode) {
                    319:    var options="scrollbars=1,resizable=1,menubar=0";
                    320:    idxmode=mode;
                    321:    idxflag=1;
                    322:    idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
                    323:    idx.focus();
                    324: }
                    325: 
                    326: // --------------------------------------------------------- Open search window
                    327: function srchopen(mode) {
                    328:    var options="scrollbars=1,resizable=1,menubar=0";
                    329:    srchmode=mode;
                    330:    srchflag=1;
                    331:    srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
                    332:    srch.focus();
                    333: }
                    334: // ----------------------------------------------------- launch indexer browser
                    335: function groupsearch() {
                    336:    srchcheck('groupsearch');
                    337: }
                    338: 
                    339: function groupimport() {
                    340:    idxcheck('groupimport');
                    341: }
                    342: // ------------------------------------------------------- Do srch status check
                    343: function srchcheck(mode) {
                    344:    if (!srch || srch.closed || srchmode!=mode) {
                    345:       srchopen(mode);
                    346:    }
                    347:    srch.focus();
                    348: }
                    349: 
                    350: // -------------------------------------------------------- Do idx status check
                    351: function idxcheck(mode) {
                    352:    if (!idx || idx.closed || idxmode!=mode) {
                    353:       idxopen(mode);
                    354:    }
                    355:    idx.focus();
                    356: }
1.15    ! www       357: 
        !           358: 
        !           359:     var editbrowser;
        !           360:     function openbrowser(formname,elementname,only,omit) {
        !           361:         var url = '/res/?';
        !           362:         if (editbrowser == null) {
        !           363:             url += 'launch=1&';
        !           364:         }
        !           365:         url += 'catalogmode=interactive&';
        !           366:         url += 'mode=edit&';
        !           367:         url += 'form=' + formname + '&';
        !           368:         if (only != null) {
        !           369:             url += 'only=' + only + '&';
        !           370:         } 
        !           371:         if (omit != null) {
        !           372:             url += 'omit=' + omit + '&';
        !           373:         }
        !           374:         url += 'element=' + elementname + '';
        !           375:         var title = 'Browser';
        !           376:         var options = 'scrollbars=1,resizable=1,menubar=0';
        !           377:         options += ',width=700,height=600';
        !           378:         editbrowser = open(url,title,options,'1');
        !           379:         editbrowser.focus();
        !           380:     }
1.6       www       381: </script>
                    382: </head>                 
1.3       www       383: <body bgcolor='#FFFFFF'>
1.8       www       384: $buttons
1.7       www       385: <font color=red>$errtext</font>
1.13      www       386: <form name=simpleedit method=post>
1.11      www       387: <input type=hidden name=forcesmp value=1>
                    388: <table>
1.12      www       389:     <tr><th width="40%">Import</th>
                    390: <th>&nbsp;</th>
                    391: <th width="40%">Target</th></tr>
                    392: <tr><td bgcolor="#FFFFCC">
                    393: <input type=button onClick="javascript:groupsearch()" value="Group Search">
1.13      www       394: <input type=button onClick="javascript:groupimport();" value="Group Import">
1.15    ! www       395: after selected
1.14      www       396: <hr>
1.15    ! www       397: <input type=text size=20 name=importmap>
        !           398: <input type=button 
        !           399: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
        !           400: value="Browse"><input type=submit name=loadmap value="Load Map"><hr>
1.14      www       401: <input type=submit name="discard" value="Discard Selected">
                    402: <input type=submit name="clear" value="Clear All">
1.12      www       403: <input type=button onClick="javascript:viewimport()" value="View">
                    404:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
                    405: <input type=button onClick="javascript:viewtarget()" value="View">
                    406: </td></tr>
                    407: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
                    408: $importwindow
                    409: </select>
1.11      www       410: </td>
1.12      www       411: <td bgcolor="#FFFFAA" align="center">
                    412: Cut selected<br>
1.11      www       413: <input type=submit name=cut value='<<<'><p>
1.12      www       414: <hr>
                    415: Paste after selected<br>
1.11      www       416: <input type=submit name=paste value='>>>'>
                    417: </td>
1.12      www       418: <td bgcolor="#FFFFCC"><select name="target" multiple>
1.11      www       419: $targetwindow
                    420: </select>
1.12      www       421: </table>
1.13      www       422: <input type=hidden name=importdetail value="">
                    423: <input type=hidden name=curimpdetail value="$importdetail">
1.12      www       424: <input type=hidden name=targetdetail value="$targetdetail">
                    425: </form>
                    426: </body></html>
1.3       www       427: ENDSMPHEAD
                    428: }
                    429: 
1.11      www       430: # ----------------------------------------------------------------- No such dir
1.4       www       431: sub nodir {
                    432:    my ($r,$dir)=@_;
                    433:    $dir=~s/^\/home\/\w+\/public\_html//;
                    434:    $r->print(<<ENDNODIR);
                    435: <html>
                    436: <body bgcolor='#FFFFFF'>
                    437: <h1>No such directory: $dir</h1>
                    438: </body>
                    439: </html>
                    440: ENDNODIR
                    441: }
                    442: 
1.8       www       443: # ---------------------------------------------------------------- View Handler
                    444: 
                    445: sub viewmap {
1.10      www       446:     my ($r,$adv,$errtext)=@_;
1.8       www       447:     $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
1.10      www       448:     if ($errtext) {
                    449: 	$r->print($errtext.'<hr>');
                    450:     }
1.9       www       451:     foreach (@resources) {
                    452: 	if (defined($_)) {
                    453: 	    my ($title,$url)=split(/\:/,$_);
                    454:             $title=~s/\&colon\;/\:/g;
                    455:             $url=~s/\&colon\;/\:/g;
1.15    ! www       456:             unless ($title) { $title=(split(/\//,$url))[-1] };
        !           457:             unless ($title) { $title='<i>Empty</i>'; }
1.9       www       458:             if ($url) {
                    459: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
                    460:             }
                    461:             $r->print(&Apache::lonratsrv::qtescape($title));
                    462:             if ($url) { $r->print('</a>'); }
                    463:             $r->print('<br>');
                    464:         }
                    465:     }
1.8       www       466:     $r->print('</body></html>');
                    467: }
                    468: 
1.3       www       469: # ================================================================ Main Handler
                    470: 
                    471: sub handler {
                    472:   my $r=shift;
                    473:   $r->content_type('text/html');
                    474:   $r->send_http_header;
                    475: 
                    476:   return OK if $r->header_only;
                    477: 
                    478:   my $url=$r->uri;
                    479:   my $fn=&Apache::lonnet::filelocation('',$url);
                    480: 
1.4       www       481:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
                    482:   unless (-e $dir) {
                    483:       &nodir($r,$dir);
                    484:       return OK;
                    485:   }
1.8       www       486: 
                    487: # ------------------------------------------- Determine which tools can be used
1.3       www       488:   my $adv=0;
                    489: 
                    490:   unless ($ENV{'form.forcesmp'}) {
                    491:      if ($ENV{'form.forceadv'}) {
                    492:         $adv=1;
                    493:      } elsif (my $fh=Apache::File->new($fn)) {
                    494: 	 my $allmap=join('',<$fh>);
                    495:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
                    496:      }
                    497:   }
                    498: 
1.8       www       499:   my $errtext='';
                    500:   my $fatal=0;
                    501: 
                    502: # -------------------------------------------------------------------- Load map
                    503:   ($errtext,$fatal)=&mapread($fn,$errtext);
                    504: 
                    505:   if ($fatal==1) { $adv=1; }
                    506: 
                    507: # ----------------------------------- adv==1 now means "graphical MUST be used"
                    508: 
                    509:   if ($ENV{'form.forceadv'}) {
1.3       www       510:       &ratedt($r,$url);
1.8       www       511:   } elsif ($ENV{'form.forcesmp'}) {
                    512:       &smpedt($r,$errtext);
1.3       www       513:   } else {
1.10      www       514:       &viewmap($r,$adv,$errtext);
1.3       www       515:   }
1.1       www       516:   return OK;
                    517: }
                    518: 
                    519: 1;
                    520: __END__
                    521: 
                    522: 
                    523: 
                    524: 
                    525: 
                    526: 
                    527: 

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