File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.14: download - view: text, annotated - select for diffs
Sat Aug 31 00:43:13 2002 UTC (21 years, 8 months ago) by www
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
Various new functionality for "ad hoc" courses.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.14 2002/08/31 00:43:13 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: # ---------------------------------------------------------------- View Handler
   66: 
   67: sub viewmap {
   68:     my ($r,$url)=@_;
   69:     $r->print('<html>');
   70:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
   71: <script>
   72: 
   73: function select_group() {
   74:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   75: }
   76: 
   77: function queue(val) {
   78:     if (eval("document.forms."+val+".filelink.checked")) {
   79: 	var l=val.length;
   80: 	var v=val.substring(4,l);
   81: 	document.forms.fileattr.acts.value+='1a'+v+'b';
   82:     }
   83:     else {
   84: 	var l=val.length;
   85: 	var v=val.substring(4,l);
   86: 	document.forms.fileattr.acts.value+='0a'+v+'b';
   87:     }
   88: }
   89: 
   90: </script>
   91: ENDSCRIPT
   92:     }
   93:     $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
   94: # ------------------ This is trying to select. Provide buttons and tie %selhash
   95:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSELECT);
   96: <form name=fileattr><input type=hidden name=acts value=''>
   97: <input type="button" name="close" value='CLOSE' onClick="self.close()">
   98: <input type="button" name="groupimport" value='GROUP IMPORT'
   99: onClick="javascript:select_group()">
  100: </form>   
  101: ENDSELECT
  102:     my $diropendb = 
  103:     "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
  104:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  105: 	    if ($ENV{'form.launch'} eq '1') {
  106: 	       &start_fresh_session();
  107: 	    }
  108:             $successtied=1;
  109: 
  110: # - Evaluate actions from previous page (both cumulatively and chronologically)
  111:         if ($ENV{'form.catalogmode'} eq 'groupimport') {
  112: 	    my $acts=$ENV{'form.acts'};
  113: 	    my @Acts=split(/b/,$acts);
  114: 	    my %ahash;
  115: 	    my %achash;
  116: 	    my $ac=0;
  117: 	    # some initial hashes for working with data
  118: 	    foreach (@Acts) {
  119: 		my ($state,$ref)=split(/a/);
  120: 		$ahash{$ref}=$state;
  121: 		$achash{$ref}=$ac;
  122: 		$ac++;
  123: 	    }
  124: 	    # sorting through the actions and changing the tied database hash
  125: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  126: 		my $key=$_;
  127: 		if ($ahash{$key} eq '1') {
  128: 		    $selhash{'store_'.$selhash{'pre_'.$key.'_link'}}=
  129: 			$selhash{'pre_'.$key.'_title'};
  130: 		    $selhash{'storectr_'.$selhash{'pre_'.$key.'_link'}}=
  131: 			$selhash{'storectr'}+0;
  132: 		    $selhash{'storectr'}++;
  133: 		}
  134: 		if ($ahash{$key} eq '0') {
  135: 		    if ($selhash{'store_'.$selhash{'pre_'.$key.'_link'}}) {
  136: 		       delete $selhash{'store_'.$selhash{'pre_'.$key.'_link'}};
  137: 		    }
  138: 		}
  139: 	    }
  140: 	    # deleting the previously cached listing
  141: 	    foreach (keys %selhash) {
  142: 		if ($_ =~ /^pre_/ && $_ =~/link$/) {
  143: 		    my $key = $_;
  144: 		    $key =~ s/^pre_//;
  145: 		    $key =~ s/_[^_]*$//;
  146: 		    delete $selhash{'pre_'.$key.'_title'};
  147: 		    delete $selhash{'pre_'.$key.'_link'};
  148: 		}
  149: 	    }
  150: 	}
  151: # -
  152:         }
  153:     }
  154: # ----------------------------- successtied is now '1' if in working selectmode
  155:     my $idx=0;
  156:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  157: 	if (defined($_)) {
  158:             $idx++;
  159:             if ($successtied) { 
  160: 		$r->print('<form name="form'.$idx.'">');
  161:             }
  162: 	    my ($title,$url)=split(/\:/,$_);
  163:             $title=~s/\&colon\;/\:/g;
  164:             $url=~s/\&colon\;/\:/g;
  165:             unless ($title) { $title=(split(/\//,$url))[-1] };
  166:             unless ($title) { $title='<i>Empty</i>'; }
  167:             if ($url) {
  168: 		if ($successtied) {
  169: 		    my $checked='';
  170: 	           if ($selhash{'store_'.$url}) {
  171: 	       	      $checked=" checked";
  172: 	           }
  173: 	           $selhash{"pre_${idx}_link"}=$url;
  174: 	           $selhash{"pre_${idx}_title"}=$title;
  175: 
  176: 		    $r->print(<<ENDCHECKBOX);
  177: <input type='checkbox' name='filelink' 
  178: value='$url' onClick='javascript:queue("form$idx")'$checked>
  179: <input type='hidden' name='title' value='$title'>
  180: ENDCHECKBOX
  181:                 }
  182: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
  183:             }
  184:             $r->print(&Apache::lonratsrv::qtescape($title));
  185:             if ($url) { $r->print('</a>'); }
  186:             if ($successtied) {
  187: 		$r->print('</form>');
  188:             } else {
  189: 		$r->print('<br>');
  190:             }
  191:         }
  192:     }
  193:     $r->print('</body></html>');
  194:     if ($successtied) {
  195: 	untie %selhash;
  196:     }
  197: }
  198: 
  199: # ----------------------------------------------------------- Clean out selhash
  200: sub start_fresh_session {
  201:     foreach (keys %selhash) {
  202: 	if ($_ =~ /^pre_/) {
  203: 	    delete $selhash{$_};
  204: 	}
  205: 	if ($_ =~ /^store/) {
  206: 	    delete $selhash{$_};
  207: 	}
  208:     }
  209: }
  210: 
  211: 
  212: # ================================================================ Main Handler
  213: 
  214: sub handler {
  215:    my $r=shift;
  216: 
  217:    if ($r->header_only) {
  218:       $r->content_type('text/html');
  219:       $r->send_http_header;
  220:       return OK;
  221:    }
  222:  
  223:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  224:                                           ['forceselect','launch']);
  225: 
  226:    my %hash;
  227:    my %bighash;
  228:    my $requrl=$r->uri;
  229: 
  230:    $successtied=0;
  231: # ------------------------------------------------------------ Tie symb db file
  232:   my $disurl='';
  233:   my $dismapid='';
  234: 
  235:   if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
  236:        my $last;
  237:        if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  238:                     &GDBM_READER(),0640)) {
  239: 	   $last=$hash{'last_direction'};
  240:            untie(%hash);
  241:        }
  242:        my $direction='';
  243:        my $prevmap='';
  244:        if ($last) {
  245: 	  ($prevmap,$direction)=(split(/\_\_\_/,$last));
  246:        }
  247: # ------------------------------------------------------------- Tie big db file
  248:        if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  249:                     &GDBM_READER(),0640)) {
  250: 	   my $disid='';
  251: 
  252:            if ($direction eq 'back') {
  253: 	       $disid=$bighash{'map_finish_'.$requrl};
  254:            } else {
  255:                $disid=$bighash{'map_start_'.$requrl};
  256:            } 
  257:            if ($disid) {
  258: 	       $disurl=$bighash{'src_'.$disid};
  259:                $dismapid=(split(/\./,$disid))[1];
  260:            }
  261: # ------------------------- If this is an empty one, skip to next non-empty one
  262:            if ((!$disurl) && ($disid)) {
  263: 	       $direction=($direction?$direction:'forward');
  264:                ($disid,$requrl)=
  265:                          &Apache::lonpageflip::fullmove($disid,
  266:                            &Apache::lonnet::declutter($requrl),$direction);
  267:                if ($disid) {
  268: 	           $disurl=$bighash{'src_'.$disid};
  269:                    $dismapid=(split(/\./,$disid))[1];
  270:                }
  271:  	   }
  272: 
  273: # --------------------------------------- Untie hash, make sure to come by here
  274:            untie(%bighash);
  275:        }
  276:    }
  277: 
  278: # now either disurl is set (going to first page), or we need another display
  279: 
  280:    if ($disurl) {
  281: # -------------------------------------------------- Has first or last resource
  282:       &Apache::lonnet::symblist($requrl,$disurl => $dismapid,
  283:                    'last_known' => &Apache::lonnet::declutter($disurl)); 
  284:       $r->content_type('text/html');
  285:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
  286:       return REDIRECT;
  287:    } else {
  288:        $r->content_type('text/html');
  289:        $r->send_http_header;
  290:        &viewmap($r,$requrl);
  291:        return OK;
  292:    }
  293: }
  294: 
  295: 1;
  296: __END__
  297: 
  298: 
  299: 
  300: 
  301: 
  302: 
  303: 

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