File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.63: download - view: text, annotated - select for diffs
Wed May 10 13:49:21 2017 UTC (6 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754. Make LON-CAPA an LTI Tool Consumer (LTI 1.1).
  - Display options are now: iframe, tab and window.

    1: # The LearningOnline Network with CAPA
    2: # Wrapper for external and binary files as standalone resources
    3: #
    4: # $Id: lonwrapper.pm,v 1.63 2017/05/10 13:49:21 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,$linktext,$explanation,
   46:         $title,$width,$height) = @_;
   47: 
   48:     my $forcereg;
   49:     unless ($env{'form.folderpath'}) {
   50:         $forcereg = 1;
   51:     }
   52:     my %lt = &Apache::lonlocal::texthash(
   53:                                           'noif' => 'No iframe support.',
   54:                                           'show' => 'Show content in pop-up window',
   55:                                         );
   56: 
   57:     my $anchor;
   58:     if ($is_ext) {
   59:         if ($env{'form.symb'}) {
   60:             (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
   61:             if ($res =~ /(#[^#]+)$/) {
   62:                 $anchor = $1;
   63:             }
   64:         } elsif ($env{'form.anchor'} ne '') {
   65:             $anchor = '#'.$env{'form.anchor'};
   66:         }
   67:     }
   68: 
   69:     my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
   70:     my $args = {'bgcolor' => '#FFFFFF'};
   71:     if ($forcereg) {
   72:         $args->{'force_register'} = $forcereg;
   73:     }
   74:     if (ref($brcrum) eq 'ARRAY') {
   75:         $args->{'bread_crumbs'} = $brcrum;
   76:     }
   77:     if ($absolute) {
   78:         $args->{'use_absolute'} = $absolute;
   79:     }
   80:     if ($env{'form.only_body'}) {
   81:         $args->{'only_body'} = $env{'form.only_body'};
   82:     }
   83: 
   84:     my $startpage = &Apache::loncommon::start_page('Menu',undef,$args);
   85:     my $endpage = &Apache::loncommon::end_page();
   86:     
   87:     if (($env{'browser.mobile'}) || ($exttool eq 'window') || ($exttool eq 'tab')) {
   88:         my $output = $startpage;
   89:         if ($is_pdf) {
   90:             if ($title eq '') {
   91:                 $title = $env{'form.title'};
   92:                 if ($title eq '') {
   93:                     unless ($env{'request.enc'}) {
   94:                         ($title) = ($url =~ m{/([^/]+)$});
   95:                         $title =~ s/(\?[^\?]+)$//;
   96:                     }
   97:                 }
   98:             }
   99:             unless ($title eq '') {
  100:                 $output .= $title.'<br />';
  101:             }
  102:             $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
  103:         } elsif (($exttool eq 'window') || ($exttool eq 'tab')) {
  104:             if ($linktext eq '') {
  105:                 $linktext = &mt('Launch External Tool');
  106:             }
  107:             if ($exttool eq 'tab') {
  108:                 $output .= '<div>'.
  109:                            '<a href="'.$url.'" target="LCExternalToolTab" style="padding:0;clear:both;margin:0;border:0">'.
  110:                            $linktext.'</a>'.
  111:                            '</div>';
  112:             } else {
  113:                 $output .= <<"ENDLINK";
  114: <script type="text/javascript">
  115: var windowObjectReference = null; 
  116: var PreviousUrl; 
  117:                            
  118: function openSinglePopup(strUrl) {
  119:     if (windowObjectReference == null || windowObjectReference.closed) {
  120:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  121:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  122:     } else if(PreviousUrl != strUrl) {
  123:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  124:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  125:         windowObjectReference.focus();
  126:     } else {
  127:         windowObjectReference.focus();
  128:     };
  129:     PreviousUrl = strUrl;
  130: }
  131: </script>
  132: <div>
  133: <a href="$url" target="LCExternalToolPopUp" onclick="openSinglePopup(this.href); return false;">
  134: $linktext</a>
  135: </div>
  136: ENDLINK
  137:             }
  138:             if ($explanation ne '') {
  139:                 $output .= '<div>'.$explanation.'</div>';
  140:             }
  141:         } else {
  142:             $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  143:                        '<iframe src="'.$url.$anchor.'" height="100%" width="100%" frameborder="0">'."\n".
  144:                        "$lt{'noif'} $noiframe\n".
  145:                        "</iframe>\n".
  146:                        "</div>\n";
  147:         }
  148:         $output .= $endpage;
  149:         return $output;
  150:     } else {
  151:         my $offset = 5;
  152:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  153:         if ($env{'form.inhibitmenu'} eq 'yes') {
  154:             $offset = 0;
  155:         }
  156:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  157:         \$(document).ready( function() {
  158:             \$(window).unbind('resize').resize(function(){
  159:                 var header = null;
  160:                 var offset = $offset;
  161:                 var height = 0;
  162:                 var hdrtop = 0;
  163:                 if (\$('div.LC_head_subbox:first').length) {
  164:                     header = \$('div.LC_head_subbox:first');
  165:                     offset = 9;
  166:                 } else {
  167:                     if (\$('#LC_breadcrumbs').length) {
  168:                         header = \$('#LC_breadcrumbs');
  169:                     }
  170:                 }
  171:                 if (header != null && header.length) {
  172:                     height = header.height();
  173:                     hdrtop = header.position().top;
  174:                 }
  175:                 var pos = height + hdrtop + offset;
  176:                 \$('.LC_iframecontainer').css('top', pos);
  177:             });
  178:         });
  179:         window.onload = function(){  \$(window).trigger('resize') };
  180: SCRIPT
  181:         # javascript will position the iframe if window was resized (or zoomed)
  182:         return <<ENDFRAME;
  183:         $startpage
  184:         $script
  185:         <div class="LC_iframecontainer">
  186:             <iframe src="$url$anchor">$lt{'noif'} $noiframe</iframe>
  187:         </div>
  188:         $endpage
  189: ENDFRAME
  190:     }
  191: }
  192: 
  193: sub handler {
  194:     my $r=shift;
  195:     &Apache::loncommon::content_type($r,'text/html');
  196:     $r->send_http_header;
  197: 
  198:     return OK if $r->header_only;
  199: 
  200:     my $url = $r->uri;
  201:     my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
  202:         $linktext,$explanation,$width,$height);
  203: 
  204:     for ($url){
  205:         s|^/adm/wrapper||;
  206:         $is_ext = $_ =~ s|^/ext/|http://|;         
  207:         s|http://https://|https://|;
  208:         s|&colon;|:|g;              
  209:     }
  210: 
  211: 
  212:     if ($url =~ /\.pdf$/i) {
  213:         $is_pdf = 1;
  214:     } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
  215:         $cdom = $1;
  216:         $cnum = $2;
  217:         my $marker = $3;
  218:         $exttool = 'iframe';
  219:         my $exttoolremote;
  220:         my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
  221:                                             $cdom,$cnum);
  222:         if ($toolhash{'id'}) {
  223:             my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
  224:             if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
  225:                 $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
  226:             }
  227:         }
  228:         if ($toolhash{'target'} eq 'window') {
  229:             $exttool = 'window';
  230:             $width = $toolhash{'width'};
  231:             $height = $toolhash{'height'};
  232:         } elsif ($toolhash{'target'} eq 'tab') {
  233:             $exttool = 'tab'; 
  234:         }
  235:         if (($exttool eq 'window') || ($exttool eq 'tab')) {
  236:             $linktext = $toolhash{'linktext'}; 
  237:             $explanation = $toolhash{'explanation'};
  238:         } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  239:             $exttool = 'tab';
  240:         }
  241:     }
  242:     if (($is_ext) || ($exttool)) {
  243:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  244:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  245:         if (($env{'form.forceedit'}) &&
  246:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  247:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  248:              ($env{'form.symb'} =~ /^uploaded/))) {
  249:             if ($env{'form.symb'}) {
  250:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  251:                 if ($res =~ /(#[^#]+)$/) {
  252:                     $url .= $1;
  253:                 }
  254:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  255:                 if ($env{'form.anchor'} ne '') {
  256:                     $url .= '#'.$env{'form.anchor'};
  257:                 }
  258:             }
  259:             my $type = 'ext';
  260:             if ($exttool) {
  261:                 $type = 'tool';
  262:             } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  263:                 $hostname = $r->hostname();
  264:             }
  265:             $r->print(
  266:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  267:                                                        $env{'form.symb'},
  268:                                                        $env{'form.idx'},$type,$cdom,
  269:                                                        $cnum,$hostname));
  270:             return OK;
  271:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  272:             my $crstype = &Apache::loncommon::course_type();
  273:             my $title = $env{'form.title'};
  274:             if ($title eq '') {
  275:                 if ($is_ext) {
  276:                     $title = &mt('External Resource');
  277:                 } else {
  278:                     $title = &mt('External Tool');
  279:                 }
  280:             }
  281:             $brcrum =
  282:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  283:         }
  284:     }
  285: 
  286: #
  287: # Actual URL
  288: #
  289:     if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
  290: #
  291: # This is uploaded homework
  292: #
  293:         $env{'request.state'}='uploaded';
  294:         &Apache::lonhomework::renderpage($r,$url);
  295:     } else {
  296: #
  297: # This is not homework
  298: #
  299:         if (($is_ext) || ($exttool)) {
  300:             $absolute = $env{'request.use_absolute'};
  301:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  302:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  303:         }
  304: 
  305:         unless ($ENV{'QUERY_STRING'} eq '') {
  306:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  307:         }
  308: 
  309:         # encrypt url if not external
  310:         unless ($is_ext) {
  311:             &Apache::lonenc::check_encrypt(\$url);
  312:         }
  313: 
  314:         $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
  315:                            $linktext,$explanation,undef,$width,$height) );
  316: 
  317:     } # not just the menu
  318:     
  319:     return OK;
  320: } # handler
  321: 
  322: 1;
  323: __END__
  324: 
  325: =pod
  326: 
  327: =head1 NAME
  328: 
  329: Apache::lonwrapper - External and binary file management.
  330: 
  331: =head1 SYNOPSIS
  332: 
  333: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  334: 
  335: This is part of the LearningOnline Network with CAPA project
  336: described at http://www.lon-capa.org.
  337: 
  338: =head1 Subroutines
  339: 
  340: =over
  341: 
  342: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$title))
  343: 
  344: =over
  345: 
  346: =item $url
  347: 
  348: url to display by including in an iframe within a
  349: LON-CAPA page which has a standard LON-CAPA inline menu.
  350: 
  351: =item $brcrum
  352: 
  353: breadcrumbs for unregistered urls
  354: (i.e., external resources in Supplemental Content).
  355: 
  356: =item $absolute
  357: 
  358: contains protocol (http or https) followed by
  359: the hostname, if menu items in the standard LON-CAPA
  360: interface created by the call to loncommon::start_page()
  361: within &wrapper() need to use absolute URLs rather than
  362: relative URLs.
  363: 
  364: That will be the case where an external resource has been 
  365: served from port 80, when the server customarily serves
  366: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  367: is used to switch requests for external resources and
  368: the syllabus: /public/<domain>/<courseid>/syllabus
  369: (which might also point at an external resource)
  370: from https:// to http:// where the the URL of the remote site 
  371: specified in the resource itself is http://.
  372: 
  373: This is done to avoid default mixed content blocking
  374: in Firefox 23 and later, when serving from Apache/SSL.
  375: 
  376: =item $is_ext
  377: 
  378: true if URL is for an external resource.
  379: 
  380: =item $is_pdf
  381: 
  382: true if URL is for a PDF (based on file extension).
  383: 
  384: =item $title
  385: 
  386: optional. If wrapped item is a PDF, and $env{'browser.mobile'} 
  387: is true, a link to a PDF is shown. The "title" will be displayed
  388: above the link, but if not provided as an arg, $env{'form.title'}
  389: will be used, otherwise, the filename will be displayed (unless
  390: hidden URL set for the resource).
  391: 
  392: =back
  393: 
  394: Returns markup for the entire page.
  395: 
  396: =item handler()
  397: 
  398: =back
  399: 
  400: =cut
  401: 

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