Annotation of rat/lonsequence.pm, revision 1.26

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

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