/// <summary> /// Gets Survey from database by Survey ID /// </summary> /// <param name="surveyID">Survey ID that tells which Survey to get</param> /// <returns></returns> public static Survey GetSurvey(int surveyID) { if (!isConnected) { Init(); } SQLiteCommand sqliteCommand = dbConnection.CreateCommand(); sqliteCommand.CommandText = "SELECT * FROM " + SURVEYS_TABLE + " WHERE id=:id"; sqliteCommand.Parameters.AddWithValue(":id", surveyID.ToString()); SQLiteDataReader sqliteSurvey = sqliteCommand.ExecuteReader(); Survey survey = new Survey(); if (!sqliteSurvey.HasRows) { return survey; } survey.Id = Convert.ToInt32(sqliteSurvey["id"]); survey.Name = sqliteSurvey["name"].ToString(); survey.TestMode = Convert.ToBoolean(sqliteSurvey["testmode"]); survey.MinScore = Convert.ToDouble(sqliteSurvey["minscore"]); survey.Polls = new List<Poll>(); sqliteSurvey.Close(); sqliteCommand.CommandText = "SELECT p.* FROM " + SURVEY_POLLS_TABLE + " pxp LEFT JOIN " + POLLS_TABLE + " p ON (pxp.poll_id=p.id) WHERE pxp.survey_id=:id"; SQLiteDataReader sqlPolls = sqliteCommand.ExecuteReader(); SQLiteCommand sqliteCommand2 = dbConnection.CreateCommand(); sqliteCommand2.CommandText = "SELECT c.* FROM " + POLL_CHOICES_TABLE + " pxc LEFT JOIN " + CHOICES_TABLE + " c ON (pxc.choice_id=c.id) WHERE pxc.poll_id=:id"; sqliteCommand2.Parameters.Add(new SQLiteParameter(":id")); while (sqlPolls.Read()) { Poll newPoll = new Poll(sqlPolls["name"].ToString()); newPoll.Id = Convert.ToInt32(sqlPolls["id"]); newPoll.Description = sqlPolls["description"].ToString(); newPoll.CorrectChoiceID = Convert.ToInt32(sqlPolls["correctchoice"]); newPoll.CustomChoiceEnabled = Convert.ToBoolean(sqlPolls["customenabled"]); sqliteCommand2.Parameters[":id"].Value = newPoll.Id; SQLiteDataReader sqlChoices = sqliteCommand2.ExecuteReader(); while (sqlChoices.Read()) { Choice newChoice = new Choice(sqlChoices["name"].ToString()); newChoice.Id = Convert.ToInt32(sqlChoices["id"]); newPoll.Choices.Add(newChoice); } sqlChoices.Close(); survey.Polls.Add(newPoll); } sqlPolls.Close(); return survey; }
public static void RunUserPoll() { foreach (Poll curentPoll in pollSession.polls) { // Clear screen and write this poll's question Console.Clear(); Console.WriteLine(curentPoll.name + "\n" + curentPoll.description); // List choices for this poll int index = 1; foreach (Choice currentChoice in curentPoll.choices) { Console.WriteLine("\t" + index + ". " + currentChoice.choice); ++ index; } // add option for customer choice if ( curentPoll.customChoice ) { Console.WriteLine("\t" + index + ". Custom Choice"); } // accept only correct choices while ( true ) { int choiceCount = (curentPoll.customChoice ? curentPoll.choices.Count + 1 : curentPoll.choices.Count); Console.Write("Pick your choice: [1-" + choiceCount + "]:"); // Get user choice try { index = Convert.ToInt32(Console.ReadLine()); --index; // check if input correct bool choiceInputIsAcceptable = (index >= 0 && index <= curentPoll.choices.Count - (curentPoll.customChoice ? 0 : 1)); if (choiceInputIsAcceptable) break; } catch( Exception ) { } Console.WriteLine("Invalid choice!"); } // check if custom choice bool ifCustomChoice = (index == curentPoll.choices.Count); if (ifCustomChoice) { Console.Write("Enter your choice:" ); // create custom choice Choice userChoice = new Choice(); userChoice.choice = Console.ReadLine(); userChoice.id = 0; userChoice.parent = curentPoll; // add custom choice to list userChoices.Add(userChoice); } else { // add one of the choices that already exist to list userChoices.Add(curentPoll.choices[index]); } } // go through choices and display them Console.Clear(); Console.WriteLine(userName + ", here is your PollSession results:"); bool isTestMode = pollSession.testMode; foreach(Choice userChoice in userChoices) { Console.WriteLine(userChoice.parent.id + ". " + userChoice.parent.name + ": " + userChoice.choice); if (isTestMode) { // Get correctChoice by id string correctChoice = ""; foreach (Choice curChoice in userChoice.parent.choices) { if (curChoice.id == userChoice.parent.correctChoiceId) correctChoice = curChoice.choice; } string isChoicePassed = (userChoice.parent.correctChoiceId == userChoice.id ? "+ Correct" : "- Wrong"); Console.WriteLine("Correct choice: " + correctChoice); Console.WriteLine(isChoicePassed + "\n"); } } if (isTestMode) { // Get correct answers count int correctAnswersCount = 0; foreach (Choice userChoice in userChoices) { if (userChoice.id == userChoice.parent.correctChoiceId) correctAnswersCount++; } // Check if is passed double userScore = Convert.ToDouble(correctAnswersCount) / Convert.ToDouble(userChoices.Count); bool isPassed = (userScore >= pollSession.minScore); if (isPassed) { Console.WriteLine("PASSED"); } else { Console.WriteLine("Sorry, try again..."); } Console.WriteLine("Your scores: " + userScore); Console.WriteLine("Minimal needed scores: " + pollSession.minScore); } }
/// <summary> /// Gets Poll from database by poll ID /// </summary> /// <param name="surveyID">Poll ID that tells which poll to get</param> /// <returns></returns> public static Poll GetPoll(int pollID) { if (!isConnected) { Init(); } SQLiteCommand sqlitePollCommand = dbConnection.CreateCommand(); sqlitePollCommand.Parameters.AddWithValue(":id", pollID.ToString()); sqlitePollCommand.CommandText = "SELECT * FROM " + POLLS_TABLE + " WHERE id=:id"; SQLiteDataReader sqlitePoll = sqlitePollCommand.ExecuteReader(); Poll poll = new Poll(); if (!sqlitePoll.HasRows) { return poll; } poll.Id = pollID; poll.Description = sqlitePoll["description"].ToString(); SQLiteCommand sqliteChoicesCommand = dbConnection.CreateCommand(); sqliteChoicesCommand.CommandText = "SELECT c.* FROM " + POLL_CHOICES_TABLE + " pxc LEFT JOIN " + CHOICES_TABLE + " c ON (pxc.choice_id=c.id) WHERE pxc.poll_id=:id"; sqliteChoicesCommand.Parameters.AddWithValue(":id", poll.Id); SQLiteDataReader sqliteChoices = sqliteChoicesCommand.ExecuteReader(); while (sqliteChoices.Read()) { Choice newChoice = new Choice(sqliteChoices["name"].ToString()); newChoice.Id = Convert.ToInt32(sqliteChoices["id"]); poll.Choices.Add(newChoice); } sqliteChoices.Close(); return poll; }
public static void ParseXml(string xmlData) { if (xmlData[0] == '!') { Console.WriteLine(xmlData); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); Environment.Exit(-1); } //---------------Init--------------- List<Poll> pollDoc = new List<Poll>(); XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(xmlData); } catch(Exception) { Console.WriteLine("Corrupt xml data sent by server!"); Console.ReadKey(true); Environment.Exit(-1); } System.Globalization.CultureInfo cultureInfo = (System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture.Clone(); cultureInfo.NumberFormat.NumberDecimalSeparator = "."; XmlNodeList xmlPollSessionList = xmlDoc.GetElementsByTagName(POLL_SESSION_ELEMENT); // Get pollSessions list foreach (XmlNode xmlPollSession in xmlPollSessionList) { pollSession.id = Convert.ToInt32(xmlPollSession.Attributes["id"].Value); pollSession.name = xmlPollSession.Attributes["name"].Value; pollSession.testMode = Convert.ToBoolean(xmlPollSession.Attributes["testMode"].Value); pollSession.minScore = Convert.ToDouble(xmlPollSession.Attributes["minScore"].Value, cultureInfo); // Get polls list foreach (XmlNode xmlPoll in xmlPollSession.ChildNodes) { Poll curPoll = new Poll(); XmlAttributeCollection xmlAttr = xmlPoll.Attributes; // Get current poll id curPoll.id = Convert.ToInt32(xmlAttr["id"].Value); // Get current poll name curPoll.name = xmlAttr["name"].Value; // Get current poll customChoiceEnabled option if (xmlAttr["customChoiceEnabled"] != null) curPoll.customChoice = Convert.ToBoolean(xmlAttr["customChoiceEnabled"].Value); // Get correct choice in current Poll curPoll.correctChoiceId = Convert.ToInt32(xmlAttr["correctChoice"].Value); // Get choices list foreach( XmlNode node in xmlPoll.ChildNodes ) { if (node.Name == "choices") { // Get list of choices of current Poll foreach (XmlNode xmlChoice in node.ChildNodes) { Choice curChoice = new Choice(); XmlAttributeCollection xmlAttrChoice = xmlChoice.Attributes; // Get current choice id curChoice.id = Convert.ToInt32(xmlAttrChoice["id"].Value); // Get current choice name curChoice.choice = xmlAttrChoice["name"].Value; // Save current poll in current choice for future convenience curChoice.parent = curPoll; curPoll.choices.Add(curChoice); } } else if (node.Name == "description") { // Get current Poll description curPoll.description = node.InnerText; } } pollSession.polls.Add(curPoll); } } Console.WriteLine("XML parsed"); }