--- loncom/interface/lonexttool.pm 2017/12/18 16:11:18 1.11 +++ loncom/interface/lonexttool.pm 2017/12/22 02:00:46 1.12 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Launch External Tool Provider (LTI) # -# $Id: lonexttool.pm,v 1.11 2017/12/18 16:11:18 raeburn Exp $ +# $Id: lonexttool.pm,v 1.12 2017/12/22 02:00:46 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -49,6 +49,10 @@ use HTML::Entities; use Apache::lonlocal; use Apache::lonnet; use Apache::loncommon; +use Apache::londatecheck; +use Apache::lonipcheck; +use Apache::lonhomework; +use Apache::structuretags; use LONCAPA::ltiutils; sub handler { @@ -62,6 +66,8 @@ sub handler { # ------------------------------------------------------------ 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? @@ -90,15 +96,15 @@ sub handler { 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 $is_tool; + my ($idx,$is_tool,%toolhash,%toolsettings); if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") { - my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); + %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum); if ($toolsettings{'id'}) { - my $idx = $toolsettings{'id'}; + $idx = $toolsettings{'id'}; my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer'); if (ref($ltitools{$idx}) eq 'HASH') { - my %toolhash = %{$ltitools{$idx}}; + %toolhash = %{$ltitools{$idx}}; $toolhash{'display'} = { target => $toolsettings{'target'}, width => $toolsettings{'width'}, @@ -107,40 +113,8 @@ sub handler { $toolhash{'crslabel'} = $toolsettings{'crslabel'}; $toolhash{'crstitle'} = $toolsettings{'crstitle'}; $toolhash{'crsappend'} = $toolsettings{'crsappend'}; + $toolhash{'gradable'} = $toolsettings{'gradable'}; $is_tool = 1; - my $launchok = 1; - if ($target eq 'tex') { - $r->print(&mt('External Tool')); - } 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)) { - 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'}, - $submittext,\%lti)); - } else { - $r->print('
'.&mt('External Tool Unavailable').'
'); - } - } } } } @@ -150,6 +124,79 @@ sub handler { } else { $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}'); } + return OK; + } + + my ($status,$open,$close,$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"); + my ($timelimit) = split(/_/,$interval[0]); + &Apache::lonnet::set_first_access($interval[1],$timelimit); + } + ($status,$msg) = &gradabletool_access_check(); + undef($Apache::lonhomework::browse); + if ($status eq 'SHOW_ANSWER') { + $r->print(&display_score()); + return OK; + } elsif ($status ne 'CAN_ANSWER') { + $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')); + } 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')); + } else { + $r->print($msg); + } + return OK; + } + } + } + my $launchok = 1; + if ($target eq 'tex') { + $r->print(&mt('External Tool')); + } 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)) { + 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'}, + $submittext,\%lti)); + } else { + $r->print('
'.&mt('External Tool Unavailable').'
'); + } } return OK; } @@ -393,4 +440,82 @@ ENDJS return $form; } +sub gradabletool_access_check { + 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'})) { + @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::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'}); + } + } + } + } + return ($status,$result,$resource_due); +} + +sub display_score { + 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 (!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 { + $comment='
'.$comment.'
'; + } + my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'}; + if (!defined($gradeinfo) || $gradeinfo!~/\w/) { + $gradeinfo=''; + } else { + $gradeinfo='
'.$gradeinfo.'
'; + } + return $display.$comment.$gradeinfo; +} + 1;