Annotation of rat/lonsequence.pm, revision 1.44

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

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