1: # The LearningOnline Network with CAPA
2: # Launch External Tool Provider (LTI)
3: #
4: # $Id: lonexttool.pm,v 1.12 2017/12/22 02:00:46 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: =pod
30:
31: =head1 NAME
32:
33: Apache::lonexttool - Tool Provider launcher
34:
35: =head1 SYNOPSIS
36:
37:
38: =head1 OVERVIEW
39:
40: =cut
41:
42: package Apache::lonexttool;
43:
44: use strict;
45: use Apache::Constants qw(:common :http);
46: use Encode;
47: use Digest::SHA;
48: use HTML::Entities;
49: use Apache::lonlocal;
50: use Apache::lonnet;
51: use Apache::loncommon;
52: use Apache::londatecheck;
53: use Apache::lonipcheck;
54: use Apache::lonhomework;
55: use Apache::structuretags;
56: use LONCAPA::ltiutils;
57:
58: sub handler {
59: my $r=shift;
60: &Apache::loncommon::content_type($r,'text/html');
61: $r->send_http_header;
62:
63: return OK if $r->header_only;
64:
65: my $target=$env{'form.grade_target'};
66: # ------------------------------------------------------------ Print the screen
67: if ($target eq 'tex') {
68: $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
69: } else {
70: $target = 'web';
71: }
72:
73: # Is this even in a course?
74: unless ($env{'request.course.id'}) {
75: if ($target ne 'tex') {
76: &Apache::loncommon::simple_error_page($r,'','Not in a course',
77: {'only_body' => 1});
78: } else {
79: $r->print('\textbf{Not in a course}\end{document}');
80: }
81: return OK;
82: }
83:
84: my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
85: $marker=~s/\D//g;
86:
87: if (!$marker) {
88: if ($target ne 'tex') {
89: $r->print(&mt('Invalid Call'));
90: } else {
91: $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
92: }
93: return OK;
94: }
95:
96: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
97: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
98: my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
99: my ($idx,$is_tool,%toolhash,%toolsettings);
100:
101: if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
102: %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
103: if ($toolsettings{'id'}) {
104: $idx = $toolsettings{'id'};
105: my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
106: if (ref($ltitools{$idx}) eq 'HASH') {
107: %toolhash = %{$ltitools{$idx}};
108: $toolhash{'display'} = {
109: target => $toolsettings{'target'},
110: width => $toolsettings{'width'},
111: height => $toolsettings{'height'},
112: };
113: $toolhash{'crslabel'} = $toolsettings{'crslabel'};
114: $toolhash{'crstitle'} = $toolsettings{'crstitle'};
115: $toolhash{'crsappend'} = $toolsettings{'crsappend'};
116: $toolhash{'gradable'} = $toolsettings{'gradable'};
117: $is_tool = 1;
118: }
119: }
120: }
121: unless ($is_tool) {
122: if ($target ne 'tex') {
123: $r->print('<div>'.&mt('Invalid Call').'</div>');
124: } else {
125: $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
126: }
127: return OK;
128: }
129:
130: my ($status,$open,$close,$msg);
131: if ($toolhash{'gradable'}) {
132: $Apache::lonhomework::browse = &Apache::lonnet::allowed('bre',$r->uri);
133: if ($env{'form.markaccess'}) {
134: my @interval=&Apache::lonnet::EXT("resource.0.interval");
135: my ($timelimit) = split(/_/,$interval[0]);
136: &Apache::lonnet::set_first_access($interval[1],$timelimit);
137: }
138: ($status,$msg) = &gradabletool_access_check();
139: undef($Apache::lonhomework::browse);
140: if ($status eq 'SHOW_ANSWER') {
141: $r->print(&display_score());
142: return OK;
143: } elsif ($status ne 'CAN_ANSWER') {
144: $r->print($msg);
145: return OK;
146: }
147: } else {
148: my ($status,$open,$close,$msg)=&Apache::londatecheck::content_date_check();
149: if ($status ne 'OPEN') {
150: if ($target eq 'tex') {
151: $r->print(&mt('Not open to be viewed'));
152: } else {
153: $r->print($msg);
154: }
155: return OK;
156: } else {
157: ($status,$msg)=&Apache::lonipcheck::ip_access_check();
158: if ($status ne 'OPEN') {
159: if ($target eq 'tex') {
160: $r->print(&mt('Not open to be viewed'));
161: } else {
162: $r->print($msg);
163: }
164: return OK;
165: }
166: }
167: }
168: my $launchok = 1;
169: if ($target eq 'tex') {
170: $r->print(&mt('External Tool'));
171: } else {
172: my $now = time;
173: if ($toolhash{'passback'}) {
174: if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
175: \%toolsettings,\%toolhash) eq 'ok') {
176: $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
177: } else {
178: undef($launchok);
179: }
180: }
181: if ($toolhash{'roster'}) {
182: if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
183: \%toolsettings,\%toolhash) eq 'ok') {
184: $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
185: }
186: }
187: my $submittext = &mt('Launch [_1]',$toolhash{'title'});
188: if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') &&
189: ($toolhash{'url'} ne '') && ($launchok)) {
190: my %lti = <i_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash);
191: my $url = $toolhash{'url'};
192: if ($toolhash{'crsappend'} ne '') {
193: $url .= $toolhash{'crsappend'};
194: }
195: $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'},
196: $submittext,\%lti));
197: } else {
198: $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
199: }
200: }
201: return OK;
202: }
203:
204: sub lti_params {
205: my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
206: my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
207: $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
208: %display,%custom,@userlangs);
209: if (ref($toolsref) eq 'HASH') {
210: $version = $toolsref->{'version'};
211: $toolname = $toolsref->{'title'};
212: $passback = $toolsref->{'passback'};
213: $gradesecret = $toolsref->{'gradesecret'};
214: $roster = $toolsref->{'roster'};
215: $rostersecret = $toolsref->{'rostersecret'};
216: $msgtype = $toolsref->{'messagetype'};
217: if (ref($toolsref->{'fields'}) eq 'HASH') {
218: %fields = %{$toolsref->{'fields'}};
219: }
220: if (ref($toolsref->{'roles'}) eq 'HASH') {
221: %rolesmap = %{$toolsref->{'roles'}};
222: }
223: if (ref($toolsref->{'display'}) eq 'HASH') {
224: %display = %{$toolsref->{'display'}};
225: }
226: if (ref($toolsref->{'custom'}) eq 'HASH') {
227: %custom = %{$toolsref->{'custom'}};
228: }
229: $crslabel = $toolsref->{'crslabel'};
230: $crstitle = $toolsref->{'crstitle'};
231: }
232: if ($version eq '') {
233: $version = 'LTI-1p0';
234: }
235: if ($context_type eq '') {
236: $context_type = 'CourseSection';
237: }
238: if ($msgtype eq '') {
239: $msgtype = 'basic-lti-launch-request';
240: }
241: if ($crslabel eq '') {
242: $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
243: }
244: if ($crstitle eq '') {
245: $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'},;
246: }
247: my $lonhost = $r->dir_config('lonHostID');
248: my $loncaparev = $r->dir_config('lonVersion');
249: my $uname = $env{'user.name'};
250: my $udom = $env{'user.domain'};
251: my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
252: my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
253: my $ltirole = $rolesmap{$roleprefix};
254: unless (grep(/^\Q$ltirole\E$/,@possroles)) {
255: $ltirole = 'Learner';
256: }
257: my $digest_user = &Encode::decode_utf8($uname.':'.$udom);
258: $digest_user = &Digest::SHA::sha1_hex($digest_user);
259: if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') {
260: @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
261: $env{'course.'.$env{'request.course.id'}.'.languages'}));
262: } else {
263: my %langhash = &Apache::loncommon::getlangs($uname,$udom);
264: if ($langhash{'languages'} ne '') {
265: @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
266: } else {
267: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
268: if ($domdefs{'lang_def'} ne '') {
269: @userlangs = ($domdefs{'lang_def'});
270: }
271: }
272: }
273: if (scalar(@userlangs) == 1) {
274: $locale = $userlangs[0];
275: }
276: my ($title,$digest_symb);
277: my ($symb) = &Apache::lonnet::whichuser();
278: if ($symb) {
279: $digest_symb = &Encode::decode_utf8($symb);
280: $digest_symb = &Digest::SHA::sha1_hex($digest_symb);
281: my $navmap = Apache::lonnavmaps::navmap->new();
282: if (ref($navmap)) {
283: my $res = $navmap->getBySymb($symb);
284: if (ref($res)) {
285: $title = $res->compTitle();
286: }
287: }
288: }
289: my $domdesc = &Apache::lonnet::domain($cdom);
290: my $primary_id = &Apache::lonnet::domain($cdom,'primary');
291: my $int_dom = &Apache::lonnet::internet_dom($primary_id);
292: my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom);
293:
294: my %ltiparams = (
295: lti_version => $version,
296: lti_message_type => $msgtype,
297: resource_link_title => $title,
298: resource_link_id => $digest_symb,
299: tool_consumer_instance_guid => $lonhost,
300: tool_consumer_instance_description => $domdesc,
301: tool_consumer_info_product_family_code => 'loncapa',
302: tool_consumer_instance_name => $int_dom,
303: tool_consumer_instance_url => $portal_url,
304: tool_consumer_info_version => $loncaparev,
305: user_id => $digest_user,
306: roles => $ltirole,
307: context_id => $env{'request.course.id'},
308: context_type => $context_type,
309: context_label => $crslabel,
310: context_title => $crstitle,
311: launch_presentation_locale => $locale,
312: );
313: my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
314: my $crshostname = &Apache::lonnet::hostname($crshome);
315: if ($crshostname) {
316: my $crsprotocol = $Apache::lonnet::protocol{$crshome};
317: unless ($crsprotocol eq 'https') {
318: $crsprotocol = 'http';
319: }
320: if (($passback) || ($roster)) {
321: if ($passback) {
322: $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
323: $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
324: if ($gradesecret) {
325: my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
326: $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid);
327: }
328: }
329: if ($roster) {
330: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
331: $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
332: if ($rostersecret) {
333: my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
334: $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
335: }
336: }
337: }
338: my %digesthash;
339: if ($ltiparams{'lis_result_sourcedid'}) {
340: $digesthash{$ltiparams{'lis_result_sourcedid'}} = "$idx\0".time;
341: }
342: if ($ltiparams{'ext_ims_lis_memberships_id'}) {
343: $digesthash{$ltiparams{'ext_ims_lis_memberships_id'}} = "$idx\0".time;
344: }
345: if (($digest_symb) && ($gradesecret || $rostersecret)) {
346: $digesthash{$digest_symb} = $symb;
347: }
348: if (($passback) && ($gradesecret)) {
349: $digesthash{$digest_user} = $uname.':'.$udom;
350: }
351: if (keys(%digesthash)) {
352: &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
353: }
354: }
355: }
356: if ($display{'target'}) {
357: $ltiparams{'launch_presentation_document_target'} = $display{'target'};
358: }
359: if ($display{'width'}) {
360: $ltiparams{'launch_presentation_width'} = $display{'width'};
361: }
362: if ($display{'height'}) {
363: $ltiparams{'launch_presentation_height'} = $display{'height'};
364: }
365: if ($fields{'firstname'}) {
366: $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'};
367: }
368: if ($fields{'lastname'}) {
369: $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'};
370: }
371: if ($fields{'fullname'}) {
372: $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom);
373: }
374: if ($fields{'email'}) {
375: my %emails = &Apache::loncommon::getemails($uname,$udom);
376: my $contact_email;
377: foreach my $type ('permanentemail','critnotification','notification') {
378: if ($emails{$type} =~ /\@/) {
379: $contact_email = $emails{$type};
380: last;
381: }
382: }
383: $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
384: }
385: if ($fields{'user'}) {
386: $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
387: }
388: if (keys(%custom)) {
389: foreach my $key (keys(%custom)) {
390: my $value = $custom{$key};
391: $value =~ s/^\s+|\s+\$//g;
392: if ($value =~ /^\QLONCAPA::env{\E([^\}]+)\}$/) {
393: if (exists($env{$1})) {
394: $value = $env{$1};
395: }
396: }
397: $ltiparams{'custom_'.$key} = $value;
398: }
399: }
400: foreach my $key (keys(%ltiparams)) {
401: $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
402: }
403: $ltiparams{'basiclti_submit'} = $submittext;
404: return %ltiparams;
405: }
406:
407: sub launch_html {
408: my ($url,$key,$secret,$submittext,$paramsref) = @_;
409: my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref);
410: my $action = &HTML::Entities::encode($url,'<>&"');
411: my $form = <<"END";
412: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
413: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
414: <body>
415: <div id="LCltiLaunch">
416: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
417: END
418: if (ref($hashref) eq 'HASH') {
419: foreach my $item (keys(%{$hashref})) {
420: my $type = 'hidden';
421: if ($item eq 'basiclti_submit') {
422: $type = 'submit';
423: }
424: $form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n";
425: }
426: }
427: $form .= "</form></div>\n";
428: $form .= <<"ENDJS";
429: <script type="text/javascript">
430: document.getElementById("LCltiLaunch").style.display = "none";
431: nei = document.createElement('input');
432: nei.setAttribute('type','hidden');
433: nei.setAttribute('name','basiclti_submit');
434: nei.setAttribute('value','$submittext');
435: document.getElementById("LCltiLaunchFormId").appendChild(nei);
436: document.LCltiLaunchForm.submit();
437: </script>
438: ENDJS
439: $form .= "</body></html>\n";
440: return $form;
441: }
442:
443: sub gradabletool_access_check {
444: my ($result,$resource_due);
445: my $status;
446: my ($symb,$courseid,$udom,$uname) = &Apache::lonnet::whichuser();
447: my @targets;
448: if (defined($env{'form.submitted'}) && defined($env{'form.validate'})) {
449: @targets = ('grade','web');
450: } else {
451: @targets = ('web');
452: }
453: foreach my $target (@targets) {
454: undef(%Apache::lonhomework::results);
455: undef(%Apache::lonhomework::history);
456: %Apache::lonhomework::history =
457: &Apache::lonnet::restore($symb,$courseid,$udom,$uname);
458: &Apache::lonhomework::set_show_problem_status(&Apache::lonnet::EXT('resource.0.problemstatus'));
459: my ($accessmsg,$slot_name,$slot,$ipused);
460: ($status,$accessmsg,$slot_name,$slot,$ipused) =
461: &Apache::lonhomework::check_slot_access('0','tool',$symb);
462: if (( $status eq 'CLOSED' ) ||
463: ( $status eq 'UNCHECKEDOUT') ||
464: ( $status eq 'NOT_YET_VIEWED') ||
465: ( $status eq 'BANNED') ||
466: ( $status eq 'UNAVAILABLE') ||
467: ( $status eq 'NOT_IN_A_SLOT') ||
468: ( $status eq 'NOTRESERVABLE') ||
469: ( $status eq 'RESERVABLE') ||
470: ( $status eq 'RESERVABLE_LATER') ||
471: ( $status eq 'INVALID_ACCESS') ||
472: ( $status eq 'NEED_DIFFERENT_IP') ||
473: ( $status eq 'WAITING_FOR_GRADE')) {
474: $result = &Apache::structuretags::access_status_msg('tool',$status,$symb,
475: $target,$ipused,$accessmsg);
476: } elsif ($status eq 'NEEDS_CHECKIN') {
477: $result = &Apache::structuretags::checkin_prompt($target,$slot_name,$slot,'tool');
478: } elsif ($target eq 'web') {
479: if ($status eq 'CAN_ANSWER') {
480: $resource_due = &Apache::lonhomework::due_date(0, $env{'request.symb'});
481: if ($slot_name ne '') {
482: $resource_due = &Apache::structuretags::selfcheckin_resource($resource_due,
483: $slot_name,$slot,
484: $env{'request.symb'});
485: }
486: }
487: }
488: }
489: return ($status,$result,$resource_due);
490: }
491:
492: sub display_score {
493: my $weight = &Apache::lonnet::EXT('resource.0.weight');
494: if ((!defined($weight)) || ($weight eq '')) { $weight=1; }
495: my $awarded = $Apache::lonhomework::history{'resource.0.awarded'};
496: if (!defined($awarded)) { $awarded=0; }
497: my $display='';
498: if (!defined($awarded)) {
499: $display = &mt('[_1] possible points.',$weight);
500: } else {
501: my $points = $awarded*$weight;
502: my $result = sprintf('%.2f',$points);
503: $display = &mt('You have [_1] out of [quant,_2,possible point]',
504: $result,$weight);
505: }
506: my $comment = $Apache::lonhomework::history{'resource.0.comment'};
507: if (!defined($comment) || $comment!~/\w/) {
508: $comment='';
509: } else {
510: $comment='<br /><table><tr><td bgcolor="#FFFFDD">'.$comment.'</td></tr></table>';
511: }
512: my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'};
513: if (!defined($gradeinfo) || $gradeinfo!~/\w/) {
514: $gradeinfo='';
515: } else {
516: $gradeinfo='<br /><table><tr><td bgcolor="#DDDDFF"><font size="+2">'.$gradeinfo.'</font></td></tr></table>';
517: }
518: return $display.$comment.$gradeinfo;
519: }
520:
521: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>