Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the API to allow retrieving group members #1956

Merged
merged 12 commits into from
Jan 5, 2024
43 changes: 41 additions & 2 deletions app/controllers/api/v1/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
damianhxy marked this conversation as resolved.
Show resolved Hide resolved

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)
umar221b marked this conversation as resolved.
Show resolved Hide resolved
end

group_json = get_group_json(group)

respond_with group_json
end

# create group endpoint
def create
require_params([:groups])
Expand Down Expand Up @@ -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
damianhxy marked this conversation as resolved.
Show resolved Hide resolved
end
9 changes: 7 additions & 2 deletions app/models/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
damianhxy marked this conversation as resolved.
Show resolved Hide resolved
end

def grouplessCUDs
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class String
def to_boolean
ActiveRecord::Type::Boolean.new.cast(self)
ActiveRecord::Type::Boolean.new.cast(self.downcase)
end
end
damianhxy marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 27 additions & 1 deletion docs/api-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
umar221b marked this conversation as resolved.
Show resolved Hide resolved


#### 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

Expand Down