-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into allen/assessment-edit
- Loading branch information
Showing
18 changed files
with
554 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
class Course::Assessment::Submission::ZipDownloadJob < ApplicationJob | ||
include TrackableJob | ||
|
||
protected | ||
|
||
# Performs the download service. | ||
# | ||
# @param [CourseUser] course_user The course user downloading the submissions. | ||
# @param [Course::Assessment] assessment The assessments to download submissions for. | ||
# @param [String|nil] students The subset of students whose submissions to download. | ||
def perform_tracked(course_user, assessment, students = nil) | ||
zip_file = Course::Assessment::Submission::ZipDownloadService. | ||
download_and_zip(course_user, assessment, students) | ||
redirect_to SendFile.send_file(zip_file, Pathname.normalize_filename(assessment.title) + '.zip') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/services/course/assessment/submission/zip_download_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
class Course::Assessment::Submission::ZipDownloadService | ||
class << self | ||
# Downloads the submissions and zip them. | ||
# | ||
# @param [CourseUser] course_user The course user downloading the submissions. | ||
# @param [Course::Assessment] assessment The assessments to download submissions from. | ||
# @param [String|nil] students The subset of students whose submissions to download. | ||
# Accepted values: 'my_students', 'students', 'others' | ||
# @return [String] The path to the zip file. | ||
def download_and_zip(course_user, assessment, students) | ||
service = new(course_user, assessment, students) | ||
ActsAsTenant.without_tenant do | ||
service.send(:download_to_base_dir) | ||
end | ||
service.send(:zip_base_dir) | ||
end | ||
end | ||
|
||
STUDENTS = { my: 'my', phantom: 'phantom' }.freeze | ||
|
||
private | ||
|
||
def initialize(course_user, assessment, students) | ||
@course_user = course_user | ||
@assessment = assessment | ||
@questions = assessment.questions.map { |q| [q.id, q] }.to_h | ||
@students = students | ||
@base_dir = Dir.mktmpdir('coursemology-download-') | ||
end | ||
|
||
# Downloads each submission to its own folder in the base directory. | ||
def download_to_base_dir | ||
submissions = @assessment.submissions.by_users(student_ids). | ||
includes(:answers, experience_points_record: :course_user) | ||
submissions.find_each do |submission| | ||
submission_dir = create_folder(@base_dir, submission.course_user.name) | ||
download_answers(submission, submission_dir) | ||
end | ||
end | ||
|
||
# Downloads each answer to its own folder in the submission directory. | ||
def download_answers(submission, submission_dir) | ||
answers = submission.answers.includes(:question).latest_answers. | ||
select { |answer| @questions[answer.question_id]&.downloadable? } | ||
answers.each do |answer| | ||
answer_dir = create_folder(submission_dir, @questions[answer.question_id].display_title) | ||
answer.specific.download(answer_dir) | ||
end | ||
end | ||
|
||
def create_folder(parent, folder_name) | ||
normalized_name = Pathname.normalize_filename(folder_name) | ||
name_generator = FileName.new(File.join(parent, normalized_name), | ||
format: '(%d)', delimiter: ' ') | ||
name_generator.create.tap do |dir| | ||
Dir.mkdir(dir) | ||
end | ||
end | ||
|
||
# Zip the directory and write to the file. | ||
# | ||
# @return [String] The path to the zip file. | ||
def zip_base_dir | ||
output_file = @base_dir + '.zip' | ||
Zip::File.open(output_file, Zip::File::CREATE) do |zip_file| | ||
Dir["#{@base_dir}/**/**"].each do |file| | ||
zip_file.add(file.sub(File.join(@base_dir + '/'), ''), file) | ||
end | ||
end | ||
|
||
output_file | ||
end | ||
|
||
def student_ids | ||
@student_ids ||= | ||
case @students | ||
when STUDENTS[:my] | ||
@course_user.my_students | ||
when STUDENTS[:phantom] | ||
@course_user.course.course_users.students.phantom | ||
else | ||
@course_user.course.course_users.students.without_phantom_users | ||
end.select(:user_id) | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
app/views/course/assessment/submission/submissions/_submissions.html.slim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.