--- loncom/lti/ltiutils.pm	2018/01/04 12:19:25	1.5
+++ loncom/lti/ltiutils.pm	2018/04/17 14:03:01	1.8
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA LTI interactions 
 #
-# $Id: ltiutils.pm,v 1.5 2018/01/04 12:19:25 raeburn Exp $
+# $Id: ltiutils.pm,v 1.8 2018/04/17 14:03:01 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -458,4 +458,93 @@ sub release_tool_lock {
     }
 }
 
+#
+# LON-CAPA as LTI Provider
+#
+# Use the part of the launch URL after /adm/lti to determine
+# the scope for the current session (i.e., restricted to a
+# single resource, to a single folder/map, or to an entire
+# course).
+#
+# Returns an array containing scope: resource, map, or course
+# and the LON-CAPA URL that is displayed post-launch, including
+# accommodation of URL encryption, and translation of a tiny URL
+# to the actual URL
+#
+
+sub lti_provider_scope {
+    my ($tail,$cdom,$cnum) = @_;
+    my ($scope,$realuri);
+    if ($tail =~ m{^/uploaded/$cdom/$cnum/(?:default|supplemental)(?:|_\d+)\.(?:sequence|page)(|___\d+___.+)$}) {
+        my $rest = $1;
+        if ($rest eq '') {
+            $scope = 'map';
+            $realuri = $tail;
+        } else {
+            my ($map,$resid,$url) = &Apache::lonnet::decode_symb($tail);
+            $realuri = &Apache::lonnet::clutter($url);
+            if ($url =~ /\.sequence$/) {
+                $scope = 'map';
+            } else {
+                $scope = 'resource';
+                $realuri .= '?symb='.$tail;
+            }
+        }
+    } elsif ($tail =~ m{^/res/$match_domain/$match_username/.+\.(?:sequence|page)(|___\d+___.+)$}) {
+        my $rest = $1;
+        if ($rest eq '') {
+            $scope = 'map';
+            $realuri = $tail;
+        } else {
+            my ($map,$resid,$url) = &Apache::lonnet::decode_symb($tail);
+            $realuri = &Apache::lonnet::clutter($url);
+            if ($url =~ /\.sequence$/) {
+                $scope = 'map';
+            } else {
+                $scope = 'resource';
+                $realuri .= '?symb='.$tail;
+            }
+        }
+    } elsif ($tail =~ m{^/tiny/$cdom/(\w+)$}) {
+        my $key = $1;
+        my $tinyurl;
+        my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
+        if (defined($cached)) {
+            $tinyurl = $result;
+        } else {
+            my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
+            my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
+            if ($currtiny{$key} ne '') {
+                $tinyurl = $currtiny{$key};
+                &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
+            }
+        }
+        if ($tinyurl ne '') {
+            my ($cnum,$symb) = split(/\&/,$tinyurl,2);
+            my ($map,$resid,$url) = &Apache::lonnet::decode_symb($symb);
+            if ($url =~ /\.(page|sequence)$/) {
+                $scope = 'map';
+            } else {
+                $scope = 'resource';
+            }
+            if ((&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) &&
+                (!$env{'request.role.adv'})) {
+                $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
+                if ($scope eq 'resource') {
+                    $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
+                }
+            } else {
+                $realuri = &Apache::lonnet::clutter($url);
+                if ($scope eq 'resource') {
+                    $realuri .= '?symb='.$symb;
+                }
+            }
+        }
+    } elsif ($tail =~ m{^/$cdom/$cnum$}) {
+        $scope = 'course';
+        $realuri = '/adm/navmaps';
+    }
+    return ($scope,$realuri);
+}
+
 1;