canvas-lms/lib/api.rb

636 lines
23 KiB
Ruby
Raw Normal View History

#
# Copyright (C) 2011 - 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Api
include Api::Errors::ControllerMethods
# find id in collection, by either id or sis_*_id
# if the collection is over the users table, `self` is replaced by @current_user.id
def api_find(collection, id, account: nil)
result = api_find_all(collection, [id], account: account).first
raise(ActiveRecord::RecordNotFound, "Couldn't find #{collection.name} with API id '#{id}'") unless result
result
end
def api_find_all(collection, ids, account: nil)
if collection.table_name == User.table_name && @current_user
ids = ids.map{|id| id == 'self' ? @current_user.id : id }
end
if collection.table_name == Account.table_name
ids = ids.map do |id|
case id
when 'self'
@domain_root_account.id
when 'default'
Account.default.id
when 'site_admin'
Account.site_admin.id
else
id
end
end
end
if collection.table_name == EnrollmentTerm.table_name
current_term = nil
ids = ids.map do |id|
case id
when 'default'
@domain_root_account.default_enrollment_term
when 'current'
if !current_term
current_terms = @domain_root_account
.enrollment_terms
.active
.where("(start_at<=? OR start_at IS NULL) AND (end_at >=? OR end_at IS NULL) AND NOT (start_at IS NULL AND end_at IS NULL)", Time.now.utc, Time.now.utc)
.limit(2)
.to_a
current_term = current_terms.length == 1 ? current_terms.first : :nil
end
current_term == :nil ? nil : current_term
else
id
end
end
end
Api.sis_relation_for_collection(collection, ids, account || @domain_root_account, @current_user)
end
# map a list of ids and/or sis ids to plain ids.
# sis ids that can't be found in the db won't appear in the result, however
# AR object ids aren't verified to exist in the db so they'll still be
# returned in the result.
def self.map_ids(ids, collection, root_account, current_user = nil)
sis_mapping = sis_find_sis_mapping_for_collection(collection)
columns = sis_parse_ids(ids, sis_mapping[:lookups], current_user,
root_account: root_account)
result = columns.delete(sis_mapping[:lookups]["id"]) || []
unless columns.empty?
relation = relation_for_sis_mapping_and_columns(collection, columns, sis_mapping, root_account)
# pluck ignores eager_load
relation = relation.joins(*relation.eager_load_values) if relation.eager_load_values.present?
result.concat relation.pluck(:id)
result.uniq!
end
result
end
SIS_MAPPINGS = {
'courses' =>
{ :lookups => { 'sis_course_id' => 'sis_source_id',
'id' => 'id',
'sis_integration_id' => 'integration_id',
'lti_context_id' => 'lti_context_id' }.freeze,
:is_not_scoped_to_account => ['id'].freeze,
:scope => 'root_account_id' }.freeze,
'enrollment_terms' =>
{ :lookups => { 'sis_term_id' => 'sis_source_id',
'id' => 'id',
'sis_integration_id' => 'integration_id' }.freeze,
:is_not_scoped_to_account => ['id'].freeze,
:scope => 'root_account_id' }.freeze,
'users' =>
{ :lookups => { 'sis_user_id' => 'pseudonyms.sis_user_id',
'sis_login_id' => {
column: 'LOWER(pseudonyms.unique_id)',
transform: ->(id) { QuotedValue.new("LOWER(#{Pseudonym.connection.quote(id)})") }
},
'id' => 'users.id',
'sis_integration_id' => 'pseudonyms.integration_id',
'lti_context_id' => 'users.lti_context_id',
'lti_user_id' => 'users.lti_context_id' }.freeze,
:is_not_scoped_to_account => ['users.id', 'users.lti_context_id'].freeze,
:scope => 'pseudonyms.account_id',
:joins => :pseudonym }.freeze,
'accounts' =>
{ :lookups => { 'sis_account_id' => 'sis_source_id',
'id' => 'id',
'sis_integration_id' => 'integration_id',
'lti_context_id' => 'lti_context_id' }.freeze,
:is_not_scoped_to_account => ['id', 'lti_context_id'].freeze,
:scope => 'root_account_id' }.freeze,
'course_sections' =>
{ :lookups => { 'sis_section_id' => 'sis_source_id',
'id' => 'id',
'sis_integration_id' => 'integration_id' }.freeze,
:is_not_scoped_to_account => ['id'].freeze,
:scope => 'root_account_id' }.freeze,
'groups' =>
{ :lookups => { 'sis_group_id' => 'sis_source_id',
'id' => 'id' }.freeze,
:is_not_scoped_to_account => ['id'].freeze,
:scope => 'root_account_id' }.freeze,
}.freeze
# (digits in 2**63-1) - 1, so that any ID representable in MAX_ID_LENGTH
# digits is < 2**63, which is the max signed 64-bit integer, which is what's
# used for the DB ids.
MAX_ID_LENGTH = 18
ID_REGEX = %r{\A\d{1,#{MAX_ID_LENGTH}}\z}
def self.sis_parse_id(id, lookups, _current_user = nil,
root_account: nil)
# returns column_name, column_value
return lookups['id'], id if id.is_a?(Numeric) || id.is_a?(ActiveRecord::Base)
id = id.to_s.strip
if id =~ %r{\Ahex:(lti_[\w_]+|sis_[\w_]+):(([0-9A-Fa-f]{2})+)\z}
sis_column = $1
sis_id = [$2].pack('H*')
elsif id =~ %r{\A(lti_[\w_]+|sis_[\w_]+):(.+)\z}
sis_column = $1
sis_id = $2
elsif id =~ ID_REGEX
return lookups['id'], (id =~ /\A\d+\z/ ? id.to_i : id)
else
return nil, nil
end
column = lookups[sis_column]
return nil, nil unless column
if column.is_a?(Hash)
sis_id = column[:transform].call(sis_id)
column = column[:column]
end
return column, sis_id
end
def self.sis_parse_ids(ids, lookups, current_user = nil, root_account: nil)
# returns {column_name => [column_value,...].uniq, ...}
columns = {}
ids.compact.each do |id|
column, sis_id = sis_parse_id(id, lookups,
current_user,
root_account: root_account)
next unless column && sis_id
columns[column] ||= []
columns[column] << sis_id
end
columns.keys.each { |key| columns[key].uniq! }
return columns
end
# remove things that don't look like valid database IDs
# return in integer format if possible
# (note that ID_REGEX may be redefined by a plugin!)
def self.map_non_sis_ids(ids)
ids.map{ |id| id.to_s.strip }.select{ |id| id =~ ID_REGEX }.map do |id|
id =~ /\A\d+\z/ ? id.to_i : id
end
end
def self.sis_find_sis_mapping_for_collection(collection)
SIS_MAPPINGS[collection.table_name] or
raise(ArgumentError, "need to add support for table name: #{collection.table_name}")
end
def self.sis_relation_for_collection(collection, ids, sis_root_account, current_user = nil)
relation_for_sis_mapping(collection,
sis_find_sis_mapping_for_collection(collection),
ids,
sis_root_account,
current_user)
end
def self.relation_for_sis_mapping(relation, sis_mapping, ids, sis_root_account, current_user = nil)
relation_for_sis_mapping_and_columns(relation,
sis_parse_ids(ids,
sis_mapping[:lookups],
current_user,
root_account: sis_root_account),
sis_mapping,
sis_root_account)
end
def self.relation_for_sis_mapping_and_columns(relation, columns, sis_mapping, sis_root_account)
raise ArgumentError, "sis_root_account required for lookups" unless sis_root_account.is_a?(Account)
return relation.none if columns.empty?
not_scoped_to_account = sis_mapping[:is_not_scoped_to_account] || []
if columns.length == 1 && not_scoped_to_account.include?(columns.keys.first)
relation = relation.where(columns)
else
args = []
query = []
columns.keys.sort.each do |column|
if not_scoped_to_account.include?(column)
query << "#{column} IN (?)"
args << columns[column]
else
raise ArgumentError, "missing scope for collection" unless sis_mapping[:scope]
ids = columns[column]
if ids.any? { |id| id.is_a?(Array) }
ids_hash = {}
ids.each do |id|
id = Array(id)
account = id.last || sis_root_account
ids_hash[account] ||= []
ids_hash[account] << id.first
end
else
ids_hash = { sis_root_account => ids }
end
ids_hash.each do |root_account, ids|
query << "(#{sis_mapping[:scope]} = #{root_account.id} AND #{column} IN (?))"
args << ids
end
end
end
args.unshift(query.join(" OR "))
relation = relation.where(*args)
end
relation = relation.eager_load(sis_mapping[:joins]) if sis_mapping[:joins]
relation
end
def self.max_per_page
Setting.get('api_max_per_page', '50').to_i
end
def self.per_page
Setting.get('api_per_page', '10').to_i
end
cleanup per_page values for json pagination if the response is json and paginated, make sure it honors the per_page parameter. also, make sure we don't accidentally allow arbitrarily large per_page values. test-plan: [check this per endpoint] - have more than <max> entries (e.g. apps in the app center) - fetch <endpoint> - should get a page with <default> results - link with rel="next" should embed per_page=<default> - fetch <endpoint>?per_page=<max+1> - should get a page with <max> results - Link header should have a link with rel="next". that link should embed per_page=<max> - fetch <endpoint>?per_page=1 - should get a page with 1 result - link with rel="next" should embed per_page=1 [endpoints] /api/v1/courses/:course_id/app_center/apps default: 72 max: 72 /api/v1/courses/:course_id/app_center/apps/:app_id/reviews default: 15 max: 50 /conversations/discussion_replies.json default: 15 max: 50 /courses/:course_id/users/:user_id/usage.json default: 50 max: 50 /courses/:course_id/group_unassigned_members.json default: 15 max: 100 /courses/:course_id/outcomes/:outcome_id/results default: 10 max: 50 /courses/:course_id/question_banks/:question_bank_id/questions default: 50 max: 50 /courses/:course_id/quizzes/:quiz_id/moderate.json default: 50 max: 50 /api/v1/courses/:course_id/activity_stream default: 21 max: 50 Change-Id: Iacb0b413de1175fb70d9b073fccef3e70082e60e Reviewed-on: https://gerrit.instructure.com/26095 QA-Review: August Thornton <august@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Cody Cutrer <cody@instructure.com> Product-Review: Jacob Fugal <jacob@instructure.com>
2013-11-08 05:37:20 +08:00
def self.per_page_for(controller, options={})
per_page_requested = controller.params[:per_page] || options[:default] || per_page
max = options[:max] || max_per_page
[[per_page_requested.to_i, 1].max, max.to_i].min
end
# Add [link HTTP Headers](http://www.w3.org/Protocols/9707-link-header.html) for pagination
# The collection needs to be a will_paginate collection (or act like one)
# a new, paginated collection will be returned
def self.paginate(collection, controller, base_url, pagination_args = {}, response_args = {})
collection = paginate_collection!(collection, controller, pagination_args)
hash = build_links_hash(base_url, meta_for_pagination(controller, collection))
links = build_links_from_hash(hash)
controller.response.headers["Link"] = links.join(',') if links.length > 0
if response_args[:enhanced_return]
{hash: hash, collection: collection}
else
collection
end
end
# Returns collection as the first return value, and the meta information hash
# as the second return value
def self.jsonapi_paginate(collection, controller, base_url, pagination_args={})
collection = paginate_collection!(collection, controller, pagination_args)
meta = jsonapi_meta(collection, controller, base_url)
return collection, meta
end
def self.jsonapi_meta(collection, controller, base_url)
pagination = meta_for_pagination(controller, collection)
meta = {
per_page: collection.per_page
}
meta.merge!(build_links_hash(base_url, pagination))
if collection.ordinal_pages?
meta[:page] = pagination[:current]
meta[:template] = meta[:current].sub(/page=\d+/, "page={page}")
end
meta[:count] = collection.total_entries if collection.total_entries
meta[:page_count] = collection.total_pages if collection.total_pages
{ pagination: meta }
end
def self.paginate_collection!(collection, controller, pagination_args)
wrap_pagination_args!(pagination_args, controller)
begin
paginated = collection.paginate(pagination_args)
rescue Folio::InvalidPage
if pagination_args[:page].to_s =~ /\d+/ && pagination_args[:page].to_i > 0 && collection.build_page.ordinal_pages?
# for backwards compatibility we currently require returning [] for
# pages beyond the end of an ordinal collection, rather than a 404.
paginated = Folio::Ordinal::Page.create
paginated.current_page = pagination_args[:page].to_i
else
# we're not dealing with a simple out-of-bounds on an ordinal
# collection, let the exception propagate (and turn into a 404)
raise
end
end
paginated
end
def self.wrap_pagination_args!(pagination_args, controller)
pagination_args.reverse_merge!(
page: controller.params[:page],
per_page: per_page_for(controller,
default: pagination_args.delete(:default_per_page),
max: pagination_args.delete(:max_per_page)))
end
def self.meta_for_pagination(controller, collection)
{
query_parameters: controller.request.query_parameters,
per_page: collection.per_page,
current: collection.current_page,
next: collection.next_page,
prev: collection.previous_page,
first: collection.first_page,
last: collection.last_page,
}
end
PAGINATION_PARAMS = [:current, :next, :prev, :first, :last]
EXCLUDE_IN_PAGINATION_LINKS = %w(page per_page access_token api_key)
def self.build_links(base_url, opts={})
links = build_links_hash(base_url, opts)
build_links_from_hash(links)
end
def self.build_links_from_hash(links)
# iterate in order, but only using the keys present from build_links_hash
(PAGINATION_PARAMS & links.keys).map do |k|
v = links[k]
"<#{v}>; rel=\"#{k}\""
end
end
def self.build_links_hash(base_url, opts={})
base_url += (base_url =~ /\?/ ? '&': '?')
qp = opts[:query_parameters] || {}
qp = qp.with_indifferent_access.except(*EXCLUDE_IN_PAGINATION_LINKS)
base_url += "#{qp.to_query}&" if qp.present?
PAGINATION_PARAMS.each_with_object({}) do |param, obj|
if opts[param].present?
obj[param] = "#{base_url}page=#{opts[param]}&per_page=#{opts[:per_page]}"
end
end
end
def self.parse_pagination_links(link_header)
link_header.split(",").map do |link|
url, rel = link.match(%r{^<([^>]+)>; rel="([^"]+)"}).captures
uri = URI.parse(url)
raise(ArgumentError, "pagination url is not an absolute uri: #{url}") unless uri.is_a?(URI::HTTP)
Rack::Utils.parse_nested_query(uri.query).merge(:uri => uri, :rel => rel)
end
end
def media_comment_json(media_object_or_hash)
media_object_or_hash = OpenStruct.new(media_object_or_hash) if media_object_or_hash.is_a?(Hash)
{
'content-type' => "#{media_object_or_hash.media_type}/mp4",
'display_name' => media_object_or_hash.title.presence || media_object_or_hash.user_entered_title,
'media_id' => media_object_or_hash.media_id,
'media_type' => media_object_or_hash.media_type,
'url' => user_media_download_url(:user_id => @current_user.id,
:entryId => media_object_or_hash.media_id,
:type => "mp4",
:redirect => "1")
}
end
def api_bulk_load_user_content_attachments(htmls, context = nil)
regex = context ? %r{/#{context.class.name.tableize}/#{context.id}/files/(\d+)} : %r{/files/(\d+)}
attachment_ids = []
htmls.compact.each do |html|
html.scan(regex).each do |match|
attachment_ids << match.first
end
end
if attachment_ids.blank?
{}
else
attachments = if context.is_a?(User) || context.nil?
Attachment.where(id: attachment_ids)
else
context.attachments.where(id: attachment_ids)
end
attachments.preload(:context).index_by(&:id)
end
end
PLACEHOLDER_PROTOCOL = 'https'
PLACEHOLDER_HOST = 'placeholder.invalid'
def get_host_and_protocol_from_request
[ request.host_with_port, request.ssl? ? 'https' : 'http' ]
end
def resolve_placeholders(content)
host, protocol = get_host_and_protocol_from_request
# content is a json-encoded string; slashes are escaped (at least in Rails 4.0)
content.gsub("#{PLACEHOLDER_PROTOCOL}:\\/\\/#{PLACEHOLDER_HOST}", "#{protocol}:\\/\\/#{host}").
gsub("#{PLACEHOLDER_PROTOCOL}://#{PLACEHOLDER_HOST}", "#{protocol}://#{host}")
end
def user_can_download_attachment?(attachment, context, user)
# checking on the context first can improve performance when checking many attachments for admins
(context && context.grants_any_right?(user, :manage_files, :read_as_admin)) || attachment.grants_right?(user, nil, :download)
end
def api_user_content(html, context = @context, user = @current_user, preloaded_attachments = {}, is_public=false)
return html if html.blank?
# use the host of the request if available;
# use a placeholder host for pre-generated content, which we will replace with the request host when available;
# otherwise let HostUrl figure out what host is appropriate
if self.respond_to?(:request)
host, protocol = get_host_and_protocol_from_request
elsif self.respond_to?(:use_placeholder_host?) && use_placeholder_host?
host = PLACEHOLDER_HOST
protocol = PLACEHOLDER_PROTOCOL
else
host = HostUrl.context_host(context, @account_domain.try(:host))
protocol = HostUrl.protocol
end
rewriter = UserContent::HtmlRewriter.new(context, user)
rewriter.set_handler('files') do |match|
if match.obj_id
obj = preloaded_attachments[match.obj_id]
obj ||= if context.is_a?(User) || context.nil?
Attachment.where(id: match.obj_id).first
else
context.attachments.find_by_id(match.obj_id)
end
end
unless obj && !obj.deleted? && ((is_public && !obj.locked_for?(user)) || user_can_download_attachment?(obj, context, user))
if obj && obj.previewable_media? && (uri = URI.parse(match.url) rescue nil)
uri.query = (uri.query.to_s.split("&") + ["no_preview=1"]).join("&")
next uri.to_s
else
next
end
end
if ["Course", "Group", "Account", "User"].include?(obj.context_type)
opts = {:only_path => true}
opts.merge!(:verifier => obj.uuid) unless respond_to?(:in_app?, true) && in_app? && !is_public
if match.rest.start_with?("/preview")
url = self.send("#{obj.context_type.downcase}_file_preview_url", obj.context_id, obj.id, opts)
else
opts[:download] = '1'
opts[:wrap] = '1' if match.rest.include?('wrap=1')
url = self.send("#{obj.context_type.downcase}_file_download_url", obj.context_id, obj.id, opts)
end
else
opts = {:download => '1', :only_path => true}
opts.merge!(:verifier => obj.uuid) unless respond_to?(:in_app?, true) && in_app? && !is_public
url = file_download_url(obj.id, opts)
end
url
end
html = rewriter.translate_content(html)
url_helper = Html::UrlProxy.new(self, context, host, protocol)
get sub account branding and custom css/includes working fixes: CNVS-24787 fixes: CNVS-23964 fixes: CNVS-23957 - Handle parent account custom css/js for new_styles test plan: * set up a root account, child account, and grandchild account * use theme editor to set a custom css/js file for each (eg: for css `* {color:red}` and for js 'console.log("from grandchild")` * make a course & a group in the grandchild account * load a page in that course and group and make sure you see grandchild account's branding, and root's, child's, and then grandchild's css loaded on the page (grandchild should be loaded last so you see it's css effects override root or child's and you should see the console.log from root then child then grandchild) * view a page in "child". it should have root and child's css/js but not grandchild * as a user that only has enrollments (account associations) in "child", go to the dashboard. you should see css/js for both root and child but not grandchild fixes: CNVS-25051 Opening Theme Editor for sub-accounts shows incorrect theme preview test plan: * Go to a sub-account in theme editor and change settings so the Branding is different and save. * the preview on the right should reflect your changes both after you "apply" and "save" (and not just show the preview of the root account's branding) fixes: CNVS-23406 - global JS and CSS files are being included when Global CSS/JavaScript includes is false test plan: * go to /accounts/self/, and go to theme editor and upload a css_override * see that that css is loaded on pages * back in root account settings disable Global CSS/JavaScript includes * check that the css is no longer loaded. * do the same thing checking a subaccount's custom css fixes: CNVS-25558 - load whole chain of custom css/js in native app api requests test plan: * make api request for a wiki page in course in a subaccount that has custom css/js within a root account that also has custom css/js * you should see both the root account's css/js and the child account's returned in the response to test grandchild js issue jeremyp found: * go to theme editor for a grandchild account * choose a js override file (like: `console.log('first')`) * preview & apply * you should see "first" in console * go back to theme editor, pick a new file (like: `console.log('second')`) * preview & apply * you should only see "second" in console. not "first" Change-Id: I8d9047948f5da94be41e0205844629a170f980af Reviewed-on: https://gerrit.instructure.com/68249 Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Jeremy Putnam <jeremyp@instructure.com> Tested-by: Jenkins Product-Review: Ryan Shaw <ryan@instructure.com>
2015-12-05 00:57:07 +08:00
account = Context.get_account(context) || @domain_root_account
include_mobile = respond_to?(:mobile_device?, true) && mobile_device?
Html::Content.rewrite_outgoing(html, account, url_helper, include_mobile: include_mobile)
end
# This removes the verifier parameters that are added to attachment links by api_user_content
# and adds context (e.g. /courses/:id/) if it is missing
# exception: it leaves user-context file links alone
def process_incoming_html_content(html)
Html::Content.process_incoming(html)
end
def value_to_boolean(value)
Canvas::Plugin.value_to_boolean(value)
end
# takes a comma separated string, an array, or nil and returns an array
def self.value_to_array(value)
value.is_a?(String) ? value.split(',') : (value || [])
end
def self.invalid_time_stamp_error(attribute, message)
Canvas::Errors.capture(
'invalid_date_time',
message: "invalid #{attribute}",
exception_message: message
)
end
# regex for valid iso8601 dates
ISO8601_REGEX = /^(?<year>[0-9]{4})-
(?<month>1[0-2]|0[1-9])-
(?<day>3[0-1]|0[1-9]|[1-2][0-9])T
(?<hour>2[0-3]|[0-1][0-9]):
(?<minute>[0-5][0-9]):
(?<second>60|[0-5][0-9])
(?<fraction>\.[0-9]+)?
(?<timezone>Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/x
# regex for valid dates
DATE_REGEX = /^\d{4}[- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])$/
# regex for shard-aware ID
ID = '(?:\d+~)?\d+'
modules api, closes #10404 also modifies the discussion topic and assignment API controllers to make sure "must_view" requirements are fulfilled test plan: * check the API documentation; ensure it looks okay * create a course with module items of each supported type * set completion criteria of each supported type * create another module, so you can set prerequisites * use the list modules API and verify its output matches the course and the documentation * as a teacher, "state" should be missing * as a student, "state" should be "locked", "unlocked", "started", or "completed" * use the show module API and verify the correct information is returned for a single module * use the list module items API and verify the output * as a teacher, the "completion_requirement" omits the "completed" flag * as a student, "completed" should be true or false, depending on whether the requirement was met * use the show module API and verify the correct information is returned for a single module item * last but not least, verify "must view" requirements can be fulfilled through the api_data_endpoints supplied for files, pages, discussions, and assignments * files are viewed when downloading their content * pages are viewed by the show action (where content is returned) * discussions are viewed when marked read via the mark_topic_read or mark_all_read actions * assignments are viewed by the show action (where description is returned). they are not viewed if the assignment is locked and the user does not have access to the content yet. Change-Id: I0cbbbc542f69215e7b396a501d4d86ff2f76c149 Reviewed-on: https://gerrit.instructure.com/13626 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Simon Williams <simon@instructure.com>
2012-09-12 01:16:48 +08:00
# maps a Canvas data type to an API-friendly type name
API_DATA_TYPE = { "Attachment" => "File",
"WikiPage" => "Page",
"DiscussionTopic" => "Discussion",
"Assignment" => "Assignment",
"Quizzes::Quiz" => "Quiz",
modules api, closes #10404 also modifies the discussion topic and assignment API controllers to make sure "must_view" requirements are fulfilled test plan: * check the API documentation; ensure it looks okay * create a course with module items of each supported type * set completion criteria of each supported type * create another module, so you can set prerequisites * use the list modules API and verify its output matches the course and the documentation * as a teacher, "state" should be missing * as a student, "state" should be "locked", "unlocked", "started", or "completed" * use the show module API and verify the correct information is returned for a single module * use the list module items API and verify the output * as a teacher, the "completion_requirement" omits the "completed" flag * as a student, "completed" should be true or false, depending on whether the requirement was met * use the show module API and verify the correct information is returned for a single module item * last but not least, verify "must view" requirements can be fulfilled through the api_data_endpoints supplied for files, pages, discussions, and assignments * files are viewed when downloading their content * pages are viewed by the show action (where content is returned) * discussions are viewed when marked read via the mark_topic_read or mark_all_read actions * assignments are viewed by the show action (where description is returned). they are not viewed if the assignment is locked and the user does not have access to the content yet. Change-Id: I0cbbbc542f69215e7b396a501d4d86ff2f76c149 Reviewed-on: https://gerrit.instructure.com/13626 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Simon Williams <simon@instructure.com>
2012-09-12 01:16:48 +08:00
"ContextModuleSubHeader" => "SubHeader",
"ExternalUrl" => "ExternalUrl",
"ContextExternalTool" => "ExternalTool",
"ContextModule" => "Module",
"ContentTag" => "ModuleItem" }.freeze
# matches the other direction, case insensitively
def self.api_type_to_canvas_name(api_type)
unless @inverse_map
m = {}
API_DATA_TYPE.each do |k, v|
m[v.downcase] = k
end
@inverse_map = m
end
return nil unless api_type
@inverse_map[api_type.downcase]
end
modules api, closes #10404 also modifies the discussion topic and assignment API controllers to make sure "must_view" requirements are fulfilled test plan: * check the API documentation; ensure it looks okay * create a course with module items of each supported type * set completion criteria of each supported type * create another module, so you can set prerequisites * use the list modules API and verify its output matches the course and the documentation * as a teacher, "state" should be missing * as a student, "state" should be "locked", "unlocked", "started", or "completed" * use the show module API and verify the correct information is returned for a single module * use the list module items API and verify the output * as a teacher, the "completion_requirement" omits the "completed" flag * as a student, "completed" should be true or false, depending on whether the requirement was met * use the show module API and verify the correct information is returned for a single module item * last but not least, verify "must view" requirements can be fulfilled through the api_data_endpoints supplied for files, pages, discussions, and assignments * files are viewed when downloading their content * pages are viewed by the show action (where content is returned) * discussions are viewed when marked read via the mark_topic_read or mark_all_read actions * assignments are viewed by the show action (where description is returned). they are not viewed if the assignment is locked and the user does not have access to the content yet. Change-Id: I0cbbbc542f69215e7b396a501d4d86ff2f76c149 Reviewed-on: https://gerrit.instructure.com/13626 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Simon Williams <simon@instructure.com>
2012-09-12 01:16:48 +08:00
def self.recursively_stringify_json_ids(value, opts = {})
case value
when Hash
stringify_json_ids(value, opts)
value.each_value { |v| recursively_stringify_json_ids(v, opts) if v.is_a?(Hash) || v.is_a?(Array) }
when Array
value.each { |v| recursively_stringify_json_ids(v, opts) if v.is_a?(Hash) || v.is_a?(Array) }
end
value
end
def self.stringify_json_ids(value, opts = {})
return unless value.is_a?(Hash)
value.keys.each do |key|
if key =~ /(^|_)id$/
# id, foo_id, etc.
value[key] = stringify_json_id(value[key], opts)
elsif key =~ /(^|_)ids$/ && value[key].is_a?(Array)
# ids, foo_ids, etc.
value[key].map!{ |id| stringify_json_id(id, opts) }
end
end
end
def self.stringify_json_id(id, opts = {})
if opts[:reverse]
id.is_a?(String) ? id.to_i : id
else
id.is_a?(Integer) ? id.to_s : id
end
end
def accepts_jsonapi?
!!(/application\/vnd\.api\+json/ =~ request.headers['Accept'].to_s)
end
Quiz Submissions API - Create & Complete Allows users to start a "quiz-taking session" via the API by creating a QuizSubmission and later on completing it. Note that this patch isn't concerned with actually using the QS to answer questions. That task will be the concern of a new API controller, QuizSubmissionQuestions. closes CNVS-8980 TEST PLAN ---- ---- - Create a quiz - Keep a tab open on the Moderate Quiz (MQ from now) page Create the quiz submission (ie, start a quiz-taking session): - Via the API, as a student: - POST to /courses/:course_id/quizzes/:quiz_id/submissions - Verify that you receive a 200 response with the newly created QuizSubmission in the JSON response. - Copy the "validation_token" field down, you will need this later - Go to the MQ tab and verify that it says the student has started a quiz attempt Complete the quiz submission (ie, finish a quiz-taking session): - Via the API, as a student, prepare a request with: - Method: POST - URI: /courses/:course_id/quizzes/:quiz_id/submissions/:id/complete - Parameter "validation_token" to what you copied earlier - Parameter "attempt" to the current attempt number (starts at 1) - Now perform the request, and: - Verify that you receive a 200 response - Go to the MQ tab and verify that it says the submission has been completed (ie, Time column reads "finished in X seconds/minutes") Other stuff to test (failure scenarios): The first endpoint (one for starting a quiz attempt) should reject your request in any of the following cases: - The quiz has been locked - You are not enrolled in the quiz course - The Quiz has an Access Code that you either didn't pass, or passed incorrectly - The Quiz has an IP filter and you're not in the address range - You are already taking the quiz (you've created the submission and did not call /complete yet) - You are not currently taking the quiz, but you already took it earlier and the Quiz does not allow for multiple attempts The second endpoint (one for completing the quiz attempt) should reject your request in any of the following cases: - You pass in an invalid "validation_token" - You already completed that quiz submission (e.g, you called that endpoint earlier) Change-Id: Iff8a47859d7477c210de46ea034544d5e2527fb2 Reviewed-on: https://gerrit.instructure.com/27015 Reviewed-by: Derek DeVries <ddevries@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Myller de Araujo <myller@instructure.com> Product-Review: Ahmad Amireh <ahmad@instructure.com>
2013-12-05 22:10:12 +08:00
add option for course grade change (log auditing) fixes: CNVS-8996 Added a grade change audit UI for searching grade change events. This allows the user to search based off of the grader, student, course id, and assignment id. In addition to each parameter a date range can be selected. Currently assignment and course can only be searched if the ID is known. This is because there is no way to query for courses based on a name with the api. Note: The submission after_save :grade_change_audit needed to be after the simply_versioned call because the grade change audit uses that to grab the previous grade. This was a bug in the grade change audit log api. This fixes that issue also. Test Case: - Create a course with an assignment and student. - Grade the assignment for the student. - Change the grade for the student a few times. - Open the admin tools. Select the Logging tab and then pick the grade change activity option in the drop down. - Search for the grader. The results from the grade changes should show accordingly. - Search for the student. The results from the grade changes should show accordingly. - Search for the course id. The results from the grade changes should show accordingly. - Search for the assignment id. The results from the grade changes should show accordingly. - Perform each search type again, testing the date range capabilities. - Make sure you cannot search with an invalid date range, grader, and student. - Enter an invalid course id, no results should be returned. - Enter an invalid assignment id, no results should be returned. Change-Id: Ie5a4d34dbb60627374035071c68ec4d404e80135 Reviewed-on: https://gerrit.instructure.com/26868 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Nick Cloward <ncloward@instructure.com> Product-Review: Nick Cloward <ncloward@instructure.com> QA-Review: Nick Cloward <ncloward@instructure.com> Tested-by: Nick Cloward <ncloward@instructure.com>
2013-12-03 04:42:07 +08:00
# Return a template url that follows the root links key for the jsonapi.org
# standard.
#
def templated_url(method, *args)
format = /^\{.*\}$/
placeholder = "PLACEHOLDER"
placeholders = args.each_with_index.map do |arg, index|
arg =~ format ? "#{placeholder}#{index}" : arg
end
url = send(method, *placeholders)
args.each_with_index do |arg, index|
url.sub!("#{placeholder}#{index}", arg) if arg =~ format
end
url
end
end