File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.68: download - view: text, annotated - select for diffs
Sat Dec 30 00:16:36 2017 UTC (6 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Done button can be used with a gradable external tool to terminate
  access.

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

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