Diff view between student submission and another file #1364
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
What?
Shows the difference of a student submission and another submission of the same student or model solution in the inspect page in git style. Green lines indicate the lines that the student has "added" compared to the other submission and red lines indicate the lines that the student has "removed". The original line numbers of both files are displayed on the left in GitLab style. When the submission is copied or downloaded with buttons "Copy to clipboard" or "Download", the diff lines are included if the compare mode is on.
Why?
Makes it easier for TAs to find bugs and catch plagiarism in students' submissions.
How?
Adds a query param "compare_to" to SubmittedFileView that returns the diff of the submitted file and another file. Possible options for the param are id of another submission or the string "model" (to compare to the model submission). The diff of the files are shown so that marker ' ' (two spaces) is prepended to the common lines and the marker '+ ' or '- ' is prepended to lines that are unique to the files. See https://docs.python.org/3/library/difflib.html for more details. The lines are rendered with different background colors depending on these markers. Some model answers have different versions for different languages, so the language of the submission is considered when fetching the model answer.
CAUTIONS: If the exercise submission includes multiple files, the files have to be paired when they are compared. When a student submits a solution, each submitted file is given a param_name file{i}, where i is an index starting from 1. The code that pairs the model submission files with the student's submitted files assumes that the model submission files in the LearningObject.model_answers attribute are listed in the same order as the file indices in the submitted files. I tested that this pairing scheme works, but it is not very reliable, as it is solely based on the file order. We could use file names to pair the files more reliably, but this does not work if the student gives different names to the files that the model solution files have.
Fixes #1129
Testing
Remember to add or update unit tests for new features and changes.
What type of test did you run?
Submitted assignments to 5+ exercises in O1 and A+ manual local instances. Compared the submissions to the model solution and previous submissions. Line numbers and colors are displayed correctly. Compare button is not shown in the submission table if there is only one submission. "Compare to model answer" button is not shown if the exercise has no model answer. An error message is shown if the compare_to param is changed to an invalid value. Buttons "Download" and "Copy to clipboard" work as expected. Submissions are compared to the model solution with the correct language version, even if the assistant uses a different language than the submitter. Submissions with arbitrary file names are paired with correct model solution files. Added playwright tests.
Did you test the changes in
Think of what is affected by these changes and could become broken
Translation
Programming style
Have you updated the README or other relevant documentation?
Is it Done?
Clean up your git commit history before submitting the pull request!