Annotation of rat/lonsequence.pm, revision 1.29

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

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