forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradebooks_controller.rb
More file actions
895 lines (793 loc) · 36.6 KB
/
Copy pathgradebooks_controller.rb
File metadata and controls
895 lines (793 loc) · 36.6 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
#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class GradebooksController < ApplicationController
include ActionView::Helpers::NumberHelper
include GradebooksHelper
include KalturaHelper
include Api::V1::AssignmentGroup
include Api::V1::Submission
include Api::V1::CustomGradebookColumn
include Api::V1::Section
before_action :require_context
before_action :require_user, only: [:speed_grader, :speed_grader_settings, :grade_summary]
batch_jobs_in_actions :only => :update_submission, :batch => { :priority => Delayed::LOW_PRIORITY }
add_crumb(proc { t '#crumbs.grades', "Grades" }) { |c| c.send :named_context_url, c.instance_variable_get("@context"), :context_grades_url }
before_action { |c| c.active_tab = "grades" }
MAX_POST_GRADES_TOOLS = 10
def grade_summary
set_current_grading_period if grading_periods?
@presenter = grade_summary_presenter
# do this as the very first thing, if the current user is a
# teacher in the course and they are not trying to view another
# user's grades, redirect them to the gradebook
if @presenter.user_needs_redirection?
return redirect_to polymorphic_url([@context, 'gradebook'])
end
if !@presenter.student || !@presenter.student_enrollment
return render_unauthorized_action
end
return unless authorized_action(@context, @current_user, :read) &&
authorized_action(@presenter.student_enrollment, @current_user, :read_grades)
log_asset_access([ "grades", @context ], "grades", "other")
return render :grade_summary_list unless @presenter.student
add_crumb(@presenter.student_name, named_context_url(@context, :context_student_grades_url,
@presenter.student_id))
gp_id = nil
if grading_periods?
@grading_periods = active_grading_periods_json
gp_id = @current_grading_period_id unless view_all_grading_periods?
effective_due_dates = EffectiveDueDates.new(@context).to_hash
end
@exclude_total = exclude_total?(@context)
Shackles.activate(:slave) do
# run these queries on the slave database for speed
@presenter.assignments
aggregate_assignments
@presenter.submissions
@presenter.submission_counts
@presenter.assignment_stats
end
submissions_json = @presenter.submissions.
select { |s| s.user_can_read_grade?(@current_user) }.
map do |s|
{
assignment_id: s.assignment_id,
score: s.score,
excused: s.excused?,
workflow_state: s.workflow_state,
}
end
grading_period = @grading_periods && @grading_periods.find { |period| period[:id] == gp_id }
ags_json = light_weight_ags_json(@presenter.groups, {student: @presenter.student})
grading_scheme = @context.grading_standard.try(:data) ||
GradingStandard.default_grading_standard
js_env(submissions: submissions_json,
assignment_groups: ags_json,
group_weighting_scheme: @context.group_weighting_scheme,
show_total_grade_as_points: @context.settings[:show_total_grade_as_points],
grading_scheme: grading_scheme,
grading_period_set: grading_period_group_json,
grading_period: grading_period,
grading_periods: @grading_periods,
effective_due_dates: effective_due_dates,
exclude_total: @exclude_total,
student_outcome_gradebook_enabled: @context.feature_enabled?(:student_outcome_gradebook),
student_id: @presenter.student_id)
end
def save_assignment_order
if authorized_action(@context, @current_user, :read)
whitelisted_orders = {
'due_at' => :due_at, 'title' => :title,
'module' => :module, 'assignment_group' => :assignment_group
}
assignment_order = whitelisted_orders.fetch(params.fetch(:assignment_order), :due_at)
@current_user.preferences[:course_grades_assignment_order] ||= {}
@current_user.preferences[:course_grades_assignment_order][@context.id] = assignment_order
@current_user.save!
if CANVAS_RAILS4_2
redirect_to :back
else
redirect_back(fallback_location: course_grades_url(@context))
end
end
end
def light_weight_ags_json(assignment_groups, opts={})
assignment_groups.map do |ag|
visible_assignments = ag.visible_assignments(opts[:student] || @current_user).to_a
if grading_periods? && @current_grading_period_id && !view_all_grading_periods?
current_period = GradingPeriod.for(@context).find_by(id: @current_grading_period_id)
visible_assignments = current_period.assignments_for_student(visible_assignments, opts[:student])
end
visible_assignments.map! do |a|
{
id: a.id,
submission_types: a.submission_types_array,
points_possible: a.points_possible,
due_at: a.due_at,
omit_from_final_grade: a.omit_from_final_grade?,
muted: a.muted?
}
end
{
id: ag.id,
rules: ag.rules_hash({stringify_json_ids: true}),
group_weight: ag.group_weight,
assignments: visible_assignments,
}
end
end
def grading_rubrics
@rubric_contexts = @context.rubric_contexts(@current_user)
if params[:context_code]
context = @rubric_contexts.detect{|r| r[:context_code] == params[:context_code] }
@rubric_context = @context
if context
@rubric_context = Context.find_by_asset_string(params[:context_code])
end
@rubric_associations = @context.sorted_rubrics(@current_user, @rubric_context)
render :json => @rubric_associations.map{ |r| r.as_json(methods: [:context_name], include: {:rubric => {:include_root => false}}) }
else
render :json => @rubric_contexts
end
end
def show
if authorized_action(@context, @current_user, [:manage_grades, :view_all_grades])
@last_exported_gradebook_csv = GradebookCsv.last_successful_export(course: @context, user: @current_user)
set_current_grading_period if grading_periods?
set_js_env
set_tutorial_js_env
@course_is_concluded = @context.completed?
@post_grades_tools = post_grades_tools
version = @current_user.preferred_gradebook_version
if @context.root_account.feature_enabled?(:gradezilla)
# params[:version] is a temporary gradezilla feature to help devs flip back and forth
# between gradebook versions. This param should never be used in the UI.
case params[:version]
when 'srgb'
render :screenreader and return
when '2'
render :gradebook and return
when 'gradezilla-individual'
render 'gradebooks/gradezilla/individual' and return
when 'gradezilla-gradebook'
render 'gradebooks/gradezilla/gradebook' and return
else # fallback to the current user's preferences hash
render 'gradebooks/gradezilla/individual' and return if version == 'individual'
render 'gradebooks/gradezilla/gradebook' and return
end
else
render :screenreader and return if version == 'srgb'
render :gradebook and return
end
end
end
def post_grades_ltis
@post_grades_ltis ||= self.external_tools.map { |tool| external_tool_detail(tool) }
end
def post_grades_tools
tool_limit = @context.feature_enabled?(:post_grades) ? MAX_POST_GRADES_TOOLS - 1 : MAX_POST_GRADES_TOOLS
tools = post_grades_ltis[0...tool_limit]
tools.push(type: :post_grades) if @context.feature_enabled?(:post_grades)
tools
end
def external_tool_detail(tool)
post_grades_placement = tool[:placements][:post_grades]
{
id: tool[:definition_id],
data_url: post_grades_placement[:canvas_launch_url],
name: tool[:name],
type: :lti,
data_width: post_grades_placement[:launch_width],
data_height: post_grades_placement[:launch_height]
}
end
def external_tools
bookmarked_collection = Lti::AppLaunchCollator.bookmarked_collection(@context, [:post_grades])
tools = bookmarked_collection.paginate(per_page: MAX_POST_GRADES_TOOLS + 1).to_a
launch_definitions = Lti::AppLaunchCollator.launch_definitions(tools, [:post_grades])
launch_definitions.each do |launch_definition|
case launch_definition[:definition_type]
when 'ContextExternalTool'
url = external_tool_url_for_lti1(launch_definition)
when 'Lti::MessageHandler'
url = external_tool_url_for_lti2(launch_definition)
end
launch_definition[:placements][:post_grades][:canvas_launch_url] = url
end
launch_definitions
end
def external_tool_url_for_lti1(launch_definition)
polymorphic_url(
[@context, :external_tool],
id: launch_definition[:definition_id],
display: 'borderless',
launch_type: 'post_grades',
)
end
def external_tool_url_for_lti2(launch_definition)
polymorphic_url(
[@context, :basic_lti_launch_request],
message_handler_id: launch_definition[:definition_id],
display: 'borderless',
)
end
def set_current_grading_period
if params[:grading_period_id].present?
@current_grading_period_id = params[:grading_period_id].to_i
else
return if view_all_grading_periods?
current = GradingPeriod.current_period_for(@context)
@current_grading_period_id = current ? current.id : 0
end
end
def view_all_grading_periods?
@current_grading_period_id == 0
end
def grading_period_group
return @grading_period_group if defined? @grading_period_group
@grading_period_group = active_grading_periods.first&.grading_period_group
end
def active_grading_periods
@active_grading_periods ||= GradingPeriod.for(@context).sort_by(&:start_date)
end
def grading_period_group_json
return @grading_period_group_json if defined? @grading_period_group_json
return @grading_period_group_json = nil unless grading_period_group.present?
@grading_period_group_json = grading_period_group
.as_json
.fetch(:grading_period_group)
.merge(grading_periods: active_grading_periods_json)
end
def active_grading_periods_json
@agp_json ||= GradingPeriod.periods_json(active_grading_periods, @current_user)
end
def set_js_env
@gradebook_is_editable = @context.grants_right?(@current_user, session, :manage_grades)
per_page = Setting.get('api_max_per_page', '50').to_i
teacher_notes = @context.custom_gradebook_columns.not_deleted.where(teacher_notes: true).first
ag_includes = [:assignments, :assignment_visibility]
chunk_size = if @context.assignments.published.count < Setting.get('gradebook2.assignments_threshold', '20').to_i
Setting.get('gradebook2.submissions_chunk_size', '35').to_i
else
Setting.get('gradebook2.many_submissions_chunk_size', '10').to_i
end
js_env STUDENT_CONTEXT_CARDS_ENABLED: @domain_root_account.feature_enabled?(:student_context_cards)
js_env GRADEBOOK_OPTIONS: {
gradezilla: @context.root_account.feature_enabled?(:gradezilla),
chunk_size: chunk_size,
assignment_groups_url: api_v1_course_assignment_groups_url(
@context,
include: ag_includes,
override_assignment_dates: "false",
exclude_assignment_submission_types: ['wiki_page']
),
context_modules_url: api_v1_course_context_modules_url(@context),
sections_url: api_v1_course_sections_url(@context),
course_url: api_v1_course_url(@context),
effective_due_dates_url: api_v1_course_effective_due_dates_url(@context),
enrollments_url: custom_course_enrollments_api_url(per_page: per_page),
enrollments_with_concluded_url:
custom_course_enrollments_api_url(include_concluded: true, per_page: per_page),
enrollments_with_inactive_url:
custom_course_enrollments_api_url(include_inactive: true, per_page: per_page),
enrollments_with_concluded_and_inactive_url:
custom_course_enrollments_api_url(include_concluded: true, include_inactive: true, per_page: per_page),
students_url: custom_course_users_api_url(per_page: per_page),
students_with_concluded_enrollments_url:
custom_course_users_api_url(include_concluded: true, per_page: per_page),
students_with_inactive_enrollments_url:
custom_course_users_api_url(include_inactive: true, per_page: per_page),
students_with_concluded_and_inactive_enrollments_url:
custom_course_users_api_url(include_concluded: true, include_inactive: true, per_page: per_page),
submissions_url: api_v1_course_student_submissions_url(@context, grouped: '1'),
outcome_links_url: api_v1_course_outcome_group_links_url(@context, outcome_style: :full),
outcome_rollups_url: api_v1_course_outcome_rollups_url(@context, per_page: 100),
change_grade_url:
api_v1_course_assignment_submission_url(@context, ":assignment", ":submission", include: [:visibility]),
context_url: named_context_url(@context, :context_url),
download_assignment_submissions_url:
named_context_url(@context, :context_assignment_submissions_url, "{{ assignment_id }}", zip: 1),
re_upload_submissions_url:
named_context_url(@context, :submissions_upload_context_gradebook_url, "{{ assignment_id }}"),
context_id: @context.id.to_s,
context_code: @context.asset_string,
context_sis_id: @context.sis_source_id,
group_weighting_scheme: @context.group_weighting_scheme,
grading_standard: (
@context.grading_standard_enabled? &&
(@context.grading_standard.try(:data) || GradingStandard.default_grading_standard)
),
course_is_concluded: @context.completed?,
course_name: @context.name,
gradebook_is_editable: @gradebook_is_editable,
context_allows_gradebook_uploads: @context.allows_gradebook_uploads?,
gradebook_import_url: new_course_gradebook_upload_path(@context),
setting_update_url: api_v1_course_settings_url(@context),
show_total_grade_as_points: @context.settings[:show_total_grade_as_points],
publish_to_sis_enabled: @context.allows_grade_publishing_by(@current_user) && @gradebook_is_editable,
publish_to_sis_url: context_url(@context, :context_details_url, anchor: 'tab-grade-publishing'),
speed_grader_enabled: @context.allows_speed_grader?,
active_grading_periods: active_grading_periods_json,
grading_period_set: grading_period_group_json,
current_grading_period_id: @current_grading_period_id,
outcome_gradebook_enabled: @context.feature_enabled?(:outcome_gradebook),
custom_columns_url: api_v1_course_custom_gradebook_columns_url(@context),
custom_column_url: api_v1_course_custom_gradebook_column_url(@context, ":id"),
custom_column_data_url: api_v1_course_custom_gradebook_column_data_url(@context, ":id", per_page: per_page),
custom_column_datum_url: api_v1_course_custom_gradebook_column_datum_url(@context, ":id", ":user_id"),
reorder_custom_columns_url: api_v1_custom_gradebook_columns_reorder_url(@context),
teacher_notes: teacher_notes && custom_gradebook_column_json(teacher_notes, @current_user, session),
change_gradebook_version_url: context_url(@context, :change_gradebook_version_context_gradebook_url, version: 2),
export_gradebook_csv_url: course_gradebook_csv_url,
gradebook_csv_progress: @last_exported_gradebook_csv.try(:progress),
attachment_url: @last_exported_gradebook_csv.try(:attachment).try(:download_url),
attachment: @last_exported_gradebook_csv.try(:attachment),
sis_app_url: Setting.get('sis_app_url', nil),
sis_app_token: Setting.get('sis_app_token', nil),
list_students_by_sortable_name_enabled: @context.list_students_by_sortable_name?,
gradebook_column_size_settings: @current_user.preferences[:gradebook_column_size],
gradebook_column_size_settings_url: change_gradebook_column_size_course_gradebook_url,
gradebook_column_order_settings: @current_user.preferences[:gradebook_column_order].try(:[], @context.id),
gradebook_column_order_settings_url: save_gradebook_column_order_course_gradebook_url,
post_grades_ltis: post_grades_ltis,
post_grades_feature: post_grades_feature?,
sections: sections_json(@context.active_course_sections, @current_user, session),
settings_update_url: api_v1_course_gradebook_settings_update_url(@context),
settings: gradebook_settings.fetch(@context.id, {}),
login_handle_name: @context.root_account.settings[:login_handle_name],
sis_name: @context.root_account.settings[:sis_name],
version: params.fetch(:version, nil),
colors: gradebook_settings.fetch(:colors, {})
}
end
def post_grades_feature?
@context.feature_enabled?(:post_grades) &&
@context.allows_grade_publishing_by(@current_user) &&
can_do(@context, @current_user, :manage_grades)
end
def history
if authorized_action(@context, @current_user, :manage_grades)
return new_history if @context.root_account.feature_enabled?(:new_gradebook_history)
#
# Temporary disabling of this page for large courses
# We need some reworking of the gradebook history to allow using it
# in large courses in a performant manner. Until that happens, we're
# disabling it over a certain threshold.
#
submissions_count = @context.submissions.count
submissions_limit = Setting.get('gradebook_history_submission_count_threshold', '0').to_i
if submissions_limit == 0 || submissions_count <= submissions_limit
# TODO this whole thing could go a LOT faster if you just got ALL the versions of ALL the submissions in this course then did a ruby sort_by day then grader
@days = SubmissionList.days(@context)
end
respond_to do |format|
format.html
end
end
end
# TODO: stop using this for speedgrader
def update_submission
if authorized_action(@context, @current_user, :manage_grades)
if params[:submissions].blank? && params[:submission].blank?
render nothing: true, status: 400
return
end
submissions = if params[:submissions]
params[:submissions].values.map { |s| ActionController::Parameters.new(s) }
else
[params[:submission]]
end
valid_user_ids = Set.new(@context.students_visible_to(@current_user, include: :inactive).pluck(:id))
submissions.select! { |s| valid_user_ids.include? s[:user_id].to_i }
users = @context.admin_visible_students.uniq.find(submissions.map { |s| s[:user_id] })
.index_by(&:id)
assignments = @context.assignments.active.find(submissions.map { |s|
s[:assignment_id]
}).index_by(&:id)
request_error_status = nil
@submissions = []
submissions.compact.each do |submission|
@assignment = assignments[submission[:assignment_id].to_i]
@user = users[submission[:user_id].to_i]
submission = submission.permit(:grade, :score, :excuse, :excused,
:graded_anonymously, :provisional, :final,
:comment, :media_comment_id)
submission[:grader] = @current_user
submission.delete(:provisional) unless @assignment.moderated_grading?
if params[:attachments]
attachments = []
params[:attachments].keys.each do |idx|
attachment = params[:attachments][idx].permit(Attachment.permitted_attributes)
attachment[:user] = @current_user
attachments << @assignment.attachments.create(attachment)
end
submission[:comment_attachments] = attachments
end
begin
if [:grade, :score, :excuse, :excused].any? { |k| submission.key? k }
# if it's a percentage graded assignment, we need to ensure there's a
# percent sign on the end. eventually this will probably be done in
# the javascript.
if @assignment.grading_type == "percent" && submission[:grade] && submission[:grade] !~ /%\z/
submission[:grade] = "#{submission[:grade]}%"
end
submission[:dont_overwrite_grade] = value_to_boolean(params[:dont_overwrite_grades])
submission.delete(:final) if submission[:final] && !@context.grants_right?(@current_user, :moderate_grades)
subs = @assignment.grade_student(@user, submission)
if submission[:provisional]
subs.each do |sub|
sub.apply_provisional_grade_filter!(sub.provisional_grade(@current_user, final: submission[:final]))
end
end
@submissions += subs
end
if [:comment, :media_comment_id, :comment_attachments].any? { |k| submission.key? k }
submission[:commenter] = @current_user
submission[:hidden] = @assignment.muted?
subs = @assignment.update_submission(@user, submission)
if submission[:provisional]
subs.each do |sub|
sub.apply_provisional_grade_filter!(sub.provisional_grade(@current_user, final: submission[:final]))
end
end
@submissions += subs
end
rescue Assignment::GradeError => e
logger.info "GRADES: grade_student failed because '#{e.message}'"
request_error_status = e.status_code
@error_message = e.to_s
end
end
@submissions = @submissions.reverse.uniq.reverse
@submissions = nil if submissions.empty? # no valid submissions
respond_to do |format|
if @submissions && !@error_message#&& !@submission.errors || @submission.errors.empty?
flash[:notice] = t('notices.updated', 'Assignment submission was successfully updated.')
format.html { redirect_to course_gradebook_url(@assignment.context) }
format.json {
render :json => submissions_json, :status => :created, :location => course_gradebook_url(@assignment.context)
}
format.text {
render :json => submissions_json, :status => :created, :location => course_gradebook_url(@assignment.context),
:as_text => true
}
else
flash[:error] = t('errors.submission_failed', "Submission was unsuccessful: %{error}", :error => @error_message || t('errors.submission_failed_default', 'Submission Failed'))
request_error_status ||= :bad_request
format.html { render :show, course_id: @assignment.context.id }
format.json { render json: { errors: { base: @error_message } }, status: request_error_status }
format.text { render json: { errors: { base: @error_message } }, status: request_error_status }
end
end
end
end
def submissions_json
@submissions.map do |s|
json = s.as_json(Submission.json_serialization_full_parameters)
json['submission']['provisional_grade_id'] = s.provisional_grade_id if s.provisional_grade_id
json
end
end
def submissions_zip_upload
return unless authorized_action(@context, @current_user, :manage_grades)
unless @context.allows_gradebook_uploads?
flash[:error] = t('errors.not_allowed', "This course does not allow score uploads.")
redirect_to named_context_url(@context, :context_assignment_url, @assignment.id)
return
end
@assignment = @context.assignments.active.find(params[:assignment_id])
if !params[:submissions_zip] || params[:submissions_zip].is_a?(String)
flash[:error] = t('errors.missing_file', "Could not find file to upload")
redirect_to named_context_url(@context, :context_assignment_url, @assignment.id)
return
end
@comments, @failures = @assignment.generate_comments_from_files(params[:submissions_zip].path, @current_user)
flash[:notice] = t('notices.uploaded',
{ :one => "Files and comments created for 1 submission",
:other => "Files and comments created for %{count} submissions" },
:count => @comments.length)
end
def speed_grader
if !@context.allows_speed_grader?
flash[:notice] = t(:speed_grader_disabled, 'SpeedGrader is disabled for this course')
return redirect_to(course_gradebook_path(@context))
end
return unless authorized_action(@context, @current_user, [:manage_grades, :view_all_grades])
@assignment = @context.assignments.active.find(params[:assignment_id])
if @assignment.unpublished?
flash[:notice] = t(:speedgrader_enabled_only_for_published_content,
'SpeedGrader is enabled only for published content.')
return redirect_to polymorphic_url([@context, @assignment])
end
if canvadoc_annotations_enabled_in_firefox? ||
submisions_attachment_crocodocable_in_firefox?(@assignment.submissions)
flash[:notice] = t("Warning: Crocodoc has limitations when used in Firefox. Comments will not always be saved.")
end
grading_role = if moderated_grading_enabled_and_no_grades_published
if @context.grants_right?(@current_user, :moderate_grades)
:moderator
else
:provisional_grader
end
else
:grader
end
respond_to do |format|
format.html do
@headers = false
@outer_frame = true
log_asset_access([ "speed_grader", @context ], "grades", "other")
env = {
:CONTEXT_ACTION_SOURCE => :speed_grader,
:settings_url => speed_grader_settings_course_gradebook_path,
:force_anonymous_grading => force_anonymous_grading?(@assignment),
:grading_role => grading_role,
:grading_type => @assignment.grading_type,
:lti_retrieve_url => retrieve_course_external_tools_url(
@context.id, assignment_id: @assignment.id, display: 'borderless'
),
:course_id => @context.id,
:assignment_id => @assignment.id,
}
if [:moderator, :provisional_grader].include?(grading_role)
env[:provisional_status_url] = api_v1_course_assignment_provisional_status_path(@context.id, @assignment.id)
end
if grading_role == :moderator
env[:provisional_copy_url] = api_v1_copy_to_final_mark_path(@context.id, @assignment.id, "{{provisional_grade_id}}")
env[:provisional_select_url] = api_v1_select_provisional_grade_path(@context.id, @assignment.id, "{{provisional_grade_id}}")
end
if @assignment.quiz
env[:quiz_history_url] = course_quiz_history_path @context.id,
@assignment.quiz.id,
:user_id => "{{user_id}}"
end
append_sis_data(env)
js_env(env)
render
end
format.json do
render json: Assignment::SpeedGrader.new(
@assignment,
@current_user,
avatars: service_enabled?(:avatars),
grading_role: grading_role
).json
end
end
end
def speed_grader_settings
grade_by_question = value_to_boolean(params[:enable_speedgrader_grade_by_question])
@current_user.preferences[:enable_speedgrader_grade_by_question] = grade_by_question
@current_user.save!
head :ok
end
def blank_submission
@headers = false
end
def change_gradebook_column_size
if authorized_action(@context, @current_user, :manage_grades)
unless @current_user.preferences.key?(:gradebook_column_size)
@current_user.preferences[:gradebook_column_size] = {}
end
@current_user.preferences[:gradebook_column_size][params[:column_id]] = params[:column_size]
@current_user.save!
render json: nil
end
end
def save_gradebook_column_order
if authorized_action(@context, @current_user, :manage_grades)
unless @current_user.preferences.key?(:gradebook_column_order)
@current_user.preferences[:gradebook_column_order] = {}
end
@current_user.preferences[:gradebook_column_order][@context.id] = params[:column_order].to_hash.with_indifferent_access
@current_user.save!
render json: nil
end
end
def change_gradebook_version
@current_user.preferences[:gradebook_version] = params[:version]
@current_user.save!
redirect_to polymorphic_url([@context, 'gradebook'])
end
private
def set_gradebook_warnings(groups, assignments)
@assignments_in_bad_groups = Set.new
if @context.group_weighting_scheme == "percent"
assignments_by_group = assignments.group_by(&:assignment_group_id)
bad_groups = groups.select do |group|
group_assignments = assignments_by_group[group.id] || []
points_in_group = group_assignments.map(&:points_possible).compact.sum
points_in_group.zero?
end
bad_group_ids = bad_groups.map(&:id)
bad_assignment_ids = assignments_by_group.
slice(*bad_group_ids).
values.
flatten
@assignments_in_bad_groups.replace bad_assignment_ids
warning = t('invalid_assignment_groups_warning',
{:one => "Score does not include %{groups} because " \
"it has no points possible",
:other => "Score does not include %{groups} because " \
"they have no points possible"},
:groups => bad_groups.map(&:name).to_sentence,
:count => bad_groups.size)
else
if assignments.all? { |a| (a.points_possible || 0).zero? }
warning = t(:no_assignments_have_points_warning,
"Can't compute score until an assignment " \
"has points possible")
end
end
js_env :total_grade_warning => warning if warning
end
def new_history
@page_title = t("Gradebook History")
@body_classes << "full-width padless-content"
js_bundle :react_gradebook_history
# css_bundle :react_gradebook_history
js_env({})
render html: "", layout: true
end
def percentage(weight)
I18n.n(weight, percentage: true)
end
def points_possible(weight, options)
return unless options[:weighting]
return t("%{weight} of Final", weight: percentage(weight)) if options[:out_of_final]
percentage(weight)
end
def aggregate_by_grading_period?
view_all_grading_periods? && @context.weighted_grading_periods?
end
def aggregate_assignments
if aggregate_by_grading_period?
@presenter.periods_assignments = periods_as_assignments(@presenter.grading_periods,
out_of_final: true,
exclude_total: @exclude_total)
else
@presenter.groups_assignments = groups_as_assignments(@presenter.groups,
out_of_final: true,
exclude_total: @exclude_total)
end
end
def groups_as_assignments(groups=nil, options = {})
as_assignments(
groups || @context.assignment_groups.active,
options.merge!(weighting: @context.group_weighting_scheme == 'percent')
) { |group| group_as_assignment(group, options) }
end
def periods_as_assignments(periods=nil, options = {})
as_assignments(
periods || @context.grading_periods.active,
options.merge!(weighting: @context.weighted_grading_periods?)
) { |period| period_as_assignment(period, options) }
end
def as_assignments(objects=nil, options={})
fakes = []
fakes.concat(objects.map { |object| yield(object) }) if objects && block_given?
fakes << total_as_assignment(options) unless options[:exclude_total]
fakes
end
def group_as_assignment(group, options)
OpenObject.build('assignment',
id: "group-#{group.id}",
rules: group.rules,
title: group.name,
points_possible: points_possible(group.group_weight, options),
hard_coded: true,
special_class: 'group_total',
assignment_group_id: group.id,
group_weight: group.group_weight,
asset_string: "group_total_#{group.id}")
end
def period_as_assignment(period, options)
OpenObject.build('assignment',
id: "period-#{period.id}",
rules: [],
title: period.title,
points_possible: points_possible(period.weight, options),
hard_coded: true,
special_class: 'group_total',
assignment_group_id: period.id,
group_weight: period.weight,
asset_string: "period_total_#{period.id}")
end
def total_as_assignment(options = {})
OpenObject.build('assignment',
id: 'final-grade',
title: t('Total'),
points_possible: (options[:out_of_final] ? '' : percentage(100)),
hard_coded: true,
special_class: 'final_grade',
asset_string: "final_grade_column")
end
def moderated_grading_enabled_and_no_grades_published
@assignment.moderated_grading? && !@assignment.grades_published?
end
def exclude_total?(context)
return true if context.hide_final_grades
return false unless grading_periods? && view_all_grading_periods?
grading_period_group.present? && !grading_period_group.display_totals_for_all_grading_periods?
end
def submisions_attachment_crocodocable_in_firefox?(submissions)
!(Canvadocs.hijack_crocodoc_sessions? && @assignment.context.account.feature_enabled?(:new_annotations)) &&
request.user_agent.to_s =~ /Firefox/ &&
submissions.
joins("left outer join #{submissions.connection.quote_table_name('canvadocs_submissions')} cs on cs.submission_id = submissions.id").
joins("left outer join #{CrocodocDocument.quoted_table_name} on cs.crocodoc_document_id = crocodoc_documents.id").
joins("left outer join #{Canvadoc.quoted_table_name} on cs.canvadoc_id = canvadocs.id").
where("cs.crocodoc_document_id IS NOT null or cs.canvadoc_id IS NOT null").
exists?
end
def canvadoc_annotations_enabled_in_firefox?
# this really means crocodoc enabled in canvadocs while using firefox
request.user_agent.to_s =~ /Firefox/ &&
Canvadocs.enabled? &&
Canvadocs.annotations_supported? &&
!@assignment.context.account.feature_enabled?(:new_annotations) &&
@assignment.submission_types.include?('online_upload')
end
def grade_summary_presenter
options = presenter_options
if options.key?(:grading_period_id)
GradingPeriodGradeSummaryPresenter.new(@context, @current_user, params[:id], options)
else
GradeSummaryPresenter.new(@context, @current_user, params[:id], options)
end
end
def presenter_options
options = {}
return options unless @context.present?
if @current_grading_period_id.present? && !view_all_grading_periods? && grading_periods?
options[:grading_period_id] = @current_grading_period_id
end
return options unless @current_user.present?
order_preferences = @current_user.preferences[:course_grades_assignment_order]
saved_order = order_preferences && order_preferences[@context.id]
options[:assignment_order] = saved_order if saved_order.present?
options
end
def custom_course_users_api_url(include_concluded: false, include_inactive: false, per_page:)
state = %w[active invited]
state << 'completed' if include_concluded
state << 'inactive' if include_inactive
api_v1_course_users_url(
@context,
include: %i[avatar_url group_ids enrollments],
enrollment_type: %w[student student_view],
enrollment_state: state,
per_page: per_page
)
end
def custom_course_enrollments_api_url(include_concluded: false, include_inactive: false, per_page:)
state = %w[active invited]
state << 'completed' if include_concluded
state << 'inactive' if include_inactive
api_v1_course_enrollments_url(
@context,
include: %i[avatar_url group_ids],
type: %w[StudentEnrollment StudentViewEnrollment],
state: state,
per_page: per_page
)
end
def gradebook_settings
@current_user.preferences.fetch(:gradebook_settings, {})
end
end