File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue Mar 1 00:22:57 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the _symb.db now actually does store symbs
- I am pretty sure the symbread changes are correct, but I still need to double check them

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.23 2005/03/01 00:22:57 albertel 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: 
   32: package Apache::lonsequence;
   33: 
   34: use strict;
   35: use Apache::lonnet;
   36: use Apache::Constants qw(:common :http REDIRECT);
   37: use GDBM_File;
   38: use Apache::lonratedt;
   39: use Apache::lonratsrv;
   40: use Apache::lonpageflip;
   41: use Apache::loncommon;
   42: use Apache::lonlocal;
   43: 
   44: my %selhash;
   45: my $successtied;
   46: 
   47: # ----------------------------------------- Attempt to read from resource space
   48: 
   49: sub attemptread {
   50:     my $fn=shift;
   51:     &Apache::lonnet::repcopy($fn);
   52:     if (-e $fn) {
   53: 	return &Apache::lonratedt::attemptread($fn);
   54:     } else {
   55:         return ();
   56:     }
   57: }
   58: 
   59: sub mapread {
   60:     my $fn=shift;
   61:     &Apache::lonnet::repcopy($fn);
   62:     if (-e $fn) {
   63: 	return &Apache::lonratedt::mapread($fn,'');
   64:     } else {
   65:         return ();
   66:     }
   67: }
   68: 
   69: # ---------------------------------------------------------------- View Handler
   70: 
   71: sub viewmap {
   72:     my ($r,$url)=@_;
   73:     $r->print('<html>');
   74:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
   75: <script>
   76: 
   77: function select_group() {
   78:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   79: }
   80: 
   81: function queue(val) {
   82:     if (eval("document.forms."+val+".filelink.checked")) {
   83: 	var l=val.length;
   84: 	var v=val.substring(4,l);
   85: 	document.forms.fileattr.acts.value+='1a'+v+'b';
   86:     }
   87:     else {
   88: 	var l=val.length;
   89: 	var v=val.substring(4,l);
   90: 	document.forms.fileattr.acts.value+='0a'+v+'b';
   91:     }
   92: }
   93: 
   94: </script>
   95: ENDSCRIPT
   96:     }
   97:     $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
   98: # ------------------ This is trying to select. Provide buttons and tie %selhash
   99:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSELECT);
  100: <form name=fileattr><input type=hidden name=acts value=''>
  101: <input type="button" name="close" value='CLOSE' onClick="self.close()">
  102: <input type="button" name="groupimport" value='GROUP IMPORT'
  103: onClick="javascript:select_group()">
  104: </form>   
  105: ENDSELECT
  106:     my $diropendb = 
  107:     "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
  108:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  109: 	    if ($ENV{'form.launch'} eq '1') {
  110: 	       &start_fresh_session();
  111: 	    }
  112:             $successtied=1;
  113: 
  114: # - Evaluate actions from previous page (both cumulatively and chronologically)
  115:         if ($ENV{'form.catalogmode'} eq 'groupimport') {
  116: 	    my $acts=$ENV{'form.acts'};
  117: 	    my @Acts=split(/b/,$acts);
  118: 	    my %ahash;
  119: 	    my %achash;
  120: 	    my $ac=0;
  121: 	    # some initial hashes for working with data
  122: 	    foreach (@Acts) {
  123: 		my ($state,$ref)=split(/a/);
  124: 		$ahash{$ref}=$state;
  125: 		$achash{$ref}=$ac;
  126: 		$ac++;
  127: 	    }
  128: 	    # sorting through the actions and changing the tied database hash
  129: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  130: 		my $key=$_;
  131: 		if ($ahash{$key} eq '1') {
  132: 		    $selhash{'store_'.$selhash{'pre_'.$key.'_link'}}=
  133: 			$selhash{'pre_'.$key.'_title'};
  134: 		    $selhash{'storectr_'.$selhash{'pre_'.$key.'_link'}}=
  135: 			$selhash{'storectr'}+0;
  136: 		    $selhash{'storectr'}++;
  137: 		}
  138: 		if ($ahash{$key} eq '0') {
  139: 		    if ($selhash{'store_'.$selhash{'pre_'.$key.'_link'}}) {
  140: 		       delete $selhash{'store_'.$selhash{'pre_'.$key.'_link'}};
  141: 		    }
  142: 		}
  143: 	    }
  144: 	    # deleting the previously cached listing
  145: 	    foreach (keys %selhash) {
  146: 		if ($_ =~ /^pre_/ && $_ =~/link$/) {
  147: 		    my $key = $_;
  148: 		    $key =~ s/^pre_//;
  149: 		    $key =~ s/_[^_]*$//;
  150: 		    delete $selhash{'pre_'.$key.'_title'};
  151: 		    delete $selhash{'pre_'.$key.'_link'};
  152: 		}
  153: 	    }
  154: 	}
  155: # -
  156:         }
  157:     }
  158: # ----------------------------- successtied is now '1' if in working selectmode
  159:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  160:     if ($fatal==1) {
  161:        $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
  162:     }
  163:     my $idx=0;
  164:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  165: 	if (defined($_)) {
  166:             $idx++;
  167:             if ($successtied) { 
  168: 		$r->print('<form name="form'.$idx.'">');
  169:             }
  170: 	    my ($title,$url)=split(/\:/,$_);
  171:             $title=~s/\&colon\;/\:/g;
  172:             $url=~s/\&colon\;/\:/g;
  173:             unless ($title) { $title=(split(/\//,$url))[-1] };
  174:             unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
  175:             if ($url) {
  176: 		if ($successtied) {
  177: 		    my $checked='';
  178: 	           if ($selhash{'store_'.$url}) {
  179: 	       	      $checked=" checked";
  180: 	           }
  181: 	           $selhash{"pre_${idx}_link"}=$url;
  182: 	           $selhash{"pre_${idx}_title"}=$title;
  183: 
  184: 		    $r->print(<<ENDCHECKBOX);
  185: <input type='checkbox' name='filelink' 
  186: value='$url' onClick='javascript:queue("form$idx")'$checked>
  187: <input type='hidden' name='title' value='$title'>
  188: ENDCHECKBOX
  189:                 }
  190: 		$r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
  191:             }
  192:             $r->print(&Apache::lonratsrv::qtescape($title));
  193:             if ($url) { $r->print('</a>'); }
  194:             if ($successtied) {
  195: 		$r->print('</form>');
  196:             } else {
  197: 		$r->print('<br>');
  198:             }
  199:         }
  200:     }
  201:     $r->print('</body></html>');
  202:     if ($successtied) {
  203: 	untie %selhash;
  204:     }
  205: }
  206: 
  207: # ----------------------------------------------------------- Clean out selhash
  208: sub start_fresh_session {
  209:     foreach (keys %selhash) {
  210: 	if ($_ =~ /^pre_/) {
  211: 	    delete $selhash{$_};
  212: 	}
  213: 	if ($_ =~ /^store/) {
  214: 	    delete $selhash{$_};
  215: 	}
  216:     }
  217: }
  218: 
  219: 
  220: # ================================================================ Main Handler
  221: 
  222: sub handler {
  223:    my $r=shift;
  224: 
  225:    if ($r->header_only) {
  226:       &Apache::loncommon::content_type($r,'text/html');
  227:       $r->send_http_header;
  228:       return OK;
  229:    }
  230:  
  231:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  232:                                           ['forceselect','launch']);
  233: 
  234:    my %hash;
  235:    my %bighash;
  236:    my $requrl=$r->uri;
  237: 
  238:    $successtied=0;
  239: # ------------------------------------------------------------ Tie symb db file
  240:   my $disurl='';
  241:   my $dismapid='';
  242:   my $exitdisid = '';
  243:   my $arrow_dir = '';
  244: 
  245:   if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
  246:        my $last;
  247:        if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  248:                     &GDBM_READER(),0640)) {
  249: 	   $last=$hash{'last_direction'};
  250:            untie(%hash);
  251:        }
  252:        my $direction='';
  253:        my $prevmap='';
  254:        if ($last) {
  255: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
  256:        }
  257: # ------------------------------------------------------------- Tie big db file
  258:        if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  259:                     &GDBM_READER(),0640)) {
  260: 	   my $disid='';
  261:            my $randomout ='';
  262: 
  263:            if ($direction eq 'back') {
  264: 	       $disid=$bighash{'map_finish_'.$requrl};
  265:            } else {
  266:                $disid=$bighash{'map_start_'.$requrl};
  267:            }
  268:            if ($disid) {
  269: 	       $disurl=$bighash{'src_'.$disid};
  270:                $dismapid=(split(/\./,$disid))[1];
  271:                $randomout = $bighash{'randomout_'.$disid};
  272:            } elsif (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  273:                     &GDBM_READER(),0640)) {
  274:                $last=$hash{'last_known'};
  275:                untie(%hash);
  276:            }
  277: 
  278: 
  279: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
  280:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
  281: 	       $direction=($direction?$direction:'forward');
  282:                ($disid,$requrl)=
  283:                          &Apache::lonpageflip::fullmove($disid,
  284:                            &Apache::lonnet::declutter($requrl),$direction);
  285:                if ($disid) {
  286: 	           $disurl=$bighash{'src_'.$disid};
  287:                    $dismapid=(split(/\./,$disid))[1];
  288:                    $randomout = $bighash{'randomout_'.$disid};
  289:                }
  290:  	   }
  291:            $exitdisid = $disid;
  292:            $arrow_dir = $direction;
  293: 
  294: # --------------------------------------- Untie hash, make sure to come by here
  295:            untie(%bighash);
  296:        }
  297:    }
  298: 
  299: # now either disurl is set (going to first page), or we need another display
  300:    if ($disurl) {
  301: # -------------------------------------------------- Has first or last resource
  302:       &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
  303: 				'last_known' => [$disurl,$dismapid]); 
  304:       &Apache::loncommon::content_type($r,'text/html');
  305:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
  306:       return REDIRECT;
  307:    } else {
  308:        &Apache::loncommon::content_type($r,'text/html');
  309:        $r->send_http_header;
  310:        if ($exitdisid eq '' && $arrow_dir ne '') {
  311:            my %lt =&Apache::lonlocal::texthash(
  312:                    'back' => 'beginning',
  313:                    'forward' => 'end',
  314:                    'emfo' => 'Empty Folder/Sequence',
  315:                    'nere' => 'Next resource could not be displayed',
  316:                    'goba' => 'Go Back',
  317:                    'nacc' => 'Navigate Course Content',
  318:                           );
  319:            my $warnmsg = 'As all folders and sequences ';
  320:            if ($arrow_dir eq 'forward') {
  321:                $warnmsg .= &mt('following the current resource were empty').',';
  322:            } elsif ($arrow_dir eq 'back') {
  323:                $warnmsg .= &mt('preceding the current resource were empty').',';
  324:            }
  325:            $warnmsg .= &mt('you have now reached the').' '.$lt{$arrow_dir}.' '.&mt('of the course.');
  326:            my $bodytag=&Apache::loncommon::bodytag('Empty Folder/Sequence');
  327:            $r->print(<<ENDNONE);
  328: <head><title>$lt{'emfo'}</title></head>
  329: $bodytag
  330: <h3>$lt{'nere'}</h3>
  331: <p>$warnmsg</p>
  332: <ul>
  333:   <li><a href="javascript:history.go(-1)">$lt{'goba'}</a></li>
  334:   <li><a href="/adm/navmaps">$lt{'nacc'}</a></li>
  335: </ul>
  336: </body>
  337:  </html>
  338: ENDNONE
  339:        } else {
  340:            &viewmap($r,$requrl);
  341:        }
  342:        return OK;
  343:    }
  344: }
  345: 
  346: 1;
  347: __END__
  348: 
  349: 
  350: 
  351: 
  352: 
  353: 
  354: 

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