Annotation of rat/lonratedt.pm, revision 1.16

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Edit Handler for RAT Maps
1.5       www         3: #
1.16    ! www         4: # $Id: lonratedt.pm,v 1.15 2002/05/13 21:26:05 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.16    ! www       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: 
1.3       www       171: # --------------------------------------------------------- Build up RAT screen
                    172: sub ratedt {
                    173:   my ($r,$url)=@_;
1.1       www       174:   $r->print(<<ENDDOCUMENT);
                    175: 
                    176: <html>
1.2       harris41  177: <head>
                    178: <script language="JavaScript">
                    179:     var flag=0;
                    180: </script>
                    181: </head>
1.1       www       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
1.3       www       190: }
                    191: 
1.8       www       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: 
1.3       www       208: sub smpedt {
1.8       www       209:    my ($r,$errtext)=@_;
                    210:    my $buttons=&buttons(2);
1.12      www       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'})) {
1.13      www       220: 	   @importselect=sort($ENV->{'form.import'});
1.12      www       221:        } else {
                    222:            @importselect=($ENV{'form.import'});
                    223:        }
                    224:    }
                    225:    if (defined($ENV{'form.target'})) {
                    226:        if (ref($ENV{'form.target'})) {
1.13      www       227: 	   @targetselect=sort($ENV->{'form.target'});
1.12      www       228:        } else {
                    229:            @targetselect=($ENV{'form.target'});
                    230:        }
                    231:    }
1.13      www       232: # ============================================================ Process commands
1.12      www       233: 
1.14      www       234:    my $targetdetail=$ENV{'form.targetdetail'};
                    235:    my $importdetail=$ENV{'form.curimpdetail'};
1.13      www       236: 
                    237: # ---------------------------------------------------- Importing from groupsort
1.16    ! www       238:    if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
1.13      www       239: 
1.14      www       240:        $importdetail='';
1.13      www       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: 
1.14      www       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: # ---------------------------
1.13      www       289:    }
1.12      www       290: 
                    291: # ------------------------------------------------------------ Assemble windows
                    292: 
1.13      www       293:    my $idx=-1;
                    294:    my $importwindow=join("\n",map {
                    295:        $idx++;
                    296:        if ($_) { 
1.15      www       297:           my ($name,$url)=split(/\=/,$_);
                    298:           unless ($name) { $name=(split(/\//,$url))[-1]; }
                    299:           unless ($name) { $name='EMPTY'; }
1.13      www       300:           '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
                    301:                                     '</option>';
                    302:       }
                    303:    } split(/\&/,$importdetail));
1.12      www       304: 
1.13      www       305:    $idx=0;
1.11      www       306:    my $targetwindow=join("\n",map { 
1.13      www       307:        my ($name,$url)=split(/\:/,$resources[$_]);
1.15      www       308:        unless ($name) {  $name=(split(/\//,$url))[-1]; }
                    309:        unless ($name) { $name='EMPTY'; }
1.13      www       310:        $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
                    311: 	                  &Apache::lonnet::escape($url);
                    312:        $idx++;
                    313:        '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
1.11      www       314:    } @order);
                    315: 
1.8       www       316: # ----------------------------------------------------- Start simple RAT screen
1.3       www       317:    $r->print(<<ENDSMPHEAD);
                    318: <html>
1.6       www       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: }
1.15      www       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:     }
1.6       www       423: </script>
                    424: </head>                 
1.3       www       425: <body bgcolor='#FFFFFF'>
1.8       www       426: $buttons
1.7       www       427: <font color=red>$errtext</font>
1.13      www       428: <form name=simpleedit method=post>
1.11      www       429: <input type=hidden name=forcesmp value=1>
                    430: <table>
1.12      www       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">
1.13      www       436: <input type=button onClick="javascript:groupimport();" value="Group Import">
1.15      www       437: after selected
1.14      www       438: <hr>
1.15      www       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>
1.14      www       443: <input type=submit name="discard" value="Discard Selected">
                    444: <input type=submit name="clear" value="Clear All">
1.12      www       445: <input type=button onClick="javascript:viewimport()" value="View">
1.16    ! www       446: 
1.12      www       447:     </td><td>&nbsp;</td><td bgcolor="#FFFFCC">
1.16    ! www       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">
1.12      www       455: </td></tr>
1.16    ! www       456: 
1.12      www       457: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
                    458: $importwindow
                    459: </select>
1.11      www       460: </td>
1.12      www       461: <td bgcolor="#FFFFAA" align="center">
                    462: Cut selected<br>
1.11      www       463: <input type=submit name=cut value='<<<'><p>
1.12      www       464: <hr>
                    465: Paste after selected<br>
1.11      www       466: <input type=submit name=paste value='>>>'>
                    467: </td>
1.12      www       468: <td bgcolor="#FFFFCC"><select name="target" multiple>
1.11      www       469: $targetwindow
                    470: </select>
1.12      www       471: </table>
1.13      www       472: <input type=hidden name=importdetail value="">
                    473: <input type=hidden name=curimpdetail value="$importdetail">
1.12      www       474: <input type=hidden name=targetdetail value="$targetdetail">
1.16    ! www       475: <input type=hidden name=impfortarget value="0">
1.12      www       476: </form>
                    477: </body></html>
1.3       www       478: ENDSMPHEAD
                    479: }
                    480: 
1.11      www       481: # ----------------------------------------------------------------- No such dir
1.4       www       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: 
1.8       www       494: # ---------------------------------------------------------------- View Handler
                    495: 
                    496: sub viewmap {
1.10      www       497:     my ($r,$adv,$errtext)=@_;
1.8       www       498:     $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
1.10      www       499:     if ($errtext) {
                    500: 	$r->print($errtext.'<hr>');
                    501:     }
1.9       www       502:     foreach (@resources) {
                    503: 	if (defined($_)) {
                    504: 	    my ($title,$url)=split(/\:/,$_);
                    505:             $title=~s/\&colon\;/\:/g;
                    506:             $url=~s/\&colon\;/\:/g;
1.15      www       507:             unless ($title) { $title=(split(/\//,$url))[-1] };
                    508:             unless ($title) { $title='<i>Empty</i>'; }
1.9       www       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:     }
1.8       www       517:     $r->print('</body></html>');
                    518: }
                    519: 
1.3       www       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: 
1.4       www       532:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
                    533:   unless (-e $dir) {
                    534:       &nodir($r,$dir);
                    535:       return OK;
                    536:   }
1.8       www       537: 
                    538: # ------------------------------------------- Determine which tools can be used
1.3       www       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: 
1.8       www       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'}) {
1.3       www       561:       &ratedt($r,$url);
1.8       www       562:   } elsif ($ENV{'form.forcesmp'}) {
                    563:       &smpedt($r,$errtext);
1.3       www       564:   } else {
1.10      www       565:       &viewmap($r,$adv,$errtext);
1.3       www       566:   }
1.1       www       567:   return OK;
                    568: }
                    569: 
                    570: 1;
                    571: __END__
                    572: 
                    573: 
                    574: 
                    575: 
                    576: 
                    577: 
                    578: 

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