示例#1
0
        public void CreateQuiz(DatabaseEntities connection)
        {
            var quiz        = new Quiz();
            var transaction = connection.Database.BeginTransaction();

            try
            {
                quiz.teacherId    = this.teacherId;
                quiz.title        = this.title;
                quiz.description  = this.description;
                quiz.totalMarks   = this.totalMarks;
                quiz.passingMarks = this.passingMarks;
                quiz.visibility   = this.visibility;
                connection.Quiz.Add(quiz);
                connection.SaveChanges();

                foreach (var question in questions)
                {
                    var newQuestion = question.ToQuestion();
                    newQuestion.quizId    = quiz.id;
                    newQuestion.teacherId = quiz.teacherId;
                    connection.Question.Add(newQuestion);
                    connection.SaveChanges();

                    if (newQuestion.type == "Multiple Choice" ||
                        newQuestion.type == "Checkboxes")
                    {
                        foreach (var option in question.options)
                        {
                            var newOption = option.ToQuestionOption();
                            newOption.questionId = newQuestion.id;
                            newOption.quizId     = quiz.id;
                            newOption.teacherId  = quiz.teacherId;
                            connection.QuestionOption.Add(newOption);
                            connection.SaveChanges();
                        }
                    }
                }

                if (this.visibility == "Public")
                {
                    foreach (string bl in this.blackList)
                    {
                        var blackEmail = new Blacklist();
                        blackEmail.email     = bl;
                        blackEmail.quizId    = quiz.id;
                        blackEmail.teacherId = quiz.teacherId;
                        connection.Blacklist.Add(blackEmail);
                        connection.SaveChanges();
                    }
                }
                else
                {
                    foreach (string wl in this.whiteList)
                    {
                        var whiteEmail = new Whitelist();
                        whiteEmail.email     = wl;
                        whiteEmail.quizId    = quiz.id;
                        whiteEmail.teacherId = quiz.teacherId;
                        connection.Whitelist.Add(whiteEmail);
                        connection.SaveChanges();
                    }
                }

                transaction.Commit();
            } catch (Exception err)
            {
                transaction.Rollback();
                throw err;
            }
        }
示例#2
0
        public void UpdateQuiz(DatabaseEntities connection)
        {
            if (this.id < 0)
            {
                throw new Exception("Quiz.id must be non-negtive to perform update.");
            }

            var quiz = connection.Quiz.Where(q => q.id == this.id &&
                                             q.teacherId == this.teacherId).Single();
            var transaction = connection.Database.BeginTransaction();

            try {
                /* remove existing data */
                this.Purge(connection);

                /* add new data */
                foreach (var question in questions)
                {
                    var newQuestion = question.ToQuestion();
                    newQuestion.quizId    = quiz.id;
                    newQuestion.teacherId = quiz.teacherId;
                    connection.Question.Add(newQuestion);
                    connection.SaveChanges();

                    if (newQuestion.type == "Multiple Choice" ||
                        newQuestion.type == "Checkboxes")
                    {
                        foreach (var option in question.options)
                        {
                            var newOption = option.ToQuestionOption();
                            newOption.questionId = newQuestion.id;
                            newOption.quizId     = quiz.id;
                            newOption.teacherId  = quiz.teacherId;
                            connection.QuestionOption.Add(newOption);
                            connection.SaveChanges();
                        }
                    }
                }

                if (this.visibility == "Public")
                {
                    foreach (string bl in this.blackList)
                    {
                        var blackEmail = new Blacklist();
                        blackEmail.email     = bl;
                        blackEmail.quizId    = quiz.id;
                        blackEmail.teacherId = quiz.teacherId;
                        connection.Blacklist.Add(blackEmail);
                        connection.SaveChanges();
                    }
                }
                else
                {
                    foreach (string wl in this.whiteList)
                    {
                        var whiteEmail = new Whitelist();
                        whiteEmail.email     = wl;
                        whiteEmail.quizId    = quiz.id;
                        whiteEmail.teacherId = quiz.teacherId;
                        connection.Whitelist.Add(whiteEmail);
                        connection.SaveChanges();
                    }
                }

                quiz.title        = this.title;
                quiz.description  = this.description;
                quiz.totalMarks   = this.totalMarks;
                quiz.passingMarks = this.passingMarks;
                quiz.visibility   = this.visibility;

                connection.SaveChanges();
                transaction.Commit();
            } catch (Exception err) {
                transaction.Rollback();
                throw new Exception("Error: please see inner exception for details.", err);
            }
        }