Ejemplo n.º 1
0
        /// <summary>
        /// Gets data from the scan item field and shows it.
        /// </summary>
        private void PopulateForm()
        {
            // load scan
            scanPictureBox.Image = ImageHandling.GetBitmap(scanItem.Image);

            // load the answers
            List <KlokanTestDBExpectedAnswer> expectedValues = new List <KlokanTestDBExpectedAnswer>(scanItem.ExpectedValues);

            TableArrayHandling.DbSetToAnswers(expectedValues, out expectedValuesStudentTable, out expectedValuesAnswerTable);

            FormTableHandling.DrawAnswers(studentTablePictureBox, expectedValuesStudentTable, 0, FormTableHandling.DrawCross, Color.Black);
            FormTableHandling.DrawAnswers(answerTable1PictureBox, expectedValuesAnswerTable, 0, FormTableHandling.DrawCross, Color.Black);
            FormTableHandling.DrawAnswers(answerTable2PictureBox, expectedValuesAnswerTable, 1, FormTableHandling.DrawCross, Color.Black);
            FormTableHandling.DrawAnswers(answerTable3PictureBox, expectedValuesAnswerTable, 2, FormTableHandling.DrawCross, Color.Black);

            bool[,,] computedValuesStudentTable;
            bool[,,] computedValuesAnswerTable;

            List <KlokanTestDBComputedAnswer> computedValues = new List <KlokanTestDBComputedAnswer>(scanItem.ComputedValues);

            TableArrayHandling.DbSetToAnswers(computedValues, out computedValuesStudentTable, out computedValuesAnswerTable);

            FormTableHandling.DrawAnswers(studentTablePictureBox, computedValuesStudentTable, 0, FormTableHandling.DrawCircle, Color.Red);
            FormTableHandling.DrawAnswers(answerTable1PictureBox, computedValuesAnswerTable, 0, FormTableHandling.DrawCircle, Color.Red);
            FormTableHandling.DrawAnswers(answerTable2PictureBox, computedValuesAnswerTable, 1, FormTableHandling.DrawCircle, Color.Red);
            FormTableHandling.DrawAnswers(answerTable3PictureBox, computedValuesAnswerTable, 2, FormTableHandling.DrawCircle, Color.Red);
        }
Ejemplo n.º 2
0
        private void updateButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show(Properties.Resources.PromptTextDatabaseUpdate, Properties.Resources.PromptCaptionDatabaseUpdate,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialogResult == DialogResult.No)
            {
                return;
            }

            updateButton.Enabled = false;

            // prepare the DbSet of chosen expected answers
            List <KlokanTestDBExpectedAnswer> expectedAnswers = new List <KlokanTestDBExpectedAnswer>();

            expectedAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBExpectedAnswer>(expectedValuesStudentTable, 0, true));
            for (int i = 0; i < 3; i++)
            {
                expectedAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBExpectedAnswer>(expectedValuesAnswerTable, i, false));
            }

            if (addMode)
            {
                scanItem.Image = ImageHandling.GetImageBytes(scanFilePath, ImageFormat.Png);
            }

            scanItem.ExpectedValues = expectedAnswers;
            scanItem.Correctness    = -1;               // correctness will only have a valid value once the evaluation is run

            using (var testDB = new KlokanTestDBContext())
            {
                // when editing an item we first have to delete the old one
                if (!addMode)
                {
                    var oldScanItemQuery = from scan in testDB.Scans
                                           where scan.ScanId == scanItem.ScanId
                                           select scan;

                    var oldExpectedAnswers = from answer in testDB.ExpectedValues
                                             where answer.ScanId == scanItem.ScanId
                                             select answer;

                    // delete the old expected answers
                    foreach (var answer in oldExpectedAnswers)
                    {
                        testDB.ExpectedValues.Remove(answer);
                    }

                    // assign new expected answers
                    var oldScanItem = oldScanItemQuery.FirstOrDefault();
                    oldScanItem.ExpectedValues = scanItem.ExpectedValues;
                    oldScanItem.Correctness    = -1;
                }
                else
                {
                    KlokanTestDBScan newScanItem = new KlokanTestDBScan
                    {
                        ExpectedValues = scanItem.ExpectedValues,
                        ComputedValues = scanItem.ComputedValues,
                        Image          = scanItem.Image,
                        Correctness    = -1
                    };

                    testDB.Scans.Add(newScanItem);
                }

                try
                {
                    testDB.SaveChanges();

                    MessageBox.Show(Properties.Resources.InfoTextDatabaseUpdated, Properties.Resources.InfoCaptionDatabaseUpdated,
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex) when(ex is DbUpdateException || ex is DbUpdateConcurrencyException)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextDatabase, Properties.Resources.ErrorCaptionGeneral, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            PopulateForm();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Uses a native library to load an image and extract answers from it.
        /// Works with both image bytes as well as just an image file path.
        /// This method is unsafe.
        /// </summary>
        /// <param name="sheet">The path to the image containing answers to be extracted as a string or an array of image bytes.</param>
        /// <param name="studentNumberAnswers">This parameter will contain the student number answers extracted from the student number table.</param>
        /// <param name="extractedAnswers">This parameter will contain the answers extracted from the answer tables.</param>
        /// <returns>True if the process has succeeded and false otherwise.</returns>
        bool ExtractAnswers <T>(T sheet, out bool[,,] studentNumberAnswers, out bool[,,] extractedAnswers)
        {
            extractedAnswers     = new bool[3, 8, 5];
            studentNumberAnswers = new bool[1, 5, 10];

            // the first row and the first column of the original tables were removed as they do not contain any answers
            int studentNumberRows    = parameters.StudentTableRows - 1;
            int studentNumberColumns = parameters.StudentTableColumns - 1;
            int answerRows           = parameters.AnswerTableRows - 1;
            int answerColumns        = parameters.AnswerTableColumns - 1;

            NumberWrapper numberWrapper = new NumberWrapper();
            AnswerWrapper answerWrapper = new AnswerWrapper();
            bool          success       = false;

            unsafe
            {
                bool *numberPtr  = numberWrapper.number;
                bool *answersPtr = answerWrapper.answers;
                bool *successPtr = &success;

                string sheetFilename;
                byte[] sheetImageBytes;

                // if the sheet has been passed as a filename
                if ((sheetFilename = sheet as string) != null)
                {
                    NativeAPIWrapper.extract_answers_path_api(sheetFilename, parameters, numberPtr, answersPtr, successPtr);
                }
                // if the sheet has been passed as image bytes
                else if ((sheetImageBytes = sheet as byte[]) != null)
                {
                    // the image is saved in PNG (or another format) so we need to convert it to BMP which the library can read
                    Image sheetImage = ImageHandling.GetBitmap(sheetImageBytes);
                    int   imageRows  = sheetImage.Height;
                    int   imageCols  = sheetImage.Width;

                    byte[] sheetImageBitmapBytes = ImageHandling.GetImageBytes(sheetImage, ImageFormat.Bmp);
                    sheetImage.Dispose();

                    fixed(byte *imageBitmapBytesPtr = sheetImageBitmapBytes)
                    {
                        NativeAPIWrapper.extract_answers_image_api(imageBitmapBytesPtr, imageRows, imageCols, parameters, numberPtr, answersPtr, successPtr);
                    }
                }
                else
                {
                    return(false);
                }

                if (!success)
                {
                    return(false);
                }

                // convert the student number values from a C-style array of answers to a C# multi-dimensional array
                for (int row = 0; row < studentNumberRows; row++)
                {
                    for (int col = 0; col < studentNumberColumns; col++)
                    {
                        if (numberPtr[row * studentNumberColumns + col] == true)
                        {
                            studentNumberAnswers[0, row, col] = true;
                        }
                        else
                        {
                            studentNumberAnswers[0, row, col] = false;
                        }
                    }
                }

                // convert the answer table values from a C-style array to a C# multi-dimensional array
                for (int table = 0; table < parameters.TableCount - 1; table++)
                {
                    for (int row = 0; row < answerRows; row++)
                    {
                        for (int col = 0; col < answerColumns; col++)
                        {
                            if (answersPtr[table * answerRows * answerColumns + row * answerColumns + col] == true)
                            {
                                extractedAnswers[table, row, col] = true;
                            }
                            else
                            {
                                extractedAnswers[table, row, col] = false;
                            }
                        }
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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());
            }
        }