课程权限判断变更,从项目权限分离

This commit is contained in:
nwb 2014-06-03 16:21:00 +08:00
parent 8460344043
commit cd10dd5433
5 changed files with 68 additions and 34 deletions

View File

@ -52,11 +52,40 @@ class CoursesController < ApplicationController
end end
end end
def handle_course courses,activities
course_activity_count_array=activities.values()
course_array=[]
i=0;
courses.each do |course|
course_array[i]=course
i=i+1
end
courses=desc_sort_course_by_avtivity(course_activity_count_array,course_array)
return courses
end
def desc_sort_course_by_avtivity(activity_count,courses)
return courses if activity_count.size<2
(activity_count.size-2).downto(0) do |i|
(0..i).each do |j|
if activity_count[j]<activity_count[j+1]
courses[j],courses[j+1]=courses[j+1],courses[j]
activity_count[j],activity_count[j+1]=activity_count[j+1],activity_count[j]
end
end
end
return courses
end
def index def index
@course_type = params[:course_type] @course_type = params[:course_type]
@school_id = params[:school_id] @school_id = params[:school_id]
per_page_option = 10 per_page_option = 10
if @school_id == "0" or @school_id.nil? if @school_id == "0" or @school_id.nil?
@courses_all = Course.active.visible. @courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id") joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id")
@ -84,7 +113,7 @@ class CoursesController < ApplicationController
case params[:course_sort_type] case params[:course_sort_type]
when '0' when '0'
@courses = @courses_all.order("created_on desc") @courses = @courses_all.order("created_at desc")
@s_type = 0 @s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@ -122,7 +151,7 @@ class CoursesController < ApplicationController
@courses = @courses[@course_pages.offset, @course_pages.per_page] @courses = @courses[@course_pages.offset, @course_pages.per_page]
else else
@s_type = 0 @s_type = 0
@courses = @courses_all.order("created_on desc") @courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
#gcm #gcm
@ -142,6 +171,7 @@ class CoursesController < ApplicationController
end end
end end
def get_course_activity courses,activities def get_course_activity courses,activities
@course_ids=activities.keys() @course_ids=activities.keys()
@ -160,11 +190,6 @@ class CoursesController < ApplicationController
activities[board.course_id]+=board.messages.where("updated_on>?",date_from).count activities[board.course_id]+=board.messages.where("updated_on>?",date_from).count
end end
#time_entry_count
TimeEntry.where(course_id: @course_ids).where("updated_on>?",date_from).each do |timeentry|
activities[timeentry.course_id]+=1
end
#feedbackc_count #feedbackc_count
JournalsForMessage.where(jour_id: @course_ids, jour_type: Project).each do |jourformess| JournalsForMessage.where(jour_id: @course_ids, jour_type: Project).each do |jourformess|
activities[jourformess.jour_id]+=1 activities[jourformess.jour_id]+=1

View File

@ -126,29 +126,25 @@ module CoursesHelper
content = ''.html_safe content = ''.html_safe
case state case state
when 0 when 0
content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:course_sort_type => '0'), :school_id => school_id, :class=>"selected"), :class=>"selected") content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0'), :school_id => school_id, :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:course_sort_type => '1', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
# content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:course_sort_type => '2'))) content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:course_sort_type => '3', :school_id => school_id)))
when 1 when 1
content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:course_sort_type => '0', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:course_sort_type => '1', :school_id => school_id), :class=>"selected"), :class=>"selected") content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id), :class=>"selected"), :class=>"selected")
# content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:course_sort_type => '2'))) content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:course_sort_type => '3', :school_id => school_id)))
when 2 when 2
content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:course_sort_type => '0', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:course_sort_type => '1', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
# content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:course_sort_type => '2'), :class=>"selected"), :class=>"selected") content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:course_sort_type => '3', :school_id => school_id)))
#gcm #gcm
when 3 when 3
content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:course_sort_type => '0', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:course_sort_type => '1', :school_id => school_id))) content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
# content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:course_sort_type => '2'))) content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id), :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:course_sort_type => '3', :school_id => school_id), :class=>"selected"), :class=>"selected")
end end
#gcmend #gcmend

View File

@ -78,16 +78,15 @@ class Course < ActiveRecord::Base
end end
def self.allowed_to_condition(user, permission, options={}) def self.allowed_to_condition(user, permission, options={})
perm = Redmine::AccessControl.permission(permission) perm = Redmine::AccessControl.permission(permission)
base_statement = (perm && perm.read? ? "#{Course.table_name}.status <> #{Course::STATUS_ARCHIVED}" : "#{Course.table_name}.status = #{Course::STATUS_ACTIVE}") base_statement = (perm && perm.read? ? "#{Course.table_name}.status <> #{Course::STATUS_ARCHIVED}" : "#{Course.table_name}.status = #{Course::STATUS_ACTIVE}")
if perm && perm.project_module if perm && perm.course_module
# If the permission belongs to a project module, make sure the module is enabled
base_statement << " AND #{Course.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" base_statement << " AND #{Course.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
end end
if options[:project] if options[:course]
project_statement = "#{Course.table_name}.id = #{options[:project].id}" course_statement = "#{Course.table_name}.id = #{options[:course].id}"
project_statement << " OR (#{Course.table_name}.lft > #{options[:project].lft} AND #{Course.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] course_statement << " OR (#{Course.table_name}.lft > #{options[:course].lft} AND #{Course.table_name}.rgt < #{options[:course].rgt})" if options[:with_subcourses]
base_statement = "(#{project_statement}) AND (#{base_statement})" base_statement = "(#{course_statement}) AND (#{base_statement})"
end end
if user.admin? if user.admin?
@ -101,9 +100,9 @@ class Course < ActiveRecord::Base
end end
end end
if user.logged? if user.logged?
user.projects_by_role.each do |role, projects| user.courses_by_role.each do |role, courses|
if role.allowed_to?(permission) && projects.any? if role.allowed_to?(permission) && courses.any?
statement_by_role[role] = "#{Course.table_name}.id IN (#{projects.collect(&:id).join(',')})" statement_by_role[role] = "#{Course.table_name}.id IN (#{courses.collect(&:id).join(',')})"
end end
end end
end end

View File

@ -0,0 +1,13 @@
class AddCourseidToEnabledModules < ActiveRecord::Migration
def change
add_column :enabled_modules, :course_id, :int
EnabledModule.all.each do |enablemodule|
project = Project.find_by_id(enablemodule.project_id)
if project && project.project_type == 1
enablemodule.course_id = board.project_id
enablemodule.save
end
end
end
end

View File

@ -180,7 +180,8 @@ Redmine::AccessControl.map do |map|
#课程权限模块 #课程权限模块
map.project_module :course do map.project_module :course do
map.permission :view_courses,{:projects => [:course]},:read => true map.permission :view_course, {:courses => [:show], :activities => [:index]}, :public => true, :read => true
map.permission :view_courses,{:courses => [:course]},:read => true
end end
#作业模块权限 #作业模块权限