# The LearningOnline Network with CAPA # Launch External Tool Provider (LTI) # # $Id: lonexttool.pm,v 1.22.2.1 2021/12/31 15:12:52 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # =pod =head1 NAME Apache::lonexttool - Tool Provider launcher =head1 SYNOPSIS =head1 OVERVIEW =cut package Apache::lonexttool; use strict; use Apache::Constants qw(:common :http); use Encode; use Digest::SHA; use HTML::Entities; use Apache::lonlocal; use Apache::lonnet; use Apache::loncommon; use Apache::londatecheck; use Apache::lonipcheck; use LONCAPA::ltiutils; sub handler { my $r=shift; &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK if $r->header_only; my $target=$env{'form.grade_target'}; # ------------------------------------------------------------ Print the screen if ($target eq 'tex') { $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'})); } else { $target = 'web'; } # Is this even in a course? unless ($env{'request.course.id'}) { if ($target ne 'tex') { &Apache::loncommon::simple_error_page($r,'','Not in a course', {'only_body' => 1}); } else { $r->print('\textbf{Not in a course}\end{document}'); } return OK; } my ($marker,$exttool) = (split(m{/},$r->uri))[4,5]; $marker=~s/\D//g; if (!$marker) { if ($target ne 'tex') { $r->print(&mt('Invalid Call')); } else { $r->print('\textbf{'&mt('Invalid Call').'}\end{document}'); } return OK; } my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; my $chome = $env{'course.'.$env{'request.course.id'}.'.home'}; my ($idx,$is_tool,%toolhash,%toolsettings); if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") { %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); if ($toolsettings{'id'}) { $idx = $toolsettings{'id'}; my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer'); if (ref($ltitools{$idx}) eq 'HASH') { %toolhash = %{$ltitools{$idx}}; $toolhash{'display'} = { target => $toolsettings{'target'}, width => $toolsettings{'width'}, height => $toolsettings{'height'}, }; foreach my $item (qw(crslabel crstitle crsappend)) { $toolhash{$item} = $toolsettings{$item}; } $is_tool = 1; } } } unless ($is_tool) { if ($target ne 'tex') { $r->print('
'.&mt('Invalid Call').'
'); } else { $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}'); } return OK; } my ($symb,$status,$open,$close,$msg,$donebuttonresult,$donemsg); if (($target eq 'tex') || ($toolhash{'gradable'})) { ($symb) = &Apache::lonnet::whichuser(); } if ($target eq 'tex') { my $title = &Apache::lonnet::gettitle($symb); $r->print(&mt('External Tool: [_1]','\textit{'.$title.'}').'\\\\'); } my ($status,$open,$close,$msg)=&Apache::londatecheck::content_date_check(); if ($status ne 'OPEN') { if ($target eq 'tex') { $r->print(&mt('Not open to be viewed').'\end{document}'); } else { $r->print($msg); } return OK; } else { ($status,$msg)=&Apache::lonipcheck::ip_access_check(); if ($status ne 'OPEN') { if ($target eq 'tex') { $r->print(&mt('Not open to be viewed').'\end{document}'); } else { $r->print($msg); } return OK; } } my $launchok = 1; if ($target eq 'tex') { $r->print('\end{document}'); } else { my $now = time; my $submittext = &mt('Launch [_1]',$toolhash{'title'}); if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '') && ($launchok)) { my %lti = <i_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash); my $url = $toolhash{'url'}; if ($toolhash{'crsappend'} ne '') { $url .= $toolhash{'crsappend'}; } $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'}, $toolhash{'sigmethod'},$submittext,\%lti)); } else { $r->print('
'.&mt('External Tool Unavailable').'
'); } } return OK; } sub lti_params { my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_; my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale, $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs,$incdom); if (ref($toolsref) eq 'HASH') { $version = $toolsref->{'version'}; $toolname = $toolsref->{'title'}; $msgtype = $toolsref->{'messagetype'}; $incdom = $toolsref->{'incdom'}; if (ref($toolsref->{'fields'}) eq 'HASH') { %fields = %{$toolsref->{'fields'}}; } if (ref($toolsref->{'roles'}) eq 'HASH') { %rolesmap = %{$toolsref->{'roles'}}; } if (ref($toolsref->{'display'}) eq 'HASH') { %display = %{$toolsref->{'display'}}; } if (ref($toolsref->{'custom'}) eq 'HASH') { %custom = %{$toolsref->{'custom'}}; } $crslabel = $toolsref->{'crslabel'}; $crstitle = $toolsref->{'crstitle'}; } if ($version eq '') { $version = 'LTI-1p0'; } if ($context_type eq '') { $context_type = 'CourseSection'; } if ($msgtype eq '') { $msgtype = 'basic-lti-launch-request'; } if ($crslabel eq '') { $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}; } if ($crstitle eq '') { $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'}; } my $lonhost = $r->dir_config('lonHostID'); my $loncaparev = $r->dir_config('lonVersion'); my $uname = $env{'user.name'}; my $udom = $env{'user.domain'}; my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner); my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./); my $ltirole = $rolesmap{$roleprefix}; unless (grep(/^\Q$ltirole\E$/,@possroles)) { $ltirole = 'Learner'; } my @possdigest; my $digest_user = &Encode::decode_utf8($uname.':'.$udom); $digest_user = &Digest::SHA::sha1_hex($digest_user); push(@possdigest,$digest_user); if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') { @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/, $env{'course.'.$env{'request.course.id'}.'.languages'})); } else { my %langhash = &Apache::loncommon::getlangs($uname,$udom); if ($langhash{'languages'} ne '') { @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'}); } else { my %domdefs = &Apache::lonnet::get_domain_defaults($udom); if ($domdefs{'lang_def'} ne '') { @userlangs = ($domdefs{'lang_def'}); } } } if (scalar(@userlangs) == 1) { $locale = $userlangs[0]; } my ($title,$digest_symb); my ($symb) = &Apache::lonnet::whichuser(); if ($symb) { $digest_symb = &Encode::decode_utf8($symb); $digest_symb = &Digest::SHA::sha1_hex($digest_symb); push(@possdigest,$digest_symb); my $navmap = Apache::lonnavmaps::navmap->new(); if (ref($navmap)) { my $res = $navmap->getBySymb($symb); if (ref($res)) { $title = $res->compTitle(); } } } my $domdesc = &Apache::lonnet::domain($cdom); my $primary_id = &Apache::lonnet::domain($cdom,'primary'); my $int_dom = &Apache::lonnet::internet_dom($primary_id); my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom,$r); my %ltiparams = ( lti_version => $version, lti_message_type => $msgtype, resource_link_title => $title, resource_link_id => $digest_symb, tool_consumer_instance_guid => $lonhost, tool_consumer_instance_description => $domdesc, tool_consumer_info_product_family_code => 'loncapa', tool_consumer_instance_name => $int_dom, tool_consumer_instance_url => $portal_url, tool_consumer_info_version => $loncaparev, user_id => $digest_user, roles => $ltirole, context_id => $env{'request.course.id'}, context_type => $context_type, context_label => $crslabel, context_title => $crstitle, launch_presentation_locale => $locale, ); my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'}; my $crshostname = &Apache::lonnet::hostname($crshome); if ($crshostname) { my $crsprotocol = $Apache::lonnet::protocol{$crshome}; unless ($crsprotocol eq 'https') { $crsprotocol = 'http'; } } if ($display{'target'}) { $ltiparams{'launch_presentation_document_target'} = $display{'target'}; } if ($display{'width'}) { $ltiparams{'launch_presentation_width'} = $display{'width'}; } if ($display{'height'}) { $ltiparams{'launch_presentation_height'} = $display{'height'}; } if ($fields{'firstname'}) { $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'}; } if ($fields{'lastname'}) { $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'}; } if ($fields{'fullname'}) { $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom); } if ($fields{'email'}) { my %emails = &Apache::loncommon::getemails($uname,$udom); my $contact_email; foreach my $type ('permanentemail','critnotification','notification') { if ($emails{$type} =~ /\@/) { $contact_email = $emails{$type}; last; } } $ltiparams{'lis_person_contact_email_primary'} = $contact_email; } if ($fields{'user'}) { if ($incdom) { $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; } else { $ltiparams{'lis_person_sourcedid'} = $uname; } } if (keys(%custom)) { foreach my $key (keys(%custom)) { my $value = $custom{$key}; $value =~ s/^\s+|\s+\$//g; if ($value =~ /^\QLONCAPA::env{\E([^\}]+)\}$/) { if (exists($env{$1})) { $value = $env{$1}; } } $ltiparams{'custom_'.$key} = $value; } } foreach my $key (keys(%ltiparams)) { $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key}); } $ltiparams{'basiclti_submit'} = $submittext; return %ltiparams; } sub launch_html { my ($url,$key,$secret,$sigmethod,$submittext,$paramsref) = @_; my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref,$sigmethod); my $action = &HTML::Entities::encode($url,'<>&"'); my $form = <<"END";
END if (ref($hashref) eq 'HASH') { foreach my $item (keys(%{$hashref})) { my $type = 'hidden'; if ($item eq 'basiclti_submit') { $type = 'submit'; } $form .= ''."\n"; } } $form .= "
\n"; $form .= <<"ENDJS"; ENDJS $form .= "\n"; return $form; } 1;