canvas-lms/app/models/wiki.rb

185 lines
5.2 KiB
Ruby
Raw Normal View History

2011-02-01 09:57:29 +08:00
#
# Copyright (C) 2011 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/>.
#
# == Schema Information
#
# Table name: wikis
#
# id :integer(4) not null, primary key
# title :string(255)
# created_at :datetime
# updated_at :datetime
#
class Wiki < ActiveRecord::Base
attr_accessible :title
2011-02-01 09:57:29 +08:00
has_many :wiki_pages, :dependent => :destroy
before_save :set_has_no_front_page_default
2011-02-01 09:57:29 +08:00
after_save :update_contexts
DEFAULT_FRONT_PAGE_URL = 'front-page'
def set_has_no_front_page_default
if self.has_no_front_page.nil? && self.id && context
self.has_no_front_page = true if context.feature_enabled?(:draft_state)
end
change new pages to show/hide elements correctly test plan (in the course and group wikis): * enable draft state for the account (enable_draft) - general * Pages course tab navigates to new pages - front page - index page (if no front page is set) * Pages breadcrumb (in pages) navigates to new pages index - index page * New Page button is hidden unless user has create rights * Publish icon & settings cog hidden unless user is a teacher or admin - show page * Published button is hidden unless user is a teacher or admin * Published button functions properly (publishes and unpublishes) * Edit button is hidden unless user has edit rights to the page * Settings cog/Delete button is hidden unless the user has delete rights to the page * Page content is restricted if the page is locked by a module - edit page * Published indicator is hidden unless user is a teacher or admin * Settings cog/Delete button is hidden unless the user has delete rights to the page * Title field is hidden unless user has full edit rights (course setting) * Hide this page from students is hidden unless user is a teacher or admin * ... can edit this page is hidden unless user has full edit rights (course setting) * Unauthorized error if page is locked by a module - pages api * Unauthorized error when updating page if page is locked by a module fixes #CNVS-6859 Change-Id: I12239e58a5f267c43fd2bcb912c7b485693de2c1 Reviewed-on: https://gerrit.instructure.com/22677 Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Reviewed-by: Sterling Cobb <sterling@instructure.com> Product-Review: Matt Goodwin <mattg@instructure.com>
2013-07-26 06:14:15 +08:00
end
private :set_has_no_front_page_default
change new pages to show/hide elements correctly test plan (in the course and group wikis): * enable draft state for the account (enable_draft) - general * Pages course tab navigates to new pages - front page - index page (if no front page is set) * Pages breadcrumb (in pages) navigates to new pages index - index page * New Page button is hidden unless user has create rights * Publish icon & settings cog hidden unless user is a teacher or admin - show page * Published button is hidden unless user is a teacher or admin * Published button functions properly (publishes and unpublishes) * Edit button is hidden unless user has edit rights to the page * Settings cog/Delete button is hidden unless the user has delete rights to the page * Page content is restricted if the page is locked by a module - edit page * Published indicator is hidden unless user is a teacher or admin * Settings cog/Delete button is hidden unless the user has delete rights to the page * Title field is hidden unless user has full edit rights (course setting) * Hide this page from students is hidden unless user is a teacher or admin * ... can edit this page is hidden unless user has full edit rights (course setting) * Unauthorized error if page is locked by a module - pages api * Unauthorized error when updating page if page is locked by a module fixes #CNVS-6859 Change-Id: I12239e58a5f267c43fd2bcb912c7b485693de2c1 Reviewed-on: https://gerrit.instructure.com/22677 Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Reviewed-by: Sterling Cobb <sterling@instructure.com> Product-Review: Matt Goodwin <mattg@instructure.com>
2013-07-26 06:14:15 +08:00
2011-02-01 09:57:29 +08:00
def update_contexts
self.context.try(:touch)
2011-02-01 09:57:29 +08:00
end
def to_atom
Atom::Entry.new do |entry|
entry.title = self.title
entry.updated = self.updated_at
entry.published = self.created_at
entry.links << Atom::Link.new(:rel => 'alternate',
:href => "/wikis/#{self.id}")
end
end
def update_default_wiki_page_roles(new_roles, old_roles)
return if new_roles == old_roles
self.wiki_pages.each do |p|
if p.editing_roles == old_roles
p.editing_roles = new_roles
p.save
end
end
end
change new pages to show/hide elements correctly test plan (in the course and group wikis): * enable draft state for the account (enable_draft) - general * Pages course tab navigates to new pages - front page - index page (if no front page is set) * Pages breadcrumb (in pages) navigates to new pages index - index page * New Page button is hidden unless user has create rights * Publish icon & settings cog hidden unless user is a teacher or admin - show page * Published button is hidden unless user is a teacher or admin * Published button functions properly (publishes and unpublishes) * Edit button is hidden unless user has edit rights to the page * Settings cog/Delete button is hidden unless the user has delete rights to the page * Page content is restricted if the page is locked by a module - edit page * Published indicator is hidden unless user is a teacher or admin * Settings cog/Delete button is hidden unless the user has delete rights to the page * Title field is hidden unless user has full edit rights (course setting) * Hide this page from students is hidden unless user is a teacher or admin * ... can edit this page is hidden unless user has full edit rights (course setting) * Unauthorized error if page is locked by a module - pages api * Unauthorized error when updating page if page is locked by a module fixes #CNVS-6859 Change-Id: I12239e58a5f267c43fd2bcb912c7b485693de2c1 Reviewed-on: https://gerrit.instructure.com/22677 Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Reviewed-by: Sterling Cobb <sterling@instructure.com> Product-Review: Matt Goodwin <mattg@instructure.com>
2013-07-26 06:14:15 +08:00
def check_has_front_page
return unless self.has_no_front_page.nil?
url = DEFAULT_FRONT_PAGE_URL
self.has_no_front_page = !self.wiki_pages.not_deleted.where(:url => url).exists?
self.front_page_url = url unless self.has_no_front_page
change new pages to show/hide elements correctly test plan (in the course and group wikis): * enable draft state for the account (enable_draft) - general * Pages course tab navigates to new pages - front page - index page (if no front page is set) * Pages breadcrumb (in pages) navigates to new pages index - index page * New Page button is hidden unless user has create rights * Publish icon & settings cog hidden unless user is a teacher or admin - show page * Published button is hidden unless user is a teacher or admin * Published button functions properly (publishes and unpublishes) * Edit button is hidden unless user has edit rights to the page * Settings cog/Delete button is hidden unless the user has delete rights to the page * Page content is restricted if the page is locked by a module - edit page * Published indicator is hidden unless user is a teacher or admin * Settings cog/Delete button is hidden unless the user has delete rights to the page * Title field is hidden unless user has full edit rights (course setting) * Hide this page from students is hidden unless user is a teacher or admin * ... can edit this page is hidden unless user has full edit rights (course setting) * Unauthorized error if page is locked by a module - pages api * Unauthorized error when updating page if page is locked by a module fixes #CNVS-6859 Change-Id: I12239e58a5f267c43fd2bcb912c7b485693de2c1 Reviewed-on: https://gerrit.instructure.com/22677 Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Reviewed-by: Sterling Cobb <sterling@instructure.com> Product-Review: Matt Goodwin <mattg@instructure.com>
2013-07-26 06:14:15 +08:00
self.save
end
2011-02-01 09:57:29 +08:00
def front_page
url = self.get_front_page_url
return nil if url.nil?
# TODO i18n
t :front_page_name, "Front Page"
# attempt to find the page and store it's url (if it is found)
page = self.wiki_pages.not_deleted.find_by_url(url)
self.set_front_page_url!(url) if self.has_no_front_page && page
# return an implicitly created page if a page could not be found
unless page
page = self.wiki_pages.new(:title => url.titleize, :url => url)
page.wiki = self
end
page
end
def has_front_page?
!self.has_no_front_page
end
def get_front_page_url
return nil unless self.has_front_page? || !context.feature_enabled?(:draft_state)
self.front_page_url || DEFAULT_FRONT_PAGE_URL
end
def unset_front_page!
if self.context.is_a?(Course) && self.context.default_view == 'wiki'
self.context.default_view = 'feed'
self.context.save
end
self.front_page_url = nil
self.has_no_front_page = true
self.save
end
def set_front_page_url!(url)
return false if url.blank?
return true if self.has_front_page? && self.front_page_url == url
self.has_no_front_page = false
self.front_page_url = url
self.save
2011-02-01 09:57:29 +08:00
end
def context
shard.activate do
@context ||= Course.find_by_wiki_id(self.id) || Group.find_by_wiki_id(self.id)
end
end
def context_type
context.class.to_s
end
def context_id
context.id
end
set_policy do
given {|user| self.context.is_public}
can :read
given {|user, session| self.cached_context_grants_right?(user, session, :read)}
can :read
given {|user, session| self.cached_context_grants_right?(user, session, :participate_as_student) && self.context.allow_student_wiki_edits}
can :read and can :create_page and can :update_page and can :update_page_content
given {|user, session| self.cached_context_grants_right?(user, session, :manage_wiki)}
can :manage and can :read and can :update and can :create_page and can :delete_page and can :delete_unpublished_page and can :update_page and can :update_page_content
end
def self.wiki_for_context(context)
return context.wiki_without_create if context.wiki_id
context.transaction do
# otherwise we lose dirty changes
context.save! if context.changed?
context.lock!
return context.wiki_without_create if context.wiki_id
# TODO i18n
t :default_course_wiki_name, "%{course_name} Wiki", :course_name => nil
t :default_group_wiki_name, "%{group_name} Wiki", :group_name => nil
self.extend TextHelper
name = truncate_text(context.name, {:max_length => 200, :ellipsis => ''})
context.wiki = wiki = Wiki.create!(:title => "#{name} Wiki")
context.save!
wiki
end
end
allow PUT requests to create wiki pages implicitly testing note: this change affects all wiki page endpoints: api, draft state, and non-draft state. basic functionality for all of these pages should be verified, specifically when dealing with non-existent or deleted pages. test plan: * PUT to /api/v1/courses/:course_id/front_page - variations: * wiki_page[title] - provided/not provided * wiki_page[published] - true/false/not provided * wiki_page[hide_from_students] - true/false/not provided (hiding/unpublishing the front page is not allowed) * PUT to /api/v1/courses/:course_id/pages/non-existent-page - same variations as front_page (above) * navigating to a non-existent/deleted page (draft state/non-draft state) - should redirect teachers to the edit page - should redirect students to the index page (with an alert message indicating why) * navigating to the 'Pages' tab (non-draft state) - if the 'Front Page' exists - shows the page - if the 'Front Page' has been deleted (with draft state enabled) - shows the edit page - if the 'Front Page' has never existed - shows the edit page * all other pages functionality should remain unchanged - non-draft state UI - show page - edit page - draft state UI - index page - show page - edit page - api - .../pages - GET (index) - POST (create page) - .../front_page - GET (show front page) - PUT (create/update front page) - .../pages/page-url - GET (show page) - PUT (create/update page) - DELETE (destroy page) fixes CNVS-8488 Change-Id: I563e6944e1602e0b21ab69c6fe2dcd643c06611d Reviewed-on: https://gerrit.instructure.com/23590 Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Matt Fairbourn <mfairbourn@instructure.com> Reviewed-by: Jeremy Stanley <jeremy@instructure.com> Product-Review: Bracken Mosbacker <bracken@instructure.com>
2013-08-22 23:43:03 +08:00
def build_wiki_page(user, opts={})
if (opts.include?(:url) || opts.include?(:title)) && (!opts.include?(:url) || !opts.include?(:title))
opts[:title] = opts[:url].to_s.titleize if opts.include?(:url)
opts[:url] = opts[:title].to_s.to_url if opts.include?(:title)
end
page = WikiPage.new(opts)
page.wiki = self
page.initialize_wiki_page(user)
page
end
2011-02-01 09:57:29 +08:00
end