File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.29: download - view: text, annotated - select for diffs
Thu Nov 2 21:27:58 2006 UTC (17 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
- complete switch to LONCAPA::map

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.29 2006/11/02 21:27:58 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: 
   30: package Apache::lonsequence;
   31: 
   32: use strict;
   33: use Apache::lonnet;
   34: use Apache::Constants qw(:common :http REDIRECT);
   35: use GDBM_File;
   36: use LONCAPA::map();
   37: use Apache::lonpageflip();
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: 
   41: my %selhash;
   42: my $successtied;
   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) {
   50: 	return &LONCAPA::map::attemptread($fn);
   51:     } else {
   52:         return ();
   53:     }
   54: }
   55: 
   56: sub mapread {
   57:     my $fn=shift;
   58:     &Apache::lonnet::repcopy($fn);
   59:     if (-e $fn) {
   60: 	return &LONCAPA::map::mapread($fn,'');
   61:     } else {
   62:         return ();
   63:     }
   64: }
   65: 
   66: # ---------------------------------------------------------------- View Handler
   67: 
   68: sub viewmap {
   69:     my ($r,$url)=@_;
   70: 
   71:     my $js;
   72:     if ($env{'form.forceselect'}) {
   73: 	$js = (<<ENDSCRIPT);
   74: <script type="text/javascript">
   75: 
   76: function select_group() {
   77:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   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:     }
   96: 
   97:     $r->print(&Apache::loncommon::start_page('Map Contents',$js).
   98: 	      '<h1>'.$url.'</h1>');
   99: # ------------------ This is trying to select. Provide buttons and tie %selhash
  100:     if ($env{'form.forceselect'}) { $r->print(<<ENDSELECT);
  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
  107:     my $diropendb = 
  108:     "/home/httpd/perl/tmp/$env{'user.domain'}\_$env{'user.name'}_sel_res.db";
  109:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  110: 	    if ($env{'form.launch'} eq '1') {
  111: 	       &start_fresh_session();
  112: 	    }
  113:             $successtied=1;
  114: 
  115: # - Evaluate actions from previous page (both cumulatively and chronologically)
  116:         if ($env{'form.catalogmode'} eq 'import') {
  117: 	    my $acts=$env{'form.acts'};
  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:         }
  158:     }
  159: # ----------------------------- successtied is now '1' if in working selectmode
  160:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  161:     if ($fatal==1) {
  162:        $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
  163:     }
  164:     my $idx=0;
  165:     foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  166: 	if (defined($_)) {
  167:             $idx++;
  168:             if ($successtied) { 
  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] };
  175:             unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
  176:             if ($url) {
  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: 
  185: 		    $r->print(<<ENDCHECKBOX);
  186: <input type='checkbox' name='filelink' 
  187: value='$url' onClick='javascript:queue("form$idx")'$checked>
  188: <input type='hidden' name='title' value='$title'>
  189: ENDCHECKBOX
  190:                 }
  191: 		$r->print('<a href="'.&LONCAPA::map::qtescape($url).'">');
  192:             }
  193:             $r->print(&LONCAPA::map::qtescape($title));
  194:             if ($url) { $r->print('</a>'); }
  195:             if ($successtied) {
  196: 		$r->print('</form>');
  197:             } else {
  198: 		$r->print('<br>');
  199:             }
  200:         }
  201:     }
  202:     $r->print(&Apache::loncommon::end_page());
  203:     if ($successtied) {
  204: 	untie %selhash;
  205:     }
  206: }
  207: 
  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: 
  221: # ================================================================ Main Handler
  222: 
  223: sub handler {
  224:    my $r=shift;
  225: 
  226:    if ($r->header_only) {
  227:       &Apache::loncommon::content_type($r,'text/html');
  228:       $r->send_http_header;
  229:       return OK;
  230:    }
  231:  
  232:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  233:                                           ['forceselect','launch']);
  234: 
  235:    my %hash;
  236:    my %bighash;
  237:    my $requrl=$r->uri;
  238: 
  239:    $successtied=0;
  240: # ------------------------------------------------------------ Tie symb db file
  241:   my $disurl='';
  242:   my $dismapid='';
  243:   my $exitdisid = '';
  244:   my $arrow_dir = '';
  245: 
  246:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  247:        my $last;
  248:        if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  249:                     &GDBM_READER(),0640)) {
  250: 	   $last=$hash{'last_direction'};
  251:            untie(%hash);
  252:        }
  253:        my $direction='';
  254:        my $prevmap='';
  255:        if ($last) {
  256: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
  257:        }
  258: # ------------------------------------------------------------- Tie big db file
  259:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  260:                     &GDBM_READER(),0640)) {
  261: 	   my $disid='';
  262:            my $randomout ='';
  263: 
  264:            if ($direction eq 'back') {
  265: 	       $disid=$bighash{'map_finish_'.$requrl};
  266:            } else {
  267:                $disid=$bighash{'map_start_'.$requrl};
  268:            }
  269:            if ($disid) {
  270: 	       $disurl=$bighash{'src_'.$disid};
  271:                $dismapid=(split(/\./,$disid))[1];
  272: 	       if (!$env{'request.role.adv'}) {
  273: 		   $randomout = $bighash{'randomout_'.$disid};
  274: 	       }
  275:            } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  276:                     &GDBM_READER(),0640)) {
  277:                $last=$hash{'last_known'};
  278:                untie(%hash);
  279:            }
  280: 
  281: 
  282: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
  283:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
  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];
  291: 		   if (!$env{'request.role.adv'}) {
  292: 		       $randomout = $bighash{'randomout_'.$disid};
  293: 		   }
  294:                }
  295:  	   }
  296:            $exitdisid = $disid;
  297:            $arrow_dir = $direction;
  298: 
  299: # --------------------------------------- Untie hash, make sure to come by here
  300:            untie(%bighash);
  301:        }
  302:    }
  303: 
  304: # now either disurl is set (going to first page), or we need another display
  305:    if ($disurl) {
  306: # -------------------------------------------------- Has first or last resource
  307:       &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
  308: 				'last_known' => [$disurl,$dismapid]); 
  309:       &Apache::loncommon::content_type($r,'text/html');
  310:       $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
  311:       return REDIRECT;
  312:    } else {
  313:        &Apache::loncommon::content_type($r,'text/html');
  314:        $r->send_http_header;
  315:        if ($exitdisid eq '' && $arrow_dir ne '') {
  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:                           );
  323:            my $warnmsg = &mt('As all folders and sequences ');
  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.');
  330:            my $start_page=
  331: 	       &Apache::loncommon::start_page('Empty Folder/Sequence');
  332:            my $end_page=
  333: 	       &Apache::loncommon::end_page();
  334:            $r->print(<<ENDNONE);
  335: $start_page
  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>
  342: $end_page
  343: ENDNONE
  344:        } else {
  345:            &viewmap($r,$requrl);
  346:        }
  347:        return OK;
  348:    }
  349: }
  350: 
  351: 1;
  352: __END__
  353: 
  354: 
  355: 
  356: 
  357: 
  358: 
  359: 

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