Example #1
0
        public async Task <string> CreateRequest(string userId)
        {
            var previousRequests = db.RestoreRequests.Where(r => r.UserId == userId).ToList();

            if (previousRequests.Any())
            {
                var hasRecent = false;
                foreach (var previous in previousRequests)
                {
                    if (DateTime.Now.Subtract(previous.Timestamp) < TimeSpan.FromMinutes(5))
                    {
                        hasRecent = true;
                    }
                    else
                    {
                        db.RestoreRequests.Remove(previous);
                    }
                }
                if (hasRecent)
                {
                    await db.SaveChangesAsync();

                    return(null);
                }
            }

            var request = new RestoreRequest
            {
                Id        = Guid.NewGuid().ToString(),
                UserId    = userId,
                Timestamp = DateTime.Now
            };

            db.RestoreRequests.Add(request);
            await db.SaveChangesAsync();

            return(request.Id);
        }
Example #2
0
        public async Task SetAdditionalScore(string courseId, Guid unitId, string userId, string scoringGroupId, int score, string instructorId)
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                var scores = db.AdditionalScores.Where(s => s.CourseId == courseId && s.UnitId == unitId && s.UserId == userId && s.ScoringGroupId == scoringGroupId);
                db.AdditionalScores.RemoveRange(scores);

                var additionalScore = new AdditionalScore
                {
                    CourseId       = courseId,
                    UnitId         = unitId,
                    UserId         = userId,
                    ScoringGroupId = scoringGroupId,
                    Score          = score,
                    InstructorId   = instructorId,
                    Timestamp      = DateTime.Now,
                };
                db.AdditionalScores.Add(additionalScore);

                transaction.Commit();
                await db.SaveChangesAsync();
            }
        }
Example #3
0
        public async Task <UserQuiz> AddUserQuiz(string courseId, bool isRightAnswer, string itemId, string quizId, Guid slideId, string text, string userId, DateTime time, int quizBlockScore, int quizBlockMaxScore)
        {
            var quizzesRepo        = new QuizzesRepo(db);
            var currentQuizVersion = quizzesRepo.GetLastQuizVersion(courseId, slideId);
            var userQuiz           = new UserQuiz
            {
                CourseId          = courseId,
                SlideId           = slideId,
                QuizVersionId     = currentQuizVersion.Id,
                IsRightAnswer     = isRightAnswer,
                ItemId            = itemId,
                QuizId            = quizId,
                Text              = text,
                Timestamp         = time,
                UserId            = userId,
                QuizBlockScore    = quizBlockScore,
                QuizBlockMaxScore = quizBlockMaxScore
            };

            db.UserQuizzes.Add(userQuiz);
            await db.SaveChangesAsync();

            return(userQuiz);
        }
Example #4
0
        public async Task <UserQuestion> AddUserQuestion(string question, string courseId, Slide slide, string userId, string userName, DateTime time)
        {
            var userSolution = db.UserQuestions.Add(new UserQuestion
            {
                Question   = question,
                UserId     = userId,
                UserName   = userName,
                SlideTitle = slide.Title,
                UnitName   = slide.Info.UnitName,
                SlideId    = slide.Id,
                CourseId   = courseId,
                Time       = time
            });
            await db.SaveChangesAsync();

            return(userSolution);
        }
Example #5
0
        public async Task <TextBlob> AddText(string text)
        {
            if (text == null)
            {
                return new TextBlob
                       {
                           Hash = null,
                           Text = null
                       }
            }
            ;

            if (text.Length > MaxTextSize)
            {
                text = text.Substring(0, MaxTextSize);
            }

            var hash = GetHash(text);
            var blob = db.Texts.Find(hash);

            if (blob != null)
            {
                return(blob);
            }

            blob = new TextBlob
            {
                Hash = hash,
                Text = text
            };
            db.Texts.AddOrUpdate(blob);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                throw new Exception(
                          string.Join("\r\n",
                                      e.EntityValidationErrors.SelectMany(v => v.ValidationErrors).Select(err => err.PropertyName + " " + err.ErrorMessage)));
            }
            return(blob);
        }
Example #6
0
        public async Task ToggleRole(string courseId, string userId, CourseRole role)
        {
            var userRole = db.UserRoles.FirstOrDefault(u => u.UserId == userId && u.Role == role && u.CourseId == courseId);

            if (userRole == null)
            {
                db.UserRoles.Add(new UserRole
                {
                    UserId   = userId,
                    CourseId = courseId,
                    Role     = role
                });
            }
            else
            {
                db.UserRoles.Remove(userRole);
            }
            await db.SaveChangesAsync();
        }
Example #7
0
        public async Task Update(string userId, string slideId, string ltiRequestJson)
        {
            var ltiRequestModel = FindElement(userId, slideId);

            if (ltiRequestModel == null)
            {
                ltiRequestModel = new LtiSlideRequest
                {
                    UserId  = userId,
                    SlideId = slideId,
                    Request = ltiRequestJson
                };
            }
            else
            {
                ltiRequestModel.Request = ltiRequestJson;
            }

            db.LtiRequests.AddOrUpdate(ltiRequestModel);
            await db.SaveChangesAsync();
        }
Example #8
0
        public async Task <CertificateTemplate> AddTemplate(string courseId, string name, string archiveName)
        {
            var template = new CertificateTemplate
            {
                Id          = Guid.NewGuid(),
                CourseId    = courseId,
                Name        = name,
                Timestamp   = DateTime.Now,
                ArchiveName = archiveName,
            };

            db.CertificateTemplates.Add(template);
            await db.SaveChangesAsync();

            return(template);
        }
Example #9
0
        public async Task AddQuizAttemptForManualChecking(string courseId, Guid slideId, string userId)
        {
            var manualChecking = new ManualQuizChecking
            {
                CourseId  = courseId,
                SlideId   = slideId,
                UserId    = userId,
                Timestamp = DateTime.Now,
            };

            db.ManualQuizCheckings.Add(manualChecking);

            await db.SaveChangesAsync();
        }