public static Apprenticeship ToEntity(this AddEditApprenticeshipViewModel model, ProviderPortalEntities db)
        {
            var userContext = UserContext.GetUserContext();
            var userId      = Permission.GetCurrentUserId();

            if (!userContext.IsProvider())
            {
                return(null);
            }
            Apprenticeship apprenticeship = model.ApprenticeshipId != 0
                ? db.Apprenticeships.FirstOrDefault(
                x =>
                x.ApprenticeshipId == model.ApprenticeshipId &&
                x.ProviderId == userContext.ItemId.Value)
                : null;

            apprenticeship = apprenticeship ?? new Apprenticeship
            {
                CreatedByUserId    = userId,
                CreatedDateTimeUtc = DateTime.UtcNow,
                ProviderId         = userContext.ItemId.Value,
                RecordStatusId     = (int)Constants.RecordStatus.Pending
            };
            apprenticeship.ModifiedByUserId     = userId;
            apprenticeship.ModifiedDateTimeUtc  = DateTime.UtcNow;
            apprenticeship.AddedByApplicationId = (int)Constants.Application.Portal;
            apprenticeship.MarketingInformation = Markdown.Sanitize(model.MarketingInformation);
            apprenticeship.Url = UrlHelper.GetFullUrl(model.Url);
            apprenticeship.ContactTelephone = model.ContactTelephone;
            apprenticeship.ContactEmail     = model.ContactEmail;
            apprenticeship.ContactWebsite   = UrlHelper.GetFullUrl(model.ContactWebsite);

            Apprenticeship frameworkOrStandard = ApprenticeshipExtensions.DecodeSearchFrameworkOrStandard(model.FrameworkOrStandardId) ?? ApprenticeshipExtensions.DecodeSearchFrameworkOrStandardByName(model.FrameworkOrStandard);

            if (frameworkOrStandard != null)
            {
                apprenticeship.StandardCode  = frameworkOrStandard.StandardCode;
                apprenticeship.Version       = frameworkOrStandard.Version;
                apprenticeship.FrameworkCode = frameworkOrStandard.FrameworkCode;
                apprenticeship.ProgType      = frameworkOrStandard.ProgType;
                apprenticeship.PathwayCode   = frameworkOrStandard.PathwayCode;
            }
            return(apprenticeship);
        }
        public static AddEditApprenticeshipViewModel Populate(this AddEditApprenticeshipViewModel model, int id,
                                                              ProviderPortalEntities db)
        {
            var apprenticeship = new Apprenticeship();

            if (id > 0)
            {
                var userContext = UserContext.GetUserContext();
                if (userContext.IsProvider())
                {
                    apprenticeship = db.Apprenticeships.FirstOrDefault(
                        x =>
                        x.ApprenticeshipId == id &&
                        x.ProviderId == userContext.ItemId.Value);
                }
            }
            model = new AddEditApprenticeshipViewModel
            {
                ApprenticeshipId      = apprenticeship.ApprenticeshipId,
                RecordStatusId        = apprenticeship.RecordStatusId,
                FrameworkOrStandardId = ApprenticeshipExtensions.GetFrameworkOrStandardId(apprenticeship),
                FrameworkOrStandard   = apprenticeship.ApprenticeshipDetails(),
                MarketingInformation  = apprenticeship.MarketingInformation,
                Url                       = UrlHelper.GetFullUrl(apprenticeship.Url),
                ContactEmail              = apprenticeship.ContactEmail,
                ContactTelephone          = apprenticeship.ContactTelephone,
                ContactWebsite            = UrlHelper.GetFullUrl(apprenticeship.ContactWebsite),
                DisplayNotPublishedBanner = !apprenticeship.Provider.ApprenticeshipContract,
                HasBeenQAdForCompliance   = apprenticeship.ApprenticeshipQACompliances.Any(),
                LastQAdForComplianceBy    = apprenticeship.ApprenticeshipQACompliances.Count == 0 ? null : apprenticeship.ApprenticeshipQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().AspNetUser.Name,
                LastQAdForComplianceOn    = apprenticeship.ApprenticeshipQACompliances.Count == 0 ? (DateTime?)null : apprenticeship.ApprenticeshipQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().CreatedDateTimeUtc,
                HasPassedComplianceChecks = apprenticeship.ApprenticeshipQACompliances.Count != 0 && apprenticeship.ApprenticeshipQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().Passed,
                HasBeenQAdForStyle        = apprenticeship.ApprenticeshipQAStyles.Any(),
                LastQAdForStyleBy         = apprenticeship.ApprenticeshipQAStyles.Count == 0 ? null : apprenticeship.ApprenticeshipQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().AspNetUser.Name,
                LastQAdForStyleOn         = apprenticeship.ApprenticeshipQAStyles.Count == 0 ? (DateTime?)null : apprenticeship.ApprenticeshipQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().CreatedDateTimeUtc,
                HasPassedStyleChecks      = apprenticeship.ApprenticeshipQAStyles.Count != 0 && apprenticeship.ApprenticeshipQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().Passed
            };
            var deliveryLocations = new DeliveryLocationListViewModel();

            model.DeliveryLocations = deliveryLocations.Populate(id, db);

            return(model);
        }
 public static bool Validate(this AddEditApprenticeshipViewModel model, ProviderPortalEntities db, ModelStateDictionary modelState)
 {
     if (modelState["FrameworkOrStandard"].Errors.Count == 0)
     {
         Apprenticeship frameworkOrStandard = ApprenticeshipExtensions.DecodeSearchFrameworkOrStandard(model.FrameworkOrStandardId) ?? ApprenticeshipExtensions.DecodeSearchFrameworkOrStandardByName(model.FrameworkOrStandard);
         if (frameworkOrStandard == null)
         {
             modelState.AddModelError("FrameworkOrStandard", AppGlobal.Language.GetText("Apprenticeship_Edit_FrameworkOrStandardRequired", "The Framework / Standard Name field is required."));
         }
         else
         {
             Int32          providerId = UserContext.GetUserContext().ItemId ?? 0;
             Apprenticeship app        = db.Apprenticeships.FirstOrDefault(x => x.ProviderId == providerId && x.StandardCode == frameworkOrStandard.StandardCode && x.Version == frameworkOrStandard.Version && x.FrameworkCode == frameworkOrStandard.FrameworkCode && x.ProgType == frameworkOrStandard.ProgType && x.PathwayCode == frameworkOrStandard.PathwayCode && (model.ApprenticeshipId == 0 || x.ApprenticeshipId != model.ApprenticeshipId));
             if (app != null)
             {
                 modelState.AddModelError("FrameworkOrStandard", AppGlobal.Language.GetText("Apprenticeship_Create_FrameworkOrStandardMustBeUnique", "The Framework / Standard Name supplied is already in use."));
             }
         }
     }
     return(modelState.IsValid);
 }
        public static ApprenticeshipListViewModel Populate(this ApprenticeshipListViewModel model,
                                                           ProviderPortalEntities db)
        {
            var userContext = UserContext.GetUserContext();

            if (model.Search == null)
            {
                model.Search = new ApprenticeshipSearchViewModel();
            }
            model.Search.Populate(db);

            if (!userContext.IsProvider())
            {
                return(model);
            }

            Provider provider = db.Providers.Find(userContext.ItemId);

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

            model.DisplayNotPublishedBanner = !provider.ApprenticeshipContract;

            var apprenticeships = db.Apprenticeships
                                  .Where(x => x.ProviderId == userContext.ItemId.Value);

            if (model.Search.DeliveryModeId != null)
            {
                apprenticeships =
                    apprenticeships.Where(
                        x => x.ApprenticeshipLocations.Any(
                            y => y.DeliveryModes.Any(
                                z => z.DeliveryModeId == model.Search.DeliveryModeId)));
            }
            if (model.Search.LocationId != null)
            {
                apprenticeships =
                    apprenticeships.Where(
                        x => x.ApprenticeshipLocations.Any(y => y.LocationId == model.Search.LocationId));
            }
            if (!String.IsNullOrWhiteSpace(model.Search.FrameworkOrStandardId) &&
                !String.IsNullOrWhiteSpace(model.Search.FrameworkOrStandard))
            {
                var decodedValues = ApprenticeshipExtensions.DecodeSearchFrameworkOrStandard(model.Search.FrameworkOrStandardId);
                if (decodedValues.FrameworkCode != null)
                {
                    apprenticeships =
                        apprenticeships.Where(x =>
                                              x.FrameworkCode == decodedValues.FrameworkCode &&
                                              x.ProgType == decodedValues.ProgType &&
                                              x.PathwayCode == decodedValues.PathwayCode);
                }
                if (decodedValues.StandardCode != null)
                {
                    apprenticeships =
                        apprenticeships.Where(x =>
                                              x.StandardCode == decodedValues.StandardCode &&
                                              x.Version == decodedValues.Version);
                }
            }

            if (Permission.HasPermission(false, false, Permission.PermissionName.CanQAApprenticeships))
            {
                model.ApprenticeshipsQAed = provider.GetQualityAssuredApprenticeshipCount();
                model.NumberOfApprenticeshipsRequiredToQA = provider.GetNumberOfApprenticeshipsRequiredToQA();
                model.DataReadyForQA = provider.DataReadyToQA;
            }

            model.Apprenticeships = apprenticeships.ToList()
                                    .Select(x => new ApprenticeshipListViewModelItem
            {
                // [FrameworkCode], [ProgType], [PathwayCode]
                // [StandardCode], [Version]
                ApprenticeshipId      = x.ApprenticeshipId,
                ApprenticeshipDetails = x.ApprenticeshipDetails(),
                LastUpdate            = x.ModifiedDateTimeUtc ?? x.CreatedDateTimeUtc,
                Status             = x.RecordStatu.RecordStatusName,
                ComplianceQAResult = !x.ApprenticeshipQACompliances.Any() ? "" : x.ApprenticeshipQACompliances.OrderByDescending(o => o.CreatedDateTimeUtc).First().Passed ? AppGlobal.Language.GetText("Apprenticeship_QAResult_Passed", "Passed") : AppGlobal.Language.GetText("Apprenticeship_QAResult_Failed", "Failed"),
                StyleQAResult      = !x.ApprenticeshipQAStyles.Any() ? "" : x.ApprenticeshipQAStyles.OrderByDescending(o => o.CreatedDateTimeUtc).First().Passed ? AppGlobal.Language.GetText("Apprenticeship_QAResult_Passed", "Passed") : AppGlobal.Language.GetText("Apprenticeship_QAResult_Failed", "Failed")
            }).OrderByDescending(x => x.LastUpdate).ToList();

            return(model);
        }