添加与course.index相关的control、view、help等实现

This commit is contained in:
nwb 2014-05-29 16:35:34 +08:00
parent 4e815a38b6
commit f936c30ec9
8 changed files with 390 additions and 10 deletions

View File

@ -73,7 +73,7 @@ class ContestsController < ApplicationController
end
end
def show_contest
def show_contest
@user = @contest.author
@jours = @contest.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC')
@limit = 10

View File

@ -1,9 +1,19 @@
class CoursesController < ApplicationController
include CoursesHelper
menu_item l(:label_sort_by_time), :only => :index
menu_item l(:label_sort_by_active), :only => :index
menu_item l(:label_sort_by_influence), :only => :index
before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches]
before_filter :authorize, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen,:view_homework_attaches,:course]
before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches]
before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar]
before_filter :require_login, :only => [:join, :unjoin]
before_filter :allow_join, :only => [:join]
def join
if User.current.logged?
course = Project.find(params[:object_id])
@ -59,4 +69,168 @@ class CoursesController < ApplicationController
end
end
def course12
@project_type = params[:project_type]
@school_id = params[:school_id]
per_page_option = 10
if @school_id == "0" or @school_id.nil?
@projects_all = Project.active.visible.
joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
where("#{Project.table_name}.project_type = ? ", Project::ProjectType_course)
else
@projects_all = Project.active.visible.
joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
joins(:course_extra).
where("#{Project.table_name}.project_type = ? AND #{Course.table_name}.school_id = ?", Project::ProjectType_course, @school_id)
end
@project_count = @projects_all.count
@project_pages = Paginator.new @project_count, per_page_option, params['page']
#gcm activity count
@project_activity_count=Hash.new
#count initialize
@projects_all.each do |project|
@project_activity_count[project.id]=0
end
#@project_activity_count=get_project_activity @projects_all,@project_activity_count
#gcm end
case params[:project_sort_type]
when '0'
@projects = @projects_all.order("created_on desc")
@s_type = 0
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
#gcm
@project_activity_count=get_project_activity @projects,@project_activity_count
#gcmend
when '1'
@projects = @projects_all.order("course_ac_para desc")
@s_type = 1
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
#gcm
@project_activity_count=get_project_activity @projects,@project_activity_count
#gcmend
when '2'
@projects = @projects_all.order("watchers_count desc")
@s_type = 2
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
#gcm
@project_activity_count=get_project_activity @projects,@project_activity_count
#gcmend
#gcm
when '3'
#gcm
@project_activity_count=get_project_activity @projects_all,@project_activity_count
#gcmend
@projects=handle_project @projects_all,@project_activity_count
@s_type = 3
@projects = @projects[@project_pages.offset, @project_pages.per_page]
else
@s_type = 0
@projects = @projects_all.order("created_on desc")
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
#gcm
@project_activity_count=get_project_activity @projects,@project_activity_count
#gcmend
end
respond_to do |format|
format.html {
render :layout => 'base'
}
format.api {
# @offset, @limit = api_offset_and_limit
# @project_count = Project.visible.count
# @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
}
format.atom {
projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
}
end
end
def index
per_page_option = 10
@courses_all = Course.active.visible.
joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.couse_id")
@course_count = @courses_all.count
@course_pages = Paginator.new @course_count, per_page_option, params['page']
#gcm activity count
@course_activity_count=Hash.new
@courses_all.each do |course|
@course_activity_count[course.id]=0
end
@course_activity_count=get_course_activity @courses_all,@course_activity_count
#gcm end
case params[:course_sort_type]
when '0'
@courses = @courses_all.order("created_on desc")
@s_type = 0
when '1'
@courses = @courses_all.order("grade desc")
@s_type = 1
when '2'
@courses = @courses_all.order("watchers_count desc")
@s_type = 2
#gcm
when '3'
#@courses=desc_sort_course_by_avtivity(@course_activity_count_array,@course_all_array)
@courses=handle_course @courses_all,@course_activity_count
@s_type = 3
@courses = @courses[@course_pages.offset, @course_pages.per_page]
else
@courses = @courses = @courses_all.order("grade desc")
@s_type = 1
end
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
respond_to do |format|
format.html {
render :layout => 'base'
# scope = Project
# unless params[:closed]
# scope = scope.active
# end
}
format.api {
# @offset, @limit = api_offset_and_limit
# @course_count = Project.visible.count
# @courses = Project.visible.offset(@offset).limit(@limit).order('lft').all
}
format.atom {
courses = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
}
end
end
end

View File

@ -170,6 +170,18 @@ module ApplicationHelper
end
end
def link_to_course(course, options={}, html_options = nil)
if course.archived?
h(course.name)
elsif options.key?(:action)
ActiveSupport::Deprecation.warn "#link_to_course with :action option is deprecated and will be removed in Redmine 3.0."
url = {:controller => 'courses', :action => 'show', :id => project}.merge(options)
link_to course.name, url, html_options
else
link_to course.name, course_path(course, options), html_options
end
end
# Generates a link to a project settings if active
def link_to_project_settings(project, options={}, html_options=nil)
if project.active?
@ -297,7 +309,41 @@ module ApplicationHelper
@project = original_project
end
s.html_safe
end
def render_course_nested_lists(courses)
s = ''
if courses.any?
ancestors = []
original_course = @course
#modified by nie
courses.each do |course|
# set the project environment to please macros.
@course = course
if (ancestors.empty? || course.is_descendant_of?(ancestors.last))
s << "<ul class=courses'>\n"
else
ancestors.pop
s << "</li>"
while (ancestors.any? && !course.is_descendant_of?(ancestors.last))
ancestors.pop
s << "</ul></li>\n"
end
end
classes = (ancestors.empty? ? 'root' : 'child')
s << "<li class='project-table'><div class='#{classes}'>"
s << (render :partial => 'courses/course', :locals => {:course => course}).to_s
s << "</div>\n"
ancestors << course
end
s << ("</li></ul>\n" * ancestors.size)
@course = original_course
end
s.html_safe
end
#added by young
def render_project_nested_lists_new(projects)
s = ''
@ -1639,9 +1685,9 @@ module ApplicationHelper
main_project_link = link_to l(:label_project_deposit), {:controller => 'welcome', :action => 'index', :host => Setting.project_domain}
main_contest_link = link_to l(:label_contest_innovate), {:controller => 'welcome', :action => 'index', :host => Setting.contest_domain}
course_all_course_link = link_to l(:label_course_all), {:controller => 'projects', :action => 'course', :project_type => 1, :host => Setting.course_domain}
course_all_course_link = link_to l(:label_course_all), {:controller => 'courses', :action => 'index', :host => Setting.course_domain}
course_teacher_all_link = link_to l(:label_teacher_all), {:controller => 'users', :action => 'index', :role => 'teacher', :host => Setting.course_domain}
courses_link = link_to l(:label_course_practice), {:controller => 'projects', :action => 'course', :project_type => 1, :host => Setting.course_domain}
courses_link = link_to l(:label_course_practice), {:controller => 'courses', :action => 'index',:host => Setting.course_domain}
projects_link = link_to l(:label_project_deposit), {:controller => 'projects', :action => 'index', :project_type => 0, :host => Setting.project_domain}
users_link = link_to l(:label_software_user), {:controller => 'users', :action => 'index', :host => Setting.user_domain}
contest_link = link_to l(:label_contest_innovate), {:controller => 'contests', :action => 'index'}

View File

@ -55,8 +55,9 @@ module ContestsHelper
content_tag('div', content, :class => "tabs_enterprise")
end
#end
#huang
def sort_contest(state)
content = ''.html_safe

View File

@ -82,6 +82,35 @@ module CoursesHelper
members
end
def sort_courses(state)
content = ''.html_safe
case state
when 0
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1')))
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2')))
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0'), :class=>"selected"), :class=>"selected")
when 1
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1'), :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2')))
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0')))
when 2
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1')))
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2'), :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0')))
end
content = content_tag('ul', content)
content_tag('div', content, :class => "tabs")
end
def render_course_hierarchy(courses)
render_course_nested_lists(courses) do |course|
s = link_to_course(course, {}, :class => "#{course.css_classes} #{User.current.member_of?(course) ? 'my-course' : nil}").html_safe
s
end
end
#useless
def searchMembersByRole project, role_id
members = []

View File

@ -1,8 +1,12 @@
class Course < ActiveRecord::Base
include Redmine::SafeAttributes
STATUS_ACTIVE = 1
STATUS_CLOSED = 5
STATUS_ARCHIVED = 9
attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password
belongs_to :project, :class_name => 'Project', :foreign_key => :extra, primary_key: :identifier
belongs_to :project, :class_name => 'Course', :foreign_key => :extra, primary_key: :identifier
belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher该方法通过tea_id来调用User表
belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school该方法通过school_id来调用School表
has_many :bid
@ -19,7 +23,24 @@ class Course < ActiveRecord::Base
'term',
'password'
#自定义验证
scope :active, lambda { where(:status => STATUS_ACTIVE) }
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
scope :all_public, lambda { where(:is_public => true) }
scope :visible, lambda {|*args| where(Course.visible_condition(args.shift || User.current, *args)) }
scope :allowed_to, lambda {|*args|
user = User.current
permission = nil
if args.first.is_a?(Symbol)
permission = args.shift
else
user = args.shift
permission = args.shift
end
where(Course.allowed_to_condition(user, permission, *args))
}
#自定义验证
def validate
if !class_period.match([0-9])
errors.add_to_base("class period can only digital")
@ -47,4 +68,50 @@ class Course < ActiveRecord::Base
return time
end
end
def self.allowed_to_condition(user, permission, options={})
perm = Redmine::AccessControl.permission(permission)
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 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}')"
end
if options[:project]
project_statement = "#{Course.table_name}.id = #{options[:project].id}"
project_statement << " OR (#{Course.table_name}.lft > #{options[:project].lft} AND #{Course.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
base_statement = "(#{project_statement}) AND (#{base_statement})"
end
if user.admin?
base_statement
else
statement_by_role = {}
unless options[:member]
role = user.logged? ? Role.non_member : Role.anonymous
if role.allowed_to?(permission)
statement_by_role[role] = "#{Course.table_name}.is_public = #{connection.quoted_true}"
end
end
if user.logged?
user.projects_by_role.each do |role, projects|
if role.allowed_to?(permission) && projects.any?
statement_by_role[role] = "#{Course.table_name}.id IN (#{projects.collect(&:id).join(',')})"
end
end
end
if statement_by_role.empty?
"1=0"
else
if block_given?
statement_by_role.each do |role, statement|
if s = yield(role, user)
statement_by_role[role] = "(#{statement} AND (#{s}))"
end
end
end
"((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
end
end
end
end

View File

@ -0,0 +1,63 @@
<% content_for :header_tags do %>
<%= auto_discovery_link_tag(:atom, {:action => 'index', :format => 'atom', :key => User.current.rss_key}) %>
<% end %>
<div class="top-content">
<%= form_tag(projects_search_path, :method => :get) do %>
<table width="940px">
<tr>
<td class="info_font" style="width: 220px; color: #15bccf"><%= l(:label_project_deposit) %></td>
<td class="location-list"><strong><%= l(:label_user_location) %> :</strong></td>
<td rowspan="2">
<% if User.current.logged? %>
<%= link_to(l(:label_project_new), {:controller => 'projects', :action => 'new', :course => 0, :project_type =>( @project_type||=0)}, :class => 'icon icon-add') if User.current.allowed_to?(:add_project, nil, :global => true) %>
<% end %>
</td>
<td rowspan="2" >
<div class="project-search" style="float: right">
<%= text_field_tag 'name', params[:name], :size => 30 %>
<%= hidden_field_tag 'project_type', params[:project_type] %>
<%= submit_tag l(:label_search), :class => "enterprise", :name => nil %>
</div>
</td>
</tr>
<tr>
<td style="padding-left: 8px"><a><%= link_to request.host()+"/projects", :controller => 'projects', :action => 'index', :project_type => 0 %> </a></td>
<td ><%=link_to l(:field_homepage), home_path %> > <%=link_to l(:label_project_deposit), :controller => 'projects', :action => 'index', :project_type => 0 %></td>
</tr>
</table>
<% end %>
</div>
<%= sort_courses(@s_type) %>
<div id="projects-index">
<%= render_course_hierarchy(@projects)%>
</div>
<div class="pagination">
<ul>
<%= pagination_links_full @project_pages, @project_count %>
</ul>
</div>
<% if User.current.logged? %>
<p style="text-align:right;">
<span class="my-project"><%= l(:label_my_projects) %></span>
</p>
<% end %>
<% other_formats_links do |f| %>
<%= f.link_to 'Atom', :url => {:key => User.current.rss_key} %>
<% end %>
<% content_for :sidebar do %>
<%= form_tag({}, :method => :get) do %>
<h3><%= l(:label_project_plural) %></h3>
<label for="closed"><%= check_box_tag 'closed', 1, params[:closed] %> <%= l(:label_show_closed_projects) %></label>
<p>
<%= submit_tag l(:button_apply), :class => 'button-small', :name => nil %>
</p>
<% end %>
<% end %>
<% html_title(l(:label_project_plural)) -%>

View File

@ -539,7 +539,7 @@ RedmineApp::Application.routes.draw do
# add by nwb
# 课程路由设置
resources :course do
resources :courses do
member do
# 之前在项目
post 'finishcourse'