File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.31: download - view: text, annotated - select for diffs
Wed Jul 11 23:51:02 2007 UTC (16 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- elimintate some copyied code

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

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