Diff for /loncom/interface/lonexttool.pm between versions 1.5 and 1.15

version 1.5, 2017/11/30 01:52:14 version 1.15, 2017/12/30 19:51:30
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 Apache::lonhomework;
   use Apache::structuretags;
   use LONCAPA::ltiutils;
   
 sub handler {  sub handler {
     my $r=shift;      my $r=shift;
Line 62  sub handler { Line 66  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}');
         }          }
Line 89  sub handler { Line 96  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 $is_tool;      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 %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);          %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
         if ($toolsettings{'id'}) {          if ($toolsettings{'id'}) {
             my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);              $idx = $toolsettings{'id'};
             if (ref($ltitools{$toolsettings{'id'}}) eq 'HASH') {              my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
                 my %toolhash = %{$ltitools{$toolsettings{'id'}}};               if (ref($ltitools{$idx}) eq 'HASH') {
                   %toolhash = %{$ltitools{$idx}};
                 $toolhash{'display'} = {                  $toolhash{'display'} = {
                                            target => $toolsettings{'target'},                                             target => $toolsettings{'target'},
                                            width  => $toolsettings{'width'},                                             width  => $toolsettings{'width'},
Line 105  sub handler { Line 113  sub handler {
                 $toolhash{'crslabel'} = $toolsettings{'crslabel'};                  $toolhash{'crslabel'} = $toolsettings{'crslabel'};
                 $toolhash{'crstitle'} = $toolsettings{'crstitle'};                  $toolhash{'crstitle'} = $toolsettings{'crstitle'};
                 $toolhash{'crsappend'} = $toolsettings{'crsappend'};                  $toolhash{'crsappend'} = $toolsettings{'crsappend'};
                   $toolhash{'gradable'} = $toolsettings{'gradable'};
                 $is_tool = 1;                  $is_tool = 1;
                 if ($target eq 'tex') {  
                     $r->print(&mt('External Tool'));  
                 } else {  
                     my $submittext = &mt('Launch [_1]',$toolhash{'title'});  
                     if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {  
                         my %lti = &lti_params($r,$cnum,$cdom,$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('<div>'.&mt('External Tool Unavailable').'</div>');  
                     }  
                 }  
             }              }
         }          }
     }      }
Line 131  sub handler { Line 124  sub handler {
         } else {          } else {
             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');              $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.'}').'\\\\');
       }
       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;
           }
       } 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').'\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;
           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 = &lti_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;      return OK;
 }  }
   
 sub lti_params {  sub lti_params {
     my ($r,$cnum,$cdom,$submittext,$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,
         $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);          $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
           %display,%custom,@userlangs);
     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'};          $passback = $toolsref->{'passback'};
           $gradesecret = $toolsref->{'gradesecret'};
         $roster = $toolsref->{'roster'};          $roster = $toolsref->{'roster'};
           $rostersecret = $toolsref->{'rostersecret'};
         $msgtype = $toolsref->{'messagetype'};          $msgtype = $toolsref->{'messagetype'};
         if (ref($toolsref->{'fields'}) eq 'HASH') {          if (ref($toolsref->{'fields'}) eq 'HASH') {
             %fields = %{$toolsref->{'fields'}};              %fields = %{$toolsref->{'fields'}};
Line 247  sub lti_params { Line 336  sub lti_params {
         my $crsprotocol = $Apache::lonnet::protocol{$crshome};          my $crsprotocol = $Apache::lonnet::protocol{$crshome};
         unless ($crsprotocol eq 'https') {          unless ($crsprotocol eq 'https') {
             $crsprotocol = 'http';              $crsprotocol = 'http';
         }           }
         if ($passback) {          if (($passback) || ($roster)) {
             if ($ltirole eq 'Learner') {              if ($passback) {
                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/passback';                  $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
                 $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};                  $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
                 $ltiparams{'lis_result_sourcedid'} = ''; #FIXME                  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 ($roster) {                  if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
             if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {                      $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
                 $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/roster';                      if ($rostersecret) {
                 $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME                          my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
                           $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
                       }
                   }
               }
               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;
               }
               if (($passback) && ($gradesecret)) {
                   $digesthash{$digest_user} = $uname.':'.$udom;
               }
               if (keys(%digesthash)) {
                   &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
             }              }
         }          }
     }      }
Line 292  sub lti_params { Line 403  sub lti_params {
         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;          $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
     }      }
     if ($fields{'user'}) {      if ($fields{'user'}) {
         $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;           $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
     }      }
     if (keys(%custom)) {      if (keys(%custom)) {
         foreach my $key (keys(%custom)) {          foreach my $key (keys(%custom)) {
Line 314  sub lti_params { Line 425  sub lti_params {
 }  }
   
 sub launch_html {  sub launch_html {
     my ($url,$key,$secret,$submittext,$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,$sigmethod,$paramsref);
     my $action = &HTML::Entities::encode($url,'<>&"');      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">
Line 349  ENDJS Line 460  ENDJS
     return $form;      return $form;
 }  }
   
 sub sign_params {  sub gradabletool_access_check {
     my ($url,$key,$secret,$paramsref) = @_;      my ($target) = @_;
     my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0)));      my ($result,$resource_due);
     my $request = Net::OAuth->request("request token")->new(      my $status;
             consumer_key => $key,      my ($symb,$courseid,$udom,$uname) = &Apache::lonnet::whichuser();
             consumer_secret => $secret,      my @targets;
             request_url => $url,      if ($target) {
             request_method => 'POST',          @targets = ($target);
             signature_method => 'HMAC-SHA1',      } elsif (defined($env{'form.submitted'}) && defined($env{'form.validate'})) {
             timestamp => time,          @targets = ('grade','web');
             nonce => $nonce,      } else {
             callback => 'about:blank',          @targets = ('web');
             extra_params => $paramsref,      }
             version      => '1.0',      foreach my $target (@targets) {
             );          undef(%Apache::lonhomework::results);
     $request->sign;          undef(%Apache::lonhomework::history);
     return $request->to_hash();          %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 ($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='<br /><table><tr><td bgcolor="#FFFFDD">'.$comment.'</td></tr></table>';
           }
       }
       my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'};
       if (!defined($gradeinfo) || $gradeinfo!~/\w/) {
           $gradeinfo='';
       } else {
           if ($target eq 'tex') {
               $gradeinfo = '\\\\'.$gradeinfo;
           } else {
               $gradeinfo='<br /><table><tr><td bgcolor="#DDDDFF"><font size="+2">'.$gradeinfo.'</font></td></tr></table>';
           }
       }
       return $display.$comment.$gradeinfo;
 }  }
   
 1;  1;

Removed from v.1.5  
changed lines
  Added in v.1.15


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>