Annotation of rat/lonsequence.pm, revision 1.34

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

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