forked from doubtfire-lms/doubtfire-api
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject_compile_portfolio_module.rb
More file actions
299 lines (252 loc) · 9.67 KB
/
project_compile_portfolio_module.rb
File metadata and controls
299 lines (252 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
module PdfGeneration
module ProjectCompilePortfolioModule
def projects_awaiting_auto_generation
Project.joins(:unit)
.where(units: { active: true, end_date: Time.zone.today..Float::INFINITY })
.where(projects: { enrolled: true, portfolio_production_date: nil })
.where("units.portfolio_auto_generation_date < ?", Time.zone.today)
.where(compile_portfolio: false)
.reject(&:portfolio_available)
end
module_function :projects_awaiting_auto_generation
# Automatically generate the portfolio for this project - only when the
# learning summary exists, the project is enrolled, and the portfolio is not
# already available.
def auto_generate_portfolio
# Check we have a draft learning summary report
return false unless enrolled && learning_summary_report_exists? && !portfolio_available
# Flag compile is needed
self.compile_portfolio = true
# Assign current target as anticipated target grade
self.submitted_grade = self.target_grade
# Inidicate this is an auto-generated portfolio
self.portfolio_auto_generated = true
# Save changes
self.save
end
# Compress the student's portfolio if larger than 20mb
def compress_portfolio
FileHelper.compress_pdf(portfolio_path, max_size: 20_000_000, timeout_seconds: 120)
end
# This class scaffolds the creation of the portfolio - mapping the required data into the erb template
class ProjectAppController < ApplicationController
attr_accessor :student,
:project,
:base_path,
:image_path,
:learning_summary_report,
:ordered_tasks,
:portfolio_tasks,
:task_defs,
:outcomes,
:files,
:institution_name,
:doubtfire_product_name,
:is_retry
def init(project, is_retry)
@student = project.student
@project = project
@learning_summary_report = project.learning_summary_report_path
@files = project.portfolio_files(ensure_valid: true, force_ascii: is_retry)
@base_path = project.portfolio_temp_path
@image_path = Rails.root.join('public/assets/images')
@ordered_tasks = project.tasks.joins(:task_definition).order('task_definitions.start_date, task_definitions.abbreviation').where("task_definitions.target_grade <= #{project.target_grade}")
@portfolio_tasks = project.portfolio_tasks
@task_defs = project.unit.task_definitions.order(:start_date)
@outcomes = project.unit.learning_outcomes.order(:ilo_number)
@institution_name = Doubtfire::Application.config.institution[:name]
@doubtfire_product_name = Doubtfire::Application.config.institution[:product_name]
@is_retry = is_retry
end
def make_pdf
render_to_string(template: '/portfolio/portfolio_pdf', layout: true)
end
end
# Create the portfolio for this project
def create_portfolio
self.compile_portfolio = false
save!
begin
pac = ProjectAppController.new
pac.init(self, false)
begin
pdf_text = pac.make_pdf
rescue StandardError => e
# Clear the old text
pdf_text = nil
# Try again... with convert to ascii
pac2 = ProjectAppController.new
pac2.init(self, true)
pdf_text = pac2.make_pdf
end
File.open(portfolio_path, 'w') do |fout|
fout.puts pdf_text
end
compress_portfolio
logger.info "Created portfolio at #{portfolio_path} - #{log_details}"
self.portfolio_production_date = Time.zone.now
save
rescue StandardError => e
logger.error "Failed to convert portfolio to PDF - #{log_details} -\nError: #{e.message}"
log_file = e.message.scan(%r{/.*\.log}).first
if log_file && File.exist?(log_file)
begin
# rubocop:disable Rails/Output
puts "--- Latex Log ---\n"
puts File.read(log_file)
puts "--- End ---\n\n"
# rubocop:enable Rails/Output
rescue StandardError
# rubocop:disable Rails/Output
puts "Failed to read log file: #{log_file}"
# rubocop:enable Rails/Output
end
end
false
end
end
def save_as_learning_summary_report(path)
file_name = {
kind: 'document',
name: 'LearningSummaryReport.pdf',
idx: 0
}
# Creates tmp portfolio path (if it doesn't exist)
FileUtils.mkdir_p(portfolio_temp_path)
# Copy the file into place
FileUtils.cp path, portfolio_tmp_file_path(file_name)
# Record we are using the draft learning summary and save
self.uses_draft_learning_summary = true
self.save
end
# Return the tasks to include in the student's portfolio
def portfolio_tasks
# Get assigned tasks that are included in the portfolio
tasks = self.tasks.joins(:task_definition).order('task_definitions.target_date, task_definitions.abbreviation').where('tasks.include_in_portfolio = TRUE')
# Now select the tasks that and have a PDF... cant include the others...
tasks.select(&:has_pdf)
end
#
# Return the path to the student's learning summary report.
# This returns nil if there is no learning summary report.
#
def learning_summary_report_path
# Cache the portfolio temp path
portfolio_tmp_dir = portfolio_temp_path
return nil unless Dir.exist? portfolio_tmp_dir
filename = "#{portfolio_tmp_dir}/000-document-LearningSummaryReport.pdf"
return nil unless File.exist? filename
filename
end
# Check if the learning summary report exists
#
# @return [Boolean] true if the learning summary report file exists for this project
def learning_summary_report_exists?
path = learning_summary_report_path
path.present? && File.exist?(learning_summary_report_path)
end
#
# Portfolio production code
#
def portfolio_temp_path
portfolio_dir = FileHelper.student_portfolio_dir(self.unit, self.student.username, create: false)
File.join(portfolio_dir, 'tmp')
end
def portfolio_tmp_file_name(dict)
extn = File.extname(dict[:name])
name = File.basename(dict[:name], extn)
name = name.tr('.', '_') + extn
FileHelper.sanitized_filename("#{dict[:idx].to_s.rjust(3, '0')}-#{dict[:kind]}-#{name}")
end
def portfolio_tmp_file_path(dict)
File.join(portfolio_temp_path, portfolio_tmp_file_name(dict))
end
def move_to_portfolio(file, name, kind)
# get path to portfolio dir
# get path to tmp folder where file parts will be stored
portfolio_tmp_dir = portfolio_temp_path
FileUtils.mkdir_p(portfolio_tmp_dir)
result = {
kind: kind,
name: file[:filename]
}
# copy up the learning summary report as first -- otherwise use files to determine idx
if name == 'LearningSummaryReport' && kind == 'document'
result[:idx] = 0
result[:name] = 'LearningSummaryReport.pdf'
# set uses_draft_learning_summary to false, since we uploaded a new learning summary
self.uses_draft_learning_summary = false
save
else
Dir.chdir(portfolio_tmp_dir)
files = Dir.glob('*')
idx = files.map { |a_file| a_file.split('-').first.to_i }.max
if idx.nil? || idx < 1
idx = 1
else
idx += 1
end
result[:idx] = idx
end
dest_file = portfolio_tmp_file_name(result)
FileUtils.cp file["tempfile"].path, File.join(portfolio_tmp_dir, dest_file)
result
end
def portfolio_files(ensure_valid: false, force_ascii: false)
# get path to portfolio dir
portfolio_tmp_dir = portfolio_temp_path
return [] unless Dir.exist? portfolio_tmp_dir
result = []
Dir.chdir(portfolio_tmp_dir)
files = Dir.glob('*').select { |f| (f =~ /^\d{3}-(cover|document|code|image)/) == 0 }
files.each do |file|
parts = file.split('-')
idx = parts[0].to_i
kind = parts[1]
name = parts.drop(2).join('-')
result << { kind: kind, name: name, idx: idx }
FileHelper.ensure_utf8_code(file, force_ascii) if ensure_valid && kind == "code"
end
result
end
# Remove a file from the portfolio tmp folder
def remove_portfolio_file(idx, kind, name)
# get path to portfolio dir
portfolio_tmp_dir = portfolio_temp_path
return unless Dir.exist? portfolio_tmp_dir
# the file is in the students portfolio tmp dir
rm_file = File.join(
portfolio_tmp_dir,
FileHelper.sanitized_filename("#{idx.to_s.rjust(3, '0')}-#{kind}-#{name}")
)
# try to remove the file
begin
FileUtils.rm_f rm_file
rescue StandardError
logger.error "Failed to remove file #{rm_file} from portfolio tmp folder #{portfolio_tmp_dir}"
end
end
def portfolio_path
FileHelper.student_portfolio_path(self.unit, self.student.username, create: true)
end
def portfolio_exists?
portfolio_production_date.present? && portfolio_available
end
def portfolio_status
if portfolio_exists?
'YES'
elsif compile_portfolio
'in process'
else
'no'
end
end
def portfolio_available
(File.exist? portfolio_path) && !compile_portfolio
end
def remove_portfolio
portfolio = portfolio_path
FileUtils.mv portfolio, "#{portfolio}.old" if File.exist?(portfolio)
end
end
end