Example #1
0
        private async Task <IActionResult> HandleReplaceResultRequest(imsx_RequestHeaderInfoType requestHeader, imsx_POXBodyType requestBody)
        {
            var replaceRequest  = requestBody.Item as replaceResultRequest ?? new replaceResultRequest();
            var replaceResponse = new replaceResultResponse();

            var result = GetResult(replaceRequest.resultRecord);

            try
            {
                var request  = new ReplaceResultRequest(result);
                var response = await OnReplaceResultAsync(request);

                if (response.StatusCode == StatusCodes.Status200OK)
                {
                    return(CreateSuccessResponse(replaceResponse, requestHeader.imsx_messageIdentifier,
                                                 $"Score for {replaceRequest.resultRecord.sourcedGUID.sourcedId} is now {replaceRequest.resultRecord.result.resultScore.textString}"));
                }
                return(StatusCode(response.StatusCode));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
        private async Task <IActionResult> HandleReplaceResultRequest(imsx_RequestHeaderInfoType requestHeader, imsx_POXBodyType requestBody)
        {
            var replaceRequest  = requestBody.Item as replaceResultRequest ?? new replaceResultRequest();
            var replaceResponse = new replaceResultResponse();

            var result = GetResult(replaceRequest.resultRecord);

            // The LTI 1.1 specification states in 6.1.1: https://www.imsglobal.org/specs/ltiv1p1/implementation-guide#toc-26
            // The TC must check the incoming grade for validity and must fail when a grade is outside the range 0.0-1.0 or if the grade is not a valid number.
            // The TC must respond to these replaceResult operations with a imsx_codeMajor of "failure".
            if (!result.Score.HasValue || !(result.Score >= 0 && result.Score <= 1))
            {
                return(CreateFailureResponse(replaceResponse, requestHeader.imsx_messageIdentifier, StatusCodes.Status400BadRequest,
                                             "The result score must be a decimal value in the range 0.0 - 1.0"));
            }

            try
            {
                var request  = new ReplaceResultRequest(result);
                var response = await OnReplaceResultAsync(request);

                if (response.StatusCode != StatusCodes.Status200OK)
                {
                    return(CreateFailureResponse(replaceResponse, requestHeader.imsx_messageIdentifier,
                                                 response.StatusCode, response.StatusDescription));
                }
                return(CreateSuccessResponse(replaceResponse, requestHeader.imsx_messageIdentifier,
                                             response.StatusDescription
                                             ?? $"Score for {replaceRequest.resultRecord.sourcedGUID.sourcedId} is now {replaceRequest.resultRecord.result.resultScore.textString}"));
            }
            catch (Exception ex)
            {
                return(CreateFailureResponse(replaceResponse, requestHeader.imsx_messageIdentifier,
                                             StatusCodes.Status500InternalServerError, ex.Message));
            }
        }