/*GROUP ACTIONS*/

        /*
         * Returns List of users groups with modified UserState_ID  == user
         * users - list of usernames which will be modified
         * if exception then returns current list of groups
         * DOESN'T CHECK CURRENT USERS ROLES!!!
         */
        public async Task<IList<Group>> ChangeCourseUserGroupAsync(IEnumerable<String> users, ApplicationDbContext appContext, Course course, CourseUsers user, Boolean addNever = true)
        {
            List<Group> groups = new List<Group>();
            try
            {
                if (users != null && course != null)
                {
                    Int32 userType = (Int32)user;
                    Group currGroup = null;
                    String currUser = null;
                    for (Int32 i = 0; i < users.Count(); i++)
                    {
                        currUser = users.ElementAt(i);
                        currGroup = await appContext.Groups
                            .Where(g => String.Compare(g.User.UserName, currUser, true) == 0 && g.Course_ID == course.ID)
                            .FirstAsync();
                        if (!addNever || currGroup.UserState_ID != userType)
                        {
                            currGroup.UserState_ID = userType;
                            groups.Add(currGroup);
                        }
                    }
                }
                return groups;
            }
            catch (InvalidOperationException)
            {
                return groups;
            }
            catch (NullReferenceException)
            {
                return groups;
            }
        }
示例#2
0
 public ActionResult addCourse(Course model)
 {
     try
     {
         if (model.Name != "" && model.Description != "")
         {
             model.DateCreated = DateTime.Now.ToUniversalTime();
             model.Active = 1;
             db.Courses.Add(model);
             db.SaveChanges();
             return Json(new { Message = "success" });
         }
         else
         {
             throw new Exception("not all fields are filled");
         }
     }
     catch (Exception e)
     {
         return Json(new { Message = e.Message.ToString() });
     }
 }
 /*
  * Checks if this user has role in the group 
  */
 public Boolean CheckGroupUserForRole(ApplicationUser user, Course course, String groupName, params String[] roles)
 {
     CourseUsers uGroupRole;
     for (Int32 i = 0; i < roles.Length; i++)
     {
         if (Enum.TryParse(roles[i], out uGroupRole))
         {
             if (user.Groups.Any(g => g.Course_ID == course.ID && g.UserState_ID == (Int32)uGroupRole && (String.Compare(g.Name, groupName, false) == 0 || g.Name == null)))
                 return true;
         }
     }
     return false;
 }
 public async Task<IList<ApplicationUser>> GetUserWithDefRoleAndCurrCourse(ApplicationDbContext appContext, ApplicationUserManager userManager, Course course, String groupName, String userRole, params String[] groupRoles)
 {
     List<ApplicationUser> foundUsers = new List<ApplicationUser>();
     try
     {
         if (appContext != null && userManager != null)
         {
             IList<ApplicationUser> users = await appContext.Users.ToListAsync();
             /*get all teachers to List*/
             IList<String> userRoles = null;
             for (Int32 i = 0; i < users.Count; i++)
             {
                 userRoles = await userManager.GetRolesAsync(users[i].Id);
                 if (userRoles.Contains(userRole))
                     foundUsers.Add(users[i]);
             }
         }
         IEnumerable<ApplicationUser> usersWithRole = foundUsers.Where(u => CheckGroupUserForRole(u, course, groupName, groupRoles));
         if (usersWithRole != null)
             return usersWithRole.ToArray();
         return new List<ApplicationUser>();
     }
     catch (InvalidOperationException)
     {
         return new List<ApplicationUser>();
     }
     catch (NullReferenceException)
     {
         return new List<ApplicationUser>();
     }
 }
 /*
  * Gets ALL users of current group(user)
  * if exception then returns current list of groups
  */
 public async Task<IList<ApplicationUser>> GetAllUsersFromGroupsAsync(ApplicationDbContext appContext, Course course)
 {
     List<ApplicationUser> users = new List<ApplicationUser>();
     try
     {
         if (course != null)
         {
             users.AddRange(await appContext.Users.Where
                 (
                     u => u.Groups
                     .Any(gr => gr.Course_ID == course.ID)
                 )
                 .ToListAsync());
         }
         return users;
     }
     catch (NullReferenceException)
     {
         return users;
     }
 }
 /*
  * Gets all users of current group(user) type
  * if exception then returns current list of users
  */
 public async Task<IList<ApplicationUser>> GetUsersFromGroupsAsync(ApplicationDbContext appContext, Course course, CourseUsers userType, Boolean checkGroupName = false)
 {
     List<ApplicationUser> users = new List<ApplicationUser>();
     try
     {
         if (course != null)
         {
             Int32 userState = (Int32)userType;
             /*adds users with groups where names == null which means that user is not in any main group*/
             users.AddRange(await appContext.Users.Where
                 (
                     u => u.Groups
                     .Any(gr => gr.Course_ID == course.ID && gr.UserState_ID == userState && checkGroupName ? gr.Name == null : true)
                 )
                 .ToListAsync());
         }
         return users;
     }
     catch (NullReferenceException)
     {
         return users;
     }
 }
 /*
  * removes all  tags from current course
  */
 public static void RemoveTagsFromCourse(Course course)
 {
     if (course != null && course.CourseTags != null && course.CourseTags.Count != 0)
     {
         for (Int32 i = 0; i < course.CourseTags.Count; i++)
             course.CourseTags.Remove(course.CourseTags.ElementAt(i));
     }
 }
 /*
  * Initializes courses cshedules, topics and tags in new thread
  */
 public static Task InitializeCourseDetailsComponentsAsync(Course course, ApplicationDbContext appContext)
 {
     return Task.Factory.StartNew(() =>
     {
         if (!appContext.Entry(course).Collection(c => c.Topics).IsLoaded)
             appContext.Entry(course).Collection(c => c.Topics).Load();
         if (!appContext.Entry(course).Collection(c => c.CourseTags).IsLoaded)
             appContext.Entry(course).Collection(c => c.CourseTags).Load();
     });
 }
        /*USABLE METHODS*/

        /*
         * Saving course file (image from local computer or by url)
         * file - file from users local PC
         * fileSite - url of image 
         * course - course to save image
         * serverMapPath - path on server to save image:
         * for example:
         * serverMapPAth - C://aaaa//aaa//
         * image new path - C://aaaa//aaa//Course{ID}//Course{ID}.extention
         */
        public static Boolean SaveCourseFile(HttpPostedFileBase file, String fileSite, Course course, String serverMapPath, String fileNameBegin = "Course", Boolean save = true)
        {
            if (file != null)
            {
                String fileName = fileNameBegin + course.ID.ToString();
                String imagePath = SaveImagePersonToFolder(fileName, file, serverMapPath);
                if (imagePath != String.Empty)
                {
                    course.Image = imagePath;
                    return true;
                }
            }
            ///*saving images http
            else if (fileSite != String.Empty)
            {
                if (CourseHelper.ValidateHttpImageRoute(fileSite))
                {
                    course.Image = fileSite;
                    return true;
                }
            }
            else if (save)
            {
                course.Image = defaultImagePath;
                return true;
            }


            /*saving image is not main task*/
            if (!save)
                return true;
            return false;
        }
示例#10
0
        public ActionResult CreateCourse(CreateCourseViewModel model)
        {
            ViewBag.AtEarliest = DateTime.Today.AddDays(1);

            Menu(Home: true);
            SetBreadcrumbs(
                one: new MenyItem { Link = "~/Teacher/", Text = "Se alla kurser" });

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            model.StartDate = new DateTime(model.StartDate.Year, model.StartDate.Month, model.StartDate.Day, 0, 0, 0);
            model.EndDate = new DateTime(model.EndDate.Year, model.EndDate.Month, model.EndDate.Day, 23, 59, 0);

            bool hasError = false;
            if (model.StartDate < DateTime.Today.AddDays(1))
            {
                ModelState.AddModelError("StartDate", "Startdatum kan tyvärr ej starta innan morgondagen, pga. planeringstid");
                hasError = true;
            }
            if (model.EndDate < model.StartDate)
            {
                ModelState.AddModelError("EndDate", "Slutdatumet kan ej vara innan startdatumet");
                hasError = true;
            }
            if (db.Courses.FirstOrDefault(c => c.Name == model.Name) != null)
            {
                ModelState.AddModelError("Name", "Namnet för denna kurs är redan upptagen");
            }
            if (hasError)
            {
                return View(model);
            }

            Course course = new Course { Name = model.Name, Description = (model.Description != null ? model.Description : ""), StartDate = model.StartDate, EndDate = model.EndDate };
            db.Courses.Add(course);
            db.SaveChanges();

            return Redirect("~/Teacher/Course/"+ course.Id);
        }