/// <summary>
        /// Возвращает фонд оценочных средств дисциплины.
        /// Если fondOcenochnihSredstvId равно null,
        /// создаёт и возвращает новый объект
        /// </summary>
        /// <param name="eduPlanId"></param>
        /// <param name="disciplineId"></param>
        /// <param name="fondOcenochnihSredstvId"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <FondOcenochnihSredstv> GetFondOcenochnihSredstvByUserNameAsync(int eduPlanId, int disciplineId, int?fondOcenochnihSredstvId, string userName)
        {
            var discipline = await GetDisciplineByUserNameAsync(eduPlanId, disciplineId, userName);

            if (discipline == null)
            {
                return(null);
            }

            FondOcenochnihSredstv fondOcenochnihSredstv = new FondOcenochnihSredstv();

            fondOcenochnihSredstv.Discipline   = discipline;
            fondOcenochnihSredstv.DisciplineId = discipline.DisciplineId;
            if (fondOcenochnihSredstvId == null || fondOcenochnihSredstvId == 0)
            {
                return(fondOcenochnihSredstv);
            }

            fondOcenochnihSredstv = discipline.FondOcenochnihSredstvList.FirstOrDefault(a => a.FondOcenochnihSredstvId == fondOcenochnihSredstvId);
            if (fondOcenochnihSredstv == null)
            {
                return(null);
            }

            return(fondOcenochnihSredstv);
        }
        /// <summary>
        /// Добавляет к фонду оценочных средств загруженный файл
        /// </summary>
        /// <param name="fondOcenochnihSredstv"></param>
        /// <param name="uploadedFile"></param>
        /// <returns></returns>
        public async Task <FondOcenochnihSredstv> UpdateFondOcenochnihSredstvAsync(FondOcenochnihSredstv fondOcenochnihSredstv, IFormFile uploadedFile)
        {
            if (fondOcenochnihSredstv == null || uploadedFile == null)
            {
                return(null);
            }
            fondOcenochnihSredstv = await _eduPlanRepository.UpdateFondOcenochnihSredstvAsync(fondOcenochnihSredstv, uploadedFile);

            return(fondOcenochnihSredstv);
        }
        /// <summary>
        /// Удаление аннотации учебной дисциплины
        /// </summary>
        /// <param name="EduPlanId"></param>
        /// <param name="DisciplineId"></param>
        /// <param name="FondOcenochnihSredstvId"></param>
        /// <returns></returns>
        public async Task <IActionResult> FondOcenochnihSredstvListPereutverjdeniyaRemove(int EduPlanId, int DisciplineId, int FondOcenochnihSredstvId)
        {
            FondOcenochnihSredstv fondOcenochnihSredstv = await _metodKomissiyaRepository.GetFondOcenochnihSredstvByUserNameAsync(EduPlanId, DisciplineId, FondOcenochnihSredstvId, User.Identity.Name);

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

            return(View(fondOcenochnihSredstv));
        }
        /// <summary>
        /// Удаляет лист переутверждения фонда оценочных средств дисциплины, если она доступна пользователю
        /// </summary>
        /// <param name="eduPlanId"></param>
        /// <param name="disciplineId"></param>
        /// <param name="fondOcenochnihSredstvId"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task RemoveFondOcenochnihSredstvListPereutverjdeniyaByUserNameAsync(int eduPlanId, int disciplineId, int fondOcenochnihSredstvId, string userName)
        {
            FondOcenochnihSredstv fondOcenochnihSredstv = await GetFondOcenochnihSredstvByUserNameAsync(eduPlanId, disciplineId, fondOcenochnihSredstvId, userName);

            if (fondOcenochnihSredstv == null)
            {
                return;
            }

            await _eduPlanRepository.RemoveFondOcenochnihSredstvListPereutverjdeniyaAsync(fondOcenochnihSredstv);
        }
        public async Task <IActionResult> FondOcenochnihSredstvListPereutverjdeniyaCreateOrEdit(int EduPlanId,
                                                                                                int DisciplineId, int?FondOcenochnihSredstvId, IFormFile uploadedFile)
        {
            FondOcenochnihSredstv fondOcenochnihSredstv = await _metodKomissiyaRepository
                                                          .GetFondOcenochnihSredstvByUserNameAsync(EduPlanId, DisciplineId, FondOcenochnihSredstvId, User.Identity.Name);

            if (fondOcenochnihSredstv != null)
            {
                await _metodKomissiyaRepository.UpdateFondOcenochnihSredstvListPereutverjdeniyaAsync(fondOcenochnihSredstv, uploadedFile);
            }

            return(RedirectToAction(nameof(EduPlanPreview), new { id = EduPlanId }));
        }
        /// <summary>
        /// Добавление / редактирование фонда оценочных средств учебной дисциплины
        /// </summary>
        /// <param name="EduPlanId"></param>
        /// <param name="DisciplineId"></param>
        /// <param name="FondOcenochnihSredstvId"></param>
        /// <returns></returns>
        public async Task <IActionResult> FondOcenochnihSredstvCreateOrEdit(int EduPlanId,
                                                                            int DisciplineId, int?FondOcenochnihSredstvId)
        {
            FondOcenochnihSredstv fondOcenochnihSredstv = await _metodKomissiyaRepository.
                                                          GetFondOcenochnihSredstvByUserNameAsync(EduPlanId, DisciplineId, FondOcenochnihSredstvId, User.Identity.Name);

            if (fondOcenochnihSredstv == null)
            {
                return(NotFound());
            }
            ViewBag.EduPlanId = EduPlanId;
            return(View(fondOcenochnihSredstv));
        }
        public async Task <IActionResult> Edit(int id, [Bind("FondOcenochnihSredstvId,DisciplineId,FileModelId")] FondOcenochnihSredstv fondOcenochnihSredstv,
                                               IFormFile uploadedFile,
                                               int?EduPlanId)
        {
            if (id != fondOcenochnihSredstv.FondOcenochnihSredstvId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                if (uploadedFile != null)
                {
                    FileModel fileModel = await KisVuzDotNetCore2.Models.Files.Files.LoadFile(_context, _appEnvironment, uploadedFile, "Фонд оценочных средств", FileDataTypeEnum.FondOcenochnihSredstv);

                    await _context.SaveChangesAsync();

                    int?fileToRemoveId = fondOcenochnihSredstv.FileModelId;
                    fondOcenochnihSredstv.FileModelId = fileModel.Id;
                    await _context.SaveChangesAsync();

                    KisVuzDotNetCore2.Models.Files.Files.RemoveFile(_context, _appEnvironment, fileToRemoveId);
                }

                try
                {
                    _context.Update(fondOcenochnihSredstv);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FondOcenochnihSredstvExists(fondOcenochnihSredstv.FondOcenochnihSredstvId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index), new { EduPlanId }));
            }
            ViewData["DisciplineId"] = new SelectList(_context.Disciplines, "DisciplineId", "DisciplineId", fondOcenochnihSredstv.DisciplineId);
            ViewData["FileModelId"]  = new SelectList(_context.Files, "Id", "Id", fondOcenochnihSredstv.FileModelId);
            return(View(fondOcenochnihSredstv));
        }
        public async Task <IActionResult> Create([Bind("FondOcenochnihSredstvId,DisciplineId,FileModelId")] FondOcenochnihSredstv fondOcenochnihSredstv, IFormFile uploadedFile, int?EduPlanId)
        {
            if (ModelState.IsValid && uploadedFile != null)
            {
                FileModel fileModel = await KisVuzDotNetCore2.Models.Files.Files.LoadFile(_context, _appEnvironment, uploadedFile, "Фонд оценочных средств", FileDataTypeEnum.FondOcenochnihSredstv);

                fondOcenochnihSredstv.FileModelId = fileModel.Id;
                _context.Add(fondOcenochnihSredstv);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index), new { EduPlanId }));
            }

            var eduPlans = _context.EduPlans
                           .Where(p => p.EduPlanId == EduPlanId)
                           .Include(p => p.BlokDiscipl)
                           .ThenInclude(bd => bd.BlokDisciplChast)
                           .ThenInclude(bdc => bdc.Disciplines)
                           .ThenInclude(d => d.DisciplineName);
            List <Discipline> disciplinesByEduPlan = new List <Discipline>();

            foreach (var plan in eduPlans)
            {
                foreach (var blokDiscipl in plan.BlokDiscipl)
                {
                    foreach (var blokDisciplChast in blokDiscipl.BlokDisciplChast)
                    {
                        foreach (var discipline in blokDisciplChast.Disciplines)
                        {
                            disciplinesByEduPlan.Add(discipline);
                        }
                    }
                }
            }

            ViewData["DisciplineId"] = new SelectList(disciplinesByEduPlan, "DisciplineId", "DisciplineName.DisciplineNameName");
            ViewBag.EduPlanId        = EduPlanId;

            return(View(fondOcenochnihSredstv));
        }