File:  [LON-CAPA] / rat / lonratedt.pm
Revision 1.12: download - view: text, annotated - select for diffs
Mon May 13 15:36:05 2002 UTC (21 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Process form input and prepare field for group import and browse

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

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