File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.10: download - view: text, annotated - select for diffs
Sat May 11 23:23:01 2002 UTC (21 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Sorts map and looks for unconnected resources

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

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