File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.45: download - view: text, annotated - select for diffs
Mon Dec 15 00:46:52 2014 UTC (9 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, version_2_11_1, HEAD
- Coding style:
 - keys()
 - for readability eliminate use of $_

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.45 2014/12/15 00:46:52 raeburn 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 LONCAPA;
   40: use Apache::lonpageflip();
   41: use Apache::loncommon();
   42: use Apache::groupsort();
   43: use Apache::lonlocal;
   44: use HTML::Entities();
   45: 
   46: my %selhash;
   47: my $successtied;
   48: 
   49: # ----------------------------------------- Attempt to read from resource space
   50: 
   51: sub attemptread {
   52:     my ($fn,$unsorted)=@_;
   53:     &Apache::lonnet::repcopy($fn);
   54:     if (-e $fn) {
   55: 	return &LONCAPA::map::attemptread($fn,$unsorted);
   56:     } else {
   57:         return ();
   58:     }
   59: }
   60: 
   61: sub mapread {
   62:     my $fn=shift;
   63:     &Apache::lonnet::repcopy($fn);
   64:     if (-e $fn) {
   65: 	return &LONCAPA::map::mapread($fn,'');
   66:     } else {
   67:         return ();
   68:     }
   69: }
   70: 
   71: # ---------------------------------------------------------------- View Handler
   72: 
   73: sub viewmap {
   74:     my ($r,$url)=@_;
   75: 
   76:     my $js;
   77:     if ($env{'form.forceselect'}) {
   78: 	$js = (<<ENDSCRIPT);
   79: <script type="text/javascript">
   80: 
   81: function select_group() {
   82:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   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:     }
  101: 
  102:     $r->print(&Apache::loncommon::start_page('Map Contents',$js).
  103: 	      '<h1>'.$url.'</h1>');
  104: # ------------------ This is trying to select. Provide buttons and tie %selhash
  105:     if ($env{'form.forceselect'}) { $r->print(<<ENDSELECT);
  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()" />
  110: </form>   
  111: ENDSELECT
  112:     my $diropendb = 
  113:   LONCAPA::tempdir() .
  114:     "$env{'user.domain'}\_$env{'user.name'}_sel_res.db";
  115:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  116: 	    if ($env{'form.launch'} eq '1') {
  117: 	       &start_fresh_session();
  118: 	    }
  119:             $successtied=1;
  120: 
  121: # - Evaluate actions from previous page (both cumulatively and chronologically)
  122: 	    if ($env{'form.catalogmode'} eq 'import') {
  123: 		&Apache::groupsort::update_actions_hash(\%selhash);
  124: 	    }
  125: # -
  126:         }
  127:     }
  128: # ----------------------------- successtied is now '1' if in working selectmode
  129:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  130:     if ($fatal==1) {
  131:        $r->print('<p class="LC_warning">'
  132:                 .&mt('Map contents are not shown in order.')
  133:                 .'</p><br />');
  134:     }
  135:     my $idx=0;
  136:     foreach my $entry (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  137: 	if (defined($entry)) {
  138:             $idx++;
  139:             if ($successtied) { 
  140: 		$r->print('<form name="form'.$idx.'">');
  141:             }
  142: 	    my ($title,$url)=split(/\:/,$entry);
  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,'\'"<>&');
  152:             if ($url) {
  153: 		if ($successtied) {
  154: 		    my $checked='';
  155: 	           if ($selhash{'store_'.$url}) {
  156: 	       	      $checked=' checked="checked"';
  157: 	           }
  158: 	           $selhash{"pre_${idx}_link"}=$url;
  159: 	           $selhash{"pre_${idx}_title"}=$title;
  160: 		    
  161: 		    $url  = &HTML::Entities::encode($url, '\'"<>&');
  162: 		    $r->print(<<ENDCHECKBOX);
  163: <input type='checkbox' name='filelink' 
  164: value='$enc_url' onClick='javascript:queue("form$idx")'$checked />
  165: <input type='hidden' name='title' value='$enc_title' />
  166: ENDCHECKBOX
  167:                 }
  168: 		$r->print('<a href="'.$enc_url.'">');
  169:             }
  170:             $r->print($enc_title);
  171:             if ($url) { $r->print('</a>'); }
  172:             if ($successtied) {
  173: 		$r->print('</form>');
  174:             } else {
  175: 		$r->print('<br />');
  176:             }
  177:         }
  178:     }
  179:     $r->print(&Apache::loncommon::end_page());
  180:     if ($successtied) {
  181: 	untie %selhash;
  182:     }
  183: }
  184: 
  185: # ----------------------------------------------------------- Clean out selhash
  186: sub start_fresh_session {
  187:     foreach my $item (keys(%selhash)) {
  188: 	if ($item =~ /^pre_/) {
  189: 	    delete $selhash{$item};
  190: 	}
  191: 	if ($item =~ /^store/) {
  192: 	    delete $selhash{$item};
  193: 	}
  194:     }
  195: }
  196: 
  197: 
  198: # ================================================================ Main Handler
  199: 
  200: sub handler {
  201:    my $r=shift;
  202: 
  203:    if ($r->header_only) {
  204:       &Apache::loncommon::content_type($r,'text/html');
  205:       $r->send_http_header;
  206:       return OK;
  207:    }
  208:  
  209:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                           ['forceselect','launch']);
  211: 
  212:    my %hash;
  213:    my %bighash;
  214:    my $requrl=$r->uri;
  215: 
  216:    $successtied=0;
  217: # ------------------------------------------------------------ Tie symb db file
  218:   my $disurl='';
  219:   my $dismapid='';
  220:   my $exitdisid = '';
  221:   my $arrow_dir = '';
  222:   my $is_encrypted = '';
  223: 
  224:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  225:        my $last;
  226:        if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  227:                     &GDBM_READER(),0640)) {
  228: 	   $last=$hash{'last_direction'};
  229:            untie(%hash);
  230:        }
  231:        my $direction='';
  232:        my $prevmap='';
  233:        if ($last) {
  234: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
  235:        }
  236: # ------------------------------------------------------------- Tie big db file
  237:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  238:                     &GDBM_READER(),0640)) {
  239: 	   my $disid='';
  240:            my $randomout ='';
  241: 
  242:            if ($direction eq 'back') {
  243: 	       $disid=$bighash{'map_finish_'.$requrl};
  244:            } else {
  245:                $disid=$bighash{'map_start_'.$requrl};
  246:            }
  247:            if ($disid) {
  248: 	       $disurl=$bighash{'src_'.$disid};
  249:                $dismapid=(split(/\./,$disid))[1];
  250: 	       if (!$env{'request.role.adv'}) {
  251: 		   $randomout = $bighash{'randomout_'.$disid};
  252: 	       }
  253:                if (!$env{'request.role.adv'}) {
  254:                    $is_encrypted = $bighash{'encrypted_'.$disid};
  255:                }
  256:            } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  257:                     &GDBM_READER(),0640)) {
  258:                $last=$hash{'last_known'};
  259:                untie(%hash);
  260:            }
  261: 
  262: 
  263: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
  264:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
  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];
  272: 		   if (!$env{'request.role.adv'}) {
  273: 		       $randomout = $bighash{'randomout_'.$disid};
  274: 		   }
  275:                    if (!$env{'request.role.adv'}) {
  276:                        $is_encrypted = $bighash{'encrypted_'.$disid};
  277:                    }
  278:                }
  279:  	   }
  280:            $exitdisid = $disid;
  281:            $arrow_dir = $direction;
  282: 
  283: # --------------------------------------- Untie hash, make sure to come by here
  284:            untie(%bighash);
  285:        }
  286:    }
  287: 
  288: # now either disurl is set (going to first page), or we need another display
  289:    if ($disurl) {
  290: # -------------------------------------------------- Has first or last resource
  291:       my $showdisurl = $disurl;
  292:       if ($is_encrypted) {
  293:           $showdisurl = &Apache::lonenc::encrypted($disurl);
  294:       }
  295:       &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
  296: 				'last_known' => [$disurl,$dismapid]); 
  297:       &Apache::loncommon::content_type($r,'text/html');
  298:       $r->header_out(Location => &Apache::lonnet::absolute_url($ENV{'SERVER_NAME'}).
  299:                                  $showdisurl);
  300:       return REDIRECT;
  301:    } else {
  302:        &Apache::loncommon::content_type($r,'text/html');
  303:        $r->send_http_header;
  304:        if ($exitdisid eq '' && $arrow_dir ne '') {
  305:            my %lt =&Apache::lonlocal::texthash(
  306:                    'nere' => 'Next resource could not be displayed',
  307:                    'goba' => 'Go Back',
  308:                    'nacc' => 'Course Contents',
  309:                           );
  310:            if (&Apache::loncommon::course_type() eq 'Community') {
  311:                $lt{'nav'} = &mt('Community Contents');
  312:            }
  313:            my $warnmsg;
  314:            if ($arrow_dir eq 'forward') {
  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.');
  318:            } elsif ($arrow_dir eq 'back') {
  319:                $warnmsg = &mt('As all folders and sequences '
  320:                              .'preceding the current resource were empty, '
  321:                              .'you have now reached the beginning of the course.');
  322:            }
  323:            my $start_page=
  324: 	       &Apache::loncommon::start_page('Empty Folder/Sequence');
  325:            my $end_page=
  326: 	       &Apache::loncommon::end_page();
  327:            $r->print(<<ENDNONE);
  328: $start_page
  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>
  335: $end_page
  336: ENDNONE
  337:        } else {
  338:            &viewmap($r,$requrl);
  339:        }
  340:        return OK;
  341:    }
  342: }
  343: 
  344: 1;
  345: __END__
  346: 
  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()
  368: 
  369: =item mapread()
  370: 
  371: =item start_fresh_session()
  372: 
  373: =back
  374: 
  375: =cut
  376: 
  377: 
  378: 
  379: 
  380: 

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