Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jplag implementation #447

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: make report downloading use new path
  • Loading branch information
JackSCarroll committed Sep 21, 2024
commit c83919c49074187cabffebba4a8e7e48dbf96319
29 changes: 29 additions & 0 deletions app/api/task_definitions_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,33 @@ class TaskDefinitionsApi < Grape::API

stream_file path
end

desc 'Download the JPLAG report for a given task'
params do
requires :unit_id, type: Integer, desc: 'The unit to download JPLAG report for'
requires :task_def_id, type: Integer, desc: 'The task definition to get the JPLAG report of'
end
get '/units/:unit_id/task_definitions/:task_def_id/jplag_report' do
unit = Unit.find(params[:unit_id])
task_def = unit.task_definitions.find(params[:task_def_id])

unless authorise? current_user, unit, :download_jplag_report
error!({ error: 'Not authorised to download JPLAG reports of unit' }, 403)
end

file_loc = FileHelper.task_jplag_report_path(unit, task_def)
logger.debug "JPLAG report file location: #{file_loc}"

if file_loc.nil? || !File.exist?(file_loc)
file_loc = Rails.root.join('public', 'resources', 'FileNotFound.pdf')
header['Content-Disposition'] = 'attachment; filename=FileNotFound.pdf'
else
header['Content-Disposition'] = "attachment; filename=#{task_def.abbreviation}-jplag-report.zip"
end
header['Access-Control-Expose-Headers'] = 'Content-Disposition'

content_type 'application/octet-stream'

stream_file file_loc
end
end
12 changes: 12 additions & 0 deletions app/helpers/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ def student_portfolio_path(unit, username, create = true)
File.join(student_portfolio_dir(unit, username, create), FileHelper.sanitized_filename("#{username}-portfolio.pdf"))
end

def task_jplag_report_dir(unit)
file_server = Doubtfire::Application.config.jplag_report_dir
dst = "#{file_server}/#{unit.code}-#{unit.id}/" # trust the server config and passed in type for paths
dst
end

def task_jplag_report_path(unit, task)
File.join(task_jplag_report_dir(unit), FileHelper.sanitized_filename("#{task.id}-result.zip"))
end

def comment_attachment_path(task_comment, attachment_extension)
"#{File.join(student_work_dir(:comment, task_comment.task), "#{task_comment.id.to_s}#{attachment_extension}")}"
end
Expand Down Expand Up @@ -658,4 +668,6 @@ def line_wrap(path, width: 160)
module_function :known_extension?
module_function :pages_in_pdf
module_function :line_wrap
module_function :task_jplag_report_dir
module_function :task_jplag_report_path
end
7 changes: 4 additions & 3 deletions app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def self.permissions
:download_stats,
:download_unit_csv,
:download_grades,
:download_jplag_report,
:exceed_capacity
]

Expand All @@ -46,6 +47,7 @@ def self.permissions
:change_project_enrolment,
:download_stats,
:download_grades,
:download_jplag_report,
:rollover_unit,
:exceed_capacity,
:perform_overseer_assessment_test
Expand All @@ -66,6 +68,7 @@ def self.permissions
:download_stats,
:download_unit_csv,
:download_grades,
:download_jplag_report,
:exceed_capacity
]

Expand Down Expand Up @@ -178,9 +181,7 @@ def role_for(user)

include UnitTiiModule

## Change this back to the OG unit_similarity_module once the jplag version is working. i.e., copy the jplag ver into the OG
require_relative 'similarity/JPlag/unit_similarity_module_jplag'
include UnitSimilarityModuleJPLAG
include UnitSimilarityModule

def detailed_name
"#{name} #{teaching_period.present? ? teaching_period.detailed_name : start_date.strftime('%Y-%m-%d')}"
Expand Down
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class Application < Rails::Application
# variable.
config.student_work_dir = ENV['DF_STUDENT_WORK_DIR'] || "#{Rails.root}/student_work"

# ==> JPLAG report directory
# File server location for storing JPLAG reports. Defaults to `jplag_results`
# directory under root but is overridden using DF_JPLAG_REPORT_DIR environment
# variable.
config.jplag_report_dir = ENV['DF_JPLAG_REPORT_DIR'] || "#{Rails.root}/jplag_results"

# ==> Load credentials from env
credentials.secret_key_base = ENV.fetch('DF_SECRET_KEY_BASE', Rails.env.production? ? nil : '9e010ee2f52af762916406fd2ac488c5694a6cc784777136e657511f8bbc7a73f96d59c0a9a778a0d7cf6406f8ecbf77efe4701dfbd63d8248fc7cc7f32dea97')
credentials.secret_key_attr = ENV.fetch('DF_SECRET_KEY_ATTR', Rails.env.production? ? nil : 'e69fc5960ca0e8700844a3a25fe80373b41c0a265d342eba06950113f3766fd983bad9ec51bf36eb615d9711bfe1dd90b8e35f01841b323f604ffee857e32055')
Expand Down
60 changes: 30 additions & 30 deletions lib/tasks/checks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ namespace :submission do
FileUtils.rm(rake_plagiarism_executing_marker_file)
end

#task :simulate_plagiarism, [:num_links] => [:skip_prod, :environment] do |t, args|
# if is_executing_plagiarism?
# puts 'Skip plagiarism check -- already executing'
# logger.info 'Skip plagiarism check -- already executing'
# else
# match_template = {
# url: 'http://moss.stanford.edu/results/375180531/match0-top.html',
# pct: Random.rand(70..100),
# html: File.read('test_files/link_template.html')
# }
# match = [match_template, match_template]
# # Give me two random distinct students with the same TD
# unit = Unit.active_units.first
# num_links = (args[:num_links] || 1).to_i
# puts "Simulating #{num_links} plagiarism links for #{unit.code}..."
# num_links.times do
# td = unit.task_definitions.first
# t1 = unit.tasks.where(task_definition: td).sample()
# t2 = unit.tasks.where(task_definition: td).where.not(project_id: t1.project.id).sample()
# if t1.nil? || t2.nil?
# puts "Can't find any tasks to simulate. Have you run submission:simulate_signoff?'"
# return
# end
# puts "Plagiarism link for #{td.abbreviation} between #{t1.project.student.name} (project_id=#{t1.project.id}) <-> #{t2.project.student.name} (project_id=#{t2.project.id}) created!"
# unit.create_plagiarism_link(t1, t2, match)
# unit.create_plagiarism_link(t2, t1, match)
# end
# end
#end
task :simulate_plagiarism, [:num_links] => [:skip_prod, :environment] do |t, args|
if is_executing_plagiarism?
puts 'Skip plagiarism check -- already executing'
logger.info 'Skip plagiarism check -- already executing'
else
match_template = {
url: 'http://moss.stanford.edu/results/375180531/match0-top.html',
pct: Random.rand(70..100),
html: File.read('test_files/link_template.html')
}
match = [match_template, match_template]
# Give me two random distinct students with the same TD
unit = Unit.active_units.first
num_links = (args[:num_links] || 1).to_i
puts "Simulating #{num_links} plagiarism links for #{unit.code}..."
num_links.times do
td = unit.task_definitions.first
t1 = unit.tasks.where(task_definition: td).sample()
t2 = unit.tasks.where(task_definition: td).where.not(project_id: t1.project.id).sample()
if t1.nil? || t2.nil?
puts "Can't find any tasks to simulate. Have you run submission:simulate_signoff?'"
return
end
puts "Plagiarism link for #{td.abbreviation} between #{t1.project.student.name} (project_id=#{t1.project.id}) <-> #{t2.project.student.name} (project_id=#{t2.project.id}) created!"
unit.create_plagiarism_link(t1, t2, match)
unit.create_plagiarism_link(t2, t1, match)
end
end
end

task check_plagiarism: :environment do
if is_executing_plagiarism?
Expand All @@ -69,7 +69,7 @@ namespace :submission do
puts ' ------------------------------------------------------------ '
puts " Starting Plagiarism Check for #{unit.name}"
puts ' ------------------------------------------------------------ '
unit.check_jplag_similarity
unit.check_similarity
end
puts ' ------------------------------------------------------------ '
puts ' done.'
Expand Down