File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.15: download - view: text, annotated - select for diffs
Mon Apr 7 20:47:06 2003 UTC (21 years ago) by www
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
Bug #1354 - show warning if it was not possible to show map in linear order.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.15 2003/04/07 20:47:06 www Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # (Handler to resolve ambiguous file locations
   30: #
   31: # (TeX Content Handler
   32: #
   33: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   34: #
   35: # 10/11,10/12 Gerd Kortemeyer)
   36: #
   37: # 10/16 Gerd Kortemeyer
   38: 
   39: package Apache::lonsequence;
   40: 
   41: use strict;
   42: use Apache::lonnet;
   43: use Apache::Constants qw(:common :http REDIRECT);
   44: use GDBM_File;
   45: use Apache::lonratedt;
   46: use Apache::lonratsrv;
   47: use Apache::lonpageflip;
   48: use Apache::loncommon;
   49: 
   50: my %selhash;
   51: my $successtied;
   52: 
   53: # ----------------------------------------- Attempt to read from resource space
   54: 
   55: sub attemptread {
   56:     my $fn=shift;
   57:     &Apache::lonnet::repcopy($fn);
   58:     if (-e $fn) {
   59: 	return &Apache::lonratedt::attemptread($fn);
   60:     } else {
   61:         return ();
   62:     }
   63: }
   64: 
   65: sub mapread {
   66:     my $fn=shift;
   67:     &Apache::lonnet::repcopy($fn);
   68:     if (-e $fn) {
   69: 	return &Apache::lonratedt::mapread($fn,'');
   70:     } else {
   71:         return ();
   72:     }
   73: }
   74: 
   75: # ---------------------------------------------------------------- View Handler
   76: 
   77: sub viewmap {
   78:     my ($r,$url)=@_;
   79:     $r->print('<html>');
   80:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
   81: <script>
   82: 
   83: function select_group() {
   84:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   85: }
   86: 
   87: function queue(val) {
   88:     if (eval("document.forms."+val+".filelink.checked")) {
   89: 	var l=val.length;
   90: 	var v=val.substring(4,l);
   91: 	document.forms.fileattr.acts.value+='1a'+v+'b';
   92:     }
   93:     else {
   94: 	var l=val.length;
   95: 	var v=val.substring(4,l);
   96: 	document.forms.fileattr.acts.value+='0a'+v+'b';
   97:     }
   98: }
   99: 
  100: </script>
  101: ENDSCRIPT
  102:     }
  103:     $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
  104: # ------------------ This is trying to select. Provide buttons and tie %selhash
  105:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSELECT);
  106: <form name=fileattr><input type=hidden name=acts value=''>
  107: <input type="button" name="close" value='CLOSE' onClick="self.close()">
  108: <input type="button" name="groupimport" value='GROUP IMPORT'
  109: onClick="javascript:select_group()">
  110: </form>   
  111: ENDSELECT
  112:     my $diropendb = 
  113:     "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
  114:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  115: 	    if ($ENV{'form.launch'} eq '1') {
  116: 	       &start_fresh_session();
  117: 	    }
  118:             $successtied=1;
  119: 
  120: # - Evaluate actions from previous page (both cumulatively and chronologically)
  121:         if ($ENV{'form.catalogmode'} eq 'groupimport') {
  122: 	    my $acts=$ENV{'form.acts'};
  123: 	    my @Acts=split(/b/,$acts);
  124: 	    my %ahash;
  125: 	    my %achash;
  126: 	    my $ac=0;
  127: 	    # some initial hashes for working with data
  128: 	    foreach (@Acts) {
  129: 		my ($state,$ref)=split(/a/);
  130: 		$ahash{$ref}=$state;
  131: 		$achash{$ref}=$ac;
  132: 		$ac++;
  133: 	    }
  134: 	    # sorting through the actions and changing the tied database hash
  135: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  136: 		my $key=$_;
  137: 		if ($ahash{$key} eq '1') {
  138: 		    $selhash{'store_'.$selhash{'pre_'.$key.'_link'}}=
  139: 			$selhash{'pre_'.$key.'_title'};
  140: 		    $selhash{'storectr_'.$selhash{'pre_'.$key.'_link'}}=
  141: 			$selhash{'storectr'}+0;
  142: 		    $selhash{'storectr'}++;
  143: 		}
  144: 		if ($ahash{$key} eq '0') {
  145: 		    if ($selhash{'store_'.$selhash{'pre_'.$key.'_link'}}) {
  146: 		       delete $selhash{'store_'.$selhash{'pre_'.$key.'_link'}};
  147: 		    }
  148: 		}
  149: 	    }
  150: 	    # deleting the previously cached listing
  151: 	    foreach (keys %selhash) {
  152: 		if ($_ =~ /^pre_/ && $_ =~/link$/) {
  153: 		    my $key = $_;
  154: 		    $key =~ s/^pre_//;
  155: 		    $key =~ s/_[^_]*$//;
  156: 		    delete $selhash{'pre_'.$key.'_title'};
  157: 		    delete $selhash{'pre_'.$key.'_link'};
  158: 		}
  159: 	    }
  160: 	}
  161: # -
  162:         }
  163:     }
  164: # ----------------------------- successtied is now '1' if in working selectmode
  165:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  166:     if ($fatal==1) {
  167:        $r->print('<p><b><font color="red">Map contents are not shown in order.</font></b></p><br />');
  168:     }
  169:     my $idx=0;
  170:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  171: 	if (defined($_)) {
  172:             $idx++;
  173:             if ($successtied) { 
  174: 		$r->print('<form name="form'.$idx.'">');
  175:             }
  176: 	    my ($title,$url)=split(/\:/,$_);
  177:             $title=~s/\&colon\;/\:/g;
  178:             $url=~s/\&colon\;/\:/g;
  179:             unless ($title) { $title=(split(/\//,$url))[-1] };
  180:             unless ($title) { $title='<i>Empty</i>'; }
  181:             if ($url) {
  182: 		if ($successtied) {
  183: 		    my $checked='';
  184: 	           if ($selhash{'store_'.$url}) {
  185: 	       	      $checked=" checked";
  186: 	           }
  187: 	           $selhash{"pre_${idx}_link"}=$url;
  188: 	           $selhash{"pre_${idx}_title"}=$title;
  189: 
  190: 		    $r->print(<<ENDCHECKBOX);
  191: <input type='checkbox' name='filelink' 
  192: value='$url' onClick='javascript:queue("form$idx")'$checked>
  193: <input type='hidden' name='title' value='$title'>
  194: ENDCHECKBOX
  195:                 }
  196: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
  197:             }
  198:             $r->print(&Apache::lonratsrv::qtescape($title));
  199:             if ($url) { $r->print('</a>'); }
  200:             if ($successtied) {
  201: 		$r->print('</form>');
  202:             } else {
  203: 		$r->print('<br>');
  204:             }
  205:         }
  206:     }
  207:     $r->print('</body></html>');
  208:     if ($successtied) {
  209: 	untie %selhash;
  210:     }
  211: }
  212: 
  213: # ----------------------------------------------------------- Clean out selhash
  214: sub start_fresh_session {
  215:     foreach (keys %selhash) {
  216: 	if ($_ =~ /^pre_/) {
  217: 	    delete $selhash{$_};
  218: 	}
  219: 	if ($_ =~ /^store/) {
  220: 	    delete $selhash{$_};
  221: 	}
  222:     }
  223: }
  224: 
  225: 
  226: # ================================================================ Main Handler
  227: 
  228: sub handler {
  229:    my $r=shift;
  230: 
  231:    if ($r->header_only) {
  232:       $r->content_type('text/html');
  233:       $r->send_http_header;
  234:       return OK;
  235:    }
  236:  
  237:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  238:                                           ['forceselect','launch']);
  239: 
  240:    my %hash;
  241:    my %bighash;
  242:    my $requrl=$r->uri;
  243: 
  244:    $successtied=0;
  245: # ------------------------------------------------------------ Tie symb db file
  246:   my $disurl='';
  247:   my $dismapid='';
  248: 
  249:   if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
  250:        my $last;
  251:        if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  252:                     &GDBM_READER(),0640)) {
  253: 	   $last=$hash{'last_direction'};
  254:            untie(%hash);
  255:        }
  256:        my $direction='';
  257:        my $prevmap='';
  258:        if ($last) {
  259: 	  ($prevmap,$direction)=(split(/\_\_\_/,$last));
  260:        }
  261: # ------------------------------------------------------------- Tie big db file
  262:        if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  263:                     &GDBM_READER(),0640)) {
  264: 	   my $disid='';
  265: 
  266:            if ($direction eq 'back') {
  267: 	       $disid=$bighash{'map_finish_'.$requrl};
  268:            } else {
  269:                $disid=$bighash{'map_start_'.$requrl};
  270:            } 
  271:            if ($disid) {
  272: 	       $disurl=$bighash{'src_'.$disid};
  273:                $dismapid=(split(/\./,$disid))[1];
  274:            }
  275: # ------------------------- If this is an empty one, skip to next non-empty one
  276:            if ((!$disurl) && ($disid)) {
  277: 	       $direction=($direction?$direction:'forward');
  278:                ($disid,$requrl)=
  279:                          &Apache::lonpageflip::fullmove($disid,
  280:                            &Apache::lonnet::declutter($requrl),$direction);
  281:                if ($disid) {
  282: 	           $disurl=$bighash{'src_'.$disid};
  283:                    $dismapid=(split(/\./,$disid))[1];
  284:                }
  285:  	   }
  286: 
  287: # --------------------------------------- Untie hash, make sure to come by here
  288:            untie(%bighash);
  289:        }
  290:    }
  291: 
  292: # now either disurl is set (going to first page), or we need another display
  293: 
  294:    if ($disurl) {
  295: # -------------------------------------------------- Has first or last resource
  296:       &Apache::lonnet::symblist($requrl,$disurl => $dismapid,
  297:                    'last_known' => &Apache::lonnet::declutter($disurl)); 
  298:       $r->content_type('text/html');
  299:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
  300:       return REDIRECT;
  301:    } else {
  302:        $r->content_type('text/html');
  303:        $r->send_http_header;
  304:        &viewmap($r,$requrl);
  305:        return OK;
  306:    }
  307: }
  308: 
  309: 1;
  310: __END__
  311: 
  312: 
  313: 
  314: 
  315: 
  316: 
  317: 

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