Annotation of rat/lonsequence.pm, revision 1.30

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

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