2jzRS : csharp
Umieszczono: 2013-04-27 16:07:35
Format: Czysty text, HTML, HTML źródło, Plik svg, Plik svg źródło, RTF, LaTeX
W każdym formacie kodowanie wyjściowe to UTF-8

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
        public ActionResult TestStart(AttemptAnswers attA, string prevBtn)
        {
            List<int> answers = null;

            foreach (var val in Request.Form.AllKeys
                                            .Where(k => k.StartsWith("answer_"))
                                            .SelectMany(k => Request.Form.GetValues(k))
                                            .Select(v => Convert.ToInt32(v)))
            {
                if (answers.IsEmpty()) answers = new List<int>();
                answers.Add(val);
            }

            // Słownik odpowiedzi wpisanych w pola tekstowe (format: answet_id -> tekst)
            // Wyjątkiem jest pytanie typu TextBox gdzie odpowiedź zapisuje się do attA.Other
            Dictionary<int, string> textAnswers = new Dictionary<int, string>();
            foreach (var key in Request.Form.AllKeys.Where(k => k.StartsWith("answerText_")))
            {
                var text = Request.Form.GetValues(key).FirstOrDefault();
                if (!text.IsEmpty())
                {
                    textAnswers.Add(
                        Convert.ToInt32(key.Substring("answerText_".Length)),
                        text
                    );
                }

            }

            var asked = HttpContext.Session.Get<List<Questions>>("asked");

            attA.Question = db.Questions.FirstOrDefault(q => q.Id == attA.QuestionId);
            attA.AttemptTests = db.AttemptTests.First(a => a.Id == attA.AttemptTestId);
            var attemptAnswer = new AttemptAnswers() { AttemptTests = attA.AttemptTests };

            if (!asked.IsEmpty()) // jesli wystapilo juz jakieś pytanie
            {
                ViewBag.PrevButton = asked.Count > 0;
                if (prevBtn != null && asked.Count > 1) //jesli zostal nacisniety przycisk wstecz
                {
                    asked.RemoveAt(asked.Count - 1);
                    attemptAnswer.Question = asked.Last();
                    if (attemptAnswer.Question == null)
                    {
                        throw new Exception("Linia 206 question jest nullem");
                    }

                    if (asked.Count <= 1)
                        ViewBag.PrevButton = false; // jestesmy na pierwszym pytaniu, wiec wyłączamy przycisk Wstecz

                    HttpContext.Session["asked"] = asked;
                    ViewData["NameTest"] = HttpContext.Session.Get<string>("NameTest");
                    ViewData["NameSection"] = HttpContext.Session.Get<string>("NameSection");

                    ViewData["multiAnswers"] = (from a in db.AttemptAnswers
                                                join q in db.Questions on a.QuestionId equals q.Id
                                                where a.AttemptTestId == attA.AttemptTestId && a.QuestionId == attemptAnswer.Question.Id
                                                select a).ToList();
                    if (ViewData["multiAnswers"] == null)
                    {
                        throw new Exception("Linia 215, multianswers jest null");
                    }
                    var tags = HttpContext.Session.Get<Dictionary<string, string>>("tags");
                    ApplyTags(attemptAnswer.Question, tags);

                    return View(attemptAnswer);
                }
            }
            else
            {
                ViewBag.PrevButton = false; //jesli nie wystpilo zadne pytanie ukrywamy przycisk Wstecz
            }

            //Zapisanie odpowiedzi
            if (attA.QuestionId != 0)
            {
                // Walidacja danych
                answers = ValidateAnswers(attA, answers, textAnswers);
                if (answers.IsEmpty() && textAnswers.IsEmpty() || !ModelState.IsValid) // Blad walidacji
                    return View(attA);

                var prevAnswers = attA.Question.AttemptAnswers.Where(a => a.AttemptTestId == attA.AttemptTestId);
                // Odpowiadano już na to pytanie? -> usuwamy stare odpowiedzi i dajemy nowe
                if (!asked.IsEmpty() && asked.Any(q => q.Id == attA.QuestionId) && prevAnswers.Count() > 0)
                {
                    foreach (var at in prevAnswers.ToList())
                        db.AttemptAnswers.Remove(at);

                    db.SaveChanges();
                }

                foreach (var item in answers)
                {
                    var answer = db.Answers.FirstOrDefault(a => a.Id == item);
                    if (answer.Type == FieldType.CheckBox_TextBox || answer.Type == FieldType.RadioButton_TextBox)
                    {
                        db.AttemptAnswers.Add(new AttemptAnswers() { AttemptTests = attA.AttemptTests, AnswerId = item, QuestionId = attA.QuestionId, Other = textAnswers[item] });
                    }
                    else
                    {
                        db.AttemptAnswers.Add(new AttemptAnswers() { AttemptTests = attA.AttemptTests, AnswerId = item, QuestionId = attA.QuestionId });
                    }
                }
                if (answers.IsEmpty() && !textAnswers.IsEmpty()) //przypadek z textArea
                {
                    db.AttemptAnswers.Add(new AttemptAnswers() { AttemptTests = attA.AttemptTests, AnswerId = textAnswers.Keys.FirstOrDefault(), QuestionId = attA.QuestionId, Other = textAnswers.Values.FirstOrDefault() });
                }
                db.SaveChanges();

            }

            // Wybranie pytania   
            attemptAnswer.Question = GetNextQuestion(attA.AttemptTests, answers, asked);

            if (attemptAnswer.Question == null)   //Gdy wszystkie pytania zostana zadane przekierowuje na inna strone
            {
                // Usuń zbędne odpowiedzi
                var useles = attA.AttemptTests.AttemptAnswers.Where(a => !asked.Any(x => x.Id == a.QuestionId)).ToList();

                foreach (var question in useles)
                    db.AttemptAnswers.Remove(question);

                attA.AttemptTests.Done = true;
                db.SaveChanges();

                return View("ThankYou");
            }

            asked.Add(attemptAnswer.Question);
            //HttpContext.Session["asked"] = asked;

            // jesli odpowiedzi do danego testu i pytania są w bazie wczytujemy je do widoku
            ViewData["multiAnswers"] = attemptAnswer.Question.AttemptAnswers.Where(a => a.AttemptTestId == attA.AttemptTestId).ToList();

            // View Data
            HttpContext.Session["NameSection"] = attemptAnswer.Question.Sections.Name;
            HttpContext.Session["NameTest"] = attA.AttemptTests.TestTemplates.Name;

            ViewData["NameTest"] = attA.AttemptTests.TestTemplates.Name;
            ViewData["NameSection"] = HttpContext.Session.Get<string>("NameSection");

            return View(attemptAnswer);
        }