示例#1
0
        private async Task <ViewModel> CreateViewModel(Course course, CourseRun courseRun)
        {
            Venue venue = default;

            if (courseRun.VenueId.HasValue)
            {
                venue = await _sqlQueryDispatcher.ExecuteQuery(new GetVenue()
                {
                    VenueId = courseRun.VenueId.Value
                });
            }

            return(new ViewModel()
            {
                CourseId = course.CourseId,
                CourseRunId = courseRun.CourseRunId,
                CourseName = courseRun.CourseName,
                DeliveryMode = courseRun.DeliveryMode,
                FlexibleStartDate = courseRun.FlexibleStartDate,
                StartDate = courseRun.StartDate,
                VenueName = venue?.VenueName,
                YourReference = courseRun.ProviderCourseId
            });
        }
示例#2
0
        public static async Task <IReadOnlyCollection <Course> > MapCourses(SqlMapper.GridReader reader)
        {
            var courses = await reader.ReadAsync <CourseResult>();

            var courseRuns = (await reader.ReadAsync <CourseRunResult>())
                             .GroupBy(r => r.CourseId)
                             .ToDictionary(g => g.Key, g => g.AsEnumerable());

            var courseRunSubRegions = (await reader.ReadAsync <CourseRunSubRegionResult>())
                                      .GroupBy(r => r.CourseRunId)
                                      .ToDictionary(g => g.Key, g => g.Select(r => r.RegionId).AsEnumerable());

            // N.B. We need to normalize HTML-encoded data here. The legacy projects HTML-encoded everything before
            // persisting it in Cosmos. We want to move the HTML encoding to the edge, where it should be done.
            // The existing data synced from Cosmos has a DataIsHtmlEncoded column set to true; read that here and
            // decode the relevant fields if it's set.

            return(courses.Select(MapCourse).ToArray());

            Course MapCourse(CourseResult row)
            {
                return(new Course()
                {
                    CourseId = row.CourseId,
                    CreatedOn = row.CreatedOn,
                    UpdatedOn = row.UpdatedOn,
                    ProviderId = row.ProviderId,
                    ProviderUkprn = row.ProviderUkprn,
                    LearnAimRef = row.LearnAimRef,
                    CourseDescription = DecodeIfNecessary(row.CourseDescription),
                    EntryRequirements = DecodeIfNecessary(row.EntryRequirements),
                    WhatYoullLearn = DecodeIfNecessary(row.WhatYoullLearn),
                    HowYoullLearn = DecodeIfNecessary(row.HowYoullLearn),
                    WhatYoullNeed = DecodeIfNecessary(row.WhatYoullNeed),
                    HowYoullBeAssessed = DecodeIfNecessary(row.HowYoullBeAssessed),
                    WhereNext = DecodeIfNecessary(row.WhereNext),
                    CourseRuns = courseRuns
                                 .GetValueOrDefault(row.CourseId, Enumerable.Empty <CourseRunResult>())
                                 .Select(MapCourseRun)
                                 .ToArray(),
                    LearnAimRefTypeDesc = row.LearnAimRefTypeDesc,
                    AwardOrgCode = row.AwardOrgCode,
                    NotionalNVQLevelv2 = row.NotionalNVQLevelv2,
                    LearnAimRefTitle = row.LearnAimRefTitle
                });

                string DecodeIfNecessary(string field) => row.DataIsHtmlEncoded != false?HtmlDecode(field) : field;
            }

            CourseRun MapCourseRun(CourseRunResult row)
            {
                var courseRun = new CourseRun()
                {
                    CourseRunId       = row.CourseRunId,
                    CourseRunStatus   = row.CourseRunStatus,
                    CreatedOn         = row.CreatedOn,
                    UpdatedOn         = row.UpdatedOn,
                    CourseName        = DecodeIfNecessary(row.CourseName),
                    VenueId           = row.VenueId,
                    ProviderCourseId  = DecodeIfNecessary(row.ProviderCourseId),
                    DeliveryMode      = row.DeliveryMode,
                    FlexibleStartDate = row.FlexibleStartDate,
                    StartDate         = row.StartDate,
                    CourseWebsite     = row.CourseWebsite,
                    Cost              = row.Cost,
                    CostDescription   = DecodeIfNecessary(row.CostDescription),
                    DurationUnit      = row.DurationUnit,
                    DurationValue     = row.DurationValue,
                    StudyMode         = row.StudyMode != 0 ? row.StudyMode : null,                 // Normalize 0 to null
                    AttendancePattern = row.AttendancePattern != 0 ? row.AttendancePattern : null, // Normalize 0 to null
                    National          = row.National,
                    SubRegionIds      = courseRunSubRegions.GetValueOrDefault(row.CourseRunId, Enumerable.Empty <string>()).ToArray(),
                    VenueName         = row.VenueName,
                    ProviderVenueRef  = row.ProviderVenueRef
                };

                // We have some bad data where fields are populated when they shouldn't be for the delivery mode. Fix it up here

                if (courseRun.DeliveryMode != CourseDeliveryMode.ClassroomBased)
                {
                    courseRun.VenueId           = null;
                    courseRun.VenueName         = null;
                    courseRun.ProviderVenueRef  = null;
                    courseRun.StudyMode         = null;
                    courseRun.AttendancePattern = null;
                }

                if (courseRun.DeliveryMode != CourseDeliveryMode.WorkBased)
                {
                    courseRun.National     = null;
                    courseRun.SubRegionIds = Array.Empty <string>();
                }

                return(courseRun);

                string DecodeIfNecessary(string field) => row.DataIsHtmlEncoded != false?HtmlDecode(field) : field;
            }
        }
        public List <Course> MappingBulkUploadCourseToCourse(List <BulkUploadCourse> bulkUploadCourses, string userId, out List <string> errors)
        {
            errors = new List <string>();
            var validationMessages = new List <string>();

            var courses         = new List <Course>();
            var listsCourseRuns = new List <BulkUploadCourseRun>();

            foreach (var bulkUploadCourse in bulkUploadCourses)
            {
                if (bulkUploadCourse.IsCourseHeader)
                {
                    var course = new Course();
                    course.id = Guid.NewGuid();
                    course.QualificationCourseTitle = bulkUploadCourse.QualificationCourseTitle;
                    course.LearnAimRef          = bulkUploadCourse.LearnAimRef;
                    course.NotionalNVQLevelv2   = bulkUploadCourse.NotionalNVQLevelv2;
                    course.AwardOrgCode         = bulkUploadCourse.AwardOrgCode;
                    course.QualificationType    = bulkUploadCourse.QualificationType;
                    course.ProviderUKPRN        = bulkUploadCourse.ProviderUKPRN;
                    course.CourseDescription    = bulkUploadCourse.CourseDescription;
                    course.EntryRequirements    = bulkUploadCourse.EntryRequirements;
                    course.WhatYoullLearn       = bulkUploadCourse.WhatYoullLearn;
                    course.HowYoullLearn        = bulkUploadCourse.HowYoullLearn;
                    course.WhatYoullNeed        = bulkUploadCourse.WhatYoullNeed;
                    course.HowYoullBeAssessed   = bulkUploadCourse.HowYoullBeAssessed;
                    course.WhereNext            = bulkUploadCourse.WhereNext;
                    course.AdvancedLearnerLoan  = bulkUploadCourse.AdvancedLearnerLoan.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false;
                    course.AdultEducationBudget = bulkUploadCourse.AdultEducationBudget.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false;
                    course.BulkUploadErrors     = ParseBulkUploadErrors(bulkUploadCourse.BulkUploadLineNumber, _courseService.ValidateCourse(course));
                    course.IsValid = course.BulkUploadErrors.Any() ? false : true;

                    course.CreatedBy   = userId;
                    course.CreatedDate = DateTime.Now;

                    course.UpdatedBy = bulkUploadCourse.TempCourseId.ToString();

                    courses.Add(course);
                }

                var courseRun = new CourseRun();
                courseRun.id = Guid.NewGuid();

                courseRun.DeliveryMode = GetValueFromDescription <DeliveryMode>(bulkUploadCourse.DeliveryMode);
                if (courseRun.DeliveryMode.Equals(DeliveryMode.Undefined))
                {
                    validationMessages.Add($"DeliveryMode is Undefined, because you have entered ( { bulkUploadCourse.DeliveryMode } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }

                // Call VenueService and for VenueName get VenueId (GUID) (Applicable only for type ClassroomBased)

                if (string.IsNullOrEmpty(bulkUploadCourse.VenueName))
                {
                    validationMessages.Add($"NO Venue Name for Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }
                else
                {
                    //GetVenuesByPRNAndNameCriteria venueCriteria = new GetVenuesByPRNAndNameCriteria(bulkUploadCourse.ProviderUKPRN.ToString(), bulkUploadCourse.VenueName);
                    var venueResultCache = cachedVenues.Where(o => o.VenueName.ToLower() == bulkUploadCourse.VenueName.ToLower() && o.Status == VenueStatus.Live).ToList();

                    if (null != venueResultCache && venueResultCache.Count > 0)
                    {
                        //var venues = (IEnumerable<Venue>)venueResultCeche.Value.Value;
                        if (venueResultCache.Count().Equals(1))
                        {
                            if (venueResultCache.FirstOrDefault().Status.Equals(VenueStatus.Live))
                            {
                                courseRun.VenueId = new Guid(venueResultCache.FirstOrDefault().ID);
                            }
                            else
                            {
                                validationMessages.Add($"Venue is not LIVE (The status is { venueResultCache.FirstOrDefault().Status }) for VenueName { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                            }
                        }
                        else
                        {
                            validationMessages.Add($"We have obtained muliple Venues for { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                            if (venueResultCache.FirstOrDefault().Status.Equals(VenueStatus.Live))
                            {
                                courseRun.VenueId = new Guid(venueResultCache.FirstOrDefault().ID);
                            }
                            else
                            {
                                validationMessages.Add($"The selected Venue is not LIVE (The status is { venueResultCache.FirstOrDefault().Status }) for VenueName { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                            }
                        }
                    }
                    else
                    {
                        validationMessages.Add($"We could NOT obtain a Venue for { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                    }
                }


                courseRun.CourseName       = bulkUploadCourse.CourseName;
                courseRun.ProviderCourseID = bulkUploadCourse.ProviderCourseID;

                courseRun.FlexibleStartDate = bulkUploadCourse.FlexibleStartDate.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false;

                DateTime specifiedStartDate;
                if (DateTime.TryParseExact(bulkUploadCourse.StartDate, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out specifiedStartDate))
                {
                    courseRun.StartDate = specifiedStartDate;
                }
                else if (DateTime.TryParse(bulkUploadCourse.StartDate, out specifiedStartDate))
                {
                    //Remove time
                    specifiedStartDate  = specifiedStartDate.Date;
                    courseRun.StartDate = specifiedStartDate;
                }
                else
                {
                    courseRun.StartDate = null;
                    validationMessages.Add($"StartDate is NULL, because you have entered ( { bulkUploadCourse.StartDate } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }. We are expecting the date in 'dd/MM/yyyy' format.");
                }

                courseRun.CourseURL = bulkUploadCourse.CourseURL;

                decimal specifiedCost;
                if (decimal.TryParse(bulkUploadCourse.Cost, out specifiedCost))
                {
                    courseRun.Cost = specifiedCost;
                }
                else
                {
                    courseRun.Cost = null;
                    validationMessages.Add($"Cost is NULL, because you have entered ( { bulkUploadCourse.Cost } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }

                courseRun.CostDescription = ReplaceSpecialCharacters(bulkUploadCourse.CostDescription);
                courseRun.DurationUnit    = GetValueFromDescription <DurationUnit>(bulkUploadCourse.DurationUnit);
                if (courseRun.DurationUnit.Equals(DurationUnit.Undefined))
                {
                    validationMessages.Add($"DurationUnit is Undefined, because you have entered ( { bulkUploadCourse.DurationUnit } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }

                int specifiedDurationValue;
                if (int.TryParse(bulkUploadCourse.DurationValue, out specifiedDurationValue))
                {
                    courseRun.DurationValue = specifiedDurationValue;
                }
                else
                {
                    courseRun.DurationValue = null;
                    validationMessages.Add($"DurationValue is NULL, because you have entered ( { bulkUploadCourse.DurationValue } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }

                courseRun.StudyMode = GetValueFromDescription <StudyMode>(bulkUploadCourse.StudyMode);
                if (courseRun.StudyMode.Equals(StudyMode.Undefined))
                {
                    validationMessages.Add($"StudyMode is Undefined, because you have entered ( { bulkUploadCourse.StudyMode } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }
                courseRun.AttendancePattern = GetValueFromDescription <AttendancePattern>(bulkUploadCourse.AttendancePattern);
                if (courseRun.AttendancePattern.Equals(AttendancePattern.Undefined))
                {
                    validationMessages.Add($"AttendancePattern is Undefined, because you have entered ( { bulkUploadCourse.AttendancePattern } ), Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                }

                switch (bulkUploadCourse.National.ToUpperInvariant())
                {
                case "YES":
                {
                    courseRun.National = true;
                    var availableRegions = new SelectRegionModel();
                    courseRun.Regions = availableRegions.RegionItems.Select(x => x.Id).ToList();
                    break;
                }

                case "NO":
                {
                    courseRun.National = false;
                    var regionResult = ParseRegionData(bulkUploadCourse.Regions, bulkUploadCourse.SubRegions);
                    if (regionResult.IsSuccess)
                    {
                        courseRun.Regions = regionResult.Value;
                    }
                    else if (!regionResult.IsSuccess)
                    {
                        validationMessages.Add($"Unable to get regions/subregions, Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                    }
                    break;
                }

                default:
                {
                    courseRun.National = null;
                    validationMessages.Add($"Choose if you can deliver this course anywhere in England, Line { bulkUploadCourse.BulkUploadLineNumber },  LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }");
                    break;
                }
                }


                courseRun.BulkUploadErrors = ParseBulkUploadErrors(bulkUploadCourse.BulkUploadLineNumber, _courseService.ValidateCourseRun(courseRun, ValidationMode.BulkUploadCourse));
                courseRun.RecordStatus     = courseRun.BulkUploadErrors.Any() ? RecordStatus.BulkUploadPending : RecordStatus.BulkUploadReadyToGoLive;

                courseRun.CreatedBy   = userId;
                courseRun.CreatedDate = DateTime.Now;

                listsCourseRuns.Add(new BulkUploadCourseRun {
                    LearnAimRef = bulkUploadCourse.LearnAimRef, TempCourseId = bulkUploadCourse.TempCourseId, CourseRun = courseRun
                });
            }

            foreach (var course in courses)
            {
                int currentTempCourseId;
                if (int.TryParse(course.UpdatedBy, out currentTempCourseId))
                {
                    course.CourseRuns = listsCourseRuns.Where(cr => cr.LearnAimRef == course.LearnAimRef && cr.TempCourseId == currentTempCourseId).Select(cr => cr.CourseRun).ToList();
                }
                else
                {
                    validationMessages.Add($"Problem with parsing TempCourseId -  ( { course.UpdatedBy } ), LARS_QAN = { course.LearnAimRef }, CourseFor = { course.CourseDescription }");
                }

                course.UpdatedBy = null;
            }

            //// Uncomment only for DEV and TESTING
            //int courseNumber = 1;
            //foreach (var course in courses)
            //{
            //    string jsonBulkUploadCoursesFilesPath = "D:\\FindACourse-BulkUploadJSONfiles";
            //    var courseJson = JsonConvert.SerializeObject(course);
            //    string jsonFileName = string.Format("{0}-{1}-{2}-{3}-{4}.json", DateTime.Now.ToString("yyMMdd-HHmmss"), course.ProviderUKPRN, course.LearnAimRef, courseNumber, course.CourseRuns.Count().ToString());
            //    File.WriteAllText(string.Format(@"{0}\{1}", jsonBulkUploadCoursesFilesPath, jsonFileName), courseJson);
            //    courseNumber++;
            //}

            // Push the courses to the CourseService
            foreach (var course in courses)
            {
                var courseResult = Task.Run(async() => await _courseService.AddCourseAsync(course)).Result;

                if (courseResult.IsSuccess)
                {
                    // Do nothing. Eventually we could have a count on successfully uploaded courses
                }
                else
                {
                    errors.Add($"The course is NOT BulkUploaded, LARS_QAN = { course.LearnAimRef }. Error -  { courseResult.Error }");
                }
            }

            return(courses);
        }
        public async Task Handle_WithValidCourseIdAndCourseRunId_ReturnsOkWithResult()
        {
            var venue = new Venue
            {
                Id        = Guid.NewGuid(),
                VenueName = "TestVenueName"
            };

            var lars = new Core.Search.Models.Lars
            {
                LearnAimRef      = "00112233",
                LearnAimRefTitle = "TestLearnAimRefTitle"
            };

            var courseRun = new CourseRun
            {
                Id           = Guid.NewGuid(),
                RecordStatus = CourseStatus.Live,
                VenueId      = venue.Id
            };

            var alternativeCourseRun = new CourseRun
            {
                Id           = Guid.NewGuid(),
                RecordStatus = CourseStatus.Live,
                VenueId      = venue.Id
            };

            var course = new Course
            {
                Id            = Guid.NewGuid(),
                ProviderUKPRN = 12345678,
                LearnAimRef   = lars.LearnAimRef,
                CourseRuns    = new[]
                {
                    courseRun,
                    alternativeCourseRun
                }
            };

            var provider = new Provider
            {
                Id = Guid.NewGuid(),
                UnitedKingdomProviderReferenceNumber = "12345678",
                ProviderContact = new[]
                {
                    new ProviderContact
                    {
                        ContactEmail = "*****@*****.**",
                        ContactType  = "P"
                    }
                }
            };

            var sqlProvider = new Core.DataStore.Sql.Models.Provider
            {
                ProviderId        = provider.Id,
                ProviderName      = "TestProviderAlias",
                DisplayNameSource = ProviderDisplayNameSource.ProviderName
            };

            var feChoice = new FeChoice
            {
                UKPRN = course.ProviderUKPRN,
                EmployerSatisfaction = 1.2M,
                LearnerSatisfaction  = 3.4M
            };

            CosmosDbQueryDispatcher.Setup(s => s.ExecuteQuery(It.IsAny <GetCourseById>()))
            .ReturnsAsync(course);

            CosmosDbQueryDispatcher.Setup(s => s.ExecuteQuery(It.IsAny <GetProviderByUkprn>()))
            .ReturnsAsync(provider);

            LarsSearchClient.Setup(s => s.Search(It.IsAny <LarsLearnAimRefSearchQuery>()))
            .ReturnsAsync(new SearchResult <Core.Search.Models.Lars>
            {
                Items = new[] { new SearchResultItem <Core.Search.Models.Lars> {
                                    Record = lars
                                } }
            });

            CosmosDbQueryDispatcher.Setup(s => s.ExecuteQuery(It.IsAny <GetVenuesByProvider>()))
            .ReturnsAsync(new[] { venue });

            CosmosDbQueryDispatcher.Setup(s => s.ExecuteQuery(It.IsAny <GetFeChoiceForProvider>()))
            .ReturnsAsync(feChoice);

            SqlQueryDispatcher.Setup(s => s.ExecuteQuery(It.IsAny <Core.DataStore.Sql.Queries.GetProviderById>()))
            .ReturnsAsync(sqlProvider);

            var result = await HttpClient.GetAsync(CourseRunDetailUrl(course.Id, courseRun.Id));

            result.StatusCode.Should().Be(StatusCodes.Status200OK);

            var resultJson = JObject.Parse(await result.Content.ReadAsStringAsync());

            using (new AssertionScope())
            {
                resultJson.ToObject <object>().Should().NotBeNull();
                resultJson["provider"].ToObject <object>().Should().NotBeNull();
                resultJson["provider"]["ukprn"].ToObject <int>().Should().Be(provider.Ukprn);
                resultJson["provider"]["providerName"].ToObject <string>().Should().Be(sqlProvider.DisplayName);
                resultJson["provider"]["tradingName"].ToObject <string>().Should().Be(sqlProvider.DisplayName);
                resultJson["provider"]["email"].ToObject <string>().Should().Be(provider.ProviderContact.Single().ContactEmail);
                resultJson["provider"]["learnerSatisfaction"].ToObject <decimal>().Should().Be(feChoice.LearnerSatisfaction);
                resultJson["provider"]["employerSatisfaction"].ToObject <decimal>().Should().Be(feChoice.EmployerSatisfaction);
                resultJson["course"].ToObject <object>().Should().NotBeNull();
                resultJson["course"]["courseId"].ToObject <Guid>().Should().Be(course.Id);
                resultJson["venue"].ToObject <object>().Should().NotBeNull();
                resultJson["venue"]["venueName"].ToObject <string>().Should().Be(venue.VenueName);
                resultJson["qualification"].ToObject <object>().Should().NotBeNull();
                resultJson["qualification"]["learnAimRef"].ToObject <string>().Should().Be(lars.LearnAimRef);
                resultJson["qualification"]["learnAimRefTitle"].ToObject <string>().Should().Be(lars.LearnAimRefTitle);
                resultJson["alternativeCourseRuns"][0]["courseRunId"].ToObject <Guid>().Should().Be(alternativeCourseRun.Id);
                resultJson["courseRunId"].ToObject <Guid>().Should().Be(courseRun.Id);
            }
        }
示例#5
0
        public IActionResult Index(Guid?courseId, Guid?courseRunId)
        {
            Course    course    = null;
            CourseRun courseRun = null;

            if (courseId.HasValue)
            {
                course    = _courseService.GetCourseByIdAsync(new GetCourseByIdCriteria(courseId.Value)).Result.Value;
                courseRun = course.CourseRuns.Where(x => x.id == courseRunId.Value).FirstOrDefault();
            }

            CourseSummaryViewModel vm = new CourseSummaryViewModel

            {
                ProviderUKPRN            = course.ProviderUKPRN,
                CourseId                 = course.id,
                QualificationCourseTitle = course.QualificationCourseTitle,
                LearnAimRef              = course.LearnAimRef,
                NotionalNVQLevelv2       = course.NotionalNVQLevelv2,
                AwardOrgCode             = course.AwardOrgCode,
                CourseDescription        = course.CourseDescription,
                EntryRequirements        = course.EntryRequirements,
                WhatYoullLearn           = course.WhatYoullLearn,
                HowYoullLearn            = course.HowYoullLearn,
                WhatYoullNeed            = course.WhatYoullNeed,
                HowYoullBeAssessed       = course.HowYoullBeAssessed,
                WhereNext                = course.WhereNext,
                IsValid           = course.IsValid,
                QualificationType = course.QualificationType,

                //Course run deets
                CourseInstanceId = courseRunId,
                CourseName       = courseRun.CourseName,
                VenueId          = courseRun.VenueId,
                Cost             = courseRun.Cost,
                CostDescription  = courseRun.CostDescription,
                DurationUnit     = courseRun.DurationUnit,
                DurationValue    = courseRun.DurationValue,
                ProviderCourseID = courseRun.ProviderCourseID,
                DeliveryMode     = courseRun.DeliveryMode,
                National         = courseRun.DeliveryMode == DeliveryMode.WorkBased & !courseRun.National.HasValue ||
                                   courseRun.National.GetValueOrDefault(),
                FlexibleStartDate = courseRun.FlexibleStartDate,
                StartDate         = courseRun.StartDate,
                StudyMode         = courseRun.StudyMode,
                AttendancePattern = courseRun.AttendancePattern,
                CreatedBy         = courseRun.CreatedBy,
                CreatedDate       = courseRun.CreatedDate,
            };

            //Determine newer edited date
            if (course.UpdatedDate > courseRun.UpdatedDate)
            {
                vm.UpdatedDate = course.UpdatedDate;
                vm.UpdatedBy   = course.UpdatedBy;
            }
            else
            {
                vm.UpdatedDate = courseRun.UpdatedDate;
                vm.UpdatedBy   = courseRun.UpdatedBy;
            }

            if (vm.VenueId != null)
            {
                if (vm.VenueId != Guid.Empty)
                {
                    vm.VenueName = _venueService
                                   .GetVenueByIdAsync(new GetVenueByIdCriteria(courseRun.VenueId.Value.ToString())).Result.Value
                                   .VenueName;
                }
            }

            if (!string.IsNullOrEmpty(courseRun.CourseURL))
            {
                if (courseRun.CourseURL.Contains("http") || courseRun.CourseURL.Contains("https"))
                {
                    vm.CourseURL = courseRun.CourseURL;
                }
                else
                {
                    vm.CourseURL = "http://" + courseRun.CourseURL;
                }
            }

            if (courseRun.Regions != null)
            {
                var allRegions = _courseService.GetRegions().RegionItems;
                var regions    = GetRegions().RegionItems.Select(x => x.Id);
                vm.Regions = FormattedRegionsByIds(allRegions, courseRun.Regions);
            }
            return(View(vm));
        }
        public IList <KeyValuePair <string, string> > ValidateCourseRun(CourseRun courseRun, ValidationMode validationMode)
        {
            IList <KeyValuePair <string, string> > validationMessages = new List <KeyValuePair <string, string> >();

            //Filtered down validation rules for DQI based on story
            //To be made more generic when we bring additional rules in
            if (validationMode == ValidationMode.DataQualityIndicator)
            {
                if (courseRun.StartDate < DateTime.Today)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("START_DATE", $"courses need their start date updating"));
                }
                return(validationMessages);
            }

            // CourseName
            if (string.IsNullOrEmpty(courseRun.CourseName))
            {
                validationMessages.Add(new KeyValuePair <string, string>("COURSE_NAME", "Enter course name"));
            }
            else
            {
                if (!HasOnlyFollowingValidCharacters(courseRun.CourseName))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COURSE_NAME", "Course Name contains invalid character"));
                }
                if (courseRun.CourseName.Length > 255)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COURSE_NAME", $"Course Name must be 255 characters or less"));
                }
            }

            // ProviderCourseID
            if (!string.IsNullOrEmpty(courseRun.ProviderCourseID))
            {
                if (!HasOnlyFollowingValidCharacters(courseRun.ProviderCourseID))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("ID", "ID contains invalid characters"));
                }
                if (courseRun.ProviderCourseID.Length > 255)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("ID", $"The maximum length of 'ID' is 255 characters"));
                }
            }

            // DeliveryMode
            switch (courseRun.DeliveryMode)
            {
            case DeliveryMode.ClassroomBased:

                // VenueId
                if (courseRun.VenueId == null || courseRun.VenueId == Guid.Empty)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("VENUE", $"Select venue"));
                }

                // StudyMode
                if (courseRun.StudyMode.Equals(StudyMode.Undefined))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("STUDY_MODE", $"Select Study Mode"));
                }

                // AttendancePattern
                if (courseRun.AttendancePattern.Equals(AttendancePattern.Undefined))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("ATTENDANCE_PATTERN", $"Select Attendance Mode"));
                }

                break;

            case DeliveryMode.Online:
                // No Specific Fields
                break;

            case DeliveryMode.WorkBased:

                //National
                if (courseRun.National == null)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("NATIONAL_DELIVERY", $"Choose if you can deliver this course anywhere in England"));
                }
                else if (courseRun.National == false)
                {
                    // Regions
                    if (courseRun.Regions == null || courseRun.Regions.Count().Equals(0))
                    {
                        validationMessages.Add(new KeyValuePair <string, string>("REGION", $"Select at least one region or sub-region"));
                    }
                }
                break;

            case DeliveryMode.Undefined:     // Question ???
            default:
                validationMessages.Add(new KeyValuePair <string, string>("DELIVERY_MODE", $"Select Delivery Mode"));
                break;
            }

            // StartDate & FlexibleStartDate
            if (courseRun.StartDate != null)
            {
                courseRun.FlexibleStartDate = false; // COUR-746-StartDate

                var currentDate = DateTime.UtcNow.Date;

                switch (validationMode)
                {
                case ValidationMode.AddCourseRun:
                case ValidationMode.CopyCourseRun:
                case ValidationMode.EditCourseBU:
                case ValidationMode.BulkUploadCourse:

                    _logger.LogError("course date" + courseRun.StartDate.Value.Date + "utc Date " + currentDate);

                    int result = DateTime.Compare(courseRun.StartDate.Value.Date, currentDate);

                    if (result < 0)
                    {
                        _logger.LogWarning("*Simon* Date in the past");
                    }

                    if (courseRun.StartDate < currentDate)
                    {
                        validationMessages.Add(new KeyValuePair <string, string>("START_DATE", $"Start Date cannot be earlier than today's date"));
                    }
                    if (courseRun.StartDate > currentDate.AddYears(2))
                    {
                        validationMessages.Add(new KeyValuePair <string, string>("START_DATE", $"Start Date cannot be later than 2 years from today’s date"));
                    }
                    break;

                case ValidationMode.EditCourseYC:
                case ValidationMode.EditCourseMT:
                    // It cannot be done easily as we need both value - the newly entered and the previous. Call to saved version or modification in the model
                    break;

                case ValidationMode.MigrateCourse:
                    if (courseRun.StartDate > currentDate.AddYears(2))
                    {
                        validationMessages.Add(new KeyValuePair <string, string>("START_DATE", $"Start Date cannot be later than 2 years from today’s date"));
                    }
                    break;

                case ValidationMode.Undefined:
                default:
                    validationMessages.Add(new KeyValuePair <string, string>("START_DATE", $"Validation Mode was not defined."));
                    break;
                }
            }

            if (courseRun.StartDate == null && courseRun.FlexibleStartDate == false)
            {
                validationMessages.Add(new KeyValuePair <string, string>("START_DATE+FLEXIBLE_START_DATE", $"Either 'Defined Start Date' or 'Flexible Start Date' has to be provided"));
            }

            // CourseURL
            if (!string.IsNullOrEmpty(courseRun.CourseURL))
            {
                if (!IsValidUrl(courseRun.CourseURL))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("URL", "The format of URL is incorrect"));
                }
                if (courseRun.CourseURL.Length > 255)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("URL", $"The maximum length of URL is 255 characters"));
                }
            }

            // Cost & CostDescription
            if (string.IsNullOrEmpty(courseRun.CostDescription) && courseRun.Cost.Equals(null))
            {
                validationMessages.Add(new KeyValuePair <string, string>("COST", $"Enter cost or cost description"));
            }

            if (!string.IsNullOrEmpty(ReplaceSpecialCharacters(courseRun.CostDescription)))
            {
                if (!HasOnlyFollowingValidCharacters(ReplaceSpecialCharacters(courseRun.CostDescription)))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COST_DESCRIPTION", "Cost Description contains invalid characters"));
                }
                if (courseRun.CostDescription.Length > 255)
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COST_DESCRIPTION", $"Cost description must be 255 characters or less"));
                }
            }

            if (!courseRun.Cost.Equals(null))
            {
                if (!IsCorrectCostFormatting(courseRun.Cost.ToString()))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COST", $"Enter the cost in pounds and pence"));
                }
                if (courseRun.Cost > decimal.Parse("999999.99"))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("COST", $"Maximum allowed cost value is 999,999.99"));
                }
            }

            // DurationValue and DurationUnit
            if (courseRun.DurationValue.Equals(null) || courseRun.DurationUnit.Equals(DurationUnit.Undefined))
            {
                validationMessages.Add(new KeyValuePair <string, string>("DURATION", $"Enter duration"));
            }
            else
            {
                if (!ValidDurationValue(courseRun.DurationValue?.ToString()))
                {
                    validationMessages.Add(new KeyValuePair <string, string>("DURATION", "Duration must be numeric and maximum length is 3 digits"));
                }
            }

            return(validationMessages);
        }
        public static Course MapTribalCourseToCourse(TribalCourse tribalCourse, int numberOfMonthsAgo, bool dummyMode, out List <string> mappingMessages, out bool courseNOTtoBeMigrated)
        {
            var course     = new Course();
            var courseRuns = new List <CourseRun>();

            mappingMessages       = new List <string>();
            courseNOTtoBeMigrated = false;
            //var courseRunsToBeRemovedAsTooOld = new List<CourseRun>();

            foreach (var tribalCourseRun in tribalCourse.TribalCourseRuns)
            {
                var courseRun = new CourseRun();

                // JUST FOR TESTING - DO NOT UNCOMMENT
                //tribalCourseRun.AttendanceType = AttendanceType.DistanceWithAttendance;
                //tribalCourseRun.DurationUnit = TribalDurationUnit.Terms;
                //tribalCourseRun.StudyMode = TribalStudyMode.PartOfAFulltimeProgram;
                //tribalCourseRun.AttendancePattern = TribalAttendancePattern.Customised;

                courseRun.id = Guid.NewGuid();
                courseRun.CourseInstanceId = tribalCourseRun.CourseInstanceId;
                courseRun.VenueId          = tribalCourseRun.VenueGuidId;
                courseRun.CourseName       = tribalCourseRun.CourseName;
                courseRun.ProviderCourseID = tribalCourseRun.ProviderOwnCourseInstanceRef;

                // AttendanceType <=> DeliveryMode,
                switch (tribalCourseRun.AttendanceType)
                {
                case AttendanceType.Location:
                    courseRun.DeliveryMode = DeliveryMode.ClassroomBased;
                    break;

                case AttendanceType.WorkBased:
                case AttendanceType.FaceToFaceNonCampus:
                    if (null != courseRun.VenueId)
                    {
                        courseRun.DeliveryMode = DeliveryMode.ClassroomBased;
                    }
                    else
                    {
                        courseRun.DeliveryMode = DeliveryMode.WorkBased;
                    }
                    break;

                case AttendanceType.OnlineWithoutAttendance:
                case AttendanceType.OnlineWithAttendance:
                    courseRun.DeliveryMode = DeliveryMode.Online;
                    break;

                case AttendanceType.MixedMode:
                case AttendanceType.DistanceWithAttendance:
                case AttendanceType.DistanceWithoutAttendance:
                case AttendanceType.NotKnown:
                case AttendanceType.Undefined:
                default:
                    courseRun.DeliveryMode = DeliveryMode.Undefined;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING " +
                                        $"because your AttendanceType is set to { tribalCourseRun.AttendanceType } and we don't have it" + Environment.NewLine);
                    break;
                }

                // StartDate & FlexibleStartDate
                // Uncomment for testing only
                //tribalCourseRun.StartDate = DateTime.Now.AddMonths(-5);
                if (tribalCourseRun.StartDate != null && tribalCourseRun.StartDate > DateTime.MinValue)
                {
                    courseRun.StartDate         = tribalCourseRun.StartDate;
                    courseRun.FlexibleStartDate = false;

                    //if (tribalCourseRun.StartDate >= (DateTime.Now.AddMonths(-numberOfMonthsAgo)))
                    //{
                    //    courseRun.StartDate = tribalCourseRun.StartDate;
                    //    courseRun.FlexibleStartDate = false;
                    //}
                    //else
                    //{
                    //    //courseRunsIsTooOld.Add(true);
                    //    courseRunsToBeRemovedAsTooOld.Add(courseRun);
                    //    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' was REMOVED " +
                    //                        $"because the CourseRun StartDate ( { tribalCourseRun.StartDate.Value.ToShortDateString() } ) was more than 3 months ago and we didn't migrate it" + Environment.NewLine);
                    //}
                }
                else
                {
                    // latest decision Imran & Mark C.
                    courseRun.StartDate         = null;
                    courseRun.FlexibleStartDate = false;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' was set to Pending, because it didn't have StartDate " + Environment.NewLine);
                }

                courseRun.CourseURL       = tribalCourseRun.Url;
                courseRun.Cost            = tribalCourseRun.Price;
                courseRun.CostDescription = tribalCourseRun.PriceAsText;

                // DurationUnit & DurationValue
                switch (tribalCourseRun.DurationUnit)
                {
                case TribalDurationUnit.Hours:
                    courseRun.DurationValue = tribalCourseRun.DurationValue;
                    courseRun.DurationUnit  = DurationUnit.Hours;
                    break;

                case TribalDurationUnit.Days:
                    courseRun.DurationValue = tribalCourseRun.DurationValue;
                    courseRun.DurationUnit  = DurationUnit.Days;
                    break;

                case TribalDurationUnit.Weeks:
                    courseRun.DurationValue = tribalCourseRun.DurationValue;
                    courseRun.DurationUnit  = DurationUnit.Weeks;
                    break;

                case TribalDurationUnit.Months:
                    courseRun.DurationValue = tribalCourseRun.DurationValue;
                    courseRun.DurationUnit  = DurationUnit.Months;
                    break;

                case TribalDurationUnit.Terms:

                    if (tribalCourseRun.DurationValue == null)
                    {
                        courseRun.DurationValue = null;
                    }
                    else
                    {
                        courseRun.DurationValue = (tribalCourseRun.DurationValue ?? 0) * 3;
                    }
                    courseRun.DurationUnit = DurationUnit.Months;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' was set to DurationUnit = { tribalCourseRun.DurationUnit } " +
                                        $"and  DurationValue = { tribalCourseRun.DurationValue }. We needed to convert it to  DurationUnit = { courseRun.DurationUnit } and DurationValue = { courseRun.DurationValue }." + Environment.NewLine);
                    //courseRun.DurationValue = tribalCourseRun.DurationValue;
                    //courseRun.DurationUnit = DurationUnit.Undefined;
                    //// Alternativly 3 x Months or X x Weeks // TODO
                    //courseRun.RecordStatus = RecordStatus.MigrationPending;
                    //mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING " +
                    //                    $"because your DurationUnit is set to { tribalCourseRun.DurationUnit } and we don't have it" +
                    //                    $"We preserved the DurationValue, but you have to set appropriate DurationUnit and change the DurationValue accordingly" + Environment.NewLine);
                    break;

                case TribalDurationUnit.Semesters:
                    courseRun.DurationValue = null;      // New requirement - null
                    courseRun.DurationUnit  = DurationUnit.Undefined;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' was set to DurationUnit = Semesters " +
                                        $"and  DurationValue = { tribalCourseRun.DurationValue }. 'Semester' DurationUnit is not supported." + Environment.NewLine);
                    // $"and  DurationValue = { tribalCourseRun.DurationValue }. We preserved the DurationValue = { courseRun.DurationValue }, but you need to select available DurationUnit and change the DurationValue accordingly." + Environment.NewLine);
                    //$"and  DurationValue = { tribalCourseRun.DurationValue }. 'Semester' DurationUnit is not supported." + Environment.NewLine);
                    break;

                /*
                 *                  case TribalDurationUnit.Semesters:
                 *                      if (tribalCourseRun.DurationValue == null)
                 *                          courseRun.DurationValue = null;
                 *                      else
                 *                          courseRun.DurationValue = (tribalCourseRun.DurationValue ?? 0) * 3;
                 *                      courseRun.DurationUnit = DurationUnit.Months;
                 *                      mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' was set to DurationUnit = { tribalCourseRun.DurationUnit } " +
                 *                                          $"and  DurationValue = { tribalCourseRun.DurationValue }. We needed to convert it to  DurationUnit = { courseRun.DurationUnit } and DurationValue = { courseRun.DurationValue }." + Environment.NewLine);
                 *
                 *
                 *                      //courseRun.DurationValue = tribalCourseRun.DurationValue;
                 *                      //courseRun.DurationUnit = DurationUnit.Undefined;
                 *                      //// Alternativly 3 x Months or X x Weeks // TODO
                 *                      //courseRun.RecordStatus = RecordStatus.MigrationPending;
                 *                      //mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING " +
                 *                      //                    $"because your DurationUnit is set to { tribalCourseRun.DurationUnit } and we don't have it" +
                 *                      //                    $"We preserved the DurationValue, but you have to set appropriate DurationUnit and change the DurationValue accordingly" + Environment.NewLine);
                 *                      break;
                 */
                case TribalDurationUnit.Years:
                    courseRun.DurationValue = tribalCourseRun.DurationValue;
                    courseRun.DurationUnit  = DurationUnit.Years;
                    break;

                case TribalDurationUnit.Undefined:
                default:
                    courseRun.DurationUnit = DurationUnit.Undefined;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING " +
                                        $"because your DurationUnit is set to { tribalCourseRun.DurationUnit } and we don't have it" + Environment.NewLine);
                    break;
                }

                // StudyMode
                switch (tribalCourseRun.StudyMode)
                {
                case TribalStudyMode.FullTime:
                    courseRun.StudyMode = StudyMode.FullTime;
                    break;

                case TribalStudyMode.PartTime:
                    courseRun.StudyMode = StudyMode.PartTime;
                    break;

                case TribalStudyMode.Flexible:
                    courseRun.StudyMode = StudyMode.Flexible;     // Here it looks identical but value changes from 4 to 3
                    break;

                case TribalStudyMode.PartOfAFulltimeProgram:
                case TribalStudyMode.NotKnown:
                case TribalStudyMode.Undefined:
                default:
                    courseRun.StudyMode = StudyMode.Undefined;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING " +
                                        $"because your StudyMode is set to { tribalCourseRun.StudyMode } and we don't have it" + Environment.NewLine);
                    break;
                }
                // AttendancePattern
                switch (tribalCourseRun.AttendancePattern)
                {
                case TribalAttendancePattern.DaytimeWorkingHours:
                    courseRun.AttendancePattern = AttendancePattern.Daytime;
                    break;

                case TribalAttendancePattern.DayBlockRelease:
                    courseRun.AttendancePattern = AttendancePattern.DayOrBlockRelease;     // Here it looks identical but value changes from 2 to 4
                    break;

                case TribalAttendancePattern.Evening:
                case TribalAttendancePattern.Twilight:
                    courseRun.AttendancePattern = AttendancePattern.Evening;
                    break;

                case TribalAttendancePattern.Weekend:
                    courseRun.AttendancePattern = AttendancePattern.Weekend;     // Here it looks identical but value changes from 5 to 3
                    break;

                case TribalAttendancePattern.Customised:
                case TribalAttendancePattern.NotKnown:
                case TribalAttendancePattern.NotApplicable:
                case TribalAttendancePattern.Undefined:
                default:
                    courseRun.AttendancePattern = AttendancePattern.Undefined;
                    mappingMessages.Add($"ATTENTION - CourseRun { tribalCourseRun.CourseInstanceId } with Ref: '{ tribalCourseRun.ProviderOwnCourseInstanceRef }' is set to PENDING, " +
                                        $"because your AttendancePattern is set to { tribalCourseRun.AttendancePattern } and we don't have it" + Environment.NewLine);
                    break;
                }

                courseRun.CreatedDate = DateTime.Now;
                courseRun.CreatedBy   = "DFC – Course Migration Tool";

                courseRuns.Add(courseRun);
            }

            course.id       = Guid.NewGuid();
            course.CourseId = tribalCourse.CourseId;
            course.QualificationCourseTitle = tribalCourse.CourseTitle;
            course.LearnAimRef          = tribalCourse.LearningAimRefId;
            course.NotionalNVQLevelv2   = tribalCourse.QualificationLevelIdString;
            course.AwardOrgCode         = tribalCourse.LearningAimAwardOrgCode;
            course.QualificationType    = tribalCourse.Qualification;
            course.ProviderUKPRN        = tribalCourse.Ukprn;
            course.CourseDescription    = tribalCourse.CourseSummary;
            course.EntryRequirements    = tribalCourse.EntryRequirements;
            course.WhatYoullLearn       = tribalCourse.WhatYoullLearn;
            course.HowYoullLearn        = tribalCourse.HowYoullLearn;
            course.WhatYoullNeed        = tribalCourse.EquipmentRequired;
            course.HowYoullBeAssessed   = tribalCourse.AssessmentMethod;
            course.WhereNext            = tribalCourse.WhereNext;
            course.AdvancedLearnerLoan  = tribalCourse.AdvancedLearnerLoan;
            course.AdultEducationBudget = false; // WE don't have the data/or rule for it.
            course.CreatedDate          = DateTime.Now;
            course.CreatedBy            = "DFC – Course Migration Tool";
            // Removing CourseRuns, which are older than 3 (configurable) months
            //foreach(var courseRunToBeRemovedAsTooOld in courseRunsToBeRemovedAsTooOld)
            //{
            //    courseRuns.Remove(courseRunToBeRemovedAsTooOld);
            //}

            if (courseRuns != null && courseRuns.Count > 0)
            {
                course.CourseRuns = courseRuns;
            }
            else
            {
                // We don't migrate courses without CourseRuns
                courseNOTtoBeMigrated = true;
            }

            return(course);
        }
示例#8
0
        public async Task <IActionResult> Publish()
        {
            if (!_session.GetInt32("UKPRN").HasValue)
            {
                return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." }));
            }

            var model = _session.GetObject <CopyCourseRunSaveViewModel>(CopyCourseRunSaveViewModelSessionKey);

            if (model == null)
            {
                return(NotFound());
            }

            if (!model.CourseId.HasValue)
            {
                return(NotFound());
            }

            var course = await _courseService.GetCourseByIdAsync(new GetCourseByIdCriteria(model.CourseId.Value));

            if (!course.IsSuccess)
            {
                return(NotFound());
            }

            var courseRun = new CourseRun
            {
                id                = Guid.NewGuid(),
                DurationUnit      = model.DurationUnit,
                AttendancePattern = model.AttendanceMode,
                DeliveryMode      = model.DeliveryMode,
                StudyMode         = model.StudyMode,
                CostDescription   = _htmlEncoder.Encode(model.CostDescription ?? ""),
                CourseName        = _htmlEncoder.Encode(model.CourseName ?? ""),
                CourseURL         = model.Url,
                DurationValue     = Convert.ToInt32(model.DurationLength),
                ProviderCourseID  = _htmlEncoder.Encode(model.CourseProviderReference ?? ""),
                RecordStatus      = RecordStatus.Live,
                Cost              = !string.IsNullOrEmpty(model.Cost) ? Convert.ToDecimal(model.Cost) : (decimal?)null,
                FlexibleStartDate = model.StartDateType != StartDateType.SpecifiedStartDate,
                CreatedDate       = DateTime.Now,
                CreatedBy         = User.Claims.Where(c => c.Type == "email").Select(c => c.Value).SingleOrDefault(),
            };

            if (!courseRun.FlexibleStartDate)
            {
                courseRun.StartDate = DateTime.ParseExact(
                    $"{int.Parse(model.Day):00}-{int.Parse(model.Month):00}-{model.Year}",
                    "dd-MM-yyyy",
                    System.Globalization.CultureInfo.InvariantCulture);
            }

            switch (model.DeliveryMode)
            {
            case DeliveryMode.ClassroomBased:
                courseRun.AttendancePattern = model.AttendanceMode;
                courseRun.StudyMode         = model.StudyMode;

                courseRun.Regions = null;
                courseRun.VenueId = model.VenueId;
                break;

            case DeliveryMode.WorkBased:
                courseRun.VenueId = null;

                var availableRegions = new SelectRegionModel();

                if (model.National)
                {
                    courseRun.National = true;
                    courseRun.Regions  = availableRegions.RegionItems.Select(x => x.Id).ToList();
                }
                else
                {
                    courseRun.National = false;
                    courseRun.Regions  = model.SelectedRegions;
                    string[] selectedRegions = availableRegions.SubRegionsDataCleanse(courseRun.Regions.ToList());

                    var subRegions = selectedRegions.Select(selectedRegion => availableRegions.GetSubRegionItemByRegionCode(selectedRegion)).ToList();
                    courseRun.SubRegions = subRegions;

                    courseRun.AttendancePattern = AttendancePattern.Undefined;
                    courseRun.StudyMode         = StudyMode.Undefined;
                }
                break;

            case DeliveryMode.Online:

                courseRun.Regions           = null;
                courseRun.VenueId           = null;
                courseRun.AttendancePattern = AttendancePattern.Undefined;
                courseRun.StudyMode         = StudyMode.Undefined;
                break;
            }

            course.Value.CourseRuns = course.Value.CourseRuns.Append(courseRun);

            try
            {
                var result = await _courseService.UpdateCourseAsync(course.Value);

                if (!result.IsSuccess)
                {
                    throw new Exception($"{nameof(_courseService.UpdateCourseAsync)} failed during CopyCourseRun: {result.Error}");
                }

                _session.SetObject(CopyCourseRunPublishedCourseSessionKey, new PublishedCourseViewModel
                {
                    CourseId    = course.Value.id,
                    CourseRunId = courseRun.id,
                    CourseName  = courseRun.CourseName
                });

                return(RedirectToAction("Published"));
            }
            finally
            {
                _session.Remove("NewAddedVenue");
                _session.Remove("Option");
                _session.Remove(CopyCourseRunSaveViewModelSessionKey);
            }
        }
        public async Task <IActionResult> Index(Guid?courseId, Guid?courseRunId)
        {
            Course    course    = null;
            CourseRun courseRun = null;

            if (courseId.HasValue)
            {
                course = await _sqlQueryDispatcher.ExecuteQuery(new GetCourse()
                {
                    CourseId = courseId.Value
                });

                courseRun = course.CourseRuns.Where(x => x.CourseRunId == courseRunId.Value).FirstOrDefault();
            }

            CourseSummaryViewModel vm = new CourseSummaryViewModel

            {
                ProviderUKPRN            = course.ProviderUkprn,
                CourseId                 = course.CourseId,
                QualificationCourseTitle = course.LearnAimRefTitle,
                LearnAimRef              = course.LearnAimRef,
                NotionalNVQLevelv2       = course.NotionalNVQLevelv2,
                AwardOrgCode             = course.AwardOrgCode,
                CourseDescription        = course.CourseDescription,
                EntryRequirements        = course.EntryRequirements,
                WhatYoullLearn           = course.WhatYoullLearn,
                HowYoullLearn            = course.HowYoullLearn,
                WhatYoullNeed            = course.WhatYoullNeed,
                HowYoullBeAssessed       = course.HowYoullBeAssessed,
                WhereNext                = course.WhereNext,
                IsValid           = true,
                QualificationType = course.LearnAimRefTypeDesc,

                //Course run deets
                CourseInstanceId = courseRunId,
                CourseName       = courseRun.CourseName,
                VenueId          = courseRun.VenueId,
                Cost             = courseRun.Cost,
                CostDescription  = courseRun.CostDescription,
                DurationUnit     = courseRun.DurationUnit,
                DurationValue    = courseRun.DurationValue,
                ProviderCourseID = courseRun.ProviderCourseId,
                DeliveryMode     = courseRun.DeliveryMode,
                National         = courseRun.DeliveryMode == CourseDeliveryMode.WorkBased & !courseRun.National.HasValue ||
                                   courseRun.National.GetValueOrDefault(),
                FlexibleStartDate = courseRun.FlexibleStartDate,
                StartDate         = courseRun.StartDate,
                StudyMode         = courseRun.StudyMode,
                AttendancePattern = courseRun.AttendancePattern,
                CreatedDate       = courseRun.CreatedOn,
            };

            //Determine newer edited date
            if (course.UpdatedOn > courseRun.UpdatedOn)
            {
                vm.UpdatedDate = course.UpdatedOn;
            }
            else
            {
                vm.UpdatedDate = courseRun.UpdatedOn;
            }

            if (vm.VenueId != null)
            {
                if (vm.VenueId != Guid.Empty)
                {
                    var venue = await _sqlQueryDispatcher.ExecuteQuery(new GetVenue()
                    {
                        VenueId = courseRun.VenueId.Value
                    });

                    vm.VenueName = venue?.VenueName;
                }
            }

            if (!string.IsNullOrEmpty(courseRun.CourseWebsite))
            {
                if (courseRun.CourseWebsite.Contains("http") || courseRun.CourseWebsite.Contains("https"))
                {
                    vm.CourseURL = courseRun.CourseWebsite;
                }
                else
                {
                    vm.CourseURL = "http://" + courseRun.CourseWebsite;
                }
            }

            if (courseRun.SubRegionIds?.Count > 0)
            {
                var allRegions = _courseService.GetRegions().RegionItems;
                var regions    = GetRegions().RegionItems.Select(x => x.Id);
                vm.Regions = FormattedRegionsByIds(allRegions, courseRun.SubRegionIds);
            }
            return(View(vm));
        }