private static LtiRequest GetLtiContentItemSelectionResponse(string url, string data)
        {
            // Both links should pass the user's username as a custom parameter
            var custom = new Dictionary <string, string> {
                { "username", "$User.username" }
            };

            // Create a graph with 2 LtiLinks
            var graph = new ContentItemSelectionGraph
            {
                Graph = new List <ContentItem>
                {
                    new LtiLink
                    {
                        Custom          = custom,
                        Id              = new Uri("https://www.company.com/tool/1"),
                        MediaType       = LtiConstants.LtiLtiLinkMediaType,
                        Text            = "Tool 1",
                        Title           = "Tool 1",
                        PlacementAdvice = new ContentItemPlacement
                        {
                            PresentationDocumentTarget = DocumentTarget.window
                        }
                    },
                    new LtiLink
                    {
                        Custom          = custom,
                        Id              = new Uri("https://www.company.com/tool/2"),
                        MediaType       = LtiConstants.LtiLtiLinkMediaType,
                        Text            = "Tool 2",
                        Title           = "Tool 2",
                        PlacementAdvice = new ContentItemPlacement
                        {
                            PresentationDocumentTarget = DocumentTarget.iframe
                        }
                    }
                }
            };

            // ReSharper disable once UseObjectOrCollectionInitializer
            var ltiRequest = new LtiRequest(LtiConstants.ContentItemSelectionLtiMessageType)
            {
                ConsumerKey = "12345",
                Url         = new Uri(url)
            };

            ltiRequest.ContentItems = graph.ToJsonLdString();
            ltiRequest.Data         = data;

            return(ltiRequest);
        }
示例#2
0
        /// <summary>
        /// Create an LtiRequestViewModel that contains a ContentItemPlacementResponse.
        /// </summary>
        /// <param name="url">The content_item_return_url from the content-item message.</param>
        /// <param name="consumerKey">The OAuth consumer key to use to sign the request.</param>
        /// <param name="consumerSecret">The OAuth consumer secret to use to sign the request.</param>
        /// <param name="contentItems">The ContentItemPlacementResponse to send.</param>
        /// <param name="data">The data received in the original content-item message.</param>
        /// <param name="ltiErrorLog">Optional plain text error message to be logged by the Tool Consumer.</param>
        /// <param name="ltiErrorMsg">Optional plain text error message to be displayed by the Tool Consumer.</param>
        /// <param name="ltiLog">Optional plain text message to be logged by the Tool Consumer.</param>
        /// <param name="ltiMsg">Optional plain text message to be displayed by the Tool Consumer.</param>
        /// <returns>The LtiRequestViewModel which contains a signed version of the response.</returns>
        public static LtiRequestViewModel CreateContentItemSelectionViewModel(
            string url, string consumerKey, string consumerSecret,
            ContentItemSelectionGraph contentItems, string data,
            string ltiErrorLog, string ltiErrorMsg, string ltiLog, string ltiMsg)
        {
            var ltiRequest = new LtiRequest(LtiConstants.ContentItemSelectionLtiMessageType)
            {
                Url          = new Uri(url),
                ConsumerKey  = consumerKey,
                ContentItems = contentItems.ToJsonLdString(),
                Data         = data,
                LtiErrorLog  = ltiErrorLog,
                LtiErrorMsg  = ltiErrorMsg,
                LtiLog       = ltiLog,
                LtiMsg       = ltiMsg
            };

            return(ltiRequest.GetViewModel(consumerSecret));
        }
示例#3
0
        public ActionResult PlaceContentItem(int id)
        {
            var tool = ProviderContext.Tools.Find(id);

            if (tool == null)
            {
                return(RedirectToAction("BadRequest", "Error", new { error = "Invalid tool id" }));
            }

            var ltiRequest = GetLtiRequestFromClaim();

            if (ltiRequest == null)
            {
                return(RedirectToAction("BadRequest", "Error", new { error = "Invalid LTI request" }));
            }

            var consumer = ProviderContext.Consumers.SingleOrDefault(c => c.Key.Equals(ltiRequest.ConsumerKey));

            if (consumer == null)
            {
                return(RedirectToAction("BadRequest", "Error", new { error = "Invalid consumer" }));
            }

            // Prepare the custom parameters this TP would like on each link.
            var custom = new Dictionary <string, string>();

            // The next two custom parameters use well-known custom parameter substitution variables.
            custom.Add("username", "$User.username");                 // Used by this TP when pairing a new user
            custom.Add("tc_profile_url", "$ToolConsumerProfile.url"); // Used by this TP to determine TC capabilities

            // Determine the best PresentationDocumentTarget from the list of targets acceptable
            // to the TC assuming the TC sent the list of acceptable targets in priority order
            var presentationDocumentTarget            = DocumentTarget.iframe;
            var acceptablePresentationDocumentTargets =
                ParseDocumentTargets(ltiRequest.AcceptPresentationDocumentTargets);

            if (acceptablePresentationDocumentTargets.Count > 0)
            {
                if (!acceptablePresentationDocumentTargets.Contains(presentationDocumentTarget))
                {
                    presentationDocumentTarget = acceptablePresentationDocumentTargets[0];
                }
            }

            // Calculate the full qualified URL for the tool.
            var toolUrl = UrlHelper.GenerateUrl("Default", "View", "Tool", new RouteValueDictionary(new { id }),
                                                RouteTable.Routes, Request.RequestContext, false);
            Uri toolUri;

            Uri.TryCreate(Request.Url, toolUrl, out toolUri);

            // Start building the response
            var graph = new List <ContentItem>
            {
                new LtiLink
                {
                    Custom          = custom,
                    Id              = toolUri,
                    MediaType       = LtiConstants.LtiLinkMediaType,
                    Text            = tool.Description ?? ltiRequest.Text,
                    Title           = tool.Name ?? ltiRequest.Title,
                    PlacementAdvice = new ContentItemPlacement
                    {
                        PresentationDocumentTarget = presentationDocumentTarget
                    }
                }
            };
            var response = new ContentItemSelectionGraph
            {
                Graph = graph
            };

            // Content-Item Message 1.0 sends each request (which can have many items)
            // back to the Tool Consumer.
            var model = ContentItemsClient.CreateContentItemSelectionViewModel(
                ltiRequest.ContentItemReturnUrl, consumer.Key,
                consumer.Secret, response, ltiRequest.Data,
                null, null, null, "Selected " + tool.Name);

            return(View(model));
        }
示例#4
0
 /// <summary>
 /// Create an LtiRequestViewModel that contains a ContentItemPlacementResponse.
 /// </summary>
 /// <param name="url">The content_item_return_url from the content-item message.</param>
 /// <param name="consumerKey">The OAuth consumer key to use to sign the request.</param>
 /// <param name="consumerSecret">The OAuth consumer secret to use to sign the request.</param>
 /// <param name="contentItems">The ContentItemPlacementResponse to send.</param>
 /// <param name="data">The data received in the original content-item message.</param>
 /// <returns>The LtiRequestViewModel which contains a signed version of the response.</returns>
 public static LtiRequestViewModel CreateContentItemSelectionViewModel(
     string url, string consumerKey, string consumerSecret,
     ContentItemSelectionGraph contentItems, string data)
 {
     return(CreateContentItemSelectionViewModel(url, consumerKey, consumerSecret, contentItems, data, null, null, null, null));
 }