Example #1
0
        public static List <UserCourseRoleMapping> DeleteUserCourseRoleMapping(UserCourseRoleMapping ucrm)
        {
            try
            {
                using (var entities = new QBotEntities())
                {
                    int courseId = ucrm.CourseId;
                    var tutorialGroupMappingsToDelete = from tutorialMapping in entities.TutorialGroupMemberships
                                                        where tutorialMapping.UserId == ucrm.UserId
                                                        select tutorialMapping;
                    if (tutorialGroupMappingsToDelete != null)
                    {
                        entities.TutorialGroupMemberships.RemoveRange(tutorialGroupMappingsToDelete);
                    }

                    var ucrms = entities.UserCourseRoleMappings.Where(x => x.Id == ucrm.Id).FirstOrDefault();
                    entities.UserCourseRoleMappings.Remove(ucrms);
                    entities.SaveChanges();
                    var updatedList = entities.UserCourseRoleMappings.Where(x => x.CourseId == courseId)
                                      .Include(x => x.User)
                                      .Include(x => x.User.TutorialGroupMemberships)
                                      .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup))
                                      .Include(x => x.Course)
                                      .Include(x => x.Role)
                                      .ToList();
                    return(updatedList);
                }
            }
            catch (Exception e)
            {
                LogExceptions(e);
                return(null);
            }
        }
Example #2
0
        public static List <UserCourseRoleMapping> SaveUserCourseRoleMapping(UserCourseRoleMapping ucrm, List <int> tutorialIds)
        {
            try
            {
                using (var entities = new QBotEntities())
                {
                    int courseId     = ucrm.CourseId;
                    var ucrmToUpdate = entities.UserCourseRoleMappings.Where(x => x.Id == ucrm.Id).FirstOrDefault();
                    if (ucrm == null)
                    {
                        ucrmToUpdate          = new UserCourseRoleMapping();
                        ucrmToUpdate.UserId   = ucrm.UserId;
                        ucrmToUpdate.CourseId = courseId;
                    }

                    ucrmToUpdate.RoleId = ucrm.RoleId;
                    entities.UserCourseRoleMappings.AddOrUpdate(ucrmToUpdate);

                    // Reset all tutorial group memberships for this course
                    List <TutorialGroupMembership> membershipsToDelete = entities.TutorialGroupMemberships
                                                                         .Where(x => x.UserId == ucrm.UserId && x.TutorialGroup.CourseId == courseId)
                                                                         .ToList();

                    if (membershipsToDelete != null)
                    {
                        entities.TutorialGroupMemberships.RemoveRange(membershipsToDelete);
                    }

                    // Add in tutorial group memberships from the UI
                    foreach (int i in tutorialIds)
                    {
                        entities.TutorialGroupMemberships.Add(new TutorialGroupMembership()
                        {
                            TutorialGroupId = i,
                            UserId          = ucrm.UserId,
                        });
                    }

                    entities.SaveChanges();

                    var updatedList = entities.UserCourseRoleMappings.Where(x => x.CourseId == courseId)
                                      .Include(x => x.User)
                                      .Include(x => x.User.TutorialGroupMemberships)
                                      .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup))
                                      .Include(x => x.Course)
                                      .Include(x => x.Role)
                                      .ToList();
                    return(updatedList);
                }
            }
            catch (Exception e)
            {
                LogExceptions(e);
                return(null);
            }
        }
Example #3
0
        public static UserCourseRoleMapping GetBotUser(int courseID)
        {
            using (var entities = new QBotEntities())
            {
                try
                {
                    var ucrm = entities.UserCourseRoleMappings
                               .Include(x => x.User)
                               .Include(x => x.User.TutorialGroupMemberships)
                               .Include(x => x.User.AnswerPoster)
                               .Include(x => x.User.OriginalPoster)
                               .Include(x => x.User.UserCourseRoleMappings)
                               .Include(x => x.Role)
                               .Include(x => x.Role.UserCourseRoleMappings)
                               .Include(x => x.Course)
                               .Include(x => x.Course.Questions)
                               .Include(x => x.Course.TutorialGroups)
                               .Include(x => x.Course.UserCourseRoleMappings)
                               .Where(x => x.CourseId == courseID && x.RoleId == 4).FirstOrDefault();

                    if (ucrm == null)
                    {
                        var user = entities.Users.Where(x => x.FirstName == "Question" && x.LastName == "Bot").FirstOrDefault();
                        if (user == null)
                        {
                            user = new User()
                            {
                                FirstName = "Question",
                                LastName  = "Bot",
                            };
                            entities.Users.Add(user);
                            entities.SaveChanges();
                        }

                        ucrm = new UserCourseRoleMapping()
                        {
                            UserId   = user.Id,
                            CourseId = courseID,
                            RoleId   = 4,
                        };
                        entities.UserCourseRoleMappings.Add(ucrm);
                        entities.SaveChanges();
                    }

                    return(ucrm);
                }
                catch (Exception e)
                {
                    LogExceptions(e);
                    return(null);
                }
            }
        }
Example #4
0
        public static List <UserCourseRoleMapping> AddStudents(List <User> students, string coursename)
        {
            try
            {
                using (var entities = new QBotEntities())
                {
                    Course course        = entities.Courses.Where(x => x.Name == coursename).FirstOrDefault();
                    int    studentRoleId = entities.Roles.Where(x => x.Name == "Student").FirstOrDefault().Id;
                    foreach (User student in students)
                    {
                        int studentID = 0;
                        if (course != null)
                        {
                            User studentToUpdate = entities.Users.Where(x => x.UserPrincipalName == student.UserPrincipalName).FirstOrDefault();

                            // Existing student
                            if (studentToUpdate != null)
                            {
                                studentToUpdate.FirstName         = student.FirstName;
                                studentToUpdate.LastName          = student.LastName;
                                studentToUpdate.UserPrincipalName = student.UserPrincipalName;
                                studentToUpdate.Email             = student.Email;
                                studentID = studentToUpdate.Id;
                            }
                            else
                            {
                                User newStudent = new User
                                {
                                    StudentId         = student.StudentId,
                                    FirstName         = student.FirstName,
                                    LastName          = student.LastName,
                                    UserPrincipalName = student.UserPrincipalName,
                                    Email             = student.Email,
                                };
                                entities.Users.Add(newStudent);

                                // Need this line to generate identity
                                entities.SaveChanges();
                                studentID = newStudent.Id;
                            }

                            // Write course role mapping
                            UserCourseRoleMapping ucrm = entities.UserCourseRoleMappings.Where(x => x.CourseId == course.Id && x.UserId == studentID).FirstOrDefault();
                            if (ucrm == null)
                            {
                                ucrm = new UserCourseRoleMapping
                                {
                                    UserId   = studentID,
                                    CourseId = course.Id,
                                    RoleId   = studentRoleId,
                                };
                                entities.UserCourseRoleMappings.Add(ucrm);
                                entities.SaveChanges();
                            }
                        }
                    }

                    entities.SaveChanges();
                    return(entities.UserCourseRoleMappings
                           .Include(x => x.User)
                           .Include(x => x.User.TutorialGroupMemberships)
                           .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup))
                           .Include(x => x.User.AnswerPoster)
                           .Include(x => x.User.OriginalPoster)
                           .Include(x => x.User.UserCourseRoleMappings)
                           .Include(x => x.Role)
                           .Include(x => x.Role.UserCourseRoleMappings)
                           .Include(x => x.Course)
                           .Include(x => x.Course.Questions)
                           .Include(x => x.Course.TutorialGroups)
                           .Include(x => x.Course.UserCourseRoleMappings)
                           .Where(x => x.CourseId == course.Id).ToList());
                }
            }
            catch (Exception e)
            {
                LogExceptions(e);
                return(null);
            }
        }