module Bearcat class Client < Footrest::Client module Submissions def course_submissions(course, params={}) get("/api/v1/courses/#{course.to_s}/students/submissions", params) end def section_submissions(section, params={}) get("/api/v1/sections/#{section.to_s}/students/submissions", params) end def get_course_submissions(course, assignment, params={}) get("/api/v1/courses/#{course.to_s}/assignments/#{assignment}/submissions", params) end def user_course_assignment_submission(course, assignment, user, params={}) get("/api/v1/courses/#{course.to_s}/assignments/#{assignment.to_s}/submissions/#{user.to_s}", params) end def course_submission(course, assignment, params) post("/api/v1/courses/#{course}/assignments/#{assignment}/submissions", params) end def section_submission(section, assignment, params) post("/api/v1/sections/#{section}/assignments/#{assignment}/submissions", params) end def grade_course_submission(course, assignment, user, params) put("/api/v1/courses/#{course}/assignments/#{assignment}/submissions/#{user}", params) end def grade_section_submission(section, assignment, user, params) put("/api/v1/sections/#{section}/assignments/#{assignment}/submissions/#{user}", params) end def course_file_upload_submission(course, assignment, user, file_data, params={}) file_upload_submission(course, assignment, user, file_data, params, type: :course) end def section_file_upload_submission(section, assignment, user, file_data, params={}) file_upload_submission(section, assignment, user, file_data, params, type: :section) end def course_update_grades(course, assignment, params={}) post("/api/v1/courses/#{course}/assignments/#{assignment}/submissions/update_grades", params) end def section_update_grades(section, assignment, params={}) post("/api/v1/sections/#{section}/assignments/#{assignment}/submissions/update_grades", params) end protected # @param file_data One of an array of file_path strings or an array of Hashes, each being upload file params plus the file's path def file_upload_submission(type_id, assignment, user, file_data=nil, params={}, type:) raise ArgumentError, 'Invalid type' unless [:course, :section].include?(type) raise ArgumentError, 'Must provide either file paths or fully formed submission params' if params.dig(:submission, :file_ids).nil? && file_data.nil? params = params.with_indifferent_access sub_params = if params.dig(:submission, :file_ids).present? {} else file_data = Array.wrap(file_data) file_ids = file_data.map do |datum| if datum.is_a?(Hash) && datum[:file_path].present? # datum is a param hash for upload_file API plus the file_path path = datum[:file_path] upload_params = datum.except(:file_path) else # treat datum as a file_path string path = datum upload_params = params end response = upload_file("/api/v1/#{type}s/#{type_id}/assignments/#{assignment}/submissions/#{user}/files", path, upload_params) response['id'] end { submission_type: 'online_upload', file_ids: file_ids, } end sub_params.merge!(params[:submission]) if params[:submission].present? params[:submission] = sub_params send("#{type}_submission".to_sym, type_id, assignment, params) end end end end