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

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