Exemplo n.º 1
0
        private void removeItemButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show(Properties.Resources.PromptTextDatabaseUpdate, Properties.Resources.PromptCaptionDatabaseUpdate,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);

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

            if (dataView.SelectedRows.Count == 0)
            {
                MessageBox.Show(Properties.Resources.ErrorTextNoRowSelected, Properties.Resources.ErrorCaptionGeneral,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // multiselect is set to false for this data view
            var rowToRemove   = dataView.SelectedRows[0];
            int rowToRemoveID = (int)(rowToRemove.Cells[0].Value);

            dataView.Rows.Remove(rowToRemove);

            using (var testDB = new KlokanTestDBContext())
            {
                var scanToRemoveQuery = from scan in testDB.Scans
                                        where scan.ScanId == rowToRemoveID
                                        select scan;

                var scanToRemove = scanToRemoveQuery.FirstOrDefault();
                if (scanToRemove != default(KlokanTestDBScan))
                {
                    // lazy loading is used, so we need to load all the relations of scan if we want Remove() to remove those as well
                    var expectedAnswers = scanToRemove.ExpectedValues;
                    var computedValues  = scanToRemove.ComputedValues;

                    testDB.Scans.Remove(scanToRemove);

                    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);
                    }
                }
            }

            ShowAverageCorrectness();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads test items from the database and shows them in the data view.
        /// </summary>
        private void PopulateDataView()
        {
            dataView.Rows.Clear();

            using (var testDB = new KlokanTestDBContext())
            {
                var scanQuery = from scan in testDB.Scans
                                select new { scan.ScanId, scan.Correctness };

                foreach (var scanInfo in scanQuery)
                {
                    dataView.Rows.Add(scanInfo.ScanId, scanInfo.Correctness);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously stores test results into a database described by KlokanTestDBContext.
        /// Results that already exist in the database are rewritten.
        /// </summary>
        /// <param name="results">Any enumerable structure of evaluation test results.</param>
        /// <returns>A void task.</returns>
        async Task OutputTestResultsDB(IEnumerable <TestResult> testResults)
        {
            using (var testDB = new KlokanTestDBContext())
            {
                foreach (var testResult in testResults)
                {
                    if (testResult.Error == true)
                    {
                        failedSheets++;
                        continue;
                    }

                    var scanQuery = from scan in testDB.Scans
                                    where scan.ScanId == testResult.ScanId
                                    select scan;

                    var oldComputedAnswersQuery = from answer in testDB.ComputedValues
                                                  where answer.ScanId == testResult.ScanId
                                                  select answer;

                    // delete old computed values
                    foreach (var answer in oldComputedAnswersQuery)
                    {
                        testDB.ComputedValues.Remove(answer);
                    }

                    // assign new computed values
                    KlokanTestDBScan correspondingScan = scanQuery.FirstOrDefault();

                    var computedValuesDbSet = new List <KlokanTestDBComputedAnswer>();
                    computedValuesDbSet.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBComputedAnswer>(testResult.StudentComputedValues, 0, true));
                    for (int i = 0; i < 3; i++)
                    {
                        computedValuesDbSet.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBComputedAnswer>(testResult.AnswerComputedValues, i, false));
                    }

                    correspondingScan.ComputedValues = computedValuesDbSet;
                    correspondingScan.Correctness    = testResult.Correctness;

                    await testDB.SaveChangesAsync(progressDialog.GetCancellationToken());
                }
            }
        }
Exemplo n.º 4
0
        private void viewItemButton_Click(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count == 0)
            {
                MessageBox.Show(Properties.Resources.ErrorTextNoRowSelected, Properties.Resources.ErrorCaptionGeneral,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // multiselect is set to false for this data view
            int scanItemId = (int)dataView.SelectedRows[0].Cells[0].Value;

            KlokanTestDBScan scanItemToView = null;

            using (var testDB = new KlokanTestDBContext())
            {
                var scanItemQuery = from scan in testDB.Scans
                                    where scan.ScanId == scanItemId
                                    select scan;

                var scanItem = scanItemQuery.FirstOrDefault();

                scanItemToView = new KlokanTestDBScan {
                    ScanId         = scanItem.ScanId,
                    ComputedValues = scanItem.ComputedValues,
                    ExpectedValues = scanItem.ExpectedValues,
                    Image          = scanItem.Image,
                    Correctness    = scanItem.Correctness
                };
            }

            TestItemForm form = new TestItemForm(scanItemToView, true);

            form.StartPosition = FormStartPosition.CenterScreen;

            // all potential changes to the scan item will be saved into the database if the user chooses to do so
            form.ShowDialog();

            PopulateDataView();
            ShowAverageCorrectness();
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
0
        private void evaluateButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Properties.Resources.PromptTextEvaluationStart, Properties.Resources.PromptCaptionEvaluationStart,
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            List <TestKlokanInstance> testInstances = new List <TestKlokanInstance>();

            // get all available test instances
            using (var testDB = new KlokanTestDBContext())
            {
                var allScansQuery = from scan in testDB.Scans
                                    select scan;

                foreach (var scan in allScansQuery)
                {
                    bool[,,] studentExpectedValues;
                    bool[,,] answerExpectedValues;
                    TableArrayHandling.DbSetToAnswers(new List <KlokanTestDBExpectedAnswer>(scan.ExpectedValues), out studentExpectedValues, out answerExpectedValues);

                    TestKlokanInstance testInstance = new TestKlokanInstance {
                        ScanId = scan.ScanId,
                        Image  = scan.Image,
                        StudentExpectedValues = studentExpectedValues,
                        AnswerExpectedValues  = answerExpectedValues
                    };

                    testInstances.Add(testInstance);
                }
            }

            if (testInstances.Count == 0)
            {
                MessageBox.Show(Properties.Resources.InfoTextNoTestItems, Properties.Resources.InfoCaptionNoTestItems,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            TestKlokanBatch testBatch = new TestKlokanBatch {
                Parameters    = chosenParameters,
                TestInstances = testInstances
            };

            ProgressDialog progressDialog = new ProgressDialog(new CancellationTokenSource());

            progressDialog.SetProgressLabel(ProgressBarState.Evaluating);

            var jobScheduler = new JobScheduler(testBatch, progressDialog);

            // new thread created, so that all tasks in it are planned in the threadpool and not in the WinForms synchronization context
            Thread thread = new Thread(jobScheduler.Run);

            thread.IsBackground = true;
            thread.Start();

            progressDialog.StartPosition = FormStartPosition.CenterScreen;
            progressDialog.ShowDialog();

            PopulateDataView();
            ShowAverageCorrectness();
        }