Annotation of rat/lonwrapper.pm, revision 1.68
1.1 www 1: # The LearningOnline Network with CAPA
2: # Wrapper for external and binary files as standalone resources
3: #
1.68 ! raeburn 4: # $Id: lonwrapper.pm,v 1.67 2017/12/22 02:01:01 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.68 ! raeburn 41: use Apache::lonhomework();
1.64 raeburn 42: use LONCAPA qw(:DEFAULT :match);
43: use HTML::Entities();
1.1 www 44:
45: # ================================================================ Main Handler
1.21 albertel 46: sub wrapper {
1.63 raeburn 47: my ($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,
48: $title,$width,$height) = @_;
1.23 albertel 49:
1.42 raeburn 50: my $forcereg;
51: unless ($env{'form.folderpath'}) {
52: $forcereg = 1;
53: }
1.46 raeburn 54: my %lt = &Apache::lonlocal::texthash(
55: 'noif' => 'No iframe support.',
56: 'show' => 'Show content in pop-up window',
57: );
58:
1.51 raeburn 59: my $anchor;
1.53 raeburn 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'};
1.51 raeburn 68: }
69: }
70:
71: my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
1.42 raeburn 72: my $args = {'bgcolor' => '#FFFFFF'};
73: if ($forcereg) {
74: $args->{'force_register'} = $forcereg;
75: }
76: if (ref($brcrum) eq 'ARRAY') {
1.45 raeburn 77: $args->{'bread_crumbs'} = $brcrum;
1.42 raeburn 78: }
1.44 raeburn 79: if ($absolute) {
1.57 raeburn 80: $args->{'use_absolute'} = $absolute;
1.44 raeburn 81: }
1.56 raeburn 82: if ($env{'form.only_body'}) {
83: $args->{'only_body'} = $env{'form.only_body'};
84: }
1.42 raeburn 85:
1.68 ! raeburn 86: my ($countdown,$donemsg);
1.67 raeburn 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'}) {
1.68 ! raeburn 90: my $symb=&Apache::lonnet::symbread($url);
! 91: my @interval=&Apache::lonnet::EXT('resource.0.interval',$symb);
1.67 raeburn 92: my ($timelimit) = split(/_/,$interval[0]);
1.68 ! raeburn 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: }
1.67 raeburn 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 ='
1.66 raeburn 113: <script type="text/javascript">
114: // <![CDATA['."\n".
115: &Apache::lonhtmlcommon::countdown().'
116: // ]]>
117: </script>'."\n".
1.67 raeburn 118: &Apache::lonhtmlcommon::set_due_date($resource_due);
119: }
1.66 raeburn 120: }
1.67 raeburn 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();
1.66 raeburn 129: }
130: }
131:
1.68 ! raeburn 132: my $startpage = &Apache::loncommon::start_page('Menu',undef,$args).$countdown.$donemsg;
1.67 raeburn 133: my $endpage = &Apache::loncommon::end_page();
134:
1.63 raeburn 135: if (($env{'browser.mobile'}) || ($exttool eq 'window') || ($exttool eq 'tab')) {
1.47 raeburn 136: my $output = $startpage;
137: if ($is_pdf) {
138: if ($title eq '') {
1.49 raeburn 139: $title = $env{'form.title'};
140: if ($title eq '') {
141: unless ($env{'request.enc'}) {
142: ($title) = ($url =~ m{/([^/]+)$});
143: $title =~ s/(\?[^\?]+)$//;
144: }
1.47 raeburn 145: }
146: }
147: unless ($title eq '') {
148: $output .= $title.'<br />';
149: }
150: $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
1.63 raeburn 151: } elsif (($exttool eq 'window') || ($exttool eq 'tab')) {
1.60 raeburn 152: if ($linktext eq '') {
153: $linktext = &mt('Launch External Tool');
154: }
1.64 raeburn 155: $url = &HTML::Entities::encode($url,'"<>&');
1.63 raeburn 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">
1.64 raeburn 164: // <![CDATA[
1.63 raeburn 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: }
1.64 raeburn 181: // ]]>
1.63 raeburn 182: </script>
183: <div>
184: <a href="$url" target="LCExternalToolPopUp" onclick="openSinglePopup(this.href); return false;">
185: $linktext</a>
186: </div>
187: ENDLINK
188: }
1.60 raeburn 189: if ($explanation ne '') {
190: $output .= '<div>'.$explanation.'</div>';
191: }
1.66 raeburn 192: if (&Apache::lonnet::EXT('resource.0.gradable')) {
193: $output .= &Apache::lonfeedback::list_discussion('tool','OPEN');
194: }
1.47 raeburn 195: } else {
1.64 raeburn 196: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
1.47 raeburn 197: $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
1.64 raeburn 198: '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
1.47 raeburn 199: "$lt{'noif'} $noiframe\n".
200: "</iframe>\n".
201: "</div>\n";
202: }
203: $output .= $endpage;
204: return $output;
1.46 raeburn 205: } else {
1.55 raeburn 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: }
1.46 raeburn 211: my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
212: \$(document).ready( function() {
213: \$(window).unbind('resize').resize(function(){
1.54 damieng 214: var header = null;
1.55 raeburn 215: var offset = $offset;
1.46 raeburn 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: }
1.54 damieng 226: if (header != null && header.length) {
1.46 raeburn 227: height = header.height();
228: hdrtop = header.position().top;
1.42 raeburn 229: }
1.46 raeburn 230: var pos = height + hdrtop + offset;
231: \$('.LC_iframecontainer').css('top', pos);
232: });
1.38 droeschl 233: });
1.46 raeburn 234: window.onload = function(){ \$(window).trigger('resize') };
1.39 droeschl 235: SCRIPT
1.46 raeburn 236: # javascript will position the iframe if window was resized (or zoomed)
1.64 raeburn 237: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
1.46 raeburn 238: return <<ENDFRAME;
239: $startpage
240: $script
241: <div class="LC_iframecontainer">
1.64 raeburn 242: <iframe src="$dest">$lt{'noif'} $noiframe</iframe>
1.46 raeburn 243: </div>
244: $endpage
1.38 droeschl 245: ENDFRAME
1.46 raeburn 246: }
1.21 albertel 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:
1.38 droeschl 256: my $url = $r->uri;
1.60 raeburn 257: my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
1.63 raeburn 258: $linktext,$explanation,$width,$height);
1.38 droeschl 259:
260: for ($url){
261: s|^/adm/wrapper||;
262: $is_ext = $_ =~ s|^/ext/|http://|;
263: s|http://https://|https://|;
264: s|:|:|g;
1.21 albertel 265: }
266:
1.50 raeburn 267:
1.47 raeburn 268: if ($url =~ /\.pdf$/i) {
269: $is_pdf = 1;
1.62 raeburn 270: } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
1.50 raeburn 271: $cdom = $1;
272: $cnum = $2;
273: my $marker = $3;
274: $exttool = 'iframe';
1.61 raeburn 275: my $exttoolremote;
1.63 raeburn 276: my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
1.60 raeburn 277: $cdom,$cnum);
1.61 raeburn 278: if ($toolhash{'id'}) {
1.65 raeburn 279: my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
1.61 raeburn 280: if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
281: $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
282: }
283: }
1.50 raeburn 284: if ($toolhash{'target'} eq 'window') {
1.63 raeburn 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')) {
1.60 raeburn 292: $linktext = $toolhash{'linktext'};
293: $explanation = $toolhash{'explanation'};
1.61 raeburn 294: } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
1.63 raeburn 295: $exttool = 'tab';
1.50 raeburn 296: }
1.47 raeburn 297: }
1.50 raeburn 298: if (($is_ext) || ($exttool)) {
1.42 raeburn 299: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.53 raeburn 300: ['forceedit','register','folderpath','symb','idx','title','anchor']);
1.42 raeburn 301: if (($env{'form.forceedit'}) &&
302: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
303: (($env{'form.folderpath'} =~ /^supplemental/) ||
304: ($env{'form.symb'} =~ /^uploaded/))) {
1.53 raeburn 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: }
1.52 raeburn 314: }
1.50 raeburn 315: my $type = 'ext';
316: if ($exttool) {
317: $type = 'tool';
1.58 raeburn 318: } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
319: $hostname = $r->hostname();
1.50 raeburn 320: }
1.42 raeburn 321: $r->print(
322: &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
323: $env{'form.symb'},
1.50 raeburn 324: $env{'form.idx'},$type,$cdom,
1.58 raeburn 325: $cnum,$hostname));
1.42 raeburn 326: return OK;
327: } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
328: my $crstype = &Apache::loncommon::course_type();
1.43 raeburn 329: my $title = $env{'form.title'};
330: if ($title eq '') {
1.50 raeburn 331: if ($is_ext) {
332: $title = &mt('External Resource');
333: } else {
334: $title = &mt('External Tool');
335: }
1.43 raeburn 336: }
1.42 raeburn 337: $brcrum =
1.43 raeburn 338: &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
1.42 raeburn 339: }
340: }
341:
1.21 albertel 342: #
343: # Actual URL
344: #
1.62 raeburn 345: if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
1.21 albertel 346: #
347: # This is uploaded homework
348: #
1.38 droeschl 349: $env{'request.state'}='uploaded';
350: &Apache::lonhomework::renderpage($r,$url);
1.21 albertel 351: } else {
352: #
353: # This is not homework
354: #
1.50 raeburn 355: if (($is_ext) || ($exttool)) {
1.49 raeburn 356: $absolute = $env{'request.use_absolute'};
1.38 droeschl 357: $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
1.42 raeburn 358: $ENV{'QUERY_STRING'} =~ s/\&$//;
1.38 droeschl 359: }
360:
1.35 raeburn 361: unless ($ENV{'QUERY_STRING'} eq '') {
1.38 droeschl 362: $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
1.35 raeburn 363: }
1.38 droeschl 364:
365: # encrypt url if not external
1.59 raeburn 366: unless ($is_ext) {
1.50 raeburn 367: &Apache::lonenc::check_encrypt(\$url);
368: }
1.38 droeschl 369:
1.63 raeburn 370: $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
371: $linktext,$explanation,undef,$width,$height) );
1.38 droeschl 372:
1.21 albertel 373: } # not just the menu
1.38 droeschl 374:
1.21 albertel 375: return OK;
1.13 www 376: } # handler
1.1 www 377:
378: 1;
379: __END__
380:
1.30 jms 381: =pod
1.1 www 382:
1.30 jms 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:
1.64 raeburn 398: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$linktext,$explanation,$title,$width,$height)
1.30 jms 399:
1.45 raeburn 400: =over
401:
1.46 raeburn 402: =item $url
403:
1.45 raeburn 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
1.42 raeburn 410: (i.e., external resources in Supplemental Content).
1.45 raeburn 411:
1.46 raeburn 412: =item $absolute
1.45 raeburn 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
1.49 raeburn 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)
1.45 raeburn 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:
1.47 raeburn 436: =item $is_pdf
437:
438: true if URL is for a PDF (based on file extension).
439:
1.49 raeburn 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:
1.45 raeburn 448: =back
449:
1.38 droeschl 450: Returns markup for the entire page.
1.30 jms 451:
452: =item handler()
453:
454: =back
455:
456: =cut
1.1 www 457:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>