--- loncom/interface/lonexttool.pm 2023/01/23 18:38:26 1.22.2.2
+++ 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.22.2.2 2023/01/23 18:38:26 raeburn Exp $
+# $Id: lonexttool.pm,v 1.23 2022/03/29 20:12:46 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -51,6 +51,8 @@ use Apache::lonnet;
use Apache::loncommon;
use Apache::londatecheck;
use Apache::lonipcheck;
+use Apache::lonhomework;
+use Apache::structuretags;
use LONCAPA::ltiutils;
sub handler {
@@ -108,7 +110,7 @@ sub handler {
width => $toolsettings{'width'},
height => $toolsettings{'height'},
};
- foreach my $item (qw(crslabel crstitle crsappend)) {
+ foreach my $item (qw(crslabel crstitle crsappend gradable)) {
$toolhash{$item} = $toolsettings{$item};
}
$is_tool = 1;
@@ -132,16 +134,37 @@ sub handler {
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);
+ if ($toolhash{'gradable'}) {
+ $Apache::lonhomework::browse = &Apache::lonnet::allowed('bre',$r->uri);
+ if ($env{'form.markaccess'}) {
+ 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($target);
+ undef($Apache::lonhomework::browse);
+ if ($status eq 'SHOW_ANSWER') {
+ $r->print(&display_score($target));
+ if ($target eq 'tex') {
+ $r->print('\end{document}');
+ }
+ return OK;
+ } elsif ($status ne 'CAN_ANSWER') {
+ if ($target eq 'tex') {
+ $r->print('\end{document}');
+ } else {
+ $r->print($msg);
+ }
+ return OK;
}
- return OK;
} else {
- ($status,$msg)=&Apache::lonipcheck::ip_access_check();
+ 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}');
@@ -149,6 +172,16 @@ sub handler {
$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;
@@ -156,6 +189,20 @@ sub handler {
$r->print('\end{document}');
} else {
my $now = time;
+ if ($toolhash{'passback'}) {
+ if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
+ \%toolsettings,\%toolhash) eq 'ok') {
+ $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
+ } else {
+ undef($launchok);
+ }
+ }
+ if ($toolhash{'roster'}) {
+ if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
+ \%toolsettings,\%toolhash) eq 'ok') {
+ $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
+ }
+ }
my $submittext = &mt('Launch [_1]',$toolhash{'title'});
if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') &&
($toolhash{'url'} ne '') && ($launchok)) {
@@ -176,10 +223,15 @@ sub handler {
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);
+ $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
+ %display,%custom,@userlangs,$incdom);
if (ref($toolsref) eq 'HASH') {
$version = $toolsref->{'version'};
$toolname = $toolsref->{'title'};
+ $passback = $toolsref->{'passback'};
+ $gradesecret = $toolsref->{'gradesecret'};
+ $roster = $toolsref->{'roster'};
+ $rostersecret = $toolsref->{'rostersecret'};
$msgtype = $toolsref->{'messagetype'};
$incdom = $toolsref->{'incdom'};
if (ref($toolsref->{'fields'}) eq 'HASH') {
@@ -288,6 +340,43 @@ sub lti_params {
unless ($crsprotocol eq 'https') {
$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);
+ }
+ }
+ if ($roster) {
+ if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
+ $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
+ if ($rostersecret) {
+ my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
+ $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
+ }
+ }
+ }
+ if (($digest_symb) && ($gradesecret || $rostersecret)) {
+ unless ((exists($currdigest{$digest_symb})) && ($currdigest{$digest_symb} eq $symb)) {
+ $digesthash{$digest_symb} = $symb;
+ }
+ }
+ if (($passback) && ($gradesecret)) {
+ 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);
+ }
+ }
}
if ($display{'target'}) {
$ltiparams{'launch_presentation_document_target'} = $display{'target'};
@@ -380,4 +469,97 @@ ENDJS
return $form;
}
+sub gradabletool_access_check {
+ my ($target) = @_;
+ my ($result,$resource_due);
+ my $status;
+ my ($symb,$courseid,$udom,$uname) = &Apache::lonnet::whichuser();
+ my @targets;
+ if ($target) {
+ @targets = ($target);
+ } elsif (defined($env{'form.submitted'}) && defined($env{'form.validate'})) {
+ @targets = ('grade','web');
+ } else {
+ @targets = ('web');
+ }
+ foreach my $target (@targets) {
+ &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) =
+ &Apache::lonhomework::check_slot_access('0','tool',$symb);
+ if (( $status eq 'CLOSED' ) ||
+ ( $status eq 'UNCHECKEDOUT') ||
+ ( $status eq 'NOT_YET_VIEWED') ||
+ ( $status eq 'BANNED') ||
+ ( $status eq 'UNAVAILABLE') ||
+ ( $status eq 'NOT_IN_A_SLOT') ||
+ ( $status eq 'NOTRESERVABLE') ||
+ ( $status eq 'RESERVABLE') ||
+ ( $status eq 'RESERVABLE_LATER') ||
+ ( $status eq 'INVALID_ACCESS') ||
+ ( $status eq 'NEED_DIFFERENT_IP') ||
+ ( $status eq 'WAITING_FOR_GRADE')) {
+ $result = &Apache::structuretags::access_status_msg('tool',$status,$symb,
+ $target,$ipused,$accessmsg);
+ } elsif ($status eq 'NEEDS_CHECKIN') {
+ $result = &Apache::structuretags::checkin_prompt($target,$slot_name,$slot,'tool');
+ } elsif ($target eq 'web') {
+ if ($status eq 'CAN_ANSWER') {
+ $resource_due = &Apache::lonhomework::due_date(0, $env{'request.symb'});
+ if ($slot_name ne '') {
+ $resource_due = &Apache::structuretags::selfcheckin_resource($resource_due,
+ $slot_name,$slot,
+ $env{'request.symb'});
+ }
+ }
+ }
+ 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);
+ } else {
+ my $points = $awarded*$weight;
+ my $result = sprintf('%.2f',$points);
+ $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 {
+ if ($target eq 'tex') {
+ $comment = '\\\\'.$comment;
+ } else {
+ $comment='
';
+ }
+ }
+ my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'};
+ if (!defined($gradeinfo) || $gradeinfo!~/\w/) {
+ $gradeinfo='';
+ } else {
+ if ($target eq 'tex') {
+ $gradeinfo = '\\\\'.$gradeinfo;
+ } else {
+ $gradeinfo='
';
+ }
+ }
+ return $display.$comment.$gradeinfo;
+}
+
1;