public async Task <IActionResult> ReCheckPlagiarism(int taskId)
        {
            try
            {
                var msgQueue = RabbitHutch.CreateBus(_config.MessageQueueInfo, _config.FrontEnd);

                var request = new PlagiarismCheckRequest
                {
                    TaskId = taskId,
                };
                await msgQueue.SendAsync(_config.MessageQueueInfo.PlagiarismQueueName, request);

                return(Content("/TestingSystem/Professor/ChangePlagiarism?taskId=" + taskId));
            }
            catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException e)
            {
                return(Content("error"));
            }
        }
Esempio n. 2
0
        public async Task CheckSolutions(PlagiarismCheckRequest checkRequest)
        {
            try
            {
                int taskId = checkRequest.TaskId;

                var task = _db.StudentTasks.Find(taskId);

                while (DateTime.Now < task.To)
                {
                    await Task.Delay(5 * 60 * 1000);

                    _db.Entry(task).Reload();
                }

                var check = _db.PlagiarismChecks.Find(taskId);

                if (check.Settings.MakeCheck)
                {
                    var sourceFileList   = new List <string>();
                    var studentSolutions = _db.StudentResults.Where(r => r.TaskId == taskId).Select(r => r.Solution).ToList();

                    var taskDir = new DirectoryInfo("C:\\tasks\\" + taskId.ToString());

                    if (taskDir.Exists)
                    {
                        taskDir.Delete(true);
                    }

                    taskDir.Create();

                    var saveDir = new DirectoryInfo("C:\\saving");

                    if (!saveDir.Exists)
                    {
                        saveDir.Create();
                    }

                    foreach (var sol in studentSolutions)
                    {
                        var curSaveDir = new DirectoryInfo(saveDir.FullName + "\\" + sol.Id.ToString());

                        if (curSaveDir.Exists)
                        {
                            curSaveDir.Delete(true);
                        }

                        curSaveDir.Create();

                        string fullPath = curSaveDir.FullName + "\\" + sol.File.Name;

                        File.WriteAllBytes(fullPath, sol.File.Content);
                        ZipFile.ExtractToDirectory(fullPath, curSaveDir.FullName, true);

                        var curSolDir = new DirectoryInfo(taskDir.FullName + "\\" + sol.Id.ToString());
                        if (curSolDir.Exists)
                        {
                            curSolDir.Delete(true);
                        }
                        curSolDir.Create();

                        var solFiles = curSaveDir.GetFiles("*.cs", SearchOption.AllDirectories).Where(p => !p.FullName.Contains("\\obj\\")).ToList();

                        foreach (var file in solFiles)
                        {
                            file.CopyTo(curSolDir.FullName + "//" + file.Name);
                        }

                        curSaveDir.Delete(true);
                    }

                    var request = new MossRequest
                    {
                        UserId          = _config.MossId,
                        IsDirectoryMode = true,
                        IsBetaRequest   = false,
                        Language        = check.Settings.Language,
                        MaxMatches      = check.Settings.MaxMatches
                    };

                    //request.BaseFile.AddRange(this.BaseFileList);

                    foreach (var solDir in taskDir.GetDirectories())
                    {
                        var curSolFiles = solDir.GetFiles("*.cs", SearchOption.AllDirectories).Select(f => f.FullName).Where(p => !p.Contains("\\obj\\")).ToList();
                        sourceFileList.AddRange(curSolFiles);
                    }

                    request.Files.AddRange(sourceFileList);

                    bool res = request.SendRequest(out string response);

                    check.Link = response;

                    if (res)
                    {
                        string html = new WebClient().DownloadString(response);

                        var config = Configuration.Default;

                        var context = BrowsingContext.New(config);

                        var document = context.OpenAsync(req => req.Content(html)).Result;

                        var allLines = document.All.Where(m => m.LocalName == "tr" && !m.Children.Any(c => c.LocalName == "th"));

                        foreach (var line in allLines)
                        {
                            var file1    = ParseUserAndPercent(line.Children[0]);
                            var file2    = ParseUserAndPercent(line.Children[1]);
                            var lMatched = int.Parse(line.Children[2].InnerHtml);

                            var solId1   = file1.Item1;
                            var percent1 = file1.Item2;

                            var solId2   = file2.Item1;
                            var percent2 = file2.Item2;

                            if (percent1 > check.Settings.MinPercent || percent2 > check.Settings.MinPercent)
                            {
                                var sol1 = _db.Solutions.Find(solId1);
                                var sol2 = _db.Solutions.Find(solId2);

                                var newResult = new PlagiarismResult
                                {
                                    SolutionId1  = sol1.Id,
                                    SolutionId2  = sol2.Id,
                                    Percent1     = percent1,
                                    Percent2     = percent2,
                                    LinesMatched = lMatched,
                                    TaskId       = taskId,
                                };

                                _db.PlagiarismResults.Add(newResult);

                                sol1.PlagiarismDetected = true;
                                sol2.PlagiarismDetected = true;
                            }
                        }
                    }
                    _db.SaveChanges();
                }
            }
            catch
            {
                return;
            }
        }
        public async Task <IActionResult> PostNewTask(string json)
        {
            TaskViewModelNew jsonTask = JsonConvert.DeserializeObject <TaskViewModelNew>(json);
            StudentTask      newTask  = new StudentTask
            {
                Name             = jsonTask.Name,
                GroupId          = jsonTask.GroupId,
                TaskText         = jsonTask.Text,
                IsContest        = jsonTask.IsContest,
                From             = jsonTask.Time[0],
                To               = jsonTask.Time[1],
                NumberOfAttempts = jsonTask.AttemptsNumber
            };

            var  x           = _db.StudentTasks.Add(newTask);
            var  beforeState = x.State;
            int  r           = _db.SaveChanges();
            var  afterState  = x.State;
            bool ok          = beforeState == Microsoft.EntityFrameworkCore.EntityState.Added && afterState == Microsoft.EntityFrameworkCore.EntityState.Unchanged && r == 1;

            if (ok)
            {
                var newTaskTests = jsonTask.Tests.Select(tt =>
                                                         new TaskTest
                {
                    TaskId   = newTask.Id,
                    TestType = tt.Type,
                    Block    = tt.Block,
                    Weight   = tt.Weight / 100.0,
                    TestData = null
                }).ToArray();

                _db.TaskTests.AddRange(newTaskTests);
                r  = _db.SaveChanges();
                ok = r == newTaskTests.Length;
            }

            if (ok)
            {
                var plagCheck = new PlagiarismCheck
                {
                    TaskId   = newTask.Id,
                    Settings = new PlagiarismCheckSettings
                    {
                        Language   = "csharp",
                        MaxMatches = 5,
                        MinPercent = 0.4,
                        MakeCheck  = false
                    }
                };

                _db.Add(plagCheck);
                _db.SaveChanges();

                try
                {
                    var msgQueue = RabbitHutch.CreateBus(_config.MessageQueueInfo, _config.FrontEnd);

                    var request = new PlagiarismCheckRequest
                    {
                        TaskId = newTask.Id,
                    };
                    await msgQueue.SendAsync(_config.MessageQueueInfo.PlagiarismQueueName, request);
                }
                catch (RabbitMQ.Client.Exceptions.BrokerUnreachableException e)
                {
                    return(Content("error"));
                }
            }

            return(Content(ok ? "/TestingSystem/Professor/ChangeTask?id=" + newTask.Id.ToString() : "error"));
        }