Annotation of rat/lonsequence.pm, revision 1.40

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

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