Exemplo n.º 1
0
        /// <summary>
        /// Outputs answer sheet selection in csv format delimited by a semicolon.
        /// </summary>
        private void OutputAnswerSheetSelection(StreamWriter sw, IQueryable <AnswerSheetSelection> answerSheetSelectionQuery)
        {
            foreach (var answerSheetSelection in answerSheetSelectionQuery)
            {
                KlokanDBInstance             currentInstance = answerSheetSelection.Instance;
                List <KlokanDBChosenAnswer>  chosenAnswers   = new List <KlokanDBChosenAnswer>(answerSheetSelection.ChosenAnswers);
                List <KlokanDBCorrectAnswer> correctAnswers  = new List <KlokanDBCorrectAnswer>(currentInstance.CorrectAnswers);

                sw.Write(answerSheetSelection.AnswerSheetId + ";");
                sw.Write(answerSheetSelection.StudentNumber + ";");
                sw.Write(currentInstance.Year + ";");
                sw.Write(currentInstance.Category + ";");
                sw.Write(answerSheetSelection.Points + ";");

                // relies on the order of answers in the database...
                for (int i = 0; i < 24; i++)
                {
                    sw.Write(chosenAnswers[i].Value + ";" + correctAnswers[i].Value + ";");
                }

                sw.WriteLine();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extract answer sheet data from the database and display it in the form.
        /// Returns false if data could not be loaded.
        /// </summary>
        private bool PopulateForm()
        {
            using (var db = new KlokanDBContext())
            {
                // load sheet data
                var sheetQuery = from sheet in db.AnswerSheets
                                 where sheet.AnswerSheetId == answerSheetId
                                 select sheet;

                KlokanDBAnswerSheet answerSheet = sheetQuery.FirstOrDefault();
                if (answerSheet == null)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextSheetNotFoundInDatabase, Properties.Resources.ErrorCaptionGeneral,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                var instanceQuery = from instance in db.Instances
                                    where instance.InstanceId == answerSheet.InstanceId
                                    select instance;

                KlokanDBInstance currentInstance = instanceQuery.FirstOrDefault();
                if (answerSheet == null)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextSheetNotFoundInDatabase, Properties.Resources.ErrorCaptionGeneral,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                studentNumber = answerSheet.StudentNumber;

                // show sheet data
                studentNumberValueLabel.Text = answerSheet.StudentNumber.ToString();
                idValueLabel.Text            = answerSheet.AnswerSheetId.ToString();
                yearValueLabel.Text          = currentInstance.Year.ToString();
                categoryValueLabel.Text      = currentInstance.Category.ToString();
                pointsValueLabel.Text        = answerSheet.Points.ToString();

                // load scan
                scanPictureBox.Image = ImageHandling.GetBitmap(answerSheet.Scan);

                // load answers and draw them
                var chosenAnswersQuery = from chosenAnswer in db.ChosenAnswers
                                         where chosenAnswer.AnswerSheetId == answerSheetId
                                         select chosenAnswer;

                var chosenAnswersList = chosenAnswersQuery.ToList();
                if (chosenAnswersList.Count == 0)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextSheetNotFoundInDatabase, Properties.Resources.ErrorCaptionGeneral,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                TableArrayHandling.DbSetToAnswers(chosenAnswersList, out chosenAnswers);

                FormTableHandling.DrawAnswers(table1PictureBox, chosenAnswers, 0, FormTableHandling.DrawCross, Color.Black);
                FormTableHandling.DrawAnswers(table2PictureBox, chosenAnswers, 1, FormTableHandling.DrawCross, Color.Black);
                FormTableHandling.DrawAnswers(table3PictureBox, chosenAnswers, 2, FormTableHandling.DrawCross, Color.Black);

                var correctAnswersQuery = from correctAnswer in db.CorrectAnswers
                                          where correctAnswer.InstanceId == answerSheet.InstanceId
                                          select correctAnswer;

                var correctAnswersList = correctAnswersQuery.ToList();
                if (correctAnswersList.Count == 0)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextSheetNotFoundInDatabase, Properties.Resources.ErrorCaptionGeneral,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                TableArrayHandling.DbSetToAnswers(correctAnswersQuery.ToList(), out correctAnswers);

                FormTableHandling.DrawAnswers(table1PictureBox, correctAnswers, 0, FormTableHandling.DrawCircle, Color.Red);
                FormTableHandling.DrawAnswers(table2PictureBox, correctAnswers, 1, FormTableHandling.DrawCircle, Color.Red);
                FormTableHandling.DrawAnswers(table3PictureBox, correctAnswers, 2, FormTableHandling.DrawCircle, Color.Red);
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously stores results into a database described by KlokanDBContext.
        /// Instances that already exist in the database are rewritten.
        /// </summary>
        /// <param name="results">Any enumerable structure of evaluation results.</param>
        /// <returns>A void task.</returns>
        async Task OutputResultsDB(IEnumerable <Result> results)
        {
            using (var db = new KlokanDBContext())
            {
                foreach (var result in results)
                {
                    if (result.Error == true)
                    {
                        failedSheets++;
                        continue;
                    }

                    // find out if the instance this result belongs to is new or if it already exists
                    var query = from instance
                                in db.Instances
                                where instance.Year == result.Year && instance.Category == result.Category
                                select instance;

                    KlokanDBInstance currentInstance = query.FirstOrDefault();

                    // if the instance isn't saved in the database
                    if (currentInstance == default(KlokanDBInstance))
                    {
                        // try to search locally too (maybe the instance was added in the previous loop cycle)
                        var querylocal = from instance
                                         in db.Instances.Local
                                         where instance.Year == result.Year && instance.Category == result.Category
                                         select instance;

                        currentInstance = querylocal.FirstOrDefault();
                    }
                    else
                    {
                        // remove it completely because the new one will rewrite it
                        // lazy loading is used, so we need to load all the relations of an instance if we want Remove() to remove those as well
                        var blah  = currentInstance.AnswerSheets;
                        var blah2 = currentInstance.CorrectAnswers;
                        List <ICollection <KlokanDBChosenAnswer> > blah3 = new List <ICollection <KlokanDBChosenAnswer> >();
                        foreach (var answerSheetBlah in blah)
                        {
                            blah3.Add(answerSheetBlah.ChosenAnswers);
                        }

                        db.Instances.Remove(currentInstance);
                        await db.SaveChangesAsync(progressDialog.GetCancellationToken());

                        currentInstance = null;
                    }

                    // if the instance doesn't exist locally either
                    if (currentInstance == default(KlokanDBInstance))
                    {
                        List <KlokanDBCorrectAnswer> correctAnswers = new List <KlokanDBCorrectAnswer>();
                        for (int i = 0; i < 3; i++)
                        {
                            correctAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanDBCorrectAnswer>(result.CorrectAnswers, i, false));
                        }

                        currentInstance = new KlokanDBInstance
                        {
                            Year           = result.Year,
                            Category       = result.Category,
                            CorrectAnswers = correctAnswers
                        };

                        db.Instances.Add(currentInstance);
                    }

                    List <KlokanDBChosenAnswer> chosenAnswers = new List <KlokanDBChosenAnswer>();
                    for (int i = 0; i < 3; i++)
                    {
                        chosenAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanDBChosenAnswer>(result.ChosenAnswers, i, false));
                    }

                    var answerSheet = new KlokanDBAnswerSheet
                    {
                        StudentNumber = result.StudentNumber,
                        Points        = result.Score,
                        ChosenAnswers = chosenAnswers,
                        Scan          = ImageHandling.GetImageBytes(result.SheetFilename, ImageFormat.Png)
                    };

                    currentInstance.AnswerSheets.Add(answerSheet);
                }

                await db.SaveChangesAsync(progressDialog.GetCancellationToken());
            }
        }