From ea66fb1881fbf7c73c6dc8728cc569cab23f51ee Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Tue, 29 Mar 2011 14:38:53 -0600 Subject: [PATCH] include needs_grading_count in assignments api json, if authorized refs #3958 Change-Id: Ia42b08303f62912e6aceb2f07529c561a1bc3686 Reviewed-on: https://gerrit.instructure.com/2862 Reviewed-by: Zach Wily Tested-by: Hudson --- app/controllers/assignment_groups_controller.rb | 2 +- app/controllers/assignments_api_controller.rb | 10 ++++++---- lib/api/v1/assignment.rb | 6 +++++- spec/apis/v1/assignment_groups_api_spec.rb | 4 ++++ spec/apis/v1/assignments_api_spec.rb | 3 +++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/controllers/assignment_groups_controller.rb b/app/controllers/assignment_groups_controller.rb index 1faa6492aa3..f1d9e8bf981 100644 --- a/app/controllers/assignment_groups_controller.rb +++ b/app/controllers/assignment_groups_controller.rb @@ -75,7 +75,7 @@ class AssignmentGroupsController < ApplicationController hash = group.as_json(:include_root => false, :only => %w(id name position)) if include_assignments - hash['assignments'] = group.assignments.active.map { |a| assignment_json(a) } + hash['assignments'] = group.assignments.active.map { |a| assignment_json(a, [], @context.user_is_teacher?(@current_user)) } end hash end diff --git a/app/controllers/assignments_api_controller.rb b/app/controllers/assignments_api_controller.rb index 0aed96a6b06..943f9e728d4 100644 --- a/app/controllers/assignments_api_controller.rb +++ b/app/controllers/assignments_api_controller.rb @@ -29,6 +29,7 @@ class AssignmentsApiController < ApplicationController # # @response_field id The unique identifier for the assignment. # @response_field name The name of the assignment. + # @response_field needs_grading_count [Integer] If the requesting user has grading rights, the number of submissions that need grading. # @response_field position [Integer] The sorting order of this assignment in # the group. # @response_field points_possible The maximum possible points for the @@ -104,7 +105,8 @@ class AssignmentsApiController < ApplicationController :include => [:assignment_group, :rubric_association, :rubric], :order => 'assignment_groups.position, assignments.position') - hashes = @assignments.map { |assignment| assignment_json(assignment) } + hashes = @assignments.map { |assignment| + assignment_json(assignment, [], @context.user_is_teacher?(@current_user)) } render :json => hashes.to_json end @@ -115,7 +117,7 @@ class AssignmentsApiController < ApplicationController @assignment = @context.active_assignments.find(params[:id], :include => [:assignment_group, :rubric_association, :rubric]) - render :json => assignment_json(@assignment).to_json + render :json => assignment_json(@assignment, [], @context.user_is_teacher?(@current_user)).to_json end end @@ -146,7 +148,7 @@ class AssignmentsApiController < ApplicationController end if @assignment.save - render :json => assignment_json(@assignment).to_json, :status => 201 + render :json => assignment_json(@assignment, [], @context.user_is_teacher?(@current_user)).to_json, :status => 201 else # TODO: we don't really have a strategy in the API yet for returning # errors. @@ -172,7 +174,7 @@ class AssignmentsApiController < ApplicationController end if @assignment.update_attributes(assignment_params) - render :json => assignment_json(@assignment).to_json, :status => 201 + render :json => assignment_json(@assignment, [], @context.user_is_teacher?(@current_user)).to_json, :status => 201 else # TODO: we don't really have a strategy in the API yet for returning # errors. diff --git a/lib/api/v1/assignment.rb b/lib/api/v1/assignment.rb index 6b60aed6195..f5085866238 100644 --- a/lib/api/v1/assignment.rb +++ b/lib/api/v1/assignment.rb @@ -17,12 +17,16 @@ # module Api::V1::Assignment - def assignment_json(assignment, includes = []) + def assignment_json(assignment, includes = [], show_admin_fields = false) # no includes supported right now hash = assignment.as_json(:include_root => false, :only => %w(id grading_type points_possible position)) hash['name'] = assignment.title + if show_admin_fields + hash['needs_grading_count'] = assignment.needs_grading_count + end + hash['submission_types'] = assignment.submission_types.split(',') if assignment.rubric_association diff --git a/spec/apis/v1/assignment_groups_api_spec.rb b/spec/apis/v1/assignment_groups_api_spec.rb index e8fd842a82a..e62ff4eb256 100644 --- a/spec/apis/v1/assignment_groups_api_spec.rb +++ b/spec/apis/v1/assignment_groups_api_spec.rb @@ -88,6 +88,7 @@ describe AssignmentGroupsController, :type => :integration do 'name' => 'test3', 'position' => 1, 'points_possible' => 8, + 'needs_grading_count' => 0, "submission_types" => [ "none", ], @@ -115,6 +116,7 @@ describe AssignmentGroupsController, :type => :integration do 'name' => 'test4', 'position' => 2, 'points_possible' => 9, + 'needs_grading_count' => 0, "submission_types" => [ "none", ], @@ -132,6 +134,7 @@ describe AssignmentGroupsController, :type => :integration do 'name' => 'test1', 'position' => 1, 'points_possible' => 10, + 'needs_grading_count' => 0, "submission_types" => [ "none", ], @@ -142,6 +145,7 @@ describe AssignmentGroupsController, :type => :integration do 'name' => 'test2', 'position' => 2, 'points_possible' => 12, + 'needs_grading_count' => 0, "submission_types" => [ "none", ], diff --git a/spec/apis/v1/assignments_api_spec.rb b/spec/apis/v1/assignments_api_spec.rb index 1ccdbea3ae0..65729177e20 100644 --- a/spec/apis/v1/assignments_api_spec.rb +++ b/spec/apis/v1/assignments_api_spec.rb @@ -78,6 +78,7 @@ describe AssignmentsApiController, :type => :integration do 'grading_type' => 'points', 'use_rubric_for_grading' => true, 'free_form_criterion_comments' => true, + 'needs_grading_count' => 0, 'submission_types' => [ "online_upload", "online_text_entry", @@ -143,6 +144,7 @@ describe AssignmentsApiController, :type => :integration do 'position' => 1, 'points_possible' => 12, 'grading_type' => 'points', + 'needs_grading_count' => 0, 'submission_types' => [ 'none', ], @@ -178,6 +180,7 @@ describe AssignmentsApiController, :type => :integration do 'position' => 1, 'points_possible' => 15, 'grading_type' => 'points', + 'needs_grading_count' => 0, 'submission_types' => [ 'none', ],