Ejemplo n.º 1
0
        public JsonResult SaveActiveEvaluation(EvaluationsCurrentActive evaluationsCurrentActive)
        {
            try
            {
                var token = _tokenValidator.Validate(HttpContext);
                if (!token.Success)
                {
                    return(Json(new ReturnData <string>
                    {
                        Success = false,
                        NotAuthenticated = true,
                        Message = $"Unauthorized:-{token.Message}",
                    }));
                }
                var checkActive = _context
                                  .EvaluationsCurrentActive
                                  .Any(
                    a => (a.EndDate.Date >= evaluationsCurrentActive.StartDate.Date &&
                          a.StartDate.Date <= evaluationsCurrentActive.StartDate.Date ||
                          a.EndDate.Date >= evaluationsCurrentActive.EndDate.Date &&
                          a.StartDate.Date <= evaluationsCurrentActive.EndDate.Date) &&
                    a.EvaluationsCurrentId == evaluationsCurrentActive.EvaluationsCurrentId
                    );
                if (checkActive)
                {
                    return(Json(new ReturnData <string>
                    {
                        Success = false,
                        Message = "There is an Evaluation between " + evaluationsCurrentActive.StartDate.Date +
                                  " and " + evaluationsCurrentActive.EndDate.Date
                    }));
                }

                /*make this distinct*/
                var targetGroups = _context.EvaluationTargetGroups

                                   .Where(g => g.EvaluationsCurrentId == evaluationsCurrentActive.EvaluationsCurrentId)
                                   .GroupBy(g => g.EvaluationsCurrentId)
                                   .Select(s => s.First())
                                   .ToList();
                var targetGroupsClass = new List <EvaluationTargetGroup>();
                targetGroups.ForEach(p =>
                {
                    targetGroupsClass.Add(new EvaluationTargetGroup
                    {
                        Names = p.Names,
                        EvaluationsCurrentId = p.EvaluationsCurrentId,
                        ReferenceId          = p.ReferenceId,
                        TargetGroupId        = p.TargetGroupId,
                        TargetType           = p.TargetType,
                    });
                });

                if (targetGroups.Count > 0)
                {
                    evaluationsCurrentActive.EvaluationTargetGroups = targetGroupsClass;
                }


                _context.EvaluationsCurrentActive.Add(evaluationsCurrentActive);

                _context.SaveChanges();
                return(Json(new ReturnData <string>
                {
                    Success = true,
                    Message = "Active Evaluation has been saved successfully"
                }));
            }

            catch (Exception ex)
            {
                return(Json(new ReturnData <string>
                {
                    Success = false,
                    Message = "An error occurred,please retry : " + ex.Message
                }));
            }
        }
Ejemplo n.º 2
0
        public JsonResult SaveCurrentEvaluation(CurrentEvaluationViewModel currentEvaluationViewModel)
        {
            try
            {
                var token = _tokenValidator.Validate(HttpContext);
                if (!token.Success)
                {
                    return(Json(new ReturnData <string>
                    {
                        Success = false,
                        NotAuthenticated = true,
                        Message = $"Unauthorized:-{token.Message}",
                    }));
                }

                if (string.IsNullOrEmpty(currentEvaluationViewModel.CurrentEvaluationName))
                {
                    return(Json(new ReturnData <string>
                    {
                        Success = false,
                        Message = "Evaluation Name name can not be empty"
                    }));
                }

                var currentEvaluation = new EvaluationsCurrent
                {
                    CurrentEvaluationName = currentEvaluationViewModel.CurrentEvaluationName,
                    EvaluationId          = currentEvaluationViewModel.EvaluationId,
                    EvaluationTarget      = Convert.ToInt32(currentEvaluationViewModel.Level),
                    //SemesterId = currentEvaluationViewModel.SemesterId,
                    YearId    = currentEvaluationViewModel.YearId,
                    Status    = currentEvaluationViewModel.Status,
                    StartDate = currentEvaluationViewModel.StartDate,
                    EndDate   = currentEvaluationViewModel.EndDate,
                };

                if (ModelState.IsValid)
                {
                    _context.EvaluationsCurrents.Add(currentEvaluation);
                }

                if (_context.SaveChanges() < 1)
                {
                    return(Json(new ReturnData <string>
                    {
                        Success = false,
                        Message = "Current evaluation not found"
                    }));
                }

                var evaluationCurrentActive = new EvaluationsCurrentActive
                {
                    EvaluationsCurrentId = currentEvaluation.Id,
                    EvaluationsId        = currentEvaluation.EvaluationId,
                    Status = currentEvaluation.Status,
                    IsElearnigEvaluation = currentEvaluationViewModel.IsElearnigEvaluation,
                    StartDate            = currentEvaluation.StartDate,
                    EvaluationTarget     = currentEvaluation.EvaluationTarget,
                    EndDate     = currentEvaluation.EndDate,
                    IsMandatory = currentEvaluationViewModel.IsMandatory,
                };

                var yearOfStudy = "All Years Of Study";
                yearOfStudy = string.IsNullOrEmpty(currentEvaluationViewModel.YearOfStudy) ? yearOfStudy : currentEvaluationViewModel.YearOfStudy;

                if (currentEvaluationViewModel.Levels.Count > 0)
                {
                    var evaluationGroups = new List <EvaluationTargetGroup>();
                    currentEvaluationViewModel.Levels.ForEach(
                        l =>
                    {
                        evaluationGroups.Add(
                            new EvaluationTargetGroup
                        {
                            EvaluationsCurrentId = currentEvaluation.Id,
                            TargetType           = Convert.ToInt32(l.Level),
                            ReferenceId          = l.Id,
                            Names         = l.Names,
                            TargetGroupId = Convert.ToInt32(l.Level) == 5 ? 0 : Convert.ToInt32(l.Id),
                            Year          = yearOfStudy
                        }
                            );
                    });

                    evaluationCurrentActive.EvaluationTargetGroups = evaluationGroups;
                }
                else
                {
                    var listEvaluationGroups = new List <EvaluationTargetGroup>();
                    var evaluationGroups     = new EvaluationTargetGroup();
                    if (!string.IsNullOrEmpty(currentEvaluationViewModel.Semester))
                    {
                        evaluationGroups = new EvaluationTargetGroup
                        {
                            EvaluationsCurrentId = currentEvaluation.Id,
                            TargetType           = 1,
                            ReferenceId          = currentEvaluation.SemesterId + "",
                            Names         = currentEvaluationViewModel.Semester,
                            Year          = yearOfStudy,
                            TargetGroupId = 0
                        };
                    }
                    else
                    {
                        evaluationGroups = new EvaluationTargetGroup
                        {
                            EvaluationsCurrentId = currentEvaluation.Id,
                            TargetType           = 0,
                            ReferenceId          = "Academic Years Target",
                            Names         = yearOfStudy,
                            TargetGroupId = 0,
                            Year          = yearOfStudy
                        };
                    }
                    listEvaluationGroups.Add(evaluationGroups);
                    evaluationCurrentActive.EvaluationTargetGroups = listEvaluationGroups;
                }

                _context.EvaluationsCurrentActive.Add(evaluationCurrentActive);
                _context.SaveChanges();
                return(Json(new ReturnData <string>
                {
                    Success = true,
                    Message = "Current Evaluation has been saved successfully"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new ReturnData <string>
                {
                    Success = false,
                    Message = "An error occurred,please retry : " + ex.Message
                }));
            }
        }