Annotation of rat/lonsequence.pm, revision 1.33

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

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