Exemplo n.º 1
0
        public LineItemsController()
        {
            OnDeleteLineItem = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), InMemoryDb.ContextId, context.Id);

                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    InMemoryDb.LineItem = null;
                    InMemoryDb.Result   = null;
                    context.StatusCode  = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            OnGetLineItem = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.Id);

                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.LineItem   = InMemoryDb.LineItem;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            OnGetLineItemWithResults = context =>
            {
                OnGetLineItem(context);
                if (context.LineItem != null && InMemoryDb.Result != null)
                {
                    context.LineItem.Result = InMemoryDb.Result == null ? new LisResult[] {} : new[] { InMemoryDb.Result };
                }
                return(Task.FromResult <object>(null));
            };

            OnGetLineItems = context =>
            {
                context.LineItemContainerPage = new LineItemContainerPage
                {
                    ExternalContextId = LtiConstants.LineItemContainerContextId,
                    Id = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId),
                    LineItemContainer = new LineItemContainer
                    {
                        MembershipSubject = new LineItemMembershipSubject
                        {
                            ContextId = context.ContextId,
                            LineItems = new LineItem[] { }
                        }
                    }
                };

                if (InMemoryDb.LineItem != null &&
                    (string.IsNullOrEmpty(context.ActivityId) ||
                     context.ActivityId.Equals(InMemoryDb.LineItem.AssignedActivity.ActivityId)))
                {
                    context.LineItemContainerPage.LineItemContainer.MembershipSubject.LineItems = new[] { InMemoryDb.LineItem };
                }
                context.StatusCode = HttpStatusCode.OK;
                return(Task.FromResult <object>(null));
            };

            // Create a LineItem
            OnPostLineItem = context =>
            {
                if (InMemoryDb.LineItem != null)
                {
                    context.StatusCode = HttpStatusCode.BadRequest;
                    return(Task.FromResult <object>(null));
                }

                // Normally LineItem.Id would be calculated based on an id assigned by the database
                context.LineItem.Id      = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, InMemoryDb.LineItemId);
                context.LineItem.Results = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, InMemoryDb.LineItemId);
                InMemoryDb.LineItem      = context.LineItem;
                context.StatusCode       = HttpStatusCode.Created;
                return(Task.FromResult <object>(null));
            };

            // Update LineItem (but not results)
            OnPutLineItem = context =>
            {
                if (context.LineItem == null || InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(context.LineItem.Id))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.LineItem.Result = InMemoryDb.LineItem.Result;
                    InMemoryDb.LineItem     = context.LineItem;
                    context.StatusCode      = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            // Update LineItem and Result
            OnPutLineItemWithResults = context =>
            {
                if (context.LineItem == null || InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(context.LineItem.Id))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    InMemoryDb.LineItem = context.LineItem;
                    if (context.LineItem.Result != null && context.LineItem.Result.Length > 0)
                    {
                        InMemoryDb.Result = context.LineItem.Result[0];
                    }
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };
        }
Exemplo n.º 2
0
        private string GetResultsServiceUrl(string contextId, string lineItemId, string id = null)
        {
            var uri = RoutingHelper.GetResultsUri(HttpContext, contextId, lineItemId, id);

            return(uri == null ? null : uri.AbsoluteUri);
        }
Exemplo n.º 3
0
        public ResultsController()
        {
            OnDeleteResult = context =>
            {
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (resultUri == null || InMemoryDb.Result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    InMemoryDb.Result  = null;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            OnGetResult = context =>
            {
                // https://www.imsglobal.org/specs/ltiomv2p0/specification-3
                // When a line item is created, a result for each user is deemed to be created with a status value of “Initialized”.
                // Thus, there is no need to actually create a result with a POST request; the first connection to a result may be a PUT or a GET request.

                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId);
                var resultUri   = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else if (InMemoryDb.Result == null || !InMemoryDb.Result.Id.Equals(resultUri))
                {
                    context.Result = new LisResult
                    {
                        ExternalContextId = LtiConstants.ResultContextId,
                        Id           = resultUri,
                        ResultOf     = lineItemUri,
                        ResultStatus = ResultStatus.Initialized
                    };
                    context.StatusCode = HttpStatusCode.OK;
                }
                else
                {
                    context.Result     = InMemoryDb.Result;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            OnGetResults = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                                                                context.ContextId, context.LineItemId);
                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.ResultContainerPage = new ResultContainerPage
                    {
                        ExternalContextId = LtiConstants.ResultContainerContextId,
                        Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId),
                        ResultContainer = new ResultContainer
                        {
                            MembershipSubject = new ResultMembershipSubject
                            {
                                Results = InMemoryDb.Result == null ? new LisResult[] {} : new[] { InMemoryDb.Result }
                            }
                        }
                    };
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };

            OnPostResult = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                                                                context.ContextId, context.LineItemId);
                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.BadRequest;
                }
                else
                {
                    InMemoryDb.Result    = context.Result;
                    InMemoryDb.Result.Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                                                                       context.ContextId, context.LineItemId, InMemoryDb.ResultId);
                    context.Result     = InMemoryDb.Result;
                    context.StatusCode = HttpStatusCode.Created;
                }
                return(Task.FromResult <object>(null));
            };

            OnPutResult = context =>
            {
                // https://www.imsglobal.org/specs/ltiomv2p0/specification-3
                // When a line item is created, a result for each user is deemed to be created with a status value of “Initialized”.
                // Thus, there is no need to actually create a result with a POST request; the first connection to a result may be a
                // PUT or a GET request.

                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                                                                context.ContextId, context.LineItemId);
                if (InMemoryDb.LineItem == null || !InMemoryDb.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    // If this is the first connection, the PUT is equivalent to a POST
                    InMemoryDb.Result = context.Result;
                    if (context.Result.Id == null)
                    {
                        InMemoryDb.Result.Id =
                            RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                                                        context.ContextId, context.LineItemId, InMemoryDb.ResultId);
                    }
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };
        }