예제 #1
0
 public string removeQuestions(List <Tuple <int, string> > questionsIdsAndResonsList)
 {
     // verify all questions exists
     lock (_syncLockQuestionId)
     {
         foreach (Tuple <int, string> t in questionsIdsAndResonsList)
         {
             if (t.Item1 >= _questionID)
             {
                 return("Error. Some questions cannot be removed.");
             }
         }
     }
     foreach (Tuple <int, string> t in questionsIdsAndResonsList)
     {
         Question q             = _db.getQuestion(t.Item1);
         string   removalReason = t.Item2;
         if (removalReason.Equals(""))
         {
             removalReason = "This quesiton has been removed. Any answer will not be accounted for.";
         }
         q.isDeleted = true;
         if (q.text == "")
         {
             q.text = removalReason;
         }
         else
         {
             q.text += Environment.NewLine + Environment.NewLine + removalReason;
         }
         _db.updateQuestion(q);
     }
     return(Replies.SUCCESS);
 }
예제 #2
0
        public Tuple <string, int> answerAQuestion(User u, Question q, bool isNormal, int normalityCertaintyLVL, List <string> diagnoses, List <int> certainties)
        {
            bool correctAnswer = true;
            Dictionary <string, int> userLevels = new Dictionary <string, int>();
            // get all correct diagnoses
            List <Diagnosis> qDiagnoses      = _db.getQuestionDiagnoses(q.QuestionId);
            List <string>    qDiagnosesNames = new List <string>();

            foreach (Diagnosis d in qDiagnoses)
            {
                qDiagnosesNames.Add(d.TopicId);
            }
            foreach (string s in diagnoses)
            {
                #region get or create user level for each diagnosis and update fields according to the answer
                UserLevel userLevel    = _db.getUserLevel(u.UserId, q.SubjectId, s);
                bool      alreadtExist = userLevel != null;
                if (userLevel == null)
                {
                    userLevel = new UserLevel {
                        UserId = u.UserId, SubjectId = q.SubjectId, TopicId = s, level = Levels.DEFAULT_LVL, timesAnswered = 0, timesAnsweredCorrectly = 0
                    };
                }
                userLevels[s] = userLevel.level;
                userLevel.timesAnswered++;
                if (qDiagnosesNames.Contains(s))
                {
                    userLevel.timesAnsweredCorrectly++;
                }
                else
                {
                    correctAnswer = false;
                }
                if (alreadtExist)
                {
                    if (userLevel.timesAnswered >= UsersLevels.MIN_ANSWERS_BEFORE_LEVEL_CHANGE)
                    {
                        if (userLevel.level < Levels.MAX_LVL && userLevel.timesAnsweredCorrectly / userLevel.timesAnswered >= UsersLevels.LEVEL_UP_SUCCESS_RATE)
                        {
                            userLevel.level++;
                            userLevel.timesAnsweredCorrectly = 0;
                            userLevel.timesAnswered          = 0;
                        }
                        if (userLevel.level > Levels.MIN_LVL && userLevel.timesAnsweredCorrectly / userLevel.timesAnswered <= UsersLevels.LEVEL_DOWN_SUCCESS_RATE)
                        {
                            userLevel.level--;
                            userLevel.timesAnsweredCorrectly = 0;
                            userLevel.timesAnswered          = 0;
                        }
                    }
                    _db.updateUserLevel(userLevel);
                }
                else
                {
                    _db.addUserLevel(userLevel);
                }
                #endregion
                // update question's fields according to each diagnosis
                double lvl = userLevel.level;
                if (qDiagnosesNames.Contains(s))
                {
                    q.points -= (Levels.MAX_LVL + 1 - lvl) / (double)qDiagnoses.Count;
                }
                else
                {
                    q.points += lvl / (double)qDiagnoses.Count;
                }
            }
            // update question's level if needed
            if (q.points <= 0 && q.level > Levels.MIN_LVL)
            {
                q.level--;
                q.points = Questions.QUESTION_INITAL_POINTS;
            }
            if (q.points >= Questions.QUESTION_INITAL_POINTS * 2 && q.level < Levels.MAX_LVL)
            {
                q.level++;
                q.points = Questions.QUESTION_INITAL_POINTS;
            }
            // create a new answer instance and save to DB
            Answer a     = new Answer {
            };
            int answerId = -1;
            lock (syncLockAnswerId)
            {
                a.AnswerId           = _answerId;
                a.QuestionId         = q.QuestionId;
                a.UserId             = u.UserId;
                a.timeAdded          = DateTime.Now;
                a.questionLevel      = q.level;
                a.isCorrectAnswer    = correctAnswer;
                a.normal             = isNormal;
                a.normalityCertainty = normalityCertaintyLVL;
                List <DiagnosisCertainty> diagnosesCertainties = new List <DiagnosisCertainty>();
                for (int i = 0; !isNormal && i < diagnoses.Count; i++)
                {
                    DiagnosisCertainty dc = new DiagnosisCertainty
                    {
                        AnswerId  = _answerId,
                        SubjectId = q.SubjectId,
                        TopicId   = diagnoses[i],
                        certainty = certainties[i],
                        userLevel = userLevels[diagnoses[i]]
                    };
                    _db.addDiagnosisCertainty(dc);
                }
                answerId = _answerId;
                _answerId++;
            }
            _db.addAnswer(a);
            _db.updateQuestion(q);
            _db.SaveChanges();
            return(new Tuple <string, int>(Replies.SUCCESS, answerId));
        }