--- loncom/interface/lonexttool.pm 2017/12/22 02:00:46 1.12 +++ loncom/interface/lonexttool.pm 2022/03/29 20:12:46 1.23 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Launch External Tool Provider (LTI) # -# $Id: lonexttool.pm,v 1.12 2017/12/22 02:00:46 raeburn Exp $ +# $Id: lonexttool.pm,v 1.23 2022/03/29 20:12:46 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -110,10 +110,9 @@ sub handler { width => $toolsettings{'width'}, height => $toolsettings{'height'}, }; - $toolhash{'crslabel'} = $toolsettings{'crslabel'}; - $toolhash{'crstitle'} = $toolsettings{'crstitle'}; - $toolhash{'crsappend'} = $toolsettings{'crsappend'}; - $toolhash{'gradable'} = $toolsettings{'gradable'}; + foreach my $item (qw(crslabel crstitle crsappend gradable)) { + $toolhash{$item} = $toolsettings{$item}; + } $is_tool = 1; } } @@ -127,28 +126,48 @@ sub handler { return OK; } - my ($status,$open,$close,$msg); + 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.'}').'\\\\'); + } if ($toolhash{'gradable'}) { $Apache::lonhomework::browse = &Apache::lonnet::allowed('bre',$r->uri); if ($env{'form.markaccess'}) { - my @interval=&Apache::lonnet::EXT("resource.0.interval"); + my @interval=&Apache::lonnet::EXT('resource.0.interval',$symb); my ($timelimit) = split(/_/,$interval[0]); &Apache::lonnet::set_first_access($interval[1],$timelimit); + } elsif ($symb && $env{'form.LC_interval_done'} eq 'true') { + # Set the event timer to zero if the "done button" was clicked. The button is + # part of the doneButton form created in lonmenu.pm + ($donebuttonresult,$donemsg) = &Apache::lonhomework::zero_timer($symb); + undef($env{'form.LC_interval_done'}); + undef($env{'form.LC_interval_done_proctorpass'}); } - ($status,$msg) = &gradabletool_access_check(); + ($status,$msg) = &gradabletool_access_check($target); undef($Apache::lonhomework::browse); if ($status eq 'SHOW_ANSWER') { - $r->print(&display_score()); + $r->print(&display_score($target)); + if ($target eq 'tex') { + $r->print('\end{document}'); + } return OK; } elsif ($status ne 'CAN_ANSWER') { - $r->print($msg); + if ($target eq 'tex') { + $r->print('\end{document}'); + } else { + $r->print($msg); + } return OK; } } else { 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')); + $r->print(&mt('Not open to be viewed').'\end{document}'); } else { $r->print($msg); } @@ -157,7 +176,7 @@ sub handler { ($status,$msg)=&Apache::lonipcheck::ip_access_check(); if ($status ne 'OPEN') { if ($target eq 'tex') { - $r->print(&mt('Not open to be viewed')); + $r->print(&mt('Not open to be viewed').'\end{document}'); } else { $r->print($msg); } @@ -167,7 +186,7 @@ sub handler { } my $launchok = 1; if ($target eq 'tex') { - $r->print(&mt('External Tool')); + $r->print('\end{document}'); } else { my $now = time; if ($toolhash{'passback'}) { @@ -193,7 +212,7 @@ sub handler { $url .= $toolhash{'crsappend'}; } $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'}, - $submittext,\%lti)); + $toolhash{'sigmethod'},$submittext,\%lti)); } else { $r->print('
'.&mt('External Tool Unavailable').'
'); } @@ -205,7 +224,7 @@ sub lti_params { my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_; my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale, $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap, - %display,%custom,@userlangs); + %display,%custom,@userlangs,$incdom); if (ref($toolsref) eq 'HASH') { $version = $toolsref->{'version'}; $toolname = $toolsref->{'title'}; @@ -214,6 +233,7 @@ sub lti_params { $roster = $toolsref->{'roster'}; $rostersecret = $toolsref->{'rostersecret'}; $msgtype = $toolsref->{'messagetype'}; + $incdom = $toolsref->{'incdom'}; if (ref($toolsref->{'fields'}) eq 'HASH') { %fields = %{$toolsref->{'fields'}}; } @@ -239,10 +259,10 @@ sub lti_params { $msgtype = 'basic-lti-launch-request'; } if ($crslabel eq '') { - $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}, + $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'}; } if ($crstitle eq '') { - $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'},; + $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'}; } my $lonhost = $r->dir_config('lonHostID'); my $loncaparev = $r->dir_config('lonVersion'); @@ -254,8 +274,10 @@ sub lti_params { unless (grep(/^\Q$ltirole\E$/,@possroles)) { $ltirole = 'Learner'; } - my $digest_user = &Encode::decode_utf8($uname.':'.$udom); + my @possdigest; + my $digest_user = &Encode::decode('UTF-8',$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'})); @@ -276,8 +298,9 @@ sub lti_params { my ($title,$digest_symb); my ($symb) = &Apache::lonnet::whichuser(); if ($symb) { - $digest_symb = &Encode::decode_utf8($symb); + $digest_symb = &Encode::decode('UTF-8',$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); @@ -289,7 +312,7 @@ 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); + my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom,$r); my %ltiparams = ( lti_version => $version, @@ -299,7 +322,7 @@ sub lti_params { 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_name => $int_dom, tool_consumer_instance_url => $portal_url, tool_consumer_info_version => $loncaparev, user_id => $digest_user, @@ -318,12 +341,17 @@ sub lti_params { $crsprotocol = 'http'; } if (($passback) || ($roster)) { + my (%currdigest,%digesthash); + if (@possdigest) { + %currdigest = &Apache::lonnet::get('exttools',\@possdigest, + $cdom,$cnum); + } if ($passback) { $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback'; $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'}; if ($gradesecret) { my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'}; - $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid); + $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid); } } if ($roster) { @@ -335,18 +363,15 @@ sub lti_params { } } } - my %digesthash; - if ($ltiparams{'lis_result_sourcedid'}) { - $digesthash{$ltiparams{'lis_result_sourcedid'}} = "$idx\0".time; - } - if ($ltiparams{'ext_ims_lis_memberships_id'}) { - $digesthash{$ltiparams{'ext_ims_lis_memberships_id'}} = "$idx\0".time; - } if (($digest_symb) && ($gradesecret || $rostersecret)) { - $digesthash{$digest_symb} = $symb; + unless ((exists($currdigest{$digest_symb})) && ($currdigest{$digest_symb} eq $symb)) { + $digesthash{$digest_symb} = $symb; + } } if (($passback) && ($gradesecret)) { - $digesthash{$digest_user} = $uname.':'.$udom; + unless ((exists($currdigest{$digest_user})) && ($currdigest{$digest_user} eq $uname.':'.$udom)) { + $digesthash{$digest_user} = $uname.':'.$udom; + } } if (keys(%digesthash)) { &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum); @@ -383,7 +408,11 @@ sub lti_params { $ltiparams{'lis_person_contact_email_primary'} = $contact_email; } if ($fields{'user'}) { - $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; + if ($incdom) { + $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; + } else { + $ltiparams{'lis_person_sourcedid'} = $uname; + } } if (keys(%custom)) { foreach my $key (keys(%custom)) { @@ -398,15 +427,15 @@ sub lti_params { } } foreach my $key (keys(%ltiparams)) { - $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key}); + $ltiparams{$key} = &Encode::decode('UTF-8',$ltiparams{$key}); } $ltiparams{'basiclti_submit'} = $submittext; return %ltiparams; } sub launch_html { - my ($url,$key,$secret,$submittext,$paramsref) = @_; - my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref); + 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"; @@ -441,20 +470,20 @@ ENDJS } sub gradabletool_access_check { + my ($target) = @_; my ($result,$resource_due); my $status; my ($symb,$courseid,$udom,$uname) = &Apache::lonnet::whichuser(); my @targets; - if (defined($env{'form.submitted'}) && defined($env{'form.validate'})) { + if ($target) { + @targets = ($target); + } elsif (defined($env{'form.submitted'}) && defined($env{'form.validate'})) { @targets = ('grade','web'); } else { @targets = ('web'); } foreach my $target (@targets) { - undef(%Apache::lonhomework::results); - undef(%Apache::lonhomework::history); - %Apache::lonhomework::history = - &Apache::lonnet::restore($symb,$courseid,$udom,$uname); + &Apache::structuretags::initialize_storage($symb); &Apache::lonhomework::set_show_problem_status(&Apache::lonnet::EXT('resource.0.problemstatus')); my ($accessmsg,$slot_name,$slot,$ipused); ($status,$accessmsg,$slot_name,$slot,$ipused) = @@ -485,35 +514,50 @@ sub gradabletool_access_check { } } } + if (keys(%Apache::lonhomework::results)) { + &Apache::structuretags::finalize_storage(); + } } return ($status,$result,$resource_due); } sub display_score { + my ($target) = @_; my $weight = &Apache::lonnet::EXT('resource.0.weight'); if ((!defined($weight)) || ($weight eq '')) { $weight=1; } my $awarded = $Apache::lonhomework::history{'resource.0.awarded'}; if (!defined($awarded)) { $awarded=0; } my $display=''; + if ($target eq 'tex') { + $display = '\\\\'; + } if (!defined($awarded)) { - $display = &mt('[_1] possible points.',$weight); + $display .= &mt('[_1] possible points.',$weight); } else { my $points = $awarded*$weight; my $result = sprintf('%.2f',$points); - $display = &mt('You have [_1] out of [quant,_2,possible point]', + $display .= &mt('You have [_1] out of [quant,_2,possible point]', $result,$weight); } my $comment = $Apache::lonhomework::history{'resource.0.comment'}; if (!defined($comment) || $comment!~/\w/) { $comment=''; } else { - $comment='
'.$comment.'
'; + if ($target eq 'tex') { + $comment = '\\\\'.$comment; + } else { + $comment='
'.$comment.'
'; + } } my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'}; if (!defined($gradeinfo) || $gradeinfo!~/\w/) { $gradeinfo=''; } else { - $gradeinfo='
'.$gradeinfo.'
'; + if ($target eq 'tex') { + $gradeinfo = '\\\\'.$gradeinfo; + } else { + $gradeinfo='
'.$gradeinfo.'
'; + } } return $display.$comment.$gradeinfo; }