File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.55: download - view: text, annotated - select for diffs
Wed Nov 30 16:02:27 2016 UTC (7 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Set offset to 0 if no LON-CAPA menu present.

    1: # The LearningOnline Network with CAPA
    2: # Wrapper for external and binary files as standalone resources
    3: #
    4: # $Id: lonwrapper.pm,v 1.55 2016/11/30 16:02:27 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: package Apache::lonwrapper;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common);
   34: use Apache::lonenc();
   35: use Apache::lonnet;
   36: use Apache::lonlocal;
   37: use Apache::loncommon();
   38: use Apache::lonhtmlcommon();
   39: use Apache::lonextresedit();
   40: use Apache::lonexttool();
   41: use LONCAPA qw(:DEFAULT :match);;
   42: 
   43: # ================================================================ Main Handler
   44: sub wrapper {
   45:     my ($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$title) = @_;
   46: 
   47:     my $forcereg;
   48:     unless ($env{'form.folderpath'}) {
   49:         $forcereg = 1;
   50:     }
   51:     my %lt = &Apache::lonlocal::texthash(
   52:                                           'noif' => 'No iframe support.',
   53:                                           'show' => 'Show content in pop-up window',
   54:                                         );
   55: 
   56:     my $anchor;
   57:     if ($is_ext) {
   58:         if ($env{'form.symb'}) {
   59:             (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
   60:             if ($res =~ /(#[^#]+)$/) {
   61:                 $anchor = $1;
   62:             }
   63:         } elsif ($env{'form.anchor'} ne '') {
   64:             $anchor = '#'.$env{'form.anchor'};
   65:         }
   66:     }
   67: 
   68:     my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
   69:     my $args = {'bgcolor' => '#FFFFFF'};
   70:     if ($forcereg) {
   71:         $args->{'force_register'} = $forcereg;
   72:     }
   73:     if (ref($brcrum) eq 'ARRAY') {
   74:         $args->{'bread_crumbs'} = $brcrum;
   75:     }
   76:     if ($absolute) {
   77:         $args->{'use_absolute'} = $absolute; 
   78:     }
   79: 
   80:     my $startpage = &Apache::loncommon::start_page('Menu',undef,$args);
   81:     my $endpage = &Apache::loncommon::end_page();
   82:     
   83:     if (($env{'browser.mobile'}) || ($exttool eq 'window')) {
   84:         my $output = $startpage;
   85:         if ($is_pdf) {
   86:             if ($title eq '') {
   87:                 $title = $env{'form.title'};
   88:                 if ($title eq '') {
   89:                     unless ($env{'request.enc'}) {
   90:                         ($title) = ($url =~ m{/([^/]+)$});
   91:                         $title =~ s/(\?[^\?]+)$//;
   92:                     }
   93:                 }
   94:             }
   95:             unless ($title eq '') {
   96:                 $output .= $title.'<br />';
   97:             }
   98:             $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
   99:         } elsif ($exttool eq 'window') {
  100:             $output .= '<div>'.
  101:                        '<a href="'.$url.'" target="LC_LTI" style="padding:0;clear:both;margin:0;border:0">'.
  102:                        &mt('Launch External Tool').'</a>'.
  103:                        '</div>';
  104:         } else {
  105:             $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  106:                        '<iframe src="'.$url.$anchor.'" height="100%" width="100%" frameborder="0">'."\n".
  107:                        "$lt{'noif'} $noiframe\n".
  108:                        "</iframe>\n".
  109:                        "</div>\n";
  110:         }
  111:         $output .= $endpage;
  112:         return $output;
  113:     } else {
  114:         my $offset = 5;
  115:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  116:         if ($env{'form.inhibitmenu'} eq 'yes') {
  117:             $offset = 0;
  118:         }
  119:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  120:         \$(document).ready( function() {
  121:             \$(window).unbind('resize').resize(function(){
  122:                 var header = null;
  123:                 var offset = $offset;
  124:                 var height = 0;
  125:                 var hdrtop = 0;
  126:                 if (\$('div.LC_head_subbox:first').length) {
  127:                     header = \$('div.LC_head_subbox:first');
  128:                     offset = 9;
  129:                 } else {
  130:                     if (\$('#LC_breadcrumbs').length) {
  131:                         header = \$('#LC_breadcrumbs');
  132:                     }
  133:                 }
  134:                 if (header != null && header.length) {
  135:                     height = header.height();
  136:                     hdrtop = header.position().top;
  137:                 }
  138:                 var pos = height + hdrtop + offset;
  139:                 \$('.LC_iframecontainer').css('top', pos);
  140:             });
  141:         });
  142:         window.onload = function(){  \$(window).trigger('resize') };
  143: SCRIPT
  144:         # javascript will position the iframe if window was resized (or zoomed)
  145:         return <<ENDFRAME;
  146:         $startpage
  147:         $script
  148:         <div class="LC_iframecontainer">
  149:             <iframe src="$url$anchor">$lt{'noif'} $noiframe</iframe>
  150:         </div>
  151:         $endpage
  152: ENDFRAME
  153:     }
  154: }
  155: 
  156: sub handler {
  157:     my $r=shift;
  158:     &Apache::loncommon::content_type($r,'text/html');
  159:     $r->send_http_header;
  160: 
  161:     return OK if $r->header_only;
  162: 
  163:     my $url = $r->uri;
  164:     my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum);
  165: 
  166:     for ($url){
  167:         s|^/adm/wrapper||;
  168:         $is_ext = $_ =~ s|^/ext/|http://|;         
  169:         s|http://https://|https://|;
  170:         s|&colon;|:|g;              
  171:     }
  172: 
  173: 
  174:     if ($url =~ /\.pdf$/i) {
  175:         $is_pdf = 1;
  176:     } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/exttools?$}) {
  177:         $cdom = $1;
  178:         $cnum = $2;
  179:         my $marker = $3;
  180:         $exttool = 'iframe';
  181:         my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target'],$cdom,$cnum);
  182:         if ($toolhash{'target'} eq 'window') {
  183:            $exttool = 'window'; 
  184:         }
  185:     }
  186:     if (($is_ext) || ($exttool)) {
  187:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  188:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  189:         if (($env{'form.forceedit'}) &&
  190:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  191:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  192:              ($env{'form.symb'} =~ /^uploaded/))) {
  193:             if ($env{'form.symb'}) {
  194:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  195:                 if ($res =~ /(#[^#]+)$/) {
  196:                     $url .= $1;
  197:                 }
  198:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  199:                 if ($env{'form.anchor'} ne '') {
  200:                     $url .= '#'.$env{'form.anchor'};
  201:                 }
  202:             }
  203:             my $type = 'ext';
  204:             my %ltitools;
  205:             if ($exttool) {
  206:                 $type = 'tool';
  207:                 %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
  208:             }
  209:             $r->print(
  210:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  211:                                                        $env{'form.symb'},
  212:                                                        $env{'form.idx'},$type,$cdom,
  213:                                                        $cnum,\%ltitools));
  214:             return OK;
  215:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  216:             my $crstype = &Apache::loncommon::course_type();
  217:             my $title = $env{'form.title'};
  218:             if ($title eq '') {
  219:                 if ($is_ext) {
  220:                     $title = &mt('External Resource');
  221:                 } else {
  222:                     $title = &mt('External Tool');
  223:                 }
  224:             }
  225:             $brcrum =
  226:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  227:         }
  228:     }
  229: 
  230: #
  231: # Actual URL
  232: #
  233:     if ($url=~/$LONCAPA::assess_re/) {
  234: #
  235: # This is uploaded homework
  236: #
  237:         $env{'request.state'}='uploaded';
  238:         &Apache::lonhomework::renderpage($r,$url);
  239:     } else {
  240: #
  241: # This is not homework
  242: #
  243:         if (($is_ext) || ($exttool)) {
  244:             $absolute = $env{'request.use_absolute'};
  245:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  246:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  247:         }
  248: 
  249:         unless ($ENV{'QUERY_STRING'} eq '') {
  250:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  251:         }
  252: 
  253:         # encrypt url if not external
  254:         unless ($is_ext || $exttool) {
  255:             &Apache::lonenc::check_encrypt(\$url);
  256:         }
  257: 
  258:         $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool) );
  259: 
  260:     } # not just the menu
  261:     
  262:     return OK;
  263: } # handler
  264: 
  265: 1;
  266: __END__
  267: 
  268: =pod
  269: 
  270: =head1 NAME
  271: 
  272: Apache::lonwrapper - External and binary file management.
  273: 
  274: =head1 SYNOPSIS
  275: 
  276: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  277: 
  278: This is part of the LearningOnline Network with CAPA project
  279: described at http://www.lon-capa.org.
  280: 
  281: =head1 Subroutines
  282: 
  283: =over
  284: 
  285: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$title))
  286: 
  287: =over
  288: 
  289: =item $url
  290: 
  291: url to display by including in an iframe within a
  292: LON-CAPA page which has a standard LON-CAPA inline menu.
  293: 
  294: =item $brcrum
  295: 
  296: breadcrumbs for unregistered urls
  297: (i.e., external resources in Supplemental Content).
  298: 
  299: =item $absolute
  300: 
  301: contains protocol (http or https) followed by
  302: the hostname, if menu items in the standard LON-CAPA
  303: interface created by the call to loncommon::start_page()
  304: within &wrapper() need to use absolute URLs rather than
  305: relative URLs.
  306: 
  307: That will be the case where an external resource has been 
  308: served from port 80, when the server customarily serves
  309: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  310: is used to switch requests for external resources and
  311: the syllabus: /public/<domain>/<courseid>/syllabus
  312: (which might also point at an external resource)
  313: from https:// to http:// where the the URL of the remote site 
  314: specified in the resource itself is http://.
  315: 
  316: This is done to avoid default mixed content blocking
  317: in Firefox 23 and later, when serving from Apache/SSL.
  318: 
  319: =item $is_ext
  320: 
  321: true if URL is for an external resource.
  322: 
  323: =item $is_pdf
  324: 
  325: true if URL is for a PDF (based on file extension).
  326: 
  327: =item $title
  328: 
  329: optional. If wrapped item is a PDF, and $env{'browser.mobile'} 
  330: is true, a link to a PDF is shown. The "title" will be displayed
  331: above the link, but if not provided as an arg, $env{'form.title'}
  332: will be used, otherwise, the filename will be displayed (unless
  333: hidden URL set for the resource).
  334: 
  335: =back
  336: 
  337: Returns markup for the entire page.
  338: 
  339: =item handler()
  340: 
  341: =back
  342: 
  343: =cut
  344: 

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