lib/bearcat/client/submissions.rb in bearcat-1.4.12 vs lib/bearcat/client/submissions.rb in bearcat-1.4.13

- old
+ new

@@ -32,33 +32,60 @@ 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_path, params={}) - response = upload_file("/api/v1/courses/#{course}/assignments/#{assignment}/submissions/#{user}/files", file_path, params) - params['submission'] = { - 'submission_type' => 'online_upload', - 'file_ids'=> [response['id']] - } - course_submission(course, assignment, params) + 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_path, params={}) - response = upload_file("/api/v1/sections/#{section}/assignments/#{assignment}/submissions/#{user}/files", file_path, params) - params['submission'] = { - 'submission_type' => 'online_upload', - 'file_ids'=> [response['id']] - } - section_submission(section, assignment, params) + 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