Annotation of rat/lonsequence.pm, revision 1.22

1.1       www         1: # The LearningOnline Network with CAPA
                      2: #
                      3: # Sequence Handler
                      4: #
1.22    ! albertel    5: # $Id: lonsequence.pm,v 1.21 2004/10/16 01:14:28 albertel Exp $
1.5       www         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: #
1.1       www        29: # (Handler to resolve ambiguous file locations
                     30: #
                     31: 
1.3       www        32: package Apache::lonsequence;
1.1       www        33: 
                     34: use strict;
                     35: use Apache::lonnet;
1.3       www        36: use Apache::Constants qw(:common :http REDIRECT);
1.1       www        37: use GDBM_File;
1.8       www        38: use Apache::lonratedt;
                     39: use Apache::lonratsrv;
1.11      www        40: use Apache::lonpageflip;
1.14      www        41: use Apache::loncommon;
1.16      www        42: use Apache::lonlocal;
1.1       www        43: 
1.12      www        44: my %selhash;
                     45: my $successtied;
1.8       www        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: 
1.15      www        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: 
1.8       www        69: # ---------------------------------------------------------------- View Handler
                     70: 
                     71: sub viewmap {
1.9       www        72:     my ($r,$url)=@_;
1.8       www        73:     $r->print('<html>');
                     74:     if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
                     75: <script>
                     76: 
                     77: function select_group() {
1.12      www        78:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
1.8       www        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:     }
1.14      www        97:     $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
1.12      www        98: # ------------------ This is trying to select. Provide buttons and tie %selhash
1.8       www        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
1.12      www       106:     my $diropendb = 
                    107:     "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
1.13      albertel  108:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.12      www       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:         }
1.8       www       157:     }
1.12      www       158: # ----------------------------- successtied is now '1' if in working selectmode
1.15      www       159:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
                    160:     if ($fatal==1) {
1.16      www       161:        $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
1.15      www       162:     }
1.8       www       163:     my $idx=0;
                    164:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
                    165: 	if (defined($_)) {
                    166:             $idx++;
1.12      www       167:             if ($successtied) { 
1.8       www       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] };
1.16      www       174:             unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
1.8       www       175:             if ($url) {
1.12      www       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: 
1.8       www       184: 		    $r->print(<<ENDCHECKBOX);
                    185: <input type='checkbox' name='filelink' 
1.12      www       186: value='$url' onClick='javascript:queue("form$idx")'$checked>
1.8       www       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>'); }
1.12      www       194:             if ($successtied) {
1.8       www       195: 		$r->print('</form>');
                    196:             } else {
                    197: 		$r->print('<br>');
                    198:             }
                    199:         }
                    200:     }
                    201:     $r->print('</body></html>');
1.12      www       202:     if ($successtied) {
                    203: 	untie %selhash;
                    204:     }
1.8       www       205: }
                    206: 
1.12      www       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: 
1.1       www       220: # ================================================================ Main Handler
                    221: 
                    222: sub handler {
                    223:    my $r=shift;
                    224: 
                    225:    if ($r->header_only) {
1.16      www       226:       &Apache::loncommon::content_type($r,'text/html');
1.1       www       227:       $r->send_http_header;
                    228:       return OK;
                    229:    }
1.8       www       230:  
                    231:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.12      www       232:                                           ['forceselect','launch']);
1.1       www       233: 
1.2       www       234:    my %hash;
1.1       www       235:    my %bighash;
1.2       www       236:    my $requrl=$r->uri;
                    237: 
1.12      www       238:    $successtied=0;
1.3       www       239: # ------------------------------------------------------------ Tie symb db file
1.9       www       240:   my $disurl='';
                    241:   my $dismapid='';
1.18      raeburn   242:   my $exitdisid = '';
                    243:   my $arrow_dir = '';
1.9       www       244: 
                    245:   if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
1.2       www       246:        my $last;
                    247:        if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.13      albertel  248:                     &GDBM_READER(),0640)) {
1.2       www       249: 	   $last=$hash{'last_direction'};
                    250:            untie(%hash);
                    251:        }
1.3       www       252:        my $direction='';
                    253:        my $prevmap='';
                    254:        if ($last) {
                    255: 	  ($prevmap,$direction)=(split(/\_\_\_/,$last));
                    256:        }
                    257: # ------------------------------------------------------------- Tie big db file
1.1       www       258:        if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.13      albertel  259:                     &GDBM_READER(),0640)) {
1.3       www       260: 	   my $disid='';
1.17      raeburn   261:            my $randomout ='';
1.10      www       262: 
1.3       www       263:            if ($direction eq 'back') {
                    264: 	       $disid=$bighash{'map_finish_'.$requrl};
                    265:            } else {
                    266:                $disid=$bighash{'map_start_'.$requrl};
1.18      raeburn   267:            }
1.3       www       268:            if ($disid) {
                    269: 	       $disurl=$bighash{'src_'.$disid};
1.4       www       270:                $dismapid=(split(/\./,$disid))[1];
1.17      raeburn   271:                $randomout = $bighash{'randomout_'.$disid};
1.18      raeburn   272:            } elsif (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
                    273:                     &GDBM_READER(),0640)) {
                    274:                $last=$hash{'last_known'};
                    275:                untie(%hash);
1.3       www       276:            }
1.18      raeburn   277: 
                    278: 
1.17      raeburn   279: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
1.18      raeburn   280:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
1.11      www       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];
1.19      raeburn   288:                    $randomout = $bighash{'randomout_'.$disid};
1.11      www       289:                }
                    290:  	   }
1.18      raeburn   291:            $exitdisid = $disid;
                    292:            $arrow_dir = $direction;
1.11      www       293: 
1.3       www       294: # --------------------------------------- Untie hash, make sure to come by here
                    295:            untie(%bighash);
1.9       www       296:        }
                    297:    }
                    298: 
                    299: # now either disurl is set (going to first page), or we need another display
                    300:    if ($disurl) {
1.3       www       301: # -------------------------------------------------- Has first or last resource
1.9       www       302:       &Apache::lonnet::symblist($requrl,$disurl => $dismapid,
1.7       www       303:                    'last_known' => &Apache::lonnet::declutter($disurl)); 
1.16      www       304:       &Apache::loncommon::content_type($r,'text/html');
1.9       www       305:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
                    306:       return REDIRECT;
1.1       www       307:    } else {
1.16      www       308:        &Apache::loncommon::content_type($r,'text/html');
1.9       www       309:        $r->send_http_header;
1.21      albertel  310:        if ($exitdisid eq '' && $arrow_dir ne '') {
1.18      raeburn   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.');
1.20      www       326:            my $bodytag=&Apache::loncommon::bodytag('Empty Folder/Sequence');
1.18      raeburn   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:        }
1.9       www       342:        return OK;
1.1       www       343:    }
                    344: }
                    345: 
                    346: 1;
                    347: __END__
                    348: 
                    349: 
                    350: 
                    351: 
                    352: 
                    353: 
                    354: 

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