예제 #1
0
        internal static QuestionWithAnswers MapDbQuestionWithAnswers(DbQuestion dbQuestion)
        {
            if (dbQuestion == null)
            {
                return(null);
            }
            var answers = new List <Answer>();

            foreach (var item in dbQuestion.Answers)
            {
                answers.Add(MapDbAnswer(item));
            }
            return(new QuestionWithAnswers()
            {
                Id = dbQuestion.Id,
                Author = dbQuestion.User?.UserName,
                Title = dbQuestion.Title,
                Content = dbQuestion.Content,
                Answers = answers,
                Created = dbQuestion.Created.ToString(dateFormat),
                LastUpdate = dbQuestion.LastUpdated.ToString(dateFormat),
                Topic = MapDbTopic(dbQuestion.Topic),
                Closed = dbQuestion.Closed,
                Type = dbQuestion.Type,
                Moderator = dbQuestion.Moderator?.UserName,
                ModeratorMessage = dbQuestion.ModeratorMessage
            });
        }
예제 #2
0
        public async Task <Question> Store(Question question)
        {
            //So there would be the same for the creation
            var now = DateTime.Now;
            var dbQ = new DbQuestion()
            {
                Content     = question.Content,
                Title       = question.Title,
                Created     = now,
                LastUpdated = now,
            };
            var user = await dbcontext.Users.SingleOrDefaultAsync(u => u.UserName == question.Author);

            if (user == null)
            {
                return(null);
            }
            dbQ.User = user;
            var topic = await dbcontext.Topics.FirstOrDefaultAsync(t => t.Id == question.Topic.Id);

            dbQ.Topic = topic;
            await dbcontext.Questions.AddAsync(dbQ);

            await dbcontext.SaveChangesAsync();

            return(DbMapper.MapDbQuestion(dbQ));
        }
예제 #3
0
        public void AddAnswer(DbQuestion q)
        {
            DbQuestion toRemove = answers.Where(a => a.ID == q.ID).FirstOrDefault();

            if (toRemove != null)
            {
                answers.Remove(toRemove);
            }
            answers.Add(q);
        }
예제 #4
0
        public void NextNode(string key)
        {
            int        input           = Convert.ToInt32(key.Remove(0, 8));
            DbQuestion currentquestion = list.Where(a => a.ID == input).FirstOrDefault();

            if (currentquestion != null)
            {
                openForm(key, false);
            }
        }
예제 #5
0
파일: Repository.cs 프로젝트: titu84/Exam
        internal bool Update(DbQuestion question)
        {
            if (question.Image != null)
            {
                question.ImageText = ImageToString(question.Image);
            }
            if (question.ImageAlt != null)
            {
                question.ImageTextAlt = ImageToString(question.ImageAlt);
            }
            string sql = "Update [Questions] SET "
                         + "Question = '" + question.question + "', "
                         + "Type = " + question.Type + ", "
                         + "QType = " + question.QType + ", "
                         + "A = '" + question.A.ReplaceSymbolToApostrophe() + "', "
                         + "B = '" + question.B.ReplaceSymbolToApostrophe() + "', "
                         + "C = '" + question.C.ReplaceSymbolToApostrophe() + "', "
                         + "D = '" + question.D.ReplaceSymbolToApostrophe() + "', "
                         + "E = '" + question.E.ReplaceSymbolToApostrophe() + "', "
                         + "F = '" + question.F.ReplaceSymbolToApostrophe() + "', "
                         + "G = '" + question.G.ReplaceSymbolToApostrophe() + "', "
                         + "HH = '" + question.HH.ReplaceSymbolToApostrophe() + "', "
                         + "I = '" + question.I.ReplaceSymbolToApostrophe() + "', "
                         + "J = '" + question.J.ReplaceSymbolToApostrophe() + "', "
                         + "K = '" + question.K.ReplaceSymbolToApostrophe() + "', "
                         + "ImageText = '" + question.ImageText + "', "
                         + "ImageTextAlt = '" + question.ImageTextAlt + "', "
                         + "H = '" + question.H.ReplaceSymbolToApostrophe() + "'"
                         + " WHERE ID = " + question.ID;

            using (SQLiteConnection con = new SQLiteConnection(conStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, con);
                try
                {
                    con.Open();
                    if (con.State == System.Data.ConnectionState.Open)
                    {
                        cmd.ExecuteNonQuery();
                        return(true);
                    }
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show("Problemik w bazie - nie udało się dodać");
                    con.Close();
                    return(false);
                }
                return(false);
            }
        }
예제 #6
0
파일: Repository.cs 프로젝트: titu84/Exam
        internal bool Add(DbQuestion question)
        {
            if (question.Image != null)
            {
                question.ImageText = ImageToString(question.Image);
            }
            if (question.ImageAlt != null)
            {
                question.ImageTextAlt = ImageToString(question.ImageAlt);
            }
            string sql = "Insert into [Questions] (Question, Type, A, B, C, D, E, F, G, H, HH, I, J, K, QType, ImageText, ImageTextAlt) Values ( " +
                         "'" + question.question + "'" + " , " +
                         question.Type + " , " +
                         "'" + question.A.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.B.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.C.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.D.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.E.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.F.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.G.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.H.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.HH.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.I.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.J.ReplaceSymbolToApostrophe() + "'" + " , " +
                         "'" + question.K.ReplaceSymbolToApostrophe() + "'" + " , " +
                         question.QType + " , " +
                         "'" + question.ImageText + "'" + " , " +
                         "'" + question.ImageTextAlt + "'" +
                         " )";

            using (SQLiteConnection con = new SQLiteConnection(conStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, con);
                try
                {
                    con.Open();
                    if (con.State == System.Data.ConnectionState.Open)
                    {
                        cmd.ExecuteNonQuery();
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("Problemik w bazie - nie udało się dodać\n" + e);
                    con.Close();
                    return(false);
                }
                return(false);
            }
        }
예제 #7
0
파일: Repository.cs 프로젝트: titu84/Exam
        internal DbQuestion GetQuestion(short id, bool?withImages = null)
        {
            DbQuestion question = new DbQuestion();
            string     sql      = "SELECT * FROM [Questions] where ID = " + id;

            using (SQLiteConnection con = new SQLiteConnection(conStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, con);
                try
                {
                    con.Open();
                    if (con.State == System.Data.ConnectionState.Open)
                    {
                        using (SQLiteDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                question.ID       = Convert.ToInt16(reader["ID"]);
                                question.Type     = Convert.ToInt16(reader["Type"]);
                                question.question = reader["Question"].ToString();
                                question.A        = reader["A"].ToString().ReplaceApostropheToSymbol();
                                question.B        = reader["B"].ToString().ReplaceApostropheToSymbol();
                                question.C        = reader["C"].ToString().ReplaceApostropheToSymbol();
                                question.D        = reader["D"].ToString().ReplaceApostropheToSymbol();
                                question.E        = reader["E"].ToString().ReplaceApostropheToSymbol();
                                question.F        = reader["F"].ToString().ReplaceApostropheToSymbol();
                                question.G        = reader["G"].ToString().ReplaceApostropheToSymbol();
                                question.H        = reader["H"].ToString().ReplaceApostropheToSymbol();
                                question.HH       = reader["HH"].ToString().ReplaceApostropheToSymbol();
                                question.I        = reader["J"].ToString().ReplaceApostropheToSymbol();
                                question.J        = reader["J"].ToString().ReplaceApostropheToSymbol();
                                question.K        = reader["K"].ToString().ReplaceApostropheToSymbol();
                                question.QType    = Convert.ToInt16(reader["QType"]);
                                if (withImages == true)
                                {
                                    question.ImageText    = reader["ImageText"].ToString();
                                    question.ImageTextAlt = reader["ImageTextAlt"].ToString();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Problemik w bazie lub z bazą... " + ex);
                    con.Close();
                }
            }
            return(question);
        }
예제 #8
0
        private async Task <Answer> AddAnswerToDbQuestion(DbQuestion question, Answer answer)
        {
            var      now      = DateTime.Now;
            DbAnswer dbAnswer = new DbAnswer()
            {
                Content     = answer.Content,
                Created     = now,
                LastUpdated = now,
                Type        = answer.Type,
            };
            var user = await dbcontext.Users.SingleOrDefaultAsync(u => u.UserName == answer.Author);

            if (question == null || user == null)
            {
                return(null);
            }
            dbAnswer.Question = question;
            dbAnswer.User     = user;
            await dbcontext.Answers.AddAsync(dbAnswer);

            return(DbMapper.MapDbAnswer(dbAnswer));
        }
예제 #9
0
        internal static Question MapDbQuestion(DbQuestion dbQuestion)
        {
            if (dbQuestion == null)
            {
                return(null);
            }
            var q = new Question()
            {
                Id               = dbQuestion.Id,
                Author           = dbQuestion.User?.UserName,
                Title            = dbQuestion.Title,
                Content          = dbQuestion.Content,
                Created          = dbQuestion.Created.ToString(dateFormat),
                LastUpdate       = dbQuestion.LastUpdated.ToString(dateFormat),
                Topic            = MapDbTopic(dbQuestion.Topic),
                Closed           = dbQuestion.Closed,
                Type             = dbQuestion.Type,
                Moderator        = dbQuestion.Moderator?.UserName,
                ModeratorMessage = dbQuestion.ModeratorMessage
            };

            return(q);
        }
예제 #10
0
        private void cbQuestionList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbQuestionList.SelectedIndex > 0)
                {
                    btnSubmit.Text    = "Zapisz";
                    btnDelete.Enabled = true;
                    short      id = Convert.ToInt16(cbQuestionList.SelectedItem);
                    Repository r  = new Repository(connStr);
                    DbQuestion q  = r.GetQuestion(id);
                    if (q.QType == 2)
                    {
                        cbQuestionType.SelectedIndex = 1;
                        pictureBox1.SizeMode         = PictureBoxSizeMode.Zoom;
                        pictureBox2.SizeMode         = PictureBoxSizeMode.Zoom;
                        // ciągniemy obrazki z bazy
                        Image[] images = r.loadImages(q.ID);
                        q.Image           = images[0];
                        q.ImageAlt        = images[1];
                        pictureBox1.Image = q.Image;
                        pictureBox2.Image = q.ImageAlt;
                    }
                    else
                    {
                        cbQuestionType.SelectedIndex = 0;
                    }
                    cbAnswerType.SelectedIndex = (int)q.Type >= 6? (int)q.Type - 2: (int)q.Type - 1;
                    richTextBox1.Text          = q.question.Replace(replace2, replace1);
                    richTextBox1.Text          = richTextBox1.Text.ReplaceApostropheToSymbol();
                    type1.Dispose();
                    type2.Dispose();
                    type3.Dispose();
                    type4.Dispose();
                    type5.Dispose();
                    type6.Dispose();
                    Point        point  = new Point(250, richTextBox1.Size.Height + pictureBox1.Size.Height + lower);
                    AnchorStyles anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                    switch (cbAnswerType.SelectedIndex)
                    {
                    case 0:
                        type1   = new Type1();
                        type1.q = q;
                        Controls.Add(type1);
                        type1.Location = point;
                        type1.Anchor   = anchor;
                        break;

                    case 1:
                        type2   = new Type2();
                        type2.q = q;
                        Controls.Add(type2);
                        type2.Location = point;
                        type2.Anchor   = anchor;
                        break;

                    case 2:
                        type3   = new Type3();
                        type3.q = q;
                        Controls.Add(type3);
                        type3.Location = point;
                        type3.Anchor   = anchor;
                        break;

                    case 3:
                        type4   = new Type4();
                        type4.q = q;
                        Controls.Add(type4);
                        type4.Location = point;
                        type4.Anchor   = anchor;
                        break;

                    case 4:
                        type5   = new Type5();
                        type5.q = q;
                        Controls.Add(type5);
                        type5.Location = point;
                        type5.Anchor   = anchor;
                        break;

                    case 5:
                        type6   = new Type6();
                        type6.q = q;
                        Controls.Add(type6);
                        type6.Location = point;
                        type6.Anchor   = anchor;
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    cbQuestionType.SelectedIndex = 0;
                    pictureBox1.Image            = null;
                    pictureBox2.Image            = null;
                    cbAnswerType_SelectedIndexChanged(sender, null);
                    btnSubmit.Text             = "Dodaj";
                    btnDelete.Enabled          = false;
                    richTextBox1.Text          = "";
                    cbAnswerType.SelectedIndex = 1; //Domyslny typ2 przy wprowadzaniu nowego. (Jednego)
                }
            }
            catch (Exception ex)
            {
                reCounter++;
                if (reCounter == 5)
                {
                    throw;
                }
                //Ponowna próba rekursywnie.
                cbQuestionList_SelectedIndexChanged(sender, e);
            }
        }
예제 #11
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                switch (cbAnswerType.SelectedIndex)
                {
                case 0:
                    if (type1.q == null)
                    {
                        goto default;
                    }
                    q      = type1.q;
                    q.Type = 1;
                    break;

                case 1:
                    if (type2.q == null)
                    {
                        goto default;
                    }
                    q      = type2.q;
                    q.Type = 2;
                    break;

                case 2:
                    if (type3.q == null)
                    {
                        goto default;
                    }
                    q      = type3.q;
                    q.Type = 3;
                    break;

                case 3:
                    if (type4.q == null)
                    {
                        goto default;
                    }
                    q      = type4.q;
                    q.Type = 4;
                    break;

                case 4:
                    if (type5.q == null)
                    {
                        goto default;
                    }
                    if (type5.q.C == "0" && (type5.q.A == "" || type5.q.B == ""))
                    {
                        MessageBox.Show("Wypełnij obie listy jeśli nie ma być dubli!");
                        return;
                    }
                    q      = type5.q;
                    q.Type = type5.q.Type;
                    break;

                case 5:
                    if (type6.q == null)
                    {
                        goto default;
                    }
                    q      = type6.q;
                    q.Type = 7;
                    break;

                default:
                    MessageBox.Show("Zaznacz odpowiedzi", "Nie dodano!!!");
                    return;
                }
                if (cbQuestionType.SelectedIndex == 1)
                {
                    //using (Repository r = new Repository(connStr))
                    //{
                    //    q.Image = r.loadImages(q.ID)[0];
                    //    q.ImageAlt = r.loadImages(q.ID)[1];
                    //}
                    q.QType    = 2;
                    q.Image    = pictureBox1.Image;
                    q.ImageAlt = pictureBox2.Image;
                }
                else
                {
                    q.QType        = 1;
                    q.Image        = null;
                    q.ImageText    = null;
                    q.ImageAlt     = null;
                    q.ImageTextAlt = null;
                }

                if (cbQuestionList.SelectedIndex > 0)
                {
                    q.ID = Convert.ToInt16(cbQuestionList.SelectedItem);
                }
                q.question = richTextBox1.Text.Replace(replace1, replace2);
                q.question = q.question.ReplaceSymbolToApostrophe();
                //Poprawianie Tabeli.
                q.question = q.question.Replace("<table border='1'></br>", "<table border='1'>");
                q.question = q.question.Replace("</table></br>", "</table>");
                q.question = q.question.Replace("</td></br>", "</td>");
                q.question = q.question.Replace("</tr></br>", "</tr>");
                q.question = q.question.Replace("</th></br>", "</th>");
                q.question = q.question.Replace("<td></br>", "<td>");
                q.question = q.question.Replace("<tr></br>", "<tr>");
                q.question = q.question.Replace("<th></br>", "<th>");
                q.question = q.question.Replace("</br><table border='1'>", "<table border='1'>");
                q.question = q.question.Replace("</br></table>", "</table>");
                q.question = q.question.Replace("</br></td>", "</td>");
                q.question = q.question.Replace("</br></tr>", "</tr>");
                q.question = q.question.Replace("</br></th>", "</th>");
                q.question = q.question.Replace("</br><td>", "<td>");
                q.question = q.question.Replace("</br><tr>", "<tr>");
                q.question = q.question.Replace("</br><th>", "<th>");
                using (Repository r = new Repository(connStr))
                {
                    bool ok = false;
                    if (cbQuestionList.SelectedIndex == 0)
                    {
                        ok = r.Add(q);
                        cbAnswerType_SelectedIndexChanged(sender, null);
                    }
                    else
                    {
                        ok = r.Update(q);
                    }
                    //if (ok)
                    //    MessageBox.Show("OK");
                    LoadFromDB(cbQuestionList.SelectedIndex);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Coś nie zadziałało, spróbuj ponownie.\n" + exc.Message);
            }
        }
예제 #12
0
파일: QuestionForm.cs 프로젝트: titu84/Exam
        private void QuestionForm_Load(object sender, EventArgs e)
        {
            int qn = getQuestionNumber();

            checkButtons(getId(qn));
            DbQuestion answer = parent.GetAnswer(qn);

            panel1.Size = new System.Drawing.Size(this.Size.Width - 130, this.Size.Height - 170);
            switch (questionType)
            {
            case 1:
                if (answer != null)
                {
                    type1.q = answer;
                }
                Controls.Add(type1);
                type1.Location = new Point(105, panel1.Size.Height + 35);
                type1.Anchor   = AnchorStyles.Bottom | AnchorStyles.Left;
                if (exam)
                {
                    type1.Leave += new System.EventHandler(this.type1_Leave);
                }
                break;

            case 2:
                if (answer != null)
                {
                    type2.q = answer;
                }
                Controls.Add(type2);
                type2.Location = new Point(105, panel1.Size.Height + 35);
                type2.Anchor   = AnchorStyles.Bottom | AnchorStyles.Left;
                type2.TabIndex = 1;
                if (exam)
                {
                    type2.Leave += new System.EventHandler(this.type2_Leave);
                }
                break;

            case 3:
                if (answer != null)
                {
                    type3.q = answer;
                }
                Controls.Add(type3);
                type3.Location = new Point(105, panel1.Size.Height + 35);
                type3.Anchor   = AnchorStyles.Bottom | AnchorStyles.Left;
                //type3.TabIndex = 1;
                if (exam)
                {
                    type3.Leave += new System.EventHandler(this.type3_Leave);
                }
                break;

            case 4:
                panel1.Size     = new System.Drawing.Size(this.Size.Width - 130, this.Size.Height - 200);
                type4q.Location = new Point(105, panel1.Size.Height + 35);
                if (answer != null)
                {
                    type4q.q = answer;
                    type4q.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                }
                else
                {
                    type4q.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                }
                type4q.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                if (exam)
                {
                    type4q.Leave += new System.EventHandler(this.type4q_Leave);
                }
                Controls.Add(type4q);
                break;

            case 5:
                panel1.Size     = new System.Drawing.Size(this.Size.Width - 130, this.Size.Height - 200);
                type5q.Location = new Point(105, panel1.Size.Height + 35);
                if (answer != null)
                {
                    type5q.q = answer;
                    type5q.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                    type5q.a = parent.list.Where(a => a.ID == qn).Select(a => a.A).FirstOrDefault();
                    type5q.b = parent.list.Where(a => a.ID == qn).Select(a => a.B).FirstOrDefault();
                }
                else
                {
                    type5q.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                    type5q.a = parent.list.Where(a => a.ID == qn).Select(a => a.A).FirstOrDefault();
                    type5q.b = parent.list.Where(a => a.ID == qn).Select(a => a.B).FirstOrDefault();
                }
                type5q.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                if (exam)
                {
                    type5q.Leave += new System.EventHandler(this.type5q_Leave);
                }
                Controls.Add(type5q);
                break;

            case 6:
                panel1.Size    = new System.Drawing.Size(this.Size.Width - 130, this.Size.Height - 200);
                type6.Location = new Point(105, panel1.Size.Height + 35);
                if (answer != null)
                {
                    type6.q = answer;
                    type6.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                    type6.a = parent.list.Where(a => a.ID == qn).Select(a => a.A).FirstOrDefault();
                    type6.b = parent.list.Where(a => a.ID == qn).Select(a => a.B).FirstOrDefault();
                }
                else
                {
                    type6.h = parent.list.Where(a => a.ID == qn).Select(a => a.H).FirstOrDefault();
                    type6.a = parent.list.Where(a => a.ID == qn).Select(a => a.A).FirstOrDefault();
                    type6.b = parent.list.Where(a => a.ID == qn).Select(a => a.B).FirstOrDefault();
                }
                type6.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                if (exam)
                {
                    type6.Leave += new System.EventHandler(this.type6_Leave);
                }
                Controls.Add(type6);
                break;

            case 7:
                if (answer != null)
                {
                    type7.q = answer;
                }
                Controls.Add(type7);
                type7.Location = new Point(105, panel1.Size.Height + 35);
                type7.Anchor   = AnchorStyles.Bottom | AnchorStyles.Left;
                type7.TabIndex = 1;
                if (exam)
                {
                    type7.Leave += new System.EventHandler(this.type7_Leave);
                }
                break;

            default:
                break;
            }
            CheckedQuestion c = new CheckedQuestion();

            c.Question = parent.list.Where(a => a.ID == qn).FirstOrDefault();
            //if(parent.list.Where(a=>a.ID==qn).Select(a=>a.QType).FirstOrDefault()==2)
            //    enableBtns(false);
            //else
            //    enableBtns(true);
            goodAnwser = c.GetGoodAnwser();
        }