public ImageDialog(Quiz quiz)
        {
            InitializeComponent();

            // Store the supplied quiz
            this.quiz = quiz;
        }
        public QuizAnswerer(Quiz quiz)
        {
            InitializeComponent();

            // Store the supplied quiz
            this.quiz = quiz;

            // Create an array to store question answer state in
            correctAnswers = new bool[quiz.Questions.Count];

            // Randomize question order
            // Makes use of LINQ to create a range of question indexes, and then order them according to a random number generator
            Random rnd = new Random();
            questionOrder = Enumerable.Range(0, quiz.Questions.Count).OrderBy<int, int>((item) => rnd.Next()).ToArray<int>();

            // Initialize the question index
            questionIndex = -1;
        }
        /// <summary>
        /// Sets up a new quiz and adds a blank question to it
        /// </summary>
        private void setupNewQuiz()
        {
            SelectedQuiz = new Quiz();
            SelectedQuestion = SelectedQuiz.AddQuestion();

            updateQuestionList();
            selectQuestion(0);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Stores a quiz to a zip file
        /// </summary>
        /// <param name="quiz">The quiz to store</param>
        /// <param name="path">The path of the zip file</param>
        public static void StoreToZip(Quiz quiz, String path)
        {
            // Create a backup of the quiz if it already exists, in case something goes wrong
            Boolean cleanup = false;
            string backup = path + StringResources.MISC_BACKUP_EXTENSION;
            if (File.Exists(path))
            {
                // Delete existing backup
                // Shouldn't exist, but just in case it might
                if (File.Exists(backup))
                    File.Delete(backup);

                // Rename file
                File.Move(path, backup);
                cleanup = true;
            }

            try
            {
                // Open the zip file
                using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Create))
                {
                    // Rebuild image references and save images
                    quiz.ImageReferences.Clear();
                    for (int i = 0; i < quiz.Images.Count; i++)
                    {
                        Image image = quiz.Images[i];
                        ImageReference reference = new ImageReference { Path = String.Format(StringResources.PATH_IMAGE, i)};
                        quiz.ImageReferences.Add(reference);

                        reference.SaveImage(image, archive);
                    }

                    // Rebuild question references and save questions
                    quiz.QuestionReferences.Clear();
                    for (int i = 0; i < quiz.Questions.Count; i++)
                    {
                        Question question = quiz.Questions[i];
                        QuestionReference reference = new QuestionReference { Path = String.Format(StringResources.PATH_QUESTION, i) };
                        quiz.QuestionReferences.Add(reference);

                        reference.SaveQuestion(question, archive);
                    }

                    // Create file in archive
                    ZipArchiveEntry quizEntry = archive.CreateEntry(StringResources.PATH_QUIZ);
                    using (Stream stream = quizEntry.Open())
                    {
                        // Serialize the Quiz object to the file
                        XmlSerializer x = new XmlSerializer(typeof(Quiz));
                        x.Serialize(stream, quiz);
                    }
                }
            }
            catch(Exception e)
            {
                // On error, restore backup
                if (cleanup && File.Exists(backup))
                {
                    File.Move(backup, path);
                    File.Delete(backup);

                    return;
                }

                // Then show an error message
                MessageBox.Show(String.Format(StringResources.ERROR_SAVE_QUIZ, e.ToString()));
            }

            // Delete backup if all goes well
            if (cleanup && File.Exists(backup))
            {
                File.Delete(backup);
                return;
            }
        }