File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue Apr 4 15:32:12 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start_page work
- some $_ elimination
- some xhtml cleanup

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.26 2006/04/04 15:32:12 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: 
   74:     my $js;
   75:     if ($env{'form.forceselect'}) {
   76: 	$js = (<<ENDSCRIPT);
   77: <script type="text/javascript">
   78: 
   79: function select_group() {
   80:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   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:     }
   99: 
  100:     $r->print(&Apache::loncommon::start_page('Map Contents',$js).
  101: 	      '<h1>'.$url.'</h1>');
  102: # ------------------ This is trying to select. Provide buttons and tie %selhash
  103:     if ($env{'form.forceselect'}) { $r->print(<<ENDSELECT);
  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
  110:     my $diropendb = 
  111:     "/home/httpd/perl/tmp/$env{'user.domain'}\_$env{'user.name'}_groupsec.db";
  112:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  113: 	    if ($env{'form.launch'} eq '1') {
  114: 	       &start_fresh_session();
  115: 	    }
  116:             $successtied=1;
  117: 
  118: # - Evaluate actions from previous page (both cumulatively and chronologically)
  119:         if ($env{'form.catalogmode'} eq 'groupimport') {
  120: 	    my $acts=$env{'form.acts'};
  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:         }
  161:     }
  162: # ----------------------------- successtied is now '1' if in working selectmode
  163:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  164:     if ($fatal==1) {
  165:        $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
  166:     }
  167:     my $idx=0;
  168:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  169: 	if (defined($_)) {
  170:             $idx++;
  171:             if ($successtied) { 
  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] };
  178:             unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
  179:             if ($url) {
  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: 
  188: 		    $r->print(<<ENDCHECKBOX);
  189: <input type='checkbox' name='filelink' 
  190: value='$url' onClick='javascript:queue("form$idx")'$checked>
  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>'); }
  198:             if ($successtied) {
  199: 		$r->print('</form>');
  200:             } else {
  201: 		$r->print('<br>');
  202:             }
  203:         }
  204:     }
  205:     $r->print(&Apache::loncommon::end_page());
  206:     if ($successtied) {
  207: 	untie %selhash;
  208:     }
  209: }
  210: 
  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: 
  224: # ================================================================ Main Handler
  225: 
  226: sub handler {
  227:    my $r=shift;
  228: 
  229:    if ($r->header_only) {
  230:       &Apache::loncommon::content_type($r,'text/html');
  231:       $r->send_http_header;
  232:       return OK;
  233:    }
  234:  
  235:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  236:                                           ['forceselect','launch']);
  237: 
  238:    my %hash;
  239:    my %bighash;
  240:    my $requrl=$r->uri;
  241: 
  242:    $successtied=0;
  243: # ------------------------------------------------------------ Tie symb db file
  244:   my $disurl='';
  245:   my $dismapid='';
  246:   my $exitdisid = '';
  247:   my $arrow_dir = '';
  248: 
  249:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  250:        my $last;
  251:        if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  252:                     &GDBM_READER(),0640)) {
  253: 	   $last=$hash{'last_direction'};
  254:            untie(%hash);
  255:        }
  256:        my $direction='';
  257:        my $prevmap='';
  258:        if ($last) {
  259: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
  260:        }
  261: # ------------------------------------------------------------- Tie big db file
  262:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  263:                     &GDBM_READER(),0640)) {
  264: 	   my $disid='';
  265:            my $randomout ='';
  266: 
  267:            if ($direction eq 'back') {
  268: 	       $disid=$bighash{'map_finish_'.$requrl};
  269:            } else {
  270:                $disid=$bighash{'map_start_'.$requrl};
  271:            }
  272:            if ($disid) {
  273: 	       $disurl=$bighash{'src_'.$disid};
  274:                $dismapid=(split(/\./,$disid))[1];
  275: 	       if (!$env{'request.role.adv'}) {
  276: 		   $randomout = $bighash{'randomout_'.$disid};
  277: 	       }
  278:            } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  279:                     &GDBM_READER(),0640)) {
  280:                $last=$hash{'last_known'};
  281:                untie(%hash);
  282:            }
  283: 
  284: 
  285: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
  286:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
  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];
  294: 		   if (!$env{'request.role.adv'}) {
  295: 		       $randomout = $bighash{'randomout_'.$disid};
  296: 		   }
  297:                }
  298:  	   }
  299:            $exitdisid = $disid;
  300:            $arrow_dir = $direction;
  301: 
  302: # --------------------------------------- Untie hash, make sure to come by here
  303:            untie(%bighash);
  304:        }
  305:    }
  306: 
  307: # now either disurl is set (going to first page), or we need another display
  308:    if ($disurl) {
  309: # -------------------------------------------------- Has first or last resource
  310:       &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
  311: 				'last_known' => [$disurl,$dismapid]); 
  312:       &Apache::loncommon::content_type($r,'text/html');
  313:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
  314:       return REDIRECT;
  315:    } else {
  316:        &Apache::loncommon::content_type($r,'text/html');
  317:        $r->send_http_header;
  318:        if ($exitdisid eq '' && $arrow_dir ne '') {
  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:                           );
  326:            my $warnmsg = &mt('As all folders and sequences ');
  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.');
  333:            my $start_page=
  334: 	       &Apache::loncommon::start_page('Empty Folder/Sequence');
  335:            my $end_page=
  336: 	       &Apache::loncommon::end_page();
  337:            $r->print(<<ENDNONE);
  338: $start_page
  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>
  345: $end_page
  346: ENDNONE
  347:        } else {
  348:            &viewmap($r,$requrl);
  349:        }
  350:        return OK;
  351:    }
  352: }
  353: 
  354: 1;
  355: __END__
  356: 
  357: 
  358: 
  359: 
  360: 
  361: 
  362: 

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