예제 #1
0
        public static SaveMap Publish(SaveMap wgSaveMap, IEnumerable <int> svrWgIds, int loggedInPersonId,
                                      EFContextProvider <EcatContext> ctxProvider)
        {
            var infos = wgSaveMap.Single(map => map.Key == tWg).Value;

            var pubWgs = GetPublishingWgData(svrWgIds, ctxProvider);

            if (pubWgs == null)
            {
                var errorMessage = "The database did not return a publishable workgroup.";
                var error        = infos.Select(
                    info => new EFEntityError(info, "Publication Error", errorMessage, "MpSpStatus"));
                throw new EntityErrorsException(error);
            }

            if (pubWgs.Count() < svrWgIds.Count())
            {
                var missingGrps = pubWgs.Where(wg => {
                    if (svrWgIds.Contains(wg.Id))
                    {
                        return(false);
                    }
                    return(true);
                });

                var errorMessage = "Some groups were found to be unpublishable";
                var error        = infos.Select(
                    info => new EFEntityError(info, "Publication Error", errorMessage, "MpSpStatus"));
                throw new EntityErrorsException(error);
            }

            foreach (var wg in pubWgs)
            {
                var stratScoreInterval = 1m / wg.PubWgMembers.Count();
                stratScoreInterval = decimal.Round(stratScoreInterval, 4);
                var stratKeeper   = new List <PubWgMember>();
                var countOfGrp    = wg.PubWgMembers.Count();
                var totalStratPos = 0;
                for (var i = 1; i <= countOfGrp; i++)
                {
                    totalStratPos += i;
                }

                foreach (var me in wg.PubWgMembers)
                {
                    var stratSum = me.PubStratResponses.Sum(strat => strat.StratPosition);
                    stratSum += me.SelfStratPosition;

                    if (me.PeersDidNotAssessMe.Any() || me.PeersIdidNotAssess.Any() || me.PeersDidNotStratMe.Any() ||
                        me.PeersIdidNotStrat.Any() || me.FacStratPosition == 0 || stratSum != totalStratPos)
                    {
                        var errorMessage =
                            $"There was a problem validating necessary information . Problem Flags Are: [Them => Me] NA: !{me.PeersDidNotAssessMe.Count()}, NS: {me.PeersDidNotStratMe.Any()} | [Me => Them] NA: {me.PeersIdidNotAssess.Count()}, NS: {me.PeersIdidNotStrat.Any()} | FacStrat: {me.FacStratPosition}";

                        var error = infos.Select(
                            info => new EFEntityError(info, "Publication Error", errorMessage, "MpSpStatus"));
                        throw new EntityErrorsException(error);
                    }
                    var peerCount   = countOfGrp - 1;
                    var resultScore = ((decimal)me.SpResponseTotalScore / (me.CountSpResponses * 6)) * 100;
                    var spResult    = new SpResult
                    {
                        CourseId             = wg.CourseId,
                        WorkGroupId          = wg.Id,
                        StudentId            = me.StudentId,
                        AssignedInstrumentId = wg.InstrumentId,
                        CompositeScore       = (int)resultScore,
                        BreakOut             = new SpResultBreakOut
                        {
                            NotDisplay = me.BreakOut.NotDisplayed,
                            IneffA     = me.BreakOut.IneffA,
                            IneffU     = me.BreakOut.IneffU,
                            EffA       = me.BreakOut.EffA,
                            EffU       = me.BreakOut.EffU,
                            HighEffA   = me.BreakOut.HighEffA,
                            HighEffU   = me.BreakOut.HighEffU
                        },
                        MpAssessResult = ConvertScoreToOutcome((int)resultScore),
                    };

                    var resultInfo = ctxProvider.CreateEntityInfo(spResult,
                                                                  me.HasSpResult ? EntityState.Modified : EntityState.Added);

                    resultInfo.ForceUpdate = me.HasSpResult;

                    if (!wgSaveMap.ContainsKey(tSpResult))
                    {
                        wgSaveMap[tSpResult] = new List <EntityInfo> {
                            resultInfo
                        };
                    }
                    else
                    {
                        wgSaveMap[tSpResult].Add(resultInfo);
                    }

                    var stratResult = new StratResult
                    {
                        CourseId       = wg.CourseId,
                        StudentId      = me.StudentId,
                        WorkGroupId    = wg.Id,
                        ModifiedById   = loggedInPersonId,
                        ModifiedDate   = DateTime.Now,
                        StratCummScore = decimal.Round(me.StratTable.Select(strat =>
                        {
                            var multipler = 1 - (strat.Position - 1) * stratScoreInterval;
                            return(multipler * strat.Count);
                        }).Sum(), 4)
                    };

                    me.StratResult = stratResult;
                    stratKeeper.Add(me);
                }

                var cummScores = new List <decimal>();
                var oi         = 1;

                foreach (var strat in stratKeeper.OrderByDescending(sk => sk.StratResult.StratCummScore))
                {
                    if (cummScores.Contains(strat.StratResult.StratCummScore) || !cummScores.Any())
                    {
                        strat.StratResult.OriginalStratPosition = oi;
                        cummScores.Add(strat.StratResult.StratCummScore);
                        continue;
                    }
                    ;
                    cummScores.Add(strat.StratResult.StratCummScore);

                    oi += 1;
                    strat.StratResult.OriginalStratPosition = oi;
                }

                var fi          = 0;
                var spInterval  = wg.WgSpTopStrat / wg.StratDivisor;
                var facInterval = wg.WgFacTopStrat / wg.StratDivisor;


                foreach (
                    var gm in
                    stratKeeper.OrderByDescending(sk => sk.StratResult.StratCummScore)
                    .ThenBy(sk => sk.FacStratPosition))
                {
                    var studAwardedPoints = Math.Max(0, wg.WgSpTopStrat - spInterval * fi);
                    var instrAwardPoints  = Math.Max(0, wg.WgFacTopStrat - (facInterval * (gm.FacStratPosition - 1)));

                    //var totalAward = studAwardedPoints + instrAwardPoints;

                    gm.StratResult.StudStratAwardedScore = studAwardedPoints;
                    gm.StratResult.FacStratAwardedScore  = instrAwardPoints;
                    gm.StratResult.FinalStratPosition    = fi + 1;

                    var info = ctxProvider.CreateEntityInfo(gm.StratResult,
                                                            gm.HasStratResult ? EntityState.Modified : EntityState.Added);
                    info.ForceUpdate = gm.HasStratResult;

                    if (!wgSaveMap.ContainsKey(tStratResult))
                    {
                        wgSaveMap[tStratResult] = new List <EntityInfo> {
                            info
                        };
                    }
                    else
                    {
                        wgSaveMap[tStratResult].Add(info);
                    }

                    fi += 1;
                }
            }
            return(wgSaveMap);
        }