File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.37: download - view: text, annotated - select for diffs
Mon Feb 16 13:52:04 2009 UTC (15 years, 2 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Replaced hardcoded style by LON-CAPA CSS style for order warning message

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

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