diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index ec553ef50..02b8840e6 100644 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -5,14 +5,39 @@ class Api::V1::GroupsController < Api::V1::BaseApiController # endpoint to obtain all groups def index - groups = @assessment.groups + show_members = params[:show_members].to_boolean + groups = @assessment.groups(show_members: show_members) + + if show_members + groups_json = [] + groups.each do |group| + group_json = get_group_json(group) + groups_json << group_json + end + else + groups_json = groups.as_json + end + group_size = @assessment.group_size respond_with({ group_size: group_size, - groups: groups, + groups: groups_json, assessment: @assessment }) end + def show + require_params([:id]) + group = @assessment.groups(show_members: true).find_by(id: params[:id]) + + if group.nil? + raise ApiError.new("Couldn't find group with id #{params[:id]}", :bad_request) + end + + group_json = get_group_json(group) + + respond_with group_json + end + # create group endpoint def create require_params([:groups]) @@ -72,4 +97,18 @@ def destroy end respond_with_hash(message: "Group #{params[:id]} successfully deleted") end + +private + + def get_group_json(group) + members = [] + group.assessment_user_data.each do |assessment_user_datum| + user_json = assessment_user_datum.course_user_datum.user.as_json + user_json[:course_user_datum_id] = assessment_user_datum.course_user_datum_id + members << user_json + end + + group_json = group.as_json + group_json[:members] = members + end end diff --git a/app/models/assessment.rb b/app/models/assessment.rb index 3a5fb676d..7cb23f454 100755 --- a/app/models/assessment.rb +++ b/app/models/assessment.rb @@ -368,8 +368,13 @@ def has_handout? overwrites_method?(:handout) || handout_is_url? || handout_is_file? end - def groups - Group.joins(:assessment_user_data).where(assessment_user_data: { assessment_id: id }).distinct + def groups(show_members: false) + if show_members + Group.includes(assessment_user_data: { course_user_datum: :user }) + .where(assessment_user_data: { assessment_id: id }) + else + Group.joins(:assessment_user_data).where(assessment_user_data: { assessment_id: id }).distinct + end end def grouplessCUDs diff --git a/config/initializers/core_ext.rb b/config/initializers/core_ext.rb index 9f02cc46d..ff971feea 100644 --- a/config/initializers/core_ext.rb +++ b/config/initializers/core_ext.rb @@ -1,6 +1,6 @@ class String def to_boolean - ActiveRecord::Type::Boolean.new.cast(self) + ActiveRecord::Type::Boolean.new.cast(self.downcase) end end diff --git a/config/routes.rb b/config/routes.rb index dcf947a1f..f77d186d3 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,8 +29,7 @@ get "handout" post "submit" post "set_group_settings" - - resources :groups, only: [:index, :create, :destroy] + resources :groups, only: [:index, :show, :create, :destroy] resources :submissions, param: :version, only: [:index] do get "feedback" diff --git a/docs/api-interface.md b/docs/api-interface.md index 1cd1ee05f..f27c9b959 100644 --- a/docs/api-interface.md +++ b/docs/api-interface.md @@ -176,11 +176,37 @@ List all groups in an assessment **Endpoint:** `GET /courses/{course name}/assessments/{assessment name}/groups` -**Parameters:** [none] +**Parameters:** + + +`show_members` + +| key | | type | description | +| ------------ |----------|---------|------------------------------------------------------| +| show_members | optional | boolean | whether to retrieve the members of each group or not | + +--- **Response:** A JSON object containing the group_size, a list of groups, and the assessment containing the groups. +If `show_members` is set to true, a list of `assessment_user_datum` objects will be retrieved for each group as well. + + +#### show + +Show the details of a group and its members + +**Scope:** 'instructor_all' + +**Endpoint:** `GET /courses/{course name}/assessments/{assessment name}/groups/{id}` + +**Parameters:** [none] + +**Response:** + +The requested group object. + #### create