Annotation of rat/lonratedt.pm, revision 1.11

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Edit Handler for RAT Maps
1.5       www         3: #
1.11    ! www         4: # $Id: lonratedt.pm,v 1.10 2002/05/11 23:23:01 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.11    ! www       169:    my $targetwindow=join("\n",map { 
        !           170:        my ($name)=split(/\:/,$resources[$_]);
        !           171:        unless ($name) { $name='UNKNOWN'; }
        !           172:        '<option value="t'.$_.'">'.$name.'</option>';
        !           173:    } @order);
        !           174: 
1.8       www       175: # ----------------------------------------------------- Start simple RAT screen
1.3       www       176:    $r->print(<<ENDSMPHEAD);
                    177: <html>
1.6       www       178: <head>
                    179: <script>
                    180: var srch;
                    181: var srchflag=-1; // 1 means currently open
                    182:                  // 0 means closed (but has been open)
                    183:                  // -1 means never yet opened/defined
                    184: var srchmode='';
                    185: 
                    186: var idx;
                    187: var idxflag=-1; // 1 means currently open
                    188:                  // 0 means closed (but has been open)
                    189:                  // -1 means never yet opened/defined
                    190: var idxmode='';
                    191: 
                    192: // ------------------------------------------------------ Clears indexer window
                    193: function idxclear() {
                    194:   idx.document.clear();
                    195: }
                    196: 
                    197: // ------------------------------------------------------- Clears search window
                    198: function srchclear() {
                    199:   srch.document.clear();
                    200: }
                    201: 
                    202: // ------------------------------------------------------ Closes indexer window
                    203: function idxclose() {
                    204:   if (idx && !idx.closed) {
                    205:     idxflag=0;
                    206:     idx.close();
                    207:   }
                    208: }
                    209: 
                    210: // ------------------------------------------------------- Closes search window
                    211: function srchclose() {
                    212:   if (srch && !srch.closed) {
                    213:     srchflag=0;
                    214:     srch.close();
                    215:   }
                    216: }
                    217: 
                    218: // -------------------------------------------------------- Open indexer window
                    219: function idxopen(mode) {
                    220:    var options="scrollbars=1,resizable=1,menubar=0";
                    221:    idxmode=mode;
                    222:    idxflag=1;
                    223:    idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
                    224:    idx.focus();
                    225: }
                    226: 
                    227: // --------------------------------------------------------- Open search window
                    228: function srchopen(mode) {
                    229:    var options="scrollbars=1,resizable=1,menubar=0";
                    230:    srchmode=mode;
                    231:    srchflag=1;
                    232:    srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
                    233:    srch.focus();
                    234: }
                    235: // ----------------------------------------------------- launch indexer browser
                    236: function groupsearch() {
                    237:    srchcheck('groupsearch');
                    238: }
                    239: 
                    240: function groupimport() {
                    241:    idxcheck('groupimport');
                    242: }
                    243: // ------------------------------------------------------- Do srch status check
                    244: function srchcheck(mode) {
                    245:    if (!srch || srch.closed || srchmode!=mode) {
                    246:       srchopen(mode);
                    247:    }
                    248:    srch.focus();
                    249: }
                    250: 
                    251: // -------------------------------------------------------- Do idx status check
                    252: function idxcheck(mode) {
                    253:    if (!idx || idx.closed || idxmode!=mode) {
                    254:       idxopen(mode);
                    255:    }
                    256:    idx.focus();
                    257: }
                    258: </script>
                    259: </head>                 
1.3       www       260: <body bgcolor='#FFFFFF'>
1.8       www       261: $buttons
1.7       www       262: <font color=red>$errtext</font>
1.11    ! www       263: <form method=post>
        !           264: <input type=hidden name=forcesmp value=1>
        !           265: <table>
        !           266:     <tr><th>Import</th><th>&nbsp;</th><th>Target</th></tr>
        !           267: <tr><td>
1.6       www       268: <a href="javascript:groupsearch()">Group Search</a>
                    269: <a href="javascript:groupimport()">Group Import</a>
1.11    ! www       270:     </td><td>&nbsp;</td><td>&nbsp;</td></tr>
        !           271: <tr><td>
        !           272: </td>
        !           273: <td>
        !           274: <input type=submit name=cut value='<<<'><p>
        !           275: <input type=submit name=paste value='>>>'>
        !           276: </td>
        !           277: <td><select name="target" multiple>
        !           278: $targetwindow
        !           279: </select>
1.3       www       280: ENDSMPHEAD
1.8       www       281: 
1.11    ! www       282:     $r->print('</table></form></body></html>');
1.3       www       283: }
                    284: 
1.11    ! www       285: # ----------------------------------------------------------------- No such dir
1.4       www       286: sub nodir {
                    287:    my ($r,$dir)=@_;
                    288:    $dir=~s/^\/home\/\w+\/public\_html//;
                    289:    $r->print(<<ENDNODIR);
                    290: <html>
                    291: <body bgcolor='#FFFFFF'>
                    292: <h1>No such directory: $dir</h1>
                    293: </body>
                    294: </html>
                    295: ENDNODIR
                    296: }
                    297: 
1.8       www       298: # ---------------------------------------------------------------- View Handler
                    299: 
                    300: sub viewmap {
1.10      www       301:     my ($r,$adv,$errtext)=@_;
1.8       www       302:     $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
1.10      www       303:     if ($errtext) {
                    304: 	$r->print($errtext.'<hr>');
                    305:     }
1.9       www       306:     foreach (@resources) {
                    307: 	if (defined($_)) {
                    308: 	    my ($title,$url)=split(/\:/,$_);
                    309:             $title=~s/\&colon\;/\:/g;
                    310:             $url=~s/\&colon\;/\:/g;
                    311:             unless ($title) { $title='<i>Unknown</i>'; }
                    312:             if ($url) {
                    313: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
                    314:             }
                    315:             $r->print(&Apache::lonratsrv::qtescape($title));
                    316:             if ($url) { $r->print('</a>'); }
                    317:             $r->print('<br>');
                    318:         }
                    319:     }
1.8       www       320:     $r->print('</body></html>');
                    321: }
                    322: 
1.3       www       323: # ================================================================ Main Handler
                    324: 
                    325: sub handler {
                    326:   my $r=shift;
                    327:   $r->content_type('text/html');
                    328:   $r->send_http_header;
                    329: 
                    330:   return OK if $r->header_only;
                    331: 
                    332:   my $url=$r->uri;
                    333:   my $fn=&Apache::lonnet::filelocation('',$url);
                    334: 
1.4       www       335:   my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
                    336:   unless (-e $dir) {
                    337:       &nodir($r,$dir);
                    338:       return OK;
                    339:   }
1.8       www       340: 
                    341: # ------------------------------------------- Determine which tools can be used
1.3       www       342:   my $adv=0;
                    343: 
                    344:   unless ($ENV{'form.forcesmp'}) {
                    345:      if ($ENV{'form.forceadv'}) {
                    346:         $adv=1;
                    347:      } elsif (my $fh=Apache::File->new($fn)) {
                    348: 	 my $allmap=join('',<$fh>);
                    349:          $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
                    350:      }
                    351:   }
                    352: 
1.8       www       353:   my $errtext='';
                    354:   my $fatal=0;
                    355: 
                    356: # -------------------------------------------------------------------- Load map
                    357:   ($errtext,$fatal)=&mapread($fn,$errtext);
                    358: 
                    359:   if ($fatal==1) { $adv=1; }
                    360: 
                    361: # ----------------------------------- adv==1 now means "graphical MUST be used"
                    362: 
                    363:   if ($ENV{'form.forceadv'}) {
1.3       www       364:       &ratedt($r,$url);
1.8       www       365:   } elsif ($ENV{'form.forcesmp'}) {
                    366:       &smpedt($r,$errtext);
1.3       www       367:   } else {
1.10      www       368:       &viewmap($r,$adv,$errtext);
1.3       www       369:   }
1.1       www       370:   return OK;
                    371: }
                    372: 
                    373: 1;
                    374: __END__
                    375: 
                    376: 
                    377: 
                    378: 
                    379: 
                    380: 
                    381: 

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