-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathexercises_controller.rb
More file actions
1104 lines (964 loc) · 38 KB
/
exercises_controller.rb
File metadata and controls
1104 lines (964 loc) · 38 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class ExercisesController < ApplicationController
require 'ims/lti'
require 'oauth/request_proxy/rack_request'
require 'zip'
require 'tempfile'
load_and_authorize_resource
skip_authorize_resource only: [:practice, :call_open_pop, :embed_collection, :export]
#~ Action methods ...........................................................
after_action :allow_iframe, only: [:practice, :embed]
# -------------------------------------------------------------
HTTP_URL = 'https://opendsa-server.cs.vt.edu:9292/answers/solve'
# GET /exercises
def index
if current_user
@exercises = Exercise.visible_to_user(current_user)
else
@exercises = Exercise.publicly_visible
end
@exercises = @exercises.page params[:page]
end
# This embed_collection fetches all exercises for the embed_collections page and provides a simplified iframe urls of exercises for SPLICE
def embed_collection
if current_user
@exercises = Exercise.visible_to_user(current_user)
else
@exercises = Exercise.publicly_visible
end
@exercises = @exercises.page(params[:page])
end
# The export function gets all exercises metadata for SPLICE
def export
# filter out stop/connector words for keywords from workout phrases or names
stop_words = ['the', 'and', 'a', 'to', 'of', 'in', 'for', 'on', 'with', 'as', 'by', 'at', 'from', 'is', 'that', 'which', 'it', 'an', 'be', 'this', 'are', 'we', 'can', 'if', 'has', 'but']
@exercises = Exercise.all
export_data = @exercises.map do |exercise|
workout_names = exercise.exercise_workouts.map { |ew| ew.workout.name }.uniq.push(exercise.name)
# split phrases into words, remove stop/connector words
keywords_array = workout_names.map { |phrase| phrase.downcase.split(/\W+/) }.flatten.uniq.reject { |word| stop_words.include?(word) || word.empty? }
{
"Platform_name": "Code-Workout",
"URL": "https://codeworkout.cs.vt.edu", # hardcoded URL for now
"LTI_Instructions_URL": "https://opendsa-server.cs.vt.edu/guides/opendsa-canvas",
"Exercise_type": Exercise::TYPE_NAMES[exercise.question_type],
"License": "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)",
"Description": exercise.exercise_collection&.description,
"Author": "Edwards",
"Institution": "VT",
"Keywords": keywords_array,
"Exercise_Name": exercise.name,
"Iframe_URL": exercise.iframe_url,
"LTI_URL": exercise.lti_launch_url
}
end
render json: export_data
end
# -------------------------------------------------------------
# GET /exercises/download.csv
def download
@exercises = Exercise.accessible_by(current_ability)
respond_to do |format|
format.csv
format.json do
render text:
ExerciseRepresenter.for_collection.new(@exercises).to_hash.to_json
end
format.yml do
render text:
ExerciseRepresenter.for_collection.new(@exercises).to_hash.to_yaml
end
end
end
# -------------------------------------------------------------
def query_data
if current_user.global_role == GlobalRole.administrator
@available_exercises = Exercise.all.select(&:is_coding?)
else
@available_exercises = Exercise.visible_through_user(current_user)
.union(Exercise.visible_through_user_group(current_user))
.uniq.select(&:is_coding?)
end
end
# -------------------------------------------------------------
def download_attempt_data
exercise_id = params[:id] # may be zero, one, or more ids (see Exercise.denormalized_attempt_data)
course_id = params[:course_id]
term_id = params[:term_id]
time = Time.now.utc.strftime("%Y%m%d%H%M%S")
if params[:progsnap].to_b
main_events, code_states = Exercise.progsnap2_attempt_csv(exercise_id, course_id, term_id)
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
main_events_file = "MainTable.csv"
zos.put_next_entry main_events_file
zos.write main_events
code_states_file = "CodeStates.csv"
zos.put_next_entry code_states_file
zos.write code_states
end
compressed_filestream.rewind
respond_to do |format|
format.zip do
send_data compressed_filestream.read, filename: "CW-progsnap-#{time}.zip", type: 'application/zip'
end
end
else
result = Exercise.denormalized_attempt_csv(exercise_id)
respond_to do |format|
format.csv do
send_data result, filename: "CW-#{time}-submissions.csv"
end
end
end
end
# -------------------------------------------------------------
def search
@terms = escape_javascript(params[:search])
@terms = @terms.split(@terms.include?(',') ? /\s*,\s*/ : nil)
@wos = []
@exs = Exercise.search(@terms, current_user)
@msg = ''
if @exs.blank?
@msg = "No exercises were found for the search terms: #{@terms}"
redirect_to(exercises_path, alert: @msg) and return
end
respond_to do |format|
format.html
format.js
end
end
# -------------------------------------------------------------
# GET /exercises/1
def show
end
# -------------------------------------------------------------
# GET /exercises/new
def new
@exercise = Exercise.new
@exercise_version = ExerciseVersion.new
@user_groups = current_user.andand.user_groups
end
# -------------------------------------------------------------
# GET /exercises/1/edit
def edit
puts "ResourceFile.UPLOAD_PATH = #{ResourceFile::UPLOAD_PATH}"
@exercise_version = @exercise.current_version
@attached_files = []
@exercise_version.ownerships.each do |e|
@attached_files.push({
name: e.filename,
url: e.resource_file.url,
uploaded: true,
deleted: false
})
end
@text_representation = @exercise_version.text_representation ||
ExerciseRepresenter.new(@exercise).to_hash.to_yaml
@user_groups = current_user.user_groups
# figure out the use/assign rights to this exercise
if ec = @exercise.exercise_collection
if ec.owned_by?(current_user)
@exercise_collection = 0 # Only Me
elsif @user_groups.include?(ec.user_group)
@exercise_collection = ec.user_group_id
else
@exercise_collection = -1 # Everyone
end
end
@return_to = request.referer || exercise_practice_path(@exercise)
if @return_to.include?(exercises_search_path)
@return_to = exercise_practice_path(@exercise)
end
session[:return_to] = @return_to
end
# -------------------------------------------------------------
# POST /exercises
def create
ex = Exercise.new
exercise_version = ExerciseVersion.new(exercise: ex)
msg = params[:exercise] || params[:coding_question]
msg[:is_public] = msg[:is_public].to_i > 0
form_hash = msg.clone()
arr = []
form_hash["current_version"] = msg[:exercise_version].clone()
if msg[:question_type].to_i == 2
msg[:coding_prompt].merge!(msg[:prompt])
test_cases = ""
msg[:coding_prompt][:test_cases_attributes].values.each do |tc|
test_cases = test_cases + tc.values.join(",") + "\n"
end
test_cases.rstrip!
msg[:coding_prompt].delete("test_cases_attributes")
msg[:coding_prompt]["tests"] = test_cases
form_hash["current_version"]["prompts"] = Array.new
codingprompt = {"coding_prompt" => msg[:coding_prompt].clone()}
form_hash["current_version"]["prompts"] << codingprompt
form_hash.delete("coding_prompt")
elsif msg[:question_type].to_i == 1
msg[:multiple_choice_prompt].merge!(msg[:prompt])
msg[:multiple_choice_prompt][:is_scrambled] = msg[:multiple_choice_prompt][:is_scrambled].to_i > 0
msg[:multiple_choice_prompt][:allow_multiple] = msg[:allow_multiple].to_i > 0
form_hash["current_version"]["prompts"] = Array.new
msg[:multiple_choice_prompt]["choices"] = msg[:multiple_choice_prompt]["choices"].values
multiplechoiceprompt = {"multiple_choice_prompt" => msg[:multiple_choice_prompt].clone()}
form_hash["current_version"]["prompts"] << multiplechoiceprompt
form_hash.delete("multiple_choice_prompt")
end
form_hash.delete("prompt")
form_hash.delete("exercise_version")
form_hash.delete("question_type")
arr << form_hash
exercises = ExerciseRepresenter.for_collection.new([]).from_hash(arr)
if exercises[0].save!
redirect_to ex, notice: 'Exercise was successfully created.'
else
#render action: 'new'
redirect_to ex, notice:
"Exercise was NOT created for #{msg} #{@exercise.errors.messages}"
end
end
# -------------------------------------------------------------
def random_exercise
exercise_dump = []
Exercise.where(is_public: true).each do |exercise|
if params[:language] ?
(exercise.language == params[:language]) :
params[:question_type] ?
(exercise.question_type == params[:question_type].to_i) :
true
exercise_dump << exercise
end
end
if exercise_dump.any?
redirect_to exercise_practice_path(exercise_dump.sample) and return
else
filters = []
filters << "language #{params[:language]}" if params[:language]
filters << "question type #{Exercise::TYPE_NAMES[params[:question_type].to_i]}" if params[:question_type]
message = "Sorry, there are currently no public exercises #{filters.any? ? "with #{filters.to_sentence}." : "."}"
redirect_to root_path, notice: message and return
end
end
# -------------------------------------------------------------
# POST exercises/create_mcqs
def create_mcqs
CSV.foreach(params[:form].fetch(:mcqfile).path, {headers: true}) do |row|
if row['Question'].include?('Python')
next
end
exercise = Exercise.new(external_id: row['ID'])
exercise.is_public = false
exercise.language = ''
end
end# def
# -------------------------------------------------------------
# GET exercises/upload_mcqs
def upload_mcqs
end
# -------------------------------------------------------------
# GET exercises/upload_exercises
def upload
end
# -------------------------------------------------------------
def upload_yaml
end
# -------------------------------------------------------------
def yaml_create
@yaml_exers = YAML.load_file(params[:form].fetch(:yamlfile).path)
@yaml_exers.each do |exercise|
@ex = Exercise.new
@ex.name = exercise['name']
@ex.external_id = exercise['external_id']
@ex.is_public = exercise['is_public']
@ex.experience = exercise['experience']
exercise['language_list'].split(",").each do |lang|
print "\nLanguage: ", lang
end
exercise['style_list'].split(",").each do |style|
print "\nStyle: ", style
end
exercise['tag_list'].split(",").each do |tag|
print "\nTag: ", tag
end
version = exercise['current_version']
@ex.versions = version['version']
@ex.save!
@version = ExerciseVersion.new(exercise: @ex,creator_id:
User.find_by(email: version['creator']).andand.id,
position:1)
@version.save!
version['prompts'].each do |prompt|
prompt = prompt['coding_prompt']
@prompt = CodingPrompt.new(exercise_version: @version)
@prompt.question = prompt['question']
@prompt.position = prompt['position']
@prompt.feedback = prompt['feedback']
@prompt.class_name = prompt['class_name']
@prompt.method_name = prompt['method_name']
@prompt.starter_code = prompt['starter_code']
@prompt.wrapper_code = prompt['wrapper_code']
@prompt.test_script = prompt['tests']
@prompt.actable_id = rand(100)
@prompt.save!
end
end
redirect_to exercises_path
end
# -------------------------------------------------------------
def cleanFile(files, fileList)
files.each_with_index do |t, index|
if !fileList.include? t.original_filename
files = files.dup.tap { |i| i.delete_at(index) }
end
end
return files
end
# -------------------------------------------------------------
# POST /exercises/upload_create
def upload_create
if params[:exercise]
exercise_params = params[:exercise]
exercise_version_params = exercise_params[:exercise_version]
use_rights = exercise_params[:exercise_collection_id].to_i
text_representation = exercise_version_params['text_representation']
hash = YAML.load(text_representation)
else
text_representation = File.read(params[:form][:file].path)
hash = YAML.load(text_representation)
use_rights = 0 # Personal exercise
end
if !hash.kind_of?(Array)
hash = [hash]
end
files = exercise_params[:files]
puts "files = #{files.inspect}"
@attached_files = exercise_params[:attached_files]
if @attached_files == "null"
@attached_files = nil
end
if @attached_files
@attached_files = JSON.parse(@attached_files)
end
puts "attached files = #{@attached_files.inspect}"
# figure out if we need to add this to an exercise collection
exercise_collection = nil
if use_rights == 0
exercise_collection = current_user.exercise_collection
if exercise_collection.nil?
exercise_collection = ExerciseCollection.new(
name: "Personal exercise collection belonging to #{current_user.display_name}",
user: current_user
)
exercise_collection.save!
end
elsif use_rights != -1 # then it must be a user group
user_group = UserGroup.find(use_rights)
exercise_collection = user_group.exercise_collection
if exercise_collection.nil?
exercise_collection = ExerciseCollection.new(
name: "Exercise collection for the #{user_group.name} group",
user_group: user_group
)
exercise_collection.save!
end
end
@return_to = session.delete(:return_to) || exercises_path
# parse the text_representation
exercises = ExerciseRepresenter.for_collection.new([]).from_hash(hash)
success_all = true
error_msgs = []
success_msgs = []
exercises.each do |e|
if !e.save
success_all = false
# put together an error message
error_msgs << "Errors while saving exercise #{e.andand.name}:<ul>"
e.errors.full_messages.each do |msg|
error_msgs << "<li>#{msg}</li>"
end
error_msgs << "</ul>"
else # successfully created the exercise
ex_ver = e.current_version
# make the current user an exercise owner if they aren't already
e.add_owner!(current_user)
# copy all retained resource files, skipping any to be removed
prev_version = e.exercise_versions.offset(1).first
if prev_version
puts "processing ownerships from prev version #{prev_version.id}"
prev_version.ownerships.each do |o|
puts "checking ownership #{o.inspect}"
# Double-loop isn't the greatest design, but both lists are short
@attached_files.each do |a|
puts "checking against attachment #{a.inspect}"
# uploaded flag is true if it was previously uploaded
# deleted flag is true if it is to be pruned/removed from exercise
if a['name'] == o.filename && a['uploaded'] && !a['deleted']
puts "adding ownership record"
ownertable = ex_ver.ownerships.create!(
filename: o.filename,
resource_file: o.resource_file)
end
end
end
end
# Now add all newly uploaded attached files
if files
files.each do |file|
puts "processing new upload #{file.inspect}"
@attached_files.each do |a|
puts "checking against attachment #{a.inspect}"
if a['name'] == file.original_filename && !a['uploaded'] && !a['deleted']
Ownership.create!(
filename: file.original_filename,
exercise_version: ex_ver,
resource_file: ResourceFile.for_upload(file, current_user)
)
end
end
end
end
# Add exercise to collection
exercise_collection.andand.add(e, override: true)
# Update the text representation
e.current_version.update(text_representation: text_representation)
# Notify user of success
success_msgs <<
"<li>X#{e.id}: #{e.name} saved, try it #{view_context.link_to 'here', exercise_practice_path(e)}.</li>"
end
end
if success_all
success_msgs = '<ul>' + success_msgs.join("") + '</ul>'
redirect_to @return_to, flash: { success: success_msgs.html_safe } and return
else
if !success_msgs.blank?
error_msgs << 'Some exercises were successfully saved.'
error_msgs << '<ul>' + success_msgs.join('') + '</ul>'
end
redirect_to @return_to, flash: { error: error_msgs.join("").html_safe } and return
end
end
# -------------------------------------------------------------
def embed
if params[:exercise_version_id] || params[:id]
set_exercise_from_params
else
@message = 'Choose an exercise to embed!'
render 'lti/error' and return
end
redirect_to exercise_practice_path(id: @exercise.id, lti_launch: true) and return
end
# -------------------------------------------------------------
def practice
# lti launch
@lti_launch = params[:lti_launch]
if params[:exercise_version_id] || params[:id]
set_exercise_from_params
else
@message = 'Choose an exercise to practice!'
if @lti_launch
render 'lti/error' and return
else
redirect_to exercises_url, notice: @message and return
end
end
@student_user = params[:review_user_id] ? User.find(params[:review_user_id]) : current_user
if params[:workout_offering_id]
@workout_offering =
WorkoutOffering.find_by(id: params[:workout_offering_id])
@workout = @workout_offering.workout
if @workout_offering.time_limit_for(@student_user)
@user_time_limit = @workout_offering.time_limit_for(@student_user)
else
@user_time_limit = nil
end
else
@workout_offering = nil
if params[:workout_id]
@workout = Workout.find(params[:workout_id])
end
end
if @workout_offering
# Re-check workout-offering permission in case the URL was entered directly.
authorize! :practice, @workout_offering
authorize! :practice, @exercise
elsif !@workout
# are they trying to practice the exercise in the gym?
authorize! :gym_practice, @exercise
end
@attempt = nil
@workout_score = params[:workout_score_id] ? WorkoutScore.find(params[:workout_score_id]) : (
@workout_offering ? @workout_offering.score_for(@student_user) : (
@workout ? @workout.score_for(@student_user, @workout_offering) : nil
))
if @student_user
@student_user.current_workout_score = @workout_score ? @workout_score : nil
@student_user.save!
end
if @workout_offering && @workout_score &&
@workout_score.workout_offering != @workout_offering
@workout_score = nil
end
if @workout_offering && !@workout_score
@workout_score = @workout_offering.score_for(@student_user)
end
if @workout_score
if @workout_score.lis_result_sourcedid.nil? && @workout_score.lis_outcome_service_url.nil?
@workout_score.lis_result_sourcedid = params[:lis_result_sourcedid]
@workout_score.lis_outcome_service_url = params[:lis_outcome_service_url]
@workout_score.save
# we set LMS gradebook ties for the first time, so force-send scores to LMS
@workout_score.update_lti if @workout_score.score
end
should_force_lti = !@lti_launch &&
@workout_offering.andand.lms_assignment_id.present? &&
(@workout_score.andand.lis_result_sourcedid.nil? ||
@workout_score.andand.lis_outcome_service_url.nil?)
if should_force_lti && !current_user.manages?(@workout_offering.course_offering)
@message = "This assignment must be accessed through your course's " +
"Learning Management System (like Canvas)."
@redirect_url = @workout_offering.lms_assignment_url
render 'lti/error' and return
end
@attempts_left = @workout_score
.attempts_left_for_exercise_version(@exercise_version)
end
@workout ||= @workout_score ? @workout_score.workout : nil
manages_course = current_user.andand.global_role.andand.is_admin? ||
@workout_offering.andand.course_offering.andand.is_manager?(current_user)
if !manages_course && @workout_score.andand.closed? &&
@workout_offering.andand.workout_policy.andand.no_review_before_close &&
!@workout_offering.andand.shutdown?
path = root_path
if @workout_offering
path = organization_workout_offering_path(
organization_id:
@workout_offering.course_offering.course.organization.slug,
course_id: @workout_offering.course_offering.course.slug,
term_id: @workout_offering.course_offering.term.slug,
id: @workout_offering.id)
elsif @workout
path = workout_path(@workout)
end
redirect_to path,
notice: "The time limit has passed for this workout." and return
end
@msg = nil
@user_deadline = nil
student_review = false
if @user_time_limit
if @workout_score.andand.closed?
@msg = 'The time limit has passed. This assignment is closed and no ' +
'longer accepting submissions.'
student_review = true
else
start_time = @workout_score ? @workout_score.started_at : Time.zone.now
@user_deadline = start_time + @user_time_limit.minutes
if @workout_offering.hard_deadline_for(current_user)
@user_deadline = [@user_deadline,
@workout_offering.hard_deadline_for(current_user)].min
end
@user_deadline = @user_deadline.to_s
@user_deadline = @user_deadline.split(" ")[0] + "T" + @user_deadline.split(" ")[1]
@server_now = Time.zone.now.to_s
@server_now = @server_now.split(" ")[0] + "T" + @server_now.split(" ")[1]
@msg = 'Time remaining - ##:##'
end
elsif @workout_offering && !@workout_offering.andand.can_be_practiced_by?(current_user)
@msg = 'This assignment is now closed and no longer accepting submissions.'
student_review = true
end
# display the scored attempt if in review mode (for students or instructors)
if @workout_score
@attempt = (params[:review_user_id] || student_review) ?
@workout_score.scoring_attempt_for(@exercise_version.exercise) :
@workout_score.previous_attempt_for(@exercise_version.exercise)
end
if @workout.andand.exercise_workouts.andand.where(
exercise: @exercise).andand.any?
@max_points = @workout.exercise_workouts.
where(exercise: @exercise).first.points
end
@responses = ['There are no responses yet!']
@explain = ['There are no explanations yet!']
# safety code to prune long session data
if session[:leaf_exercises] && session[:leaf_exercises].length > 5
session[:leaf_exercises].shift(session[:leaf_exercises].length - 5)
end
# Only add exercise to leaf list if this isn't part of a workout
@workout ||= @workout_score.andand.workout ||
@workout_offering.andand.workout
if !@workout
if session[:leaf_exercises]
if session[:leaf_exercises] && session[:leaf_exercises].length >= 5
session[:leaf_exercises].shift(1)
end
session[:leaf_exercises] << @exercise.id
else
session[:leaf_exercises] = [@exercise.id]
end
end
# EOL stands for end of line
# @wexs is the variable to hold the list of exercises of this workout
# yet to be attempted by the user apart from the current exercise
if params[:wexes] != 'EOL'
@wexs = params[:wexes] || session[:remaining_wexes]
else
@wexs = nil
end
# decide whether or not to hide the sidebar
# hide it if this workout (if present) has less than two exercises
ex_count = @workout.andand.exercises.andand.count
@hide_sidebar = (!@workout && @lti_launch) || (ex_count && ex_count < 2)
# Updata image tags in the exercise question
@exercise_version.image_processing(true)
# Display all files to students
@file_res = @exercise_version.file_processing
render layout: 'two_columns'
end
# -------------------------------------------------------------
def create_choice
@ans = Choice.create
@pick.push()
end
# -------------------------------------------------------------
#GET /evaluate/1
def evaluate
@lti_launch = params[:lti_launch]
if params[:exercise_version_id] || params[:id]
set_exercise_from_params
else
@message = 'Choose an exercise to evaluate!'
if @lti_launch
render 'lti/error' and return
else
redirect_to exercises_url,
notice: 'Choose an exercise to practice!' and return
end
end
if current_user
@student_drift_user = current_user
elsif session[:student_drift_user_id]
@student_drift_user = User.find(session[:student_drift_user_id])
else
user_ip = request.remote_ip.clone()
fake_email = user_ip.clone().gsub('.','') + Time.now.to_i.to_s + '@cw.edu'
fake_password = Time.now.to_i.to_s + user_ip.clone().gsub('.','')
@student_drift_user = User.new(email: fake_email, slug: fake_email,
password: fake_password,
current_sign_in_ip: request.remote_ip,
last_sign_in_ip: request.remote_ip,
global_role_id: 4)
@student_drift_user.save!
session[:student_drift_user_id] = @student_drift_user.id
end
@workout = nil
@workout_offering = nil
if params[:workout_offering_id]
@workout_offering =
WorkoutOffering.find_by(id: params[:workout_offering_id])
if @workout_offering &&
!@workout_offering.workout.contains?(@exercise_version.exercise)
@workout_offering = nil
end
else
@workout_offering = nil
end
if @workout_offering.nil? &&
@student_drift_user.andand.current_workout_score &&
@student_drift_user.current_workout_score.workout.contains?(
@exercise_version.exercise)
@workout_offering = @student_drift_user.current_workout_score.workout_offering
if @workout_offering.nil?
@workout = @student_drift_user.current_workout_score.workout
end
end
if @workout.nil?
if @workout_offering
@workout = @workout_offering.workout
elsif params[:workout_id]
@workout = Workout.find_by(id: params[:workout_id])
end
end
if @workout.nil? && session[:current_workout]
@workout = Workout.find_by(id: session[:current_workout])
if !@workout.contains?(@exercise_version.exercise)
@workout = nil
end
end
@workout_score = nil
if @workout_offering
@workout_score = @workout_offering.score_for(@student_drift_user)
elsif @workout
@workout_score = @workout.score_for(@student_drift_user, nil,
params[:lis_outcome_service_url],
params[:lis_result_sourcedid])
end
# Has the allotted time for the workout offering passed?
if @workout_score.andand.closed? &&
!@workout_offering.andand.can_be_practiced_by?(@student_drift_user)
p 'WARNING: attempt to evaluate exercise after time expired.'
return
end
@attempts_left = @workout_score ?
@workout_score.attempts_left_for_exercise_version(@exercise_version)
: nil
if current_user &&
!current_user.is_staff?(@workout_offering.andand.course_offering) &&
@attempts_left &&
@attempts_left <= 0
p 'WARNING: attempt to evaluate workout_offering after attempts expired.'
return
end
# update the in-memory count of attempts_left, so it is represented
# in the partial
@attempts_left = (@attempts_left && @attempts_left > 0) ?
@attempts_left - 1 : @attempts_left
@attempt = @exercise_version.new_attempt(
user: @student_drift_user, workout_score: @workout_score)
@attempt.save!
# FIXME: Need to make it work for multiple prompts
# prompt = @exercise_version.prompts.first.specific
# prompt_answer = @attempt.prompt_answers.first # already specific here
if @workout.andand.exercise_workouts.andand.where(exercise: @exercise).andand.any?
@max_points = @workout.exercise_workouts.
where(exercise: @exercise).first.points
else # case when exercise being practised is not part of any workout
@max_points = 10.0
end
if @exercise_version.is_mcq?
if params[:exercise_version]
# Usual single prompt question
response_ids = params[:exercise_version][:choice][:id]
else
# Multi-prompt questions
prompt_keys = params.keys.select{|key| key.include?("prompt-") }
response_ids = prompt_keys.map{|prompt_key| params[prompt_key] }
prompt_keys.each_with_index do |prompt_key, i|
@attempt.prompt_answers[i].choices << Choice.find(response_ids[i])
end
end
@responses = Array.new
if response_ids.class == Array
# Remove blank responses and duplicates
response_ids.reject!(&:blank?)
response_ids.uniq!
response_ids.compact!
# FIXME: Assumes no multi-choice answers
response_ids.each do |r|
@responses.push(Choice.find(r))
end
else
@responses.push(Choice.find(response_ids))
end
@responses.compact!
@responses.each do |answer|
answer[:answer] = markdown(answer[:answer])
end
# recording the answer choices
# FIXME: Only temporary
if params[:exercise_version]
@attempt.prompt_answers.first.choices = @responses
end
@score = @exercise_version.score(@responses)
if @workout
@score = @score * @max_points / @exercise_version.max_mcq_score
end
# TODO: Enable @explain and @exercise_feedback again
#@explain = @exercise_version.collate_feedback(@responses)
@exercise_feedback = 'You have attempted exercise '
# + "#{@exercise.id}:#{@exercise.name}" +
# ' and its feedback for you: ' +
# @explain.to_sentence
# TODO: calculate experience based on correctness and num submissions
# using count_submission()
@xp = @exercise_version.mcq_experience_on(@responses, @attempt.submit_num)
@attempt.score = @score
@attempt.feedback_ready = true
@attempt.experience_earned = @xp
@attempt.save!
if @workout_score
@workout_score.record_attempt(@attempt)
end
if @max_points <= @attempt.score ||
!@workout_score.andand.show_feedback?
@is_perfect = true
end
if @is_perfect
flash.notice = "Your previous question's answer choice has been saved and scored"
if @workout_score.andand.workout_offering
@workout_offering = @workout_score.workout_offering
render :js => "window.location = '" +
organization_workout_offering_practice_path(
exercise_id: @workout_score.workout.next_exercise(@exercise),
organization_id: @workout_offering.course_offering.course.organization.slug,
course_id: @workout_offering.course_offering.course.slug,
term_id: @workout_offering.course_offering.term.slug,
id: @workout_offering.id,
lti_launch: @lti_launch) + "' "
elsif @workout_score.andand.workout
render :js => "window.location = '" +
exercise_practice_path(
@workout_score.workout.next_exercise(@exercise),
workout_id: @workout_score.workout.id,
workout_score_id: @workout_score.andand.id,
lti_launch: @lti_launch) + "' "
end
end
elsif @exercise_version.is_coding?
@answer_code = params[:exercise_version][:answer_code]
@exercise_version.prompts.each_with_index do |exercise_prompt, i|
exercise_prompt_answer = @attempt.prompt_answers[i]
exercise_prompt_answer.answer = params[:exercise_version][:answer_code]
if exercise_prompt_answer.save
CodeWorker.new.async.perform(@attempt.id)
else
puts 'IMPROPER PROMPT',
'unable to save prompt_answer: ' \
"#{prompt_answer.errors.full_messages.to_s}",
'IMPROPER PROMPT'
end
end
@workout ||= @workout_score.andand.workout
end
end
# -------------------------------------------------------------
# PATCH/PUT /exercises/1
def update
new_exercise = create_new_version()
@exercise.base_exercise.exercises << new_exercise
new_exercise.save
@exercise.base_exercise.current_version = new_exercise
@exercise.base_exercise.save
if new_exercise.update_attributes(exercise_params)
respond_to do |format|
format.html do
redirect_to new_exercise,
notice: 'Exercise was successfully updated.'
end
format.json { head :no_content } # 204 No Content
end
else
respond_to do |format|
format.html { render action: 'edit' }
format.json do
render json: new_exercise.errors, status: :unprocessable_entity
end
end
end
end
# -------------------------------------------------------------
# DELETE /exercises/1
def destroy
@exercise.destroy
redirect_to exercises_url, notice: 'Exercise was successfully destroyed.'
end
# -------------------------------------------------------------
# OpenPOP support
def call_open_pop
require 'rest-client'
require 'json'
payload = {'exercise_id' => params[:exercise_id].strip,
'code' => params[:code].strip
}
request = RestClient::Request.execute(:method => :post,
:url => HTTP_URL,
:payload => payload.to_json,
:headers => {'Content-Type' => 'application/json'},
:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
trace = JSON.parse(request.body)
@openpop_results = trace
curr_user = nil
unless current_user.nil?
curr_user = current_user