1.修正新建课程错误

2.优化课程角色权限判断
This commit is contained in:
nwb 2014-06-09 10:26:45 +08:00
parent 59742b9ddb
commit 54505340eb
7 changed files with 50 additions and 14 deletions

View File

@ -100,6 +100,7 @@ class CoursesController < ApplicationController
unless User.current.admin? unless User.current.admin?
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
m = Member.new(:user => User.current, :roles => [r]) m = Member.new(:user => User.current, :roles => [r])
m.project_id = -1
course = CourseInfos.new(:user_id => User.current.id, :course_id => @course.id) course = CourseInfos.new(:user_id => User.current.id, :course_id => @course.id)
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
if params[:course][:is_public] == '1' if params[:course][:is_public] == '1'
@ -367,9 +368,6 @@ class CoursesController < ApplicationController
:with_subprojects => false, :with_subprojects => false,
:author => @author) :author => @author)
@activity.scope_select {|t| !has["show_#{t}"].nil?} @activity.scope_select {|t| !has["show_#{t}"].nil?}
# logger.debug "=========================================#{@activity.scope}"
# @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty?
#Added by young
events = @activity.events(@date_from, @date_to) events = @activity.events(@date_from, @date_to)
@offset, @limit = api_offset_and_limit({:limit => 10}) @offset, @limit = api_offset_and_limit({:limit => 10})
@ -377,7 +375,7 @@ class CoursesController < ApplicationController
@events_pages = Paginator.new @events_count, @limit, params['page'] @events_pages = Paginator.new @events_count, @limit, params['page']
@offset ||= @events_pages.offset @offset ||= @events_pages.offset
events = events.slice(@offset,@limit) events = events.slice(@offset,@limit)
#Ended by young
@events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)} @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)}
# documents # documents
@sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'

View File

@ -16,6 +16,7 @@ class Course < ActiveRecord::Base
has_many :member_principals, :class_name => 'Member', has_many :member_principals, :class_name => 'Member',
:include => :principal, :include => :principal,
:conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})" :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})"
has_many :principals, :through => :member_principals, :source => :principal
has_many :users, :through => :members has_many :users, :through => :members
has_many :homeworks, :through => :homework_for_courses, :source => :bid, :dependent => :destroy has_many :homeworks, :through => :homework_for_courses, :source => :bid, :dependent => :destroy
has_many :journals_for_messages, :as => :jour, :dependent => :destroy has_many :journals_for_messages, :as => :jour, :dependent => :destroy
@ -23,6 +24,7 @@ class Course < ActiveRecord::Base
has_many :student, :through => :students_for_courses, :source => :user has_many :student, :through => :students_for_courses, :source => :user
has_many :course_infos, :class_name => 'CourseInfos',:dependent => :destroy has_many :course_infos, :class_name => 'CourseInfos',:dependent => :destroy
has_many :enabled_modules, :dependent => :delete_all has_many :enabled_modules, :dependent => :delete_all
has_many :boards, :dependent => :destroy, :order => "position ASC"
acts_as_taggable acts_as_taggable
#acts_as_nested_set :order => 'name', :dependent => :destroy #acts_as_nested_set :order => 'name', :dependent => :destroy
@ -31,6 +33,10 @@ class Course < ActiveRecord::Base
validates_presence_of :password, :term validates_presence_of :password, :term
validates_format_of :class_period, :message => "class period can only digital!", :with =>/^[1-9]\d*$/ validates_format_of :class_period, :message => "class period can only digital!", :with =>/^[1-9]\d*$/
after_save :create_board_sync
before_destroy :delete_all_members
safe_attributes 'extra', safe_attributes 'extra',
'time', 'time',
'name', 'name',
@ -128,6 +134,25 @@ class Course < ActiveRecord::Base
end end
end end
def create_board_sync
@board = self.boards.build
self.name=" #{l(:label_borad_course) }"
@board.name = self.name
@board.description = self.name.to_s
@board.project_id = -1
if @board.save
logger.debug "[Course Model] ===> #{@board.to_json}"
else
logger.error "[Course Model] ===> Auto create board when course saved, because #{@board.full_messages}"
end
end
def delete_all_members
me, mr = Member.table_name, MemberRole.table_name
connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.course_id = #{id})")
Member.delete_all(['course_id = ?', id])
end
def get_endup_time def get_endup_time
begin begin
end_time = Time.parse(self.endup_time) end_time = Time.parse(self.endup_time)

View File

@ -39,6 +39,10 @@ class JournalsForMessage < ActiveRecord::Base
:timestamp => "#{self.table_name}.updated_on", :timestamp => "#{self.table_name}.updated_on",
:find_options => {:include => :project } :find_options => {:include => :project }
acts_as_activity_provider :author_key => :user_id,
:timestamp => "#{self.table_name}.updated_on",
:find_options => {:include => :course }
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
validates :notes, presence: true validates :notes, presence: true

View File

@ -1129,17 +1129,13 @@ class Project < ActiveRecord::Base
# 创建项目后在项目下同步创建一个讨论区 # 创建项目后在项目下同步创建一个讨论区
def create_board_sync def create_board_sync
@board = self.boards.build @board = self.boards.build
if project_type == 1
self.name=" #{l(:label_borad_course) }" self.name=" #{l(:label_borad_course) }"
else
self.name =" #{l(:label_borad_project) }"
end
@board.name = self.name @board.name = self.name
@board.description = self.name.to_s @board.description = self.name.to_s
if @board.save if @board.save
logger.debug "[Project Model] ===> #{@board.to_json}" logger.debug "[Course Model] ===> #{@board.to_json}"
else else
logger.error "[Project Model] ===> Auto create board when project saved, because #{@board.full_messages}" logger.error "[Course Model] ===> Auto create board when Course saved, because #{@board.full_messages}"
end end
end end

View File

@ -634,6 +634,15 @@ class User < Principal
@membership_by_project_id[project_id] @membership_by_project_id[project_id]
end end
def coursemembership(course)
course_id = course.is_a?(Course) ? course.id : course
@membership_by_course_id ||= Hash.new {|h, course_id|
h[course_id] = coursememberships.where(:course_id => course_id).first
}
@membership_by_course_id[course_id]
end
# Return user's roles for project # Return user's roles for project
def roles_for_project(project) def roles_for_project(project)
roles = [] roles = []
@ -662,7 +671,7 @@ class User < Principal
return roles if course.nil? || course.archived? return roles if course.nil? || course.archived?
if logged? if logged?
# Find course membership # Find course membership
membership = membership(course) membership = coursemembership(course)
if membership if membership
roles = membership.roles roles = membership.roles
else else

View File

@ -16,6 +16,7 @@
<table width="580px" border="0"> <table width="580px" border="0">
<tr> <tr>
<td colspan="2" valign="top"> <td colspan="2" valign="top">
<%= test=e.course%>
<strong> <%= h(e.course) if @course.nil? || (e.course != nil && @course.id != e.course.id) %></strong> <strong> <%= h(e.course) if @course.nil? || (e.course != nil && @course.id != e.course.id) %></strong>
<span class="font_lighter"> <span class="font_lighter">
<% if @canShowRealName %> <% if @canShowRealName %>

View File

@ -74,6 +74,9 @@ module Redmine
scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options)) scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
elsif respond_to?(:visible) elsif respond_to?(:visible)
scope = scope.visible(user, options) scope = scope.visible(user, options)
elsif options[:course]
ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option."
scope = scope.scoped(:conditions => Course.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
else else
ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option." ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option."
scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options)) scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))