Annotation of rat/lonwrapper.pm, revision 1.67
1.1 www 1: # The LearningOnline Network with CAPA
2: # Wrapper for external and binary files as standalone resources
3: #
1.67 ! raeburn 4: # $Id: lonwrapper.pm,v 1.66 2017/12/18 23:23:14 raeburn Exp $
1.4 www 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: #
1.1 www 28:
1.29 jms 29:
1.1 www 30: package Apache::lonwrapper;
31:
32: use strict;
33: use Apache::Constants qw(:common);
1.38 droeschl 34: use Apache::lonenc();
1.18 albertel 35: use Apache::lonnet;
1.42 raeburn 36: use Apache::lonlocal;
37: use Apache::loncommon();
38: use Apache::lonhtmlcommon();
39: use Apache::lonextresedit();
1.50 raeburn 40: use Apache::lonexttool();
1.64 raeburn 41: use LONCAPA qw(:DEFAULT :match);
42: use HTML::Entities();
1.1 www 43:
44: # ================================================================ Main Handler
1.21 albertel 45: sub wrapper {
1.63 raeburn 46: my ($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,
47: $title,$width,$height) = @_;
1.23 albertel 48:
1.42 raeburn 49: my $forcereg;
50: unless ($env{'form.folderpath'}) {
51: $forcereg = 1;
52: }
1.46 raeburn 53: my %lt = &Apache::lonlocal::texthash(
54: 'noif' => 'No iframe support.',
55: 'show' => 'Show content in pop-up window',
56: );
57:
1.51 raeburn 58: my $anchor;
1.53 raeburn 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'};
1.51 raeburn 67: }
68: }
69:
70: my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
1.42 raeburn 71: my $args = {'bgcolor' => '#FFFFFF'};
72: if ($forcereg) {
73: $args->{'force_register'} = $forcereg;
74: }
75: if (ref($brcrum) eq 'ARRAY') {
1.45 raeburn 76: $args->{'bread_crumbs'} = $brcrum;
1.42 raeburn 77: }
1.44 raeburn 78: if ($absolute) {
1.57 raeburn 79: $args->{'use_absolute'} = $absolute;
1.44 raeburn 80: }
1.56 raeburn 81: if ($env{'form.only_body'}) {
82: $args->{'only_body'} = $env{'form.only_body'};
83: }
1.42 raeburn 84:
1.67 ! raeburn 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 ='
1.66 raeburn 102: <script type="text/javascript">
103: // <![CDATA['."\n".
104: &Apache::lonhtmlcommon::countdown().'
105: // ]]>
106: </script>'."\n".
1.67 ! raeburn 107: &Apache::lonhtmlcommon::set_due_date($resource_due);
! 108: }
1.66 raeburn 109: }
1.67 ! raeburn 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();
1.66 raeburn 118: }
119: }
120:
1.67 ! raeburn 121: my $startpage = &Apache::loncommon::start_page('Menu',undef,$args).$countdown;
! 122: my $endpage = &Apache::loncommon::end_page();
! 123:
1.63 raeburn 124: if (($env{'browser.mobile'}) || ($exttool eq 'window') || ($exttool eq 'tab')) {
1.47 raeburn 125: my $output = $startpage;
126: if ($is_pdf) {
127: if ($title eq '') {
1.49 raeburn 128: $title = $env{'form.title'};
129: if ($title eq '') {
130: unless ($env{'request.enc'}) {
131: ($title) = ($url =~ m{/([^/]+)$});
132: $title =~ s/(\?[^\?]+)$//;
133: }
1.47 raeburn 134: }
135: }
136: unless ($title eq '') {
137: $output .= $title.'<br />';
138: }
139: $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
1.63 raeburn 140: } elsif (($exttool eq 'window') || ($exttool eq 'tab')) {
1.60 raeburn 141: if ($linktext eq '') {
142: $linktext = &mt('Launch External Tool');
143: }
1.64 raeburn 144: $url = &HTML::Entities::encode($url,'"<>&');
1.63 raeburn 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">
1.64 raeburn 153: // <![CDATA[
1.63 raeburn 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: }
1.64 raeburn 170: // ]]>
1.63 raeburn 171: </script>
172: <div>
173: <a href="$url" target="LCExternalToolPopUp" onclick="openSinglePopup(this.href); return false;">
174: $linktext</a>
175: </div>
176: ENDLINK
177: }
1.60 raeburn 178: if ($explanation ne '') {
179: $output .= '<div>'.$explanation.'</div>';
180: }
1.66 raeburn 181: if (&Apache::lonnet::EXT('resource.0.gradable')) {
182: $output .= &Apache::lonfeedback::list_discussion('tool','OPEN');
183: }
1.47 raeburn 184: } else {
1.64 raeburn 185: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
1.47 raeburn 186: $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
1.64 raeburn 187: '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
1.47 raeburn 188: "$lt{'noif'} $noiframe\n".
189: "</iframe>\n".
190: "</div>\n";
191: }
192: $output .= $endpage;
193: return $output;
1.46 raeburn 194: } else {
1.55 raeburn 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: }
1.46 raeburn 200: my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
201: \$(document).ready( function() {
202: \$(window).unbind('resize').resize(function(){
1.54 damieng 203: var header = null;
1.55 raeburn 204: var offset = $offset;
1.46 raeburn 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: }
1.54 damieng 215: if (header != null && header.length) {
1.46 raeburn 216: height = header.height();
217: hdrtop = header.position().top;
1.42 raeburn 218: }
1.46 raeburn 219: var pos = height + hdrtop + offset;
220: \$('.LC_iframecontainer').css('top', pos);
221: });
1.38 droeschl 222: });
1.46 raeburn 223: window.onload = function(){ \$(window).trigger('resize') };
1.39 droeschl 224: SCRIPT
1.46 raeburn 225: # javascript will position the iframe if window was resized (or zoomed)
1.64 raeburn 226: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
1.46 raeburn 227: return <<ENDFRAME;
228: $startpage
229: $script
230: <div class="LC_iframecontainer">
1.64 raeburn 231: <iframe src="$dest">$lt{'noif'} $noiframe</iframe>
1.46 raeburn 232: </div>
233: $endpage
1.38 droeschl 234: ENDFRAME
1.46 raeburn 235: }
1.21 albertel 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:
1.38 droeschl 245: my $url = $r->uri;
1.60 raeburn 246: my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
1.63 raeburn 247: $linktext,$explanation,$width,$height);
1.38 droeschl 248:
249: for ($url){
250: s|^/adm/wrapper||;
251: $is_ext = $_ =~ s|^/ext/|http://|;
252: s|http://https://|https://|;
253: s|:|:|g;
1.21 albertel 254: }
255:
1.50 raeburn 256:
1.47 raeburn 257: if ($url =~ /\.pdf$/i) {
258: $is_pdf = 1;
1.62 raeburn 259: } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
1.50 raeburn 260: $cdom = $1;
261: $cnum = $2;
262: my $marker = $3;
263: $exttool = 'iframe';
1.61 raeburn 264: my $exttoolremote;
1.63 raeburn 265: my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
1.60 raeburn 266: $cdom,$cnum);
1.61 raeburn 267: if ($toolhash{'id'}) {
1.65 raeburn 268: my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
1.61 raeburn 269: if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
270: $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
271: }
272: }
1.50 raeburn 273: if ($toolhash{'target'} eq 'window') {
1.63 raeburn 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')) {
1.60 raeburn 281: $linktext = $toolhash{'linktext'};
282: $explanation = $toolhash{'explanation'};
1.61 raeburn 283: } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
1.63 raeburn 284: $exttool = 'tab';
1.50 raeburn 285: }
1.47 raeburn 286: }
1.50 raeburn 287: if (($is_ext) || ($exttool)) {
1.42 raeburn 288: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.53 raeburn 289: ['forceedit','register','folderpath','symb','idx','title','anchor']);
1.42 raeburn 290: if (($env{'form.forceedit'}) &&
291: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
292: (($env{'form.folderpath'} =~ /^supplemental/) ||
293: ($env{'form.symb'} =~ /^uploaded/))) {
1.53 raeburn 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: }
1.52 raeburn 303: }
1.50 raeburn 304: my $type = 'ext';
305: if ($exttool) {
306: $type = 'tool';
1.58 raeburn 307: } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
308: $hostname = $r->hostname();
1.50 raeburn 309: }
1.42 raeburn 310: $r->print(
311: &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
312: $env{'form.symb'},
1.50 raeburn 313: $env{'form.idx'},$type,$cdom,
1.58 raeburn 314: $cnum,$hostname));
1.42 raeburn 315: return OK;
316: } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
317: my $crstype = &Apache::loncommon::course_type();
1.43 raeburn 318: my $title = $env{'form.title'};
319: if ($title eq '') {
1.50 raeburn 320: if ($is_ext) {
321: $title = &mt('External Resource');
322: } else {
323: $title = &mt('External Tool');
324: }
1.43 raeburn 325: }
1.42 raeburn 326: $brcrum =
1.43 raeburn 327: &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
1.42 raeburn 328: }
329: }
330:
1.21 albertel 331: #
332: # Actual URL
333: #
1.62 raeburn 334: if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
1.21 albertel 335: #
336: # This is uploaded homework
337: #
1.38 droeschl 338: $env{'request.state'}='uploaded';
339: &Apache::lonhomework::renderpage($r,$url);
1.21 albertel 340: } else {
341: #
342: # This is not homework
343: #
1.50 raeburn 344: if (($is_ext) || ($exttool)) {
1.49 raeburn 345: $absolute = $env{'request.use_absolute'};
1.38 droeschl 346: $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
1.42 raeburn 347: $ENV{'QUERY_STRING'} =~ s/\&$//;
1.38 droeschl 348: }
349:
1.35 raeburn 350: unless ($ENV{'QUERY_STRING'} eq '') {
1.38 droeschl 351: $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
1.35 raeburn 352: }
1.38 droeschl 353:
354: # encrypt url if not external
1.59 raeburn 355: unless ($is_ext) {
1.50 raeburn 356: &Apache::lonenc::check_encrypt(\$url);
357: }
1.38 droeschl 358:
1.63 raeburn 359: $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
360: $linktext,$explanation,undef,$width,$height) );
1.38 droeschl 361:
1.21 albertel 362: } # not just the menu
1.38 droeschl 363:
1.21 albertel 364: return OK;
1.13 www 365: } # handler
1.1 www 366:
367: 1;
368: __END__
369:
1.30 jms 370: =pod
1.1 www 371:
1.30 jms 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:
1.64 raeburn 387: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$linktext,$explanation,$title,$width,$height)
1.30 jms 388:
1.45 raeburn 389: =over
390:
1.46 raeburn 391: =item $url
392:
1.45 raeburn 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
1.42 raeburn 399: (i.e., external resources in Supplemental Content).
1.45 raeburn 400:
1.46 raeburn 401: =item $absolute
1.45 raeburn 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
1.49 raeburn 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)
1.45 raeburn 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:
1.47 raeburn 425: =item $is_pdf
426:
427: true if URL is for a PDF (based on file extension).
428:
1.49 raeburn 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:
1.45 raeburn 437: =back
438:
1.38 droeschl 439: Returns markup for the entire page.
1.30 jms 440:
441: =item handler()
442:
443: =back
444:
445: =cut
1.1 www 446:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>