--- loncom/lti/ltiutils.pm 2018/05/28 23:26:04 1.11 +++ loncom/lti/ltiutils.pm 2018/05/30 17:22:30 1.12 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utility functions for managing LON-CAPA LTI interactions # -# $Id: ltiutils.pm,v 1.11 2018/05/28 23:26:04 raeburn Exp $ +# $Id: ltiutils.pm,v 1.12 2018/05/30 17:22:30 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -38,6 +38,7 @@ use Apache::loncoursedata; use Apache::lonuserutils; use Apache::lonenc(); use Apache::longroup(); +use Apache::lonlocal; use Math::Round(); use LONCAPA qw(:DEFAULT :match); @@ -573,6 +574,13 @@ sub lti_provider_scope { } } +# +# LON-CAPA as LTI Provider +# +# Obtain a list of course personnel and students from +# the LTI Consumer which launched this instance. +# + sub get_roster { my ($id,$url,$ckey,$secret) = @_; my %ltiparams = ( @@ -631,6 +639,13 @@ sub get_roster { return; } +# +# LON-CAPA as LTI Provider +# +# Passback a grade for a user to the LTI Consumer which originally +# provided the lis_result_sourcedid +# + sub send_grade { my ($id,$url,$ckey,$secret,$scoretype,$total,$possible) = @_; my $score; @@ -671,6 +686,16 @@ sub send_grade { } } +# +# LON-CAPA as LTI Provider +# +# Create a new user in LON-CAPA. If the domain's configuration +# includes rules for format of "official" usernames, those rules +# will apply when determining if a user is to be created. In +# additional if institutional user information is available that +# will be used when creating a new user account. +# + sub create_user { my ($ltiref,$uname,$udom,$domdesc,$data,$alerts,$rulematch,$inst_results, $curr_rules,$got_rules) = @_; @@ -768,8 +793,17 @@ sub create_user { return $result; } +# +# LON-CAPA as LTI Provider +# +# Create a password for a new user if the authentication +# type to assign to new users created following LTI launch is +# to be LON-CAPA "internal". +# + sub create_passwd { my $passwd = ''; + srand( time() ^ ($$ + ($$ << 15)) ); # Seed rand. my @letts = ("a".."z"); for (my $i=0; $i<8; $i++) { my $lettnum = int(rand(2)); @@ -788,8 +822,20 @@ sub create_passwd { return ($passwd); } +# +# LON-CAPA as LTI Provider +# +# Enroll a user in a LON-CAPA course, with the specified role and (optional) +# section. If this is a self-enroll case, i.e., a user launched the LTI tool +# in the Consumer, user privs will be added to the user's environment for +# the new role. +# +# If this is a self-enroll case, a Course Coordinator role will only be assigned +# if the current user is also the course owner. +# + sub enrolluser { - my ($udom,$uname,$role,$cdom,$cnum,$sec,$start,$end) = @_; + my ($udom,$uname,$role,$cdom,$cnum,$sec,$start,$end,$selfenroll) = @_; my $enrollresult; my $area = "/$cdom/$cnum"; if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) { @@ -801,16 +847,68 @@ sub enrolluser { $enrollresult = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef, undef,undef,$sec,$end,$start, - 'ltienroll',undef,$cdom.'_'.$cnum,undef, - 'ltienroll','',$instcid); + 'ltienroll',undef,$cdom.'_'.$cnum, + $selfenroll,'ltienroll','',$instcid); } elsif ($role =~ /^(cc|in|ta|ep)$/) { $enrollresult = &Apache::lonnet::assignrole($udom,$uname,$area,$role,$end,$start, - undef,undef,'ltienroll'); + undef,$selfenroll,'ltienroll'); + } + if ($enrollresult eq 'ok') { + if ($selfenroll) { + my (%userroles,%newrole,%newgroups); + &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum, + $area); + &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups); + $userroles{'user.role.'.$spec} = $start.'.'.$end; + &Apache::lonnet::appenv(\%userroles,[$role,'cm']); + } } return $enrollresult; } +# +# LON-CAPA as LTI Provider +# +# Batch addition of users following LTI launch by a user +# with LTI Instructor status. +# +# A list of users is obtained by a call to get_roster() +# if the calling Consumer support the LTI extension: +# Context Memberships Service. +# +# If a user included in the retrieved list does not currently +# have a user account in LON-CAPA, an account will be created. +# +# If a user already has an account, and the same role and +# section assigned (currently active), then no change will +# be made for that user. +# +# Information available for new users (besides username and) +# role) may include: first name, last name, full name (from +# which middle name will be extracted), permanent e-mail address, +# and lis_result_sourcedid (for passback of grades). +# +# If grades are to be passed back, the passback url will be +# the same as for the current user's session. +# +# The roles which may be assigned will be determined from the +# LTI roles included in the retrieved roster, and the mapping +# of LTI roles to LON-CAPA roles configured for this LTI Consumer +# in the domain configuration. +# +# Course Coordinator roles will only be assigned if the current +# user is also the course owner. +# +# The domain configuration for the corresponding Consumer can include +# a section to assign to LTI users. If the roster includes students +# any existing student roles with a different section will be expired, +# and a role in the LTI section will be assigned. +# +# For non-student rules (excluding Course Coordinator) a role will be +# assigned with the LTI section )or no section, if one is not rquired. +# + sub batchaddroster { my ($item) = @_; return unless(ref($item) eq 'HASH'); @@ -1015,6 +1113,27 @@ sub batchaddroster { return; } +# +# LON-CAPA as LTI Provider +# +# Gather a list of available LON-CAPA roles derived +# from a comma separated list of LTI roles. +# +# Which LON-CAPA roles are assignable by the current user +# and how LTI roles map to LON-CAPA roles (as defined in +# the domain configuration for the specific Consumer) are +# factored in when compiling the list of available roles. +# +# Inputs: 3 +# $rolestr - comma separated list of LTI roles. +# $allowedroles - reference to array of assignable LC roles +# $maproles - ref to HASH of mapping of LTI roles to LC roles +# +# Outputs: 2 +# (a) reference to array of available LC roles. +# (b) reference to array of LTI roles. +# + sub get_lc_roles { my ($rolestr,$allowedroles,$maproles) = @_; my (@ltiroles,@lcroles); @@ -1055,6 +1174,15 @@ sub get_lc_roles { return (\@lcroles,\@ltiroles); } +# +# LON-CAPA as LTI Provider +# +# Compares current start and dates for a user's role +# with dates to apply for the same user/role to +# determine if there is a change between the current +# ones and the updated ones. +# + sub datechange_check { my ($oldstart,$oldend,$startdate,$enddate) = @_; my $datechange = 0; @@ -1073,6 +1201,13 @@ sub datechange_check { return $datechange; } +# +# LON-CAPA as LTI Provider +# +# Store the URL used by a specific LTI Consumer to process grades passed back +# by an LTI Provider. +# + sub store_passbackurl { my ($ltinum,$pburl,$cdom,$cnum) = @_; my %history = &Apache::lonnet::restore($ltinum,'passbackurl',$cdom,$cnum);