Пример #1
0
        /// <summary>
        /// Handle when user clicks edit button
        /// </summary>
        private async void ButtonEdit_Clicked(string DBId)
        {
            QuizInfo info = QuizRosterDatabase.GetQuizInfo(DBId);

            if (!CredentialManager.IsLoggedIn)
            {
                await this.DisplayAlert("Hold on!", "Before you can edit any quizzes, you have to login.", "Ok");
            }
            else if (info.SyncStatus == (int)SyncStatusEnum.NotDownloadedAndNeedDownload)
            {
                await this.DisplayAlert("Hold on!", "This quiz isn't on your device, download it before you try to edit it", "Ok");
            }
            else
            {
                CreateNewQuizPage quizPage = new CreateNewQuizPage(info); //Create the quizPage

                quizPage.SetQuizName(info.QuizName);
                Quiz quizDB = new Quiz(info.DBId);
                foreach (Question question in quizDB.GetQuestions())
                {
                    quizPage.AddNewQuestion(question);
                }
                await this.Navigation.PushAsync(quizPage);
            }
        }
Пример #2
0
        /// <summary>
        /// Saves the user created quiz
        /// </summary>
        /// <param name="sender">  </param>
        /// <param name="e">       </param>
        private async void ButtonCreateQuiz_Clicked(object sender, EventArgs e)
        {
            this.Done.IsEnabled = false;
            if (string.IsNullOrWhiteSpace(this.EditorQuizName.Text))
            {
                await this.DisplayAlert("Couldn't Create Quiz", "Please give your quiz a name.", "OK");
            }
            else if (this.StackLayoutQuestionStack.Children.Count < 2)
            {
                await this.DisplayAlert("Couldn't Create Quiz", "Please create at least two questions", "OK");
            }
            else if (this.PickerCategory.SelectedIndex == -1)
            {
                await this.DisplayAlert("Couldn't Create Quiz", "Please give your quiz a category", "OK");
            }
            else
            {
                List <Question> previousQuestions = new List <Question>();

                List <Question> NewQuestions = new List <Question>();  // A list of questions the user wants to add to the database

                // Loops through each question frame on the screen
                foreach (Frame frame in this.StackLayoutQuestionStack.Children)
                {
                    // A list of all the children of the current frame
                    IList <View> children = ((StackLayout)frame.Content).Children;

                    Question addThis;
                    //The answers to the question
                    string[] answers = { ((Editor)children[2]).Text?.Trim(),   //Correct answer
                                         ((Editor)children[3]).Text?.Trim(),   // Incorect answer
                                         ((Editor)children[4]).Text?.Trim(),   // Incorect answer
                                         ((Editor)children[5]).Text?.Trim() }; // Incorect answer

                    // Checks if there is a question set
                    if (string.IsNullOrWhiteSpace(((Editor)children[1]).Text))
                    {
                        await this.DisplayAlert("Couldn't Create Quiz", "Every question must have a question set", "OK");

                        goto exit;
                    }

                    if (((ImageButton)children[6]).IsVisible) // if the question needs an image
                    {
                        addThis = new Question(
                            ((Editor)children[1]).Text?.Trim(),                        // The Question
                            ((ImageButton)children[6]).Source.ToString().Substring(6), // adds image using the image source
                            answers)
                        {
                            NeedsPicture = true
                        };
                    }
                    else // if the question does not need an image
                    {
                        addThis = new Question(
                            ((Editor)children[1]).Text?.Trim(),
                            answers);
                    }

                    string questionType = ((Button)((StackLayout)children[0]).Children[0]).Text;

                    // Sets the question type

                    if (questionType == "Question Type: Multiple choice")
                    {
                        int size = 0;
                        foreach (string answer in answers)
                        {
                            if (!string.IsNullOrWhiteSpace(answer))
                            {
                                size++;
                            }
                        }
                        if (size < 2 || string.IsNullOrWhiteSpace(answers[0]))
                        {
                            await this.DisplayAlert("Couldn't Create Quiz", "Mulitple choice questions must have a correct answer and at least one wrong answer", "OK");

                            goto exit;
                        }

                        addThis.QuestionType = 0;
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(answers[0]))
                        {
                            await this.DisplayAlert("Couldn't Create Quiz", "Text answer questions must have an answer", "OK");

                            goto exit;
                        }

                        if (questionType == "Question Type: Text answer")
                        {
                            addThis.QuestionType = 1;
                        }
                        else
                        {
                            addThis.QuestionType = 2;
                        }
                    }

                    addThis.QuestionId = frame.StyleId; // Set the questionId

                    NewQuestions.Add(addThis);
                }

                Quiz database;
                // If editing their own quiz
                if (this.originalQuizInfo != null && this.originalAuthor == CredentialManager.Username)
                {
                    database = new Quiz(this.originalQuizInfo.DBId);

                    // Set previousQuestions to the correct previous questions
                    previousQuestions = database.GetQuestions();
                }
                else // If new quiz
                {
                    database = new Quiz(
                        CredentialManager.Username,
                        this.EditorQuizName.Text.Trim(),
                        this.PickerCategory.Items[this.PickerCategory.SelectedIndex]);
                }

                // Add if it doesn't already exist, delete if it doesn't exist anymore, update the ones that need to be updated, and do nothing to the others
                if (previousQuestions.Count == 0 || originalAuthor != CredentialManager.Username)
                {
                    // if the user created this for the first time

                    // Save a new QuizInfo into the quiz database, which also adds this QuizInfo to the device quiz roster
                    database.AddQuestions(NewQuestions.ToArray());
                }
                else // edit
                {
                    string   currentDirectory = database.DBFolderPath;
                    QuizInfo updatedQuizInfo  = new QuizInfo(database.QuizInfo)
                    {
                        QuizName         = this.EditorQuizName.Text?.Trim(),
                        LastModifiedDate = DateTime.Now.ToString(),
                        Category         = this.PickerCategory.Items[this.PickerCategory.SelectedIndex]
                    };
                    database.EditQuizInfo(updatedQuizInfo);
                    if (currentDirectory != updatedQuizInfo.RelativePath)
                    {
                        Directory.CreateDirectory(updatedQuizInfo.RelativePath);
                        Directory.Delete(updatedQuizInfo.RelativePath, true);
                        Directory.Move(currentDirectory, updatedQuizInfo.RelativePath);
                    }

                    // Logic for how to save each question.
                    for (int i = 0; i <= previousQuestions.Count() - 1; i++)
                    {
                        bool DBIdSame = true;
                        // test each old question with each new question
                        foreach (Question newQuestion in NewQuestions)
                        {
                            if (previousQuestions[i].QuestionId == newQuestion.QuestionId)
                            {
                                DBIdSame = true;
                                // the same question, but changed, so update
                                database.EditQuestion(newQuestion);
                                NewQuestions.Remove(newQuestion);
                                break;
                            }
                            else
                            {
                                DBIdSame = false;
                            }
                        }

                        if (!DBIdSame) // if the question doesn't exist in the new list. delete it
                        {
                            database.DeleteQuestions(previousQuestions[i]);
                        }
                    }

                    // Add all the questions that aren't eddited
                    database.AddQuestions(NewQuestions.ToArray());
                }

                File.Create(database.DBFolderPath + ".nomedia");

                // Returns user to front page of QuizEditor and refreshed database
                await this.Navigation.PopAsync(true);
            }
            exit :;
            this.Done.IsEnabled = true;
        }