version 1.2, 2016/01/25 20:13:02
|
version 1.22.2.1, 2021/12/31 15:12:52
|
Line 43 package Apache::lonexttool;
|
Line 43 package Apache::lonexttool;
|
|
|
use strict; |
use strict; |
use Apache::Constants qw(:common :http); |
use Apache::Constants qw(:common :http); |
use Net::OAuth; |
|
use Encode; |
use Encode; |
use Digest::SHA; |
use Digest::SHA; |
use HTML::Entities; |
use HTML::Entities; |
use Apache::lonlocal; |
use Apache::lonlocal; |
use Apache::lonnet; |
use Apache::lonnet; |
use Apache::loncommon; |
use Apache::loncommon; |
|
use Apache::londatecheck; |
|
use Apache::lonipcheck; |
|
use LONCAPA::ltiutils; |
|
|
sub handler { |
sub handler { |
my $r=shift; |
my $r=shift; |
Line 62 sub handler {
|
Line 64 sub handler {
|
# ------------------------------------------------------------ Print the screen |
# ------------------------------------------------------------ Print the screen |
if ($target eq 'tex') { |
if ($target eq 'tex') { |
$r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'})); |
$r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'})); |
|
} else { |
|
$target = 'web'; |
} |
} |
|
|
# Is this even in a course? |
# Is this even in a course? |
unless ($env{'request.course.id'}) { |
unless ($env{'request.course.id'}) { |
if ($target ne 'tex') { |
if ($target ne 'tex') { |
&Apache::loncommon::simple_error_page($r,'','Not in a course'); |
&Apache::loncommon::simple_error_page($r,'','Not in a course', |
|
{'only_body' => 1}); |
} else { |
} else { |
$r->print('\textbf{Not in a course}\end{document}'); |
$r->print('\textbf{Not in a course}\end{document}'); |
} |
} |
return OK; |
return OK; |
} |
} |
|
|
my $marker = (split(m{/},$r->uri))[4]; |
my ($marker,$exttool) = (split(m{/},$r->uri))[4,5]; |
$marker=~s/\D//g; |
$marker=~s/\D//g; |
|
|
if (!$marker) { |
if (!$marker) { |
if ($target ne 'tex') { |
if ($target ne 'tex') { |
&Apache::loncommon::simple_error_page($r,'Invalid Call', |
$r->print(&mt('Invalid Call')); |
'Invalid Call'); |
|
} else { |
} else { |
$r->print('\textbf{Invalid call}\end{document}'); |
$r->print('\textbf{'&mt('Invalid Call').'}\end{document}'); |
} |
} |
return OK; |
return OK; |
} |
} |
Line 90 sub handler {
|
Line 94 sub handler {
|
my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; |
my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; |
my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; |
my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; |
my $chome = $env{'course.'.$env{'request.course.id'}.'.home'}; |
my $chome = $env{'course.'.$env{'request.course.id'}.'.home'}; |
|
my ($idx,$is_tool,%toolhash,%toolsettings); |
|
|
if ($r->uri eq "/adm/$cdom/$cnum/$marker/exttool") { |
if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") { |
my %toolhash=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); |
%toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); |
if ($target eq 'tex') { |
if ($toolsettings{'id'}) { |
$r->print(&mt('External Tool')); |
$idx = $toolsettings{'id'}; |
} else { |
my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer'); |
if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) { |
if (ref($ltitools{$idx}) eq 'HASH') { |
my %lti = <i_params($r,\%toolhash); |
%toolhash = %{$ltitools{$idx}}; |
$r->print(&launch_html($toolhash{'url'},$toolhash{'key'}, |
$toolhash{'display'} = { |
$toolhash{'secret'},$toolhash{'title'},\%lti)); |
target => $toolsettings{'target'}, |
} else { |
width => $toolsettings{'width'}, |
&Apache::loncommon::simple_error_page($r,'External Tool Unavailable', |
height => $toolsettings{'height'}, |
'External Tool Unavailable'); |
}; |
|
foreach my $item (qw(crslabel crstitle crsappend)) { |
|
$toolhash{$item} = $toolsettings{$item}; |
|
} |
|
$is_tool = 1; |
} |
} |
} |
} |
} else { |
} |
|
unless ($is_tool) { |
if ($target ne 'tex') { |
if ($target ne 'tex') { |
&Apache::loncommon::simple_error_page($r,'Invalid Call', |
$r->print('<div>'.&mt('Invalid Call').'</div>'); |
'Invalid Call'); |
|
} else { |
} else { |
$r->print('\textbf{Invalid call}\end{document}'); |
$r->print('\textbf{'.&mt(Invalid Call).'}\end{document}'); |
} |
} |
return OK; |
return OK; |
} |
} |
|
|
&print_end_page($r,$target); |
my ($symb,$status,$open,$close,$msg,$donebuttonresult,$donemsg); |
return OK; |
if (($target eq 'tex') || ($toolhash{'gradable'})) { |
} |
($symb) = &Apache::lonnet::whichuser(); |
|
} |
sub print_end_page { |
if ($target eq 'tex') { |
my ($r,$target) = @_; |
my $title = &Apache::lonnet::gettitle($symb); |
if ($target ne 'tex') { |
$r->print(&mt('External Tool: [_1]','\textit{'.$title.'}').'\\\\'); |
$r->print(&Apache::loncommon::end_page()); |
} |
|
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 { |
} 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}'); |
$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('<div>'.&mt('External Tool Unavailable').'</div>'); |
|
} |
} |
} |
|
return OK; |
} |
} |
|
|
sub lti_params { |
sub lti_params { |
my ($r,$toolsref) = @_; |
my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_; |
my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale, |
my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale, |
%fields,%rolesmap,%display,%custom,@userlangs); |
$crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs,$incdom); |
if (ref($toolsref) eq 'HASH') { |
if (ref($toolsref) eq 'HASH') { |
$version = $toolsref->{'version'}; |
$version = $toolsref->{'version'}; |
$toolname = $toolsref->{'title'}; |
$toolname = $toolsref->{'title'}; |
$passback = $toolsref->{'passback'}; |
|
$roster = $toolsref->{'roster'}; |
|
$msgtype = $toolsref->{'messagetype'}; |
$msgtype = $toolsref->{'messagetype'}; |
|
$incdom = $toolsref->{'incdom'}; |
if (ref($toolsref->{'fields'}) eq 'HASH') { |
if (ref($toolsref->{'fields'}) eq 'HASH') { |
%fields = %{$toolsref->{'fields'}}; |
%fields = %{$toolsref->{'fields'}}; |
} |
} |
Line 150 sub lti_params {
|
Line 194 sub lti_params {
|
if (ref($toolsref->{'custom'}) eq 'HASH') { |
if (ref($toolsref->{'custom'}) eq 'HASH') { |
%custom = %{$toolsref->{'custom'}}; |
%custom = %{$toolsref->{'custom'}}; |
} |
} |
|
$crslabel = $toolsref->{'crslabel'}; |
|
$crstitle = $toolsref->{'crstitle'}; |
} |
} |
if ($version eq '') { |
if ($version eq '') { |
$version = 'LTI-1p0'; |
$version = 'LTI-1p0'; |
Line 160 sub lti_params {
|
Line 206 sub lti_params {
|
if ($msgtype eq '') { |
if ($msgtype eq '') { |
$msgtype = 'basic-lti-launch-request'; |
$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 $lonhost = $r->dir_config('lonHostID'); |
my $loncaparev = $r->dir_config('lonVersion'); |
my $loncaparev = $r->dir_config('lonVersion'); |
my $uname = $env{'user.name'}; |
my $uname = $env{'user.name'}; |
my $udom = $env{'user.domain'}; |
my $udom = $env{'user.domain'}; |
my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner); |
my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner); |
my $ltirole = $rolesmap{$env{'request.role'}}; |
my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./); |
|
my $ltirole = $rolesmap{$roleprefix}; |
unless (grep(/^\Q$ltirole\E$/,@possroles)) { |
unless (grep(/^\Q$ltirole\E$/,@possroles)) { |
$ltirole = 'Learner'; |
$ltirole = 'Learner'; |
} |
} |
|
my @possdigest; |
my $digest_user = &Encode::decode_utf8($uname.':'.$udom); |
my $digest_user = &Encode::decode_utf8($uname.':'.$udom); |
$digest_user = &Digest::SHA::sha1_hex($digest_user); |
$digest_user = &Digest::SHA::sha1_hex($digest_user); |
|
push(@possdigest,$digest_user); |
if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') { |
if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') { |
@userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/, |
@userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/, |
$env{'course.'.$env{'request.course.id'}.'.languages'})); |
$env{'course.'.$env{'request.course.id'}.'.languages'})); |
Line 193 sub lti_params {
|
Line 248 sub lti_params {
|
if ($symb) { |
if ($symb) { |
$digest_symb = &Encode::decode_utf8($symb); |
$digest_symb = &Encode::decode_utf8($symb); |
$digest_symb = &Digest::SHA::sha1_hex($digest_symb); |
$digest_symb = &Digest::SHA::sha1_hex($digest_symb); |
|
push(@possdigest,$digest_symb); |
my $navmap = Apache::lonnavmaps::navmap->new(); |
my $navmap = Apache::lonnavmaps::navmap->new(); |
if (ref($navmap)) { |
if (ref($navmap)) { |
my $res = $navmap->getBySymb($symb); |
my $res = $navmap->getBySymb($symb); |
Line 201 sub lti_params {
|
Line 257 sub lti_params {
|
} |
} |
} |
} |
} |
} |
|
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 = ( |
my %ltiparams = ( |
lti_version => $version, |
lti_version => $version, |
lti_message_type => $msgtype, |
lti_message_type => $msgtype, |
resource_link_title => $title, |
resource_link_title => $title, |
resource_link_id => $digest_symb, |
resource_link_id => $digest_symb, |
tool_consumer_instance_guid => $lonhost, |
tool_consumer_instance_guid => $lonhost, |
|
tool_consumer_instance_description => $domdesc, |
tool_consumer_info_product_family_code => 'loncapa', |
tool_consumer_info_product_family_code => 'loncapa', |
|
tool_consumer_instance_name => $int_dom, |
|
tool_consumer_instance_url => $portal_url, |
tool_consumer_info_version => $loncaparev, |
tool_consumer_info_version => $loncaparev, |
user_id => $digest_user, |
user_id => $digest_user, |
lis_person_sourcedid => $uname.':'.$udom, |
|
roles => $ltirole, |
roles => $ltirole, |
context_id => $env{'request.course.id'}, |
context_id => $env{'request.course.id'}, |
context_type => $context_type, |
context_type => $context_type, |
context_label => $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}, |
context_label => $crslabel, |
context_title => $env{'course.'.$env{'request.course.id'}.'.description'}, |
context_title => $crstitle, |
launch_presentation_locale => $locale, |
launch_presentation_locale => $locale, |
); |
); |
my $crshostname = $env{'course.'.$env{'request.course.id'}.'.home'}; |
my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'}; |
my $crsprotocol = $Apache::lonnet::protocol{$crshostname}; |
my $crshostname = &Apache::lonnet::hostname($crshome); |
if ($crshostname) { |
if ($crshostname) { |
my $crsprotocol = $Apache::lonnet::protocol{$crshostname}; |
my $crsprotocol = $Apache::lonnet::protocol{$crshome}; |
unless ($crsprotocol eq 'https') { |
unless ($crsprotocol eq 'https') { |
$crsprotocol = 'http'; |
$crsprotocol = 'http'; |
} |
|
if ($passback) { |
|
if ($ltirole eq 'Learner') { |
|
$ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltipassback'; |
|
$ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'}; |
|
$ltiparams{'lis_result_sourcedid'} = ''; #FIXME |
|
} |
|
} |
|
if ($roster) { |
|
if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) { |
|
$ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltiroster'; |
|
$ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME |
|
} |
|
} |
} |
} |
} |
if ($display{'target'}) { |
if ($display{'target'}) { |
Line 260 sub lti_params {
|
Line 310 sub lti_params {
|
if ($fields{'email'}) { |
if ($fields{'email'}) { |
my %emails = &Apache::loncommon::getemails($uname,$udom); |
my %emails = &Apache::loncommon::getemails($uname,$udom); |
my $contact_email; |
my $contact_email; |
foreach my $email ('permanentemail','critnotification','notification') { |
foreach my $type ('permanentemail','critnotification','notification') { |
if ($email =~ /\@/) { |
if ($emails{$type} =~ /\@/) { |
$contact_email = $email; |
$contact_email = $emails{$type}; |
last; |
last; |
} |
} |
} |
} |
$ltiparams{'lis_person_contact_email_primary'} = $contact_email; |
$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)) { |
if (keys(%custom)) { |
foreach my $key (keys(%custom)) { |
foreach my $key (keys(%custom)) { |
$ltiparams{'custom_'.$key} = $custom{$key}; |
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)) { |
foreach my $key (keys(%ltiparams)) { |
$ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key}); |
$ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key}); |
} |
} |
|
$ltiparams{'basiclti_submit'} = $submittext; |
return %ltiparams; |
return %ltiparams; |
} |
} |
|
|
sub launch_html { |
sub launch_html { |
my ($url,$key,$secret,$toolname,$paramsref) = @_; |
my ($url,$key,$secret,$sigmethod,$submittext,$paramsref) = @_; |
my $hashref = &sign_params($url,$key,$secret,$paramsref); |
my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref,$sigmethod); |
my $submittext = &mt('Launch [_1]',$toolname); |
my $action = &HTML::Entities::encode($url,'<>&"'); |
my $form = <<"END"; |
my $form = <<"END"; |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
<body> |
<body> |
<div id="LCltiLaunch"> |
<div id="LCltiLaunch"> |
<form name="LCltiLaunchForm" action="$url" method="post" encType="application/x-www-form-urlencoded"> |
<form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded"> |
<input type="submit" name="LCbasicltiSubmit" value="$submittext" /> |
|
END |
END |
if (ref($hashref) eq 'HASH') { |
if (ref($hashref) eq 'HASH') { |
foreach my $item (keys(%{$hashref})) { |
foreach my $item (keys(%{$hashref})) { |
$form .= '<input type="hidden" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n"; |
my $type = 'hidden'; |
|
if ($item eq 'basiclti_submit') { |
|
$type = 'submit'; |
|
} |
|
$form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n"; |
} |
} |
} |
} |
$form .= "</form></div>\n"; |
$form .= "</form></div>\n"; |
Line 302 END
|
Line 370 END
|
document.getElementById("LCltiLaunch").style.display = "none"; |
document.getElementById("LCltiLaunch").style.display = "none"; |
nei = document.createElement('input'); |
nei = document.createElement('input'); |
nei.setAttribute('type','hidden'); |
nei.setAttribute('type','hidden'); |
nei.setAttribute('name','LCbasicltiSubmit'); |
nei.setAttribute('name','basiclti_submit'); |
nei.setAttribute('value','$submittext'); |
nei.setAttribute('value','$submittext'); |
document.getElementById("LCltiLaunchForm").appendChild(nei); |
document.getElementById("LCltiLaunchFormId").appendChild(nei); |
document.LCltiLaunchForm.submit(); |
document.LCltiLaunchForm.submit(); |
</script> |
</script> |
ENDJS |
ENDJS |
Line 312 ENDJS
|
Line 380 ENDJS
|
return $form; |
return $form; |
} |
} |
|
|
sub sign_params { |
|
my ($url,$key,$secret,$paramsref) = @_; |
|
my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0))); |
|
my $request = Net::OAuth->request("request token")->new( |
|
consumer_key => $key, |
|
consumer_secret => $secret, |
|
request_url => $url, |
|
request_method => 'POST', |
|
signature_method => 'HMAC-SHA1', |
|
timestamp => time, |
|
nonce => $nonce, |
|
callback => 'about:blank', |
|
extra_params => $paramsref, |
|
version => '1.0', |
|
); |
|
$request->sign; |
|
return $request->to_hash(); |
|
} |
|
|
|
1; |
1; |