File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.66: download - view: text, annotated - select for diffs
Mon Dec 18 23:23:14 2017 UTC (6 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Inline menu for page wrapper for LTI provider launcher can include
  countdown timer, if instance of external tool is set to be "gradable".

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

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