Example #1
0
        public async Task <IActionResult> Create(CourseAddViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var    teacher = _userManager.Users.FirstOrDefault(u => u.Id == viewModel.TeacherId);
                Course course  = _mapper.Map <Course>(viewModel);

                List <User> participants = new List <User>();
                if (!(teacher is null))
                {
                    participants.Add(teacher);
                }

                course.Users = participants;

                await _context.Courses.AddAsync(course);

                _context.SaveChanges();

                TempData["AlertMsg"] = "Course added";
                return(RedirectToAction(nameof(Details), new { course.Id }));
            }
            else
            {
                var teachers = await _userManager.GetUsersInRoleAsync("Teacher");

                viewModel.Teachers = teachers.Select(t => new Tuple <string, string>(t.Id, t.UserName)).ToList();
            }

            return(View(viewModel));
        }
Example #2
0
        public ActionResult Create(CreateDocumentViewModel vm)
        {
            if (ModelState.IsValid)
            {
                if (vm.file is null)
                {
                    ModelState.AddModelError("file", "File can't be empty!");
                    ViewData["parentUrl"] = $"/Activity/Details/{vm.Id}";
                    ViewData["Title"]     = "Add Activity Document";
                    return(View("_CreateDocumentPartial", vm));
                }
                var newDocument = new ActivityDocument()
                {
                    Description = vm.Description,
                    Name        = vm.file.FileName,
                    UploadTime  = DateTime.Now,
                    ActivityId  = vm.EnitityId,
                };
                if (newDocument.Description is null)
                {
                    newDocument.Description = "none";
                }
                newDocument.UserId = _userManager.GetUserId(User);

                using (var memoryStream = new MemoryStream())
                {
                    vm.file.CopyTo(memoryStream);
                    newDocument.DocumentData = memoryStream.ToArray();
                }

                _context.ActivityDocument.Add(newDocument);
                _context.SaveChanges();

                //Can't get it to accept nameof(Details) for some reason

                if (User.IsInRole("Teacher"))
                {
                    TempData["AlertMsg"] = "Document added";
                    return(RedirectToAction("Details", "Activity", new { id = vm.EnitityId }));
                }
                else
                {
                    TempData["AlertMsg"] = "Assignment uploaded - good luck!";
                    return(RedirectToAction("Index", "Student"));
                }
            }
            else
            {
                ViewData["parentUrl"] = $"/Activity/Details/{vm.Id}";
                ViewData["Title"]     = "Add Activity Document";
                return(View("_CreateDocumentPartial", vm));
            }
        }
Example #3
0
        public async Task <IActionResult> Delete(int id)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }

            var activity = await _context.Activities.FirstOrDefaultAsync(a => a.Id == id);

            if (!(activity is null))
            {
                _context.Remove(activity);
                _context.SaveChanges();

                var module = await _context.Modules.FirstOrDefaultAsync(a => a.Id == activity.ModuleId);

                if (!(module is null))
                {
                    TempData["AlertMsg"] = "Activity deleted";
                    return(RedirectToAction("Details", "module", new { id = module.Id }));
                }
            }

            return(NotFound());
        }
Example #4
0
        // GET: Module/Delete
        public async Task <IActionResult> Delete(int id)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }

            var module = await _context.Modules.FirstOrDefaultAsync(a => a.Id == id);

            if (!(module is null))
            {
                _context.Remove(module);
                _context.SaveChanges();

                var course = await _context.Courses.FirstOrDefaultAsync(a => a.Id == module.CourseId);

                if (!(course is null))
                {
                    TempData["AlertMsg"] = "Module deleted";
                    return(RedirectToAction("Details", "Course", new { id = course.Id }));
                }
            }

            return(NotFound());
        }
Example #5
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            var options = serviceProvider.GetRequiredService <DbContextOptions <LexiconLMSContext> >();

            using (var context = new LexiconLMSContext(options))
            {
                try
                {
                    if (context.ActivityType.Any())
                    {
                        return;
                    }

                    // Let's seed!
                    var ActivityTypes = new List <ActivityType>()
                    {
                        new ActivityType()
                        {
                            Type = "E-Learning"
                        },
                        new ActivityType()
                        {
                            Type = "Lectures"
                        },
                        new ActivityType()
                        {
                            Type = "Exercise"
                        },
                    };
                    //people.Add(person);

                    context.ActivityType.AddRange(ActivityTypes);
                    context.SaveChanges();
                }
                catch
                {
                    //Table ActivityType should exist, but if it doesn't, don't crash the application completely.
                }
            }
        }