//Insert information entered in the pnlQuestionDetails into the question array when it is a questionObjectComplex private void applyChangesToQuesitonComplex(questionObjectComplex question) { question = new questionObjectComplex(); question.questionText = pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("questionText", true)[0].Text; question.questionType = questionEdit.GetChildAtPoint(new Point(68, 29)).Text; question.questionNumber = pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("questionNum", true)[0].Text; question.weight = int.Parse(pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("weight", true)[0].Text); question.questionItems = new List <itemContainer>(); int panelNumber = 0; foreach (Panel c in pnlQuestionDetails.Controls) { if (c.Name != "questionEdit") { //Go through container panels and create new container based off the panel name (is number) itemContainer tempContainer = new itemContainer(); tempContainer.itemContainerLabel = c.Controls[1].Text; tempContainer.items = new List <questionItemBase>(); question.questionItems.Add(tempContainer); //Go through each item panel, then add item to corresponding container object. for (int t = 0; t < (c.Controls.Count - 4); t++) { var item = c.Controls[t + 4]; questionItemBase tempItem = new questionItemBase(); tempItem.itemText = item.Controls[1].Text; question.questionItems[panelNumber - 1].items.Add(tempItem); } } panelNumber++; } game[(int)lastSelected.Tag] = question; }
private void btnGameGenerate_Click(object sender, EventArgs e) { applyAll(); string colour = null; string gameN = null; string filename = ""; Form2 gameName = new Form2(); //Show testDialog as a modal dialog and determine if DialogResult = OK. if (gameName.ShowDialog(this) == DialogResult.OK) { //Read the contents of gameName's TextBox. gameN = gameName.txtName.Text; filename = gameName.txtName.Text + ".html"; } else { return; } //Destroy the second form from memory because there is no reason gameName.Dispose(); string directory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string path = Path.Combine(directory, filename); DialogResult = MessageBox.Show("Would you like to use a custom colour scheme?", "Change colour:", MessageBoxButtons.YesNo); if (DialogResult == DialogResult.Yes) { //Allow for changing of main colour colourPicker.ShowDialog(); var colourChoice = colourPicker.Color; colour = ColorTranslator.ToHtml(colourChoice); } //Variables to check which game type frameworks should be connected to. bool dnd = false; bool mc = false; bool popup = false; //Go through each question within the game and trigger the correct framework variables foreach (var question in game) { if (question.Value is questionObjectSimple) { mc = true; //Check for if game uses popups in any of the questions. If it does, will include popup plugin. for (int f = 0; f < question.Value.questionItems.Count; f++) { if (question.Value.questionItems[f].popups.popupEnabled == true) { popup = true; } } } else if (question.Value is questionObjectComplex) { dnd = true; } } //Using StringBuilder instead of just a string or the original write, due to performance gains. When modifying a string repeatedly, a lot of overhead is used with realocating the memory. StringBuilder does //not deal with this issue and therefore saves time. The reason I am changing to just a single write vs multiple is due to errors. This way I can return out of the function without having a half completed file //saved to disk. //Setting the default max capacity to around 1500 as a start and it will automatically double when capacity is reached. Just to make sure that it is not constantly allocating more memory. StringBuilder documentContent = new StringBuilder("<!DOCTYPE html>", 1500); documentContent.Append("<html lang=\"en\">"); documentContent.Append("<head>"); documentContent.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"); documentContent.Append("<link rel=\"stylesheet\" href=\"https://mylearningspace.wlu.ca/shared/root/tna.css\">"); //if a question is a drag n drop then link the framework if (dnd == true) { documentContent.Append("<link rel=\"stylesheet\" href=\"https://mylearningspace.wlu.ca/shared/root/dnd.css\">"); } //if a question is a multiple choice then link the appropriate framework if (mc == true) { if (popup == true) { documentContent.Append("<link rel=\"stylesheet\" href=\"https://mylearningspace.wlu.ca/shared/root/pop.css\">"); } } if (colour != null) { documentContent.Append("<style>#header,.item,.option,.question{background:" + colour + "}.button{color:" + colour + "}</style>"); } documentContent.Append("<script src=\"https://mylearningspace.wlu.ca/shared/root/jsf.js\" type=\"text/javascript\" async></script>"); //Writting Javascript underneath the css, this is why I check the booleans twice. if (dnd == true) { documentContent.Append("<script src=\"https://mylearningspace.wlu.ca/shared/root/dnf.js\" type=\"text/javascript\" async></script>"); } if (mc == true) { documentContent.Append("<script src=\"https://mylearningspace.wlu.ca/shared/root/mpf.js\" type=\"text/javascript\" asnyc></script>"); } documentContent.Append("<script>"); bool multiQ = false; string questionArray = ""; int maxScore = 0; int i = 1; List <questionItemSimple> simpleOptions = new List <questionItemSimple>(); List <questionItemBase> complexOptions = new List <questionItemBase>(); string options = ""; string itemDrops = ""; string popupItems = ""; string answers = ""; int nextQuestion; if (game.Count > 1) { multiQ = true; } foreach (var question in game) { options = ""; popupItems = ""; answers = ""; nextQuestion = i; if (i != 1) { questionArray += ", "; } //q stands for question questionArray += "q" + i; if (question.Value is questionObjectSimple) { documentContent.Append("function q" + i + "() {"); documentContent.Append("question = {};"); for (int c = 0; c < question.Value.questionItems.Count; c++) { //Add correct items to the answers string if (question.Value.questionItems[c].correct == true) { maxScore++; if (answers == "") { //o for options answers += "o[" + (c) + "]"; } else { answers += ", o[" + (c) + "]"; } } //Add the items in the question to the options list if (options == "") { options += "\"" + question.Value.questionItems[c].itemText + "\""; } else { options += ", \"" + question.Value.questionItems[c].itemText + "\""; } } documentContent.Append("var o = [" + options + "];"); //Enable popups and include popup items for (int y = 0; y < question.Value.questionItems.Count; y++) { if (question.Value.questionItems[y].popups.popupEnabled == true) { popup = true; if (popupItems != "") { popupItems += " ,"; } popupItems += "[\"" + question.Value.questionItems[y].popups.popupTitle + "\", \"" + question.Value.questionItems[y].popups.popupBody + "\"]"; } } documentContent.Append("var popupItems = [" + popupItems + "];"); documentContent.Append("var answer = " + answers + ";"); documentContent.Append("enablePopups(" + popup.ToString().ToLower() + ");"); if (i == game.Count()) { nextQuestion = -1; } documentContent.Append("question = {nextQIndex: " + nextQuestion + ", question: '" + question.Value.questionText + "', answer: answer, options: o, popup: " + popup.ToString().ToLower() + ", popupItems: popupItems};"); documentContent.Append("if (currentGameSession.scoreWeights.length >= question.nextQIndex){"); documentContent.Append("currentGameSession.score[question.nextQIndex] =0;"); documentContent.Append("currentGameSession.scoreWeights[question.nextQIndex] =" + question.Value.weight + ";"); documentContent.Append("currentGameSession.maxScore[question.nextQIndex] =" + question.Value.weight + ";}"); documentContent.Append("else {"); documentContent.Append("currentGameSession.score.push(0);"); documentContent.Append("currentGameSession.scoreWeights.push(" + question.Value.weight + ");"); documentContent.Append("currentGameSession.maxScore.push(" + question.Value.weight + ");"); documentContent.Append("}"); documentContent.Append("multiStart();"); documentContent.Append("}"); } else if (question.Value is questionObjectComplex) { options = ""; answers = ""; itemDrops = ""; documentContent.Append("function q" + i + "() {"); documentContent.Append("question = {};"); for (int j = 0; j < question.Value.questionItems.Count; j++) { //reshuffle complexOptions list to randomize the listings in the game List <questionItemBase> complexOptionsTemp = new List <questionItemBase>(); try { for (int o = 0; o < question.Value.questionItems[j].items.Count; o++) { //Add itembox values into main item pool complexOptions.Add(question.Value.questionItems[j].items[o]); } } catch (Exception) { errorHandle("Cannot have a Container without items inside it. Sorry!"); return; } } //Randomize the items in complexOptions so that the drag and drop doesn't display the options in the correct order. Random rng = new Random(); int n = complexOptions.Count; while (n > 1) { n--; int k = rng.Next(0, n); questionItemBase value = complexOptions[k]; complexOptions[k] = complexOptions[n]; complexOptions[n] = value; } int tempararyCounter = 0; foreach (var item in complexOptions) { if (tempararyCounter != 0) { options += ", "; } options += "\"" + item.itemText + "\""; tempararyCounter++; } for (int y = 0; y < question.Value.questionItems.Count; y++) { //create item drops in same loop itemDrops += "'" + question.Value.questionItems[y].itemContainerLabel + "'"; //if not the last item, place a comma to separate the items if (y != question.Value.questionItems.Count - 1) { itemDrops += ", "; } answers += "answers[" + y + "] = ["; for (int r = 0; r < question.Value.questionItems[y].items.Count; r++) { for (int q = 0; q < complexOptions.Count; q++) { //Compare each correct item to all items in the list to match the indexes if (complexOptions[q] == question.Value.questionItems[y].items[r]) { answers += "[options[" + complexOptions.IndexOf(complexOptions[q]) + "]]"; if (r != (question.Value.questionItems[y].items.Count - 1)) { answers += ", "; } else { answers += "];"; } } } } } if (i == game.Count) { nextQuestion = -1; } maxScore = complexOptions.Count; documentContent.Append("var options = [" + options + "];"); documentContent.Append("var itemDrops = [" + itemDrops + "];"); documentContent.Append("var answers = new Array(2);"); documentContent.Append(answers); documentContent.Append("question = {nextQIndex: " + nextQuestion + ", question: '" + question.Value.questionText + "', answer: answers, options: options, itemDrops: itemDrops};"); documentContent.Append("if (currentGameSession.scoreWeights.length >= question.nextQIndex){"); documentContent.Append("currentGameSession.score[question.nextQIndex] =0;"); documentContent.Append("currentGameSession.scoreWeights[question.nextQIndex] =" + question.Value.weight + ";"); documentContent.Append("currentGameSession.maxScore[question.nextQIndex] =" + question.Value.weight + ";}"); documentContent.Append("else {"); documentContent.Append("currentGameSession.score.push(0);"); documentContent.Append("currentGameSession.scoreWeights.push(" + question.Value.weight + ");"); documentContent.Append("currentGameSession.maxScore.push(" + question.Value.weight + ");"); documentContent.Append("}"); documentContent.Append("dndStart();}"); } i++; } documentContent.Append("window.onload = function startupConfiguration() {currentGameSession.questionArray = [" + questionArray + "];setMaxScore(" + maxScore + ", false);headerSetup('" + gameN + "', " + multiQ.ToString().ToLower() + ");q1();}"); documentContent.Append("</script>"); documentContent.Append("<title id=\"title\">Questions</title>"); documentContent.Append("</head>"); documentContent.Append("<body>"); documentContent.Append("<header id=\"header\">"); documentContent.Append("<h1 id=\"gameName\"></h1>"); documentContent.Append("<p id=\"score\"></p>"); documentContent.Append("</header>"); documentContent.Append("<span id=\"spacing\"></span>"); documentContent.Append("<section id=\"questionArea\" class=\"slideDown\">"); documentContent.Append("<header id=\"question\" class=\"question\"></header>"); documentContent.Append("<div id=\"options\"></div>"); documentContent.Append("</section>"); documentContent.Append("</body>"); documentContent.Append("</html>"); using (var w = new StreamWriter(path)) { w.Write(documentContent); } }
//Insert information entered in the pnlQuestionDetails into the question array when it is a questionObjectComplex private void applyChangesToQuesitonComplex(questionObjectComplex question) { question = new questionObjectComplex(); question.questionText = pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("questionText", true)[0].Text; question.questionType = questionEdit.GetChildAtPoint(new Point(68, 29)).Text; question.questionNumber = pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("questionNum", true)[0].Text; question.weight = int.Parse(pnlQuestionDetails.Controls.Find("questionEdit", true)[0].Controls.Find("weight", true)[0].Text); question.questionItems = new List<itemContainer>(); int panelNumber = 0; foreach (Panel c in pnlQuestionDetails.Controls) { if (c.Name != "questionEdit") { //Go through container panels and create new container based off the panel name (is number) itemContainer tempContainer = new itemContainer(); tempContainer.itemContainerLabel = c.Controls[1].Text; tempContainer.items = new List<questionItemBase>(); question.questionItems.Add(tempContainer); //Go through each item panel, then add item to corresponding container object. for (int t = 0; t < (c.Controls.Count - 4); t++) { var item = c.Controls[t + 4]; questionItemBase tempItem = new questionItemBase(); tempItem.itemText = item.Controls[1].Text; question.questionItems[panelNumber - 1].items.Add(tempItem); } } panelNumber++; } game[(int)lastSelected.Tag] = question; }