예제 #1
0
        public ResultsController()
        {
            OnDeleteResult = context =>
            {
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (resultUri == null || LineItemsController.Result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    LineItemsController.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 (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else if (LineItemsController.Result == null || !LineItemsController.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     = LineItemsController.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 (LineItemsController.LineItem == null || !LineItemsController.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 = LineItemsController.Result == null ? new LisResult[] {} : new[] { LineItemsController.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 (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.BadRequest;
                }
                else
                {
                    LineItemsController.Result    = context.Result;
                    LineItemsController.Result.Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                                                                                context.ContextId, context.LineItemId, LineItemsController.ResultId);
                    context.Result     = LineItemsController.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 (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    // If this is the first connection, the PUT is equivalent to a POST
                    LineItemsController.Result = context.Result;
                    if (context.Result.Id == null)
                    {
                        LineItemsController.Result.Id =
                            RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                                                        context.ContextId, context.LineItemId, LineItemsController.ResultId);
                    }
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };
        }
예제 #2
0
        public LineItemsController()
        {
            OnDeleteLineItem = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), ContextId, context.Id);

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

            OnGetLineItemWithResults = context =>
            {
                OnGetLineItem(context);
                if (context.LineItem != null && Result != null)
                {
                    context.LineItem.Result = Result == null ? new LisResult[] {} : new[] { 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 (LineItem != null &&
                    (string.IsNullOrEmpty(context.ActivityId) ||
                     context.ActivityId.Equals(LineItem.AssignedActivity.ActivityId)))
                {
                    context.LineItemContainerPage.LineItemContainer.MembershipSubject.LineItems = new[] { LineItem };
                }
                context.StatusCode = HttpStatusCode.OK;
                return(Task.FromResult <object>(null));
            };

            // Create a LineItem
            OnPostLineItem = context =>
            {
                if (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, LineItemId);
                context.LineItem.Results = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, LineItemId);
                LineItem           = context.LineItem;
                context.StatusCode = HttpStatusCode.Created;
                return(Task.FromResult <object>(null));
            };

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

            // Update LineItem and Result
            OnPutLineItemWithResults = context =>
            {
                if (context.LineItem == null || LineItem == null || !LineItem.Id.Equals(context.LineItem.Id))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    LineItem = context.LineItem;
                    if (context.LineItem.Result != null && context.LineItem.Result.Length > 0)
                    {
                        Result = context.LineItem.Result[0];
                    }
                    context.StatusCode = HttpStatusCode.OK;
                }
                return(Task.FromResult <object>(null));
            };
        }