Skip to content

Commit 383becb

Browse files
committed
clean up code
1 parent 6a72f30 commit 383becb

4 files changed

Lines changed: 63 additions & 52 deletions

File tree

app/controllers/exercises_controller.rb

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ def practice
597597
end
598598

599599

600+
601+
600602
@responses = ['There are no responses yet!']
601603
@explain = ['There are no explanations yet!']
602604
if session[:leaf_exercises]
@@ -619,61 +621,17 @@ def practice
619621
@workout ||= @workout_score.andand.workout || @workout_offering.andand.workout
620622
ex_count = @workout.andand.exercises.andand.count
621623
@hide_sidebar = (!@workout && @lti_launch) || (ex_count && ex_count < 2)
622-
623-
allOwnerships = ExerciseVersion.where(id: @exercise.current_version_id)[0].ownerships
624-
checkMarkDown = @exercise_version.prompts[0].question.scan(/(\!\[.*?\]\((.*?)\))/)
625-
if checkMarkDown.length()>0
626-
checkMarkDown.each do |block|
627-
arr = block[1].strip.split(" ")
628-
counter = 0
629-
arr.each do |name|
630-
enterName = File.basename(name)
631-
if !allOwnerships.find_by(filename: enterName).nil?
632-
uniqueFile = ResourceFile.where(id: allOwnerships.find_by(filename: enterName).resource_file_id)[0].filename
633-
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
634-
fb = block[0].gsub("#{block[1]}", "/uploads/resource_file/#{uniqueFilename}")
635-
@exercise_version.prompts[0].question = @exercise_version.prompts[0].question.gsub("#{block[0]}", "#{fb}")
636-
else
637-
counter = counter + 1
638-
end
639-
end
640-
if counter == arr.length()
641-
@exercise_version.prompts[0].question = @exercise_version.prompts[0].question.gsub("#{block[0]}", "(**!#{block[0]}** Image does not exist!)")
642-
end
643-
end
644-
645-
end
646-
checkHTMLtag = @exercise_version.prompts[0].question.scan(/(\<img .*?src=(.*? ).*?>)/)
647-
if checkHTMLtag.length()>0
648-
checkHTMLtag.each do |block|
649-
enterName = File.basename(block[1]).strip
650-
if !allOwnerships.find_by(filename: enterName).nil?
651-
uniqueFile = ResourceFile.where(id: allOwnerships.find_by(filename: enterName).resource_file_id)[0].filename
652-
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
653-
fb = block[0].gsub("#{enterName}", "/uploads/resource_file/#{uniqueFilename}")
654-
@exercise_version.prompts[0].question = @exercise_version.prompts[0].question.gsub("#{block[0]}", "#{fb}")
655-
else
656-
@exercise_version.prompts[0].question = @exercise_version.prompts[0].question.gsub("#{block[0]}", "(Image **src=#{enterName}** does not exist!)")
657-
end
658-
end
659-
end
660-
661-
@allFiles= []
662-
@allFilesRegularName= []
663-
allOwnerships.each do |res|
664-
if ![".jpg",".jpeg",".png",".gif"].include?(File.extname(res.filename))
665-
@allFilesRegularName.push(res.filename)
666-
uniqueFile = ResourceFile.where(id: res.resource_file_id)[0].filename
667-
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
668-
@allFiles.push(uniqueFilename)
669-
end
670-
end
671-
@fileres = @allFiles.zip @allFilesRegularName
624+
# Updata image tags in the exercise question
625+
@exercise_version.imageProcessing
626+
# Display all files to students
627+
@fileRes = @exercise_version.fileProcessing
672628
render layout: 'two_columns'
629+
673630
end
674631

675632

676633

634+
677635

678636
def download_file
679637
client = Client.find(params[:id])

app/models/exercise_version.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,60 @@ def is_mcq?
188188
exercise.is_mcq?
189189
end
190190

191+
def imageProcessing
192+
ownerships = ExerciseVersion.where(id:self.id)[0].ownerships
193+
checkMarkDown = self.prompts[0].question.scan(/(\!\[.*?\]\((.*?)\))/)
194+
if checkMarkDown.length()>0
195+
checkMarkDown.each do |block|
196+
arr = block[1].strip.split(" ")
197+
counter = 0
198+
arr.each do |name|
199+
enterName = File.basename(name)
200+
if !ownerships.find_by(filename: enterName).nil?
201+
uniqueFile = ResourceFile.where(id: ownerships.find_by(filename: enterName).resource_file_id)[0].filename
202+
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
203+
fb = block[0].gsub("#{block[1]}", "/uploads/resource_file/#{uniqueFilename}")
204+
self.prompts[0].question = self.prompts[0].question.gsub("#{block[0]}", "#{fb}")
205+
else
206+
counter = counter + 1
207+
end
208+
end
209+
if counter == arr.length()
210+
self.prompts[0].question = self.prompts[0].question.gsub("#{block[0]}", "(**!#{block[0]}** Image does not exist!)")
211+
end
212+
end
213+
end
214+
checkHTMLtag = self.prompts[0].question.scan(/(\<img .*?src=(.*? ).*?>)/)
215+
if checkHTMLtag.length()>0
216+
checkHTMLtag.each do |block|
217+
enterName = File.basename(block[1]).strip
218+
if !ownerships.find_by(filename: enterName).nil?
219+
uniqueFile = ResourceFile.where(id: ownerships.find_by(filename: enterName).resource_file_id)[0].filename
220+
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
221+
fb = block[0].gsub("#{enterName}", "/uploads/resource_file/#{uniqueFilename}")
222+
self.prompts[0].question = self.prompts[0].question.gsub("#{block[0]}", "#{fb}")
223+
else
224+
self.prompts[0].question = self.prompts[0].question.gsub("#{block[0]}", "(Image **src=#{enterName}** does not exist!)")
225+
end
226+
end
227+
end
228+
end
229+
230+
231+
def fileProcessing
232+
@allFiles= []
233+
@allFilesRegularName= []
234+
ownerships = ExerciseVersion.where(id:self.id)[0].ownerships
235+
ownerships.each do |res|
236+
if ![".jpg",".jpeg",".png",".gif"].include?(File.extname(res.filename))
237+
@allFilesRegularName.push(res.filename)
238+
uniqueFile = ResourceFile.where(id: res.resource_file_id)[0].filename
239+
uniqueFilename = uniqueFile.model.token+uniqueFile.file.file.match(/\.\w*/)[0]
240+
@allFiles.push(uniqueFilename)
241+
end
242+
end
243+
@fileRes = @allFiles.zip @allFilesRegularName
244+
end
191245

192246
# -------------------------------------------------------------
193247
def is_coding?

app/views/exercises/_oneblockform.html.haml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
%table#tablelist.table
2727
%thead
2828
%tr
29-
//%th.col-sm-1{:style => "background-color:lavender;"} Type
3029
%th.col-sm-1 Type
3130
%th.col-sm-2 Thumbnail
3231
%th.col-sm-2 Name

app/views/exercises/practice.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- prompt.examples.each do |example|
3232
= example.display_description
3333
%br
34-
-@fileres.each do |uniqueName, realName|
34+
-@fileRes.each do |uniqueName, realName|
3535
%p
3636
=link_to "#{realName}", "/uploads/resource_file/#{uniqueName}"
3737
.col-md-7#user-deadline{ data: { deadline: @user_deadline, server_now: @server_now } }

0 commit comments

Comments
 (0)